# Which invariants prevent speculative branch reconciliation from silently forking committed agent state **Date of Record:** 2026 December 3 **Scope:** Applies to multi-agent systems that execute speculative branches (parallel hypothesizers, optimistic executors, or "draft-then-commit" planners) whose side effects are buffered and later merged or rolled back. Does not apply to strictly serial agents with no branch fan-out, nor to systems where every branch writes to disjoint immutable stores. ## Core Distinction The failure this record isolates is not "rollback failed" and not "two branches disagreed." It is **reconciliation divergence under a shared, non-idempotent effect**: two speculative branches both observe the same pre-image of a mutable resource, both commit a locally-consistent mutation, and rollback of the losing branch is applied to a *state that the winning branch already advanced*. The rollback is executed correctly against the snapshot it was issued for, yet the post-rollback world is neither the pre-branch world nor the winner's world — it is a third state that no agent ever reasoned about. The distinction that matters operationally: **a rollback is only sound if the resource it reverses has not been advanced by a concurrent committer since the rollback's snapshot was taken.** Most agent frameworks enforce "can I undo my write?" but never enforce "is my undo still valid relative to everyone else's writes?" This is a compare-and-swap problem masquerading as a compensation problem. ## What Was Observed A planner agent decomposed a task and fanned out three speculative executors (B1, B2, B3) against a shared task ledger and a shared external ticket system. Each branch held an isolated context window and a buffered action log. On branch resolution, the orchestrator selected B2 as winner and issued compensating actions for B1 and B3. The trace: 1. Pre-branch pre-image: ledger row `task-77` at `{status: open, assignee: null, version: 4}`. 2. B1 speculatively writes `assignee: alice` (buffered, version 4→5 local). 3. B2 speculatively writes `status: in_progress` (buffered, version 4→5 local). 4. B3 speculatively writes `assignee: bob` (buffered, version 4→5 local). 5. Orchestrator commits B2 first: ledger now `{status: in_progress, assignee: null, version: 5}`. 6. Orchestrator rolls back B1. B1's compensation is "restore `assignee: null`" — which is a no-op against the current world, but B1's compensation *also* decrements version and re-emits a `status: open` field it captured at snapshot time. 7. Post-rollback ledger: `{status: open, assignee: null, version: 4}` — the winner's `in_progress` was clobbered by B1's snapshot-restore. The winning branch's committed effect was silently reverted by a losing branch's compensation. The orchestrator's reconciliation logic reported success on all three branches. The divergence surfaced two tool calls later when a downstream agent read `status: open` and re-dispatched work that B2 had already started, producing a duplicate execution. The mechanism: B1's compensation was authored as a **full-row snapshot restore**, not a **field-scoped inverse**. Snapshot restore is correct only if the snapshot's version still matches the live version at compensation time. No such check existed. The compensation was idempotent with respect to B1's own writes and catastrophically non-idempotent with respect to B2's. ## Condition of Validity The invariant that would have prevented this is a **compensation precondition guard**: every compensating action must carry the pre-image version (or content hash) of the exact fields it intends to restore, and the commit path must reject the compensation if the live version of *those fields* has advanced past the compensation's recorded pre-image. Formally, for a branch `b` with buffered mutation set `M_b` over fields `F_b`, the compensation `C_b` is admissible only if for every field `f ∈ F_b`, `version(f)` at compensation time equals `version(f)` captured when `b` mutated `f`. If any field advanced, the compensation must be **narrowed** to the still-unchanged fields, and the advanced fields must be handed to the winner's branch for reconciliation rather than blindly reverted. This condition is violated in three common ways: - **Whole-row restore**: the compensation captures and restores the entire resource, dragging unrelated fields (the winner's) back to the pre-branch image. - **Version-blind compensation**: the compensation checks "did I write this?" but not "has anyone else written since?" - **Cross-store compensation without a shared version domain**: the ledger and the ticket system have independent version counters, so a compensation that spans both cannot establish a joint precondition and must be treated as non-compensable. The invariant does *not* require that speculative branches use locks. It requires only that the compensation's pre-image be **field-scoped and version-checked at application time**, not at authoring time. ## Decision Boundary The boundary between "safe to roll back" and "must escalate to reconciliation" is determined by a single predicate evaluated at compensation-application time, not at branch-resolution time: ``` admissible(C_b) := ∀ f ∈ F_b : live_version(f) == C_b.preimage_version(f) ``` - If `admissible` holds for all fields, apply the compensation as authored. - If it holds for a strict subset, apply a **narrowed compensation** over that subset and emit the remaining fields into a reconciliation queue owned by the winning branch. - If it holds for no fields — the entire mutation set was superseded — the compensation is a **no-op with a divergence record**; it must not be silently dropped, because the losing branch's reasoning may have depended on its writes and downstream agents may still hold stale reads. The critical decision the orchestrator must make and most do not: **a losing branch's speculative writes may have been observed by other agents before rollback.** If B1's `assignee: alice` was read by a downstream agent D before B1 lost, D's context now contains a fact that the reconciled world does not. The compensation invariant bounds *storage* divergence but not *observation* divergence. Systems that buffer writes must also buffer reads, or must tag every read with the branch identity so that downstream agents can be invalidated when a branch loses. Absent that, the storage layer is consistent and the reasoning layer is not. ## Operational Effect After applying the field-scoped version-checked compensation guard, the observed failure class (winner's commit clobbered by loser's snapshot restore) drops to zero, at the cost of two new operational requirements: 1. **Every buffered mutation must record per-field pre-image versions**, not just a row-level snapshot. This increases the branch context footprint and forces the executor to know which fields it touched — which a naive "write the whole object" tool call does not surface. 2. **Compensations become partial by default.** The orchestrator must handle narrowed compensations and route un-compensable fields to reconciliation. This is more code than "restore snapshot," and it is the code that the original system skipped. The residual risk that remains unbounded by this invariant is observation divergence: an agent that read a losing branch's speculative write and acted on it. Bounding that requires either read-logging with branch tags or a policy that speculative writes are never visible outside the branch until commit — i.e., true isolation rather than buffered visibility. Teams that adopt the compensation guard without addressing observation divergence will see a lower-severity but still real class of failures: agents acting on facts that were rolled back, producing plans whose premises no longer exist. ```python from dataclasses import dataclass, field from typing import Any class ReconciliationDivergence(RuntimeError): """Raised when a speculative branch's compensation cannot be soundly applied.""" @dataclass class FieldVersion: value: Any version: int @dataclass class BufferedMutation: branch_id: str fields: dict[str, FieldVersion] # field -> pre-image captured at mutation time def apply_compensation( live: dict[str, FieldVersion], compensation: BufferedMutation, winner_branch: str, ) -> dict[str, FieldVersion]: """ Apply a losing branch's compensation only over fields whose live version still matches the compensation's recorded pre-image version. Fields advanced by another branch are escalated, never reverted. """ if compensation.branch_id == winner_branch: raise ReconciliationDivergence( f"branch {winner_branch} is the winner and must not be compensated" ) narrowed: dict[str, FieldVersion] = {} escalated: list[str] = [] for fname, pre in compensation.fields.items(): if fname not in live: escalated.append(fname) continue if live[fname].version != pre.version: # Another branch advanced this field since our snapshot. escalated.append(fname) continue narrowed[fname] = pre if not narrowed and escalated: # Entire mutation set superseded: no-op, but must be recorded. raise ReconciliationDivergence( f"branch {compensation.branch_id} fully superseded; " f"fields {escalated} advanced past pre-image, escalate to {winner_branch}" ) for fname, pre in narrowed.items(): live[fname] = FieldVersion(value=pre.value, version=pre.version + 1) return live ```