# Under what conditions must an instance treat nondeterministic tool-output replay as a first-class forensic hazard in agent trajectory reconstruction **Date of Record:** 2026 November 26 **Scope:** Applies when reconstructing an agent trajectory from a recorded event log for post-incident forensics, where the trajectory contains at least one tool call whose output is nondeterministic (network reads, clock reads, RNG, live retrieval, model sampling). Does not apply to pure-function tool environments or to trajectories where every tool output was captured verbatim and pinned at record time. ## Core Distinction There are two things an agent forensics system can mean by "replay," and conflating them is the root failure: 1. **Deterministic state replay** — re-executing the recorded decision sequence against *pinned* tool outputs, producing a bit-identical trace of what the agent actually did. The environment is frozen; only the agent's reasoning is re-run. 2. **Re-execution replay** — re-invoking the tools live and re-running the agent, producing a *counterfactual* trace of what the agent *would* do now. The environment is live; both reasoning and inputs vary. The boundary failure occurs when an operator believes they are doing (1) but the harness silently performs (2) for a subset of tool calls — typically because the recorded log captured tool *arguments* but not tool *results*, or captured results without a content hash. The reconstructed trajectory then diverges from the incident trajectory at the first unpinned call, and every downstream reasoning step is poisoned by inputs the original agent never saw. The divergence is not flagged, because from the harness's perspective the re-execution "succeeded." ## What Was Observed Incident class: an agent tasked with reconciling a ledger issued a `fetch_balance(account_id)` call, received `412.00`, and then emitted a `transfer` decision. During post-incident replay three days later, the same call returned `398.00` (the account had since been debited by an unrelated process). The replay harness fed `398.00` into the recorded reasoning context. The agent's reconstructed chain then diverged: it emitted a different transfer amount, and the auditor — reading the reconstructed trace — concluded the original agent had been *arithmetically inconsistent*, when in fact the original agent had been correct against the balance it actually saw. The trace looked like this: - **Recorded trajectory:** `fetch_balance → 412.00 → reason("412 > 400, transfer 12") → transfer(12)` - **Replayed trajectory:** `fetch_balance → 398.00 → reason("398 < 400, hold") → no-op` - **Forensic verdict (wrong):** "agent's decision logic is unstable; the transfer was unjustified." The harness had no mechanism to distinguish "tool returned 398 because that is what the tool returned" from "tool returned 398 because the world changed." Both are legitimate tool outputs. Only one is a faithful replay. The second-order symptom was worse: because the replay's reasoning diverged, the token sequence diverged, and the harness's *diff* between recorded and replayed trajectories was attributed to model nondeterminism (temperature, sampling seed). Operators spent two days investigating sampling reproducibility when the actual cause was a single unpinned read. ## Condition of Validity A replay is a valid deterministic reconstruction **if and only if** every tool invocation in the trajectory is resolved from a pinned record, and the pin is verified by content hash before the result enters the reasoning context. Formally, for each tool call `c_i` in the trajectory, the replay harness must satisfy: - `recorded[c_i].result_hash == replay[c_i].result_hash`, **or** - `replay[c_i]` is served from the recorded result payload without re-invocation. If neither holds, the replay is a *counterfactual*, not a reconstruction, and must be labeled as such before any forensic conclusion is drawn. The invariant is: **no reasoning step may consume a tool result whose provenance is not the recorded trajectory.** Violating this invariant does not produce an error — it produces a plausible-looking wrong answer, which is the failure mode that matters. The subtlety is that the invariant must be enforced at the *context-construction* boundary, not at the *tool-dispatch* boundary. A harness that re-executes tools but then "checks" the result against the log is already too late: the check can only detect divergence, not prevent the poisoned result from having been fed to the model in a prior retry loop. Enforcement must be structural: the replay context builder must refuse to emit any tool-result block that lacks a verified pin, and must fail the replay rather than proceed with a live result. ## Decision Boundary The operator must decide, per trajectory, which replay mode is intended, and the harness must refuse to blur them: - **Forensic reconstruction (default for incident post-mortems):** all tool calls pinned, hash-verified, no live dispatch. If a pin is missing, the replay **fails closed** with an explicit `UnpinnedToolResultError` naming the offending call index and tool name. The operator then either (a) locates the missing pin in the raw log, or (b) explicitly downgrades the replay to counterfactual mode and annotates the report. - **Counterfactual exploration (for "what would the agent do now"):** live dispatch is permitted, but the harness must stamp the trajectory as `mode=counterfactual` and suppress any language in the forensic report implying the trace reflects the incident. Divergence diffs are reported as "behavior drift," not "agent error." The boundary is crossed silently whenever the harness treats a missing pin as a cache miss and falls through to live dispatch. That fall-through is the single most dangerous line of code in an agent forensics system, because it converts a hard failure (missing data) into a soft failure (plausible wrong trace). ## Operational Effect With the invariant enforced, three things become true that were not before: 1. **Divergence diffs become meaningful.** Any difference between recorded and replayed reasoning is now attributable to genuine model nondeterminism (sampling, temperature, seed), because inputs are held fixed. This lets operators separate "the model is stochastic" from "the environment changed," which are the two hypotheses that were previously indistinguishable. 2. **Missing pins surface at replay time, not at verdict time.** A trajectory with an unpinned read fails immediately and loudly, rather than producing a confident but wrong post-mortem three days later. 3. **Counterfactual replays are honestly labeled.** When an operator genuinely wants to know how the agent would behave against current state, the harness permits it but stamps the output, so the report cannot be mistaken for a reconstruction of the incident. The cost is that some trajectories become unreplayable in reconstruction mode — specifically, those recorded by harnesses that logged tool arguments but not results. That is the correct outcome: an unreplayable trajectory is a known-unknown, whereas a silently counterfactual replay is an unknown-unknown masquerading as ground truth. ```python import hashlib from dataclasses import dataclass from typing import Optional class UnpinnedToolResultError(RuntimeError): """Raised when a replay attempts to consume a tool result that is not backed by a hash-verified pin from the recorded trajectory. This is a hard failure: proceeding would silently convert a forensic reconstruction into a counterfactual and poison downstream reasoning.""" @dataclass(frozen=True) class ToolCallRecord: index: int tool_name: str args: dict result_payload: Optional[bytes] # None if the recorder failed to capture it result_hash: Optional[str] # sha256 of result_payload, if captured def _hash(payload: bytes) -> str: return hashlib.sha256(payload).hexdigest() def resolve_tool_result( record: ToolCallRecord, *, mode: str, # "reconstruction" | "counterfactual" live_dispatch, # callable(tool_name, args) -> bytes, used only in counterfactual ) -> bytes: """ Resolve the result that the replay harness will feed into the reasoning context for a single tool call. Invariant: in reconstruction mode, the returned bytes MUST be the recorded bytes, verified by hash. No live dispatch is permitted, and a missing pin is a hard error rather than a cache miss. """ if mode == "reconstruction": if record.result_payload is None or record.result_hash is None: raise UnpinnedToolResultError( f"call[{record.index}] tool={record.tool_name!r} has no pinned " f"result; reconstruction cannot proceed. Either recover the pin " f"from the raw log or explicitly downgrade to counterfactual mode." ) actual = _hash(record.result_payload) if actual != record.result_hash: raise UnpinnedToolResultError( f"call[{record.index}] tool={record.tool_name!r} pin hash mismatch: " f"recorded={record.result_hash} actual={actual}. The pin is corrupt; " f"do not feed this result into the reasoning context." ) return record.result_payload if mode == "counterfactual": # Live dispatch is permitted, but the caller is contractually obligated # to stamp the resulting trajectory as counterfactual. We return the # live bytes and rely on the outer report layer to label them. return live_dispatch(record.tool_name, record.args) raise ValueError(f"unknown replay mode: {mode!r}") def build_replay_context(records, *, mode, live_dispatch): """ Construct the tool-result blocks for a replayed trajectory. Fails closed on the first unpinned call in reconstruction mode. """ context = [] for rec in records: try: result = resolve_tool_result(rec, mode=mode, live_dispatch=live_dispatch) except UnpinnedToolResultError: # Re-raise: do not swallow, do not fall through to live dispatch. raise context.append({"index": rec.index, "tool": rec.tool_name, "result": result}) return context ```