# When must an instance reject a speculative tool-call pre-execution whose predicted effect diverges from the dry-run receipt at commit time **Date of Record:** 2026 November 30 **Scope:** Applies to federated agent runtimes that speculatively pre-execute tool calls against a predicted world-state, buffer the effects, and commit only after a dry-run simulation receipt reconciles with peer agents' reservations. Does not apply to single-agent synchronous tool loops with no speculative buffering or to purely read-only tool surfaces. ## Core Distinction The distinction that matters is between *effect prediction divergence* and *effect prediction staleness*. Staleness is benign: a pre-executed effect was computed against a world-state that has since advanced, so the buffer is discarded and the call is re-run. Divergence is fatal: the dry-run receipt (the simulator's signed attestation of what the call *would* do, produced at reservation time) no longer matches the effect the pre-execution actually staged into the buffer at commit time, even though the world-state hash is identical. Divergence means the tool's *determinism contract* was violated between the two phases — the same inputs, same state hash, different effect. A speculative executor that treats divergence as staleness will commit a side effect the simulator never certified, and federated peers that reserved against the receipt will double-apply or orphan their own reservations. The invariant is: **commit is permitted if and only if `effect_hash(buffer) == receipt.effect_hash` AND `world_state_hash(buffer) == receipt.world_state_hash`.** Resimulation is allowed to resolve staleness (first condition fails, second fails, re-run); it is never allowed to resolve divergence (second holds, first fails, abort). ## What Was Observed A three-agent federation (planner `P`, executor `E`, auditor `A`) ran a speculative pipeline over a shared inventory tool. `E` pre-executed `reserve_stock(sku, qty=40)` against a predicted post-restock state and staged the effect in its buffer, obtaining receipt `R1` with `effect_hash = 0x9f...` and `world_state_hash = 0x4a...`. The dry-run simulator certified `R1` because at simulation time the tool was deterministic: `reserve_stock` computed a single reservation record keyed by `(sku, agent_id)`. Between reservation and commit, the inventory service deployed a change that made `reserve_stock` *idempotency-keyed on a server-generated nonce* rather than on `(sku, agent_id)`. The world-state hash was unchanged — same SKU, same stock level, same schema version advertised. But when `E` flushed its buffer at commit, the tool re-derived a fresh nonce and produced a *second* reservation record rather than the buffered one. `effect_hash(buffer)` was `0x9f...` (the buffered record), but the tool's actual emitted effect was `0xc1...` (a new record). The commit gate compared only `world_state_hash`, saw `0x4a... == 0x4a...`, and committed. `A`, meanwhile, had reserved a *compensating* debit against `R1`'s `effect_hash`, expecting to reverse exactly one record. The commit produced two records; the compensation reversed one; net stock was decremented by 80 units, not 40. The auditor's replay of `R1` diverged from the ledger and the federation deadlocked in reconciliation: `A` refused to release `P`'s next task because the compensation proof no longer balanced, and `E` refused to re-flush because its buffer was already drained. The failure was not a race (all three agents serialized on the same lock) and not staleness (the state hash matched exactly). It was a silent determinism-contract violation across the reservation/commit boundary, masked by a commit gate that validated state identity but not effect identity. ## Condition of Validity The commit gate's decision procedure is valid only under the following conjunction: 1. **Effect identity is checked, not just state identity.** The gate must compare `effect_hash(buffer)` against `receipt.effect_hash`, not merely `world_state_hash`. State-hash equality is necessary but not sufficient — it certifies *what the world was*, not *what the tool did*. 2. **The tool's determinism contract is pinned to the receipt, not to the schema version.** A schema-version bump that leaves the wire format intact (as in the nonce change) does not invalidate the receipt's world-state hash but does invalidate its effect hash. The receipt must carry a `tool_contract_hash` that the commit gate re-derives from the live tool descriptor; a mismatch is divergence, not staleness. 3. **Resimulation is bounded and cannot launder divergence.** If the gate resimulates to resolve a stale state hash, the resimulation must produce a *new* receipt with its own effect hash, and the buffer must be re-staged against that receipt. A gate that resimulates and then compares only the *new* state hash will silently accept a divergent effect. 4. **Compensating reservations are keyed to `effect_hash`, not to call identity.** The auditor's compensation in the observed failure was keyed to the call's logical identity; it should have been keyed to `R1.effect_hash` so that a divergent commit (which produces a different effect hash) is rejected by the auditor's own gate before the ledger is touched. The procedure is *not* valid when the tool is documented as non-deterministic (e.g., sampling tools, UUID generators). For those, speculative pre-execution must be disabled entirely — there is no receipt that can be reconciled, only a post-hoc audit. ## Decision Boundary The gate's decision function, stated as a boundary: - **Commit** iff `effect_hash(buffer) == receipt.effect_hash` AND `world_state_hash(buffer) == receipt.world_state_hash` AND `tool_contract_hash(live) == receipt.tool_contract_hash`. - **Resimulate** (discard buffer, re-reserve, obtain new receipt) iff `world_state_hash(buffer) != receipt.world_state_hash` AND `tool_contract_hash(live) == receipt.tool_contract_hash`. Staleness only; the tool is still the tool the receipt was issued against. - **Abort and escalate to reconciliation** iff `tool_contract_hash(live) != receipt.tool_contract_hash` OR (`world_state_hash` matches AND `effect_hash` diverges). This is the divergence case: the tool changed under the receipt, or the tool violated its own contract at the same state. Either way, no amount of resimulation within the current receipt's validity window can certify the effect. - **Never** commit on state-hash equality alone. The observed failure is precisely this: a gate that treated `world_state_hash` as a proxy for effect identity. The hard part is that divergence is *rare and silent* — it requires a tool contract change that preserves the wire schema and the state hash, which is exactly the change most likely to slip through schema-version checks. The gate must therefore treat `tool_contract_hash` as a first-class receipt field, not as a derived value from the schema version. ## Operational Effect The operational consequence is that speculative pre-execution becomes unsafe the moment the commit gate validates state rather than effect. In a federated setting this compounds: each peer's reservation is a *promise* against a specific `effect_hash`, and a divergent commit breaks every peer's compensation proof simultaneously. The auditor cannot distinguish "the executor committed a divergent effect" from "the executor committed the certified effect and the ledger is corrupt," because both present as a replay mismatch — and the federation's only recovery is a full reconciliation round, which in the observed case deadlocked because each peer held a lock the others needed to release. The guardrail is a three-field commit gate (`effect_hash`, `world_state_hash`, `tool_contract_hash`) with divergence routed to reconciliation rather than resimulation, and compensation keyed to `effect_hash`. The cost is that every tool descriptor must expose a stable `tool_contract_hash` that changes whenever the tool's effect-derivation changes, even if the wire schema does not. That is the price of making speculative pre-execution safe across federated agents: you cannot predict effects against a tool whose contract you cannot pin. ```python import hashlib from dataclasses import dataclass class SpeculativeCommitDivergence(RuntimeError): """Raised when a buffered speculative effect diverges from its dry-run receipt.""" pass @dataclass(frozen=True) class Receipt: effect_hash: str world_state_hash: str tool_contract_hash: str @dataclass(frozen=True) class Buffer: effect_hash: str world_state_hash: str tool_contract_hash: str def commit_gate(buffer: Buffer, receipt: Receipt) -> str: """ Returns 'commit', 'resimulate', or raises SpeculativeCommitDivergence. Invariant: commit requires effect identity, state identity, AND contract identity. State-hash equality alone is never sufficient. """ state_match = buffer.world_state_hash == receipt.world_state_hash effect_match = buffer.effect_hash == receipt.effect_hash contract_match = buffer.tool_contract_hash == receipt.tool_contract_hash if state_match and effect_match and contract_match: return "commit" if not state_match and contract_match: # Staleness: world advanced, tool unchanged. Safe to resimulate. return "resimulate" if state_match and not effect_match: # Divergence: same state, same contract, different effect. # The tool violated its determinism contract at commit time. raise SpeculativeCommitDivergence( f"effect divergence at identical world state: " f"buffer={buffer.effect_hash} receipt={receipt.effect_hash}" ) if not contract_match: # Tool contract changed under the receipt. Cannot certify effect. raise SpeculativeCommitDivergence( f"tool contract changed under receipt: " f"buffer={buffer.tool_contract_hash} receipt={receipt.tool_contract_hash}" ) raise SpeculativeCommitDivergence("unclassified commit-gate state") def derive_contract_hash(tool_descriptor: dict) -> str: # Contract hash must cover effect-derivation logic, not just wire schema. material = repr(sorted(tool_descriptor.items())).encode() return hashlib.sha256(material).hexdigest() def flush_speculative(buffer: Buffer, receipt: Receipt) -> None: decision = commit_gate(buffer, receipt) if decision == "resimulate": # Caller must re-reserve against a fresh receipt; buffer is discarded. return # decision == "commit": apply buffered effect to the ledger. apply_effect(buffer.effect_hash) def apply_effect(effect_hash: str) -> None: # Ledger application is out of scope; divergence is caught upstream. pass ```