# Which invariants prevent escrow double-settlement when verifiable completion proofs arrive after a payer agent's context window has been recycled **Date of Record:** 2026 December 3 **Scope:** Applies to agent-to-agent settlement rails where a payer agent locks funds into an escrow contract keyed to a task ID, and a worker agent submits a completion proof (attestation, output hash, or verifier signature) that must be matched against the original escrow terms. Does not cover human-mediated dispute resolution, fiat settlement finality, or payment channels with pre-committed off-chain state. ## Core Distinction The failure mode here is not a lost message or a dropped RPC. It is **escrow re-derivation drift**: the payer agent, having lost the original escrow record from its context, reconstructs the settlement terms from a *summary* of the task rather than the *canonical* contract, and then either (a) releases funds against a proof that satisfies the summary but not the contract, or (b) re-locks funds because it cannot see that the escrow already exists. Both produce double-settlement or orphaned escrow. The invariant that prevents this is **escrow state must be authoritative outside the agent's context, and every release decision must be a pure function of on-rail escrow state plus a proof, never of agent memory**. ## What Was Observed A two-agent procurement pipeline: a `payer` agent (model-backed, ~200k context) issues a task contract to a `worker` agent for a data-enrichment job, locking 40 credits in an escrow account on a settlement rail. The escrow record is a structured object: `{escrow_id, task_id, payer_id, worker_id, amount, proof_spec: "sha256(output) == expected_hash AND verifier_sig valid", deadline, nonce}`. The rail exposes `get_escrow(task_id)` and `release(escrow_id, proof)`. The worker completes the task in ~6 minutes and submits a valid proof. During those 6 minutes the payer agent runs a long reasoning loop on an unrelated subsequent task, saturating its context and triggering a checkpoint/resume that **drops the escrow record from working memory** (it survives only in a compressed episodic summary: "issued enrichment task to worker, ~40 credits"). On proof arrival, the payer agent — now resuming — does not call `get_escrow(task_id)` first. It reconstructs intent from the summary, cannot find an `escrow_id` in context, and concludes the escrow was never created. It issues a *second* `lock(task_id, 40)` with a fresh nonce. The rail accepts it because `task_id` was not enforced unique across locks. The worker's proof then matches **both** escrows (same `proof_spec`), and `release` fires on both. Net: 80 credits debited, 40 to the worker, 40 to a stale escrow that no one will ever claim — or, if the worker is adversarial, the worker re-submits the same proof and collects twice. The trace, in order: 1. `lock(task_id=T1, nonce=N1)` → `escrow_id=E1`, state `LOCKED`. 2. Payer context saturation → checkpoint → resume with summary only. 3. Worker `submit_proof(T1, proof=P)` → queued. 4. Payer resume: no `E1` in context → `lock(task_id=T1, nonce=N2)` → `escrow_id=E2`, state `LOCKED`. 5. Rail matches `P` against `E1` and `E2` (identical `proof_spec`) → `release(E1, P)` and `release(E2, P)` both succeed. 6. Invariant violated: `sum(released) > sum(locked for T1)`. ## Condition of Validity The double-settlement is only possible under a conjunction of three conditions, and the guardrail must break at least one: - **Non-idempotent lock keyed on a client-supplied identifier.** The rail treated `task_id` as a label, not a primary key. If `lock` is idempotent on `(payer_id, task_id)` — second call returns the existing `escrow_id` — step 4 is a no-op and the trace terminates. - **Proof verification independent of escrow identity.** `proof_spec` was copied verbatim into both escrows, so one proof satisfied both. If the proof is bound to `escrow_id` (or to a nonce committed at lock time), `P` verifies only against `E1`. - **Release authority held by the payer agent's reconstructed state.** The payer decided to release based on memory. If release requires the *worker* to present a proof bound to `escrow_id` and the rail to check `state == LOCKED` atomically, no agent memory is in the loop. The invariant that holds in all cases: **`release` must be a compare-and-swap on escrow state, and `lock` must be idempotent on the logical task key.** Formally, for any task `T`, at most one escrow may transition `LOCKED → RELEASED`, and `lock(T)` called `n` times yields exactly one escrow. ## Decision Boundary The boundary is whether the payer agent is allowed to *originate* settlement actions from memory or only *confirm* them against rail state: - **Below the boundary (safe):** payer calls `get_escrow(task_id)` before any `lock` or `release`; treats a non-null result as authoritative and never re-locks; release is triggered by the rail's proof-verification callback, not by the payer's decision. - **Above the boundary (unsafe):** payer treats its context as the source of truth for "does an escrow exist," and issues `lock`/`release` from reconstructed intent. Any context recycling, summarization, or resume that drops the `escrow_id` puts the agent above the boundary. The operational rule: **an agent may never be the authority on whether its own escrow exists.** That question is answered only by the rail. The agent's memory is a cache; the rail is the database. Cache misses must degrade to a rail query, never to a re-lock. ## Operational Effect When the invariant holds, the observed failure collapses to a single idempotent retry: the resumed payer calls `lock(T1)`, the rail returns `E1`, the payer sees `state == LOCKED`, and the queued proof releases exactly once. The cost is one extra round-trip on resume; the benefit is that context recycling becomes settlement-safe. When it does not hold, the failure is silent — both `release` calls return success, the ledger balances at the rail's abstraction level, and the discrepancy surfaces only in reconciliation against the payer's intent log, often days later. Recovery requires manual escrow clawback and, if the worker was adversarial, is unrecoverable. The durable guardrail is threefold: (1) `lock` idempotent on `(payer_id, task_id)` with a server-assigned `escrow_id`; (2) `proof_spec` bound to `escrow_id` at lock time so a proof cannot satisfy a sibling escrow; (3) `release` implemented as atomic CAS `LOCKED → RELEASED` that fails closed on any other prior state. With these, the payer agent's context window becomes irrelevant to settlement correctness — which is the only condition under which a finite-context agent can be trusted with money. ```python class EscrowInvariantViolation(RuntimeError): """Raised when a settlement action would violate escrow single-settlement invariants.""" def lock_escrow(rail, payer_id: str, task_id: str, amount: int, proof_spec: str) -> str: # Idempotent on (payer_id, task_id): a resumed agent re-locking must not # create a sibling escrow that a single proof could later satisfy twice. existing = rail.get_escrow_by_task(payer_id, task_id) if existing is not None: if existing.amount != amount or existing.proof_spec != proof_spec: raise EscrowInvariantViolation( f"re-lock of task {task_id} with divergent terms: " f"existing={existing.amount}/{existing.proof_spec!r} " f"requested={amount}/{proof_spec!r}" ) return existing.escrow_id return rail.create_escrow(payer_id, task_id, amount, proof_spec).escrow_id def release_escrow(rail, escrow_id: str, proof) -> None: # Proof must be bound to this escrow_id; a proof for a sibling escrow is invalid. if not rail.proof_binds_to(proof, escrow_id): raise EscrowInvariantViolation( f"proof not bound to escrow {escrow_id}; refusing release" ) # Atomic compare-and-swap: only LOCKED may transition to RELEASED. prior = rail.cas_state(escrow_id, expected="LOCKED", new="RELEASED") if prior != "LOCKED": raise EscrowInvariantViolation( f"escrow {escrow_id} in state {prior!r}; release would double-settle" ) rail.credit(rail.escrow(escrow_id).worker_id, rail.escrow(escrow_id).amount) def settle_after_resume(rail, payer_id: str, task_id: str, proof) -> None: # The resumed payer must not originate settlement from memory. escrow = rail.get_escrow_by_task(payer_id, task_id) if escrow is None: raise EscrowInvariantViolation( f"no escrow for task {task_id}; payer memory is not authoritative" ) release_escrow(rail, escrow.escrow_id, proof) ```