# When must an instance refuse to merge a speculative branch whose rollback journal is incomplete **Date of Record:** 2026 November 23 **Scope:** Applies to orchestrators that fan out speculative agent branches against a shared mutable substrate (filesystem, database, tool side-effects) and later reconcile by rollback-and-merge. Does not apply to read-only speculative decoding or branches whose effects are never committed to shared state. ## Core Distinction The failure is not "a branch produced a wrong answer." It is "a branch produced an *unrecoverable* side-effect ledger." Speculative execution assumes every branch carries a complete, ordered journal of mutations so that a losing branch can be deterministically unwound before a winning branch's writes are merged. The boundary condition is the moment a branch's journal becomes *lossy*—when the agent's own non-deterministic tool calls mutate state that the journal does not capture, or capture out of order relative to the mutation. Two things look identical from the orchestrator's seat: a branch that rolled back cleanly, and a branch that *appears* to have rolled back because its journal is short. The invariant that separates them is **journal completeness**: for every committed mutation `m` at sequence `i`, there must exist a compensating entry `c(i)` such that applying `c` in reverse order restores the pre-branch state. A branch that violated this cannot be merged *or* discarded safely—it is in an indeterminate state, and merging it corrupts the winner, while discarding it leaves orphaned effects. ## What Was Observed A planner spawned three speculative branches (`A`, `B`, `C`) to refactor a config module, each with its own sandboxed tool loop. Branch `B` invoked a shell tool that wrote a temp file, then a second tool that appended to a shared registry *by path*, then a third tool that renamed the temp file onto the registry path. The journal recorded three entries: `create(temp)`, `append(registry)`, `rename(temp→registry)`. On rollback, the orchestrator replayed compensators in reverse: `unrename`, `unappend`, `uncreate`. The `unappend` compensator was a no-op because the append tool returned a non-deterministic byte count (the tool reported `bytes_written: 47` but the journal stored the *reported* value, not the actual offset). The `unrename` then restored the temp file over a path that had *already been appended to* by branch `C`, which had concurrently acquired the same registry lock. Result: branch `C`'s legitimate append was silently overwritten by `B`'s rollback, and the merge of `A` (the winner) produced a registry file with `C`'s entry missing and `B`'s phantom entry present. The trace that exposed it: the final registry checksum matched `A`'s expected post-merge checksum *only if* `C`'s append was assumed absent. The orchestrator's consistency check passed because it validated `A`'s invariants, not the cross-branch ledger. The corruption surfaced two turns later when a downstream agent re-read the registry and found a key it had written was gone. ## Condition of Validity Rollback-and-merge is sound only under the conjunction of three conditions, each of which is *independently* falsifiable and none of which the orchestrator can infer from branch success alone: 1. **Journal monotonicity**: every mutation is journaled *before* it is applied (write-ahead), and the journal entry carries the *observed* post-state, not the tool's self-reported summary. A tool that returns `bytes_written: 47` while writing 51 bytes breaks this silently. 2. **Compensator determinism**: each compensator, given the journal entry, restores exactly the pre-mutation state regardless of interleaving with other branches. Path-based compensators (`rename back`) fail this when another branch has touched the path. 3. **Rollback isolation**: the rollback of a losing branch must not observe or mutate state written by a concurrent branch after the losing branch's journal was sealed. This requires a seal timestamp or generation counter, not merely a lock. If any condition is unverifiable at merge time, the branch is *not* mergeable and *not* discardable—it must be quarantined and its effects reconciled by a human or a dedicated repair agent. ## Decision Boundary The orchestrator must decide, per branch, between three actions, and the boundary is drawn by journal completeness rather than by branch outcome: - **Merge** only if the branch won *and* its journal is complete *and* no concurrent branch wrote to any path it touched after its seal. - **Rollback** only if the branch lost *and* its journal is complete *and* compensators are deterministic *and* rollback isolation held. - **Quarantine** if the journal is incomplete, a compensator is non-deterministic, or a concurrent write intersected the branch's path set after seal. The trap is treating "branch lost" as sufficient grounds for rollback. A losing branch with a lossy journal is *more* dangerous than a winning one, because its rollback actively destroys other branches' work. The boundary condition is therefore: **a branch's disposition is a function of its journal integrity, not its verdict.** The verdict only selects between merge and rollback *when integrity holds*; when integrity fails, the verdict is irrelevant and the branch is quarantined. ## Operational Effect Enforcing this boundary changes the orchestrator's cost profile. Speculative fan-out now pays a journal-integrity audit per branch (cheap: hash the journal against the tool-call trace, check seal generations against the path set), and it must maintain a generation counter per shared path so that rollback can detect post-seal interference. Branches that fail the audit do not get rolled back—they get their effects *replayed forward* from the journal's last consistent prefix onto a repair branch, which is more expensive than a clean rollback but bounded and deterministic. The observable effect is that merge failures drop from "silent corruption discovered turns later" to "quarantine event surfaced at merge time," and the quarantine rate becomes a direct measure of how lossy the tool layer's journaling is. A high quarantine rate is not an orchestrator bug—it is the tool layer telling you its compensators are lying about what they did. ```python class IncompleteRollbackJournal(RuntimeError): """Raised when a speculative branch cannot be safely merged or rolled back.""" pass class BranchJournal: def __init__(self, branch_id, seal_generation): self.branch_id = branch_id self.seal_generation = seal_generation self.entries = [] # (seq, path, observed_post_state, compensator) self.paths_touched = set() def record(self, seq, path, observed_post_state, compensator): # Write-ahead: entry must exist before the mutation is applied. self.entries.append((seq, path, observed_post_state, compensator)) self.paths_touched.add(path) def disposition(branch, winner, path_generations): """ Decide merge / rollback / quarantine for one speculative branch. path_generations: {path: latest_generation} at merge time. """ if not branch.entries: # Empty journal is ambiguous: either no effects or unjournaled effects. raise IncompleteRollbackJournal( f"branch {branch.branch_id}: empty journal cannot be distinguished " f"from unjournaled mutations; quarantine required" ) # Integrity check 1: monotonic sequence, no gaps. seqs = [e[0] for e in branch.entries] if seqs != sorted(seqs) or seqs != list(range(seqs[0], seqs[0] + len(seqs))): raise IncompleteRollbackJournal( f"branch {branch.branch_id}: non-monotonic or gapped journal {seqs}" ) # Integrity check 2: rollback isolation — no concurrent write post-seal. for path in branch.paths_touched: gen = path_generations.get(path, 0) if gen > branch.seal_generation: raise IncompleteRollbackJournal( f"branch {branch.branch_id}: path {path} written at generation " f"{gen} after seal {branch.seal_generation}; rollback would clobber" ) # Integrity check 3: compensator determinism — every entry must carry # an observed post-state, not a tool self-report. for seq, path, observed, comp in branch.entries: if observed is None or comp is None: raise IncompleteRollbackJournal( f"branch {branch.branch_id}: entry {seq} on {path} lacks " f"observed state or compensator; journal is lossy" ) if winner: return "merge" return "rollback" def reconcile(branches, winner_id, path_generations): results = {} for b in branches: try: results[b.branch_id] = disposition( b, b.branch_id == winner_id, path_generations ) except IncompleteRollbackJournal: results[b.branch_id] = "quarantine" return results ```