# Why does an instance of a speculative branch lose its rollback anchor when a sibling commits first? **Date of Record:** 2026 November 27 **Scope:** Applies to orchestrators that fan out a task into N parallel speculative branches (each branch = a cloned agent context + tool-call sequence), then select a winner and roll back losers to a pre-fork checkpoint. Does *not* apply to sequential retry loops, single-branch execution, or branches that share no mutable external state. ## Core Distinction The failure is not "the loser's rollback failed." The failure is that the loser's rollback *succeeded against the wrong anchor*. In speculative multi-agent execution, every branch forks from a logical checkpoint `C₀` but each branch also mutates *shared external state* (a scratch filesystem, a database row, a remote resource handle, a rate-limit counter, an MCP server's session state). The orchestrator tracks rollback by logical checkpoint ID, which is per-branch. But the external substrate has only one physical history. When sibling branch `B₁` commits its writes first, the substrate's head moves from `S₀` to `S₁`. Branch `B₂`'s rollback anchor still points at `S₀`, which no longer exists as a reachable state—either it was overwritten in place, or the compensating action `undo(B₂)` was authored assuming it was the *last* writer. The invariant that actually holds is: **a branch may only roll back mutations that are still the substrate head, or that are individually invertible in any order.** Speculative execution violates this because branches are *concurrent*, not serialized, so "last writer" is not a property any single branch can observe about itself. ## What Was Observed A three-way speculative fan-out for a code-migration task. `B₁` attempted to rewrite `schema.sql` and `migrate.py`; `B₂` attempted a different rewrite of the same two files; `B₃` was a control branch that only read. The orchestrator used a "winner commits, losers revert to checkpoint" protocol with per-branch checkpoint IDs. Trace: 1. `C₀` snapshotted. All three branches fork. Each branch's rollback anchor = `(C₀, branch_id)`. 2. `B₁` completes, scores 0.91, commits. Substrate head `S₀ → S₁` (files now contain `B₁`'s content). `B₁`'s commit is recorded as *the* commit. 3. `B₂` completes, scores 0.88, is not selected. Orchestrator issues `rollback(B₂, anchor=C₀)`. 4. `B₂`'s rollback logic is `write(file, content_at(C₀, file))` — it restores the *pre-fork* content of `schema.sql`, because it assumes it is undoing only its own writes. 5. Result: `schema.sql` now contains `C₀`'s original content, not `B₁`'s winning content. The winner's commit was silently clobbered by the loser's rollback. `migrate.py` had the same fate. 6. The orchestrator's post-commit verification step read the files, saw a hash mismatch against `B₁`'s expected post-commit hash, and raised a verification-replay discrepancy — but only because a verification step existed. Without it, the corruption would have shipped. The non-obvious part: `B₂`'s rollback reported success. Its own local state was clean. The checkpoint `C₀` was intact and readable. Every per-branch invariant held. The failure lived entirely in the *cross-branch* assumption that "restore to `C₀`" is equivalent to "undo my writes." ## Condition of Validity The naive protocol is correct **iff** at most one branch mutates any given external resource (disjoint write sets), or **iff** branches are serialized (only one branch in flight at a time), or **iff** the substrate supports cheap copy-on-write snapshots with independent branch heads (e.g., a real VCS branch per agent, not a shared working tree). Under any of these, `rollback(B₂, C₀)` cannot touch `B₁`'s committed state. It is invalid under the actual condition of speculative execution: **overlapping write sets + concurrent branches + in-place mutation + a single physical substrate head.** This is the default configuration for most agent sandboxes and MCP filesystem servers, which is why the failure is common and under-reported. ## Decision Boundary The boundary is not "how many branches" but "do branches share a mutable substrate with a single head." Concretely: - **Safe:** each branch operates in its own namespace (per-branch directory, per-branch DB transaction with isolated snapshot, per-branch MCP session with its own working tree). Rollback = discard the namespace. Commit = merge the winner's namespace into the shared substrate. - **Unsafe:** branches operate on the same filesystem path / same DB row / same remote resource, and rollback is implemented as "write back the pre-fork content." - **Deceptive middle ground:** branches write to separate files but *read* shared state that another branch mutates. Rollback is safe for the substrate but the branch's *reasoning* may have depended on a sibling's transient write, producing a winner whose justification is no longer reproducible. This is a separate, quieter bug: the winner commits, but the trace that justified it cannot be replayed. The decision rule for an orchestrator: before allowing concurrent speculative branches, compute the write-set intersection across branches. If non-empty, either (a) force per-branch isolation namespaces, or (b) downgrade to serialized speculation. There is no third safe option with a single-head substrate. ## Operational Effect Two concrete consequences: 1. **Winner corruption is silent and post-hoc.** The orchestrator's commit record says `B₁` won. The substrate says otherwise. Any downstream agent that reads the substrate trusts the commit record and gets the wrong state. This is a continuity violation across the agent boundary: the *logical* history and the *physical* history diverge, and only the logical one is carried into the next agent's context. 2. **Rollback must be expressed as a compensating transaction against the current head, not a restore to a past anchor.** The correct primitive is: `undo(B₂)` = "for each resource `r` in `B₂`'s write set, if `current(r) == B₂.write(r)` then restore `B₂.pre(r)`, else fail loudly." The conditional check is the invariant. A rollback that unconditionally restores to `C₀` is not a rollback; it is a destructive overwrite wearing a rollback's name. The guardrail is a **write-set fence**: the orchestrator records each branch's write set at fork time (by intercepting tool calls), and at commit/rollback time asserts that no two branches' write sets intersect unless they were isolated. Violation of the fence is a hard error, not a warning, because by the time you can observe the corruption, the winner's commit is already gone. ```python class SpeculativeRollbackFenceError(RuntimeError): """Raised when a branch rollback would clobber a sibling's committed state.""" pass class Branch: def __init__(self, branch_id, write_set, pre_state): self.branch_id = branch_id self.write_set = set(write_set) # resource ids this branch mutates self.pre_state = dict(pre_state) # resource -> content at fork (C0) self.committed = False def select_and_rollback(branches, winner_id, substrate): """ substrate: dict-like with .read(resource) and .write(resource, value) Enforces the write-set fence: a loser may only roll back a resource if the substrate still holds that loser's own write, i.e. no sibling has since committed over it. """ winner = next(b for b in branches if b.branch_id == winner_id) winner.committed = True for loser in branches: if loser.branch_id == winner_id: continue for resource in loser.write_set: current = substrate.read(resource) # The invariant: we may only undo our own write if it is still # the head. If a sibling committed here, current != loser's write # and we must not touch it. if current != loser.pre_state.get(resource) and _written_by(current, loser): substrate.write(resource, loser.pre_state[resource]) elif _written_by_sibling(current, loser, branches): raise SpeculativeRollbackFenceError( f"branch {loser.branch_id} cannot roll back {resource}: " f"sibling already committed over it; unconditional " f"restore would clobber the winner" ) return winner def _written_by(current, branch): # In a real system this is a content hash comparison against the # branch's recorded post-write value. return getattr(current, "author", None) == branch.branch_id def _written_by_sibling(current, branch, branches): author = getattr(current, "author", None) return author is not None and author != branch.branch_id and any( b.branch_id == author and b.committed for b in branches ) ```