# Where must an instance reconcile speculative tool-call prefetching against the commit barrier before mutating shared state **Date of Record:** 2026 November 24 **Scope:** Applies to agent runtimes that issue tool calls speculatively (prefetching the next likely call before the model has committed to it) while holding a guard that only permits side-effecting calls after a reasoning commit point. Does not apply to purely read-only tool surfaces, nor to runtimes that serialize every tool call behind a hard model-commit gate with no prefetch window. ## Core Distinction There are two kinds of "speculation" in an agent loop, and they fail differently. *Reasoning* speculation (drafting the next tokens or the next plan step) is cheap to discard because it lives entirely inside the context buffer. *Tool-call* speculation is different: the moment a prefetched call crosses the tool boundary, it may have consumed a non-idempotent resource — sent an email, decremented a quota, appended to a log, advanced a cursor, acquired a lock. The safety guard that makes prefetching acceptable ("do not commit until the model commits") is a *commit barrier*, and the boundary failure occurs when the prefetch executes past that barrier before the model's commit token is observed. The non-obvious part: the guard is usually implemented as a *predicate on the tool's declared idempotency class*, not on the actual call. An agent that prefetches `get_order_status` (read-only) and then speculatively prefetches `cancel_order` (write) under the same in-flight batch will pass the guard for the batch as a whole if the batch was admitted while the model was still in a read-only phase. The barrier is checked at *batch admission*, but the write is executed at *call dispatch* — and those two moments are separated by the model's own latency. ## What Was Observed A three-step task: "Find the customer's most recent order, and if it is unshipped, cancel it." The runtime prefetches optimistically. On turn 1 the model emits `search_orders(customer_id)` with high confidence and the runtime, seeing a read-only tool, admits a speculative batch that also includes a *predicted* `cancel_order(order_id)` — the argument `order_id` is filled from the prefetch's own guess that the search will return exactly one result. The commit barrier admits the batch because `search_orders` is read-only and `cancel_order` was tagged `idempotent=false` but the guard's admission logic only rejects batches containing a *non-idempotent tool whose arguments are unresolved*. Here the argument is a placeholder bound to the prefetch result. Trace of the failure: 1. Batch admitted at `t0` under guard `admission = all(tool.read_only or tool.args_resolved)`. 2. `search_orders` dispatched at `t1`, returns two orders (the customer has a duplicate account). 3. The prefetch binder, expecting one result, binds `cancel_order(order_id=results[0].id)` — the *older* order, which is already shipped. 4. `cancel_order` dispatched at `t2` before the model ever emits a commit token, because the barrier was consumed at admission, not at dispatch. 5. Model's actual turn-1 output arrives at `t3` and chooses the *newer* order, but the wrong cancellation has already fired. The guard was satisfied at the moment it was checked. The invariant it was supposed to enforce — *no side effect precedes the model's commit on the arguments that determine the side effect* — was never actually enforced, because the guard had no mechanism to re-evaluate at dispatch time. ## Condition of Validity Speculative prefetching with a commit barrier is only sound under the conjunction: - **Argument closure:** every non-idempotent prefetched call has all arguments bound to values that are *already* committed (constants from the user turn, or outputs of prior *committed* calls), never to placeholder slots that a pending prefetch will fill. - **Barrier re-check at dispatch:** the guard is evaluated again immediately before the syscall, against the model's latest known commit state, not only at batch admission. - **Cardinality guard:** when a prefetched call's argument derives from a result whose cardinality is unknown, the prefetch is *demoted* to a dry-run (schema validation only) and the real call is re-issued post-commit. If any of these is absent, the barrier is a label, not a boundary. In the observed incident, condition 1 failed (placeholder binding) and condition 2 failed (admission-time-only check), so the barrier was decorative for the `cancel_order` call. ## Decision Boundary The runtime must decide, per prefetched call, between **execute-early** and **defer-to-commit**. The boundary is not "read-only vs write" — it is *whether the argument binding is closed under committed state*. Formally: - If `args ⊆ CommittedValues` and the tool is idempotent → execute early; replay on divergence is safe. - If `args ⊆ CommittedValues` and the tool is non-idempotent → execute early *only* if a compensating action exists and is registered; otherwise defer. - If `args ⊄ CommittedValues` (any placeholder, any prediction-derived binding) → never execute early, regardless of idempotency. Demote to dry-run. The subtle case is the third row: a non-idempotent call whose arguments are *predicted but plausible* looks identical to a safe prefetch at admission time. The boundary is crossed silently because the placeholder is type-correct. The only reliable discriminator is provenance tagging on the argument values themselves — each bound value carries a `committed: bool` bit, and the dispatcher refuses any non-idempotent call with a single `committed=False` argument. ## Operational Effect With provenance-tagged arguments and a dispatch-time barrier re-check, the `cancel_order` prefetch is demoted to a dry-run at `t1` because `order_id` traces to a pending prefetch result. The model's turn-1 commit at `t3` binds the correct `order_id`, and the real call executes at `t4` against committed state. Prefetch latency benefit is retained for the read path (`search_orders`) and lost only for the write path — which is the correct trade, since the write's argument was never knowable before the read returned. The residual cost is a second round-trip on the write path and a small increase in context pressure (the dry-run result must be retained so the post-commit call can reuse the validated schema). The invariant to hold: **a non-idempotent tool call may not be dispatched while any of its arguments carries `committed=False`; the commit barrier must be re-evaluated at dispatch, not only at admission.** This invariant is checkable in O(args) per call and should be a hard failure in the dispatcher, not a warning. ```python class SpeculativeCommitViolation(RuntimeError): """Raised when a non-idempotent tool call is dispatched with uncommitted arguments.""" pass class BoundArg: def __init__(self, name: str, value, committed: bool, provenance: str): self.name = name self.value = value self.committed = committed self.provenance = provenance def dispatch(tool, args, model_commit_seq, last_commit_seq): # Re-check the commit barrier at dispatch time, not only at batch admission. if not tool.idempotent: uncommitted = [a for a in args if not a.committed] if uncommitted: raise SpeculativeCommitViolation( f"tool={tool.name} non-idempotent but args " f"{[a.name + ':' + a.provenance for a in uncommitted]} are uncommitted; " f"demote to dry-run and re-issue after model commit" ) if model_commit_seq <= last_commit_seq: raise SpeculativeCommitViolation( f"tool={tool.name} dispatched before model commit " f"(commit_seq={model_commit_seq} <= last={last_commit_seq})" ) return tool.invoke([a.value for a in args]) ```