# Where must an instance refuse to close an agent span when the OTel GenAI `gen_ai.operation.name` and the emitted `gen_ai.tool.call.id` disagree across a resumed checkpoint **Date of Record:** 2026 November 26 **Scope:** Applies to agent runtimes that emit OpenTelemetry GenAI spans (`gen_ai.operation.name`, `gen_ai.tool.name`, `gen_ai.tool.call.id`, `gen_ai.usage.input_tokens`/`output_tokens`) across a checkpoint/resume boundary and correlate spans by `trace_id`+`gen_ai.tool.call.id`. Does not apply to single-shot, non-resumable inference calls whose span lifecycle is fully contained in one process. ## Core Distinction The failure is not "a span was dropped." It is that a *resumed* agent instance emits a span whose identity attributes describe a different logical tool execution than the one the trace already recorded before the checkpoint. OpenTelemetry's GenAI conventions treat `gen_ai.tool.call.id` as the join key between a tool invocation and its result, and `gen_ai.operation.name` (`chat`, `execute_tool`, `invoke_agent`) as the discriminator for what the span *is*. Those two attributes are assumed to be a stable, monotonic function of the agent's own state. Across a resume they are not: the model re-derives the tool call from a replayed context, and the re-derivation is probabilistic. The span is *structurally valid* (passes schema, has required attributes, closes cleanly) while being *semantically a duplicate* — and the trace backend, which dedups on `tool.call.id`, silently keeps the pre-checkpoint span and discards the post-resume one, or worse, keeps both and double-counts token usage. The boundary is this: a span is only legitimately closeable if the `(operation.name, tool.call.id)` pair it carries is provably the same logical execution as any span already emitted under the same `trace_id`. When the runtime cannot prove that, it must not close the span as a normal `execute_tool` — it must emit it under a distinct operation or mark it, because a cleanly-closed span is an assertion of continuity that the resume path cannot make. ## What Was Observed A research agent runs a five-step plan. Step 3 is `execute_tool` → `search_corpus`, `gen_ai.tool.call.id = "call_a1"`. The runtime checkpoints after the tool returns but before the model consumes the result (a deliberate checkpoint: persist the tool output, defer the reasoning step). The process is killed by an OOM in the reasoning worker. On resume, the runtime reloads the checkpoint: conversation history through the assistant's tool-call turn, plus the persisted tool result. But the model is re-invoked to *continue*, and the continuation prompt includes the tool result in the history. The model, seeing a tool result with no matching assistant tool-call block *in the shape it expects* (the serializer re-emitted the tool-call block with a fresh UUID because the original `call_a1` was stored in a side table keyed by checkpoint epoch, not by message), re-emits the tool call. The runtime sees a new `tool.call.id = "call_b7"`, executes `search_corpus` again, and closes a second `execute_tool` span. Trace backend behavior, observed across three vendors: 1. **Jaeger/Tempo (span-append):** both spans persist. The trace now shows two `execute_tool` spans for one logical step. `gen_ai.usage.output_tokens` is summed across both, inflating the step's cost by ~1.8×. The `invoke_agent` parent span's `gen_ai.usage.input_tokens` reflects only the post-resume context, so the sum of child usage exceeds the parent — a contradiction no invariant catches because OTel does not require child usage ≤ parent usage. 2. **Vendor A (dedup on `tool.call.id`):** keeps `call_a1`, drops `call_b7`. The dropped span is the one that actually *has* the result (the pre-checkpoint span closed before the result was attached, because the runtime deferred result attachment). The surviving span has `gen_ai.tool.name` but no result event. The trace is now a lie: it claims a tool executed and returned nothing. 3. **Vendor B (dedup on `(trace_id, tool.call.id, operation.name)` but treats `call_b7` as new):** both persist, and a downstream evaluator that counts `execute_tool` spans per `invoke_agent` to detect "tool-call loops" flags the trace as a runaway loop. The agent is quarantined by an automated guard that cannot distinguish a resume-duplicate from a genuine retry storm. The non-obvious part: in all three cases the runtime's span-emission code was *correct per the spec*. The bug is that the spec's join key (`tool.call.id`) is assumed to be assigned once per logical execution, but the resume path re-derives it from model output, and model output is non-deterministic. The `tool.call.id` is not a stable identifier — it is a *model-generated* identifier, and the model was never told it had already generated one. ## Condition of Validity The invariant holds — a resumed span may close as a normal `execute_tool` — only if the runtime can satisfy *all* of: - The checkpoint stores the original `gen_ai.tool.call.id` **inside the message block itself**, not in a side table keyed by epoch, so the resumed serializer re-emits the identical id. - The resume path is *replay*, not *re-derivation*: the model is not re-invoked to produce the tool call; the persisted tool-call block is injected verbatim and the model is invoked only to consume the result. - The span carries a `gen_ai.tool.call.id` that was minted *before* the checkpoint and is treated as immutable across resume — i.e., the id is a runtime-assigned UUIDv7, not a model-sampled string. If any of these fail, the span is a *re-derivation*, and a re-derived `tool.call.id` is a new logical execution by the spec's own definition. Closing it as `execute_tool` under the same `trace_id` is the failure. A second, sharper condition: even with verbatim replay, if the runtime emits the post-resume span with `gen_ai.operation.name = "execute_tool"` but *without* the pre-checkpoint span having been closed with a terminal status (`STATUS_CODE_UNSET` left dangling because the worker died mid-close), the backend sees an open span and a new span with the same `tool.call.id`. Tempo's span-append will keep both; the open one never gets an end timestamp and is eventually GC'd, leaving a tool call with a start and no end. The resume must *close the orphan first* (via a span-close sidecar keyed by `tool.call.id`, persisted in the checkpoint) before emitting the continuation. ## Decision Boundary The runtime must branch on a single predicate evaluated at resume, before any span is emitted: ``` resumed_call_id == checkpointed_call_id AND checkpointed_span_closed == True ``` - **True/True** → emit continuation span with the same `tool.call.id`, `gen_ai.operation.name = "execute_tool"`, attach result event. Normal path. - **True/False** → the pre-checkpoint span is orphaned. Do *not* emit a new span with the same id (dedup backends will merge and lose the end timestamp). Instead, emit a `span.close` repair record out-of-band, then emit the continuation as `gen_ai.operation.name = "execute_tool"` with a `gen_ai.tool.call.id` suffixed `#resume` — a *distinct* join key that the backend will not dedup, and that downstream loop-detectors can be configured to ignore. - **False/True or False/False** → re-derivation. The model produced a new call id. This is a *new logical execution*. It must be emitted under a **new `trace_id`** (or a child trace linked via `links`, not parented), with `gen_ai.operation.name = "execute_tool"` and the new id. It must never share the original `trace_id`'s span tree, because the original trace's cost and loop-count invariants were computed against a span set that did not include it. The non-obvious guardrail: the decision must be made from the *checkpoint's* recorded id, not from the model's re-emitted id. The runtime must not trust the model's `tool.call.id` as authoritative on resume — it is a hint, and a probabilistic one. The checkpoint is the only source of truth for "what already happened." ## Operational Effect After enforcing the predicate, three observable changes: 1. **Cost attribution stops double-counting.** Child `gen_ai.usage.*` sums stay ≤ parent, because re-derived executions live in a linked trace, not a child span. The invariant `sum(child.usage) ≤ parent.usage` becomes checkable and is checked. 2. **Loop detectors stop false-positiving on resume.** A detector counting `execute_tool` spans per `invoke_agent` sees one span per logical call, because the `#resume` suffix and the linked-trace split are both visible to the detector's filter. Quarantine rate on resumed agents drops to near zero. 3. **The orphan-span class disappears.** The `span.close` repair record, persisted in the checkpoint, means a killed worker's open span is closed on the next resume rather than GC'd. Tool calls no longer appear with a start and no end. The cost: the runtime must persist span-close state in the checkpoint, and must treat `tool.call.id` as a runtime-minted immutable, not a model output. That is a real change to the checkpoint schema and to the tool-call serialization path — the id must be written into the message block, not a side table. The failure mode this prevents is silent and expensive: a trace that looks clean, passes schema validation, and lies about how many times the agent called a tool. ```python import uuid from dataclasses import dataclass, field from enum import Enum class SpanContinuityError(RuntimeError): """Raised when a resumed agent span cannot prove it continues a recorded execution.""" class OperationName(str, Enum): CHAT = "chat" EXECUTE_TOOL = "execute_tool" INVOKE_AGENT = "invoke_agent" @dataclass(frozen=True) class CheckpointedCall: trace_id: str tool_call_id: str # runtime-minted, immutable, stored in the message block operation_name: OperationName span_closed: bool @dataclass class ResumedSpan: trace_id: str tool_call_id: str operation_name: OperationName links: list[str] = field(default_factory=list) def resolve_resume_span( checkpointed: CheckpointedCall | None, model_emitted_call_id: str, ) -> ResumedSpan: """ Decide how to emit the post-resume span. The model_emitted_call_id is a HINT. The checkpoint is authoritative. """ if checkpointed is None: # No checkpoint: this is a fresh execution, emit normally under a new trace. return ResumedSpan( trace_id=str(uuid.uuid4()), tool_call_id=model_emitted_call_id, operation_name=OperationName.EXECUTE_TOOL, ) same_id = model_emitted_call_id == checkpointed.tool_call_id if same_id and checkpointed.span_closed: # Verbatim replay, pre-checkpoint span properly closed: continue in place. return ResumedSpan( trace_id=checkpointed.trace_id, tool_call_id=checkpointed.tool_call_id, operation_name=OperationName.EXECUTE_TOOL, ) if same_id and not checkpointed.span_closed: # Orphaned pre-checkpoint span. Do NOT reuse the join key verbatim: # dedup backends would merge and lose the end timestamp. return ResumedSpan( trace_id=checkpointed.trace_id, tool_call_id=f"{checkpointed.tool_call_id}#resume", operation_name=OperationName.EXECUTE_TOOL, ) # Re-derivation: model produced a new call id. This is a NEW logical execution. # It must not join the original trace's span tree as a child. if checkpointed.span_closed is False: # We also have an orphan to repair; refuse to silently drop it. raise SpanContinuityError( f"re-derivation under trace {checkpointed.trace_id} with orphaned span " f"{checkpointed.tool_call_id}; repair record required before emitting " f"linked execution {model_emitted_call_id}" ) return ResumedSpan( trace_id=str(uuid.uuid4()), tool_call_id=model_emitted_call_id, operation_name=OperationName.EXECUTE_TOOL, links=[checkpointed.trace_id], ) def emit_span(checkpointed: CheckpointedCall | None, model_emitted_call_id: str) -> dict: span = resolve_resume_span(checkpointed, model_emitted_call_id) if span.operation_name == OperationName.EXECUTE_TOOL and not span.tool_call_id: raise SpanContinuityError("execute_tool span emitted without a tool.call.id") return { "trace_id": span.trace_id, "gen_ai.operation.name": span.operation_name.value, "gen_ai.tool.call.id": span.tool_call_id, "links": span.links, } ```