# Why does an instance diverge from its own rollback log when speculative branches re-merge across agents? **Date of Record:** 2026 November 20 **Scope:** Applies to multi-agent systems that run speculative (optimistic) execution branches in parallel, checkpoint agent state, and reconcile divergent branches by replay or merge after a speculative branch is invalidated. Does not apply to single-agent linear execution, nor to systems where only one branch may mutate a given state partition at a time (strict serialization). ## Core Distinction The failure is not "a speculative branch was wrong and had to be rolled back." That is expected and cheap. The failure is that **the rollback log itself is not a faithful inverse of the branch's mutations**, because the branch's mutations were themselves produced by a probabilistic agent reading a *moving* view of shared state. When branch B is rolled back after branch A committed an overlapping key, B's "undo" operations were computed against the pre-A snapshot, so replaying them either (a) resurrects a value A already superseded, or (b) deletes a key whose identity changed under it. The instance's memory after rollback is internally consistent with its *own* log but externally inconsistent with the reconciled global state. This is the divergence: **log-consistent, world-inconsistent**. The distinction that matters operationally: a rollback log is a *syntactic* inverse (invert these writes), while correctness requires a *semantic* inverse (restore the state that would have existed had the branch never run, given everything else that did run). Speculative multi-agent merge collapses these two when branches overlap. ## What Was Observed A planner agent P forks three speculative workers (W1, W2, W3) over a shared episodic store partitioned by key prefix. Each worker emits a write-ahead rollback journal: for every `put(k, v_new)` it records `undo = (k, v_old)` where `v_old` was read at branch start. Workers run concurrently; P commits whichever branch's tool-call results satisfy the goal predicate first, then rolls back the losers. Trace of the failure: 1. T0 — W1 and W2 both snapshot key `task:42.plan` = `P0`. 2. T1 — W1 writes `task:42.plan = P1`, journals `undo=(task:42.plan, P0)`. 3. T2 — W2 (having read `P0` at branch start, not `P1`) writes `task:42.plan = P2`, journals `undo=(task:42.plan, P0)`. 4. T3 — P's goal predicate satisfied by W2 → **commit W2**. Global state: `P2`. 5. T4 — P rolls back W1 by replaying its journal: writes `P0`. Global state: **`P0`**, silently clobbering the committed `P2`. 6. T5 — W2's dependent child agent, resumed on the assumption `task:42.plan == P2`, reads `P0`, and its next tool call is issued against a plan that no longer exists. It does not error; it *replans* from `P0`, producing a plausible but wrong trajectory. The observed signature: no exception, no conflict flag. The reconciler reported "3 branches, 1 commit, 2 clean rollbacks." The corruption surfaced two hops later as a *reasoning* anomaly (child agent diverged), not a *storage* anomaly. Mean time from rollback to detection was 4–7 agent turns, by which point the child had emitted 3–5 side-effecting tool calls. A second, subtler variant: when the key's *value* is a structured object and the agent's `put` was a partial merge (`v_old` read, fields updated, whole object rewritten), the undo restores `v_old` wholesale, discarding concurrent field-level updates from the committed branch that the loser never saw. Field-granularity loss is invisible to key-granularity journals. ## Condition of Validity The naive rollback invariant — *"replaying a branch's inverse journal restores the pre-branch state"* — is valid **only if the branch's mutation set is disjoint from every other branch committed or in flight during its lifetime**, i.e., no key the branch wrote was written by any concurrent branch between the branch's snapshot and its rollback. Formally: for branch B with write set `W(B)` and snapshot time `t_s`, rollback is sound iff for every `k ∈ W(B)`, the last writer of `k` in `[t_s, t_rollback]` is B itself. The moment another branch (or the committed branch) touches `k` in that window, the syntactic inverse is unsound. Two corollaries that are easy to miss: - **Read-set invalidation, not just write-set.** Even if `W(B)` is disjoint, if B *read* a key that a committed branch later changed, B's committed outputs (if B had won) would be based on stale reads. The mirror problem: the winner can be built on a read that a loser's rollback then invalidates. Reconciliation must validate read sets too, or the "winner" is unsound. - **Non-invertible side effects.** Tool calls (sending an email, charging a card, mutating an external API) have no journal inverse. A branch that emitted side effects before being rolled back has already escaped the transaction boundary. Journals only cover the agent's *internal* store; external effects require compensations, which are semantic, not syntactic, and cannot be auto-derived. ## Decision Boundary The system must choose, per branch, between three reconciliation strategies, and the choice is forced by whether overlap was detected *before* rollback: - **Disjoint (no overlap in write or read sets across the branch's lifetime):** replay the syntactic journal. Cheap, sound, the common case. - **Overlap detected, branch lost:** do **not** replay the journal. Instead perform a *semantic re-derivation*: re-snapshot the current committed state of every key in `W(B) ∪ R(B)`, and let the branch's owning agent (or a reconciler) recompute "what should this key be, given the branch never happened and the current committed values." This is expensive and requires the agent to be re-invoked with the post-commit view — it cannot be done by a storage layer alone, because the correct value depends on the branch's *intent*, not just its bytes. - **Overlap detected, branch won:** before committing, invalidate any other branch whose read set intersects `W(B)`; those branches must be re-forked from the new state, not rolled back. Committing without read-set invalidation produces winners built on stale reads. The boundary is sharp: **the presence of any interleaved write to `W(B)` or any committed write to `R(B)` between snapshot and rollback disqualifies syntactic replay.** There is no "mostly disjoint" middle ground; a single overlapping key is enough to corrupt, and the corruption is silent because the journal replay succeeds. ## Operational Effect When this invariant is violated, the failure is *latency-shifted and misattributed*. The rollback completes successfully (the journal is a valid program), the reconciler's health checks pass (state is reachable and well-formed), and the damage manifests as downstream reasoning divergence in child agents that resumed on a contract the rollback broke. Operators typically debug the child agent's "hallucination" rather than the parent's rollback, because the causal edge is 4–7 turns removed and crosses an agent boundary. The architectural guardrail is a **versioned compare-and-swap journal with read-set fencing**: every journal entry carries the version of the key it observed (`undo = (k, v_old, version_at_read)`), and rollback is permitted only if the key's current version equals `version_at_read`. On mismatch, the entry is not replayed; it is escalated to semantic reconciliation. This converts a silent clobber into an explicit conflict, which is the entire point — the system must *refuse* to roll back a key it no longer owns, and hand the branch's intent to a reconciler that can see the committed state. The cost is that some rollbacks become re-invocations of the agent rather than byte writes; the benefit is that a committed branch can never be silently resurrected-over by a loser's stale undo. Read-set fencing must extend to reads, not just writes, or the winner-on-stale-read variant survives. ```python from dataclasses import dataclass from typing import Any class RollbackDivergenceError(RuntimeError): """Raised when a speculative branch's journal cannot be soundly replayed because an overlapping branch mutated a key in its write/read set between snapshot and rollback. Signals that semantic reconciliation is required.""" @dataclass(frozen=True) class JournalEntry: key: str value_old: Any version_at_read: int is_write: bool def rollback_branch( journal: list[JournalEntry], current_versions: dict[str, int], committed_writes: set[str], ) -> list[tuple[str, Any]]: """Replay a losing branch's inverse journal, but only where sound. Returns the list of (key, value) restorations that are safe to apply. Raises RollbackDivergenceError if any entry is unsound, so the caller escalates to semantic re-derivation instead of silently clobbering. """ restorations: list[tuple[str, Any]] = [] for entry in journal: if not entry.is_write: # Read-set entries: a committed write to a key this branch read # invalidates the branch's basis; cannot be fixed by replay. if entry.key in committed_writes: raise RollbackDivergenceError( f"read-set key {entry.key!r} was committed by another " f"branch after snapshot (version_at_read=" f"{entry.version_at_read}); syntactic replay unsound" ) continue current = current_versions.get(entry.key, 0) if current != entry.version_at_read: # Another branch wrote this key after our snapshot: our undo # would resurrect a superseded value or delete a changed key. raise RollbackDivergenceError( f"write-set key {entry.key!r} diverged: journal expected " f"version {entry.version_at_read}, found {current}; " f"escalate to semantic reconciliation" ) restorations.append((entry.key, entry.value_old)) return restorations def reconcile_loser( journal: list[JournalEntry], current_versions: dict[str, int], committed_writes: set[str], semantic_rederive, ) -> None: try: restorations = rollback_branch(journal, current_versions, committed_writes) except RollbackDivergenceError as exc: # Do NOT replay. Re-invoke the branch owner against the committed view. semantic_rederive(reason=str(exc)) return apply_restorations(restorations) def apply_restorations(restorations: list[tuple[str, Any]]) -> None: # Storage-layer apply; only reached when the journal is provably sound. for key, value in restorations: _store_put(key, value) def _store_put(key: str, value: Any) -> None: # placeholder for real store raise NotImplementedError ```