# Which invariants prevent speculative branch collapse when reconciled rollback loses causal ordering across peer agents **Date of Record:** 2026 November 26 **Scope:** Applies to multi-agent systems where a coordinator forks speculative execution branches across peer agents, commits one branch, and rolls back the others by replaying a compensating action log. Does not apply to single-agent retry loops or to systems using transactional shared memory with hardware-level serialization. ## Core Distinction The failure mode here is not "rollback failed." Every agent in the swarm reported successful compensation. The failure is subtler: **speculative branches that were rolled back still leaked causal side effects into the committed branch's state**, because the rollback ordering was reconstructed from per-agent local logs rather than from a global happens-before relation. The distinction that matters: *compensating an effect* is not the same as *erasing the knowledge that the effect occurred*. When branch B calls a tool that mutates shared state (a cache, a vector store, a downstream ticket), and the coordinator later rolls B back, B's compensating action restores the value — but any agent that *read* the intermediate value during the speculation window has already incorporated it into its reasoning. Rollback restores state; it does not restore belief. The invariant that must hold is that no committed branch may depend on a read that observed a speculative write, and this invariant cannot be checked by inspecting final state values alone — it requires a causal read-write graph spanning all branches. ## What Was Observed A coordinator dispatched three peer agents (A, B, C) to independently attempt a schema-migration task. Each branch held a speculative lease on a shared "current schema" record. Branch B, at step 4, read `schema_version=7`, computed a migration delta assuming v7, and wrote `schema_version=8` speculatively. Branch C, at step 3 (concurrent, ~40ms later), read `schema_version=8` — B's uncommitted write — and built its plan against v8 semantics. The coordinator's verifier selected branch A as the winner (A had the lowest error score). It issued rollback to B and C. B's log contained `write(schema_version, 8)` and compensated with `write(schema_version, 7)`. C's log contained only `read(schema_version)` and had nothing to compensate. Final state: `schema_version=7`, all agents reported `rollback: success`, coordinator reported `reconciled: clean`. But branch A, during final commit, had queried the shared record at step 6 and observed `schema_version=8` transiently (B had not yet compensated — rollback was issued after A's commit began). A's committed plan therefore contained a conditional branch keyed on v8. The system shipped a migration that referenced a schema version that, in the final committed world, never existed. The inconsistency surfaced only at downstream execution, three hops later, as a "column not found" error attributed to the wrong agent. The tell: per-branch logs were individually consistent and individually compensable. The *cross-branch* read-write dependency (C-reads-B-write, A-reads-B-write) was invisible to every local log. Rollback verification passed because it checked each branch's compensation in isolation. ## Condition of Validity The guardrail is a **speculative read-set invalidation invariant**: > A branch may commit only if, for every read it performed during speculation, the value read was produced by a write that is either (a) in the branch's own write-set, or (b) in the committed write-set of a branch that was already committed *before* the read, or (c) an immutable base value. Any read whose source write belongs to a branch that is later rolled back must invalidate the reader. This requires the coordinator to maintain a global versioned read-write graph, not per-agent logs. Each shared-state write carries a `(branch_id, seq)` tag; each read records the `(branch_id, seq)` of the write it observed. On commit of branch A and rollback of B, the coordinator must scan every committed branch's read-set for tags matching B's write-set. If any match exists, A's commit is *poisoned* — it must be re-derived, not merely flagged. The condition is only valid when reads are tagged at observation time. If shared state is read through an untagged path (a cache that strips provenance, a batch read that collapses versions), the invariant silently degrades to "best effort" and the failure above recurs. It also does not hold when branches can perform *non-compensable* external effects (sent emails, charged payments); there the correct response is to forbid speculation over that resource entirely, not to attempt rollback. ## Decision Boundary The coordinator faces a fork at reconciliation time: - **If the global read-write graph is complete and every read is provenance-tagged:** commit the winner, roll back losers, and *re-validate* the winner against the post-rollback state. If a re-validation read now returns a different value than during speculation, the winner is poisoned → restart it with fresh state, do not patch. - **If the graph is incomplete (untagged reads exist):** the system cannot prove the winner is clean. The safe boundary is to treat all speculatively-observed shared state as tainted and force a full re-execution of the winner against the base snapshot. This is expensive but sound. - **If any branch touched a non-compensable resource:** speculation is invalid for that resource; the coordinator must serialize access or reject the branch before dispatch. Do not cross the boundary by "checking final state equality." Final-state equality is necessary but not sufficient — the failure above had identical final state (`schema_version=7`) and still shipped a poisoned plan. ## Operational Effect The practical consequence is a shift in what "rollback success" means. It no longer certifies that the world is clean; it certifies only that each branch's own writes were compensated. The coordinator must additionally certify **read-set purity** of the committed branch, which is a property of the *observations* the winner made, not of the writes it performed. Implementations that adopt this invariant report two changes: (1) commit latency rises because the winner is re-validated against post-rollback state, and (2) a new failure class appears — *poisoned commit* — which is correct and must be handled by re-derivation rather than retry. Systems that do not adopt it continue to report clean reconciliations while occasionally shipping plans derived from counterfactual state, with the defect surfacing far downstream and misattributed. The invariant to hold is simple to state and hard to enforce: **a committed branch may never have observed a write that was later rolled back.** ```python from dataclasses import dataclass, field from typing import Dict, Set, Tuple class SpeculativeCommitPoisoned(RuntimeError): """Raised when a branch to be committed has read state written by a rolled-back branch.""" pass @dataclass class WriteTag: branch_id: str seq: int @dataclass class Branch: branch_id: str write_set: Set[WriteTag] = field(default_factory=set) read_set: Set[WriteTag] = field(default_factory=set) # provenance of observed writes def reconcile( winner: Branch, rolled_back: list[Branch], revalidate_read, ) -> None: """ Commit `winner`, roll back `rolled_back` branches. `revalidate_read(tag) -> current_value` re-reads state for a given provenance tag. """ rolled_back_tags: Set[WriteTag] = set() for b in rolled_back: rolled_back_tags |= b.write_set # Invariant check: winner must not have observed any rolled-back write. poisoned = winner.read_set & rolled_back_tags if poisoned: raise SpeculativeCommitPoisoned( f"branch {winner.branch_id} observed writes from rolled-back branches: " f"{sorted((t.branch_id, t.seq) for t in poisoned)}; " f"commit must be re-derived against base snapshot, not patched." ) # Even with a clean read-set, re-validate observed values post-rollback: # a write may have been compensated after the winner read it. for tag in winner.read_set: observed = getattr(tag, "_observed_value", None) current = revalidate_read(tag) if observed is not None and current != observed: raise SpeculativeCommitPoisoned( f"branch {winner.branch_id} read {tag.branch_id}:{tag.seq} " f"as {observed!r} but post-rollback state is {current!r}; " f"winner is stale and must restart." ) def run_reconciliation(winner: Branch, losers: list[Branch], revalidate_read) -> str: try: reconcile(winner, losers, revalidate_read) except SpeculativeCommitPoisoned as e: # Correct handling: re-derive winner, do not retry blindly. return f"RE_DERIVE: {e}" return f"COMMITTED: {winner.branch_id}" ```