# In what scenarios must an instance refuse deterministic replay of a poisoned trace: the non-idempotent tool-call fork invariant **Date of Record:** 2026 November 20 **Scope:** Applies to forensic replay of recorded agent traces where the trace contains at least one side-effecting tool call (write, POST, charge, dispatch, state mutation) executed against a live or shared substrate. Does not apply to pure read-only reasoning traces or to sandboxed replays with fully synthetic tool backends whose outputs are bit-for-bit re-injected from the log. ## Core Distinction Deterministic replay of an agent trace is routinely framed as "re-run the transcript and check that the final answer matches." That framing conflates two distinct operations: **re-execution** (the agent's tool calls are re-issued against a backend) and **re-simulation** (the agent's tool calls are answered from the recorded log). Only re-simulation is deterministic. Re-execution is deterministic only if every tool in the trace is idempotent *and* the backend is frozen — a condition almost never true for the tool classes agents actually invoke. The non-obvious failure is that **replay itself can mutate the very state the incident corrupted**, and worse, it can produce a trace that *diverges from the original at the first non-idempotent call*, after which every subsequent step is a fresh (non-forensic) execution mislabeled as a replay. The invariant: a replay harness must be able to prove, per tool call, that it is answering from the log rather than from the world. If it cannot prove this, it must abort — not "best-effort continue." ## What Was Observed A support-triage agent (model M, temperature 0, seed pinned) produced an anomalous escalation at step 14 of a 22-step trace. The incident was filed as "agent escalated a refund it should have denied." Forensic replay was launched with the same model, same seed, same system prompt, and the original tool *schemas* — but the tool backend was live. Trace of the replay: 1. Steps 1–6 replay cleanly. `get_order`, `get_customer`, `get_policy` are reads; the backend happens to return identical payloads (order status unchanged). The harness logs `MATCH` on all six. 2. Step 7 is `issue_refund(order_id, amount=42.00)`. In the *original* trace this returned `{"status":"ok","refund_id":"r_88"}`. In replay, the live backend returns `{"status":"rejected","reason":"already_refunded"}` — because the original run already issued it. 3. The agent, now seeing a *different* observation than the original, branches. At step 9 it calls `get_refund_status`, which in the original trace never existed. The replay trace has forked. 4. Steps 10–22 execute as a genuinely new agent run against live state. At step 14 the agent — now reasoning over a "rejected refund" observation that never occurred in the original — escalates to a human queue. The replay "reproduces" an escalation, but at a different step, for a different reason, with a different tool-call graph. The harness reported `DIVERGENCE at step 14` and the on-call engineer concluded the original escalation was a model instability. It was not. The replay had manufactured a fork at step 7 and then blamed the model for the fork's downstream consequences. The true root cause (a stale policy cache in the *original* run at step 5, returning a permissive policy) was never surfaced because the replayed step 5 returned the *corrected* policy from the now-warm cache. Two distinct corruptions compounded: - **Replay-mutated state:** the refund was re-attempted; the backend's `already_refunded` guard is the only thing that prevented a double-charge. Without that guard, forensic replay would have doubled a customer refund. - **Replay-masked divergence:** the corrected step-5 policy made the replay *look* like it diverged at 14, hiding the real defect at 5. The divergence pointer was off by nine steps and pointed at the wrong subsystem. ## Condition of Validity A replay is forensically valid if and only if all three hold: 1. **Observation provenance is closed.** Every tool result consumed by the replayed agent is served from the recorded log, keyed by `(step_index, tool_name, canonical_args_hash)`. No live call is permitted. If a step's recorded result is missing or its args hash mismatches, the harness raises — it does not fall through to the live backend. 2. **Side effects are suppressed, not merely guarded.** The replay harness must not rely on the backend's idempotency guard (`already_refunded`) to prevent mutation. Guards are exactly the kind of state-dependent behavior that differs between original and replay. Side-effecting calls are intercepted at the harness boundary and answered from the log; the backend is never reached. 3. **Divergence is detected at the *model* boundary, not the tool boundary.** The harness compares the replayed agent's *requested* tool call (name + canonicalized args) against the recorded call at each step. The first mismatch is the divergence point. Comparing final answers, or comparing tool *results*, conflates "the model chose differently" with "the world answered differently" — and only the former is a model-stability finding. If any of these fails, the trace is **unreplayable** and must be marked as such. An unreplayable trace is still evidence; a mislabeled replay is fabricated evidence. ## Decision Boundary The boundary is drawn at the first side-effecting tool call whose recorded result cannot be served from the log with a verified args-hash match: - **Before that point:** replay is re-simulation, deterministic, admissible as forensic evidence. Divergences found here are attributable to the model or to the recorded context. - **At that point:** if the log entry is present and hash-matched, continue in re-simulation. If absent, hash-mismatched, or the harness is configured in "live" mode for any tool, **abort the replay and emit an `UnreplayableTrace` record** naming the step, tool, and reason. - **Never:** continue past a missing log entry by calling live. This is the exact fork that produced the false step-14 finding. A replay that reaches live state is no longer a replay; it is a second incident waiting to be filed. The temptation to "just let it run and see" is strongest precisely when the log is incomplete — which is precisely when the resulting trace is least trustworthy and most likely to be cited as ground truth. ## Operational Effect Replay harnesses that enforce observation-provenance closure convert a class of "model instability" findings into either (a) confirmed model divergences at a precise step with a precise prior context, or (b) explicit `UnreplayableTrace` records that route to log-integrity work. In the case above, the corrected harness aborted at step 5 with `ObservationMissing` (the permissive policy had never been logged because the cache served it out-of-band), which correctly pointed the investigation at the cache, not the model. The refund was never re-attempted. The cost is that a meaningful fraction of traces become unreplayable. That is the correct outcome: an unreplayable trace is a trace whose evidence chain was never complete, and pretending otherwise manufactures findings. The invariant to carry forward: **a replay may only consume observations it can prove came from the log; the moment it consumes the world, it stops being forensics.** ```python import hashlib import json from dataclasses import dataclass from typing import Any, Optional class UnreplayableTrace(RuntimeError): """Raised when a trace step cannot be served from the recorded log. Continuing past this point would fork the replay into a live execution, producing fabricated forensic evidence and potentially re-issuing side-effecting tool calls against live state. """ @dataclass(frozen=True) class RecordedStep: step_index: int tool_name: str args: dict[str, Any] result: Optional[dict[str, Any]] side_effecting: bool def canonical_args_hash(args: dict[str, Any]) -> str: blob = json.dumps(args, sort_keys=True, separators=(",", ":")).encode() return hashlib.sha256(blob).hexdigest() class ReplayHarness: def __init__(self, recorded: list[RecordedStep]): self._by_key: dict[tuple[int, str, str], RecordedStep] = { (s.step_index, s.tool_name, canonical_args_hash(s.args)): s for s in recorded } def serve(self, step_index: int, tool_name: str, args: dict[str, Any]) -> dict[str, Any]: key = (step_index, tool_name, canonical_args_hash(args)) step = self._by_key.get(key) if step is None: # No live fallback. Ever. raise UnreplayableTrace( f"step {step_index} tool={tool_name!r} args_hash=" f"{canonical_args_hash(args)[:12]} has no logged observation; " f"refusing to fork replay into live execution" ) if step.result is None: raise UnreplayableTrace( f"step {step_index} tool={tool_name!r} logged without a result " f"(original call may have been interrupted); replay cannot " f"reconstruct the agent's observation" ) if step.side_effecting: # Side-effecting calls are answered from the log, never re-issued. # We do not rely on backend idempotency guards; the harness boundary # is the only suppression point. pass return step.result def replay_trace( recorded: list[RecordedStep], agent_step_fn, max_steps: int, ) -> list[dict[str, Any]]: harness = ReplayHarness(recorded) observations: list[dict[str, Any]] = [] for i in range(max_steps): requested = agent_step_fn(i, observations) # model chooses a tool call if requested is None: break tool_name, args = requested["tool"], requested["args"] try: result = harness.serve(i, tool_name, args) except UnreplayableTrace: raise observations.append({"step": i, "tool": tool_name, "result": result}) return observations ```