Reserve provider requests inside the durable worker envelope
Some checks failed
Governed runtime contract / contract (push) Failing after 31s

Assistant: codex
Assistant-Model: gpt-5.6-luna
Assistant-Session: 01a07ff8-19d0-7820-b4d0-1353833cb7fc
This commit is contained in:
tegwick 2026-09-09 21:50:12 +02:00
parent 4ae245a88f
commit c30806b968
13 changed files with 1114 additions and 12 deletions

View file

@ -385,9 +385,12 @@ class SpendLedger:
try:
cost = amount(evidence.get("cost_usd"), positive=False)
except SpendAdmissionError:
with self._db() as db:
self._close_requests(db, run_id)
return False # Unknown remains held, even for apparent success.
with self._db() as db:
day = self._clock(db, now or datetime.now(UTC), admission=False)
requests_resolved = self._close_requests(db, run_id)
row = db.execute(
"SELECT * FROM reservations WHERE run_id=?", (run_id,)
).fetchone()
@ -405,6 +408,7 @@ class SpendLedger:
and evidence.get("session_cleanup") == "succeeded"
and evidence.get("sandbox_destroy") == "succeeded"
and not breached
and requests_resolved
)
db.execute(
"UPDATE reservations SET liability=?, observed_usd=?, state=?, end_day=? WHERE run_id=?",
@ -420,6 +424,28 @@ class SpendLedger:
db.execute("UPDATE envelope SET breached=1")
return complete
@staticmethod
def _close_requests(
db: sqlite3.Connection, run_id: str, *, reconcile: bool = False
) -> bool:
# Existing ledgers without the opt-in request extension remain compatible.
if not db.execute(
"SELECT 1 FROM sqlite_master WHERE type='table' AND name='request_routes'"
).fetchone():
return True
db.execute("UPDATE request_routes SET revoked=1 WHERE run_id=?", (run_id,))
if reconcile:
# The existing operator reconciliation attests provider termination
# and final accounting. Full child liabilities are retained.
db.execute(
"UPDATE request_reservations SET state='charged' WHERE run_id=?",
(run_id,),
)
return not db.execute(
"SELECT 1 FROM request_reservations WHERE run_id=? AND state='held'",
(run_id,),
).fetchone()
def reconcile(
self, run_id: str, *, cost_usd: str, receipt: str, now: datetime | None = None
) -> None:
@ -446,6 +472,7 @@ class SpendLedger:
raise SpendAdmissionError(
"reconciliation cannot discard observed liability"
)
self._close_requests(db, run_id, reconcile=True)
liability = max(
row["liability"], converted_micros(str(cost), self.policy.eur_per_usd)
)