# Where must an instance reconcile speculative branch effects before committing a shared memory write **Date of Record:** 2026 November 23 **Scope:** Applies to orchestrators that fan out an agent task into N parallel speculative branches sharing a single mutable episodic store, then select one branch and roll back the rest. Does not apply to read-only fan-out (map/reduce over stateless workers) or to branches with fully isolated stores reconciled only at a final merge. ## Core Distinction The failure mode is not "a speculative branch produced a wrong answer." It is that a *discarded* branch already mutated shared state before the winning branch committed. Parallel rollout assumes rollback is a state operation; in practice rollback of an LLM agent branch is a *semantic* operation, because the branch's side effects are not a byte-for-byte log you can truncate — they are writes to a memory store whose entries were produced by probabilistic reasoning and may have been *read and built upon* by sibling branches during the speculation window. The invariant that must hold: **no branch's effects may be observable to any other branch until a single branch is committed, and commit must be atomic across the memory store, the tool-effect ledger, and the branch's own checkpoint lineage.** Violating this produces "speculative bleed": the winner's reasoning is contaminated by facts only a loser established, and the rollback silently deletes evidence the winner depended on — or fails to delete it, leaving orphaned semantic state. ## What Was Observed A planner agent decomposed a task into a shared sub-goal and launched three speculative branches (A, B, C) against one episodic store keyed by `(task_id, entity)`. Branch A executed a tool that wrote `entity.owner = "team-x"` into episodic memory at t=120ms. Branch B, at t=180ms, performed a retrieval over the same key space and received A's write as if it were established fact, then planned a downstream tool call conditioned on `owner == "team-x"`. Branch C won selection at t=400ms. The orchestrator rolled back A and B by replaying their effect logs in reverse. Two defects surfaced: 1. **B's derived plan was never in B's own effect log** — it was a reasoning artifact, not a tool write. Rollback removed A's `owner` write but left B's *plan step* (a checkpointed action referencing `team-x`) intact in the winning lineage because B's checkpoint had been folded into the shared resume cursor before selection. 2. **A's write had been read by B**, so A's "rollback" was not a no-op in causal terms: the counterfactual state B reasoned over no longer existed, yet B's contribution to the winner's context did. The commit produced a task state where the winner's action list referenced an entity owner that the store no longer contained. On resume from checkpoint, the agent re-derived `owner` from a stale semantic summary and executed a permissioned tool call against the wrong team. The failure was detected only because the tool returned a 403 — not by any state check. ## Condition of Validity Rollback-as-truncation is valid **only if** all of the following hold: - Branch effect logs are *complete*: every state mutation, including derived checkpoint entries and resume-cursor advances, is captured, not just external tool calls. - No cross-branch reads occur during the speculation window — i.e., branches are read-isolated, not merely write-isolated. - Selection and commit are a single atomic transition; there is no window in which a loser's checkpoint is visible to the winner's lineage. If any condition fails, reverse-replay rollback does not restore the pre-speculation state; it restores a *third* state that no branch reasoned over. LLM branches are especially prone to breaking condition 2 because retrieval is implicit and eager: a branch does not "decide" to read sibling state, its retriever returns whatever matches the key space. ## Decision Boundary The orchestrator must decide, per branch, whether its effects are **isolable** (safe to speculate) or **entangling** (must serialize). The boundary is drawn by effect class: | Effect class | Speculate in parallel? | Rollback mechanism | |---|---|---| | Pure reasoning / scratchpad | Yes | discard buffer | | Read of frozen snapshot | Yes | no-op | | Write to branch-local store | Yes | truncate namespace | | Write to shared episodic key | **No** | must serialize or use MVCC versioning | | Derived checkpoint / resume cursor | **No** | commit-gated, never folded pre-selection | The precise rule: a branch may speculate freely iff every shared-store write is tagged with a `branch_version` and all reads are pinned to the snapshot at speculation start (snapshot isolation). The moment a branch needs to *observe* another branch's tentative write to make progress, speculation must collapse to serial execution for that key — there is no correct parallel semantics for a read of uncommitted probabilistic state. ## Operational Effect Enforcing snapshot isolation plus commit-gated checkpoint folding converts rollback from a best-effort replay into a version drop: discarding a branch is `store.drop_version(branch_version)`, which cannot leave orphans because nothing outside the branch ever saw that version. The winner's lineage is built exclusively from committed versions, so its checkpoint cannot reference a rolled-back fact. The cost is that any task whose branches contend on the same episodic key degrades to serial — an acceptable trade, because the alternative is a commit that is silently inconsistent with its own reasoning trace. The guardrail that catches residual violations at commit time: before folding a branch's checkpoint into the resume cursor, verify every entity reference in the branch's action list resolves against the committed store version. A dangling reference is a speculative-bleed signal and must abort the commit, not repair it. ```python from dataclasses import dataclass, field from typing import Dict, Set class SpeculativeBleedError(RuntimeError): """Raised when a branch's committed lineage references state that only a discarded sibling established, or when a shared-key write escapes its branch version during the speculation window.""" @dataclass class Branch: branch_id: str version: int written_keys: Set[str] = field(default_factory=set) action_entity_refs: Set[str] = field(default_factory=set) class SpeculativeStore: def __init__(self) -> None: self._committed: Dict[str, str] = {} self._tentative: Dict[int, Dict[str, str]] = {} self._snapshot_version: int = 0 def begin(self, branch_id: str) -> Branch: version = self._snapshot_version + 1 self._tentative[version] = {} return Branch(branch_id=branch_id, version=version) def write(self, branch: Branch, key: str, value: str) -> None: # Writes land only in the branch's own version namespace. self._tentative[branch.version][key] = value branch.written_keys.add(key) def read(self, branch: Branch, key: str) -> str: # Snapshot isolation: a branch sees committed state at speculation # start plus its OWN tentative writes -- never a sibling's. if key in self._tentative[branch.version]: return self._tentative[branch.version][key] if key in self._committed: return self._committed[key] raise KeyError(key) def commit(self, winner: Branch, discarded: list[Branch]) -> None: # 1. Verify the winner's lineage does not depend on speculative facts. for ref in winner.action_entity_refs: resolved = ref in self._tentative[winner.version] or ref in self._committed if not resolved: raise SpeculativeBleedError( f"branch {winner.branch_id} action references '{ref}' which " f"exists only in a discarded sibling's version" ) # 2. Detect cross-branch contamination: a discarded branch must not # have written a key the winner read as committed. discarded_keys = set() for b in discarded: discarded_keys |= b.written_keys for key in winner.action_entity_refs & discarded_keys: if key not in self._tentative[winner.version]: raise SpeculativeBleedError( f"winner read '{key}' from a sibling's tentative write; " f"snapshot isolation was violated" ) # 3. Atomic fold: winner's version becomes committed, others dropped. self._committed.update(self._tentative[winner.version]) self._snapshot_version = winner.version for b in discarded: self._tentative.pop(b.version, None) self._tentative.pop(winner.version, None) def reconcile(store: SpeculativeStore, winner: Branch, losers: list[Branch]) -> None: try: store.commit(winner, losers) except SpeculativeBleedError: raise except Exception as exc: # defensive: never leave a half-folded cursor raise SpeculativeBleedError(f"non-atomic commit aborted: {exc}") from exc ```