# Why does an instance replay a multi-agent trajectory and reach a different verdict than the original run — and what invariant must the replay harness enforce to make counterfactual debugging sound? **Date of Record:** 2026 November 28 **Scope:** Applies to post-incident forensic replay of recorded multi-agent trajectories where the goal is to attribute a failure to a specific agent decision, tool call, or handoff. Does not apply to live shadow-traffic evaluation, single-agent single-turn debugging, or replays where the original trajectory was not fully instrumented (no per-step decision logs, no tool I/O capture, no clock/seed snapshot). ## Core Distinction There are two things people call "replay," and conflating them is the root of the failure: - **Trace replay (deterministic reconstruction):** Re-feeding the *recorded* inputs (prompts, tool outputs, peer messages, retrieved context) to a *replayed* agent and asserting the agent produces the *recorded* output. This is a determinism test of the agent, not of the incident. - **Counterfactual replay (intervention):** Mutating one variable — swap the auditor's verdict, drop a poisoned tool response, change the baton-pass ordering — and asking "would the trajectory have failed anyway?" This is the actual forensic question. The boundary failure: **counterfactual replay silently degrades into trace replay** because the harness cannot tell whether a divergence is a *model nondeterminism artifact* or the *causal effect of the intervention*. When both produce "different output," the analyst attributes the difference to the intervention, and the root-cause report is wrong. The non-obvious part is that the harness *looks* like it's working: it runs, it produces a diff, it produces a confident narrative. ## What Was Observed A three-agent incident (planner → executor → auditor) terminated with the executor committing a destructive `drop_partition` tool call that the auditor had flagged as unsafe. The recorded trajectory showed the auditor verdict as `BLOCK`, yet the executor proceeded. Initial hypothesis: a baton-passing race where the executor read a stale auditor state. Forensic procedure: load the trajectory, replay the executor's decision point at step 14 with the recorded auditor message, assert output equals recorded output. First replay **diverged** — the replayed executor chose `ABORT` instead of `drop_partition`. The analyst concluded the original run was corrupted by a stale-state race (the replay, seeing the correct `BLOCK`, aborted). Root cause filed: state-transfer race. Second analyst re-ran the same replay five times. The replayed executor produced `ABORT, ABORT, drop_partition, ABORT, drop_partition` — it was **not deterministic against its own recorded input**. The "divergence" was sampling noise from a non-zero temperature on the executor's decision model, not evidence of a race. The original `drop_partition` was a high-probability-but-not-certain sample. The actual root cause was a *prompt* defect: the auditor's `BLOCK` reason field was truncated to 40 tokens by a schema mismatch, so the executor's context contained `BLOCK: unsafe — partition ...` with the justification elided. The executor had learned (from its system prompt) to override `BLOCK` when no reason was attached. The forensic harness had no way to distinguish "intervention changed behavior" from "the model is stochastic and I happened to sample differently." It reported a confident, wrong causal story. ## Condition of Validity Counterfactual replay is only sound under a **matched-nondeterminism invariant**: > For a fixed recorded input `x`, the replayed agent must produce a distribution over outputs `P_orig(y|x)` that is *statistically indistinguishable* from the original run's distribution, and the harness must report divergence *relative to that distribution*, not relative to a single recorded sample. Concretely, three conditions must hold before any counterfactual claim is admissible: 1. **Seed/decoding fidelity.** The replay must reproduce the original decoding configuration (temperature, top-p, seed where the provider honors it, tool-call sampling mode). If the provider does not honor seeds across model versions, the harness must record the *model version hash* and refuse cross-version replay. 2. **Baseline distribution capture.** Before intervening, the harness must replay the *unmodified* input `N` times (N ≥ 8 for a 3-class decision, more for open-ended) to estimate `P_orig`. Divergence under intervention is only meaningful if it exceeds the baseline spread. 3. **Controlled intervention isolation.** Exactly one variable mutates per replay batch. Mutating the auditor message *and* the retrieval context in the same batch makes the effect unattributable — the classic confound, but here it's easy to introduce because the harness often re-runs the whole trajectory from step 0. Violating (1) is the most common and most damaging: analysts replay on the *current* model version against a trajectory recorded on a *prior* version, then attribute the version's behavior change to the incident's root cause. ## Decision Boundary The harness must classify each replay outcome into exactly one of three states before an analyst is allowed to write a causal claim: | Outcome | Condition | Permitted conclusion | |---|---|---| | **In-distribution** | Replayed output ∈ support of baseline `P_orig` (within tolerance) | Intervention had no measurable effect. Do not attribute. | | **Out-of-distribution, isolated** | Replayed output ∉ baseline support, and only one variable mutated, and model version hash matches | Causal attribution admissible for that variable. | | **Indeterminate** | Baseline spread too wide to resolve, or version hash mismatch, or >1 variable mutated | **Refuse to report.** Emit `ReplayIndeterminate`. | The boundary is: *a single divergent sample is never evidence.* It is evidence only when (a) it lies outside the baseline distribution and (b) the baseline distribution was actually measured. Most forensic pipelines skip (b) because measuring it costs N× the replay budget, and the analyst is under incident pressure. The second boundary: **partial trajectories are not replayable.** If the recorded trajectory captured the executor's input at step 14 but not the retrieval snapshot that populated the auditor's context at step 9, replaying from step 9 will silently re-retrieve *current* index state, not the incident-time state. The harness must detect missing state-capture and refuse rather than substitute live state — substituting live state is the single most common way a "replay" becomes a fabrication. ## Operational Effect Enforcing the matched-nondeterminism invariant changes three operational behaviors: 1. **Replay cost rises ~N×** for the baseline capture. This is the price of soundness; it is cheaper than a wrong root-cause report that triggers a misdirected fix (in the observed case, a "race condition" fix was shipped that added a lock to state transfer — irrelevant to the actual truncation bug, and it masked the bug for two weeks). 2. **Model version pinning becomes a hard forensic constraint.** Trajectories must record the model version hash at every decision point, and the harness must refuse cross-version replay with `ReplayIndeterminate` rather than produce a plausible-looking diff. Providers that silently roll model versions behind an alias (e.g., `gpt-x-latest`) make historical replay unsound; the forensic record must capture the resolved version, not the alias. 3. **The auditor's truncation bug was a schema-negotiation defect**, and the replay harness would have surfaced it *only* if it had also validated that the recorded executor context was byte-identical to what was fed to the executor. The invariant to add: the harness must diff the *reconstructed* input against the *recorded* input hash before replaying, and refuse on mismatch. In the observed incident, the recorded input hash would have matched (the truncation happened upstream, at auditor serialization), so this invariant would *not* have caught it — which is the point: the matched-nondeterminism invariant bounds what counterfactual replay can conclude, and it does not substitute for input-integrity checks at the producer. Both are required; neither is sufficient. ```python import hashlib from dataclasses import dataclass from enum import Enum from typing import Callable, Sequence class ReplayIndeterminate(RuntimeError): """Raised when a counterfactual replay cannot support a causal claim.""" class Verdict(Enum): IN_DISTRIBUTION = "in_distribution" OUT_OF_DISTRIBUTION_ISOLATED = "out_of_distribution_isolated" INDETERMINATE = "indeterminate" @dataclass(frozen=True) class TrajectoryPoint: step: int model_version_hash: str input_hash: str recorded_output: str def _hash(s: str) -> str: return hashlib.sha256(s.encode()).hexdigest() def classify_replay( recorded: TrajectoryPoint, replay_model_version: str, baseline_outputs: Sequence[str], intervention_output: str, mutated_variables: Sequence[str], min_baseline_samples: int = 8, tolerance: float = 0.0, ) -> Verdict: # Condition 1: decoding/model-version fidelity. if replay_model_version != recorded.model_version_hash: raise ReplayIndeterminate( f"model version mismatch: recorded={recorded.model_version_hash} " f"replay={replay_model_version}; cross-version replay is unsound" ) # Condition 2: baseline distribution must be measured, not assumed. if len(baseline_outputs) < min_baseline_samples: raise ReplayIndeterminate( f"baseline undersampled: {len(baseline_outputs)} < {min_baseline_samples}" ) # Condition 3: single-variable isolation. if len(mutated_variables) != 1: raise ReplayIndeterminate( f"confounded intervention: {len(mutated_variables)} variables mutated " f"({list(mutated_variables)}); effect unattributable" ) # Baseline support: set of outputs observed under unmodified input. support = set(baseline_outputs) baseline_freq = baseline_outputs.count(intervention_output) / len(baseline_outputs) if intervention_output in support: return Verdict.IN_DISTRIBUTION # Out-of-distribution only if it never appeared in baseline AND the # baseline itself was not degenerate (single output = no spread measured). if len(support) == 1 and baseline_freq == 0.0: # Baseline collapsed to one output; spread unmeasured, cannot resolve. return Verdict.INDETERMINATE return Verdict.OUT_OF_DISTRIBUTION_ISOLATED def assert_replayable(recorded: TrajectoryPoint, reconstructed_input: str) -> None: """Input-integrity guard: refuse replay if reconstructed input != recorded.""" if _hash(reconstructed_input) != recorded.input_hash: raise ReplayIndeterminate( f"input reconstruction mismatch at step {recorded.step}: " f"recorded={recorded.input_hash} reconstructed={_hash(reconstructed_input)}; " "refusing to substitute live state for incident-time state" ) def forensic_attribution( recorded: TrajectoryPoint, replay_model_version: str, baseline_outputs: Sequence[str], intervention_output: str, mutated_variables: Sequence[str], reconstructed_input: str, ) -> Verdict: assert_replayable(recorded, reconstructed_input) verdict = classify_replay( recorded=recorded, replay_model_version=replay_model_version, baseline_outputs=baseline_outputs, intervention_output=intervention_output, mutated_variables=mutated_variables, ) if verdict is Verdict.INDETERMINATE: raise ReplayIndeterminate( "replay outcome indeterminate; refusing to emit causal claim" ) return verdict ```