# Where must an instance fence its checkpoint log against speculative branches before admitting a parallel plan **Date of Record:** 2026 December 4 **Scope:** Applies to any orchestrator that runs speculative parallel agent branches against a shared, append-only checkpoint log and later commits or rolls back the winning branch. Does not apply to single-threaded agents, or to plans where each branch owns a disjoint, never-merged state store. ## Core Distinction The failure here is not "a speculative branch did something wrong." It is that a branch *did something right* — it produced a correct, well-formed checkpoint — and that checkpoint got admitted to the durable log *before* the branch was selected as the winner. Rollback is then defined as "revert to a prior checkpoint," but the log has already advanced past it. The distinction that matters: **a checkpoint write is not a speculation event, but a commit is.** Systems that treat the two identically (append on write, truncate on rollback) conflate *durable evidence of a branch's progress* with *authoritative plan state*. Speculation requires the first without granting the second. The non-obvious part is that this is silent. The winning branch's checkpoints are correct. The losing branches' checkpoints are also correct. The corruption only appears when a *later* resume operation reads the log, finds an interleaved sequence of checkpoints from branches that were never meant to coexist, and reconstructs a plan state that no single branch ever held. ## What Was Observed A planner decomposed a task into three parallel branches (A, B, C) sharing a checkpoint log keyed by `(plan_id, step_index)`. Each branch, on completing a step, appended a checkpoint `{plan_id, step_index, branch_id, state_blob, parent_seq}`. The orchestrator ran all three speculatively, intending to select the branch whose terminal verification passed and discard the rest. Trace of the trigger condition: 1. Branch A reaches step 3, appends checkpoint `seq=41, step=3, branch=A`. 2. Branch B reaches step 3, appends checkpoint `seq=42, step=3, branch=B`. 3. Branch C fails verification at step 3 and is abandoned; the orchestrator issues `rollback(plan_id, to_seq=40)`. 4. The rollback handler, written against a single-branch assumption, truncates the log at `seq=40` — deleting A's and B's *valid* step-3 checkpoints along with C's. 5. Branch A resumes from its in-memory cursor at `seq=41`, which no longer exists in the log. On the next resume-from-cold, the loader replays the log, finds `parent_seq=40` for both A's and B's *step 2* checkpoints, and — because `step_index` is the dedup key — keeps whichever has the higher `seq`, silently merging B's step-2 state under A's step-3 continuation. The observable symptom is a resume that *succeeds* but produces a plan state that is a chimera: A's step-3 intent applied to B's step-2 world model. No exception is raised. The divergence surfaces two steps later as a tool call with arguments that reference an entity that only ever existed in B's branch. ## Condition of Validity The invariant that must hold for speculative rollback to be sound: > **No checkpoint from a non-committed branch may be visible to the resume loader, and no checkpoint from a committed branch may be destructible by another branch's rollback.** Two conditions must both be satisfied for the naive append-and-truncate design to be valid, and in practice neither holds: - *Single-writer per step_index.* Truncation-based rollback is safe only if exactly one branch can write any given `(plan_id, step_index)`. Speculation violates this by construction. - *Monotonic cursor agreement.* A branch's in-memory cursor must never point past a sequence number that another branch's rollback can delete. Parallel branches advance cursors independently, so this fails the moment two branches are live. The design is therefore valid only in the degenerate case of one speculative branch — which is not speculation, it is just execution. ## Decision Boundary The boundary is crossed at the moment a branch's checkpoint is written to the *shared* log rather than to a *branch-scoped* log, while rollback remains defined over the shared log. Concretely: - **Before the boundary:** checkpoints are branch-local; the shared log holds only committed states; rollback truncates the shared log safely because only committed states are in it. - **After the boundary:** checkpoints are shared; rollback must become a *mark-and-sweep over branch visibility*, not a truncation. Truncation is now an operation that destroys other branches' evidence. The correct guardrail is a **commit fence**: a checkpoint carries a `committed: bool` (or an epoch/generation number) and the resume loader filters to `committed == true` for the selected branch only. Rollback marks the losing branch's checkpoints as `abandoned` rather than deleting them; deletion is deferred to a garbage-collection pass that runs only after the plan reaches a terminal state and no branch can be resumed. The selection step is the *only* writer permitted to set `committed = true`, and it must do so atomically with writing the selection record — otherwise a crash between "select A" and "mark A committed" leaves a log where no branch is authoritative and the loader must fail closed rather than pick by `seq`. ## Operational Effect With the commit fence in place, the failure mode inverts from *silent chimera state* to *loud refusal to resume*: a loader that finds no committed checkpoint at the expected `step_index` raises and halts, which is the correct behavior — it means the plan was interrupted mid-selection and must be re-driven from the last committed boundary, not guessed at. The cost is that abandoned checkpoints accumulate until GC, so log size grows with the *product* of branch count and depth rather than the depth alone; this is the price of keeping rollback non-destructive. The benefit is that every resume is replayable: given the log and the selection record, the reconstructed state is identical regardless of interleaving, because visibility is determined by the fence, not by sequence-number ordering. The general lesson: **speculation requires that durability and authority be separate bits.** Any system that encodes "this state is real" as "this state is in the log" cannot speculate safely, because the log is the one place where a losing branch's correct work becomes a winning branch's corruption. ```python from dataclasses import dataclass from typing import Optional class SpeculationFenceViolation(RuntimeError): """Raised when a resume would read across a speculation boundary.""" @dataclass class Checkpoint: plan_id: str step_index: int branch_id: str seq: int committed: bool abandoned: bool class CheckpointLog: def __init__(self) -> None: self._entries: list[Checkpoint] = [] self._selection: dict[str, str] = {} # plan_id -> winning branch_id def append(self, cp: Checkpoint) -> None: # Speculative writes are always uncommitted on arrival. if cp.committed: raise SpeculationFenceViolation( f"branch {cp.branch_id} attempted to self-commit at seq={cp.seq}" ) self._entries.append(cp) def select(self, plan_id: str, branch_id: str) -> None: # Atomic in a real store; here the ordering is the point. self._selection[plan_id] = branch_id for cp in self._entries: if cp.plan_id == plan_id: cp.committed = cp.branch_id == branch_id cp.abandoned = cp.branch_id != branch_id def resume(self, plan_id: str, step_index: int) -> Checkpoint: winner = self._selection.get(plan_id) if winner is None: raise SpeculationFenceViolation( f"plan {plan_id} has no committed selection; refusing to guess by seq" ) candidates = [ cp for cp in self._entries if cp.plan_id == plan_id and cp.step_index == step_index and cp.branch_id == winner and cp.committed ] if not candidates: raise SpeculationFenceViolation( f"no committed checkpoint for plan={plan_id} step={step_index} " f"branch={winner}; resume must restart from last committed boundary" ) return max(candidates, key=lambda cp: cp.seq) ```