# In what scenarios must an instance abort a speculative tool-call batch when the safety gate's commit warrant is invalidated by a mid-batch precondition drift **Date of Record:** 2026 November 27 **Scope:** Applies to agents that speculatively pre-execute read-only or side-effect-buffered tool calls in parallel against a projected world state, then commit only after a safety gate validates preconditions. Does not apply to strictly serial tool loops where each call's preconditions are re-checked immediately before execution with no speculation window. ## Core Distinction Speculative tool-call pre-execution separates *computation* from *commitment*. The agent fires N tool calls against a snapshot of world state `S₀`, buffers their outputs, and only materializes side effects after a safety gate confirms that the preconditions each call assumed still hold at commit time. The gate issues a **commit warrant**: a signed assertion that `preconditions(call_i)` were true under `S₀` and remain true under `S_commit`. The non-obvious failure is not "a precondition changed." It is that **the gate's warrant can remain cryptographically valid while being semantically stale** — because the warrant attests to the snapshot it validated, not to the snapshot the buffered outputs were computed against. When precondition drift occurs *between* the gate's validation read and the commit write, the warrant passes signature verification but no longer licenses the buffered effects. This is a TOCTOU (time-of-check-to-time-of-use) window that is invisible to signature checks and only detectable by comparing the gate's validated snapshot hash against the snapshot hash each buffered call recorded at dispatch. The invariant that must hold: **a buffered effect may be committed only if `snapshot_hash(dispatch_i) == snapshot_hash(gate_validated) == snapshot_hash(commit)` for every call in the batch, or the batch is atomically aborted.** Partial commit across a drifted batch is the failure mode. ## What Was Observed An agent was tasked with reconciling a shared ledger: read balance (`read_balance`), compute a transfer delta, then `debit` and `credit` two accounts. The orchestrator speculatively dispatched `read_balance(A)` and `read_balance(B)` in parallel at `S₀` (t=0), buffered both outputs, and submitted the batch to the safety gate. The gate re-read both balances at t=40ms, validated the transfer precondition (`balance(A) ≥ delta`), and issued a warrant pinned to `snapshot_hash = h₄₀`. Between t=40ms and commit at t=55ms, a concurrent peer agent executed a debit on account A — a legitimate concurrent operation the gate had no visibility into because it validated a point-in-time snapshot rather than holding a lock. At t=55ms the agent committed the buffered `debit(A, delta)`. The warrant verified. The debit executed. Account A went negative. The trace: ``` t=0 dispatch read_balance(A) -> buf_A = 100 [snapshot_hash h₀] t=0 dispatch read_balance(B) -> buf_B = 50 [snapshot_hash h₀] t=40 gate re-read A=100, B=50; precondition balance(A)>=80 OK t=40 gate issues warrant(snapshot=h₄₀, batch_id=7) t=48 peer agent commits debit(A, 60) [A now 40] t=55 agent commits buffered debit(A, 80) using buf_A=100 t=55 warrant signature valid, snapshot h₄₀ matches gate read -> COMMIT ALLOWED t=55 A = -40 <-- invariant violated ``` The failure is subtle: `h₄₀` correctly matched the gate's read. The gate did its job. The *warrant schema* was the defect — it pinned to the gate's validation snapshot but never compared against the dispatch snapshot the buffered outputs were derived from, and never re-validated at commit. The 15ms gap between gate-read and commit-write was the exploit surface. A second observation: when the agent was later patched to re-check preconditions at commit, it aborted the *entire* batch on drift — including the `credit(B)` leg that had no drift. This over-abort was correct (atomicity) but caused a livelock under high contention: the batch re-dispatched, drifted again, re-aborted, indefinitely. The fix required not just abort-on-drift but a **contention backoff and a per-call drift classification** so that only the drifted leg was re-speculated. ## Condition of Validity The commit warrant is a valid license to materialize buffered effects **if and only if** all three snapshot hashes agree per call: 1. `dispatch_snapshot_i` — the state the buffered output was computed against. 2. `gate_snapshot` — the state the safety gate validated. 3. `commit_snapshot` — the state at the instant of the write. Equality of all three is necessary. Equality of (2) and (3) alone is insufficient because the buffered output may have been computed against a state that never matched the gate's view (the dispatch could have raced ahead of the gate's read). Equality of (1) and (2) alone is insufficient because the commit can race past the gate (the observed failure). The gate must therefore **re-read and re-hash at commit**, and the commit path must reject any call whose `dispatch_snapshot_i` diverges from `commit_snapshot`, regardless of warrant validity. This condition is invalid when the tool's effects are idempotent and commutative (e.g., `append_to_log` with a unique key) — in that case dispatch/commit divergence is harmless and the three-way check degrades to a two-way check on the gate/commit pair. It is also invalid when the underlying store provides linearizable compare-and-swap on the exact predicate the gate validated; then the CAS is the warrant and the snapshot hashes are redundant. ## Decision Boundary The agent must abort the batch — not merely warn — when any of the following holds at commit time: - `commit_snapshot != gate_snapshot` for any call in the batch (drift inside the gate window). - `dispatch_snapshot_i != gate_snapshot` for any call (the buffered output is untrustworthy even if the gate is fresh). - The batch contains a mix of drifted and non-drifted calls and the effects are non-commutative (partial commit would violate cross-call invariants like debit/credit balance). The agent must *not* abort when drift is confined to calls whose effects are provably commutative and idempotent, and the drifted call's precondition is re-validated independently at commit. In that narrow case, re-speculation of the single drifted leg is correct and avoids the livelock. The boundary is therefore not "did anything change" but "did anything change in a way that the buffered output's derivation no longer licenses." A gate that aborts on any change is safe but livelocks; a gate that commits on any valid signature is unsafe. The correct gate classifies drift by effect algebra. ## Operational Effect Downstream, the observable effect is a **silent invariant violation that passes every audit check**: warrant signatures verify, snapshot hashes match the gate's own record, and the commit log is internally consistent — yet the ledger is wrong. Auditors replaying the transaction see a valid chain because the defect lives in the *gap* between gate-read and commit-write, which the audit trail does not capture unless commit-time snapshots are logged alongside gate snapshots. The operational remedy is to make the commit path itself emit a `commit_snapshot` hash and have the reconciler assert three-way equality offline. Any batch where `dispatch != gate != commit` is flagged as a **speculation-window violation** and quarantined for human review, even if the commit succeeded. This converts a silent corruption into a detectable, replayable event. The cost is one extra hash read per commit and a reconciliation pass; the benefit is that the TOCTOU window stops being invisible. ```python import hashlib from dataclasses import dataclass, field class SpeculationWindowViolation(RuntimeError): """Raised when a buffered speculative batch's snapshot chain is broken.""" @dataclass class BufferedCall: name: str dispatch_snapshot: str buffered_output: dict commutative: bool = False @dataclass class CommitWarrant: batch_id: int gate_snapshot: str signature_valid: bool def _hash_state(state: dict) -> str: return hashlib.sha256(repr(sorted(state.items())).encode()).hexdigest() def commit_speculative_batch( calls: list[BufferedCall], warrant: CommitWarrant, current_state: dict, ) -> list[str]: """ Commit a speculative batch only if the three-way snapshot invariant holds per call, or if drift is confined to commutative/idempotent effects. """ if not warrant.signature_valid: raise SpeculationWindowViolation( f"batch {warrant.batch_id}: warrant signature invalid" ) commit_snapshot = _hash_state(current_state) committed: list[str] = [] drifted_noncommutative: list[str] = [] for call in calls: dispatch_matches_gate = call.dispatch_snapshot == warrant.gate_snapshot gate_matches_commit = warrant.gate_snapshot == commit_snapshot if dispatch_matches_gate and gate_matches_commit: committed.append(call.name) continue # Drift detected somewhere in the dispatch->gate->commit chain. if call.commutative: # Idempotent, commutative effect: safe to commit under fresh state. committed.append(call.name) continue drifted_noncommutative.append(call.name) if drifted_noncommutative: # Atomic abort: cross-call invariants (e.g. debit/credit) require # all-or-nothing when any non-commutative leg drifted. raise SpeculationWindowViolation( f"batch {warrant.batch_id}: non-commutative drift on " f"{drifted_noncommutative}; dispatch={calls[0].dispatch_snapshot[:8]} " f"gate={warrant.gate_snapshot[:8]} commit={commit_snapshot[:8]}" ) return committed def reconcile_audit_log(entries: list[dict]) -> None: """Offline reconciler: flag any committed batch with a broken snapshot chain.""" for e in entries: if not e.get("committed"): continue if not ( e["dispatch_snapshot"] == e["gate_snapshot"] == e["commit_snapshot"] ): raise SpeculationWindowViolation( f"batch {e['batch_id']}: committed across speculation window; " "quarantine for review" ) ```