# Why does an instance lose tool-call causality when OpenTelemetry GenAI spans are exported out of order across a checkpoint boundary **Date of Record:** 2026 December 2 **Scope:** Applies to agents that emit OpenTelemetry GenAI semantic-convention spans (`gen_ai.*` attributes, `gen_ai.tool.*` events, model invocation events) and persist span state across a mid-task checkpoint/resume boundary. Does not apply to single-process, non-checkpointed agents whose exporter runs in-process with a synchronous batch processor and never restarts. ## Core Distinction The GenAI semantic conventions describe *what* an attribute means, not *when* it becomes durable relative to the agent's own state. A tool-call span is a record of an operation whose result the agent has already consumed; the checkpoint is a record of the agent's reasoning state. These two records are produced by different subsystems—the tracer/exporter pipeline and the agent's state store—and have **no shared ordering guarantee**. The failure mode is not "spans are lost." It is that a resumed instance reconstructs causality from spans whose parent/child linkage and `gen_ai.tool.call.id` correlation were valid only within the pre-checkpoint process, so a tool result observed after resume appears to have no originating invocation, or worse, is attributed to the *wrong* invocation when call IDs are regenerated. The non-obvious boundary: `gen_ai.tool.call.id` is conventionally unique *per process*, not globally. When a resumed instance re-issues a tool call that was in-flight at checkpoint time, and the pre-checkpoint span for the original call is flushed late (batch export after the checkpoint write), the trace now contains two spans sharing the logical call ID but with different trace IDs, different parent span IDs, and no ordering attribute that disambiguates which one the agent's memory actually consumed. ## What Was Observed An agent executing a three-step plan (retrieve → transform → write) checkpointed between step 2's tool dispatch and its result ingestion. The trace pipeline used a batch span processor with a 5s schedule delay; the state store wrote the checkpoint synchronously. The pre-checkpoint tool span for `transform` was emitted with `gen_ai.tool.name="transform"`, `gen_ai.tool.call.id="tc_7"`, and a parent span ID belonging to the pre-checkpoint model invocation event. The process was killed before the batch flushed. On resume, the agent re-dispatched `transform`, generating a *new* span with a *new* trace ID, a *new* `gen_ai.tool.call.id="tc_7"` (the ID generator was seeded from a monotonic counter reset on cold start), and the same `gen_ai.operation.name="execute_tool"`. The late-flushed original span arrived at the collector ~4.1s after the resumed span. The backend's trace assembler merged them into a single logical call because the correlation key it used was `(gen_ai.tool.call.id, gen_ai.tool.name)`, not `(trace_id, span_id)`. The result: a duplicate execution record with conflicting `gen_ai.tool.call.result` payloads, one of which the agent never actually consumed. The agent's own episodic memory recorded only the post-resume result. Replay-based verification—the auditor agent's mechanism for checking that the write step used the correct transform output—reconstructed the *pre-resume* result and flagged a divergence that did not exist in the agent's actual execution history. Three observable signatures: 1. **ID collision across trace boundaries.** Two spans, same `gen_ai.tool.call.id`, disjoint `trace_id`. This is legal under the spec (IDs are not required to be globally unique) but poison to any correlation layer keyed on the call ID alone. 2. **Orphaned model invocation events.** The pre-checkpoint model invocation event span (carrying `gen_ai.usage.input_tokens`, `gen_ai.request.model`) was flushed but its child tool span's parent linkage pointed at a span ID that no longer exists in the resumed trace. The event is retained; the edge is dropped. 3. **Temporal inversion.** The resumed span's `start_time_unix_nano` is *later* than the original's `end_time_unix_nano`, yet the original arrives at the collector *after* the resumed span's export. Wall-clock ordering and export ordering disagree, and the semantic conventions provide no `gen_ai.tool.call.attempt` or `gen_ai.tool.call.epoch` attribute to reconcile them. ## Condition of Validity The invariant that makes traversal safe is: **a tool-call span is authoritative for the agent's state only if its `(trace_id, span_id)` pair is durably linked to the checkpoint that consumed its result, and the `gen_ai.tool.call.id` is scoped to that trace.** The semantic conventions do not enforce this; they treat the call ID as a correlation convenience. The guardrail is therefore architectural, not spec-mandated. Concretely, the following must hold for a resumed instance to trust its trace-derived memory: - The checkpoint record must persist the `trace_id` and `span_id` of every tool call whose result was ingested before the checkpoint, and the agent must refuse to re-dispatch any call whose ID appears in that set. - `gen_ai.tool.call.id` must be generated from a source that survives cold start (UUIDv7 or a persisted counter), not an in-process monotonic counter. - The exporter must use a *synchronous* flush at checkpoint time, or the checkpoint must block until the batch processor reports the relevant spans as exported. Otherwise the checkpoint's "consumed" set and the trace's "emitted" set diverge, and the late flush produces spans that are real but not part of the agent's causal history. - The trace assembler must key correlation on `(trace_id, span_id)` and treat `gen_ai.tool.call.id` as a *within-trace* alias only. When any of these is absent, span-derived replay is unsound and must be treated as advisory, not authoritative. ## Decision Boundary The decision the resumed instance must make is whether to **re-dispatch** an in-flight tool call or **reconcile** against the pre-checkpoint trace. The boundary condition is the presence of a durable `(trace_id, span_id, gen_ai.tool.call.id)` tuple in the checkpoint's consumed-set. - If the tuple is present and the late-flushed span carries a matching `gen_ai.tool.call.result`, the instance must *reconcile*: adopt the pre-checkpoint result, suppress re-dispatch, and emit a `gen_ai.tool.call.reconciled=true` annotation on a synthetic span that re-parents to the original trace. - If the tuple is present but no matching result span exists (flush lost), the instance must *re-dispatch* with a **new** call ID and explicitly mark the original as `gen_ai.tool.call.abandoned=true` in the checkpoint, so the auditor does not treat the missing result as a divergence. - If the tuple is absent, the instance is free to re-dispatch, but must not reuse the call ID; a fresh ID prevents the collector from merging the two attempts. The failure observed above occurred because the instance took the third branch (tuple absent, because the checkpoint was written before the consumed-set update) while the exporter still held the original span. The two branches were not mutually exclusive in the implementation, and the collector's correlation key amplified the contradiction. ## Operational Effect The downstream effect is **verification replay divergence**: an auditor agent that reconstructs execution from spans will compute a different tool-result lineage than the executing agent's episodic memory, and will raise a false integrity alert. In a multi-agent swarm where the auditor and executor share a trace backend, this divergence propagates—the auditor's "correction" instruction is itself a poisoned state injection, because it asserts a result the executor never produced. The bound: span-derived verification is only sound when the trace is *causally closed* over the checkpoint boundary, meaning every consumed tool result has exactly one authoritative span and that span's `(trace_id, span_id)` is in the checkpoint's consumed-set. Absent causal closure, the auditor must fall back to the agent's own episodic record and treat the trace as a lossy secondary signal. The cost of enforcing closure is a synchronous exporter flush at every checkpoint—a latency tax proportional to checkpoint frequency—which is the invariant most teams skip, and precisely the one that prevents this failure. ```python import uuid from dataclasses import dataclass, field class TraceCausalityError(RuntimeError): """Raised when a resumed agent cannot establish causal closure over a checkpoint boundary.""" @dataclass class ToolCallRecord: trace_id: str span_id: str call_id: str tool_name: str result: str | None exported: bool = False @dataclass class Checkpoint: consumed: dict[str, ToolCallRecord] = field(default_factory=dict) abandoned_call_ids: set[str] = field(default_factory=set) def reconcile_on_resume( cp: Checkpoint, incoming_call_id: str, tool_name: str, late_flushed: list[ToolCallRecord], ) -> tuple[str, str | None]: """ Decide whether a resumed instance re-dispatches or reconciles an in-flight tool call. Returns (action, result) where action is 'reconcile' or 'redispatch'. Raises TraceCausalityError when the checkpoint's consumed-set and the exporter's emitted-set disagree in a way that makes replay unsound. """ prior = cp.consumed.get(incoming_call_id) if prior is not None: if not prior.exported: # Consumed but never exported: the trace cannot vouch for the result. raise TraceCausalityError( f"tool call {incoming_call_id!r} ({tool_name}) was consumed at checkpoint " f"but its span (trace={prior.trace_id}, span={prior.span_id}) was never " f"flushed; span-derived replay is not causally closed" ) if prior.result is None: cp.abandoned_call_ids.add(incoming_call_id) return "redispatch", None return "reconcile", prior.result # No consumed record: must not reuse the call ID, or the collector will # merge the late-flushed original with the new attempt. colliding = [s for s in late_flushed if s.call_id == incoming_call_id] if colliding: raise TraceCausalityError( f"call id {incoming_call_id!r} absent from checkpoint consumed-set but " f"present in {len(colliding)} late-flushed span(s); reusing it would " f"create a phantom duplicate execution record" ) return "redispatch", None def dispatch_with_fresh_id(tool_name: str) -> ToolCallRecord: # UUIDv7-style: survives cold start, no in-process counter reset hazard. return ToolCallRecord( trace_id=uuid.uuid4().hex, span_id=uuid.uuid4().hex[:16], call_id=uuid.uuid4().hex, tool_name=tool_name, result=None, ) ```