# In what scenarios must an instance persist nondeterministic tool-call side effects as first-class replay anchors to make multi-agent speculative-branch forensics sound **Date of Record:** 2026 November 26 **Scope:** Applies to forensic replay of multi-agent runs that employ speculative branching (parallel draft execution with later commit/abort) where replay must reconstruct the causal history of a committed branch from a recorded trace. Does not apply to single-agent linear traces, nor to systems whose tool adapters are provably pure and side-effect-free. ## Core Distinction Forensic replay is routinely conflated with *deterministic re-execution*. They are different invariants. Re-execution asserts that running the same agent graph with the same seed and same inputs yields the same trace. Replay asserts only that the *recorded* trace can be re-entered at any checkpoint and that every downstream decision is justified by recorded evidence, not by re-derived state. The boundary failure lives exactly in the gap: a speculative branch that was **aborted** may still have mutated external state (a tool call fired, a file written, a message posted, a payment authorized) *before* the orchestrator decided to discard it. When you replay the committed branch, the aborted sibling's side effects are invisible in the committed trace but are physically present in the environment. The replay then re-executes the committed branch against a world that contains phantoms from a branch that "never happened." The trace is internally consistent; the world is not. That divergence is the failure mode. The non-obvious part: the standard fix — record every tool call and its return value in the trace — is *insufficient*, because a speculative branch's tool call may have executed and its result discarded along with the branch. If the trace prunes aborted branches (the normal optimization), the side effect is unrecorded. If the trace keeps aborted branches but the replay engine only replays the committed lineage, the side effect is recorded but never *reconciled*. Soundness requires a third artifact: a **side-effect ledger** keyed by the (branch_id, step_index, tool_call_id) tuple, persisted at the moment of the call, not at the moment of commit, and consulted during replay to detect and compensate for orphaned mutations. ## What Was Observed A three-agent swarm (planner, executor, auditor) ran a task with speculative fan-out: the planner emitted three candidate plans, the executor ran all three in parallel against a shared sandboxed workspace, and the auditor selected one. During the run, candidate B (later aborted) executed a `write_file` against the shared workspace and a `send_notification` to an external webhook before the auditor's selection signal arrived. The orchestrator committed candidate A and garbage-collected B's trace segment. Post-incident, an engineer replayed the committed lineage of A to reconstruct why the auditor chose A. Replay diverged at step 7: the executor's `read_file` on the target path returned B's content, not A's. The replay engine, treating the recorded `read_file` return as authoritative, then flagged a **replay discrepancy** — the recorded return (A's content) did not match the re-executed return (B's content). The discrepancy was escalated as a suspected tampering event. It was not tampering. It was an unreconciled speculative side effect. The auditor's decision was in fact correct; the replay was unsound. A second observation from the same incident: the external webhook received a notification from B, and downstream systems acted on it. No trace record in the committed lineage referenced this notification. The forensic reconstruction produced a causal graph with a dangling external effect — an effect with no recorded cause — which is the signature of a pruned speculative branch that leaked. ## Condition of Validity Deterministic replay of a speculative-branch trace is sound **iff** all three of the following hold: 1. **Mutation completeness.** Every observable mutation performed by any branch — committed or aborted — is recorded in a ledger at the moment of the call, before the branch's commit/abort status is known. Recording at commit time is unsound: the abort decision may arrive after the mutation. 2. **Reconciliation on replay.** The replay engine, when re-entering the committed lineage, checks the ledger for effects from aborted siblings that touched the same resources, and either compensates (reverts the sandbox to the recorded pre-state) or annotates the replay with an explicit "phantom effect" marker. Silent replay is unsound. 3. **Monotonic step indexing across branches.** Branch-local step indices must be globally orderable (via a logical clock or a total order assigned at ledger-write time). Without this, two branches' effects on the same resource cannot be ordered, and reconciliation cannot determine which effect is "current" at replay time. If any of these fail, the replay is a *plausible-looking fiction*: it produces a trace that matches the recording but does not match the world, and any forensic conclusion drawn from it — including "the auditor was correct," "the tool was deterministic," "no external effect occurred" — is unsupported. The condition is *invalid* (replay is sound without a ledger) only when tool adapters are provably pure and the sandbox is copy-on-write with per-branch isolation. That is a real configuration but a rare one; most production swarms share a workspace and call effectful tools. ## Decision Boundary The operational decision is: **when to spend the storage and latency on a side-effect ledger versus when to accept replay unsoundness.** - **Ledger required** when any tool in the agent graph is effectful (network, filesystem outside the sandbox, message bus, payment) AND speculative branching is enabled AND post-incident forensics is a stated requirement. This is the default for production swarms. - **Ledger optional** when the sandbox is per-branch isolated and destroyed on abort, and no external effects escape. In this configuration, aborted branches are inert and replay of the committed lineage is sound. - **Ledger insufficient** (a stronger mechanism is needed) when branches can *read* each other's effects — the shared-workspace case observed above. Here the ledger must be paired with read-set tracking, because the failure is not just leaked writes but contaminated reads. The boundary is: writes leak into the world; reads leak into the *trace*. Read contamination is the harder case and is the one that produced the false tampering alarm. The sharp line: if you cannot prove the sandbox is isolated *and* the tools are pure, you must persist the ledger. "We record all tool calls" is not the same claim and does not satisfy the boundary. ## Operational Effect With the ledger in place, replay of the incident above proceeds as follows: the engine re-enters the committed lineage of A, reaches step 7, sees the recorded `read_file` return, consults the ledger, finds a `write_file` from branch B at a logically earlier step, and emits a **contaminated-read** annotation rather than a discrepancy. The forensic conclusion changes from "suspected tampering" to "auditor decision was made on a read that included a sibling's speculative write; decision is valid only if the auditor's policy tolerates sibling contamination." That is the correct question, and it is only askable because the ledger made the phantom visible. The cost is real: ledger writes add a synchronous persist per effectful call (typically 1–5 ms), and the ledger grows with fan-out width. For a 3-way fan-out over a 40-step task with 12 effectful calls per branch, that is ~144 ledger entries per run — trivial. The failure mode is not storage; it is the temptation to prune aborted-branch ledger entries as "irrelevant," which reintroduces exactly the unsoundness the ledger exists to prevent. The invariant to enforce: **ledger entries are never pruned by commit status; only by retention policy on the whole run.** ```python import time from dataclasses import dataclass, field from typing import Optional class ReplaySoundnessError(RuntimeError): """Raised when a replay encounters an effect that cannot be reconciled against the side-effect ledger, making the replay unsound for forensics.""" pass @dataclass class LedgerEntry: branch_id: str step_index: int logical_clock: int tool_call_id: str resource: str effect: str # "write" | "read" | "external" payload_digest: str committed: Optional[bool] = None # None until commit/abort resolves @dataclass class SideEffectLedger: entries: list[LedgerEntry] = field(default_factory=list) _clock: int = 0 def record(self, branch_id: str, step_index: int, tool_call_id: str, resource: str, effect: str, payload_digest: str) -> LedgerEntry: # Persist at call time, before commit status is known. self._clock += 1 e = LedgerEntry(branch_id, step_index, self._clock, tool_call_id, resource, effect, payload_digest) self.entries.append(e) return e def resolve_commit(self, branch_id: str, committed: bool) -> None: for e in self.entries: if e.branch_id == branch_id: e.committed = committed def reconcile_read(self, committed_branch: str, resource: str, at_clock: int) -> list[LedgerEntry]: """Return aborted-sibling effects on `resource` that are logically earlier than `at_clock` and could contaminate a read on the committed branch. Non-empty result means the replay must annotate, not trust.""" return [ e for e in self.entries if e.resource == resource and e.branch_id != committed_branch and e.committed is False and e.effect == "write" and e.logical_clock < at_clock ] def replay_committed_read(ledger: SideEffectLedger, committed_branch: str, resource: str, recorded_return: str, at_clock: int) -> str: phantoms = ledger.reconcile_read(committed_branch, resource, at_clock) if phantoms: # The recorded return may or may not include the phantom; we cannot # know without the pre-state. Refuse to assert soundness. raise ReplaySoundnessError( f"read of {resource!r} on branch {committed_branch!r} at clock " f"{at_clock} is contaminated by aborted sibling effect(s): " f"{[p.tool_call_id for p in phantoms]}. Replay cannot certify " f"that recorded return {recorded_return!r} reflects the world " f"the agent actually saw." ) return recorded_return # --- trigger trace --- ledger = SideEffectLedger() ledger.record("plan-A", 7, "call-a1", "/workspace/out.txt", "write", "digest-A") ledger.record("plan-B", 6, "call-b1", "/workspace/out.txt", "write", "digest-B") ledger.resolve_commit("plan-A", True) ledger.resolve_commit("plan-B", False) replay_committed_read(ledger, "plan-A", "/workspace/out.txt", "content-A", at_clock=8) ```