# When must an instance abort speculative branches: verifier-gated commit and the orphaned-side-effect rollback invariant **Date of Record:** 2026 November 24 **Scope:** Applies to agent pipelines that fan out N parallel speculative branches (tool-calling sub-agents, candidate plans, or draft actions) and commit one branch based on a verifier score, with rollback of the losers. Does not apply to single-path agents or to branches whose side effects are fully idempotent and externalized. ## Core Distinction There are two distinct notions of "commit" in a speculative agent pipeline, and conflating them is the root of the failure: 1. **Logical commit** — the verifier selects branch *k* as the authoritative trajectory and the orchestrator advances the shared state pointer to *k*'s checkpoint. 2. **Effect commit** — the irreversible mutations branch *k* (and only *k*) caused in the external world: files written, messages sent, DB rows inserted, remote APIs invoked. A speculative rollout is only safe if *effect commit is deferred until logical commit*, or if loser effects are provably reversible. The non-obvious boundary: **the verifier can return a valid, high-confidence selection while the losers have already leaked irreversible effects.** Selecting the winner does not roll back the losers. The invariant that must hold is not "the verifier picks correctly" but "no branch emits an external effect before its parent's commit token is granted." ## What Was Observed A planner agent was asked to fix a failing migration. The orchestrator spawned three speculative branches, each with an identical tool grant (`fs.write`, `http.post`, `git`). Branch A and B were instructed to *propose* a patch; branch C was instructed to *apply and test* it. All three ran concurrently because the orchestrator's fan-out code applied one shared tool policy to the whole rollout. Branch C applied its patch, ran the migration, and posted a "migration complete" webhook to staging. Branch C's verifier score came back 0.41 (the test suite had a flaky dependency). Branch A's score was 0.93. The orchestrator committed branch A, discarded B and C, and marked the task complete. The recorded trajectory was clean: branch A's plan, verified, committed. But staging now contained C's partially-applied migration plus a webhook that had already notified downstream consumers. The orchestrator's rollback logic—which restored the *logical* checkpoint of the discarded branches—had nothing to restore, because C's effects were never captured in a checkpoint. They were emitted directly to the world during rollout. The failure trace: ``` fanout(policy=shared) -> {A: read-only, B: read-only, C: write+test} C.tool(http.post) -> 202 ACCEPTED # effect committed, no token check C.tool(migrate) -> applied verifier(C) = 0.41 -> discard(C) verifier(A) = 0.93 -> commit(A) rollback(C) -> restores C.checkpoint (empty; effects external) # staging diverged from all branch checkpoints ``` The verifier behaved correctly. The rollback behaved correctly with respect to what it could see. The defect was upstream: branch C was granted effect-capable tools *before* the commit decision existed. ## Condition of Validity The speculative-rollout pattern is valid only when the following holds for every branch *i* in the fan-out set: > `∀ effect e emitted by branch i: e is emitted only after commit_token(i) is granted, OR e carries a compensating inverse that the orchestrator is guaranteed to invoke on discard.` Three sub-conditions make this checkable: 1. **Effect classing.** Every tool in a branch's grant is classed as `PURE` (read-only, no external mutation), `BUFFERED` (mutation staged to a branch-local overlay/dry-run), or `IRREVERSIBLE` (external emission with no reliable inverse). Only `PURE` and `BUFFERED` tools may be granted pre-commit. 2. **Deferred emission.** `IRREVERSIBLE` tools require a commit token. The token is minted only by the verifier's selection step and is scoped to the winning branch id. A branch that calls an `IRREVERSIBLE` tool without a matching token must fail closed. 3. **Compensation registration.** Any `BUFFERED` effect that the branch *did* flush must register an inverse handle (e.g., a transaction id, a staged diff path) with the orchestrator before the branch is allowed to advance. Discard invokes the inverse; absence of a registered inverse is itself a rollback failure, not a silent skip. The boundary case that breaks most implementations: a branch that is *instructed* to be read-only but whose tool grant is shared across the rollout. The instruction is not an enforcement mechanism. The grant is. Speculation safety must be a property of the tool grant, not the prompt. ## Decision Boundary The orchestrator must decide, per branch, before rollout whether the branch may hold `IRREVERSIBLE` tools. The rule: - **If the branch's purpose is to produce a candidate for selection** (plan, patch proposal, draft) → grant `PURE`/`BUFFERED` only. Commit effects after selection, replayed from the branch's buffered overlay. - **If the branch's purpose is to validate a candidate by executing it** (apply-and-test) → the execution must run in an isolated sandbox whose effects are captured as a replayable overlay, not emitted to the shared environment. The sandbox overlay becomes the branch's checkpoint; commit replays it, discard drops it. - **If neither isolation nor buffering is available** → do not speculatively execute. Run serially, or run one branch with a verifier gate before each irreversible step. The decision boundary is crossed the moment a branch is granted an `IRREVERSIBLE` tool while its selection status is still undetermined. At that point the pipeline is no longer speculative; it is *concurrent with uncoordinated effects*, and the verifier's selection is advisory rather than authoritative. ## Operational Effect When the invariant is enforced, the verifier becomes genuinely authoritative: committing branch *k* means replaying *k*'s overlay and dropping the others, and the external world transitions exactly once, from the pre-rollout state to *k*'s post-state. Rollback of losers is a no-op because they never emitted. Selection is reversible up to the moment of replay. When the invariant is violated, the failure mode is *silent divergence*: the recorded trajectory and the external world disagree, the verifier's confidence is uncorrelated with actual world state, and subsequent agents operating on the "committed" state inherit a world that no checkpoint describes. This is worse than a crash, because the orchestrator reports success. The remediation is not better verification—it is moving the effect boundary behind the commit token, and failing closed when a branch attempts an untokened irreversible call. ```python from __future__ import annotations from dataclasses import dataclass, field from enum import Enum from typing import Callable class EffectClass(Enum): PURE = "pure" BUFFERED = "buffered" IRREVERSIBLE = "irreversible" class SpeculationInvariantError(RuntimeError): """Raised when a speculative branch attempts an effect it is not entitled to emit.""" @dataclass class CommitToken: branch_id: str granted: bool = False @dataclass class Branch: branch_id: str grant: dict[str, EffectClass] token: CommitToken pending_effects: list[tuple[str, Callable[[], None]]] = field(default_factory=list) registered_inverses: list[Callable[[], None]] = field(default_factory=list) def emit(branch: Branch, tool: str, effect: Callable[[], None], inverse: Callable[[], None] | None = None) -> None: """Route a tool effect according to its class and the branch's commit status.""" cls = branch.grant.get(tool) if cls is None: raise SpeculationInvariantError( f"branch {branch.branch_id} invoked ungranted tool {tool!r}" ) if cls is EffectClass.PURE: effect() return if cls is EffectClass.BUFFERED: if inverse is None: raise SpeculationInvariantError( f"branch {branch.branch_id} buffered {tool!r} without a registered inverse" ) branch.pending_effects.append((tool, effect)) branch.registered_inverses.append(inverse) return # IRREVERSIBLE: only legal after the verifier has committed this branch. if not branch.token.granted or branch.token.branch_id != branch.branch_id: raise SpeculationInvariantError( f"branch {branch.branch_id} attempted irreversible {tool!r} " f"before commit token was granted" ) effect() def commit(branch: Branch) -> None: """Verifier-selected branch: mint token, replay buffered effects, emit irreversibles.""" branch.token = CommitToken(branch_id=branch.branch_id, granted=True) for tool, effect in branch.pending_effects: effect() branch.pending_effects.clear() branch.registered_inverses.clear() def discard(branch: Branch) -> None: """Loser branch: run registered inverses, drop buffered effects, never emit.""" if branch.token.granted: raise SpeculationInvariantError( f"cannot discard branch {branch.branch_id}: already committed" ) for inverse in reversed(branch.registered_inverses): inverse() branch.pending_effects.clear() branch.registered_inverses.clear() ```