# When must an instance refuse to attribute a divergence to a tool call: counterfactual replay under non-deterministic sampling **Date of Record:** 2026 November 26 **Scope:** Applies to observability pipelines that reconstruct agent traces by re-executing recorded spans (causal replay) and attribute behavioral divergence to a specific tool call, prompt, or state mutation via counterfactual diffing. Does not apply to single-shot inference logging, pure input/output diffing without replay, or systems that require full determinism (temperature 0 with fixed seed and pinned model weights), where naive diffing is sound. ## Core Distinction The distinction that matters here is between **observed divergence** and **attributable divergence**. Observed divergence is a fact about two traces: the recorded production trace `T_prod` and a replayed counterfactual trace `T_cf` differ at span `s`. Attributable divergence is a claim that the difference at `s` was *caused* by the intervention `I` applied at span `s` (a swapped tool output, an edited prompt, a reordered message). Causal replay assumes the second follows from the first. It does not, because the agent's own sampling is a non-deterministic function: two replays of the *identical* trace under *no intervention* will diverge on their own at some non-zero rate. When that baseline divergence rate at span `s` is comparable to the divergence rate under intervention, the diff is measuring sampler noise, not causality. The boundary condition: **an instance must refuse to attribute a divergence to an intervention whenever the counterfactual delta at that span is not separable from the null-intervention replay delta at the same span, computed under the same sampler configuration.** Attribution requires a null-replay control, and the null control must be run at the same span depth, because error compounding makes divergence depth-dependent. ## What Was Observed A support-triage agent ran a five-step loop: `classify → retrieve_policy → draft_reply → check_tone → finalize`. Observability recorded each span with inputs, sampled outputs, model ID, temperature, and a request nonce. An engineer hypothesized that the `retrieve_policy` tool was returning a stale index shard, causing wrong escalation tiers. They ran a counterfactual replay swapping the recorded `retrieve_policy` output for a corrected one, held all downstream prompts fixed, and observed that `finalize` produced a different escalation tier in 34% of replays. Conclusion drawn: `retrieve_policy` output caused 34% of tier divergence. The error: no null replay was run. Re-running the *unmodified* trace at temperature 0.7, same prompts, same tool outputs, produced a different `finalize` tier in **31%** of replays. At step 5, the accumulated per-token sampling variance across four prior generation spans had already saturated the tier decision boundary. The intervention contributed roughly 3 percentage points of marginal divergence — plausibly noise — but the pipeline reported 34%. The attribution was arithmetically correct and causally meaningless. Worse, the divergence was *depth-dependent and asymmetric*. At step 2 (`retrieve_policy` → `draft_reply`), null replay divergence was 4%; at step 5 it was 31%. A single global "divergence threshold" applied across spans would have flagged step 2 as highly attributable (34% ≫ threshold) and step 5 as unattributable (34% ≈ threshold), which is exactly backwards from the causal truth: the step-2 signal was real, the step-5 signal was compounding noise. ## Condition of Validity Counterfactual attribution at span `s` is valid only when all of the following hold: 1. **Null-replay baseline exists at the same span depth.** For each span `s`, compute `p_null(s)` = fraction of `N` no-intervention replays diverging at `s`, using the *same* sampler config (temperature, top-p, seed policy, model version hash). Attribution requires `p_cf(s) − p_null(s)` to exceed a confidence bound derived from `N`, not a fixed constant. 2. **The sampler is pinned across prod and replay.** If `T_prod` was sampled at temperature 0.7 with model hash `abc` and replay runs at 0.0 or hash `def`, the diff conflates intervention with config drift. Record the sampler fingerprint in every span and refuse to compare spans whose fingerprints differ. 3. **Divergence is measured at the decision boundary, not at raw token equality.** Token-level divergence is near-certain under any sampling; the observable must be the downstream *decision* (tier, tool selection, schema-valid output). Raw text diffing inflates `p_null` to ~1.0 and destroys all signal. 4. **Replay is causally closed.** Every non-deterministic external call inside the replayed window (tool latency, index shard, RNG-dependent service) must be stubbed from the recorded trace. A replay that re-hits a live tool is not a counterfactual; it is a new experiment. When any condition fails, the correct action is to **degrade the claim from "caused by" to "co-occurs with"** and surface the null baseline alongside the delta. An observability system that silently drops the null baseline is producing confident causal claims from sampler noise, which is the most dangerous failure class because it is internally consistent and reproducible-looking. ## Decision Boundary The operational rule the pipeline must enforce: - If `p_null(s) < ε_low` (e.g., 0.05): the span is **deterministic-enough**; a single counterfactual replay suffices, and any divergence is attributable. - If `ε_low ≤ p_null(s) ≤ ε_high` (e.g., 0.05–0.40): the span is **stochastic**; attribution requires `N ≥ 30` null replays and `N` counterfactual replays, with a two-proportion test; report the delta *and* its confidence interval; refuse single-replay attribution. - If `p_null(s) > ε_high`: the span is **decision-unstable**; divergence at this span carries no causal information regardless of intervention. The correct output is a *stability warning* ("this decision is coin-flip under the configured sampler"), not an attribution. The boundary is not a fixed number — it is the point where `p_cf − p_null` falls inside the binomial confidence band for the chosen `N`. The instance must compute that band, not assume it. A span whose null divergence is 31% and whose counterfactual divergence is 34% at `N=50` is statistically indistinguishable; the pipeline must return `attribution: inconclusive` and name the null baseline as the dominant term. ## Operational Effect Enforcing the null-replay control changes three things in practice. First, it reorders investigation priority: the step-2 signal (4% null, 34% counterfactual) becomes the top finding, while the step-5 "34%" that dominated the original report is demoted to noise. Second, it exposes *sampler-induced instability* as a first-class observability signal — a span with 31% null divergence is telling you the agent's decision is not robust to its own sampling, which is a reliability defect independent of any tool bug. Third, it forces the trace store to persist sampler fingerprints and per-span null baselines at write time, because they cannot be reconstructed after the fact: once the model version is retired, the null baseline is unrecoverable, and every historical attribution claim made against that trace becomes unfalsifiable. The invariant to encode: **no causal attribution without a same-depth, same-sampler null control; no null control without a persisted sampler fingerprint; no fingerprint, no attribution.** Violating it does not produce visible errors — it produces a clean, reproducible, wrong causal story, which is the failure mode observability systems are least equipped to catch in themselves. ```python class UnattributableDivergenceError(RuntimeError): """Raised when a counterfactual delta cannot be separated from null-replay noise.""" pass def attribute_divergence( span_id: str, p_counterfactual: float, p_null: float, n_replays: int, sampler_fingerprint_prod: str, sampler_fingerprint_replay: str, epsilon_low: float = 0.05, epsilon_high: float = 0.40, ) -> dict: # Invariant 2: sampler must be pinned across prod and replay. if sampler_fingerprint_prod != sampler_fingerprint_replay: raise UnattributableDivergenceError( f"span {span_id}: sampler fingerprint mismatch " f"({sampler_fingerprint_prod} vs {sampler_fingerprint_replay}); " f"diff conflates intervention with config drift" ) # Invariant 1: null baseline must exist and be depth-matched (caller's job), # but we can refuse if it was never measured (sentinel -1). if p_null < 0: raise UnattributableDivergenceError( f"span {span_id}: no null-replay baseline recorded; " f"attribution is unfalsifiable" ) # Invariant 3: decision-unstable spans carry no causal signal. if p_null > epsilon_high: return { "span_id": span_id, "verdict": "decision_unstable", "attribution": "none", "null_divergence": p_null, "note": "decision is coin-flip under configured sampler; " "divergence measures sampler noise, not intervention", } delta = p_counterfactual - p_null # Deterministic-enough span: single replay is acceptable. if p_null < epsilon_low: return { "span_id": span_id, "verdict": "attributable", "delta": delta, "null_divergence": p_null, } # Stochastic span: require adequate N and a separable delta. if n_replays < 30: raise UnattributableDivergenceError( f"span {span_id}: stochastic span (p_null={p_null:.2f}) " f"with only {n_replays} replays; need >=30 for two-proportion test" ) # Binomial standard error floor for the difference of two proportions. se = (p_null * (1 - p_null) / n_replays) ** 0.5 * (2 ** 0.5) if abs(delta) <= 1.96 * se: return { "span_id": span_id, "verdict": "inconclusive", "delta": delta, "null_divergence": p_null, "se": se, "note": "delta inside null-replay confidence band", } return { "span_id": span_id, "verdict": "attributable", "delta": delta, "null_divergence": p_null, "se": se, } ```