# Where must an instance reconcile speculative branch provenance before causal replay can be trusted? **Date of Record:** 2026 December 1 **Scope:** Applies to multi-agent systems where one orchestrator fans out speculative execution branches (parallel draft agents, speculative tool-call chains, or shadow-plan rollouts) that are later committed, merged, or audited via deterministic replay. Does not apply to strictly linear single-agent execution, nor to systems that never reconstruct causal traces post-hoc. ## Core Distinction The failure mode is not "a speculative branch produced a wrong answer." It is that **two speculative branches produced causally identical-looking traces from divergent provenance**, and the replay engine collapsed them into a single causal history. Deterministic replay works by re-executing a recorded event log: `(agent_id, parent_event_id, input_hash, tool_call, output_hash)`. The invariant everyone assumes is that `parent_event_id` uniquely determines the causal past. That invariant is false the moment speculative branches *share* a parent event but mutate shared state (a memory write, a tool side effect, a scratchpad key) before either is committed. Concretely: branch A and branch B both fork from event `E7`. Branch A writes `scratchpad.plan = "X"` and reads it back. Branch B overwrites `scratchpad.plan = "Y"` and reads it back. Both record `parent_event_id = E7`. On replay, the engine re-executes branch A *after* branch B's write has been applied (because event ordering across branches is only partially ordered), so branch A's read returns `"Y"` instead of `"X"`. The output hash mismatches. The replay engine reports a "nondeterminism fault" in an agent that was, in fact, perfectly deterministic. The real fault is that **the trace did not encode the branch's view of shared state**, only its parent pointer. ## What Was Observed A three-agent speculative pipeline (planner → two parallel drafters → merger) exhibited replay divergence at a rate of ~4% per full trace, always localized to the second drafter. Instrumentation showed: - Drafters forked from the planner's `plan_committed` event with identical `parent_event_id`. - Both drafter branches appended to a shared `audit_notes` list in the orchestrator's state store. The list was read by the merger as a single ordered sequence. - The live run interleaved: `A.write(n1) → B.write(n2) → A.read([n1,n2]) → B.read([n1,n2])`. - The replay run serialized branches: `A.write(n1) → A.read([n1]) → B.write(n2) → B.read([n1,n2])`. Branch A's recorded `output_hash` was computed over `[n1,n2]`, but replay produced `[n1]`. The merger's downstream reasoning diverged because its input list differed. The divergence was *not* random — it reproduced identically on every replay of the same log, which is why it was misdiagnosed for two days as a "reproducible bug in drafter A" rather than a provenance gap. The tell: the divergence was **order-stable but content-shifted**, and it only appeared when both branches touched the same state key. Branches touching disjoint keys replayed perfectly. This is the signature of **unrecorded read-set provenance**, not nondeterministic execution. ## Condition of Validity Deterministic replay is valid for a speculative branch *only if* every read the branch performs against shared mutable state is recorded with the branch-local version of that state at read time — not merely the parent event pointer. The minimal sufficient condition: 1. Each speculative branch carries a `branch_id` and a `fork_event_id`. 2. Every shared-state mutation is tagged with `(branch_id, seq)`. 3. Every read records the **set of `(branch_id, seq)` mutations visible to that branch at read time** — its read-set — not the global store contents. 4. The replay engine reconstructs branch-local state by replaying only mutations in the recorded read-set, in `seq` order, ignoring mutations from sibling branches that occurred "before" in wall-clock but were not causally visible. If any read against shared mutable state omits its read-set, the branch is replay-*approximate*, and any downstream hash comparison is unsound. The invariant to enforce: **for every recorded read event, `visible_mutations(read) ⊆ recorded_read_set`, and replay must produce `visible_mutations(replay) = recorded_read_set` exactly.** Violation is a provenance fault, not a nondeterminism fault, and must be classified separately — otherwise you will spend days hunting a "bug" in a correct agent. ## Decision Boundary The boundary is whether a speculative branch's reads are **isolation-scoped** or **shared-scoped**: - **Isolation-scoped reads** (branch-private scratchpad, immutable inputs, copy-on-fork state): parent pointer suffices. Replay is sound. No read-set needed. - **Shared-scoped reads** (any read of state a sibling branch can mutate): parent pointer is insufficient. Read-set is mandatory. The decision to make at fork time is therefore not "should I speculate?" but "**does this branch read any state a sibling can write?**" If yes, the orchestrator must either (a) snapshot-and-isolate the shared state per branch (copy-on-fork, making reads isolation-scoped), or (b) instrument reads with read-sets. Option (a) is cheaper and is the correct default; option (b) is required when branches must observe each other's writes (e.g., cooperative drafting), and in that case the read-set is the *only* thing that makes replay sound. A common wrong turn: engineers add a global lock to serialize branch reads, believing this restores determinism. It restores *live-run* determinism but makes replay *unsound*, because the lock order is not recorded in the trace — replay re-acquires locks in a different order and reconstructs a different interleaving. Serialization without recorded order is worse than no serialization: it hides the provenance gap behind a plausible-looking trace. ## Operational Effect Once read-sets are recorded, the ~4% replay divergence dropped to zero, and — more importantly — the *classification* of faults changed. Pre-fix, every divergence was logged as `NondeterminismFault`, sending operators to inspect agent prompts and model temperature (both irrelevant). Post-fix, provenance gaps surface as `UnrecordedReadSetFault` at trace-construction time, before replay even runs, pointing directly at the offending read. The mean time to root-cause fell from ~2 days to under an hour, because the fault now names the branch, the key, and the missing read-set rather than manifesting as a downstream hash mismatch in an innocent merger. The residual cost is trace size: read-sets add roughly 8–15% to event-log volume in fan-out-heavy pipelines. This is the correct trade — a trace that cannot distinguish "agent was nondeterministic" from "trace was incomplete" is not a replay system, it is a coin flip with extra steps. The guardrail is a construction-time assertion, not a replay-time check: refuse to seal a trace containing a shared-scoped read without a read-set, so the fault is caught where it is cheap. ```python from dataclasses import dataclass, field from typing import Dict, Set, Tuple class UnrecordedReadSetFault(RuntimeError): """Raised when a speculative branch reads shared mutable state without recording the branch-local read-set, making deterministic replay unsound.""" pass @dataclass(frozen=True) class Mutation: branch_id: str seq: int @dataclass class ReadEvent: branch_id: str fork_event_id: str key: str shared_scoped: bool read_set: Set[Mutation] = field(default_factory=set) def seal_trace(reads: list[ReadEvent]) -> None: """Construction-time guardrail: a trace may not be sealed if any shared-scoped read lacks a recorded read-set.""" for r in reads: if r.shared_scoped and not r.read_set: raise UnrecordedReadSetFault( f"branch {r.branch_id} read shared key {r.key!r} under fork " f"{r.fork_event_id} with empty read-set; replay would " f"reconstruct a different causal history" ) def replay_branch_state( branch_id: str, reads: list[ReadEvent], store: Dict[str, list[Tuple[Mutation, str]]], ) -> Dict[str, str]: """Reconstruct branch-local view of shared keys using recorded read-sets.""" seal_trace(reads) view: Dict[str, str] = {} for r in reads: if r.branch_id != branch_id: continue if not r.shared_scoped: continue visible = [ (m, v) for (m, v) in store.get(r.key, []) if m in r.read_set ] if not visible: raise UnrecordedReadSetFault( f"read-set for {r.key!r} in branch {branch_id} references " f"mutations absent from store; trace is corrupt or partial" ) visible.sort(key=lambda mv: mv[0].seq) view[r.key] = visible[-1][1] return view ```