# In what scenarios must an instance treat observability replay as non-deterministic and pin a trace-reconstruction contract before forensic use of multi-turn tool-use episodes **Date of Record:** 2026 December 3 **Scope:** Applies to forensic replay of recorded multi-turn agent episodes where tool calls are re-executed or re-simulated against captured payloads to reconstruct causal chains. Does not apply to passive log inspection where tools are never re-invoked, nor to single-turn episodes with no cross-turn state. ## Core Distinction A recorded trace is not a replayable program. The distinction that governs everything below is between **trace fidelity** (the log faithfully records what the agent emitted and what the tool returned) and **replay determinism** (re-executing that trace yields the same state transitions). These are independent properties, and observability tooling routinely conflates them. A byte-perfect trace of a non-deterministic tool call is still a non-deterministic replay. The specific failure mode: an agent's tool-use episode contains a tool whose *output* is a function of hidden state not captured in the trace—wall-clock time, a mutable external resource, a random seed, a paginated cursor, or an LLM sampling temperature. On replay, the tool returns a *different* value than the original episode saw. The agent's next turn was conditioned on the original value, so the replayed agent diverges at the first such turn. The forensic analyst, unaware, attributes the divergence to "agent reasoning error" when in fact the reconstruction contract was never closed. The invariant required: **a replay is forensically valid only if every tool invocation in the episode is classified as either (a) pure w.r.t. its captured inputs, or (b) pinned to a recorded output that is substituted rather than re-derived.** Any invocation that is neither is a replay hazard and must be flagged, not silently re-executed. ## What Was Observed An episode of 14 turns was captured: the agent queried a search tool, a database read, and a timestamped pricing endpoint, then issued a refund decision at turn 12. The trace recorded every request and response with full payloads and a monotonic turn index. On replay for a post-incident review, the agent's turn-12 decision differed: the original episode refunded; the replay declined. The trace was byte-identical. The divergence traced to a single invocation at turn 7: the pricing endpoint returned `{"price": 42.10, "as_of": "..."}` in the original run, and on replay returned `{"price": 39.80}` because the endpoint is live and prices moved. The agent's turn-8 reasoning compared the turn-7 price against a threshold and stored a derived boolean in its context; that boolean flipped, and the decision at turn 12 inverted. Three things made this non-obvious: 1. The trace *looked* complete. Every request and response was logged. Nothing was missing from the record. 2. The replay harness re-invoked tools by default—it treated captured responses as documentation, not as authoritative inputs. The recorded response at turn 7 was present in the log but was *not used* during replay. 3. The divergence was silent. No exception, no warning. The replay produced a plausible, internally consistent, but wrong causal chain. A second episode exposed the same class from the opposite direction: a tool with a random tiebreaker (a `shuffle` in a candidate-ranking step) returned a different order on replay, and the agent's subsequent selection differed. Here even substituting the recorded output would have been insufficient *if* the agent had been allowed to re-call the tool with a fresh seed—the hazard was that the harness re-executed rather than substituted. ## Condition of Validity Replay determinism holds if and only if, for every tool invocation `i` in the episode, one of the following is enforced by the harness: - **Purity pin:** `i` is proven deterministic and side-effect-free w.r.t. its captured arguments (e.g., a pure lookup against an immutable snapshot), so re-execution is sound. - **Output substitution:** `i`'s recorded response is injected as the tool result and the tool is never actually invoked during replay. - **Explicit hazard flag:** `i` is neither pure nor substituted, and the replay is marked *partial* — the reconstruction is valid only up to the first hazard, and downstream turns are labeled "unverified." The condition fails when the harness defaults to re-execution without classification. It also fails when classification is done by *tool name* rather than by *invocation*: a nominally pure "database read" becomes impure when it hits a mutable table, and a nominally impure "search" is pure when pinned to a frozen index snapshot. Purity is a property of the invocation's environment, not its label. A subtler validity condition: substitution must preserve the *ordering and timing* semantics the agent observed. If the original episode saw turn-7's response after a 2-second latency and the agent's turn-8 logic had a timeout branch, substitution that returns instantly changes the observable environment. The reconstruct must pin latency as well as payload, or explicitly record that latency-sensitive branches were not exercised. ## Decision Boundary The boundary is between **replaying to explain** and **replaying to re-decide**. These require different contracts. - *Replay to explain* (forensics, root-cause): the goal is to reproduce the *observed* trajectory. Here, output substitution is mandatory for every non-pure invocation; re-execution is a bug. The analyst wants to know "given what the agent saw, why did it act?" Substitution guarantees the agent sees exactly what it saw. - *Replay to re-decide* (counterfactual, evaluation): the goal is to ask "what if the tool had returned X?" Here, re-execution or injection of counterfactual outputs is the point, but the harness must *declare* that it is no longer reproducing the original episode and must diff against the recorded trajectory to localize where the counterfactual took effect. The failure in the observed case was a *replay to explain* run through a harness that silently behaved as *replay to re-decide*. The decision boundary was crossed without a declaration, and the output was mislabeled as a faithful reconstruction. Concretely: if the harness cannot prove purity for an invocation, and the run is forensic, it must substitute. If it substitutes, it must record the substitution in the replay manifest. If it neither substitutes nor records, the replay is inadmissible as evidence. ## Operational Effect The operational consequence is that trace storage must carry a **replay contract** alongside the payloads: a per-invocation purity classification, the environment fingerprint (index version, clock source, seed) at capture time, and a declared replay mode. Without this, the forensic value of a trace collapses to "what the agent emitted," not "what the agent experienced," and the two diverge precisely at the non-deterministic invocations that are most likely to be the subject of the investigation. Practically, teams that adopt this contract report two shifts: replay harnesses gain a mandatory classification gate that refuses to re-execute unclassified invocations, and incident timelines gain a "hazard boundary" marker showing the last turn at which reconstruction is verified. Downstream turns are retained but annotated as *inferred*, not *reconstructed*—a distinction that prevents the exact misattribution observed above, where a live pricing endpoint's drift was initially blamed on agent reasoning. The invariant to enforce: **no forensic replay may re-execute a tool invocation whose purity is unproven; substitution is the default, re-execution is the exception requiring proof.** The guardrail below encodes that gate and raises when the contract is violated. ```python class ReplayContractViolation(RuntimeError): """Raised when a forensic replay would re-execute an unclassified or impure tool invocation.""" pass PURE = "pure" # deterministic w.r.t. captured args; safe to re-execute SUBSTITUTE = "substitute" # must inject recorded output; never re-invoke HAZARD = "hazard" # neither pure nor substitutable; replay is partial def classify_invocation(invocation, environment_fingerprint): """ Purity is a property of the invocation's environment, not its tool name. A 'database read' against a mutable table is impure; a 'search' pinned to a frozen index snapshot is pure. Classification must consult the captured fingerprint, not the label. """ tool = invocation["tool"] pinned = invocation.get("pinned_snapshot") mutable = invocation.get("targets_mutable_state", False) nondeterministic = invocation.get("nondeterministic", False) if nondeterministic or mutable: return SUBSTITUTE if invocation.get("recorded_response") is not None else HAZARD if pinned is not None and pinned == environment_fingerprint.get("snapshot_id"): return PURE if invocation.get("recorded_response") is not None: # cannot prove purity -> substitution is the safe default return SUBSTITUTE return HAZARD def replay_episode(episode, environment_fingerprint, mode="explain"): """ mode='explain' -> forensic reconstruction; substitution mandatory for non-pure. mode='re-decide'-> counterfactual; re-execution allowed but must be declared. """ if mode not in ("explain", "re-decide"): raise ReplayContractViolation(f"unknown replay mode: {mode!r}") manifest = [] for turn in episode["turns"]: for inv in turn.get("invocations", []): kind = classify_invocation(inv, environment_fingerprint) if kind == HAZARD: # Forensic replay cannot proceed past an unclassifiable invocation. raise ReplayContractViolation( f"turn {turn['index']} tool {inv['tool']!r}: neither pure nor " f"substitutable; forensic reconstruction is partial beyond this point" ) if mode == "explain" and kind != PURE: # Substitute the recorded output; never re-invoke. manifest.append({"turn": turn["index"], "tool": inv["tool"], "action": "substituted", "response": inv["recorded_response"]}) elif mode == "re-decide": # Re-execution permitted, but the divergence from the recorded # trajectory must be declared in the manifest. manifest.append({"turn": turn["index"], "tool": inv["tool"], "action": "re-executed", "declared_counterfactual": True}) return {"mode": mode, "manifest": manifest, "verified_through": _last_verified(manifest)} def _last_verified(manifest): # last turn at which reconstruction is faithful; downstream turns are inferred return max((m["turn"] for m in manifest), default=-1) ```