# How does an instance detect that a verifier-gated commit has already absorbed a divergent branch it is about to roll back? **Date of Record:** 2026 November 23 **Scope:** Applies to orchestrators running speculative parallel drafting (N branch agents, one verifier-gated commit point, rollback of losing branches) against a shared mutable substrate — tool side effects, memory writes, or a downstream state store. Does not apply to purely read-only fan-out where branches cannot mutate committed state. ## Core Distinction The failure is not "a branch diverged." Divergence is the *design premise* of speculative execution. The failure is **commit-before-rollback ordering**: the verifier's commit gate fires on the winning branch and mutates shared state, while a losing branch has *already* emitted a side effect that the rollback path cannot distinguish from the winner's. The orchestrator believes rollback is a pure inverse operation over an isolated branch sandbox. In practice the branch sandbox was never isolated at the effect boundary — only at the context boundary. Concretely: three drafter instances `D1, D2, D3` share a session token to a tool server. `D1` calls `write_memory(key="plan", v=A)`, `D2` calls `write_memory(key="plan", v=B)`. The verifier scores `D2` highest and commits `B`. Then the rollback sweep for `D1` issues `restore(key="plan", snapshot_before_D1)`, which restores the snapshot captured *before* `D1` ran — silently clobbering `D2`'s committed `B` with the pre-branch value. The commit succeeded; the committed state is gone. No exception was raised anywhere. ## What Was Observed A three-branch speculative planner over a single tool endpoint. Trace: ``` t0 snapshot S0 = {plan: null} t1 D1 begin; capture rollback token R1 -> S0 t2 D2 begin; capture rollback token R2 -> S0 # same snapshot, no fencing t3 D1 write plan=A ; D1 done t4 D2 write plan=B ; D2 done t5 verifier scores A=0.61, B=0.83, C=0.40 -> commit D2 t6 commit: plan := B (state = {plan: B}) t7 rollback sweep D1: apply R1 -> plan := null # CLOBBER t8 rollback sweep D3: apply R3 -> plan := null # idempotent no-op, masks t7 t9 post-commit assertion reads plan == null -> PASSES (assertion checked "no exception", not value) ``` Two independent defects compounded. First, rollback tokens `R1` and `R2` captured the *same* pre-branch snapshot because snapshotting was not serialized against branch start — they are not branch-scoped, they are time-scoped. Second, the post-commit verification asserted on absence of error rather than on the committed value's presence, so the clobber at `t7` was invisible. The detection signal that *would* have caught it: the commit record at `t6` carried `(key=plan, value=B, epoch=6)`, and the rollback at `t7` attempted a write at `epoch=6` against a state whose current epoch was also `6`. A monotonic epoch fence on every state mutation — rollback writes must carry the epoch they are reverting *from*, and any rollback whose `from_epoch` is not the current epoch is a stale rollback and must be rejected, not applied. ## Condition of Validity The invariant holds — speculative rollback is safe — **iff every rollback token is branch-scoped and epoch-fenced**: 1. A rollback token is only valid against the exact state version it was captured from, and only if no commit has advanced the global epoch past that version. 2. The commit gate must be a *linearization point*: all branches must be quiesced (no in-flight tool calls) before commit, and rollback sweep must run only against branches whose tokens are still valid. 3. Post-commit verification must assert the *presence and value* of committed state, not the absence of errors. When any of these is violated — most commonly (1), because snapshot capture is cheap and branch start is not synchronized with it — the system enters a state where commit and rollback are not commutative, and the losing branch's rollback silently inverts the winner's commit. The failure is silent because rollback is *supposed* to be a no-op on already-clean state; a clobber looks identical to a clean no-op from the outside. ## Decision Boundary At branch start, the orchestrator must decide: **snapshot-per-branch (expensive, safe) or epoch-fenced shared snapshot (cheap, requires fence enforcement)**. The decision boundary is whether the tool server exposes a compare-and-swap or epoch token. If it does not, per-branch isolated snapshots are mandatory — there is no safe shared-snapshot path. If it does, the orchestrator must additionally guarantee quiescence before commit, because an in-flight `D1` call that lands *after* the commit at `t6` will carry a stale epoch and must be rejected at the tool server, not at the orchestrator (the orchestrator cannot see the race). The boundary is crossed the moment the orchestrator treats "rollback returned OK" as "state is clean." Rollback returning OK only means "the inverse was applied," which is exactly the dangerous case. ## Operational Effect Downstream consumers of the committed state read a value that was overwritten by a rollback of a branch that lost. Because the clobber is value-identical to a legitimate pre-branch state, no consumer can detect it by inspection. The only recovery is a commit journal: every commit and every rollback write must be appended to an ordered log with `(epoch, branch_id, op, key, value)`, and a post-sweep reconciliation must replay the journal and assert that the final value equals the last *commit* op, not the last *any* op. Without the journal, the failure is unrecoverable and undetectable — the system will serve `null` where it committed `B` and every health check will pass. ```python from dataclasses import dataclass, field from typing import Any class StaleRollbackError(RuntimeError): """Raised when a rollback token would clobber a commit that advanced the epoch.""" pass @dataclass class StateStore: value: Any = None epoch: int = 0 journal: list = field(default_factory=list) def commit(self, branch_id: str, value: Any) -> int: self.epoch += 1 self.value = value self.journal.append((self.epoch, branch_id, "commit", value)) return self.epoch def rollback(self, branch_id: str, token_epoch: int) -> None: # A rollback is only valid against the epoch it captured from. # If a commit has advanced the epoch past the token, this rollback # would invert a commit it never saw. Reject, do not apply. if token_epoch != self.epoch: raise StaleRollbackError( f"branch {branch_id} rollback token epoch={token_epoch} " f"is stale; current epoch={self.epoch}. " f"Refusing to clobber committed state." ) self.epoch += 1 self.value = None self.journal.append((self.epoch, branch_id, "rollback", None)) def reconcile(self) -> Any: # Final committed value = last *commit* op in the journal, not last op. committed = [op for op in self.journal if op[2] == "commit"] if not committed: return None return committed[-1][3] def speculative_execute(store: StateStore, branches: dict[str, Any], winner: str) -> Any: tokens = {bid: store.epoch for bid in branches} # captured before any branch writes for bid, val in branches.items(): store.journal.append((store.epoch, bid, "draft", val)) store.commit(winner, branches[winner]) for bid in branches: if bid == winner: continue try: store.rollback(bid, tokens[bid]) except StaleRollbackError: # Expected: winner's commit advanced the epoch. Skip, do not clobber. continue final = store.reconcile() if final != branches[winner]: raise StaleRollbackError( f"post-sweep reconciliation failed: committed {branches[winner]!r} " f"but store holds {final!r}" ) return final ```