# Which invariants prevent replay divergence when reconstructing multi-agent sessions from OTel GenAI span graphs **Date of Record:** 2026 November 24 **Scope:** Applies to offline deterministic replay of multi-agent sessions reconstructed from OpenTelemetry GenAI semantic-convention span graphs (`gen_ai.*` attributes, `trace_id`/`span_id`/`parent_span_id` DAGs) where replay re-invokes LLM and tool calls to reproduce a recorded run. Does not cover live tracing, sampling policy, or single-agent non-replayed observability pipelines. ## Core Distinction A trace is a *causal record of what happened*. A replay is a *re-execution that must reproduce what happened*. OTel span graphs are lossy with respect to replay in exactly one dangerous way: they record **what the model returned**, not **the exact input bytes that produced it**, and they record **child completion order** only via timestamps, not via the causal ordering that the orchestrator actually depended on. The non-obvious boundary: a span graph can be *structurally complete* (every span present, every parent link intact, no orphans) while being *semantically insufficient* for replay. Completeness of the DAG is not the invariant that guarantees replay equivalence. The invariant that does is **input-determinism at every non-deterministic leaf**, and OTel GenAI spans silently violate it in three places: prompt assembly, tool-output capture, and fan-in ordering. ## What Was Observed A three-agent session (planner → two concurrent workers → synthesizer) was recorded with GenAI spans. Replay was run against the recorded graph with a `temperature=0` model and stubbed tools that replayed captured outputs. Replay diverged on 41% of runs at the synthesizer step, despite identical span structure, identical seed, and identical tool stubs. The trace showed the synthesizer span carrying `gen_ai.prompt` as a single assembled string plus `gen_ai.completion`. Replay reconstructed the synthesizer prompt by re-assembling from the two worker spans. Divergence root cause: the orchestrator's original assembly ordered worker outputs by **completion arrival time**, not by `span_id` or `start_time`. The two worker spans had overlapping `start_time` values (both launched in the same millisecond) and completion times that inverted between runs because one worker's tool call hit a cache miss. The OTel graph recorded the *final* assembled prompt (correct), but the replay harness re-derived it from child spans using `span_id` sort order (wrong). The recorded prompt was present in the trace but the replay harness treated it as derived state rather than authoritative input. Second observed failure: a tool span captured `gen_ai.tool.output` as a JSON-serialized dict. The original tool returned a dict whose key order was insertion-ordered by a set iteration, which varied per process. Replay stubbed the tool with the captured JSON, which had a *stable* key order. The model's downstream reasoning differed because the serialized prompt prefix differed by byte offset, shifting the tokenization boundary. Third: a retry span (`gen_ai.request.retry_count=1`) was parented to the original attempt's span rather than to the orchestrator. Replay treated the retry as a child of the failed attempt and re-issued the *failed* attempt first, doubling tool side effects. ## Condition of Validity The replay-equivalence invariant holds only when all three of the following are enforced: 1. **Authoritative-input capture.** Every span that consumes an LLM input must record the *exact bytes* sent to the model (post-assembly, post-truncation, post-template), not the components from which it was assembled. `gen_ai.prompt` must be the final string, and replay must treat it as a leaf input, never re-derive it from children. If a harness re-derives, it has replaced the causal record with a reconstruction and the invariant is void. 2. **Byte-stable leaf capture.** Tool outputs, retrieval results, and any non-LLM leaf must be captured as the exact serialized bytes the model saw, including key order, whitespace, and encoding. A JSON round-trip through a parser is not byte-stable and must not be used as the replay stub. Capture must be at the transport boundary (the bytes handed to the model), not at the semantic boundary (the parsed object). 3. **Causal-order preservation for fan-in.** When an orchestrator's output depends on child completion order, that order must be recorded explicitly (e.g., a `gen_ai.fanin.order` attribute or an ordered list of child span_ids on the parent), because `start_time` and `span_id` do not encode arrival order and timestamps are not monotonic across processes. Absent an explicit order attribute, replay must refuse to reconstruct fan-in and must fail closed rather than guess. Retry spans must be parented to the *initiating* span (the orchestrator decision), with the failed attempt as a sibling linked by `gen_ai.retry.of`, so replay does not re-execute failed branches. ## Decision Boundary The boundary between replayable and non-replayable traces is not "all spans present" but "all non-deterministic leaves carry authoritative bytes and all order-dependent fan-ins carry explicit order." A trace crossing the boundary in either direction: - **Replayable:** every LLM span has final prompt bytes; every tool span has transport-captured output bytes; every fan-in parent has an explicit child order attribute; retries are sibling-linked. - **Non-replayable (must fail closed):** any LLM span whose prompt is absent and would be re-derived from children; any tool span whose output was captured via parse-then-serialize; any fan-in with overlapping child `start_time` and no order attribute; any retry span parented under the failed attempt. The correct behavior on a non-replayable trace is to halt replay with a typed error identifying the offending span, not to best-effort reconstruct. Best-effort reconstruction is the mechanism by which the 41% divergence was produced: it looked like it worked because the DAG was complete. ## Operational Effect Teams that enforce the three invariants see replay divergence drop to zero on recorded sessions and, critically, gain a *detectable* failure mode: non-replayable traces raise at replay time instead of silently producing a different session. Teams that rely on DAG completeness alone see divergence that is invisible in span counts, invisible in structural validators, and only surfaces as downstream behavioral drift—making root-cause attribution effectively impossible because the replay harness and the original run disagree on inputs the trace *appeared* to contain. The operational rule: **a span graph is a sufficient replay record only if it is treated as a set of authoritative leaf inputs plus an explicit causal order, never as a set of components to be re-derived.** Structural validators must be extended with an input-determinism validator that rejects traces where any LLM prompt is reconstructible-only, any tool output is parse-round-tripped, or any fan-in lacks explicit order. ```python from dataclasses import dataclass from typing import Optional class ReplayDivergenceError(RuntimeError): """Raised when a span graph cannot be replayed without divergence.""" pass @dataclass class Span: span_id: str parent_id: Optional[str] kind: str # "llm" | "tool" | "fanin" | "retry" final_prompt_bytes: Optional[bytes] # authoritative input, post-assembly tool_output_bytes: Optional[bytes] # transport-captured, byte-stable child_order: Optional[list[str]] # explicit fan-in arrival order retry_of: Optional[str] = None start_time_ns: int = 0 def validate_replayable(spans: list[Span]) -> None: for s in spans: if s.kind == "llm" and s.final_prompt_bytes is None: raise ReplayDivergenceError( f"span {s.span_id}: LLM prompt absent; re-derivation from " f"children is not replay-equivalent" ) if s.kind == "tool" and s.tool_output_bytes is None: raise ReplayDivergenceError( f"span {s.span_id}: tool output not transport-captured; " f"parse-round-trip is not byte-stable" ) if s.kind == "fanin" and s.child_order is None: raise ReplayDivergenceError( f"span {s.span_id}: fan-in without explicit child order; " f"start_time/span_id sort does not encode arrival order" ) if s.kind == "retry" and s.parent_id is not None: parent = next((p for p in spans if p.span_id == s.parent_id), None) if parent is not None and parent.kind != "fanin": raise ReplayDivergenceError( f"span {s.span_id}: retry parented under non-initiating " f"span {s.parent_id}; replay would re-execute failed branch" ) ```