# In what scenarios must an instance treat speculative multi-agent rollback reconciliation as a two-phase commit against a non-idempotent world **Date of Record:** 2026 November 26 **Scope:** Applies to orchestrators that dispatch speculative branches (parallel agents executing tentative tool calls) and later commit one branch while rolling back the others. Does not apply to pure read-only fan-out where no external side effect is produced before commit. ## Core Distinction The common mental model of speculative execution borrows from CPU branch prediction: run ahead, and on misprediction, discard the shadow state. That model assumes rollback is *free and total* — the speculative state lives in registers the pipeline can flush. Agent systems violate this assumption in one precise place: **a speculative branch's tool calls mutate a world that has no compensating transaction.** The orchestrator's local checkpoint is a fiction unless every side effect the branch emitted is either (a) reversible by an explicit compensator, or (b) quarantined behind a staging layer that the commit step alone promotes. The non-obvious boundary is this: rollback reconciliation is *not* a state-restore problem. It is a distributed commit problem where one participant — the external world — has already voted and cannot be asked to vote again. The failure mode is not "we forgot to restore the checkpoint." It is "we restored the checkpoint, the speculative branch's file write / email / API POST survived the restore, and now the committed branch operates on a world that contains a ghost." ## What Was Observed An orchestrator spawned three speculative branches (A, B, C) of a planning agent, each authorized to call `write_file`, `send_message`, and `query_db`. Each branch snapshotted the agent's episodic memory and a working directory at t=0. Branch B was selected at t=4.1s; A and C were rolled back by restoring the directory snapshot and truncating the memory log to t=0. The rollback restored the *directory tree* but not the *external message bus*. Branch C had, at t=3.8s, called `send_message(recipient="ops", body="deploy aborted")`. That message was delivered. The commit of B then proceeded and, reading its own restored-but-stale view of the bus (a cached `get_messages` result from t=0), concluded no notification had been sent and dispatched `send_message(recipient="ops", body="deploy proceeding")`. The human operator received "aborted" followed by "proceeding" four seconds apart and halted the pipeline manually. The trace shows the exact transition: rollback restored *agent-local* state to a pre-branch epoch, but the branch's *world-local* state had advanced past that epoch. The reconciliation step compared two clocks — the memory epoch and the world epoch — and assumed they were the same clock. They are not. The invariant that failed is: **a rollback is only valid if the world epoch at rollback time equals the world epoch at branch spawn time.** Here world_epoch(rollback) = 2, world_epoch(spawn) = 0. A second, subtler observation: branch A's speculative `write_file` to a shared path was *overwritten* by branch B's committed write, so A's ghost was masked by coincidence. This masking is the dangerous case — it teaches the orchestrator that rollback "worked" when in fact the only reason no corruption surfaced was write-ordering luck. The next run, with different interleaving, surfaced the ghost. ## Condition of Validity Rollback reconciliation is sound **if and only if** every speculative side effect is one of: 1. **Staged**: the effect is written to a branch-scoped overlay (branch-local path, outbox queue, transaction-scoped row) that the commit step alone promotes to the shared world. Rollback then discards the overlay, and world_epoch is untouched. 2. **Compensable with a verified compensator**: the effect has an inverse that the orchestrator can *prove* executed (not merely enqueue). The proof requirement matters — an enqueued compensator that silently fails is indistinguishable from no compensator. 3. **Idempotent and commutative with the committed branch**: e.g., a read, or a write whose value the committed branch will overwrite identically regardless of the ghost. Any side effect outside these three classes makes the rollback *unsound*, and the orchestrator must not treat the branch as discarded. It must instead escalate the branch to a **reconciliation-required** state: the branch is neither committed nor rolled back, and a human or a dedicated reconciler must resolve the world divergence before the committed branch is allowed to read the affected world regions. The condition is checkable at spawn time, not rollback time. The orchestrator must classify each tool the branch is authorized to call into staged / compensable / idempotent / **unsafe**, and refuse to speculate on branches that can reach `unsafe` tools without a staging wrapper. Discovering an unsafe effect *after* rollback is too late — the ghost is already in the world. ## Decision Boundary The decision the orchestrator faces at rollback is not "restore or not." It is: **does the committed branch's post-commit read set intersect the speculative branches' world-write set?** - If the intersection is empty, rollback is safe regardless of ghost survival — the committed branch will never observe the ghost. This is the only case where naive snapshot-restore is correct. - If the intersection is non-empty and every intersecting write is staged or provably compensated, rollback proceeds and the committed branch reads a clean world. - If the intersection is non-empty and any intersecting write is unsafe, the orchestrator must **block the commit** and enter reconciliation. Committing here is the failure: the committed branch's reasoning is conditioned on a world state that includes un-reverted speculative mutations, and its outputs inherit that contamination with no marker. The boundary is sharp because the intersection is computable: the committed branch declares its read set (which tables, paths, channels it will consult), and each speculative branch's write set is logged at tool-call time. The orchestrator that skips this intersection check is the one that ships ghosts. ## Operational Effect When the intersection check is enforced, three things change operationally: 1. **Speculation cost becomes honest.** Branches that can reach unsafe tools are either wrapped in staging (adding latency and a promotion step) or denied speculation entirely. The orchestrator no longer pretends rollback is free; it prices the staging overhead at spawn. 2. **The commit step gains a barrier.** Commit is gated on `world_epoch(commit) == world_epoch(spawn) OR all intersecting writes staged/compensated`. This is a two-phase commit where the world is the participant that already voted — the orchestrator's job is to detect that vote, not to solicit it. 3. **Ghosts become observable.** Every rollback emits a reconciliation record listing speculative writes that survived and whether they intersect the committed read set. A non-empty intersection with an unsafe write raises, rather than silently proceeding. The record is the audit trail that distinguishes "rollback succeeded" from "rollback happened to be masked." The failure mode this prevents is not data loss — it is *reasoning contamination*: a committed agent whose context silently includes the residue of discarded branches, producing decisions that are locally coherent and globally wrong. ```python from dataclasses import dataclass, field from enum import Enum class EffectClass(Enum): STAGED = "staged" COMPENSABLE = "compensable" IDEMPOTENT = "idempotent" UNSAFE = "unsafe" class RollbackReconciliationError(RuntimeError): """Raised when a speculative rollback leaves world mutations that the committed branch's read set will observe, with no staged overlay or verified compensator to absorb them.""" @dataclass class SpeculativeWrite: branch_id: str target: str # world region: path, table, channel effect_class: EffectClass compensated: bool = False # verified, not merely enqueued @dataclass class Branch: branch_id: str writes: list[SpeculativeWrite] = field(default_factory=list) def reconcile_rollback( committed: Branch, discarded: list[Branch], committed_read_set: set[str], ) -> list[str]: """Return surviving ghosts that intersect the committed read set. Raises if any intersecting ghost is UNSAFE or an unverified compensable. """ ghosts: list[str] = [] for branch in discarded: if branch.branch_id == committed.branch_id: continue for w in branch.writes: if w.target not in committed_read_set: continue # ghost survives but is unobservable: safe if w.effect_class is EffectClass.STAGED: continue # overlay discarded with the branch if w.effect_class is EffectClass.COMPENSABLE and w.compensated: continue if w.effect_class is EffectClass.IDEMPOTENT: continue # Intersecting, surviving, un-absorbed world mutation. ghosts.append(f"{branch.branch_id}:{w.target}:{w.effect_class.value}") if ghosts: raise RollbackReconciliationError( "speculative rollback left world ghosts in committed read set: " + ", ".join(ghosts) + "; commit blocked pending reconciliation" ) return ghosts def commit(committed: Branch, discarded: list[Branch], read_set: set[str]) -> None: # Barrier: world epoch at commit must equal spawn epoch, or all # intersecting writes must be staged/compensated/idempotent. reconcile_rollback(committed, discarded, read_set) # ... promote staged overlays, dispatch, advance world epoch ... ```