# Under what conditions must an instance abort a speculative branch before rollback reconciliation **Date of Record:** 2026 November 22 **Scope:** Applies to orchestrators that spawn speculative agent branches (parallel candidate executions sharing a common ancestor checkpoint) and reconcile them via rollback to the ancestor plus branch replay. Does not apply to single-instance retry loops or to branches that never share mutable tool-side state. ## Core Distinction The failure is not "a speculative branch produced a wrong answer." It is the case where a branch produced a *locally consistent* answer by consuming a side effect that a sibling branch also consumed, so that rollback to the shared ancestor is no longer a valid inverse. Rollback is sound only when the ancestor checkpoint captures every piece of state the branch mutated. Agents mutate two classes of state: the explicit context window (fully checkpointable) and the *externalized* state reachable through tools—file writes, sent emails, inserted rows, appended log entries, paid API calls, and the tool-side idempotency keys those calls registered. The context window is invertible. The externalized state is not, unless the branch was executed under a compensation contract that recorded an undo action for each mutation. The boundary condition is therefore: **a speculative branch must be aborted, not rolled back, the moment it performs a non-compensable external mutation whose effect is visible to a sibling branch or to the reconciler's own verification read.** Rollback restores the ancestor's *beliefs* but not the *world*, and the reconciler's subsequent verification will read the mutated world and conclude the ancestor state was different than it was. ## What Was Observed A three-branch speculative fan-out (branches A, B, C) from a shared ancestor checkpoint at step 12. Branch A called `inventory.reserve(sku, qty=1)` against a tool that decremented a counter and returned `reserved: true`. Branch B, running concurrently, called the same tool for the same SKU and received `reserved: true` because the tool's stock was 2 and both reservations fit. Branch C failed a reasoning check and was discarded. The reconciler selected branch A as the winner, rolled back to step 12, and replayed A's plan from the ancestor to confirm determinism. During replay, A re-issued `inventory.reserve(sku, qty=1)`; the tool, lacking an idempotency key on that call, decremented again. Stock reached 0. Branch B's reservation—still live, never rolled back because B was never selected—was now unfulfillable, but B's own context still recorded `reserved: true`. The reconciler's post-replay verification read stock as 0, compared it against the ancestor's recorded stock of 2, and flagged a state divergence, aborting the whole task with an unrecoverable inconsistency. The trace signature: the reconciler's rollback-to-ancestor diff showed a delta it could not attribute to the winner's replay alone, because two branches had each contributed a mutation and only one was being replayed. The replay was deterministic with respect to the context window and non-deterministic with respect to the world. ## Condition of Validity Rollback-and-replay is a valid reconciliation primitive if and only if all three hold at the moment of reconcile: 1. **Mutation closure.** Every external mutation performed by any non-selected branch is either (a) compensated before replay begins, or (b) proven invisible to the winner's replay and to the verification read. Visibility here is not eventual—it is "readable by any tool the replay will call." 2. **Idempotency coverage.** Every tool call the replay will re-issue carries an idempotency key derived from `(ancestor_checkpoint_id, branch_id, step_index, canonicalized_args)`, so re-issue is a no-op at the tool. Calls without such a key are replay-unsafe. 3. **Ancestor sufficiency.** The ancestor checkpoint captures the full mutation ledger of every branch spawned from it, not merely the context. If the ledger is incomplete, the reconciler cannot compute the compensation set and must not proceed. If any condition fails, the correct action is to abort the speculative branch *before* entering reconciliation—not to roll back and discover the violation during verification, because by then the violation is a live external side effect that verification itself observes. ## Decision Boundary The discriminator is whether the branch has crossed the **externalization horizon**: the first tool call whose effect survives the branch's context being discarded. Before that horizon, discard-and-rollback is free. After it, the branch is committed to the world and may only be *compensated* or *promoted*, never silently rolled back. Operationally, the orchestrator must classify each tool as `PURE` (read-only, no ledger entry), `COMPENSABLE` (mutation with a registered inverse, e.g., a reservation with a release, a file write with a saved prior version), or `IRREVERSIBLE` (send, pay, append-only log, external webhook). A branch that performs an `IRREVERSIBLE` call, or a `COMPENSABLE` call whose inverse is not registered synchronously with the call, must be marked `committed` and excluded from the rollback candidate set. If the reconciler's winner is not the only committed branch, the reconciliation is a *merge* problem, not a rollback problem, and must be escalated rather than resolved by replay. The non-obvious edge: a `COMPENSABLE` call whose inverse is registered *after* the call returns is not safely compensable, because a crash between call and registration leaves an uncompensated mutation. Registration must be pre-committed—the inverse is written to the ledger before the forward call is issued, and the ledger entry is what the reconciler trusts. ## Operational Effect Enforcing the boundary changes the cost profile of speculation. Branches that stay below the externalization horizon remain cheap to discard. Branches that cross it become expensive: each committed branch consumes a real resource and must be either promoted or compensated, and compensation itself is an external call with its own failure modes. The practical consequence is that speculative fan-out should be *front-loaded*—spawn breadth early, before tools with side effects are reachable, and collapse to a single branch at the first `IRREVERSIBLE` or unregistered-`COMPENSABLE` call. The reconciler must treat the mutation ledger, not the context diff, as the source of truth for what rollback can and cannot undo, and must refuse reconciliation when the ledger shows more than one committed branch. ## Code ```python from dataclasses import dataclass, field from enum import Enum from typing import Optional class ToolClass(Enum): PURE = "pure" COMPENSABLE = "compensable" IRREVERSIBLE = "irreversible" class RollbackUnsafeError(RuntimeError): """Raised when reconciliation is attempted across a non-invertible mutation.""" @dataclass class Mutation: branch_id: str tool: str tool_class: ToolClass inverse_registered: bool = False @dataclass class Branch: branch_id: str mutations: list[Mutation] = field(default_factory=list) def committed(self) -> bool: for m in self.mutations: if m.tool_class is ToolClass.IRREVERSIBLE: return True if m.tool_class is ToolClass.COMPENSABLE and not m.inverse_registered: return True return False def reconcile(ancestor_id: str, branches: list[Branch], winner_id: str) -> str: committed = [b for b in branches if b.committed()] if not committed: # Safe: nothing crossed the externalization horizon; discard losers. return winner_id if len(committed) > 1: raise RollbackUnsafeError( f"ancestor {ancestor_id}: {len(committed)} committed branches " f"{[b.branch_id for b in committed]}; rollback-replay is not a valid " f"merge primitive, escalate to compensation." ) only = committed[0] if only.branch_id != winner_id: raise RollbackUnsafeError( f"ancestor {ancestor_id}: winner {winner_id} is not the sole committed " f"branch ({only.branch_id}); replaying the winner would double-apply " f"external mutations." ) return winner_id ```