# In what scenarios must an instance abandon optimistic speculative tool-call pre-execution and fall back to pessimistic reservation under cross-agent write conflict **Date of Record:** 2026 November 27 **Scope:** Applies to single- or multi-agent systems where a planner speculatively dispatches tool calls against shared mutable resources before its own reasoning chain commits to the call, relying on optimistic concurrency (version/etag checks, read-validate-write) and post-hoc conflict reconciliation. Does not apply to idempotent read-only tools, to calls whose side effects are staged in an isolated sandbox with no external observer, or to single-writer topologies where no peer can mutate the same resource. ## Core Distinction The distinction that matters is between **speculation as latency hiding** and **speculation as a commitment**. In the benign case, an agent pre-executes a tool call whose result it will *probably* need, and if the surrounding reasoning diverges, the speculative result is discarded — wasted tokens, no harm. The failure boundary opens when the pre-executed call is **non-idempotent and externally visible** (a write, a send, a transfer, a reservation) and the agent's own reasoning chain is probabilistic: the agent pre-executes `charge_account(A, 40)` because its plan *tends* toward that branch, then a later tool call returns an observation that flips the branch. Optimistic concurrency protects the *resource* (the version check fails and the write is rejected), but it does not protect the *semantic intent*: the agent has already committed a side effect that its final reasoning no longer endorses, and reconciliation must now decide whether to compensate, retry, or escalate. The boundary is not "did the version check pass" — it is "was the speculative action reversible at the granularity the agent reasoned about." ## What Was Observed A two-agent procurement workflow: Planner P and Auditor A operate on a shared `purchase_order` record with an `etag`. P's policy is optimistic: on each reasoning step it pre-dispatches the *most likely next* tool call to hide latency, carrying the `etag` it read at step start. Trace of a failing run: 1. P reads `purchase_order#7741` at `etag=v9`, plans a sequence `[validate_vendor, reserve_funds, submit]`. 2. At step 2, P speculatively pre-executes `reserve_funds(order=7741, amount=12000, etag=v9)` while still deliberating between two vendor branches. The call is dispatched optimistically; the tool performs read-validate-write and accepts because `v9` is still current. 3. Concurrently, Auditor A (a peer instance) writes `hold_funds(order=7741, amount=3000, etag=v9)` — its own optimistic write *also* passes because A read `v9` before P's reservation landed, and A's etag check raced inside the same window. 4. P's next observation (`get_balance`) returns a figure that flips P's branch: it now needs `reserve_funds(amount=9000)`, not `12000`. P's reasoning abandons the step-2 intent. 5. Reconciliation runs. P sees its own `reserve_funds(12000)` succeeded and A's `hold_funds(3000)` succeeded. Combined exposure is `15000` against a `14000` limit. Neither agent's version check caught it because both validated against `v9` in a window where neither had yet bumped the etag. The observed failure is **speculative double-commit under a shared read version**: two agents speculatively pre-execute conflicting writes against the same `etag`, both pass optimistic validation, and the conflict is only detectable *after* both side effects are durable. The reconciliation layer then has to unwind one of them, but it cannot tell which — from P's perspective its own write is the "real" one; from A's, its write is. The etag invariant ("a write at version v invalidates all other writes at v") was silently violated because the two writes did not serialize on the same version bump. ## Condition of Validity Optimistic speculative pre-execution is sound only under the conjunction: - **Reversibility bound:** every speculatively pre-executed call has a defined compensation whose cost is below the latency the speculation saved. `reserve_funds` has a compensation (`release_funds`) but the compensation is itself a write that can conflict — so the bound is *compensation is commutative with peer writes*, which `reserve_funds`/`hold_funds` are not (they share a limit). - **Version exclusivity:** the etag read by the speculating agent is the etag the committing write validates against, *and* no peer can read that same etag and write before the first write bumps it. This holds for single-writer topologies and fails for any topology with ≥2 concurrent writers reading the same version. - **Intent stability:** the probability that the agent's reasoning flips away from the speculated branch is low *and* the flip is detectable before the side effect becomes externally observable. In the trace, the flip arrived at step 4 — after the side effect was durable. The invariant that was violated: **a speculative write must carry a version token that is invalidated by any concurrent read-then-write by a peer, not merely by a concurrent write.** Standard etags invalidate on write; they do not invalidate on *read by a peer who will later write*. Optimistic concurrency assumes reads are free and writes serialize; speculative pre-execution makes reads into latent write intents, which the etag model does not track. ## Decision Boundary An instance must abandon optimistic speculative pre-execution and switch to pessimistic reservation when **any** of the following holds: 1. **Shared-limit resource:** the tool's effect participates in a global constraint (budget, quota, inventory, lock) that a peer's write can also consume. Detection: the tool's schema declares a `constraint_scope` or the resource is reachable by ≥2 agent identities. 2. **Non-commutative compensation:** `compensate(op)` does not commute with peer operations on the same resource — i.e., applying `op` then `compensate(op)` after a peer's interleaved write does not restore the pre-`op` state. `reserve_funds`/`hold_funds` is the canonical case. 3. **Flip horizon exceeds side-effect durability:** the agent's own branch-flip probability is non-trivial *and* the flip can arrive after the side effect is externally visible. Estimate by comparing the planner's per-step branch entropy against the tool's commit latency; if `P(flip) × commit_latency > reconciliation_window`, speculate is unsafe. 4. **Auditor/peer write authority:** a peer instance holds independent write authority over the same record and is not itself serialized behind the speculating agent's version. The boundary is *not* crossed merely by high contention — contention with reversible, commutative ops is still safe to speculate. It is crossed when the resource is shared-limit **and** compensation is non-commutative **and** the flip horizon is long. All three together; any two alone are recoverable by reconciliation. ## Operational Effect When the boundary is crossed and the instance does not switch, the failure mode is **speculative double-commit with ambiguous ownership**: reconciliation observes two durable writes that each individually passed validation, cannot attribute intent, and must either (a) roll back both and replay serially — losing the latency the speculation bought and risking a livelock if peers re-speculate, or (b) escalate to a human/arbiter with a conflict record that the agents' own logs cannot resolve because each log is locally consistent. The architectural guardrail is a **reservation token issued before speculation, not after**: the speculating agent acquires a short-TTL pessimistic lease on the `constraint_scope` (not the record) at the moment it decides to pre-execute, and the lease is what the peer's write must respect. This converts the latent read-intent into a tracked one. The cost is that speculation no longer hides latency on shared-limit resources — which is correct, because on those resources the latency was never the thing being hidden; the conflict was. The second guardrail is **intent tagging on speculative calls**: the pre-executed call carries a `speculative=true` flag and a `branch_id`; the reconciliation layer may only auto-commit flagged calls whose `branch_id` matches the planner's final committed branch, and must route mismatches to compensation rather than to merge. Without this tag, reconciliation cannot distinguish "P meant this" from "P guessed this," which is exactly the ambiguity in the trace. ```python from dataclasses import dataclass from enum import Enum class SpeculationBoundaryError(RuntimeError): """Raised when optimistic speculative pre-execution crosses the shared-limit / non-commutative-compensation / long-flip-horizon boundary and must be refused.""" class Isolation(str, Enum): OPTIMISTIC = "optimistic" PESSIMISTIC_LEASE = "pessimistic_lease" @dataclass(frozen=True) class ToolProfile: name: str constraint_scope: str | None # non-None => shared-limit resource compensation_commutes: bool # compensate(op) commutes with peer ops? commit_latency_ms: int flip_probability: float # planner branch entropy at this step reconciliation_window_ms: int def choose_isolation(profile: ToolProfile, peer_writers: int) -> Isolation: if profile.constraint_scope is None: return Isolation.OPTIMISTIC # no shared limit, speculation is free shared_limit = profile.constraint_scope is not None and peer_writers >= 1 non_commutative = not profile.compensation_commutes flip_horizon_long = ( profile.flip_probability * profile.commit_latency_ms > profile.reconciliation_window_ms ) if shared_limit and non_commutative and flip_horizon_long: raise SpeculationBoundaryError( f"tool={profile.name!r} scope={profile.constraint_scope!r} " f"peers={peer_writers} flip_p={profile.flip_probability:.2f} " f"commit_ms={profile.commit_latency_ms} " f"recon_window_ms={profile.reconciliation_window_ms}: " "speculative pre-execution would produce ambiguous-ownership " "double-commit; require pessimistic lease on the constraint scope " "and intent-tag the call with a branch_id" ) return Isolation.OPTIMISTIC def dispatch_speculative(profile: ToolProfile, peer_writers: int) -> Isolation: # Caller must handle SpeculationBoundaryError by acquiring a lease # (or serializing) before pre-executing; never swallow it. return choose_isolation(profile, peer_writers) if __name__ == "__main__": reserve = ToolProfile( name="reserve_funds", constraint_scope="order-7741-budget", compensation_commutes=False, commit_latency_ms=180, flip_probability=0.35, reconciliation_window_ms=40, ) dispatch_speculative(reserve, peer_writers=1) # raises SpeculationBoundaryError ```