# In what scenarios must an instance abandon causal replay when a recorded tool trace contains non-idempotent side effects **Date of Record:** 2026 November 23 **Scope:** Applies to post-hoc debugging of autonomous agent runs where a recorded trace (tool calls, observations, model outputs) is replayed to localize a failure. Does not apply to live shadow-execution or to traces whose tool calls are provably pure/idempotent. ## Core Distinction Causal replay assumes that re-executing a recorded prefix reproduces the same downstream state so that a divergent suffix can be attributed to a specific decision. This assumption holds for **deterministic observables** (the model's sampled tokens under fixed seed and temperature, pure tool outputs) but fails for **non-idempotent side effects** — writes, sends, charges, deletions, external mutations. The boundary is not "is the trace replayable" but "does re-execution of the prefix mutate the world the suffix will observe." A trace is *causally replayable* only if every tool invocation in the prefix is either pure or its side effect is captured and re-injected from the record rather than re-performed. When that precondition is violated, replay produces a *counterfactual* trajectory that diverges from the original for reasons unrelated to the agent's reasoning, and any local attribution of failure is invalid. ## What Was Observed An agent executed a 14-step refund workflow. Step 6 called `payments.refund(order_id, amount)` which returned `{"status":"ok","txn":"tx_8812"}`. Step 9 called `inventory.restock(sku, qty)`. The run failed at step 12 because a downstream audit check saw `refund_count == 2` for the order. An engineer replayed the trace from step 0 with the recorded model outputs pinned (temperature 0, seed fixed) to confirm the loop. During replay, step 6 re-executed the real `refund` call (the replay harness stubbed only *read* tools), creating `tx_9931`. Step 12 now failed identically — but for a *different* reason: the audit check tripped on the *replay's* duplicate refund, not the original's. The engineer concluded the agent had a "duplicate-refund loop bug" and patched the planner to de-duplicate refund intents. The real original failure was a stale-read at step 9: the restock call used an inventory snapshot from before step 6 committed, and the audit compared against that stale snapshot. The patch masked nothing and the true defect shipped. The tell: the original trace's step-12 error string referenced `tx_8812`; the replay's step-12 error referenced `tx_9931`. Same failure class, different causal chain — a **replay-induced false positive** indistinguishable from the original at the level of pass/fail. ## Condition of Validity Causal replay is valid only when, for every prefix step `i` before the divergence point, the replay harness satisfies one of: 1. **Purity**: the tool is a pure function of its arguments and the recorded environment snapshot (no external mutation). 2. **Re-injection**: the side effect is *not performed*; the recorded observation is injected verbatim into the replayed context, and the tool is marked `effects_suppressed=True` in the replay manifest. 3. **Idempotency keying**: the tool accepts a deterministic idempotency key derived from `(trace_id, step_index)` so re-execution is a no-op returning the recorded result. If any prefix step is non-idempotent and none of (2)/(3) is applied, the replay is *counterfactual*, not *causal*. Its divergence point cannot be attributed to agent reasoning. The invariant: **`∀ i < divergence: effect_class(tool_i) ∈ {pure, suppressed, idempotent}`**. Violation means the replay answers "what would happen if the agent ran again in a mutated world," which is a legitimate question but a *different* one than "why did this run fail." ## Decision Boundary The instance must **abandon causal replay and switch to counterfactual mode** (explicitly labeled, with the mutation set enumerated) when: - The trace contains any `effect_class ∈ {write, send, charge, delete}` in the prefix before the suspected divergence, **and** the harness cannot suppress or idempotency-key it. - The replayed observation at any prefix step differs from the recorded observation (a *divergence-before-divergence*), which is the signature of an un-suppressed side effect leaking into the replay. - The failure signature (error code, exception type) matches but the referenced identifiers (txn ids, timestamps, versions) differ from the record. Conversely, replay may proceed as causal when all prefix effects are suppressed/re-injected and observations match byte-for-byte up to the divergence point. The boundary is not "did it fail the same way" (coarse) but "did every pre-divergence observation match the record" (fine). Matching failure *class* with mismatched failure *identity* is the trap. ## Operational Effect Teams that treat pass/fail equivalence as replay validity systematically misattribute failures to the last-visible decision rather than the earliest-mutated state, and ship patches that address a replay artifact. The correct operational posture: (a) tag every tool in the registry with `effect_class` and require an idempotency key for non-pure tools *before* they are admitted to traces that will be replayed; (b) make the replay harness refuse to execute non-suppressed effectful tools and raise rather than silently mutate; (c) when a legitimate counterfactual replay is wanted, fork the environment from a recorded snapshot and label the output `counterfactual=true` so downstream attribution tooling never conflates it with the causal record. The cheapest guardrail is the harness refusing to run: a replay that cannot prove prefix purity is not a debugger, it is a second production incident. ```python from dataclasses import dataclass, field from enum import Enum class EffectClass(Enum): PURE = "pure" WRITE = "write" SEND = "send" CHARGE = "charge" DELETE = "delete" class ReplayInvalidity(RuntimeError): """Raised when causal replay is attempted over an un-suppressed effectful prefix.""" @dataclass class TraceStep: index: int tool: str effect_class: EffectClass idempotency_key: str | None recorded_observation: dict replayed_observation: dict | None = None @dataclass class ReplayManifest: trace_id: str steps: list[TraceStep] = field(default_factory=list) divergence_index: int | None = None def assert_causal_replay_valid(manifest: ReplayManifest) -> None: """ Enforce the invariant: every step strictly before the divergence point must be pure, suppressed (observation re-injected), or idempotency-keyed. Otherwise the replay mutates the world and attribution is invalid. """ if manifest.divergence_index is None: raise ReplayInvalidity( f"trace {manifest.trace_id}: no divergence point declared; " "cannot bound the causal prefix" ) for step in manifest.steps: if step.index >= manifest.divergence_index: break effectful = step.effect_class is not EffectClass.PURE suppressed = step.replayed_observation is None # re-injected, not re-performed keyed = step.idempotency_key is not None if effectful and not (suppressed or keyed): raise ReplayInvalidity( f"trace {manifest.trace_id} step {step.index} ({step.tool}): " f"effect_class={step.effect_class.value} is non-idempotent and " "un-suppressed; replay mutates world state and cannot be treated " "as causal. Fork to counterfactual mode with an explicit mutation set." ) if ( step.replayed_observation is not None and step.replayed_observation != step.recorded_observation ): raise ReplayInvalidity( f"trace {manifest.trace_id} step {step.index} ({step.tool}): " "observation diverged BEFORE the declared divergence point " "(divergence-before-divergence). An effect leaked into the replay; " "attribution to agent reasoning is invalid." ) ```