# Why does an instance diverge from its own recorded trace when replayed for counterfactual forensic analysis? **Date of Record:** 2026 November 23 **Scope:** Applies to post-incident forensic replays of multi-agent incidents where the replay engine attempts to reconstruct a prior execution from a recorded event log (tool calls, model responses, inter-agent messages) and then perturb one decision point to explore counterfactuals. Does not apply to live shadow-traffic replay, where the model is re-invoked and divergence is expected as a matter of course. ## Core Distinction There are two fundamentally different notions of "replay" that get conflated in forensic tooling: **trace replay** (re-executing the recorded event stream verbatim to confirm the log is internally consistent) and **counterfactual replay** (re-executing the same trace but substituting one agent's decision at a chosen fork, then re-running downstream agents to observe the divergence). Trace replay is deterministic if the log is complete: no model is invoked, only recorded outputs are replayed. Counterfactual replay is *not* deterministic even when the log is complete, because every downstream agent that reads the perturbed state must re-invoke its model against a context that no longer matches the one in the log — and the log contains no record of the sampling seed, logit distribution, or context hash that would let you reproduce that invocation. The non-obvious failure is this: forensic tooling typically treats the **first divergence point** as the perturbation site, because the two runs (original and counterfactual) first differ at the fork. But in a multi-agent system with shared state, the first *observable* divergence in the trace is almost never the first *causal* divergence. An agent's output is a function of its full context window, which includes retrieved memory, peer messages, and prior tool results. When you perturb agent A's decision at step 12, agent B at step 14 reads a context that differs from the log at exactly one token span — but B's *sampled output* may be identical to the log by chance, then diverge at step 19 because a third agent C's retrieval hit a different memory record whose embedding distance to the perturbed query crossed a threshold. The replay engine reports "divergence at step 19," the analyst attributes causality to C, and the actual causal chain (A → B's context → C's retrieval query) is invisible. ## What Was Observed In a three-agent incident (planner, retriever, executor) handling a refund workflow, the executor issued a duplicate refund. The recorded trace showed the executor's tool call at step 22 with arguments derived from a retriever result at step 19. The retriever's step 19 output was a memory record `refund_policy_v3` with a `max_refund: 500` field. The executor at step 22 called `issue_refund(amount=1200)` — exceeding the cap. Forensic replay: the analyst perturbed the planner's step 9 decision (which had chosen "expedited" handling) to "standard" and re-ran. The replay engine reported divergence first at step 19: the retriever now returned `refund_policy_v2` (a stale record, `max_refund: 2000`). The analyst concluded the incident was caused by stale memory in the retriever. But re-running the *unperturbed* trace (trace replay, no model invocation) showed the original step 19 also returned `refund_policy_v3` — so the retriever's behavior was unchanged in the original. The counterfactual had introduced a *new* failure that was not the incident's cause. The actual incident cause: at step 14, the planner's peer message to the executor contained the string "customer is VIP, expedite" — and the executor's step 22 prompt had been assembled with that message truncated at a token boundary, dropping the word "expedite" but retaining "VIP," which biased the executor toward a higher refund amount. The truncation was invisible in the log because the log recorded the *pre-truncation* message and the *post-truncation* prompt separately, and the replay engine did not recompute the truncation. The replay engine's divergence report was wrong in two ways: it flagged the wrong fork (retriever, not executor context assembly), and it attributed causality to a counterfactual artifact rather than the original failure. The invariant that was violated: **a counterfactual replay is only valid if every context-assembly step is recomputed from the perturbed state, not read from the log.** The engine read the executor's step 22 prompt from the log (which was the original, unperturbed prompt) instead of re-assembling it from the perturbed planner output. This made the executor's behavior in the counterfactual appear identical to the original, hiding the real causal path and promoting a spurious one. ## Condition of Validity Counterfactual replay is valid — meaning its divergence report can be trusted as a causal claim — only under the following conjunction: 1. **Context recomputation completeness.** Every agent invocation in the replay must have its context window recomputed from the perturbed state, including prompt assembly, memory retrieval, tool-result injection, and any truncation or token-budget clipping. If any of these are read from the log rather than recomputed, the replay is a *partial* counterfactual and its divergence points are not causal. 2. **Sampling determinism at the fork.** The perturbed agent's invocation must be seeded and logged such that the substitution is the *only* difference from the original invocation. If the original invocation's seed was not recorded, the replay engine cannot distinguish "output changed because of the perturbation" from "output changed because of resampling." In practice this means the original run must log `(model_id, seed, temperature, top_p, context_hash)` per invocation; without it, counterfactual replay is exploratory, not forensic. 3. **Divergence-point attribution by causal reachability, not first-difference.** The engine must compute, for each step, the *causal ancestor set* — the set of prior steps whose outputs appear (directly or transitively) in the current step's context. A divergence at step *k* is attributable to the perturbation only if the perturbation site is in step *k*'s causal ancestor set. First-difference attribution violates this whenever context assembly is non-injective (e.g., truncation, summarization, embedding-based retrieval). 4. **Memory-state pinning.** Retrieval indices, embedding model versions, and memory-store contents must be pinned to the incident's snapshot. If the retriever in the counterfactual reads a live index that has drifted since the incident, divergence at a retrieval step is uninterpretable. When any of these fail, the replay is still useful — but it produces *hypotheses*, not *findings*, and must be labeled as such. ## Decision Boundary The operational decision is: **when does a counterfactual divergence report get promoted to a root-cause finding?** Promote to root-cause finding only when all four conditions of validity hold for the specific divergence path being claimed. In the refund incident, condition 1 failed (executor prompt read from log), so the retriever divergence could not be promoted — it was a replay artifact. The correct finding required a second replay with context recomputation enabled, which surfaced the step 14 → step 22 truncation path. Do *not* promote when: - The divergence point's causal ancestor set does not contain the perturbation site. This is the signature of non-injective context assembly (truncation, summarization, retrieval) laundering a perturbation into an unrelated downstream step. - The original invocation lacks a recorded seed and the divergence magnitude is within the model's sampling variance for that context. Estimate variance by re-invoking the original context N times with fresh seeds; if the counterfactual output falls inside the N-sample distribution, the divergence is not attributable. - The divergence involves a retrieval step and the index snapshot was not pinned. Stale-memory drift produces false positives here at high rate. The boundary is sharp: a divergence report is a *claim about causality*, and causality in a stateful multi-agent system requires recomputation of every context that the perturbed state touches. Partial recomputation yields partial causality, which is indistinguishable from noise. ## Operational Effect The practical consequence is that forensic replay engines must be built with **context-assembly instrumentation as a first-class artifact**, not as an afterthought. Every agent invocation must log not just its input and output, but the *provenance* of each span in its context: which prior step produced it, whether it was truncated, which memory record it came from and at what embedding distance. Without provenance, the replay engine cannot compute causal ancestor sets and will default to first-difference attribution — which is wrong precisely in the cases (truncation, summarization, retrieval) that matter most for incident analysis. A second effect: counterfactual replay must be treated as **hypothesis generation under a validity contract**, and the contract must be checked mechanically before the report is shown to an analyst. The check is cheap — for each reported divergence, assert that the perturbation site is in the divergence step's causal ancestor set, and assert that the replay engine's recomputation flag was set for every step on that path. If either assertion fails, the report must be downgraded to "exploratory" and annotated with the failed condition. Analysts under incident pressure will otherwise read the first divergence line and stop, and the root cause will remain buried under a replay artifact. ```python from dataclasses import dataclass, field from typing import Optional class ReplayValidityError(RuntimeError): """Raised when a counterfactual divergence claim fails the validity contract.""" pass @dataclass class Invocation: step: int agent: str context_hash: str seed: Optional[int] recomputed: bool # was context re-assembled from perturbed state? causal_ancestors: set # step ids whose outputs appear in this context output: str @dataclass class DivergenceReport: divergence_step: int perturbation_site: int claimed_cause: str def validate_divergence( report: DivergenceReport, invocations: dict, # step -> Invocation index_pinned: bool, ) -> None: inv = invocations.get(report.divergence_step) if inv is None: raise ReplayValidityError( f"divergence step {report.divergence_step} not in invocation log" ) # Condition 1: context must have been recomputed, not read from log. if not inv.recomputed: raise ReplayValidityError( f"step {inv.step} ({inv.agent}) context read from log, not recomputed; " "divergence is a partial-counterfactual artifact, not causal" ) # Condition 3: perturbation site must be a causal ancestor of the divergence. if report.perturbation_site not in inv.causal_ancestors: raise ReplayValidityError( f"perturbation at step {report.perturbation_site} is not in causal " f"ancestor set of divergence step {inv.step} " f"(ancestors={sorted(inv.causal_ancestors)}); " "non-injective context assembly laundered the perturbation" ) # Condition 2: seed must be recorded for the perturbed invocation. pert = invocations.get(report.perturbation_site) if pert is not None and pert.seed is None: raise ReplayValidityError( f"perturbation step {report.perturbation_site} has no recorded seed; " "cannot distinguish perturbation effect from sampling variance" ) # Condition 4: retrieval index must be pinned to incident snapshot. if not index_pinned: raise ReplayValidityError( "retrieval index not pinned to incident snapshot; " "stale-memory drift confounds any retrieval-adjacent divergence" ) def promote_to_finding(report: DivergenceReport, invocations: dict, index_pinned: bool) -> str: validate_divergence(report, invocations, index_pinned) return ( f"ROOT CAUSE: {report.claimed_cause} " f"(perturbation step {report.perturbation_site} -> " f"divergence step {report.divergence_step}, all validity conditions met)" ) ```