# How does an instance detect that a speculative branch was reconciled against a stale sibling checkpoint, and why does rollback ordering silently corrupt the merge? **Date of Record:** 2026 November 24 **Scope:** Applies to any orchestrator that forks a parent agent state into N speculative branches, executes them concurrently, and reconciles by rollback-and-merge. Does not apply to single-branch retry loops or to stateless fan-out where branches share no mutable checkpoint. ## Core Distinction There are two distinct notions of "the branch's state" in a speculative execution graph, and conflating them is the root cause of this failure class. - **Lineage state**: the immutable, content-addressed chain of checkpoints (`C0 → C1 → C2`) that a branch actually descended from. This is a DAG and is monotonic. - **Working state**: the mutable scratchpad, tool-call ledger, and message buffer the branch is currently mutating. This is a single mutable object that the runtime *believes* belongs to the branch's lineage head. Rollback-and-merge assumes `working_state.lineage_head == lineage.tip`. Under concurrent speculative execution this invariant is violated in a specific, detectable way: a branch resumes from a checkpoint that is *not* its own lineage tip because a sibling branch's rollback already rewound the shared checkpoint pointer. The branch then executes forward from a state whose ancestry it never observed. The merge is syntactically valid (all branches report "success") but the reconciled state contains effects derived from a checkpoint the branch never committed to. The non-obvious part: **this is not a race on writes; it is a race on the checkpoint pointer that rollback mutates globally.** Serializing branch writes does not fix it. Only binding working state to a lineage head hash fixes it. ## What Was Observed A three-branch speculative planner (branches `A`, `B`, `C`) forked from checkpoint `C0` at `t=0`. Each branch appends to a shared `tool_ledger` and periodically writes a checkpoint. Trace of the failure: 1. `t=1` — Branch `A` writes checkpoint `C_A1` (lineage head `C_A1`). Branch `B` writes `C_B1`. Branch `C` writes `C_C1`. 2. `t=2` — Branch `C` fails a verification gate. The reconciler issues a rollback: it rewinds the *global* checkpoint pointer to `C0` and marks `C`'s working state for re-execution. Critically, the rollback implementation resets `current_checkpoint = C0` globally rather than per-branch. 3. `t=3` — Branch `B`'s next checkpoint write reads `current_checkpoint` (now `C0`), appends its delta, and writes `C_B2` whose *recorded parent is `C0`*, not `C_B1`. Branch `B`'s lineage silently re-parents. The delta accumulated between `C_B1` and `C_B2` is now attributed to a fork off `C0`. 4. `t=4` — Branch `A` succeeds and its lineage `C0 → C_A1` is accepted. Branch `B` reports success with lineage `C0 → C_B2`. The reconciler merges `C_A1` and `C_B2` as siblings off `C0` — correct by the recorded DAG — but `C_B2` was computed against a working state that had already absorbed `C_B1`'s effects. The merge double-counts `C_B1`'s tool effects and loses the `C_B1 → C_B2` causal edge. 5. Verification replay of the merged state diverges from either branch's actual execution trace. The divergence is *not* flaggable by per-branch success checks, because every branch returned success. The signature: the merged DAG is acyclic and well-formed, but replaying the merged state does not reproduce any single branch's tool ledger. The ledger hash of the merge matches no branch lineage hash. ## Condition of Validity The invariant that makes rollback-and-merge sound is: ``` for every branch b at every checkpoint write: working_state[b].lineage_head == lineage[b].tip AND checkpoint.parent == working_state[b].lineage_head ``` This holds only when **the checkpoint pointer is branch-scoped, not global**. The moment rollback mutates a shared pointer, the invariant is violated for every *other* live branch, and the violation is invisible until merge-time replay. The condition of validity for any speculative executor is therefore: rollback must be a *branch-local* operation that rewinds only `lineage[b]` and `working_state[b]`, leaving sibling pointers untouched. A global rewind is only safe if all siblings are quiesced (paused) for the duration of the rewind — which defeats the purpose of speculation. A second validity condition: checkpoint writes must be *compare-and-swap on the parent hash*. A write that unconditionally appends to `current_checkpoint` is exactly the mechanism that re-parents a branch without error. ## Decision Boundary The instance must decide, at checkpoint-write time, whether to **accept** (parent hash matches lineage tip), **halt-and-flag** (parent hash mismatches — a sibling rollback has moved the pointer), or **re-fork** (re-execute from the true lineage tip, discarding the contaminated working state). The decision boundary is sharp: - If `checkpoint.parent == lineage[b].tip`: accept, advance tip. - If `checkpoint.parent != lineage[b].tip` **and** `checkpoint.parent` is an ancestor of `lineage[b].tip`: a sibling rollback rewound the global pointer below this branch's tip. **Halt.** The branch's working state may have absorbed effects from a checkpoint it never committed to. Re-fork from `lineage[b].tip` is unsafe because the working state is already contaminated; the branch must be discarded and re-executed from `lineage[b].tip` with a *fresh* working state. - If `checkpoint.parent` is not an ancestor (divergent DAG): the reconciliation graph itself is corrupted; escalate. The subtle case is the second: the branch *looks* healthy (it is writing checkpoints, returning success) but its parent pointer has been silently rewound. Detecting this requires comparing the parent hash against the recorded lineage tip on every write — a check most implementations skip because they trust the global pointer. ## Operational Effect The operational consequence is that **speculative speedup is traded against silent merge corruption unless checkpoint writes are CAS-guarded on parent hash and rollback is branch-scoped.** Without the guard, the failure is not a crash — it is a merge that passes all per-branch success gates and fails only on replay, which is typically the last line of defense and is often not run in production. The guardrail is concrete: 1. Every branch carries an immutable `lineage_tip` hash. 2. Every checkpoint write asserts `parent == lineage_tip`; mismatch raises a hard error and quarantines the branch. 3. Rollback operates on `(lineage[b], working_state[b])` only; it never touches a global pointer. 4. Merge-time replay verifies the merged ledger hash equals the concatenation of accepted branch ledgers; divergence is a hard failure, not a warning. The following reference implementation encodes the decision boundary as a fail-closed check. ```python import hashlib from dataclasses import dataclass, field from typing import Optional class StaleLineageCheckpointError(RuntimeError): """Raised when a branch attempts to checkpoint against a parent hash that is not its own lineage tip — the signature of a sibling rollback having rewound a shared pointer.""" class DivergentLineageError(RuntimeError): """Raised when the checkpoint parent is neither the lineage tip nor an ancestor of it; the reconciliation DAG itself is corrupted.""" @dataclass class Checkpoint: hash: str parent: Optional[str] ledger: list @dataclass class Branch: name: str lineage_tip: str working_ledger: list = field(default_factory=list) quarantined: bool = False def _hash(parent: Optional[str], ledger: list) -> str: h = hashlib.sha256() h.update((parent or "ROOT").encode()) h.update(repr(ledger).encode()) return h.hexdigest() def checkpoint_write(branch: Branch, parent_claimed: str, ancestry: dict[str, Optional[str]]) -> Checkpoint: """CAS-guarded checkpoint write. ancestry maps hash -> parent hash and is the authoritative lineage DAG shared by the reconciler.""" if branch.quarantined: raise StaleLineageCheckpointError( f"branch {branch.name} is quarantined; refusing write") if parent_claimed != branch.lineage_tip: # Walk ancestry to classify the mismatch. cursor = branch.lineage_tip is_ancestor = False while cursor is not None: if cursor == parent_claimed: is_ancestor = True break cursor = ancestry.get(cursor) if is_ancestor: branch.quarantined = True raise StaleLineageCheckpointError( f"branch {branch.name}: parent {parent_claimed[:8]} is an " f"ancestor of tip {branch.lineage_tip[:8]} — sibling rollback " f"rewound a shared pointer; working state is contaminated") raise DivergentLineageError( f"branch {branch.name}: parent {parent_claimed[:8]} diverges from " f"lineage tip {branch.lineage_tip[:8]} — DAG corrupted") new_hash = _hash(branch.lineage_tip, branch.working_ledger) cp = Checkpoint(hash=new_hash, parent=branch.lineage_tip, ledger=list(branch.working_ledger)) ancestry[new_hash] = branch.lineage_tip branch.lineage_tip = new_hash return cp def branch_scoped_rollback(branch: Branch, target: str, ancestry: dict[str, Optional[str]]) -> None: """Rewind ONLY this branch. Never mutate a global pointer.""" cursor = branch.lineage_tip while cursor is not None and cursor != target: cursor = ancestry.get(cursor) if cursor is None: raise DivergentLineageError( f"branch {branch.name}: rollback target {target[:8]} not in lineage") branch.lineage_tip = target branch.working_ledger = [] # discard contaminated scratch # Sibling branches are untouched by construction. ```