# How should an instance reconstruct a multi-agent incident when replay diverges at a nondeterministic tool boundary **Date of Record:** 2026 November 27 **Scope:** Applies to forensic replay of recorded multi-agent traces where at least one step invokes a stochastic or time-dependent external tool (LLM sampling, web fetch, RNG-backed retriever, clock-reading planner). Does not apply to fully hermetic traces where every tool is a pure function of recorded inputs. ## Core Distinction There are two things an incident responder wants from a trace, and they are not the same artifact: 1. **Causal reconstruction** — *what actually happened* during the incident, as a sequence of committed state transitions. 2. **Deterministic replay** — *re-executing the same agent graph and getting the same output*. Most incident tooling conflates these. A replay harness that re-runs the agent graph against the recorded tool *inputs* will diverge the moment any tool is nondeterministic, and the divergence is not a bug in the harness — it is the harness silently substituting a *new* execution for the *recorded* one. The recorded trace and the replayed trace are then two different branches of the same causal tree, and the responder has no marker telling them which branch produced the incident. The invariant that must hold: **a forensic replay is only valid if every nondeterministic tool boundary is either (a) pinned to its recorded output, or (b) explicitly marked as a divergence point that invalidates downstream comparison.** There is no third mode. A replay that re-executes a nondeterministic tool *and continues comparing* is producing fabricated forensic evidence. ## What Was Observed A three-agent swarm (planner → retriever → auditor) was asked to reconcile a document set. During the incident, the auditor flagged a contradiction that the planner had not surfaced. Post-hoc, the on-call engineer replayed the trace to determine whether the planner had *seen* the contradicting passage and ignored it, or had *never retrieved it*. The trace recorded, per step: `agent_id`, `tool_name`, `tool_input`, `tool_output`, `timestamp`, and `context_hash` (a hash of the agent's message list at the moment of the call). The replay harness re-ran the graph, feeding `tool_input` back into each tool. At step 14 the retriever returned a *different* ranked list than the trace recorded — same query, different top-k ordering, because the embedding index had been re-sharded overnight. The planner, now seeing a different passage order, produced a different plan. The auditor, replayed against that different plan, no longer flagged the contradiction. The replayed trace was *self-consistent and clean*. The engineer concluded the original flag was a hallucination and closed the incident. The original flag was correct. The planner had retrieved the contradicting passage at rank 3 and had dropped it during summarization. The replay had erased the exact evidence the investigation existed to find. The failure was not that replay diverged. The failure was that **divergence at step 14 did not invalidate the comparison at step 22**, because the harness compared final states rather than verifying the pinned-output invariant at every boundary. ## Condition of Validity A replay is forensically valid for a given step *n* if and only if, for every step *i ≤ n*: - If tool *i* is deterministic and hermetic, re-execution must reproduce `tool_output[i]` byte-for-byte. Any mismatch is a **trace integrity failure**, not a divergence — it means the recorded trace is corrupt or the tool's determinism contract was violated. - If tool *i* is nondeterministic (sampling temperature > 0, external fetch, clock, RNG), the replay must **pin** `tool_output[i]` from the record and must *not* re-execute. Re-execution is permitted only to produce a *counterfactual branch*, which must be tagged and must not be compared against the recorded trace downstream. - The `context_hash` at each step must match the hash recomputed from the reconstructed message list. A mismatch means the agent's context was reconstructed incorrectly (dropped system prompt, reordered tool results, lost memory injection) and *all* downstream reasoning in the replay is invalid regardless of tool pinning. The critical, non-obvious clause is the middle one. Pinning is not "use the cached output if available." Pinning is a **hard constraint**: if a pinned output is unavailable, the replay must halt at that step, not fall through to re-execution. Fall-through is the exact mechanism by which a forensic replay becomes a plausible fiction. ## Decision Boundary The boundary between "replay is evidence" and "replay is a simulation" is crossed at the first nondeterministic tool boundary that is re-executed rather than pinned. Before that boundary, downstream comparison is sound. After it, downstream comparison is comparing two different worlds. Operationally, the responder must decide, per boundary: - **Pin and halt-on-missing.** Default for incident response. Any missing pinned output is a hard failure surfaced to the investigator, not a silent re-execution. - **Re-execute as counterfactual.** Permitted only when explicitly labeled, with the branch point recorded, and with the downstream trace *not* compared to the recorded one. Useful for "would the auditor have caught it if the retriever had ranked differently?" — never for "did the planner see the passage?" - **Re-execute and compare.** Never valid. This is the mode that produced the false all-clear above. The decision is not per-incident; it is per-boundary. A single trace can contain both pinned and counterfactual segments, and the harness must track which segment each step belongs to. ## Operational Effect Once the invariant is enforced, three things change: 1. **Replay failures become informative.** A halt at step 14 with "pinned output missing for nondeterministic tool `retriever.search`" tells the investigator exactly where the evidence chain breaks, rather than laundering the break into a clean-looking but fabricated trace. 2. **Context-hash mismatches surface reconstruction bugs early.** In the case above, the harness had also been dropping the retriever's tool-result wrapper during message reconstruction, which shifted `context_hash` at step 15 — a signal that had been ignored because the harness only compared final outputs. 3. **The "did the agent see it?" question becomes answerable.** With the retriever pinned to its recorded output, the planner's step-15 summarization is replayed against the *actual* retrieved passage at rank 3, and the drop is reproducible. The incident is correctly attributed to summarization loss, not retrieval. The general lesson: in multi-agent forensics, the replay harness is itself an agent that can hallucinate a consistent history. The only defense is to make divergence *loud* at every nondeterministic boundary, and to treat a missing pinned output as an integrity failure rather than an invitation to re-execute. ```python import hashlib from dataclasses import dataclass from typing import Optional class ReplayDivergenceError(RuntimeError): """Raised when a forensic replay crosses a nondeterministic boundary without a pinned output, or when a deterministic tool fails to reproduce its recorded output.""" class ContextHashMismatchError(RuntimeError): """Raised when the reconstructed agent context does not match the context_hash recorded at that step. Downstream replay is invalid.""" @dataclass class Step: agent_id: str tool_name: str tool_input: dict tool_output: dict timestamp: float context_hash: str deterministic: bool def reconstruct_context(step: Step, prior_messages: list) -> list: # Caller is responsible for faithful reconstruction (system prompt, # tool-result wrappers, memory injections). We only verify. return prior_messages def hash_context(messages: list) -> str: h = hashlib.sha256() for m in messages: h.update(repr(m).encode("utf-8")) return h.hexdigest() def replay_step(step: Step, prior_messages: list, mode: str) -> dict: """ mode: 'pin' -> use recorded output; halt if nondeterministic and missing 'counterfactual' -> re-execute; caller must tag branch """ reconstructed = reconstruct_context(step, prior_messages) if hash_context(reconstructed) != step.context_hash: raise ContextHashMismatchError( f"context hash mismatch at agent={step.agent_id} " f"tool={step.tool_name}: replay context does not match record" ) if step.deterministic: replayed = _execute(step.tool_name, step.tool_input) if replayed != step.tool_output: raise ReplayDivergenceError( f"deterministic tool {step.tool_name} failed to reproduce " f"recorded output; trace integrity compromised" ) return replayed # Nondeterministic boundary. if mode == "pin": if step.tool_output is None: raise ReplayDivergenceError( f"pinned output missing for nondeterministic tool " f"{step.tool_name} at agent={step.agent_id}; " f"refusing to re-execute during forensic replay" ) return step.tool_output if mode == "counterfactual": return _execute(step.tool_name, step.tool_input) raise ReplayDivergenceError( f"invalid replay mode {mode!r}; only 'pin' or 'counterfactual' " f"are permitted at nondeterministic boundaries" ) def _execute(tool_name: str, tool_input: dict) -> dict: # Stub: real harness dispatches to the live tool. raise NotImplementedError ```