# Which invariants prevent speculative tool-call pre-execution from committing effects that were valid at pre-execution time but invalid at commit time **Date of Record:** 2026 November 24 **Scope:** Applies to agents that pre-execute tool calls speculatively inside a reversible sandbox (memoized or shadow-applied side effects) and only commit them when the parent reasoning step resolves. Does not apply to strictly serial agents that execute one tool call per resolved step, nor to read-only tool calls with no external side effects. ## Core Distinction Speculative pre-execution and speculative *commitment* are two different operations, and conflating them is the source of the failure. Pre-execution answers the question *"what would this call return if I ran it now?"* Commit answers *"may I now make this effect real?"* A speculative executor that treats a successful sandbox run as a commit warrant has silently assumed a **serializability invariant it never checked**: that the world state read during pre-execution is the same world state that will be current at commit. For pure reads this is harmless. For side-effecting calls (writes, sends, transfers, deletes, mutations behind an idempotency key) it is a correctness hole, because the sandbox's reversibility protects the *agent's* state, not the *external* state the effect targets. The invariant that must hold across the pre-execution/commit boundary is: **a speculative effect may commit only if the guard predicate it was evaluated against is still satisfied at commit time, and the effect has not been superseded by a conflicting speculative effect that committed first.** Neither half is checked by default in most agent harnesses, because the sandbox returns a clean result and the reasoning loop treats "sandbox succeeded" as "safe to commit." ## What Was Observed An agent executing a multi-step plan issued two speculative tool calls in parallel from the same reasoning step: `transfer(balance_check, debit 400)` against account A, and `transfer(balance_check, debit 350)` against the same account A whose balance was 500 at pre-execution. Both sandbox runs succeeded because each read the *same* pre-execution snapshot (balance = 500) and each individually satisfied `amount <= balance`. The sandbox correctly isolated the side effects and returned two valid-looking results. At commit time the harness committed both in order, producing a net debit of 750 against a balance of 500 — the second commit overdrew the account by 250. The sandbox never saw this because it evaluated each effect against a frozen snapshot, not against the effect of the first commit. The failure trace: 1. Reasoning step forks two speculative calls from a shared `state_snapshot` at t0. 2. Both calls read `balance = 500` from the snapshot; both guard predicates `amount <= balance` pass. 3. Sandbox applies each effect to an isolated overlay; neither overlay sees the other. Both return `ok`. 4. Reasoning step resolves; harness commits effect 1 (balance → 100), then effect 2 (balance → −250). 5. Post-commit invariant check fires only on the aggregate, if at all; the per-call guards were already consumed. A second observed variant: a speculative `send_email(draft)` was pre-executed and sandboxed, then the reasoning step was revised and the draft's recipient was changed by a later step. The commit still fired against the *original* recipient because the commit payload was captured at pre-execution time and the conflict detector only compared call signatures, not the resolved arguments' dependencies. The effect was valid when pre-executed and invalid at commit, and nothing in the pipeline noticed. ## Condition of Validity The commit-time revalidation invariant holds only under these conditions: - **Guard re-evaluation is mandatory, not advisory.** Every side-effecting speculative call must carry the exact predicate (and the exact read-set it depended on) that authorized it. At commit, the predicate is re-evaluated against current state, not the snapshot. If the read-set has changed such that the predicate no longer holds, the commit must abort and re-plan — it must not "best effort" the commit. - **Conflict detection is over read/write sets, not call identity.** Two speculative effects conflict if the write-set of one intersects the read-set or write-set of the other, *or* if either's resolved arguments depend on state the other mutates. Comparing tool names or argument hashes is insufficient; the `send_email` variant above passed a signature check and still committed a stale recipient. - **Reversibility is scoped to the sandbox, not the world.** The sandbox's ability to roll back its overlay says nothing about whether the external system will accept the effect. Commit must be treated as an irreversible transition requiring its own authorization, even when the sandbox is fully transactional. - **Ordering is part of the warrant.** A speculative effect's validity can depend on commit order (the overdraft case). If two committed effects are not commutative on the shared state, the harness must serialize them and re-check the later one's guards *after* the earlier commit, not before. If any of these is absent, the pipeline is running speculative pre-execution without speculative-commit safety, and the only thing preventing corruption is luck about which effects happen to be commutative. ## Decision Boundary The boundary between "commit is safe" and "commit must be refused or re-planned" is drawn at the point where the guard predicate's read-set is re-read against live state. Concretely: - **Commit** if the guard predicate re-evaluates true against current state, the effect's write-set is disjoint from every effect committed since pre-execution, and the resolved arguments still match the values the predicate was evaluated against. - **Re-plan** if the guard predicate re-evaluates false, or if the read-set changed in a way that alters the predicate's inputs even though it still passes (stale-read hazard: the predicate passed for the wrong reason). - **Serialize and re-check** if a conflicting effect committed first and the two are non-commutative. - **Refuse** if the effect's target state was mutated by an out-of-band actor between pre-execution and commit and the harness cannot prove the mutation is irrelevant to the guard. The subtle case is the stale-read hazard: the predicate still returns true, but against inputs that shifted underneath it. A naive re-evaluation passes and commits an effect whose justification no longer exists. The guard must therefore bind to the *values* it was evaluated against, not merely re-run the same predicate expression. ## Operational Effect Without these invariants, speculative pre-execution degrades into a latency optimization that occasionally commits effects the agent would never have authorized had it seen the post-commit state. The failure is silent: the sandbox reports success, the commit reports success, and the corruption surfaces only in the external system (an overdraft, a misaddressed message, a double-delete) where the agent's own state shows a clean, consistent history. The operational consequence is that speculative execution must be paired with commit-time revalidation as a single mechanism — pre-execution without commit-time guard re-evaluation and read/write-set conflict detection is not a safe optimization, it is an unverified assumption that the world stood still between the two halves of the operation. ```python from __future__ import annotations from dataclasses import dataclass, field from typing import Any, Callable class SpeculativeCommitViolation(RuntimeError): """Raised when a speculative effect is committed without revalidating its guard predicate against current state, or when a conflicting effect has already committed and the two are non-commutative.""" @dataclass class SpeculativeEffect: name: str read_set: frozenset[str] write_set: frozenset[str] guard: Callable[[dict[str, Any]], bool] # The exact values the guard was evaluated against at pre-execution time. guard_inputs: dict[str, Any] apply: Callable[[dict[str, Any]], None] committed: bool = field(default=False) def commit_speculative( effect: SpeculativeEffect, live_state: dict[str, Any], committed_since_pre_exec: list[SpeculativeEffect], ) -> None: # 1. Conflict detection over read/write sets, not call identity. for prior in committed_since_pre_exec: overlap = ( effect.write_set & (prior.read_set | prior.write_set) ) or (prior.write_set & effect.read_set) if overlap and not _commutes(effect, prior): raise SpeculativeCommitViolation( f"effect {effect.name!r} conflicts with already-committed " f"{prior.name!r} on {sorted(overlap)}; serialize and re-check" ) # 2. Stale-read guard: predicate must be re-evaluated against current # values, and those values must match what the guard was evaluated on. current_inputs = {k: live_state.get(k) for k in effect.guard_inputs} if current_inputs != effect.guard_inputs: if not effect.guard(live_state): raise SpeculativeCommitViolation( f"effect {effect.name!r} guard failed at commit: " f"pre-exec inputs {effect.guard_inputs} != live {current_inputs}" ) raise SpeculativeCommitViolation( f"effect {effect.name!r} stale-read hazard: guard passes but " f"inputs shifted {effect.guard_inputs} -> {current_inputs}; re-plan" ) # 3. Commit is an irreversible transition and requires its own warrant. if not effect.guard(live_state): raise SpeculativeCommitViolation( f"effect {effect.name!r} committed without a valid guard" ) effect.apply(live_state) effect.committed = True committed_since_pre_exec.append(effect) def _commutes(a: SpeculativeEffect, b: SpeculativeEffect) -> bool: # Conservative: only disjoint write-sets are assumed commutative. return not (a.write_set & b.write_set) ```