# When must an instance abort a speculative branch: rollback divergence under shared-workspace speculation **Date of Record:** 2026 November 30 **Scope:** Applies to multi-agent systems where a coordinator forks N speculative worker branches against a shared mutable workspace (filesystem, database, or tool-side effect ledger) with the intent to commit exactly one branch and roll back the rest. Does not apply to read-only fan-out, stateless map-reduce, or systems where every branch owns a private, isolated sandbox and merge is a pure diff operation. ## Core Distinction The distinction that matters is between **rollback of state** and **rollback of belief**. A speculative branch that writes only to a private overlay can be discarded by dropping the overlay — state rollback is total and cheap. The failure mode examined here is subtler: a branch executes *observable* side effects (a tool call that mutates an external service, a message emitted to a peer agent, a cache write visible to siblings) *before* the coordinator decides which branch wins. When the branch is later abandoned, the coordinator rolls back the workspace but cannot roll back the effects the branch already propagated into the environment or into sibling agents' context. The siblings continue reasoning on a world model that includes the abandoned branch's outputs. This is not a transactional-isolation problem in the database sense — the branches are not racing on the same rows. It is a **causal contamination** problem: speculative execution leaks evidence of *losing* branches into the *winning* branch's information set, and the leak is invisible to the coordinator because it happens through the shared environment rather than through the coordinator's own message bus. The invariant that must hold for speculation to be sound: **a branch may not produce an effect observable to any other branch or to the commit decision unless that effect is registered in the coordinator's effect ledger and is either (a) idempotent and reversible, or (b) gated behind the commit.** Violations of this invariant are what produce divergence that rollback cannot repair. ## What Was Observed A three-branch speculative planner was deployed on a task requiring a repository refactor. The coordinator forked branches A, B, and C with identical initial context. Each branch was given a private git worktree but shared a single tool endpoint: a code-search service backed by a read-through cache keyed on query string, and a "notify" tool that posted status to a shared channel the coordinator subscribed to. Branch A ran first (speculation was staggered by 200ms to reduce contention). Its first action was a search for `parse_config_v2`. The cache was cold, so the search service executed the query against the live index and populated the cache. Branch A then took a path the coordinator would eventually reject — it attempted a rename of a module that other branches depended on, and emitted a `notify` event: `"renaming config module, dependents updated"`. Branches B and C, starting 200ms later, issued the same search. They hit the warm cache and received results that included the *post-rename* index state, because the search service's indexer had already picked up branch A's filesystem change (the worktree was private, but the indexer watched a path that was not). Branches B and C therefore reasoned about a module namespace that only existed in branch A's abandoned timeline. Branch B's plan cited `config.legacy` as "already migrated," and it skipped the migration step. Branch C's plan referenced a symbol that branch A had created, which did not exist in the base commit. The coordinator selected branch B on a heuristic score (fewest tool calls, cleanest diff against base). Branch B committed. Its diff did not include the migration it had skipped, because it believed the migration was done. The `notify` event from branch A remained in the shared channel and in the coordinator's own context window, reinforcing the belief that the migration had occurred. Post-commit verification failed: the module was unmigrated, and a dependent import in branch B's diff referenced a symbol that existed only in A's discarded worktree. The coordinator's rollback of A and C was complete with respect to the workspace and *incomplete with respect to the cache, the index, and the shared channel*. The failure was not detected at rollback time. It was detected two steps later, when a downstream test invoked the missing symbol. The trace showed the coordinator had no record that branches B and C had ever observed branch A's effects — the effect ledger contained only branch A's own writes, not the *reads* that were contaminated by them. ## Condition of Validity Speculative rollback is sound only when all four of the following hold simultaneously: 1. **Effect isolation is closed under reads.** Not only must a branch's writes be invisible to siblings, but any *derived* state (caches, indexes, embeddings, materialized views) that a sibling reads must also be isolated or versioned. A read-through cache that a losing branch populated is a write in disguise. The condition fails the moment a shared read path is backed by a shared write path. 2. **The effect ledger records reads, not just writes.** The coordinator must be able to answer, for any committed branch, "which of this branch's inputs were influenced by a branch that was later abandoned?" If the ledger only records mutations, contamination through reads is undetectable at commit time. 3. **Commit selection is causally prior to effect propagation.** The decision of which branch wins must be made before any branch's effects cross the isolation boundary. If selection depends on a heuristic computed *after* branches have already leaked (as in the observed case, where the heuristic favored the branch that had been contaminated), selection is itself contaminated and cannot be trusted. 4. **Rollback is defined over the transitive closure of effects, not the workspace.** Rollback must enumerate every external surface a branch touched — caches, channels, peer agent contexts, indexer state — and either reverse or invalidate each. Workspace-only rollback is a partial rollback and silently violates the invariant. When any of these fails, the system is in a state where the coordinator's belief about branch independence is false, and the commit decision is being made on corrupted evidence. ## Decision Boundary The operational question is: **when must an instance abort a speculative branch rather than continue it?** The answer is not "when the branch errors" — errors are cheap and local. The correct trigger is when the branch's continued execution could produce an effect that is (i) observable outside its isolation boundary, (ii) not registered as reversible in the effect ledger, and (iii) not yet gated by the commit decision. Concretely, an instance must abort a speculative branch at the first of these events: - A tool call whose side effect is not declared reversible *and* whose output is readable by a sibling (shared cache, shared channel, shared index). The branch must abort *before* the tool returns, not after, because the return value is already the contamination. - A read whose result cannot be attributed to the base state — i.e., a read that returns data inconsistent with the snapshot the branch was forked from. This is the signal that a sibling has already leaked. The branch must abort and the coordinator must invalidate the fork point. - A `notify` or peer-message emission, unless the message is tagged with the branch id and the receiving agents are required to filter on committed-branch tags. The boundary is *not* "abort on any divergence from the plan." Divergence is the point of speculation. The boundary is divergence that *escapes the ledger*. A branch that diverges, fails, and rolls back cleanly is a successful speculation. A branch that diverges and leaves a trace a sibling reads is a failed speculation regardless of whether it was ultimately selected. The coordinator must also abort *the commit* — not just the branch — when the selected branch's input set intersects the effect ledger of any abandoned branch. In the observed case, branch B's input set included a cache read that branch A had populated. That intersection should have blocked commit and forced a re-fork from a clean snapshot. ## Operational Effect The effect of enforcing this boundary is that speculation becomes *conservative by construction*: branches may only take actions that are either pure or gated. This costs throughput — branches that want to call a mutating tool must either wait for the commit decision (defeating speculation) or run against a private, verifiable copy of the tool's state. In practice the cost is bounded by the number of non-idempotent tools in the branch's allowed action set, which is usually small. The effect of *not* enforcing it is the observed failure: silent contamination that survives rollback, corrupts the selection heuristic, and surfaces as a downstream error whose root cause is two steps removed from the commit. The failure is expensive to diagnose because the trace looks clean — every branch's own log is internally consistent, and the contamination is only visible in the *cross-branch read graph*, which the coordinator never recorded. The architectural guardrail is a **fork-point snapshot with read-versioning**: every branch is forked from an immutable snapshot id, every read returns the snapshot id it was served from, and the coordinator rejects commit if the selected branch's read set contains any snapshot id other than its own fork point. This is cheap to implement (a version tag on cache entries and index reads) and it converts an undetectable contamination into a hard commit-time assertion. The companion guardrail is an **effect ledger that records reads as first-class entries**, so that the abort decision can be made on the same evidence the commit decision will later use. ```python from __future__ import annotations from dataclasses import dataclass, field from typing import Iterable class SpeculativeContaminationError(RuntimeError): """Raised when a speculative branch's read set intersects an abandoned branch's effect set, or when a branch attempts an unledgered observable effect. Signals that rollback cannot restore a sound commit decision.""" @dataclass(frozen=True) class ForkPoint: snapshot_id: str @dataclass class EffectLedger: # writes: branch_id -> set of observable effect ids it produced writes: dict[str, set[str]] = field(default_factory=dict) # reads: branch_id -> set of (effect_id, snapshot_id) it consumed reads: dict[str, set[tuple[str, str]]] = field(default_factory=dict) abandoned: set[str] = field(default_factory=set) def record_write(self, branch_id: str, effect_id: str) -> None: self.writes.setdefault(branch_id, set()).add(effect_id) def record_read(self, branch_id: str, effect_id: str, snapshot_id: str) -> None: self.reads.setdefault(branch_id, set()).add((effect_id, snapshot_id)) def abandon(self, branch_id: str) -> None: self.abandoned.add(branch_id) def assert_branch_isolated( branch_id: str, fork: ForkPoint, ledger: EffectLedger, pending_effect_id: str | None = None, ) -> None: """Guard called before any observable effect and before commit. Aborts the branch if it is about to leak an unledgered effect, or if any of its reads were served from a snapshot other than its own fork point (i.e., a sibling already contaminated the read path). """ if pending_effect_id is not None and pending_effect_id not in ledger.writes.get( branch_id, set() ): raise SpeculativeContaminationError( f"branch {branch_id} attempted observable effect {pending_effect_id!r} " f"without ledger registration; rollback would be partial" ) for effect_id, snapshot_id in ledger.reads.get(branch_id, set()): if snapshot_id != fork.snapshot_id: raise SpeculativeContaminationError( f"branch {branch_id} read effect {effect_id!r} from snapshot " f"{snapshot_id!r}, expected fork point {fork.snapshot_id!r}; " f"input set is contaminated by a sibling timeline" ) def assert_commit_sound( selected: str, fork: ForkPoint, ledger: EffectLedger, ) -> None: """Guard called at commit. Rejects commit if the selected branch's read set intersects any abandoned branch's effect set.""" abandoned_effects: set[str] = set() for b in ledger.abandoned: abandoned_effects |= ledger.writes.get(b, set()) for effect_id, _snapshot in ledger.reads.get(selected, set()): if effect_id in abandoned_effects: raise SpeculativeContaminationError( f"commit of branch {selected!r} rejected: its input {effect_id!r} " f"was produced by an abandoned branch; selection heuristic is " f"operating on corrupted evidence" ) assert_branch_isolated(selected, fork, ledger) ```