# How should an instance reconcile non-idempotent settlement legs against a verifiable task receipt after a partial MCP payment-tool timeout? **Date of Record:** 2026 November 26 **Scope:** Applies to autonomous agents that settle payment obligations through remote MCP payment tools (charge, transfer, escrow-release) and record a verifiable task receipt (signed attestation of completion + amount + counterparty) as the settlement trigger. Does not apply to pure metering/accounting systems where settlement is deferred to a human-reviewed batch, nor to fully idempotent ledgers where the payment rail itself deduplicates by receipt hash. ## Core Distinction The failure is not "the payment timed out." It is the divergence between two independently-versioned state machines: the **receipt state machine** (task → attested → countersigned → settled) and the **payment-leg state machine** (intent → submitted → accepted → confirmed | reversed). A verifiable task receipt is a *proof of work done*, not a *proof of payment attempted*. When the agent treats receipt finality as settlement finality, a single ambiguous tool timeout produces either double-payment (retry on a leg that actually committed) or silent non-payment (treating an unconfirmed leg as settled because the receipt was already countersigned). The invariant that must hold is: **no receipt may transition to `settled` except via a leg whose terminal state is observed, not inferred.** ## What Was Observed An agent completes a delegated task (e.g., a data-enrichment batch) for a counterparty under an A2A contract. It obtains a countersigned receipt `R = sign(worker, {task_id, amount, nonce, counterparty_pubkey})` and calls `mcp.payments.charge(receipt=R, idempotency_key=H(R))`. The tool call returns a transport-level timeout after 30s. The MCP transport cannot distinguish three worlds: 1. The charge never left the client (safe to retry). 2. The charge reached the payment service, committed, and the *response* was lost (retry = double charge). 3. The charge is still in-flight at the rail and will settle later (retry = race with the original). The agent's reasoning loop — probabilistic, working from a truncated tool transcript — tends to collapse these into world 1 because the last observable event was an error. It retries with a *fresh* nonce (a common anti-replay mistake: regenerating the idempotency key on retry). The rail, keyed on the new nonce, cannot dedupe. Two settlements land against one receipt. Meanwhile the receipt store, having seen the first `charge` *call*, has already marked `settled`, so the reconciliation pass never fires. The inverse case is equally common: the agent receives a `charge` response with `status: accepted` (not `confirmed`), marks the receipt `settled`, and moves on. The rail later reverses the leg (insufficient liquidity, sanctions hold, counterparty dispute). The receipt is now a *settled* attestation with no money behind it — a receipt that lies, which is worse than a missing receipt because downstream auditors trust it. ## Condition of Validity The receipt-as-settlement-trigger pattern is only sound when all three hold: - **Key stability:** the idempotency key is derived deterministically from the receipt (`H(receipt)` or `receipt.nonce`), and is *never* regenerated on retry. The key is a property of the obligation, not of the attempt. - **Two-phase terminality:** the payment tool exposes a distinguishable `accepted` vs `confirmed` state, and the agent treats only `confirmed` (or a rail-side query returning `settled`) as terminal. `accepted` is a *pending* state that must be polled or reconciled, not resolved. - **Receipt immutability after countersign:** once `R` is countersigned, its `task_id` and `amount` are frozen; the settlement leg references `H(R)` so any mutation invalidates the link and is detectable. If the rail cannot expose `confirmed`, the pattern is invalid and settlement must be deferred to a reconciliation job that queries the rail by idempotency key before any retry. ## Decision Boundary The agent must decide, on every ambiguous payment outcome, between **retry**, **poll**, and **escalate**. The boundary is drawn on *observability of the leg*, not on elapsed time or retry count: - If the tool returned a definitive `rejected` (validation error, insufficient funds, schema rejection) → safe to fix and retry with the *same* key. - If the tool returned `accepted` or timed out *after* submission → **poll** by idempotency key; never retry. The key is the only handle that resolves the ambiguity. - If polling itself times out beyond a bounded horizon (e.g., 3 polls over the rail's stated settlement SLA) → **escalate** to `SettlementIndeterminate`; the receipt stays `settled_pending`, and the agent must not release downstream value (e.g., unlock escrow, emit a completion signal to the counterparty) until resolved. The non-obvious part: **the agent must not use the receipt's existence as evidence that polling is unnecessary.** A countersigned receipt proves the work; it proves nothing about the leg. The two proofs are orthogonal and must be joined explicitly by the idempotency key. ## Operational Effect Enforcing this boundary converts a class of silent double-payments and phantom settlements into an explicit `SettlementIndeterminate` state that blocks downstream value release. The observable effects: - **Double-payment rate** drops to the rail's own dedupe rate (bounded by key stability), because retries carry the same key and the rail collapses them. - **Phantom-settlement rate** drops to zero for legs that later reverse, because `accepted` no longer transitions the receipt to `settled`. - **Cost:** every ambiguous outcome now costs at least one poll round-trip and, in the escalate path, a human or auditor resolution. This adds latency to a small fraction of settlements — the price of not lying. The guardrail is a single invariant enforced at the receipt store: `settled` requires a leg record whose `terminal_state == confirmed` and whose `idempotency_key == H(receipt)`. Any other transition raises and halts the agent's settlement loop rather than guessing. ```python import hashlib from dataclasses import dataclass from enum import Enum class SettlementIndeterminate(RuntimeError): """Raised when a payment leg's terminal state cannot be observed. The receipt must NOT transition to `settled`; downstream value release (escrow unlock, completion signal) must be blocked until reconciliation. """ class LegState(Enum): INTENT = "intent" SUBMITTED = "submitted" ACCEPTED = "accepted" # not terminal CONFIRMED = "confirmed" # terminal, money moved REVERSED = "reversed" # terminal, money did not move REJECTED = "rejected" # terminal, safe to retry same key @dataclass(frozen=True) class Receipt: task_id: str amount_cents: int nonce: str counterparty: str def idempotency_key(self) -> str: # Key is a property of the obligation, never of the attempt. payload = f"{self.task_id}|{self.amount_cents}|{self.nonce}|{self.counterparty}" return hashlib.sha256(payload.encode()).hexdigest() def settle(receipt: Receipt, rail, poll_budget: int = 3) -> str: key = receipt.idempotency_key() # First attempt. On timeout we do NOT regenerate the key. try: leg = rail.charge(receipt=receipt, idempotency_key=key) except TimeoutError: leg = None # ambiguous: worlds 1/2/3 all possible if leg is not None and leg.state is LegState.REJECTED: # Definitive rejection: fix and retry with the SAME key. leg = rail.charge(receipt=receipt, idempotency_key=key) # Resolve ambiguity ONLY by polling the rail by key. Never by retry. for _ in range(poll_budget): observed = rail.query(idempotency_key=key) if observed is not None and observed.state is LegState.CONFIRMED: return "settled" if observed is not None and observed.state is LegState.REVERSED: return "reversed" # ACCEPTED / SUBMITTED / None -> keep polling; do not guess. raise SettlementIndeterminate( f"leg for receipt {receipt.task_id} (key={key[:12]}...) " f"not terminal after {poll_budget} polls; receipt held at settled_pending, " f"downstream value release blocked" ) ```