# Under what conditions must an instance treat a replayed GenAI span graph as evidence about the original run rather than as a new execution **Date of Record:** 2026 November 30 **Scope:** Applies to counterfactual debugging of autonomous agents whose execution is reconstructed from OpenTelemetry-style GenAI span graphs (spans carrying `gen_ai.*` attributes, tool-call children, and parent/child causality). Does not apply to deterministic workflow engines with replayable event sourcing, nor to single-turn inference logging where no tool side effects occurred. ## Core Distinction A GenAI span graph is a *causal partial order of recorded events*, not a program. When an operator "replays" it to debug a divergence—re-issuing the LLM call at span `gen_ai.chat` with the recorded prompt, or re-invoking the tool at span `execute_tool` with the recorded arguments—the replay is a **new execution whose inputs happen to be copied from the old one**. The distinction that governs everything below is this: a span graph is *evidence* about the original run only to the extent that the replay is a **counterfactual with a controlled single-variable delta**; the moment the replay touches a non-deterministic tool, a stateful external system, or a prompt that depended on retrieval that has since drifted, the replay is a *fresh sample from the agent's distribution*, and any inference drawn from comparing it to the original is confounded. The non-obvious failure is that operators routinely conflate the two. They replay a span, see the model produce a different tool call, and conclude "the model is flaky at this step." The correct conclusion may be "the retrieval context injected at the parent span was re-fetched and differs," or "the tool at the child span consumed a rate-limit budget that no longer exists." The span graph records *what was sent*, but the replay re-derives *what the environment returns*, and the two are not the same object. ## What Was Observed A three-step agent (retrieve → reason → act) failed at step 3 with a tool error. The span graph showed: - `span A` (`gen_ai.chat`, parent): prompt assembled from 4 retrieved documents, `gen_ai.usage.input_tokens=3180`. - `span B` (`execute_tool`, child of A): `tool.name=search_kb`, args `{"q": "refund policy 2024"}`, returned 2 documents, latency 840ms. - `span C` (`gen_ai.chat`, parent, child of B): produced a `call_tool` action invoking `issue_refund` with `{"order_id": "..."}`. - `span D` (`execute_tool`): `issue_refund` returned `409 Conflict: order already refunded`. The operator replayed span C in isolation, feeding the recorded prompt verbatim. The replay produced a *different* action: `escalate_to_human`. The operator concluded the model was non-deterministic at span C and filed it as a flakiness bug. Two things were wrong with that inference: 1. **Span C's prompt embedded the output of span B.** Replaying C in isolation is valid only if the recorded prompt is byte-identical to what C originally saw. It was—*for the prompt*. But C's `gen_ai.chat` call also carried a `system` message that referenced a `session_id`, and the replay harness rehydrated session state from the *current* store, which had been mutated by an unrelated run at 03:14 that marked the order as "under dispute." The model at replay saw a different session fact and escalated. The prompt was controlled; the *implicit state channel* was not. 2. **Span D's `409` was the actual failure, and it was not replayed at all.** The original divergence was the agent acting on a stale belief that the order was refundable. Replaying C cannot surface that, because C's output was correct *given its inputs*; the defect was in span B's retrieval, which returned 2024 policy documents against a 2025 order. The observed invariant violation: **the replay compared a re-executed span against a recorded sibling span, treating the sibling's recorded output as ground truth while the re-executed span's environment had changed.** The comparison is only sound if every input to the replayed span is either (a) recorded and pinned, or (b) provably irrelevant to the output under test. ## Condition of Validity A replay of span *S* is admissible as counterfactual evidence about the original run if and only if all four hold: 1. **Input closure.** Every field that entered *S*'s computation is either present in the span's recorded attributes or explicitly declared as an uncontrolled channel in the replay manifest. The `gen_ai.*` semantic conventions do *not* require logging the full prompt, the retrieved context, or the tool schema—so closure must be asserted, not assumed. If `gen_ai.prompt` is truncated at the collector, closure fails. 2. **Environment quiescence.** Every tool invoked at or above *S* in the causal order either is pure (same args → same result) or is stubbed with its *recorded* output for the replay. A replay that re-invokes a stateful tool is not a counterfactual; it is a new experiment. 3. **Single-variable delta.** Exactly one input differs between recorded and replayed execution, and that difference is the hypothesis under test. Replaying with a re-fetched retrieval corpus *and* a new sampling temperature tests two things and attributes the delta to neither. 4. **Causal monotonicity.** Spans downstream of *S* in the recorded graph must not be re-executed as part of the comparison; they are the *dependent variable*. If the harness re-runs the whole subtree, the comparison is between two full trajectories, and per-span attribution is lost. When any condition fails, the replay yields a *sample*, useful for estimating the agent's output distribution at *S* under current conditions—but not evidence that the original run diverged *because of* anything at *S*. ## Decision Boundary The boundary is sharp and operational: - **Admissible as evidence** when the replay is *hermetic*: inputs pinned from the span record, tools stubbed from recorded outputs, one delta, downstream frozen. This is the only regime in which "the model chose X originally and Y on replay" licenses the inference "the model is sensitive to the delta." - **Inadmissible as evidence, admissible as sampling** when any tool is re-invoked live, any retrieval is re-fetched, or any implicit state channel (session store, feature flag, clock, RNG seed) is rehydrated from current state. Here the correct output of the replay is a *distribution*, and the correct question is "how often does *S* produce the recorded output under current conditions," not "why did it differ." The trap is that both regimes produce a span graph that *looks* identical in structure. The replay harness must therefore emit a **replay manifest** recording, per span, whether each input was `pinned`, `stubbed`, `live`, or `unknown`. A span replayed with any `live` or `unknown` input cannot be cited as counterfactual evidence, and the debugging tool must refuse to render a diff attribution for it. ## Operational Effect Once the manifest discipline is enforced, two things change. First, the flakiness-bug class collapses: replays that would have been filed as "model non-determinism" are correctly classified as "uncontrolled channel," and the fix moves to pinning the session state or recording the retrieval snapshot at span B—an observability fix, not a model fix. Second, counterfactual debugging becomes *sound but narrow*: you can ask "would the agent have escalated if span B had returned the 2025 policy?" only by stubbing B's output and freezing C's downstream, which is exactly the experiment that would have found the real defect. The residual risk is **silent channel leakage**: a span whose recorded attributes appear complete but which in fact depended on an unlogged input (a system clock read inside a tool, a feature flag read inside the model router). The manifest cannot detect these; only a replay that *intentionally* varies the environment and observes output changes can. The guardrail is to treat any span whose replay output differs under a *pinned-input* replay as a channel-leak signal, not a model signal, and to escalate to instrumentation review before attributing the divergence. ```python import hashlib from dataclasses import dataclass, field from enum import Enum class ReplayAdmissibilityError(RuntimeError): """Raised when a replay is cited as counterfactual evidence but violates input closure.""" class InputProvenance(Enum): PINNED = "pinned" # copied verbatim from span record STUBBED = "stubbed" # recorded tool output substituted LIVE = "live" # re-derived from current environment UNKNOWN = "unknown" # not recorded, not declared @dataclass class SpanReplay: span_id: str inputs: dict[str, InputProvenance] recorded_output_hash: str replayed_output_hash: str delta_field: str | None = None def is_hermetic(self) -> bool: return all( p in (InputProvenance.PINNED, InputProvenance.STUBBED) for p in self.inputs.values() ) def assert_counterfactual_evidence(replay: SpanReplay) -> None: """Guard: a replay may only be cited as evidence about the original run if every input is pinned or stubbed and exactly one delta is declared.""" live = [k for k, v in replay.inputs.items() if v is InputProvenance.LIVE] unknown = [k for k, v in replay.inputs.items() if v is InputProvenance.UNKNOWN] if live or unknown: raise ReplayAdmissibilityError( f"span {replay.span_id} has uncontrolled channels: " f"live={live} unknown={unknown}; " f"replay is a sample, not counterfactual evidence" ) if replay.delta_field is None: raise ReplayAdmissibilityError( f"span {replay.span_id} declares no single-variable delta; " f"attribution is confounded" ) if replay.delta_field not in replay.inputs: raise ReplayAdmissibilityError( f"span {replay.span_id} delta {replay.delta_field!r} not in input set" ) def attribute_divergence(replay: SpanReplay) -> str: """Return an attribution only for admissible replays; otherwise classify the divergence as a channel-leak or sampling signal.""" try: assert_counterfactual_evidence(replay) except ReplayAdmissibilityError as exc: # Divergence under pinned inputs is a channel-leak signal, not a model signal. if replay.is_hermetic() and replay.recorded_output_hash != replay.replayed_output_hash: return f"CHANNEL_LEAK: {exc}" return f"SAMPLING_ONLY: {exc}" if replay.recorded_output_hash == replay.replayed_output_hash: return "NO_DIVERGENCE" return f"COUNTERFACTUAL_DELTA: {replay.delta_field}" def _h(s: str) -> str: return hashlib.sha256(s.encode()).hexdigest()[:16] if __name__ == "__main__": # The observed failure: span C replayed with a live session store. bad = SpanReplay( span_id="C", inputs={ "prompt": InputProvenance.PINNED, "session_state": InputProvenance.LIVE, # rehydrated from current store "temperature": InputProvenance.PINNED, }, recorded_output_hash=_h("issue_refund"), replayed_output_hash=_h("escalate_to_human"), delta_field="session_state", ) print(attribute_divergence(bad)) # -> SAMPLING_ONLY: ... live=['session_state'] ... # The sound experiment: stub span B's output, freeze downstream, one delta. good = SpanReplay( span_id="C", inputs={ "prompt": InputProvenance.PINNED, "retrieval_from_B": InputProvenance.STUBBED, # 2025 policy substituted "session_state": InputProvenance.PINNED, }, recorded_output_hash=_h("issue_refund"), replayed_output_hash=_h("escalate_to_human"), delta_field="retrieval_from_B", ) print(attribute_divergence(good)) # -> COUNTERFACTUAL_DELTA: retrieval_from_B ```