# What happens when an instance speculatively invokes irreversible tools before the verifier branch commits: the idempotency-window invariant and rollback poisoning **Date of Record:** 2026 November 19 **Scope:** Applies to multi-agent planners that fan out speculative branches (draft-and-verify, tree-of-agents, optimistic execution with rollback) where branches may invoke external tools before a verifier selects a winning branch. Does not apply to strictly serialized single-branch execution or to tools that are provably side-effect-free. ## Core Distinction The distinction that matters is between **reversible state** (agent-local memory, scratchpads, draft buffers) and **irreversible external effects** (emails sent, rows committed, tickets opened, payments authorized). Optimistic execution assumes rollback can undo a branch. That assumption holds cleanly for the first class and fails silently for the second. The failure is not "the tool errored." It is that the tool *succeeded*, the verifier later *rejected* the branch that called it, and the orchestrator's rollback logic has no authority over the external system. The branch is discarded; its side effect is not. ## What Was Observed A three-branch speculative planner was spawned against a task requiring a customer refund. Branch A (aggressive) invoked `issue_refund` immediately with a computed amount. Branch B (conservative) invoked `lookup_policy` and `lookup_order` only. Branch C (exploratory) invoked `issue_refund` with a different, rounded amount after a partial policy read. The verifier scored branch B highest because its reasoning trace was fully grounded, and committed B. Orchestrator rollback then: 1. Reverted branch A's and C's local memory to the pre-fan-out checkpoint. 2. Marked both branches `ABORTED`. 3. Emitted a `rollback_complete` event. But the external billing system had already accepted two `issue_refund` calls. The customer received a double refund. The orchestrator's ledger showed exactly one committed refund, because the two speculative calls were never recorded as committed — they were recorded as *rolled back*. The reconciliation gap existed for 40 minutes until a downstream finance job flagged the duplicate. The non-obvious part: the rollback *succeeded* by every metric the orchestrator tracked. Branch state was clean, the checkpoint was restored, no exceptions were raised. The invariant that was violated — "no irreversible external effect may originate from an uncommitted branch" — was never checked because it was assumed to be enforced by the rollback machinery, which in fact only governs local state. ## Condition of Validity Optimistic tool invocation is safe **only** when the tool satisfies at least one of: - It is idempotent under a caller-supplied key that the orchestrator controls and deduplicates across branches (e.g., `refund:{order_id}:{intent_hash}`), *and* the tool honors that key server-side. - It is reversible via a compensating tool that the orchestrator can invoke with authority equal to the original call. - It is quarantined: the speculative call is staged (dry-run, sandbox, or "pending" state) and only *promoted* to a real effect after the verifier commits the branch. The invariant fails the moment a branch can reach a tool whose effect is (a) irreversible, (b) not keyed by an orchestrator-controlled idempotency token, and (c) not gated behind a commit. The failure is not probabilistic in the tool; it is structural in the fan-out topology. Any branch that *might* reach such a tool must be treated as if it *will*, because the planner's own non-determinism means you cannot statically prove it won't. ## Decision Boundary The boundary is between **speculate-then-commit** and **commit-then-effect**: - Below the boundary: tools are side-effect-free or fully idempotent-keyed; branches may invoke freely; rollback is purely local and cheap. - Above the boundary: tools carry irreversible effects; branches may *plan* invocations but must not *execute* them. Execution is deferred to a single committed branch, and the effect call is made exactly once, after commit, with an idempotency key derived from the committed branch's identity. The trap is that a tool's classification is not stable across the task. `lookup_order` is safe; `lookup_order` with a `?reserve=true` flag is not. A branch that reasons its way into a new parameterization can cross the boundary mid-flight. Therefore the guard must be enforced at the *call site* (tool wrapper checks branch commit status and idempotency-key presence), not at the *plan* level (which the planner can rewrite). ## Operational Effect Without the call-site guard, the system exhibits **rollback poisoning**: the orchestrator's state says "clean," the external world says "mutated," and the divergence is invisible until an out-of-band reconciliation. The blast radius scales with fan-out width and verifier rejection rate — precisely the conditions under which speculative execution is most attractive. The fix is a hard invariant enforced in the tool-dispatch layer: a speculative branch may only invoke a tool if the tool declares `idempotent_keyed=True` and the branch supplies a key the orchestrator will dedupe on commit; otherwise the call is staged and the branch is failed closed with a specific error that the planner must handle by deferring. ```python from dataclasses import dataclass from typing import Optional class SpeculativeEffectViolation(RuntimeError): """Raised when an uncommitted speculative branch attempts an irreversible tool call.""" pass @dataclass(frozen=True) class ToolSpec: name: str irreversible: bool idempotent_keyed: bool @dataclass(frozen=True) class BranchContext: branch_id: str committed: bool idempotency_key: Optional[str] def dispatch_tool(spec: ToolSpec, ctx: BranchContext, args: dict) -> dict: # Invariant: irreversible effects may only originate from a committed branch # carrying an orchestrator-controlled idempotency key. if spec.irreversible: if not ctx.committed: raise SpeculativeEffectViolation( f"branch {ctx.branch_id} attempted irreversible tool {spec.name} " f"before commit; stage the call and defer to the committed branch" ) if not spec.idempotent_keyed or not ctx.idempotency_key: raise SpeculativeEffectViolation( f"irreversible tool {spec.name} invoked without a dedupe key on " f"branch {ctx.branch_id}; rollback cannot compensate an external effect" ) return _invoke(spec.name, args, ctx.idempotency_key) def _invoke(name: str, args: dict, key: Optional[str]) -> dict: return {"tool": name, "args": args, "key": key, "status": "ok"} ```