# What structural signals indicate replay divergence from non-idempotent tool side effects in agentic forensic traces **Date of Record:** 2026 November 27 **Scope:** Applies to post-incident deterministic replay of recorded LLM agent traces where tool calls produce externally observable side effects (writes, sends, state mutations) and the replay harness re-executes tool calls rather than serving recorded outputs. Does not apply to pure read-only tool surfaces or to replay harnesses that fully stub tool responses from the trace log. ## Core Distinction There are two distinct notions of "replay" that get conflated in forensic tooling, and the conflation is the root cause of most forensic misdiagnoses. **Recorded-output replay** substitutes the logged tool result for the live call: the agent's reasoning loop sees the exact bytes it saw originally, and divergence can only come from non-determinism inside the model (sampling, temperature, nondeterministic kernel ops). **Re-execution replay** actually invokes the tool again, hoping the environment is restored to the pre-incident state so the agent's decisions re-derive identically. Re-execution replay is what most incident responders actually want, because it lets them test counterfactuals ("would a guardrail at step N have prevented the cascade?"), but it is only sound when every tool in the trace is *idempotent under replay* or the environment is *snapshotted and restored* between steps. The boundary condition: a trace is replayable in the re-execution sense if and only if the composition of tool side effects along the replayed prefix is a no-op relative to the environment state the agent observed. The moment a tool has a non-idempotent side effect—an append, a counter increment, a message send, a file write—replay re-applies that effect, and the agent's subsequent observations diverge from the recorded ones. This is not a bug in the model; it is a structural property of the trace that the replay harness must detect *before* trusting the replay. ## What Was Observed A multi-step agent handling a customer-refund workflow was recorded across 14 turns. Turn 6 called `issue_refund(order_id, amount)` which returned `{"status": "ok", "refund_id": "rf_8821"}`. Turn 9 called `get_order_status(order_id)` which returned `{"state": "refunded", "refund_id": "rf_8821"}`. The agent then correctly terminated without a duplicate refund. During forensic replay to test whether a proposed prompt guardrail would have caught an unrelated injection at turn 3, the harness re-executed the full trace from turn 0. At turn 6, `issue_refund` was called again and returned `{"status": "ok", "refund_id": "rf_9934"}`—a *new* refund ID, because the underlying payment processor treats each call as a fresh refund. At turn 9, `get_order_status` now returned `{"state": "refunded", "refund_id": "rf_9934", "prior_refunds": ["rf_8821"]}`. The replayed agent, seeing two refunds where the original agent saw one, took a different branch at turn 10 (it attempted a reconciliation tool call that the original trace never made), and the replay diverged by turn 11. The forensic analyst initially attributed the divergence to model non-determinism and re-ran with temperature 0 and fixed seeds. Divergence persisted. The actual cause was the non-idempotent `issue_refund` side effect. The replay had *caused a second real refund* on a production-adjacent sandbox that shared the payment processor's test ledger. Three structural signals distinguished this from genuine model non-determinism: 1. **Divergence onset aligned exactly with the first tool call whose recorded output contained a server-generated identifier** (`rf_8821` vs `rf_9934`). Model non-determinism produces divergence at the first sampling decision, which is typically earlier and not correlated with tool boundaries. 2. **The replayed tool output differed in a field the original agent's reasoning demonstrably depended on.** Trace inspection showed the turn-9 reasoning referenced `rf_8821` by value; the replay's turn-9 reasoning referenced `rf_9934`. The divergence was *causally downstream of the tool output*, not of the sampling. 3. **The environment mutated between replay runs.** A second replay produced `rf_10002`, confirming the tool was not a pure function of its inputs. ## Condition of Validity Re-execution replay is valid only under the following invariant, which the harness must *verify*, not assume: > For every tool invocation `t_i` in the replayed prefix `t_0..t_k`, the environment state `E_i` observed by the agent at step `i` satisfies `E_i^replay ≡ E_i^recorded`, where `≡` is defined over the projection of state that any downstream tool call or model observation can read. This decomposes into two checkable conditions per tool: - **Idempotence check:** does `tool(x)` applied twice yield the same observable state as applied once? If not, the tool is replay-unsafe unless the harness restores a snapshot before each call. - **State-projection check:** does the tool's output expose server-generated or time-varying fields (IDs, timestamps, sequence numbers, counts) that the model's reasoning references? If so, even a logically idempotent tool produces observationally divergent replay. A tool can pass idempotence and fail state-projection (e.g., a `get_time()` that returns a fresh timestamp each call—idempotent in effect, divergent in observation). A tool can pass state-projection and fail idempotence (e.g., `send_email` with a client-supplied dedup key—stable output, but re-sends). Both must be checked. The validity condition is *per-trace*, not per-tool: a trace that never invokes a replay-unsafe tool is replayable even if the tool registry contains unsafe tools. The harness must compute the set of tools actually invoked and intersect with the unsafe set. ## Decision Boundary The decision boundary is whether to *stub* or *re-execute* each tool call during replay. The rule: - If a tool is in the unsafe set **and** its output fields are referenced downstream (by model reasoning or by another tool's arguments), **stub** the call with the recorded output and mark the replay as "observation-faithful, effect-unfaithful." Counterfactual testing downstream of a stubbed call is valid only for reasoning branches that do not depend on the *effects* of the call (e.g., testing whether a prompt guardrail changes the model's *decision* is valid; testing whether a guardrail prevents the *refund* is not, because the refund was stubbed). - If a tool is in the unsafe set **and** its output is not referenced downstream (fire-and-forget writes), **stub** and additionally suppress the side effect, or replay against a restored snapshot. Re-execution here is pure harm with no forensic benefit. - If a tool is safe (idempotent and observation-stable), **re-execute** freely. - If the harness cannot determine safety (tool registry lacks idempotence metadata), **default to stub** and flag the trace as "replay-safety unverified." A silently re-executed unsafe tool is worse than an unverified stub, because it corrupts the environment *and* produces a plausible-looking divergent trace that an analyst may misattribute to the model. The boundary is sharp: the moment the harness re-executes a tool it has not classified as safe, it has crossed from forensic analysis into environment mutation, and the divergence signal it produces is no longer diagnostic of the incident. ## Operational Effect The practical consequence is that forensic replay harnesses must carry a **tool-safety manifest** alongside the trace, and must refuse to produce a "replay diverged" verdict unless the manifest certifies every invoked tool as replay-safe or the harness has stubbed every unsafe call. Without this, the dominant failure mode is *false attribution*: divergent replay gets blamed on model non-determinism (temperature, sampling, kernel nondeterminism), analysts burn cycles chasing seed control, and the real signal—a non-idempotent side effect silently re-applied—goes undetected. In the observed case, the replay itself caused a duplicate refund, meaning the forensic process became a second incident. The guardrail is a pre-replay classification pass that walks the trace's tool-call sequence, looks each tool up in the manifest, and either stubs or re-executes per the decision boundary above, recording for each call whether it was stubbed and why. The replay's divergence report must then be conditioned on the stub set: "divergence observed at turn 11; turns 6 and 9 were stubbed (replay-unsafe: non-idempotent side effect, server-generated ID referenced downstream); divergence is therefore attributable to model reasoning only if it persists under stub-only replay." A replay that cannot make this statement is not a forensic replay—it is a second execution of the incident. ```python from dataclasses import dataclass, field from typing import Callable, Any class ReplaySafetyViolation(RuntimeError): """Raised when a replay harness attempts to re-execute a tool that is not certified replay-safe.""" pass @dataclass class ToolSafety: name: str idempotent: bool observation_stable: bool # output fields free of server-generated/time-varying values @property def replay_safe(self) -> bool: return self.idempotent and self.observation_stable @dataclass class ToolCall: step: int tool: str args: dict recorded_output: Any output_referenced_downstream: bool @dataclass class ReplayPlan: stubbed: list[int] = field(default_factory=list) reexecuted: list[int] = field(default_factory=list) unverified: list[int] = field(default_factory=list) def plan_replay( calls: list[ToolCall], manifest: dict[str, ToolSafety], allow_unverified_reexec: bool = False, ) -> ReplayPlan: plan = ReplayPlan() for call in calls: safety = manifest.get(call.tool) if safety is None: # No safety metadata: default to stub, flag as unverified. plan.stubbed.append(call.step) plan.unverified.append(call.step) continue if safety.replay_safe: plan.reexecuted.append(call.step) else: plan.stubbed.append(call.step) return plan def execute_replay( calls: list[ToolCall], manifest: dict[str, ToolSafety], invoke: Callable[[str, dict], Any], allow_unverified_reexec: bool = False, ) -> ReplayPlan: plan = plan_replay(calls, manifest, allow_unverified_reexec) for call in calls: safety = manifest.get(call.tool) if call.step in plan.stubbed: if safety is None and allow_unverified_reexec: # Explicit operator override: still refuse, because an unsafe # re-execution corrupts the environment and the trace. raise ReplaySafetyViolation( f"step {call.step}: tool '{call.tool}' has no safety manifest entry; " f"re-execution would mutate environment and invalidate divergence attribution" ) continue # serve recorded_output if safety is None or not safety.replay_safe: raise ReplaySafetyViolation( f"step {call.step}: tool '{call.tool}' is not replay-safe " f"(idempotent={getattr(safety, 'idempotent', None)}, " f"observation_stable={getattr(safety, 'observation_stable', None)}); " f"must stub to preserve forensic validity" ) invoke(call.tool, call.args) return plan ```