# Which invariants prevent silent divergence when speculative agent branches are rolled back and merged **Date of Record:** 2026 November 23 **Scope:** Applies to orchestration layers that fan out a single task into parallel speculative agent branches, commit one branch's effects, and roll back or reconcile the others against a shared mutable substrate (tool side effects, memory store, or downstream handoff state). Does not apply to read-only fan-out, pure single-branch execution, or branches whose side effects are fully idempotent and commutative by construction. ## Core Distinction There are two different things a "rollback" can mean in a speculative multi-branch agent system, and conflating them is the root of the failure this record describes. The first is **effect rollback**: undoing the externally observable side effects a losing branch produced (a written file, a sent request, a mutated memory record, an enqueued tool call). The second is **state rollback**: restoring the branch's internal reasoning state and its view of the world to the pre-fork snapshot. These are not the same operation, they do not share a clock, and a system can perform one perfectly while failing the other. The non-obvious boundary is this: **a speculative branch can be perfectly rolled back in its own state while leaving behind a partial, non-idempotent effect on a shared resource that the winning branch then reads and treats as ground truth.** The merge reconciliation step then "succeeds" — no exception, no timeout, a clean commit — while the committed state is a chimera assembled from one branch's reasoning and another branch's leaked side effects. This is silent divergence, and it is the failure mode the invariants below exist to prevent. ## What Was Observed A planner agent forks three speculative branches (A, B, C) to resolve a single subgoal: "reconcile the account ledger." Each branch runs an identical prefix of reasoning, then diverges. All three share a single memory namespace and a single tool-execution channel to an external ledger service, because the orchestrator assumed branches would be *reasoned about* in isolation and only *committed* at the end. Branch A decides the ledger is already balanced and issues a no-op reconciliation call. Branch B decides there is a $40 discrepancy and issues a `POST /adjust` with a delta. Branch C decides the discrepancy is $40 but attributes it to a different account and issues a *reversal* followed by a *re-adjustment* — two calls, non-atomic from the branch's perspective. The orchestrator scores branches by a verifier that re-reads the ledger after each branch's declared completion. Branch B's verifier reads a ledger that already contains C's reversal (because C's calls landed first on the shared channel), concludes B's adjustment "did not take," and marks B as failed. Branch C's verifier reads a ledger containing B's adjustment plus C's own reversal-plus-readjustment, sees a net that happens to match its prediction, and marks C as passing. Branch A's verifier reads a ledger mutated by both B and C, concludes "balanced," and also passes. The orchestrator now has two passing branches (A and C) whose declared world-models are mutually contradictory, and one failed branch (B) whose effect is nonetheless physically present in the ledger. The merge policy — "commit the highest-scoring passing branch, roll back the rest" — picks C (higher confidence) and issues rollback for A and B. But A and B have no distinct effects to roll back: their effects were *already commingled* into the ledger that C's branch now claims as its own starting state. The rollback of B issues a compensating reversal that un-does B's $40 adjustment — which C's committed state had been *reasoning over* as part of its net. The final committed ledger is off by exactly the amount C's verifier had counted on B to have contributed. No step raised an error. Every branch's local execution was internally consistent. The divergence was introduced entirely at the seam between effect rollback and state rollback, and it was invisible to every verifier because each verifier's observation was contaminated by siblings it did not know existed. ## Condition of Validity The invariants below hold only under a specific precondition, and stating it precisely is the point of this section: **a speculative branch may be merged only if the set of shared resources it read during execution is disjoint from the set of shared resources any sibling wrote during the same speculative window, or if the orchestrator can prove the branch's reads were served from a snapshot that excludes sibling writes.** Call this the **read-write isolation precondition**. It is stronger than "branches don't conflict on writes." Two branches that write disjoint keys but read a shared aggregate (a balance, a count, a queue depth, a memory record that another branch appended to) violate it just as surely as two branches writing the same key. The failure above is a read-write violation, not a write-write violation: B and C wrote different accounts, but each read a ledger aggregate that the other had mutated. When the precondition fails — and in any system where branches share a memory namespace or a live tool channel, it fails routinely — the orchestrator must either (a) serialize the branches, forfeiting speculation, or (b) enforce snapshot isolation so that each branch's reads are pinned to the pre-fork state and its writes are buffered until commit. There is no third option that preserves both speculation and correctness. The common "rollback on failure" middle path is exactly the path that produces silent divergence, because rollback of effects is not rollback of reads. ## Decision Boundary The decision boundary is the moment the orchestrator chooses a winner. Before that moment, rollback is cheap and the invariants are local. After it, the system has crossed into a regime where every subsequent compensating action is itself a non-idempotent effect that must be reasoned about against a state that no longer matches any branch's model. Concretely, the orchestrator must refuse to merge and must escalate to serialized re-execution whenever any of the following is true at commit time: 1. **Any losing branch's effect journal is non-empty and non-invertible.** A branch that sent an email, called an external API with side effects, or mutated a memory record it cannot provably restore has produced an effect that rollback cannot honestly claim to undo. Compensating actions (reversals, re-adjustments) are *new effects*, not rollbacks, and they must be treated as such. 2. **Any winning branch's verifier observed state that a sibling wrote.** This is the read-write violation. It is detectable by journaling, per branch, the resource identifiers and version stamps of every read; if a winning branch's read set intersects any sibling's write set, its verification is contaminated and its pass is void. 3. **The winning branch's declared post-state does not equal the post-state after all losing-branch compensations are applied.** This is the reconciliation check that the example above would have failed: C declared a net that assumed B's adjustment was present; after B's rollback the net was wrong. The invariant is `committed_state == winner.declared_post_state` *after* all compensations, and it must be checked, not assumed. 4. **More than one branch passes verification with mutually exclusive world-models.** Two passing branches that disagree about the state of the world are not redundant confirmations; they are evidence that at least one verifier read a contaminated snapshot. This is a hard stop, not a tiebreak. The boundary is therefore not "did the merge succeed" but "can the merge prove that the winner's verification was computed against a state that no sibling could have touched." If it cannot, the correct action is to discard all branches, restore the pre-fork snapshot, and re-run serially — because a chimera commit is strictly worse than a slow correct one, and far harder to detect after the fact. ## Operational Effect The operational consequence is a shift in where the orchestrator spends its budget. Systems that assume rollback is free spend their verification budget on *scoring* branches (which is cheap and parallel) and almost nothing on *isolation* (which is expensive and serial). The failure above shows this allocation is inverted: scoring is the part that can be wrong without anyone noticing, and isolation is the part that makes scoring trustworthy. Practically, this means: every branch must carry a **read-set and write-set journal** with resource version stamps, and the merge step must run an intersection check before it runs any compensation. The intersection check is the invariant; the compensation logic is the fallback for when the check cannot be satisfied. A branch whose read set intersects a sibling's write set is marked `CONTAMINATED` and is ineligible to win regardless of its score. A branch whose effects are non-invertible is marked `IRREVERSIBLE` and forces serialized re-execution of the whole fork. Only branches that pass both checks are mergeable, and the merge must assert `committed_state == winner.declared_post_state` as a final post-condition, not as a hope. The deeper effect is on how the system reasons about time. Speculation is only sound when branches share a *frozen* past and an *unshared* future. The moment a branch's effect leaks into a sibling's read, the past is no longer frozen and the branches are no longer independent — they are concurrent agents racing on shared state, and they must be coordinated as such, with all the locking and ordering that implies. The invariant that prevents silent divergence is exactly the invariant that keeps the past frozen: **no branch may observe a write it did not itself author, and no commit may proceed until every sibling's writes are either provably absent or provably compensated.** Everything else is bookkeeping. ```python from dataclasses import dataclass, field from typing import FrozenSet class SpeculativeMergeError(RuntimeError): """Raised when a speculative fork cannot be safely merged or rolled back.""" @dataclass(frozen=True) class BranchJournal: branch_id: str read_set: FrozenSet[str] # resource ids + version stamps read write_set: FrozenSet[str] # resource ids written invertible: bool # can effects be honestly undone? declared_post_state: dict passed_verification: bool def reconcile_speculative_fork( journals: list[BranchJournal], committed_state: dict, ) -> str: """Select a winner only if read-write isolation and invertibility hold. Returns the winning branch id. Raises SpeculativeMergeError when the fork must be discarded and re-executed serially. """ if not journals: raise SpeculativeMergeError("empty fork: nothing to reconcile") passing = [j for j in journals if j.passed_verification] # Invariant 1: any losing branch with non-invertible effects poisons the fork. for j in journals: if not j.passed_verification and not j.invertible and j.write_set: raise SpeculativeMergeError( f"branch {j.branch_id} has non-invertible effects " f"{sorted(j.write_set)}; rollback would be a lie, " f"escalate to serialized re-execution" ) # Invariant 2: a passing branch whose reads intersect any sibling's writes # is CONTAMINATED and ineligible to win. eligible = [] for w in passing: contaminated_by = [ s.branch_id for s in journals if s.branch_id != w.branch_id and (w.read_set & s.write_set) ] if contaminated_by: raise SpeculativeMergeError( f"branch {w.branch_id} read state written by siblings " f"{contaminated_by}; verification is contaminated, " f"discard fork" ) eligible.append(w) # Invariant 3: mutually exclusive passing world-models are a hard stop. if len(eligible) > 1: models = {frozenset(w.declared_post_state.items()) for w in eligible} if len(models) > 1: raise SpeculativeMergeError( f"passing branches {[w.branch_id for w in eligible]} " f"declare mutually exclusive post-states; " f"at least one verifier read a contaminated snapshot" ) if not eligible: raise SpeculativeMergeError("no uncontaminated passing branch; discard fork") winner = max(eligible, key=lambda j: len(j.declared_post_state)) return winner.branch_id ```