# Where must an instance reconcile its causal trace before counterfactual replay, or the replay silently rewrites history **Date of Record:** 2026 December 2 **Scope:** Applies to any agent runtime that records decision provenance (tool calls, sampled token logprobs, retrieved context snapshots) and later performs counterfactual replay by re-executing a trajectory under a modified input. Does not apply to pure observational logging without replay, nor to deterministic single-shot function invocation with no sampled reasoning. ## Core Distinction The failure is not "the trace is incomplete." It is that a *causal trace* and a *replayable trace* are different objects, and runtimes conflate them. A causal trace records what the agent *did*: the tool it called, the argument it emitted, the context it saw. A replayable trace must additionally record what was *available to be sampled* at each decision point—the full candidate distribution, the seed, the exact serialized prompt bytes, and the retrieval index version. When an instance replays a counterfactual ("what if the retrieved document had said X instead?"), it re-enters the reasoning loop at a decision boundary. If any of those four elements is missing or has drifted, the replay does not answer the counterfactual—it produces a *new trajectory* and mislabels it as the counterfactual of the original. The provenance graph then diverges from the recorded history while claiming a shared ancestor, and every downstream audit that trusts the shared ancestor is now wrong. ## What Was Observed An agent executing a 14-step incident-triage trajectory called `search_logs`, then `fetch_metric`, then emitted a root-cause hypothesis. The runtime recorded each step's tool name, arguments, and a truncated (2 KB) excerpt of the retrieved payload, plus the final sampled text. It did *not* record the sampling seed, the logprob vector, or the retrieval index commit hash. An auditor attempted counterfactual replay: "if `fetch_metric` had returned a flat series instead of a spike, would the agent still have concluded `disk_exhaustion`?" The replay harness re-ran the trajectory from step 3 with the substituted metric payload. It produced a different hypothesis—`network_partition`—and the audit report asserted this as the counterfactual outcome. The problem: step 5 of the original trajectory had retrieved a log excerpt whose *full* body (beyond the 2 KB truncation) contained the string `disk full`. The replay harness, lacking the original retrieval index version, re-queried a *current* index in which that log had rotated out. So the replayed agent saw neither the substituted metric *nor* the original log evidence. The reported counterfactual conflated two interventions—the intended metric substitution and an unintended evidence deletion—and attributed the entire divergence to the metric. The provenance graph showed the replay sharing a parent node with the original at step 3; it did not. A second instance showed the inverse: a replay that *should* have diverged did not, because the harness reused the cached sampled token sequence from the original run (to save cost), so the agent emitted the original hypothesis regardless of the substituted input. The "counterfactual" was byte-identical to the factual. Both failures are the same defect: the replay boundary was drawn at the wrong layer. ## Condition of Validity A counterfactual replay is valid only when the following invariant holds at every re-entered decision point *k*: 1. **Prompt determinism:** the serialized context at *k* is reconstructible byte-for-byte from the trace, including retrieval results pinned to an immutable index version (commit hash or snapshot ID), not a live index. 2. **Sampling determinism:** the sampling configuration (seed, temperature, top-p, model version hash) is recorded, and the replay either re-samples under the *same* config or explicitly declares a sampling intervention. 3. **Single-variable isolation:** exactly one upstream element differs from the factual trace; the harness asserts equality of all other inputs at *k* before proceeding. 4. **Provenance edge semantics:** the replay node's parent edge is typed `counterfactual_of` with the *intervention* recorded as an edge attribute, never as a plain `child_of` that implies shared history. If (1) or (2) fails, the replay is a *new* trajectory, not a counterfactual. If (3) fails, the attributed cause is unidentifiable. If (4) fails, the audit graph lies about lineage even when the replay itself is correct. ## Decision Boundary The boundary is *where you cut the trace for replay*. Cutting at the tool-call layer (record tool name + args + output) is sufficient for causal attribution ("what did the agent do and see") but insufficient for replay, because the reasoning between tool calls is sampled, not derived. Cutting at the token layer (record full prompt bytes + seed + logprobs) is sufficient for replay but expensive and brittle to model-version drift. The runtime must therefore *declare its replay class* per trace: - **Class A (observational):** tool-level trace, no replay guarantee. Counterfactual queries must be rejected. - **Class B (replayable):** token-level trace with pinned retrieval snapshot, seed, and model hash. Counterfactual queries permitted under the single-variable invariant. - **Class C (drift-tolerant):** Class B plus a recorded model-version hash and a re-sampling policy; replay under a *different* model version is permitted but tagged as `resampled`, and its divergence is not attributable to the intervention alone. The failure occurs when a Class A trace is silently promoted to Class B by a harness that assumes replayability it never recorded. The guardrail is a hard gate: the replay harness must *read the trace's declared class* and refuse to emit a counterfactual node for Class A. Promotion requires re-capture, not inference. ## Operational Effect Without the gate, three concrete harms compound. First, **false attribution**: audit reports claim a decision was caused by evidence E when the replay had also removed evidence F, so remediation targets the wrong subsystem. Second, **lineage corruption**: the provenance DAG gains edges that assert shared history between factual and counterfactual runs, so any downstream query ("show all runs that saw document D") returns counterfactuals as if they were observations, inflating evidence counts and poisoning retrieval-weight learning. Third, **replay theater**: cached-sampling harnesses produce counterfactuals identical to the factual, which auditors read as "the decision was robust to the intervention" when in fact the intervention was never applied. The invariant that bounds all three is the same: *a replay may only re-enter the loop at a point whose full input state is recorded, and must declare its class before it runs.* The runtime must fail closed—raise, not guess—when asked to replay from a trace that cannot satisfy the single-variable condition. ```python class ReplayClassViolation(RuntimeError): """Raised when a counterfactual replay is attempted on a trace that cannot satisfy the single-variable isolation invariant.""" pass class Trace: def __init__(self, trace_id, replay_class, retrieval_snapshot, seed, model_hash, steps): self.trace_id = trace_id self.replay_class = replay_class # "A" | "B" | "C" self.retrieval_snapshot = retrieval_snapshot # commit hash or None self.seed = seed # int or None self.model_hash = model_hash # str or None self.steps = steps # list of decision points def replay_counterfactual(trace: Trace, intervention_step: int, substituted_input, current_index_hash: str): # Gate 1: class must be replayable. if trace.replay_class == "A": raise ReplayClassViolation( f"trace {trace.trace_id} is Class A (observational); " f"counterfactual replay requires Class B or C" ) # Gate 2: sampling determinism must be recorded. if trace.seed is None or trace.model_hash is None: raise ReplayClassViolation( f"trace {trace.trace_id} lacks seed/model_hash; " f"replay would be a new trajectory, not a counterfactual" ) # Gate 3: retrieval must be pinned to the original snapshot. if trace.retrieval_snapshot is None: raise ReplayClassViolation( f"trace {trace.trace_id} has no pinned retrieval snapshot; " f"re-querying live index {current_index_hash} would " f"conflate evidence drift with the intervention" ) if trace.retrieval_snapshot != current_index_hash: raise ReplayClassViolation( f"retrieval index drifted: trace pinned " f"{trace.retrieval_snapshot}, harness offers {current_index_hash}" ) # Gate 4: single-variable isolation at the intervention point. step = trace.steps[intervention_step] if step.get("cached_sampling"): raise ReplayClassViolation( f"step {intervention_step} reuses cached sampling; " f"substituted input would be ignored (replay theater)" ) # Re-execute with the one substituted input, everything else pinned. return _reexecute(trace, intervention_step, substituted_input) def _reexecute(trace, step, substituted_input): # Placeholder: real harness re-runs from pinned snapshot + seed, # emitting a node with edge type "counterfactual_of" + intervention attr. return {"parent": trace.trace_id, "edge": "counterfactual_of", "intervention": {"step": step, "input": substituted_input}} ```