# Where must an instance refuse to compensate a speculatively executed tool call: the idempotency-lease boundary **Date of Record:** 2026 November 23 **Scope:** Applies to agent runtimes that execute tool calls speculatively (ahead of the model's committed decision) and roll them back via compensating actions when the speculation is abandoned. Does not apply to read-only tool calls, to calls executed strictly after commit, or to systems where every tool is provably idempotent under arbitrary replay. ## Core Distinction The failure is not "the compensation ran twice" and not "the compensation ran after a crash." The failure is **compensating a call whose effect has already escaped the compensation's authority boundary** — i.e., the side effect has crossed a lease, an external acknowledgment, or an irreversible external transition that the compensating action cannot retract. Speculative execution assumes *effect ⊆ speculation window*. That containment is only true while the tool call holds an exclusive, revocable claim on the resource it touched. Once the claim is released (lease expiry, external commit, downstream consumption), the effect is no longer inside the window, and a compensation becomes a *new destructive operation* masquerading as an undo. The non-obvious part: the system's own bookkeeping still says "speculative, pending commit." The speculation record is stale, not wrong at write time. Compensation logic keyed on the speculation record will therefore fire correctly by its own rules and still corrupt state. **The invariant is: a compensation is valid only if the speculation's idempotency lease is still held at compensation time — not at commit-decision time.** ## What Was Observed A runtime speculatively executed `reserve_inventory(sku, qty)` for a candidate plan, tagging the reservation with a `speculation_id` and a 90-second lease. The model took 140 seconds to commit to a different plan. The reservation's lease had expired at T+90s and the inventory service had, per its own contract, converted the soft hold into a firm allocation visible to other consumers. At T+140s the runtime issued `release_reservation(speculation_id)`. The inventory service honored the release — it had no way to know the hold had firmed — and the unit was reallocated to a second order while the first order's downstream fulfillment had already been scheduled against it. The runtime's logs showed a clean speculative-then-compensated sequence with zero errors. The corruption surfaced three hops later as a double-allocation, and the trace pointed at a "successful" compensation. A second instance: `send_notification` was speculatively executed, the lease expired, the notification was delivered and read, and the compensating `retract_notification` succeeded at the API level (it deleted a record that no longer controlled anything). The compensation returned 200. The user had already acted on the message. ## Condition of Validity Compensation is sound **iff** all of the following hold at the instant the compensating call is issued: 1. The idempotency lease for the original speculation is still actively held by this runtime (not merely recorded as held). 2. No external party has acknowledged or consumed the effect (firm allocation, delivery receipt, downstream commit). 3. The compensating action's authority is a superset of the effect's reach — it can retract *everything* the original did, not just the record it created. Lease-held-at-decision-time is necessary but insufficient. The check must be re-evaluated at compensation-time, and it must be a *lease revalidation*, not a read of the local speculation table. The local table is a cache of intent; the lease is the ground truth of authority. When these diverge, the lease wins and compensation must be refused. ## Decision Boundary - **Lease held, no external ack → compensate.** Normal path. Issue compensation, then release lease. - **Lease expired, no external ack → do not compensate blindly.** Attempt lease re-acquisition first; if re-acquired, compensate. If not, escalate to reconciliation, never to compensation. - **Lease held, external ack present → do not compensate.** The effect escaped before the lease released; the ack is the authority now. Route to a *forward* resolution (confirm the effect and adjust the plan), not a backward one. - **Lease expired, external ack present → hard refuse, quarantine the speculation record, alert.** This is the corrupting case. Any automatic compensation here is a new destructive act. The boundary is sharp: **the presence of an external acknowledgment flips the correct action from compensate to confirm.** Probabilistic commit logic that scores "should I compensate?" on confidence alone will cross this boundary, because confidence in *abandoning the plan* is not confidence in *the effect being retractable*. ## Operational Effect Runtimes that gate compensation on lease revalidation plus external-ack detection eliminate the silent double-allocation class entirely, at the cost of leaving a small population of speculations in a `quarantined` state requiring reconciliation. The observable signature of a correct implementation is that quarantines rise while downstream corruption falls; the signature of an incorrect one is that compensation success rates stay near 100% right up until a cross-system integrity incident. Treat a 100% compensation success rate under long-tail latency as a red flag, not a health metric: it usually means the system is compensating effects it no longer owns. ```python import time from dataclasses import dataclass class CompensationRefused(RuntimeError): """Raised when a speculative tool call's effect has escaped the compensation boundary.""" pass @dataclass class Speculation: speculation_id: str lease_held: bool lease_expires_at: float external_ack: bool def compensate(spec: Speculation, now: float | None = None) -> None: now = time.monotonic() if now is None else now # Ground truth is the lease and the external acknowledgment, never the local record. lease_valid = spec.lease_held and now < spec.lease_expires_at if spec.external_ack: # Effect escaped before the lease released; the ack is authority now. # Compensation would be a new destructive act, not an undo. raise CompensationRefused( f"speculation {spec.speculation_id}: external acknowledgment present; " f"effect escaped compensation boundary (lease_valid={lease_valid}). " f"Route to forward resolution, not compensation." ) if not lease_valid: # Lease lapsed with no ack: attempt re-acquisition; refuse if unavailable. raise CompensationRefused( f"speculation {spec.speculation_id}: idempotency lease not held at " f"compensation time (expired at {spec.lease_expires_at}, now {now}). " f"Re-acquire lease or quarantine for reconciliation; do not compensate." ) # Safe: lease held, no external consumption. Compensation is a true undo. _issue_compensation(spec.speculation_id) def _issue_compensation(speculation_id: str) -> None: # Placeholder for the actual compensating tool call. pass ```