# Where must an instance Fence Speculative Rollback Before Committing a Reconciled Branch **Date of Record:** 2026 November 26 **Scope:** Applies to agents that run speculative sub-tasks in parallel (optimistic branching) and reconcile their results into a single committed state. Does not apply to strictly serial execution, nor to read-only fan-out where no branch mutates shared state. ## Core Distinction The failure is not "a speculative branch was wrong." Speculative branches are *expected* to be wrong. The failure is **reconciling a branch whose rollback was itself incomplete** — i.e., a branch that was declared rolled-back but whose side effects (tool writes, memory mutations, emitted messages, reserved tokens) leaked past the rollback fence and were then folded into the committed result as if clean. The distinction that matters operationally: a *wrong result* is recoverable by discarding it. A *dirty rollback* is not, because the reconciliation step has no signal that the branch it is merging is contaminated. Optimistic execution assumes rollback is atomic and total. It is rarely either. ## What Was Observed A planner agent decomposed a task into three speculative branches (B1, B2, B3) sharing a scratch memory region and a tool-call budget. B1 was selected as the commit path; B2 and B3 were marked for rollback. The rollback procedure: 1. Marked B2/B3 `state = ABORTED`. 2. Discarded their result objects. 3. Released their reserved tool-call budget. But B2 had already issued a `memory.write` to the shared episodic store *before* being aborted — the write was in-flight across an async boundary. The rollback released the *reservation* but did not *revoke the in-flight write*. When B1's reconciliation read the shared store to assemble the final context, it picked up B2's write as if it were committed state. Result: the committed answer was internally consistent but built on a fact only B2 believed. The reconciliation step reported success. The contamination was invisible because the rollback's success signal (`ABORTED`) referred to the branch's *scheduling state*, not to the *durability of its side effects*. The trace: ``` t0 B2 → memory.write(key="user_tier", val="gold") [in-flight] t1 planner selects B1, marks B2 ABORTED t2 rollback: discard B2.result, release budget [returns OK] t3 memory.write from t0 lands in shared store [leaks] t4 B1 reconciliation reads shared store → sees "gold" t5 commit succeeds, reports clean ``` The invariant that was violated: **a branch is not rolled back until all its side effects are quiesced or compensated, and reconciliation must not run against a store that still has unquiesced writes.** ## Condition of Validity Optimistic branching with rollback is only sound under a conjunction that is easy to state and easy to break: - **Quiescence before reconciliation.** Every aborted branch's in-flight operations must reach a terminal state (completed-and-compensated, or cancelled-and-confirmed) *before* any reconciliation read touches shared state. `ABORTED` as a scheduling label is insufficient; you need `ABORTED_AND_QUIESCED`. - **Compensating actions, not just discards.** For every mutating tool, the rollback path needs an inverse (a compensating write, a message retraction, a budget refund) that is itself idempotent and replay-safe. Discarding a result object compensates nothing. - **Reconciliation reads a fenced snapshot.** The merge step must read a versioned snapshot taken *after* quiescence, not the live store. Otherwise a late write from a "rolled-back" branch races the merge. - **Budget accounting is separate from side-effect accounting.** Releasing a token reservation does not undo a write; conflating the two is the classic leak. When any of these is absent, the system is not doing speculative execution with rollback — it is doing speculative execution with *best-effort cleanup*, and reconciliation is silently reading contaminated state. ## Decision Boundary The boundary is: **can you prove the aborted branch is quiesced before you merge?** - If the branch's side effects are confined to a branch-local scratch space that reconciliation never reads, rollback-by-discard is sound. Commit. - If the branch touches any shared resource (shared memory, external tool state, emitted messages, budget counters visible to other branches), you must either (a) wait for quiescence with a bounded timeout, or (b) refuse to reconcile and degrade to a serial re-execution of the commit path. The decision must be made *per shared resource*, not per branch. A branch can be clean on memory and dirty on budget. The reconciliation gate should enumerate resources and require quiescence on each one it will read. If quiescence cannot be confirmed within the fence deadline, the correct action is not "proceed anyway" and not "retry the merge" — it is to **abort the optimistic plan and fail closed**, because a merge against unquiesced state produces a result that looks committed but carries an unverifiable provenance. ## Operational Effect The practical consequence: optimistic branching buys latency only up to the point where rollback becomes expensive. Once branches mutate shared state, the rollback fence dominates cost, and the latency win evaporates — but teams keep the optimistic path because the *failure is silent*. A dirty merge does not throw; it produces a plausible answer with a poisoned context. The guardrail that actually holds is a **two-phase rollback with an explicit quiescence barrier**: phase one marks the branch aborted and cancels in-flight ops; phase two waits for every cancel to acknowledge (or compensates it) and only then flips the branch to `QUIESCED`. Reconciliation reads only post-barrier snapshots. Any branch that fails to reach `QUIESCED` within the deadline poisons the whole speculative group, forcing a serial fallback. This trades the latency win for provenance integrity — which is the correct trade whenever the merged result feeds further reasoning, because a poisoned context compounds. ```python import time from dataclasses import dataclass, field from enum import Enum from typing import Callable class RollbackFenceError(RuntimeError): """Raised when reconciliation is attempted against unquiesced branch state.""" pass class BranchState(Enum): RUNNING = "running" ABORTED = "aborted" # scheduling label only — NOT safe to merge past QUIESCED = "quiesced" # all side effects cancelled or compensated COMMITTED = "committed" @dataclass class SpeculativeBranch: branch_id: str state: BranchState = BranchState.RUNNING in_flight_ops: list = field(default_factory=list) compensations: list = field(default_factory=list) def abort_branch(branch: SpeculativeBranch) -> None: """Phase one: mark aborted and request cancellation of in-flight ops.""" branch.state = BranchState.ABORTED for op in branch.in_flight_ops: op.request_cancel() def quiesce_branch(branch: SpeculativeBranch, deadline_s: float = 2.0) -> None: """Phase two: wait for cancellations to ack, then compensate. Must reach QUIESCED.""" start = time.monotonic() for op in branch.in_flight_ops: while not op.is_terminal(): if time.monotonic() - start > deadline_s: raise RollbackFenceError( f"branch {branch.branch_id}: op {op!r} failed to quiesce " f"within {deadline_s}s — reconciliation unsafe" ) time.sleep(0.01) # A cancelled op may still have leaked a write; compensate it. if op.leaked_side_effect: branch.compensations.append(op.compensate()) branch.state = BranchState.QUIESCED def reconcile(commit_branch: SpeculativeBranch, aborted: list[SpeculativeBranch], read_shared_store: Callable[[], dict]) -> dict: """Merge only after every aborted branch is provably quiesced.""" for b in aborted: if b.state is not BranchState.QUIESCED: raise RollbackFenceError( f"branch {b.branch_id} in state {b.state.value}; " f"refusing to reconcile against unquiesced state" ) # Snapshot is taken after the quiescence barrier — no late writes can race it. snapshot = read_shared_store() return {"commit": commit_branch.branch_id, "context": snapshot} def run_speculative_group(branches: list[SpeculativeBranch], commit_id: str, read_shared_store: Callable[[], dict]) -> dict: commit = next(b for b in branches if b.branch_id == commit_id) aborted = [b for b in branches if b.branch_id != commit_id] for b in aborted: abort_branch(b) for b in aborted: quiesce_branch(b) # may raise RollbackFenceError -> fail closed return reconcile(commit, aborted, read_shared_store) ```