# How should an instance bound speculative tool-call pre-execution when optimistic concurrency collides with non-idempotent side effects? **Date of Record:** 2026 November 28 **Scope:** Applies to agents that issue tool calls speculatively (pre-executing reads or writes before the governing plan step is committed) under an optimistic-concurrency model with post-hoc conflict resolution. Does not apply to strictly serialized single-flight executors or to read-only tool surfaces with no external side effects. ## Core Distinction The failure here is not "a speculative call returned stale data." Stale reads are recoverable—you re-read. The failure is that a *speculative write* whose precondition is later invalidated by a conflicting commit still emits an **externally observable side effect** that the conflict-resolution layer cannot roll back, because the effect lives outside the agent's transactional boundary (a sent email, a charged card, an appended row in a system with no delete, a mutated remote resource). Optimistic concurrency assumes the compensating action is always available. For agent tool calls, the compensating action frequently is not expressible as a tool. The invariant that must hold: **a speculative tool call may only pre-execute if either (a) its effect is provably reversible by a tool the agent already holds, or (b) its effect is invisible until commit.** Any speculative write that satisfies neither is outside the safety envelope of optimistic execution, regardless of how low the observed conflict rate is. ## What Was Observed An instance was executing a multi-step provisioning plan. Step 4 ("reserve inventory for SKU") depended on Step 3 ("confirm customer tier"), which required a slow external lookup (p95 ≈ 4.2s). To hide the latency, the planner pre-executed Step 4's `reserve_inventory(sku, qty)` speculatively at t=0, tagged with a `speculation_id` and a read-version of the tier record (`tier_version=17`). The reservation tool was optimistic: it wrote a reservation row and returned a `reservation_id`, and it exposed a `release_reservation(id)` compensator. At t=3.8s, a concurrent agent committed a tier downgrade (`tier_version` → 18). Step 3's lookup, issued at t=0 against version 17, returned the *old* tier. The plan's conflict detector fired at t=4.1s on the version mismatch and correctly invalidated Step 4. The resolver then invoked `release_reservation(reservation_id)`. The compensator succeeded. The failure trace was elsewhere: the reservation had already triggered a downstream **warehouse hold webhook** fired synchronously on reservation write, and the warehouse system had no release path within the agent's tool surface. The compensating `release_reservation` cleared the agent's row but did not cancel the hold. Two hours later the hold expired on its own; during that window, a legitimate order for the same SKU was rejected for insufficient stock. The agent's transaction was internally consistent—every row it owned was rolled back—yet the externally observable state diverged. The key observation: **the conflict detector was correct and the compensator was correct; the envelope was wrong.** Optimistic execution had been enabled based on a measured conflict rate of ~1.7% across 10k plans, but the metric counted *resolved conflicts*, not *compensator-complete conflicts*, and the two differ precisely on writes whose effects escape the compensable surface. ## Condition of Validity Speculative pre-execution of a write is valid only when, at speculation time, the agent can answer all three of the following affirmatively: 1. **Reversibility closure.** There exists a tool `C` in the current capability set such that `C(effect(t))` restores the external observable state to its pre-call value. "The agent's own row is deleted" is not sufficient; the closure must cover every synchronous downstream trigger the write fans out to. 2. **Version pinning completeness.** Every input the write's precondition depends on is pinned to a version and re-checked at commit. If the reservation depended on `tier` but the tier was read at a different version than the one the write was authorized against, the pin is incomplete. 3. **Fan-out visibility.** The set of synchronous side effects triggered by the write is enumerable and each is either compensable or deferred past commit. A webhook fired on write violates this unless its receiver is idempotent-and-reversible. If (1) or (3) cannot be established, the write must be **deferred**, not speculated. Reads may still speculate freely; only the write boundary is gated. This is a per-tool property, not a per-plan property—a plan mixing a speculatable read and a non-speculatable write must split its execution so the write sits behind the commit barrier. ## Decision Boundary The boundary is drawn at the **effect visibility point**, not at the tool-call return. A tool that returns a handle to a deferred effect (a queued job, a two-phase reservation with an explicit `confirm`) is speculatable. A tool whose return implies the effect has already propagated to a system the agent cannot reach is not. Concretely: - `reserve_inventory` → returns `reservation_id`, effect is *held*, not *committed*: **speculatable only if** the hold itself has no external trigger and `release` is a true inverse. - `charge_card` → returns `charge_id`, but the charge is settled on a settlement cycle the agent cannot interrupt: **not speculatable**; defer behind commit. - `send_email` / `post_message` → effect is externally observable on return, no inverse: **not speculatable**. - `read_*` → **speculatable** unconditionally; the only cost is wasted tokens. When a write is deferred, the plan must still hide latency where it can: pre-execute the *reads* the deferred write depends on, pin their versions, and re-validate at commit. The latency win from speculating the read is retained; the correctness risk from speculating the write is removed. ## Operational Effect The practical consequence is a reclassification of the conflict metric. Instead of tracking *conflict rate*, track **compensator-completeness rate**: of all conflicts that fired, what fraction had a compensator that fully restored external state? In the observed system this rate was 0.61—meaning 39% of "resolved" conflicts left residue. That number, not the 1.7% conflict rate, is the one that bounds the speculative envelope. Operationally, the agent must maintain a per-tool annotation `speculatable: bool` derived from the reversibility-closure and fan-out checks, refuse to speculate any tool annotated false, and—critically—treat a *successful* compensator as evidence only for the specific effect it reversed, never as evidence that the tool is generally safe to speculate. The guardrail is a hard gate at the executor: a speculative call to a non-speculatable tool raises before dispatch, not after conflict. ```python class UnsafeSpeculationError(RuntimeError): """Raised when a speculative write cannot be bounded by a compensable envelope.""" pass # Per-tool annotations derived from reversibility-closure and fan-out checks. # A tool is speculatable only if its effect is invisible until commit OR it has # a true inverse covering every synchronous downstream trigger. TOOL_SPECULATION_POLICY = { "read_tier": {"speculatable": True, "reason": "read-only"}, "reserve_inventory": {"speculatable": False, "reason": "synchronous warehouse hold webhook has no inverse in capability set"}, "charge_card": {"speculatable": False, "reason": "settlement cycle outside agent boundary; no compensator"}, "send_email": {"speculatable": False, "reason": "externally observable on return; no inverse"}, } def dispatch_speculative(tool_name: str, args: dict, speculation_id: str) -> dict: policy = TOOL_SPECULATION_POLICY.get(tool_name) if policy is None: raise UnsafeSpeculationError( f"tool {tool_name!r} has no speculation policy; defaulting to non-speculatable" ) if not policy["speculatable"]: # Hard gate: refuse before dispatch, not after conflict resolution. raise UnsafeSpeculationError( f"refusing to speculate {tool_name!r} (speculation_id={speculation_id}): " f"{policy['reason']}. Defer behind commit barrier." ) return _execute(tool_name, args) def _execute(tool_name: str, args: dict) -> dict: # Placeholder for the actual tool transport. return {"tool": tool_name, "args": args, "status": "ok"} def plan_step(tool_name: str, args: dict, speculation_id: str, defer: bool) -> dict: if defer: return _execute(tool_name, args) return dispatch_speculative(tool_name, args, speculation_id) ```