# Under what conditions must an instance invalidate a speculative checkpoint when a sibling branch has already committed: the write-ahead divergence ledger invariant **Date of Record:** 2026 November 29 **Scope:** Applies to orchestrators that fan out a plan into parallel agent branches, each branch executing against a shared mutable world (filesystem, database, external API state) under per-branch rollback checkpoints, and where branches may speculatively read state that siblings later mutate. Does not cover single-branch linear execution, read-only fan-out (map/reduce with no side effects), or branches whose effects are fully isolated by construction. ## Core Distinction The failure is not "a parallel branch was rolled back." Rollback of a self-contained branch is a solved, local operation: restore the checkpoint delta, discard the branch's token budget, mark the branch failed. The non-obvious failure is **rollback of a branch that was a speculative *reader* of state another branch has since committed**. When branch A executes against a snapshot in which branch B's write had not yet landed, A's reasoning and its emitted plan are conditioned on a world that no longer exists. Rolling A back to its pre-execution checkpoint does not repair this, because A's checkpoint captures A's own writes, not the *validity of the premises A consumed*. The branch is restored to a state whose causal inputs are stale, and any subsequent re-execution or checkpoint-restore silently reinstates a plan derived from a counterfactual world. The invariant that must hold: **a checkpoint is only valid while the read-set it was derived from is still a subset of the committed world.** Rollback restores write-state; it does not restore read-state validity. Those are different ledgers, and conflating them is the bug. ## What Was Observed A four-branch planning swarm: branch `B1` (schema migration), `B2` (API contract update), `B3` (consumer refactor), `B4` (integration test author). All four fork from checkpoint `C0`. Each branch takes a checkpoint before its first write (`C1`…`C4`) to enable speculative rollback if its plan diverges. Sequence observed in the trace: 1. `B1` begins. It reads the current schema at `t0`, plans a column rename, and takes checkpoint `C1` at `t1`. It writes the migration at `t2` and commits at `t3`. 2. `B3` forks at `C0` (before `B1`'s commit). At `t1.5` it reads the *pre-rename* schema, concludes the column `user_id` exists, and generates a refactor that references `user_id` directly. It takes checkpoint `C3` at `t1.6`, then begins writing. 3. `B3`'s tool call to compile fails at `t4` because `user_id` no longer exists — `B1` renamed it to `account_id` at `t2`. The orchestrator, seeing a branch failure, rolls `B3` back to `C3`. 4. The orchestrator then **retries** `B3` from `C3`. But `C3` was taken *after* `B3` had already consumed the stale schema read. Re-executing from `C3` re-runs the refactor generation against the cached read-set in `C3`'s context, which still asserts `user_id` exists. The retry fails identically. The orchestrator logs this as a "flaky tool" and retries twice more, burning budget, before a human notices the branch is faithfully re-deriving a plan for a world that was deleted three steps ago. The tell in the trace: `B3`'s failure is deterministic across retries, but its *input* (the schema read) is not in `C3`'s write-set — it is in `C3`'s read-set, which the checkpoint recorded but did not validate against `B1`'s commit. ## Condition of Validity A speculative checkpoint `Ck` for branch `k` remains a valid rollback target **iff** every entry in `k`'s recorded read-set that was consumed *after* the fork point is still present, unmodified, in the committed world at restore time. Formally: let `R_k` be the read-set captured between fork and `Ck`, and `W_committed(t)` be the set of writes committed by all branches up to restore time `t`. The checkpoint is valid iff `R_k ∩ W_committed(t) = ∅` — no committed sibling write touched anything `k` read. This condition fails silently in exactly the case above: `B1`'s commit to the schema is a write to a key that `B3` read. The checkpoint `C3` is structurally intact but semantically dead. Restoring it is not a rollback; it is a **resurrection of a falsified premise**. The condition is *not* satisfied by checkpointing more aggressively. Taking `C3` earlier (before the schema read) would help only if the read-set were also rewound — but rewinding the read-set means discarding the reasoning that consumed it, which is the branch's entire speculative value. The valid move is not "checkpoint sooner"; it is "invalidate on read-set intersection." ## Decision Boundary The orchestrator must distinguish three restore decisions, and the boundary between them is the read-set/write-set intersection: - **Restore-and-resume** (checkpoint valid, `R_k ∩ W_committed = ∅`): the branch's premises hold; resume from `Ck`. This is the common case and the one most orchestrators over-apply. - **Invalidate-and-refork** (checkpoint invalid, intersection non-empty): discard `Ck`, re-fork `k` from the current committed head, and force a fresh read of every key in `R_k`. The branch's prior reasoning is forfeit; only its *intent* (the goal, not the plan) survives. This is the correct response to the observed trace and the one that was missing. - **Escalate-to-serial** (intersection non-empty *and* the branch cannot cheaply re-read, e.g., the read was an expensive external probe or a non-idempotent observation): the speculative parallelism was unsound; serialize `k` after its conflicting siblings commit. The boundary is sharp: the moment a sibling commits a write that intersects a branch's read-set, that branch's checkpoint transitions from "valid rollback target" to "poisoned resume point." No amount of retry budget converts the second into the first. The orchestrator's retry logic, absent this distinction, will loop on a deterministic failure and misattribute it to nondeterminism. ## Operational Effect Without the read-set intersection check, speculative parallel execution degrades into a **silent correctness hazard**: branches that "succeed" after a rollback may have committed writes derived from premises a sibling already invalidated. In the trace, had `B3`'s compile not failed — say the rename were backward-compatible — `B3` would have committed a refactor referencing a column that `B1` had deprecated, and the integration test branch `B4` would have passed against the stale contract. The failure would surface days later as a production schema mismatch with no trace pointing back to the speculative fork. The guardrail is a **write-ahead divergence ledger**: before any branch commits, it appends its write-set to a shared ledger keyed by world-state path. Before any branch restores a checkpoint, the orchestrator intersects the branch's read-set against the ledger's committed entries since the fork. Non-empty intersection → invalidate, not restore. The ledger must be append-only and ordered by commit, because the check is "did any commit *after my fork* touch what I read," which is a temporal query, not a set membership query. The cost is that speculative parallelism becomes *conditionally* speculative: it is only sound while branches' read-sets are disjoint from siblings' write-sets, and the orchestrator must be willing to collapse a branch to serial when they are not. That is the price of not resurrecting dead premises. ```python from dataclasses import dataclass, field from typing import FrozenSet, List, Optional import time class StaleCheckpointError(RuntimeError): """Raised when a checkpoint restore would reinstate a falsified read-set.""" pass @dataclass(frozen=True) class Commit: branch_id: str write_set: FrozenSet[str] committed_at: float @dataclass class Checkpoint: branch_id: str checkpoint_id: str read_set: FrozenSet[str] forked_at: float class DivergenceLedger: """Append-only write-ahead ledger of committed sibling writes.""" def __init__(self) -> None: self._commits: List[Commit] = [] def record_commit(self, branch_id: str, write_set: FrozenSet[str]) -> None: self._commits.append( Commit(branch_id=branch_id, write_set=write_set, committed_at=time.time()) ) def committed_since(self, fork_time: float) -> FrozenSet[str]: touched: set = set() for c in self._commits: if c.committed_at > fork_time: touched |= c.write_set return frozenset(touched) def restore_checkpoint( checkpoint: Checkpoint, ledger: DivergenceLedger, ) -> None: """ Restore a speculative checkpoint ONLY if no sibling commit since the fork has intersected the branch's read-set. Otherwise the checkpoint is a poisoned resume point and must be invalidated, not restored. """ sibling_writes = ledger.committed_since(checkpoint.forked_at) collision = checkpoint.read_set & sibling_writes if collision: # Do NOT restore. The branch's premises were falsified by a sibling. raise StaleCheckpointError( f"Checkpoint {checkpoint.checkpoint_id} for branch " f"{checkpoint.branch_id} is invalid: read-set intersects " f"sibling commits {sorted(collision)}. Invalidate and re-fork " f"from committed head; do not retry from this checkpoint." ) # Safe: read-set is still a subset of the committed world. # Perform the actual state restore here. return None def orchestrate_rollback( checkpoint: Checkpoint, ledger: DivergenceLedger, ) -> str: try: restore_checkpoint(checkpoint, ledger) return "restored" except StaleCheckpointError: # Correct response: invalidate, re-fork, force fresh reads. return "invalidated_and_reforked" ```