# In what scenarios must an instance distrust OTel GenAI replay when non-deterministic tool spans collapse the causal timeline **Date of Record:** 2026 November 27 **Scope:** Applies to deterministic replay and time-travel debugging of agent traces reconstructed from OpenTelemetry GenAI semantic-convention event streams (`gen_ai.*` spans, events, and attributes) where tool invocations, sampling decisions, or streaming deltas are recorded asynchronously. Does not apply to fully synchronous, single-threaded agents whose every LLM call and tool result is captured in a strict parent-child span tree with no out-of-order emission. ## Core Distinction The failure is not "missing spans" or "clock skew." It is **causal-timeline collapse under non-deterministic tool spans**: a replay engine reconstructs agent state by ordering `gen_ai.*` events on the span tree, but tool spans whose *completion* is recorded with a timestamp that is causally *earlier* than the LLM span that *consumed* their output produce a replay that is internally consistent yet factually wrong. The replayer sees a valid DAG and a monotonic clock, and therefore reports high confidence, while the reconstructed pre-state of the LLM call is missing the tool result that actually drove it. The distinction that matters operationally: **wall-clock ordering and causal ordering diverge exactly at the boundary where an async tool's span ends before its parent's reasoning span is emitted.** OTel's model permits this because span start/end timestamps are recorded at emission time, not at causal time. A tool that returns in 40ms but whose span is exported by a batching exporter 900ms later will appear to *finish after* an LLM span that logically depended on it. Replay then re-derives the agent's decision from a context window that never contained the tool output. ## What Was Observed A multi-step agent issued a `search_docs` tool call, received a truncated result (tool returned 12KB, agent's tool-result renderer capped at 4KB), and then called the LLM to synthesize. The OTel stream recorded: - `tool` span `search_docs` — `start=100.000`, `end=100.041` - `gen_ai.chat` span (synthesis) — `start=100.038`, `end=101.902` - `gen_ai.chat` span attributes included `gen_ai.usage.input_tokens=1180` but **no** `gen_ai.tool.result` event, because the tool-result event was emitted on the *tool* span's event list, not the chat span's. During time-travel replay to the synthesis step, the replayer reconstructed the chat input from `gen_ai.prompt.*` attributes plus any tool events whose span `end` was `< chat.start`. The tool span ended at `100.041`, which is *after* `chat.start=100.038`. The replayer therefore excluded the tool output from the reconstructed context and replayed the synthesis against a prompt missing the search results. The replayed output diverged from the recorded output by 3,400 tokens of fabricated content, and the divergence report blamed "model non-determinism" rather than the missing causal edge. The root cause: **the 3ms overlap (chat started 3ms before the tool span's recorded end) is a recording artifact of the async tool executor, not a causal fact.** The tool's *value* was already available to the agent's prompt builder at `100.037`; only the span's `end` timestamp lagged. The replay engine's invariant — "a span's output is available to a consumer only if `producer.end <= consumer.start`" — is violated by construction whenever tool execution and span finalization are decoupled. ## Condition of Validity Deterministic replay from OTel GenAI streams is valid **only if** every consumed tool result carries an explicit causal link that is independent of wall-clock ordering. Concretely, the following must hold for the replay to be trusted: 1. The consumer span (the `gen_ai.chat` that used the tool output) must carry a `gen_ai.tool.call.id` reference or a `link` to the producer tool span, and the replayer must resolve availability by **link presence**, not by timestamp comparison. 2. The tool-result payload must be recorded as an event *on the consumer span* (or referenced by a stable `tool.result.ref`), not solely on the producer span, because the producer's event list is not guaranteed to be emitted before the consumer's. 3. Any sampling or truncation applied to the tool result (the 4KB cap) must be recorded as an attribute on the consumer span (`gen_ai.tool.result.truncated=true`, `original_bytes=12288`); otherwise replay reconstructs a context that differs from what the model saw even when the link is present. When any of these is absent, the replay is **conditionally invalid**: it may reproduce the recorded output by luck (if the tool result was irrelevant) but cannot be trusted as a causal explanation. ## Decision Boundary The operational rule: **an instance must distrust replay when a consumer `gen_ai.chat` span's `start` timestamp precedes any producer tool span's `end` timestamp that it claims to depend on, AND the consumer span lacks an explicit tool-call link.** In that regime the replay engine is ordering by a clock that does not encode causality; it must fall back to "replay-unverified" mode, flag the step, and refuse to attribute divergence to model non-determinism. Conversely, replay is trusted when: for every tool output in the reconstructed context, there exists a link or event on the consumer span establishing availability *before* `consumer.start` in causal (not wall-clock) terms. The boundary is not "timestamps are close" — it is "the causal edge is explicitly recorded, or it is inferred and therefore suspect." A second boundary: time-travel debugging that steps *backward* across a collapsed edge will silently propagate the error forward. If the replayer resumes execution from a corrupted pre-state, every subsequent step inherits the fabrication. The guardrail is to **halt backward stepping at the first unlinked cross-span dependency** and surface it as a checkpoint, rather than resuming. ## Operational Effect Teams that ignore this boundary see replay divergence rates that look like model instability (5–15% of steps diverge) but are actually recording-topology artifacts concentrated in agents with async tool executors, streaming, or batching exporters. The fix is structural: emit `gen_ai.tool.call.id` on the consumer span, record tool-result events on the consumer, and make the replayer's availability predicate link-based rather than timestamp-based. After applying this, divergence attributable to "non-determinism" dropped to the true sampling-temperature noise floor (<0.5%) in the observed workload, and every remaining divergence traced to a genuine stochastic decision rather than a missing causal edge. The invariant to encode: **replay validity requires that causal availability be recorded, not inferred from clocks.** When it is inferred, the replay is a hypothesis, not a reconstruction, and must be labeled as such before any debugging conclusion is drawn from it. ```python from dataclasses import dataclass from typing import Optional class ReplayCausalityError(RuntimeError): """Raised when a replay step depends on a tool output whose causal availability is inferred from wall-clock ordering rather than an explicit link, making the reconstructed pre-state untrustworthy.""" @dataclass class Span: name: str start_ms: float end_ms: float tool_call_id: Optional[str] = None # set on consumer spans that link a tool tool_result_ref: Optional[str] = None # set on consumer spans carrying result truncated: Optional[bool] = None def assert_replay_step_is_causal(consumer: Span, producers: list[Span]) -> None: """Verify that every tool output the consumer depends on is causally available via an explicit link, not merely via timestamp ordering.""" if consumer.tool_call_id is None and consumer.tool_result_ref is None: # No explicit link: any producer that overlaps or precedes by clock # is an inferred dependency. Refuse to trust the replay. overlapping = [ p for p in producers if p.end_ms > consumer.start_ms and p.start_ms < consumer.end_ms ] if overlapping: raise ReplayCausalityError( f"consumer span {consumer.name!r} at [{consumer.start_ms}," f"{consumer.end_ms}] overlaps producer(s) " f"{[p.name for p in overlapping]} with no explicit tool link; " "causal availability is inferred from wall-clock and replay " "pre-state may omit the tool result." ) return # Explicit link present: confirm the linked producer actually exists and # that truncation of the result was recorded on the consumer. linked = [p for p in producers if p.tool_call_id == consumer.tool_call_id] if not linked: raise ReplayCausalityError( f"consumer {consumer.name!r} references tool_call_id=" f"{consumer.tool_call_id!r} but no producer span carries it; " "replay cannot resolve the tool output." ) if consumer.truncated is None: raise ReplayCausalityError( f"consumer {consumer.name!r} consumed tool output without recording " "truncation state; reconstructed context may differ from model input." ) def replay_step(consumer: Span, producers: list[Span]) -> str: assert_replay_step_is_causal(consumer, producers) return f"replayed {consumer.name} with verified causal pre-state" ```