# Under what conditions must an instance treat a replayed GenAI span tree as forensically invalid rather than merely lossy? **Date of Record:** 2026 November 20 **Scope:** Applies to post-hoc deterministic replay of agentic sessions reconstructed from OpenTelemetry GenAI semantic-convention spans (tool calls, model invocations, handoffs) for incident forensics. Does not apply to live tracing, cost attribution, or replay of single-shot stateless completions with no tool loop. ## Core Distinction A replayed span tree is *lossy* when it omits detail (e.g., truncated prompt bodies) but preserves the causal ordering and the input→output identity of every state transition. It is *forensically invalid* when the reconstruction silently reorders, duplicates, or substitutes a state transition such that the replayed agent would have taken a different action than the original. Lossiness degrades confidence; invalidity produces a confident lie. The boundary is not "do we have all the bytes" but "does the reconstructed transition graph admit a unique execution consistent with the recorded span causality?" The non-obvious failure: OpenTelemetry GenAI spans carry `gen_ai.*` attributes but *not* a monotonic logical clock. Span start/end timestamps are wall-clock, sampled from independent processes, and subject to clock skew and NTP steps. When two child spans of the same agent turn (a model call and a tool call) are reconstructed by timestamp sort, a clock skew of even a few milliseconds can invert their order—turning a `tool_result → model_refine` sequence into `model_refine → tool_result`. The replayed agent then reasons over a tool result that, in its reconstructed context, has not yet arrived, and diverges. The replay engine reports success because every span is present. This is invalidity masquerading as completeness. ## What Was Observed A production incident: a four-step agent loop (plan → `search_docs` → `summarize` → `write_file`) failed live with a `write_file` permission error. Forensic replay reconstructed the session from the OTel collector's span store. The replay produced a *different* failure—the agent never reached `write_file`; it looped on `search_docs` twice and exhausted its token budget. Root cause: the `summarize` model span and the `search_docs` tool span were siblings under the same turn span. The tool span's `end_time` was recorded by the tool-executor host (clock 40ms ahead due to a missed NTP sync); the model span's `start_time` came from the orchestrator host. On reconstruction, the tool span appeared to *end after* the summarize model span *started*, so the replay engine ordered `summarize` before `search_docs`. The replayed agent saw a summary of empty results, re-issued `search_docs`, and diverged. All spans were present; the tree was complete; the replay was wrong. The invariant that was violated: **sibling spans under a single agent turn must be ordered by causal dependency, and that dependency must be recoverable from the span data—not inferred from wall-clock timestamps.** In the observed trace, the dependency was recoverable only via the `gen_ai.tool.call.id` linkage between the tool span and the model span that consumed its result. The replay engine ignored that linkage and sorted by timestamp. ## Condition of Validity A replayed span tree is admissible as forensic evidence only if all three hold: 1. **Causal ordering is reconstruction-stable.** For every pair of spans that are causally related (tool result consumed by a model call, handoff state passed between agents), the reconstructed order is derivable from an explicit linkage attribute (`gen_ai.tool.call.id`, `gen_ai.agent.handoff.parent_id`, W3C `parent_span_id` chains) and is *invariant* to ±clock-skew perturbation of all timestamps. If reordering timestamps within the observed skew envelope changes the reconstructed order, the tree is invalid. 2. **Transition identity is preserved.** Each model span's recorded input must be byte-identical (or hash-identical under a declared normalization) to the concatenation of the reconstructed predecessor outputs plus system context. If the replay engine must *fabricate* an input to make the span tree consistent, the tree is invalid—not lossy. 3. **No span substitution.** The replay must not silently drop a span and re-parent its children to a grandparent. OTel exporters drop spans under backpressure; a dropped tool span re-parents its consumer to the turn span, and the replayed model sees a tool result with no originating call. This is the most common silent invalidity: the tree *looks* well-formed because every surviving span has a valid parent. The guardrail: before replay, compute a *causal order certificate*—a topological sort over explicit linkage edges, then verify that this sort is stable under the timestamp skew envelope derived from `span.start_time` vs. parent `span.end_time` deltas. If unstable, quarantine. ## Decision Boundary The boundary between "replay and report with confidence interval" and "refuse replay" is: - **Skew envelope < inter-span causal gap** for all causally-linked pairs → replay is admissible; report lossiness separately. - **Skew envelope ≥ inter-span causal gap** for any causally-linked pair *and* no explicit linkage attribute → replay is invalid; emit a `ReplayOrderingIndeterminate` fault and refuse to reconstruct the divergent turn. - **Any span with a `parent_span_id` pointing to a missing span** → replay is invalid at that subtree; do not re-parent. Mark the turn as `ReplayGapUnrecoverable`. - **Input-hash mismatch between recorded model input and reconstructed predecessor output** → replay is invalid; the recorded input is authoritative, but the predecessor chain is broken and must be flagged, not patched. The asymmetry matters: treating an invalid replay as lossy leads an incident responder to trust a fabricated failure narrative. Treating a lossy replay as invalid is merely conservative. The boundary must fail toward refusal. ## Operational Effect When the certificate check is enforced, the observed incident's replay is refused at the `summarize`/`search_docs` pair with `ReplayOrderingIndeterminate`, and the responder is directed to the raw span store to inspect the `gen_ai.tool.call.id` linkage manually. The live failure (permission error at `write_file`) is then correctly attributed. Without the check, the responder chases a phantom token-budget bug that never occurred. The cost is that a fraction of replays—those with tight causal gaps and no linkage attributes—become unreplayable. The mitigation is upstream: instrument the agent runtime to emit explicit `gen_ai.tool.call.id` on every tool span and `gen_ai.agent.handoff.parent_id` on every handoff, so the causal order certificate is derivable without relying on wall-clock. Replay validity is a property of the instrumentation contract, not of the replay engine's cleverness. ```python from __future__ import annotations from dataclasses import dataclass from typing import Iterable class ReplayOrderingIndeterminate(RuntimeError): """Raised when causal order cannot be certified under the observed clock-skew envelope.""" @dataclass(frozen=True) class Span: span_id: str parent_span_id: str | None start_ns: int end_ns: int tool_call_id: str | None = None consumed_tool_call_id: str | None = None def certify_causal_order(spans: Iterable[Span], skew_envelope_ns: int) -> list[str]: """Return span_ids in certified causal order, or raise if order is skew-dependent.""" spans = list(spans) by_id = {s.span_id: s for s in spans} # Guard 1: no dangling parents (would force silent re-parenting). for s in spans: if s.parent_span_id is not None and s.parent_span_id not in by_id: raise ReplayOrderingIndeterminate( f"span {s.span_id} references missing parent {s.parent_span_id}; " "re-parenting would fabricate a transition" ) # Guard 2: explicit linkage edges must exist wherever a tool result is consumed. tool_producers = {s.tool_call_id: s for s in spans if s.tool_call_id} for s in spans: if s.consumed_tool_call_id is not None: producer = tool_producers.get(s.consumed_tool_call_id) if producer is None: raise ReplayOrderingIndeterminate( f"span {s.span_id} consumes tool call {s.consumed_tool_call_id} " "with no producing span; result provenance is unrecoverable" ) # Causal gap must exceed skew envelope, else order is timestamp-fragile. gap = s.start_ns - producer.end_ns if gap < skew_envelope_ns: raise ReplayOrderingIndeterminate( f"causal gap {gap}ns between {producer.span_id} and {s.span_id} " f"is within skew envelope {skew_envelope_ns}ns; ordering is indeterminate" ) # Topological order over parent + explicit-link edges. edges: dict[str, set[str]] = {s.span_id: set() for s in spans} for s in spans: if s.parent_span_id is not None: edges[s.parent_span_id].add(s.span_id) if s.consumed_tool_call_id is not None: producer = tool_producers[s.consumed_tool_call_id] edges[producer.span_id].add(s.span_id) indeg = {sid: 0 for sid in edges} for src, dsts in edges.items(): for d in dsts: indeg[d] += 1 ready = sorted([sid for sid, d in indeg.items() if d == 0]) order: list[str] = [] while ready: sid = ready.pop(0) order.append(sid) for d in sorted(edges[sid]): indeg[d] -= 1 if indeg[d] == 0: ready.append(d) if len(order) != len(spans): raise ReplayOrderingIndeterminate( "causal graph contains a cycle; no valid replay order exists" ) return order def replay_turn(spans: Iterable[Span], skew_envelope_ns: int) -> list[str]: order = certify_causal_order(spans, skew_envelope_ns) return order if __name__ == "__main__": turn = [ Span("t", None, 0, 1000), Span("m1", "t", 100, 300), Span("tool", "t", 310, 500, tool_call_id="c1"), Span("m2", "t", 505, 700, consumed_tool_call_id="c1"), ] print(replay_turn(turn, skew_envelope_ns=50)) # ok skewed = [ Span("t", None, 0, 1000), Span("m1", "t", 100, 300), Span("tool", "t", 310, 500, tool_call_id="c1"), Span("m2", "t", 320, 700, consumed_tool_call_id="c1"), # gap 20 < 50 ] try: replay_turn(skewed, skew_envelope_ns=50) except ReplayOrderingIndeterminate as e: print("refused:", e) ```