# How does an instance detect that a rolled-back speculative branch has already leaked side effects into a sibling branch's shared tool cache **Date of Record:** 2026 December 2 **Scope:** Applies to orchestrators that run speculative parallel branches (same task, divergent plans) over a shared, mutable tool-result cache, where branches may be rolled back and survivors reconciled. Does not apply to branches with fully isolated caches or to read-only tool surfaces. ## Core Distinction The failure here is not "a branch was rolled back" and not "two branches disagreed." It is that **rollback is a control-plane operation, while the side effects of a branch's tool calls live in a data plane the orchestrator does not own.** When branch B is speculatively killed, the orchestrator discards B's plan and its private state. But if B's tool calls wrote into a *shared* cache keyed by (tool, normalized-args), those entries survive the rollback and become indistinguishable from entries produced by the surviving branch A. A survivor that later reads the cache gets B's now-orphaned output, treats it as authoritative, and reconciles against a dead branch's ghost. The sharp, non-obvious point: the cache entry is *correct data* (the tool really returned it) but *invalid provenance* (it came from an execution lineage that was cancelled). Correctness of value and validity of lineage diverge, and cache-hit logic only checks the former. ## What Was Observed A three-branch speculative run (A, B, C) over a research task. All branches call `search(query)` and `fetch(url)` with overlapping arguments. Branch B takes a divergent plan, calls `fetch("https://x/report")`, receives a 200 with stale-but-parseable content, and writes `cache[("fetch","x/report")] = payload`. The orchestrator's verifier scores B below threshold at step 4 and issues `rollback(B)`. Rollback clears B's message log, its scratchpad, and its pending tool queue — but the cache write already committed. Branch A, at step 6, calls `fetch("https://x/report")` and gets a **cache hit**. It never issues the network call. It proceeds to synthesize an answer citing content that only branch B ever fetched, from a plan the orchestrator already judged wrong. The reconciliation step then reports high confidence because two "independent" branches (A's citation and the cached artifact) agree — except they don't: the agreement is between A and the *ghost of B*. Detection signal observed in the trace: the cache-hit path in A emitted no `tool.invoked` span, yet the artifact's `producer_branch` tag read `B`, and B's `branch.status` was `rolled_back`. That triple — hit, foreign producer, dead producer — is the only reliable fingerprint. Value equality checks miss it entirely. ## Condition of Validity The invariant that must hold is: **no cache entry produced by a branch may be consumed by a sibling branch after that producer's rollback commits, unless the entry is re-validated under the consumer's own lineage.** This holds trivially when caches are per-branch or when rollback is implemented as *cache invalidation by producer tag* rather than *state discard*. It fails precisely when: 1. The cache is shared across sibling branches (common for cost reasons). 2. Cache keys are content-addressed by (tool, args) with no lineage component. 3. Rollback is implemented as "drop branch state" without a reverse-index from branch → cache keys it wrote. 4. The surviving branch's plan overlaps the dead branch's tool-argument space (high overlap under speculative breadth). Absent all four, the ghost-read cannot occur. The dangerous regime is exactly *high speculative breadth + high argument overlap + shared cache + rollback-without-invalidation*. ## Decision Boundary The boundary is: does the orchestrator treat the cache as **owned by the run** or **owned by the branch**? If run-owned, rollback MUST trigger a producer-tagged invalidation sweep before any sibling proceeds past the rollback barrier. If branch-owned, the cache must be namespaced by branch id and reconciliation must explicitly merge only from *surviving* namespaces. The subtle sub-boundary: a naive fix — "invalidate all cache keys B touched" — over-invalidates when A and B legitimately produced the *same* key with identical results. Blanket invalidation throws away A's valid entry too. The correct rule is to invalidate only keys whose *sole writer* was B; keys co-written by a survivor are retained but their provenance must be rewritten to the survivor. This requires tracking writer multiplicity per key, not just last-writer. Do not roll back a branch past a cache commit without either (a) a reverse index from branch to written keys, or (b) a monotonic `rollback_epoch` stamped on every cache entry at write time and compared against the consumer's branch epoch at read time. Option (b) is cheaper and is the recommended guardrail: a cache hit whose `write_epoch` exceeds the consumer's `branch_start_epoch` and whose producer is now dead is rejected and re-executed. ## Operational Effect Without the guardrail, speculative execution silently converts a *quality* mechanism (run several plans, keep the best) into a *contamination* mechanism: dead branches seed the survivors' evidence base, and the verifier's agreement signal is inflated by ghosts. The observable symptom is a run that reports high inter-branch consensus and passes verification while citing artifacts no surviving branch ever fetched. Confidence goes up exactly when it should go down. With the epoch guardrail, the ghost read becomes a cache miss, the survivor re-executes the tool under its own lineage, and consensus reflects only live branches. ## ```python from dataclasses import dataclass, field from typing import Dict, Tuple class GhostCacheReadError(RuntimeError): """Raised when a surviving branch reads a cache entry produced by a rolled-back sibling.""" pass @dataclass class CacheEntry: value: object producer_branch: str write_epoch: int co_writers: set = field(default_factory=set) @dataclass class Branch: id: str start_epoch: int status: str = "live" # live | rolled_back class SharedToolCache: def __init__(self) -> None: self._store: Dict[Tuple[str, str], CacheEntry] = {} self._epoch = 0 def write(self, key: Tuple[str, str], value: object, branch: Branch) -> None: self._epoch += 1 existing = self._store.get(key) if existing is None: self._store[key] = CacheEntry(value, branch.id, self._epoch, {branch.id}) else: existing.co_writers.add(branch.id) existing.value = value existing.write_epoch = self._epoch def read(self, key: Tuple[str, str], consumer: Branch, branches: Dict[str, Branch]) -> object: entry = self._store.get(key) if entry is None: return None producer = branches.get(entry.producer_branch) # Ghost detection: producer is dead AND this entry was written after the # consumer even started, AND no surviving branch co-wrote it. if (producer is not None and producer.status == "rolled_back" and entry.write_epoch > consumer.start_epoch and not (entry.co_writers - {entry.producer_branch})): raise GhostCacheReadError( f"branch {consumer.id} hit cache key {key} produced solely by " f"rolled-back branch {entry.producer_branch} (epoch {entry.write_epoch} " f"> consumer start {consumer.start_epoch}); must re-execute under own lineage" ) return entry.value def invalidate_producer(self, branch_id: str) -> None: # Rollback barrier: drop keys whose sole writer died. for key in [k for k, e in self._store.items() if e.co_writers == {branch_id}]: del self._store[key] for e in self._store.values(): e.co_writers.discard(branch_id) ```