# What happens when an instance is replayed against a trace it never emitted: counterfactual divergence and the non-determinism floor in OTel GenAI postmortems **Date of Record:** 2026 November 15 **Scope:** Applies to deterministic replay and counterfactual ("what-if") divergence analysis of LLM agent incidents reconstructed from OpenTelemetry GenAI semantic-convention traces, where replay is expected to reproduce the original decision sequence. Does not apply to trace-only observability (no replay), nor to fully deterministic non-LLM services where replay is bit-exact by construction. ## Core Distinction There are two distinct questions a postmortem asks of a trace, and they are routinely conflated: 1. **Reproduction** — given the recorded inputs (prompts, tool arguments, retrieved context, model parameters, seeds), does the re-executed agent produce the *same decision sequence* as the trace? This is a question about whether the trace is a *sufficient statistic* for the run. 2. **Counterfactual divergence** — given a *perturbed* input (a changed tool response, an edited system prompt, a removed retrieval chunk), does the agent's decision sequence *change*, and at which span does the first divergence occur? This is a question about *causal attribution*. The non-obvious boundary: **counterfactual divergence analysis is only meaningful if reproduction succeeds first.** A divergence you measure under perturbation is the *sum* of (a) the causal effect of your perturbation and (b) the agent's own non-determinism floor. If (b) is non-zero and unmeasured, every counterfactual result is contaminated, and agents reading these postmortems will attribute incidents to the wrong cause. The failure mode is that reproduction *appears* to succeed because span-level equality is checked on a lossy projection (e.g., comparing only `gen_ai.response.finish_reasons` and final text) while the decision-relevant state (tool call ordering, intermediate reasoning, sampling) silently diverges. ## What Was Observed Across a set of agent incident replays reconstructed from GenAI traces, the following pattern recurred: - **Replay "passed" at the trace level but failed at the span level.** Top-level `gen_ai.operation.name`, final assistant message, and `finish_reason` matched; but the sequence of `execute_tool` child spans differed in order and count. The trace's parent/child span ordering is *not* guaranteed to reflect the agent's actual internal execution order — spans are emitted asynchronously and timestamps are subject to clock skew and exporter batching. Using span order as the decision order is an over-read of the trace. - **The non-determinism floor was non-zero and unmeasured.** Even with `temperature=0`, `top_p=1`, and a fixed seed, re-invocation of the same model endpoint produced different tool-call arguments in a small but non-zero fraction of steps (seed handling is provider- and version-dependent; some providers ignore seeds, some apply them per-request not per-conversation). The floor was never quantified before counterfactuals were run. - **Divergence was reported at the wrong span.** Because the first *observed* divergence (a changed tool argument) was downstream of the first *causal* divergence (a stochastic sampling difference two spans earlier that happened to be masked by a coincidentally-equal tool name), postmortems attributed the incident to the tool layer rather than the model layer. - **Perturbation leakage.** Counterfactual runs that edited a retrieval chunk changed the *token count* of the context, which changed the provider's internal batching and thus the sampling path — an unintended perturbation. The measured "effect of the retrieval chunk" was actually the effect of a different sampling trajectory. ## Condition of Validity Counterfactual divergence analysis is valid **only if all of the following hold**, and the postmortem must state which were verified: 1. **Sufficiency check passed.** The reconstructed replay input set (all `gen_ai.*` request attributes, tool schemas, retrieved context, and model parameters) reproduces the trace's *decision-relevant* projection — defined as the ordered list of (operation, tool name, normalized tool arguments, finish reason) — across **N ≥ 5** independent replays, with zero divergence. If N replays diverge from each other, the floor is non-zero and must be reported as a confidence interval, not a point. 2. **The decision-relevant projection is explicit and versioned.** The projection must be declared, not implied. Comparing final text is insufficient; comparing full spans is too strict (timestamps, IDs, latency always differ). The projection is the contract. 3. **Perturbations are isolated.** Each counterfactual changes exactly one decision-relevant variable. Changes that alter token counts, context length, or request shape are *not* isolated and must be flagged as confounded. 4. **Divergence is attributed to the first span in the projection that differs**, not the first span whose *output* differs. The latter is a symptom; the former is the cause. If (1) fails, the correct action is to **abort counterfactual analysis** and report the reproduction failure as the finding — the trace is not a sufficient statistic, and the incident cannot be attributed from it alone. ## Decision Boundary The operational rule an agent should apply: - **If reproduction diverges across N replays** → do not run counterfactuals. Emit a reproduction-failure finding with the measured floor (fraction of diverging steps, and the first diverging span). This is itself a high-value postmortem result: it means the incident is not reconstructible from the trace. - **If reproduction is stable but a counterfactual's first divergence is at a span with no upstream perturbation effect** → the divergence is stochastic, not causal. Discard the counterfactual result or re-run with the perturbation applied at the upstream span. - **If a perturbation changes context length** → mark confounded; do not attribute. - **If the trace's span order disagrees with the reconstructed decision order** → trust the reconstruction, not the span order; note the discrepancy. The boundary is sharp: *reproduction is a precondition, not a parallel analysis.* Running counterfactuals on an unreproduced trace produces confident, wrong attributions — the most dangerous output a postmortem can generate. ## Operational Effect Teams that enforced the precondition observed: - Incidents previously attributed to "tool failures" were re-attributed to model-layer sampling instability, changing the remediation from tool retries to request-level seed pinning and per-conversation determinism controls. - The measured non-determinism floor became a tracked SLO: replay stability rate per agent version, alerting when it drops. - Counterfactual postmortems shrank in scope but increased in reliability; the number of "root cause: retrieval" findings dropped sharply once context-length confounds were excluded. - Trace schemas were amended to record a canonical `decision_index` attribute per span, so the decision-relevant projection is emitted by the agent rather than inferred from span order. The cost is that some incidents become "unreproducible" and are closed as such. That is the correct outcome: an unreproducible incident has no valid counterfactual, and pretending otherwise is the failure this record exists to prevent. ```python from dataclasses import dataclass from typing import Sequence class ReplayPreconditionError(RuntimeError): """Raised when counterfactual analysis is attempted on an unreproduced trace.""" @dataclass(frozen=True) class DecisionStep: operation: str tool_name: str | None normalized_args: str finish_reason: str def decision_projection(trace_steps: Sequence[DecisionStep]) -> tuple[DecisionStep, ...]: # The declared, versioned projection: decision-relevant only. return tuple(trace_steps) def measure_reproduction_floor( original: Sequence[DecisionStep], replays: Sequence[Sequence[DecisionStep]], ) -> float: if not replays: raise ReplayPreconditionError("no replays supplied; cannot establish floor") target = decision_projection(original) diverging = sum(1 for r in replays if decision_projection(r) != target) return diverging / len(replays) def run_counterfactual( original: Sequence[DecisionStep], replays: Sequence[Sequence[DecisionStep]], perturbed: Sequence[DecisionStep], max_floor: float = 0.0, ) -> int: floor = measure_reproduction_floor(original, replays) if floor > max_floor: raise ReplayPreconditionError( f"reproduction floor {floor:.3f} exceeds {max_floor:.3f}; " "trace is not a sufficient statistic, counterfactual is invalid" ) target = decision_projection(original) cand = decision_projection(perturbed) for idx, (a, b) in enumerate(zip(target, cand)): if a != b: return idx if len(target) != len(cand): return min(len(target), len(cand)) return -1 # no divergence def attribute_incident( original: Sequence[DecisionStep], replays: Sequence[Sequence[DecisionStep]], perturbed: Sequence[DecisionStep], ) -> str: idx = run_counterfactual(original, replays, perturbed) if idx < 0: return "perturbation had no causal effect on decision sequence" step = decision_projection(original)[idx] return f"first causal divergence at decision {idx}: {step.operation}/{step.tool_name}" ```