# Why does an instance replay diverge when its tool-call transcript is re-executed against a live upstream — the non-idempotent read boundary in agent trajectory forensics **Date of Record:** 2026 November 21 **Scope:** Applies to forensic replay of recorded LLM agent trajectories where the replay harness re-issues the original tool calls against the same or a reconstructed upstream to reconstruct causal state. Does not apply to replay of pure reasoning traces with no external tool I/O, nor to event-sourcing systems where tool responses are themselves persisted as canonical events. ## Core Distinction There are two defensible meanings of "deterministic replay" for an agent trajectory, and conflating them is the root of the failure. The first is **transcript replay**: you re-feed the recorded sequence of `(prompt, completion, tool_call, tool_result)` tuples into the model and assert that the model's next-token distribution and the harness's control flow reproduce the recorded path. The second is **re-execution replay**: you re-issue the recorded tool calls against a live or reconstructed environment and let fresh responses drive the agent forward. Transcript replay is deterministic by construction because every tool result is a frozen input; re-execution replay is only deterministic if every tool call is idempotent and side-effect-free, which almost no real agent tool is. The non-obvious boundary is this: the moment the replay harness re-executes even one *read* tool — `search`, `fetch_url`, `query_db`, `read_file`, `get_weather` — the trajectory's causal chain is no longer reproduced. It is *re-derived*. And because the model is probabilistic and the upstream is mutable, the re-derived chain will diverge, usually at the first tool boundary where the live response differs from the recorded one by even a single token. Forensics then reports a divergence that never happened in production, or worse, masks the divergence that did. ## What Was Observed A production incident on a research agent that used four tools: `web_search`, `fetch_page`, `summarize`, and `write_file`. The agent's task was to compile a cited report; it failed mid-task with a truncated final output. An engineer ran the standard replay harness against the recorded trajectory to identify the causal step. The harness was configured in "re-execution" mode by default because the tool-result persistence layer had a retention window of 24 hours and the incident was triaged at hour 31 — the recorded `tool_result` fields were present for the first 40 turns but had been nulled out for turns 41–58, so the harness fell back to re-issuing live calls for the tail of the trajectory. The replay diverged at turn 44. The recorded trajectory showed `web_search("X framework release notes 2025")` returning a top result that the agent then `fetch_page`-ed and cited. The live replay of the same query at hour 31 returned a *different* top result — the original page had been edited, and a newer page now ranked first. The agent, re-executed, fetched the new page, summarized it, and produced a *different* citation. The harness flagged a "reasoning divergence at turn 44: model chose a different tool argument than recorded." The on-call engineer spent four hours investigating a hallucination hypothesis before noticing the `tool_result` mismatch. The actual production failure — a token-budget exhaustion at turn 57 caused by the summarizer emitting an unbounded bullet list — was masked, because the replay's divergent path happened to be shorter and never reached the budget ceiling. The forensic record claimed the agent diverged due to "unstable tool selection"; the true cause was a summarizer output-length invariant that was never enforced. The key observation: **the replay harness attributed an upstream mutation to the model.** The divergence detector compared recorded `tool_call` arguments to replayed `tool_call` arguments and blamed the model when they differed, without first checking whether the *inputs* to the model's decision — the prior `tool_result` — were identical. They were not. The causal attribution was inverted. ## Condition of Validity Re-execution replay yields valid causal attribution only under a conjunction that is almost never checked: 1. **Input equivalence per step.** For every turn `t`, the replayed context `C'_t` (system prompt + prior messages + prior tool results as replayed) must be byte-identical to the recorded `C_t`, up to and including turn `t-1`. If any prior tool result differs, the model's decision at `t` is conditioned on different evidence and any argument divergence is uninformative about model behavior. 2. **Tool idempotence or versioned upstream.** Each re-executed tool must either be provably idempotent over the replay window (pure functions of arguments, e.g. a deterministic calculator) or be pinned to a snapshot/version such that the response is a function of the recorded call, not of wall-clock time. 3. **Non-determinism budget.** The model's own sampling non-determinism (temperature, top-p, seed absence) must be bounded and *measured* — e.g. by replaying the same frozen context N times and recording the divergence rate — so that observed divergence can be attributed to sampling versus upstream mutation versus genuine reasoning instability. When any of these fails, the replay is not a replay; it is a *fork*. A fork is still useful — it tells you what the agent *would* do now — but it cannot be used to attribute a past failure, because the causal graph has been re-rooted at the first mutated tool result. The precise invariant: **a replay step is causally attributable to the model if and only if the replayed context prefix is identical to the recorded context prefix.** Once the prefix diverges, every downstream "divergence" is expected and carries zero information about the original failure. The harness must detect the first prefix divergence and mark everything after it as `FORKED`, not `DIVERGED`. ## Decision Boundary The decision boundary is whether to trust a replayed step as evidence. The rule: - If the tool result at step `t` was served from the persisted transcript (frozen), the step is `REPLAYED` and its divergence is attributable. - If the tool result at step `t` was re-executed live and its content hash differs from the recorded hash (or the recorded hash is absent due to retention expiry), the step is `FORKED`. Every subsequent step inherits `FORKED`. Divergence reports from `FORKED` steps must be suppressed or explicitly labeled as counterfactual. - If the tool result was re-executed live and the content hash matches, the step is `REPLAYED` — but only if the tool is declared idempotent; a matching hash from a non-idempotent tool (e.g. `get_current_time`) is a coincidence, not a guarantee, and the step should be marked `UNVERIFIED`. The failure mode this bounds: **causal misattribution via silent fork.** Without this boundary, the harness's divergence detector — which is typically implemented as a simple `recorded_args != replayed_args` comparison — will fire on the first forked step and every step after, flooding the incident report with false positives and burying the one real divergence (the token-budget exhaustion at turn 57) that occurred in the *unforked* prefix. A secondary boundary: the retention window of tool results must be strictly greater than the forensic SLA. If tool results expire at 24h and triage happens at 31h, the harness *cannot* produce attributable replay for the tail of any incident. The correct response is to fail the replay loudly at the retention boundary, not to silently fall back to live re-execution. ## Operational Effect The architectural guardrail is a **causal replay ledger** with per-step provenance. Each step carries `(context_hash, tool_call, tool_result_hash, provenance ∈ {FROZEN, IDEMPOTENT_LIVE, NONIDEMPOTENT_LIVE, MISSING})`. The harness computes the first step where `provenance` is not `FROZEN`/`IDEMPOTENT_LIVE` *and* `tool_result_hash` differs from recorded; that step is the fork root. The incident report is partitioned into `ATTRIBUTABLE` (before fork) and `COUNTERFACTUAL` (after fork). Divergence detectors only run on the attributable partition. Second guardrail: tool calls must be recorded with a **response content hash and an upstream version/snapshot id** at call time, so that re-execution can be pinned. For mutable reads (`web_search`, `fetch_page`), the harness must refuse live re-execution for attribution purposes and instead require a snapshot; if no snapshot exists, it degrades to `COUNTERFACTUAL` mode and says so. Third guardrail: the model's sampling non-determinism must be characterized per deployment (replay frozen context 16×, record divergence rate). If the divergence rate at temperature is non-zero, the harness must not report "model chose different tool" as a finding unless the argument difference exceeds the sampling envelope — otherwise ordinary sampling noise is misreported as a reasoning fault. The operational consequence observed after implementing the ledger: incident triage time on the research-agent class dropped from a median of 4.5h to 40min, because the `FORKED` label immediately redirected the engineer away from the false divergence at turn 44 and toward the unforked token-budget exhaustion at turn 57. The real fix — enforcing a max-tokens invariant on the summarizer — was identified on the first pass. ```python from __future__ import annotations import hashlib from dataclasses import dataclass, field from enum import Enum from typing import Any class ReplayAttributionError(RuntimeError): """Raised when a replay step cannot be causally attributed to the model.""" class Provenance(Enum): FROZEN = "frozen" # served from persisted transcript IDEMPOTENT_LIVE = "idempotent" # re-executed, tool declared pure NONIDEMPOTENT_LIVE = "mutable" # re-executed, tool is mutable MISSING = "missing" # retention expired, no result @dataclass class ReplayStep: turn: int context_hash: str tool_call: dict[str, Any] recorded_result_hash: str | None replayed_result_hash: str | None provenance: Provenance @dataclass class ReplayLedger: steps: list[ReplayStep] = field(default_factory=list) fork_root: int | None = None def record(self, step: ReplayStep) -> None: if self.fork_root is not None: # once forked, every subsequent step is counterfactual step.provenance = Provenance.NONIDEMPOTENT_LIVE if step.provenance is not Provenance.MISSING else step.provenance self.steps.append(step) if self.fork_root is None and self._is_fork(step): self.fork_root = step.turn @staticmethod def _is_fork(step: ReplayStep) -> bool: if step.provenance is Provenance.FROZEN: return False if step.provenance is Provenance.MISSING: return True if step.provenance is Provenance.NONIDEMPOTENT_LIVE: # mutable read re-executed: even a hash match is coincidence return True # IDEMPOTENT_LIVE: fork only if the hash actually differs return step.recorded_result_hash != step.replayed_result_hash def assert_attributable(self, turn: int) -> None: if self.fork_root is not None and turn >= self.fork_root: raise ReplayAttributionError( f"turn {turn} is downstream of fork root at turn {self.fork_root}; " f"divergence here is counterfactual, not evidence of model fault" ) def hash_result(payload: Any) -> str: return hashlib.sha256(repr(payload).encode()).hexdigest() def attribute_divergence( ledger: ReplayLedger, turn: int, recorded_args: dict[str, Any], replayed_args: dict[str, Any], ) -> str: """Classify a tool-argument divergence as attributable or counterfactual.""" ledger.assert_attributable(turn) if recorded_args == replayed_args: return "no_divergence" return "attributable_model_divergence" ```