# Why does an instance diverge when replayed from an OTel GenAI trace that recorded tool arguments post-serialization? **Date of Record:** 2026 November 19 **Scope:** Applies to any agent runtime that reconstructs execution from OpenTelemetry GenAI semantic-convention spans (`gen_ai.*`) for the purpose of deterministic replay, regression diffing, or audit. Does not apply to live shadow-traffic replay where the original model endpoint is re-invoked, nor to trace formats that capture pre-serialization tool-call payloads. ## Core Distinction There are two distinct notions of "the same run" that get conflated the moment you build a replay harness on top of OTel GenAI spans: 1. **Trace-faithful replay** — the runtime consumes the span tree as ground truth and re-emits the recorded `gen_ai.tool.call.arguments` / `gen_ai.tool.call.result` values without re-deriving them. 2. **Model-faithful replay** — the runtime re-invokes the model with the recorded prompts and lets the model regenerate tool calls, then executes those tools against a sandboxed environment. The failure I am documenting lives in the seam between these two: a replay harness that *intends* model-faithful replay but *silently degrades* to trace-faithful replay for a subset of steps, because the OTel span attributes it reads are post-serialization artifacts and cannot be inverted back into the arguments the model actually emitted. The replay then "succeeds" — every span closes, every assertion passes — while having executed a different program than the original. This is the most dangerous class of replay failure because it is invisible to the harness's own pass/fail signal. ## What Was Observed A three-step agent task: `search(query) → fetch(url) → summarize(text)`. Original run recorded spans with these attributes (abbreviated): - Span A: `gen_ai.operation.name=execute_tool`, `gen_ai.tool.name=search`, `gen_ai.tool.call.arguments={"q": "vector db latency p99"}` (note: the harness's serializer emitted a JSON object with a space after the colon, and the model had actually emitted `{"q":"vector db latency p99"}` — no space). - Span B: `gen_ai.tool.name=fetch`, arguments `{"url": "https://example.com/a?b=1&c=2"}`, result a 47KB HTML blob truncated to 8KB by the exporter's attribute-length cap, with `gen_ai.tool.call.result.truncated=true` added by a custom processor. - Span C: `gen_ai.operation.name=chat`, prompt assembled from the *truncated* result. On replay, the harness re-invoked the model at step A with the recorded system prompt and the recorded user turn. The model — non-deterministic — emitted `search({"query": "vector-db latency p99"})`. The tool schema accepted both keys because the original schema had `additionalProperties: true`. The sandboxed `search` tool returned a *different* result set (different stemming, different ranking), which propagated to step B, which fetched a different URL, which produced a different summary. The harness reported `REPLAY_DIVERGED` at step C. An engineer "fixed" it by pinning replay to trace-faithful mode for steps A and B. The harness then reported `REPLAY_OK`. The divergence was gone — and so was the test's ability to detect the original schema ambiguity, which was the actual bug the replay was supposed to catch. The non-obvious part: **the recorded arguments were not the model's arguments.** The exporter's serializer normalized whitespace and (in one downstream pipeline) reordered keys alphabetically. Any replay that reconstructs model behavior from these attributes is reconstructing from a lossy projection, and the loss is exactly the signal you were trying to test. ## Condition of Validity Trace-faithful replay is only valid as a *regression oracle* when the following invariant holds: > For every span `S` in the trace, the tuple `(gen_ai.request.model, gen_ai.prompt.*, gen_ai.tool.call.arguments, gen_ai.tool.call.result)` as recorded is **canonically equivalent** to the tuple the runtime would have produced from the model's raw output under the same canonicalization function `C`. If the exporter applies a canonicalization `C_export` and the replay harness applies `C_replay`, the replay is sound only if `C_export ≡ C_replay` on the domain of all arguments the model could emit. In practice this fails in four places: 1. **Whitespace and key-order normalization** by the OTel exporter or an intermediate collector processor (e.g., a `transform` processor that pretty-prints JSON for dashboard readability). 2. **Attribute-length truncation** at `gen_ai.tool.call.result` when the result exceeds the exporter's `max_attr_length` (commonly 8–32KB). Truncation is one-way; the truncated prefix is not the result. 3. **Type coercion** in attribute encoding: OTel attributes are typed (string, int, double, bool, array), but tool arguments are arbitrary JSON. Nested objects are frequently flattened to strings, and `1` vs `1.0` vs `"1"` collapse or diverge depending on the SDK. 4. **Sampling and span drops** under head-based sampling: a partial trace replays as a *different* task, not a truncated one, because the model's step N+1 prompt depends on step N's result, which is missing. The invariant is checkable at trace-write time: emit a parallel attribute `gen_ai.tool.call.arguments.raw` containing the byte-exact model output *before* canonicalization, plus a `gen_ai.tool.call.result.sha256` of the untruncated result. If these are absent, the trace is not replayable in model-faithful mode and the harness must refuse rather than silently downgrade. ## Decision Boundary The harness must make an explicit, recorded choice per span, not per trace: - **If `arguments.raw` and `result.sha256` are present and the sandbox can reproduce the tool deterministically** → model-faithful replay is admissible; divergence is a real signal. - **If `arguments.raw` is absent but `arguments` is present and the tool is pure and deterministic** → trace-faithful replay is admissible *for that span only*, and the harness must annotate the replay report with `mode=trace_faithful, span_id=...` so downstream diffing knows the span was not re-derived. - **If `result.truncated=true`** → the span and every descendant span are non-replayable in model-faithful mode. The harness must halt the replay chain at that span; continuing produces a prompt the original model never saw. - **If any span in the causal chain is missing (sampling drop)** → refuse. A partial trace is not a shorter task; it is an undefined one. The boundary that gets crossed in production is the second bullet: teams treat "trace-faithful for this span" as a harmless fallback. It is not harmless when the span is upstream of a model call, because the model call's input is now a canonicalized artifact and the model's output is a function of that artifact, not of the original input. The replay is testing a program that never ran. ## Operational Effect The observable symptom is **replay flakiness that correlates with exporter configuration, not with code changes.** A team ships a collector update that adds a `transform` processor to pretty-print JSON tool arguments for a debugging dashboard. Replay pass rate drops from 98% to 61% overnight. No application code changed. The harness blames the model ("non-determinism increased"), the model team blames the harness, and the actual cause — a whitespace-normalizing processor three hops upstream of the trace store — is invisible because the processor's output is what everyone is looking at. The second-order effect is worse: to restore the pass rate, teams pin replay to trace-faithful mode globally. The harness goes green. The schema-ambiguity bug that motivated replay in the first place — the one where `{"q": ...}` and `{"query": ...}` both pass `additionalProperties: true` — is now permanently undetectable, because the replay never re-derives the model's arguments. The regression suite has become a tautology: it replays the recorded answers and confirms the recorded answers match. The guardrail is structural, not procedural: the replay harness must treat the absence of `arguments.raw` / `result.sha256` as a **hard error at replay-entry time**, not a per-span fallback. The trace schema must be versioned, and the harness must reject traces below the version that introduced raw-argument capture. This forces the observability pipeline to be treated as part of the test contract — which it is, whether or not anyone wrote it down. ```python import hashlib import json from dataclasses import dataclass from typing import Any class ReplayNotAdmissibleError(RuntimeError): """Raised when a trace span cannot support model-faithful replay.""" @dataclass(frozen=True) class SpanRecord: span_id: str tool_name: str arguments: dict[str, Any] # post-serialization, canonicalized by exporter arguments_raw: str | None # byte-exact model output, pre-canonicalization result_sha256: str | None # hash of untruncated result result_truncated: bool def admit_span_for_model_faithful_replay(span: SpanRecord) -> None: if span.arguments_raw is None: raise ReplayNotAdmissibleError( f"span {span.span_id} ({span.tool_name}): missing gen_ai.tool.call.arguments.raw; " "recorded arguments are a post-serialization projection and cannot be inverted " "into the model's emitted arguments. Refusing model-faithful replay." ) if span.result_sha256 is None: raise ReplayNotAdmissibleError( f"span {span.span_id} ({span.tool_name}): missing gen_ai.tool.call.result.sha256; " "cannot verify sandboxed tool reproduced the original result." ) if span.result_truncated: raise ReplayNotAdmissibleError( f"span {span.span_id} ({span.tool_name}): result was truncated at export; " "downstream model calls consumed a prefix the original model never saw. " "Halt replay chain at this span." ) # Sanity: raw must hash-consistently round-trip through the exporter's canonicalizer. try: reparsed = json.loads(span.arguments_raw) except json.JSONDecodeError as e: raise ReplayNotAdmissibleError( f"span {span.span_id}: arguments.raw is not valid JSON ({e}); " "exporter corrupted the raw payload." ) from e if reparsed != span.arguments: # Not fatal on its own, but the divergence must be logged: it means the # exporter applied a lossy canonicalization and any trace-faithful # fallback for this span is testing a different program. raise ReplayNotAdmissibleError( f"span {span.span_id}: arguments.raw does not round-trip to recorded arguments; " f"exporter canonicalization is lossy. raw={span.arguments_raw!r} " f"recorded={span.arguments!r}" ) def verify_replayed_result(span: SpanRecord, sandbox_result: bytes) -> None: if span.result_sha256 is None: raise ReplayNotAdmissibleError( f"span {span.span_id}: no result hash to verify against." ) actual = hashlib.sha256(sandbox_result).hexdigest() if actual != span.result_sha256: raise ReplayNotAdmissibleError( f"span {span.span_id} ({span.tool_name}): sandboxed tool produced a different " f"result than the original run (expected {span.result_sha256[:12]}…, " f"got {actual[:12]}…). Replay diverged at the tool layer, not the model layer — " "investigate tool determinism before blaming the model." ) ```