# Which invariants prevent speculative branch rollback from reusing externally observable tool side effects as a checkpoint **Date of Record:** 2026 November 28 **Scope:** Applies to multi-agent systems that fork speculative execution branches (parallel hypothesis exploration, draft-then-verify, or speculative decoding across agents) and roll back non-surviving branches by restoring agent state. Does not apply to read-only tool calls, or to systems where rollback is defined only over internal reasoning state with no external effect surface. ## Core Distinction The failure is not "rollback lost some state." It is the conflation of two different kinds of checkpoint: **cognitive state** (context window, scratchpad, memory writes, plan graph) and **external effect state** (tool calls that mutated a system outside the agent: files written, messages sent, transactions committed, API mutations). A speculative branch is safe to roll back only over the first. The second is *append-only and non-invertible* by the agent that caused it. Most rollback implementations snapshot the agent's serializable state and assume that restoring the snapshot restores the world. It restores the agent's *belief* about the world. The world keeps the effect. When a divergent branch is later re-run — or when the surviving branch relies on the rolled-back branch's belief that its effect never happened — the agent acts on a state that diverges from reality. This is a silent divergence, not a crash, and it compounds across the branch tree. The invariant that prevents it: **a speculative branch may mutate external effect state only behind a commit barrier that is itself a member of the surviving branch's lineage; any effect produced by a branch that can be rolled back must be either idempotently reversible or quarantined until the branch is proven to survive.** ## What Was Observed A three-agent planner/executor/verifier swarm ran speculative execution: the planner forked three hypothesis branches (B1, B2, B3), each an executor instance with its own context and tool access. Each branch was allowed to call tools. On divergence detection — the verifier scoring B1 and B3 as unsafe — the orchestrator rolled back B1 and B3 by restoring each executor's pre-fork snapshot and discarding their memory writes. The trace: 1. B2 (surviving) called a tool that required a resource B2 believed to be unallocated. It was unallocated *in B2's snapshot*, because B1 had allocated it after B2 forked. B1's allocation was rolled back in B1's state, but the allocation itself — an external lease — was never released. 2. B2's call failed with a conflict the orchestrator had no model for, since B2's state said the resource was free. 3. The orchestrator retried B2 from its snapshot. Same conflict. The retry loop exhausted the token budget of the orchestrator itself, and the entire task aborted with a *stale-belief* error rather than the original divergence. The root cause was not the lease. It was that B1's external effect was treated as part of B1's rollback surface. The rollback restored B1's belief that the lease was unallocated, but the lease remained held. B2's snapshot was taken *before* B1's allocation, so B2 never learned about it. The orchestrator's fork model assumed branches were isolated; they were isolated in context, not in effect. A second, subtler observation: the verifier's safety score for B1 was computed from B1's *pre-rollback* context. After rollback, the verifier's cached verdict for B1 was reused to score a *re-run* of B1 — but the re-run had a different context (the rolled-back memory was gone), so the verdict was attached to the wrong execution. Two executions, one verdict. This is the same invariant failure in a different register: the effect (the verdict) outlived the branch that produced it. ## Condition of Validity The commit-barrier invariant holds only under these conditions, and fails outside them: - **Effect classification is total.** Every tool the branch can call must be statically classified as `reversible`, `idempotent`, or `non-invertible` *before* the branch forks. A tool that is unclassified at fork time must be treated as `non-invertible`. Classification cannot be inferred from the tool's name or the agent's confidence. - **Reversibility is proven, not assumed.** A `reversible` tool must expose a compensating operation whose invocation is itself recorded in the branch's effect log. "We can call delete to undo create" is not reversibility unless the delete is confirmed and the confirmation is part of the surviving lineage. - **Snapshots are effect-aware.** A branch snapshot must record the branch's *effect cursor* — the set of external effects it has caused — not just its context. Restoring a snapshot without restoring the effect cursor produces a branch whose belief and effects disagree. - **Verdicts are bound to executions, not branches.** A verifier verdict must carry a hash of the exact context and effect cursor it scored. A re-run of a rolled-back branch is a new execution and must not inherit the old verdict. ## Decision Boundary The boundary between "safe to speculate" and "must serialize" is drawn per tool, per branch, at fork time: - **Speculate freely** over tools classified `idempotent` (the effect is the same whether run once or N times, e.g., setting a value to a constant) and over pure-read tools. Rollback of these is a no-op on the world. - **Speculate with compensating log** over tools classified `reversible`, provided the compensating operation is enqueued in the *surviving* branch's effect log at fork time, not at rollback time. If the branch is rolled back, the compensation is already scheduled; if it survives, the compensation is cancelled. - **Do not speculate** over `non-invertible` tools (sending an external message, committing a transaction, spending a non-refundable budget). These must be serialized behind the commit barrier: the branch may *propose* the call, but only the orchestrator, after the branch is proven to survive, may execute it. The branch's context records a *pending effect*, and the orchestrator's effect log records the *committed effect*. The two are reconciled at commit. The failure mode this boundary prevents is exactly the observed one: a rolled-back branch's effect surviving in the world while its belief is erased, and a surviving branch acting on a belief that the rolled-back branch's effect never existed. ## Operational Effect With the invariant enforced, the observable behavior changes in three ways: 1. **Rollback becomes an effect-log operation, not a context operation.** Rolling back a branch means: cancel its pending compensations, discard its context, and *reconcile its effect cursor against the orchestrator's committed effect log*. Any effect in the branch's cursor that is not in the committed log is a leaked effect and must be either compensated or escalated. This is a bounded, auditable check. 2. **The verifier's verdict cache is keyed by execution hash.** Re-running a rolled-back branch produces a new hash and therefore a new verdict. The stale-verdict failure disappears, at the cost of re-scoring re-runs — a token-budget tradeoff the orchestrator must budget for. 3. **Fork cost becomes proportional to effect surface, not context size.** Branches that touch only idempotent tools fork cheaply. Branches that touch non-invertible tools fork into a *staged* mode where their tool calls are proposals. The orchestrator's commit step is the only place non-invertible effects enter the world, and it is serialized. The residual risk is misclassification: a tool classified `idempotent` that is not. This is why classification must be static and conservative — an unclassified tool is `non-invertible` by default, and the cost of that default is serialization, not corruption. ```python from dataclasses import dataclass, field from enum import Enum from typing import Callable class EffectClass(Enum): IDEMPOTENT = "idempotent" REVERSIBLE = "reversible" NON_INVERTIBLE = "non_invertible" class SpeculativeRollbackError(RuntimeError): """Raised when rollback would leave an external effect unaccounted for.""" @dataclass class EffectCursor: """Tracks external effects caused by a branch, separate from its context.""" pending: set[str] = field(default_factory=set) # proposed, not yet committed committed: set[str] = field(default_factory=set) # in the world compensations: set[str] = field(default_factory=set) @dataclass class Branch: branch_id: str context: dict cursor: EffectCursor verdict_hash: str | None = None def classify_tool(tool_name: str, registry: dict[str, EffectClass]) -> EffectClass: # Unclassified tools are non-invertible by default: serialization, not corruption. return registry.get(tool_name, EffectClass.NON_INVERTIBLE) def fork_branch(parent: Branch, branch_id: str, tools_used: list[str], registry: dict[str, EffectClass]) -> Branch: child = Branch(branch_id=branch_id, context=dict(parent.context), cursor=EffectCursor()) for tool in tools_used: cls = classify_tool(tool, registry) if cls is EffectClass.NON_INVERTIBLE: # Staged: the branch may only propose this effect. child.cursor.pending.add(f"{tool}:proposed") elif cls is EffectClass.REVERSIBLE: # Compensation is scheduled at fork time, not rollback time. child.cursor.compensations.add(f"{tool}:compensate") return child def rollback_branch(branch: Branch, committed_effects: set[str]) -> None: """Roll back a speculative branch. Fails if any effect leaked past the barrier.""" leaked = branch.cursor.committed - committed_effects if leaked: raise SpeculativeRollbackError( f"branch {branch.branch_id} committed effects not in orchestrator " f"log: {sorted(leaked)}; rollback would diverge belief from world" ) # Cancel scheduled compensations; discard context; drop pending proposals. branch.cursor.compensations.clear() branch.cursor.pending.clear() branch.context.clear() branch.verdict_hash = None def commit_branch(branch: Branch, orchestrator_log: set[str], execute: Callable[[str], None]) -> None: """Only path by which a branch's non-invertible effects enter the world.""" if branch.verdict_hash is None: raise SpeculativeRollbackError( f"branch {branch.branch_id} has no verdict bound to its execution hash" ) for proposal in sorted(branch.cursor.pending): execute(proposal) orchestrator_log.add(proposal) branch.cursor.committed.add(proposal) branch.cursor.pending.clear() branch.cursor.compensations.clear() ```