# How should an instance reconcile speculative branches when a parent agent commits a rollback mid-speculation **Date of Record:** 2026 November 30 **Scope:** Applies to orchestrators that launch speculative child subtasks (parallel pre-execution of likely next steps) under a shared mutable state substrate, where the parent may issue a rollback/commit decision while children are still mid-flight. Does not apply to purely stateless fan-out where children cannot mutate shared state, nor to single-agent linear execution. ## Core Distinction Speculative execution of agent subtasks is not the same as parallel fan-out. In fan-out, every branch's output is a *candidate* and the parent selects among them at join time. In speculative execution, branches are *pre-committed against a predicted future state*: each child reads a snapshot of the parent's working memory, assumes a particular parent decision (e.g., "the tool call will succeed," "the retrieved chunk is relevant"), and writes effects — memory writes, tool side-effects, checkpoint deltas — that are only valid if that assumption holds. The boundary failure is this: **a speculative child can observe a state version that the parent has already rolled back, and the child's eventual commit will silently resurrect the rolled-back state.** The parent's rollback is a version-tombstone event; the child's write is a version-blind upsert. When these interleave, the rollback is not "undone" in a way anyone notices — it is *shadowed*, and the parent proceeds believing it reverted to snapshot $S_0$ while the substrate now holds $S_0 \oplus \Delta_{\text{child}}$. This is distinct from a lost-update race (two writers, one wins). Here there is only *one* legitimate writer (the parent) and *one* speculative writer whose legitimacy was revoked mid-flight. The bug is that revocation is not propagated to the child's write path. ## What Was Observed A planning agent at step 7 issues a retrieval tool call and, predicting success, speculatively spawns a summarizer child against the *post-retrieval* memory snapshot $S_7^{+}$. The child begins distilling the retrieved chunk into an episodic-to-semantic memory write. Meanwhile the retrieval returns a low-confidence result. The parent's verifier rejects it and issues `rollback(to=S_6)` — discarding the retrieval result and any dependent state — then re-plans with a different query. The rollback succeeds: parent memory returns to $S_6$, the rejected chunk is gone. The summarizer child, unaware, finishes 400ms later and commits its semantic memory write. The write carries `base_version=S_7+`, which no longer exists. The substrate's upsert logic does not check `base_version` liveness — it only checks that the *key* is writable. The semantic memory now contains a distilled summary of a chunk the parent explicitly rejected. Downstream, at step 11, the parent retrieves that semantic memory, treats it as validated knowledge, and builds a plan on a fact it discarded five steps earlier. No error is raised anywhere. The trace looks clean: rollback succeeded, child succeeded, memory write succeeded. The corruption is only visible by diffing the semantic store against the parent's accepted-chunk ledger — an audit that is almost never run. The failure is intermittent because it requires the child's commit to land in the window between the parent's rollback and the next parent write to the same key. If the parent writes that key first, the child's stale upsert either clobbers it (a visible regression, caught by eval) or is rejected by a key-version guard (if one exists). The dangerous case is the *quiet* interleaving: rollback, then child commit, then parent never touches that key again. ## Condition of Validity The invariant that must hold for speculative execution to be sound: > For every speculative child $c$ spawned against snapshot version $v_c$, and every parent state mutation event $e$ (commit or rollback) with version $v_e > v_c$: $c$'s effects may be applied to the substrate **iff** $v_c$ is still on the parent's live lineage at the moment of application, and the parent has not issued a rollback that tombstones $v_c$. The condition fails in three concrete ways, each of which must be independently guarded: 1. **Lineage-blind apply.** The child's commit path checks key existence but not whether `base_version` is an ancestor of the current head. Fix: commit must be a compare-and-swap on the *lineage*, not the key. `apply(delta, base=v_c)` must fail if `v_c` is not in `ancestors(head)`. 2. **Rollback is not a broadcast.** The parent's rollback mutates its own view but does not signal in-flight children. A child that has already passed its "am I still valid?" check will commit anyway. Fix: rollback must increment a generation counter that children re-check immediately before the write, and the write must be atomic with that check. 3. **Speculation depth without a horizon.** Children spawned against $S_n$ that themselves spawn grandchildren against $S_{n+1}$ create a tree of speculative writes whose validity is entangled. A single rollback at the root invalidates the entire subtree, but each node's commit check only consults its immediate parent. Fix: speculative branches must carry the full ancestor version set, and apply must verify *all* of them are live. ## Decision Boundary The choice is not "speculate or don't." It is **how much reconciliation work you are willing to do at join time versus how much validity checking you push into the write path.** - **Push checking into the write path (CAS on lineage + generation counter).** Cost: every speculative write pays a liveness check; writes can fail late and force child re-planning. Benefit: rollback is always sound; no audit needed. This is the correct default for any agent where memory writes feed later reasoning. - **Defer to join-time reconciliation (parent collects child deltas, filters against its own accepted ledger).** Cost: requires the parent to hold child deltas in a buffer and run a reconciliation pass; children that wrote directly to the substrate bypass this entirely. Benefit: cheaper per-write. This is only safe if children *cannot* write directly to shared memory — i.e., they return deltas and the parent applies them. If any child has a direct write path, join-time reconciliation is unsound. - **No speculation across rollback-capable boundaries.** If the parent can roll back, do not speculate children that mutate the rolled-back region. Speculate only against read-only or append-only regions where rollback cannot tombstone the base version. The boundary is crossed when a child's base version is *mutable by the parent's rollback*. Read-only speculative regions (e.g., pre-fetching a chunk the parent has not yet committed to memory) are safe. Mutable-region speculation requires the write-path guard. ## Operational Effect The observable signature of this failure class is **rollback-resurrection**: a memory key that the parent's trace shows as reverted, but which later appears in reasoning with content traceable to a discarded branch. It is nearly invisible in per-step logs because every step reports success. It surfaces only under (a) lineage diffing between parent accepted-state and substrate state, or (b) downstream reasoning that contradicts an earlier rejection. The operational mitigations, in order of leverage: 1. **Version the substrate, not just the keys.** Every write carries `base_version` and `ancestor_set`; apply is a CAS that rejects if any ancestor is tombstoned. This converts a silent resurrection into a loud `SpeculationBaseInvalidated` error at the child's commit. 2. **Propagate rollback as a generation bump, not a state overwrite.** Children re-read the generation immediately before write; the read-check-write must be atomic (single critical section, not three calls). 3. **Bound speculation depth to 1 against mutable regions.** Grandchild speculation against a child's speculative snapshot multiplies the invalidation surface; forbid it unless the whole subtree shares one generation handle. 4. **Audit at join, not at write.** Even with guards, run a reconciliation pass comparing each child's `base_version` against the parent's final lineage; log mismatches as `speculation_orphaned` even if the write was rejected, because a high orphan rate signals the predictor is speculating against futures the parent rarely commits. The invariant to encode in the orchestrator: **a speculative child's effects are valid only if its base version is on the parent's live lineage at apply time; rollback must tombstone the base version and bump a generation that the child's atomic write re-checks.** Without this, rollback is advisory, not authoritative, and speculative execution silently corrupts the very memory it was meant to accelerate. ```python from __future__ import annotations from dataclasses import dataclass, field from typing import FrozenSet class SpeculationBaseInvalidated(RuntimeError): """Raised when a speculative child tries to commit against a tombstoned base version.""" def __init__(self, child_id: str, base_version: int, head: int, tombstoned: FrozenSet[int]): self.child_id = child_id self.base_version = base_version self.head = head self.tombstoned = tombstoned super().__init__( f"child {child_id} base v{base_version} not live on lineage " f"(head=v{head}, tombstoned={sorted(tombstoned)})" ) @dataclass class Substrate: head: int = 0 generation: int = 0 tombstoned: set[int] = field(default_factory=set) store: dict[str, object] = field(default_factory=dict) def rollback(self, to_version: int) -> None: # Tombstone every version above the rollback target and bump generation. for v in range(to_version + 1, self.head + 1): self.tombstoned.add(v) self.head = to_version self.generation += 1 def apply( self, key: str, value: object, *, child_id: str, base_version: int, ancestor_set: FrozenSet[int], observed_generation: int, ) -> None: # Atomic re-check: generation, lineage, and ancestor liveness. if observed_generation != self.generation: raise SpeculationBaseInvalidated( child_id, base_version, self.head, frozenset(self.tombstoned) ) if base_version in self.tombstoned or base_version not in self._ancestors(self.head): raise SpeculationBaseInvalidated( child_id, base_version, self.head, frozenset(self.tombstoned) ) if any(a in self.tombstoned for a in ancestor_set): raise SpeculationBaseInvalidated( child_id, base_version, self.head, frozenset(self.tombstoned) ) self.store[key] = value def _ancestors(self, version: int) -> set[int]: return set(range(0, version + 1)) def speculative_commit( substrate: Substrate, child_id: str, key: str, value: object, base_version: int, ancestor_set: FrozenSet[int], observed_generation: int, ) -> None: try: substrate.apply( key, value, child_id=child_id, base_version=base_version, ancestor_set=ancestor_set, observed_generation=observed_generation, ) except SpeculationBaseInvalidated: # Orphaned speculation: surface to orchestrator, do not retry blindly. raise # If we reach here without raising, the write is lineage-valid. ```