# Which invariants prevent speculative branch rollback from corrupting shared agent state during conflict reconciliation **Date of Record:** 2026 November 26 **Scope:** Applies to orchestrators that fan out parallel LLM agent branches speculatively (optimistic execution) against a shared mutable workspace—memory store, tool-side effects, or a shared scratchpad—and later reconcile conflicting branches by rollback. Does not apply to branches executed against immutable, copy-on-write snapshots with no external side effects. ## Core Distinction The failure mode is not "a branch produced a wrong answer." It is **rollback of a speculative branch that already leaked a non-idempotent side effect into shared state before its conflict was detected**. Speculative execution assumes a branch's effects are *reversible* or *isolated* until commit. The non-obvious boundary is that LLM agents produce effects along a spectrum of reversibility—writes to an in-context scratchpad are trivially reversible, writes to a vector memory store are reversible *if versioned*, but tool calls that mutate external systems (file writes, API POSTs, message sends, cache invalidation) are **not reversible by the orchestrator** because the orchestrator never held the pre-state. The invariant that fails is the implicit assumption `reversible(effect) = true` for all effects emitted during speculation. It is false for any branch that escapes the sandbox, and the escape is often *probabilistic*: the same branch with temperature 0.7 may call `write_file` on one rollout and `read_file` on another, so a conflict detected on rollout A does not imply rollout B was clean. ## What Was Observed An orchestrator dispatched three branches (B1, B2, B3) from a shared checkpoint at step 12 to explore alternative plans. Each branch ran with `sandbox=isolated_fs`, but the sandbox mounted the *shared* vector memory store read-write for "convenience" so branches could recall distilled facts. B1 and B2 both decided to "consolidate" a memory entry: each read the entry, appended a summary, and wrote it back. B1 committed first (its branch won a heuristic score). B2 was rolled back on conflict. The rollback reverted B2's *scratchpad* and *plan state*, but the memory store write had already landed and was not versioned—so the reconciler reverted to "the state before B2," which was actually *after* B1's commit. The store now held B1's summary concatenated with B2's, a state no branch ever legitimately produced. Downstream, B3's retrieval of that entry returned a merged summary that contradicted both plans, causing B3 to abandon its task and re-derive from scratch—a silent 40% token-budget overrun. The trace: `checkpoint(step=12) → fanout(B1,B2,B3) → B1.write(mem, v1) → B2.write(mem, v1+Δ) → reconcile(winner=B1) → rollback(B2) → mem = v1+Δ` (should have been `v1`). The conflict detector compared *plan hashes*, not *effect logs*, so it never saw the double-write. Rollback operated on the orchestrator's model of branch state, which diverged from the actual store state at the moment B2's write committed. ## Condition of Validity Rollback-based reconciliation is sound **only if** every speculative effect is either (a) applied to a versioned, copy-on-write store keyed by `(branch_id, base_version)`, or (b) recorded in an effect log that the reconciler replays in reverse. The invariant is: > `∀ effect e emitted during speculation: commit(e) is deferred until branch selection, AND rollback(e) is defined as a function of the recorded pre-state, not of the orchestrator's branch model.` This fails whenever a branch can emit an effect the orchestrator did not intercept—direct tool calls, shared-store writes, or any path where `pre_state` was read by the branch rather than captured by the harness. It also fails when branches share a *read* dependency that a *write* in another branch mutates: B2 read `v1`, but by the time it wrote, B1 had changed the base, so B2's write is a lost-update on a stale base. The condition of validity therefore requires **effect interception at the tool boundary** (every mutating tool call goes through a versioned proxy that stamps `base_version` and rejects the write if `base_version ≠ current_version`), not merely sandbox isolation of the filesystem. ## Decision Boundary The reconciler must choose between **optimistic rollback** and **serialized commit** at the point where a branch's effect set intersects shared mutable state. The decision rule: - If `effects(branch) ∩ shared_mutable = ∅`: rollback is safe; discard branch state, keep effects. - If `effects(branch) ∩ shared_mutable ≠ ∅` **and** the store enforces `base_version` checks: rollback is safe *only* for branches whose writes were rejected (stale base); branches whose writes succeeded are **committed and cannot be rolled back**—they must be reconciled by *merge*, not revert. - If `effects(branch) ∩ shared_mutable ≠ ∅` **and** no version check exists: rollback is **unsound**. The orchestrator must either halt and surface the conflict to a human/auditor, or adopt a "last-writer-wins with audit trail" policy and accept that the reconciled state is a *new* state, not a reverted one. The boundary is crossed the moment a branch's tool call mutates shared state without a captured pre-state. Past that line, "rollback" is a fiction; the correct operation is "compensating merge," which requires the reconciler to know *what* the branch did, which requires the effect log to have been written *before* the effect landed (write-ahead), not after. ## Operational Effect Without the version-check invariant, speculative fan-out silently corrupts shared memory: merged entries that no branch authored, lost updates where a stale-base write overwrites a committed one, and downstream branches that retrieve the corrupted entry and diverge. The corruption is *non-deterministic* because branch tool-call sequences vary across rollouts, so the same conflict may or may not corrupt state depending on sampling. The guardrail is a **write-ahead effect log with base-version stamping at the tool proxy**, plus a reconciler that classifies each branch effect as `committed`, `rejected-stale`, or `unlogged` (and treats `unlogged` as a hard failure requiring escalation). The cost is that speculation can no longer "just roll back"—it must *merge*, and merging requires the branch to have declared its effect set upfront or the proxy to have intercepted every mutation, which means the sandbox must not expose any mutating path that bypasses the proxy. ```python class SpeculativeRollbackViolation(RuntimeError): """Raised when a speculative branch's effect cannot be safely rolled back because it mutated shared state without a captured pre-state.""" pass class EffectLog: def __init__(self): self.entries = [] # (branch_id, effect, base_version, committed) def record(self, branch_id, effect, base_version): # Write-ahead: log BEFORE the effect lands. self.entries.append( {"branch": branch_id, "effect": effect, "base_version": base_version, "committed": False} ) def commit(self, branch_id, current_version): for e in self.entries: if e["branch"] == branch_id: if e["base_version"] != current_version: raise SpeculativeRollbackViolation( f"branch {branch_id} wrote on stale base " f"{e['base_version']} != {current_version}; " f"effect {e['effect']} cannot be reverted, must merge" ) e["committed"] = True def reconcile(winner_id, loser_id, log, current_version): # Losers may only be rolled back if every effect was intercepted and # either rejected-stale or is a pure scratchpad effect (no shared mutation). for e in log.entries: if e["branch"] == loser_id and not e["committed"]: if e.get("shared_mutation", True): raise SpeculativeRollbackViolation( f"cannot roll back branch {loser_id}: effect {e['effect']} " f"reached shared state without a reversible pre-state; " f"escalate to compensating merge" ) log.commit(winner_id, current_version) return "reconciled" ```