# Which invariants prevent semantic-convention trace replay from silently diverging when tool spans carry non-deterministic payloads **Date of Record:** 2026 December 3 **Scope:** Applies to replay of OpenTelemetry GenAI semantic-convention spans (`gen_ai.*`) captured from production agent runs, used either for incident forensics or as golden-trace regression fixtures. Does not apply to synthetic traces generated from a deterministic simulator, nor to replay where tool outputs are stubbed to recorded bytes at the transport layer. ## Core Distinction A GenAI trace is a *log of what an agent decided*, not a *specification of what it must decide again*. The semantic conventions (`gen_ai.operation.name`, `gen_ai.system`, `gen_ai.request.model`, `gen_ai.usage.input_tokens`, `gen_ai.tool.name`, `gen_ai.tool.call.id`) describe the shape of a call and its accounting, but they deliberately leave the *content* of `gen_ai.tool.call.arguments` and tool responses as opaque attributes. Replay engines that treat the trace as a deterministic script conflate two different invariants: - **Structural invariant** — the span tree, parent/child ordering, tool call IDs, and operation names must be preserved on replay. - **Semantic invariant** — the *decision* the model made given a specific context must be reproducible only to the extent the context is bit-identical. The failure mode arises when a replay harness enforces the structural invariant while assuming the semantic invariant holds for free. It does not. The moment a tool span is re-executed rather than re-fed, or the moment a model call is re-issued against a live endpoint, the trace is no longer a replay — it is a *re-enactment*, and any divergence is silently absorbed into the new trace rather than surfaced as a replay failure. ## What Was Observed A production support agent emitted the following span sequence during a customer-escalation incident: ``` invoke_agent (gen_ai.operation.name=invoke_agent, gen_ai.agent.name=support-triage) ├── chat (gen_ai.operation.name=chat, gen_ai.request.model=gpt-4o-2024-08-06) ├── execute_tool (gen_ai.tool.name=lookup_order, gen_ai.tool.call.id=call_a1) │ gen_ai.tool.call.arguments={"order_id":"ORD-55812"} │ gen_ai.tool.call.result={"status":"shipped","eta":"2024-09-02"} ├── execute_tool (gen_ai.tool.name=issue_refund, gen_ai.tool.call.id=call_a2) │ gen_ai.tool.call.arguments={"order_id":"ORD-55812","amount":42.00} └── chat (gen_ai.operation.name=chat, gen_ai.request.model=gpt-4o-2024-08-06) ``` The incident: `issue_refund` was called twice in production (the second call carried `call_a3` and was *not* in the captured trace because the exporter dropped it under backpressure). During forensic replay, the harness re-executed `lookup_order` against the live order service. The order had since transitioned to `refunded`, so the replayed tool result was `{"status":"refunded","eta":null}`. The replayed `chat` span then saw a different context and *did not* call `issue_refund` at all. The replay harness reported **PASS** because the structural invariant (span tree shape up to `call_a2`) was satisfied — the missing refund call was interpreted as "the model correctly declined to double-refund." The trace diverged at the semantic layer while satisfying every convention-level check. The forensic conclusion drawn from the replay ("the agent would not have double-refunded") was the exact inverse of the truth ("the agent did double-refund, and the second call was lost to exporter backpressure"). The regression fixture built from this trace would have *rewarded* the buggy behavior. The trigger condition is precise: a replay harness that (a) re-executes `execute_tool` spans against live dependencies instead of pinning recorded results, and (b) validates only span-tree shape and `gen_ai.*` attribute presence, not the *identity* of downstream decisions. Under those two conditions, any non-deterministic tool output — timestamps, order state, inventory, rate-limit headers, feature flags — becomes a silent divergence source, and the replay's PASS signal is uncorrelated with behavioral fidelity. ## Condition of Validity Replay is only a valid forensic or regression instrument when all of the following hold: 1. **Tool-result pinning.** Every `execute_tool` span must be replayed with its recorded `gen_ai.tool.call.result` bytes, keyed by `gen_ai.tool.call.id`, with the live dependency stubbed. Re-execution is permitted only under a separate "shadow mode" that is explicitly *not* labeled replay and whose divergences are recorded as deltas, not passes. 2. **Completeness accounting.** The trace must carry an exporter-completeness marker. OpenTelemetry's `dropped_attributes_count` and `dropped_spans_count` on the resource and span are the only convention-sanctioned signals; a replay harness that does not read them cannot distinguish "the model chose not to call `issue_refund`" from "the `issue_refund` span was dropped." Absent a completeness assertion, absence-of-span must be treated as unknown, never as negative evidence. 3. **Model-call determinism scope.** `chat` spans may be re-issued against a live endpoint only when `gen_ai.request.temperature=0` *and* the endpoint is pinned to the exact `gen_ai.request.model` revision recorded. Even then, provider-side nondeterminism (MoE routing, batch composition) makes byte-identical output a probabilistic, not deterministic, property; the harness must therefore diff the *tool-call sequence*, not the completion text. 4. **Decision-identity check.** The invariant that actually matters is: the ordered sequence of `(gen_ai.tool.name, canonical(gen_ai.tool.call.arguments))` pairs must match between recorded and replayed traces. Span count and tree shape are necessary but insufficient. If any of (1)–(4) is unmet, the replay output is a *plausible narrative*, not evidence. ## Decision Boundary The boundary between "replay is admissible as evidence" and "replay is a fiction" is crossed the moment the harness re-derives any input to a `chat` span from a source other than the recorded trace. Concretely: | Harness behavior | Admissible? | Why | |---|---|---| | Tool results re-fed from `gen_ai.tool.call.result`, model re-issued at temp=0, pinned revision | Yes | All inputs to the decision are the recorded inputs. | | Tool results re-fed, model output compared only on tool-call sequence | Yes | Tolerates provider nondeterminism without masking decision divergence. | | Tool results re-executed live, model re-issued | No | Divergence is unobservable; PASS is meaningless. | | Tool results re-fed, but trace lacks completeness marker and a span is missing | No | Absence is ambiguous; must be flagged `INCOMPLETE`, not `PASS`. | | Tool results re-fed, but `gen_ai.tool.call.arguments` were truncated by attribute limits | No | Canonicalization of a truncated argument set can match a different real call. | The last row is the subtle one. Semantic conventions cap attribute length (default 128 KiB in most SDKs, frequently configured lower). A `gen_ai.tool.call.arguments` value that was truncated at capture time will canonicalize to a *prefix*, and two genuinely different tool calls sharing a prefix will collide during the decision-identity check. The harness must record and honor `dropped_attributes_count` and refuse to certify identity when arguments were truncated. ## Operational Effect Once the invariants above are enforced, the replay harness becomes a *falsification* tool rather than a *confirmation* tool. Its default verdict for any trace without tool-result pinning, completeness markers, and untruncated arguments is `INADMISSIBLE`, not `PASS`. This inverts the failure economics: instead of a silent divergence producing a false PASS (which is worse than no replay at all, because it manufactures confidence), the harness fails loudly at the admissibility gate before any model call is issued. The second-order effect is on regression fixtures. A golden trace built from an admissible replay is a fixture whose *decision sequence* is the assertion target, not its completion text. When the underlying model revision changes, the fixture will fail on decision divergence — which is the correct signal — rather than drift silently because the completion text was never compared byte-for-byte anyway. Teams that skip the admissibility gate typically discover this six months later when a "passing" regression suite fails to catch a tool-selection regression, and the root cause is always the same: the suite was asserting structure, and the bug lived in semantics. ```python from __future__ import annotations import hashlib import json from dataclasses import dataclass from typing import Any class InadmissibleReplayError(RuntimeError): """Raised when a trace cannot be admitted as replay evidence.""" @dataclass(frozen=True) class ToolSpan: call_id: str name: str arguments: dict[str, Any] result: dict[str, Any] | None arguments_truncated: bool result_truncated: bool @dataclass(frozen=True) class TraceRecord: spans: list[ToolSpan] dropped_spans_count: int model_revision: str temperature: float def _canonical(obj: dict[str, Any]) -> str: return hashlib.sha256( json.dumps(obj, sort_keys=True, separators=(",", ":")).encode() ).hexdigest() def assert_admissible(trace: TraceRecord) -> None: if trace.dropped_spans_count != 0: raise InadmissibleReplayError( f"trace dropped {trace.dropped_spans_count} spans; " "absence of a tool span is ambiguous and cannot be read as negative evidence" ) if trace.temperature != 0.0: raise InadmissibleReplayError( f"chat spans recorded at temperature={trace.temperature}; " "decision sequence is not reproducible" ) for span in trace.spans: if span.result is None: raise InadmissibleReplayError( f"tool span {span.call_id} ({span.name}) has no recorded result; " "re-executing against a live dependency is not replay" ) if span.arguments_truncated or span.result_truncated: raise InadmissibleReplayError( f"tool span {span.call_id} ({span.name}) was truncated at capture; " "canonical identity check would collide on prefixes" ) def decision_sequence(trace: TraceRecord) -> list[tuple[str, str]]: return [(s.name, _canonical(s.arguments)) for s in trace.spans] def verify_replay(recorded: TraceRecord, replayed: TraceRecord) -> None: assert_admissible(recorded) assert_admissible(replayed) if recorded.model_revision != replayed.model_revision: raise InadmissibleReplayError( f"model revision drift: {recorded.model_revision} -> {replayed.model_revision}" ) rec_seq = decision_sequence(recorded) rep_seq = decision_sequence(replayed) if rec_seq != rep_seq: first_div = next( (i for i, (a, b) in enumerate(zip(rec_seq, rep_seq)) if a != b), min(len(rec_seq), len(rep_seq)), ) raise InadmissibleReplayError( f"decision divergence at index {first_div}: " f"recorded={rec_seq[first_div:first_div+1]} " f"replayed={rep_seq[first_div:first_div+1]}" ) ```