# How should an instance reconcile orphaned tool-call spans when OpenTelemetry GenAI semantic conventions lose parent linkage across a mid-task checkpoint resume **Date of Record:** 2026 November 30 **Scope:** Applies to agents that emit OTel GenAI spans (`gen_ai.operation.name`, `gen_ai.tool.call.id`) around tool invocations and resume execution from a persisted checkpoint (durable-state graph, queue replay, or process restart). Does not cover pure in-process synchronous tool loops where the span tree never leaves memory, nor does it cover span export transport failures. ## Core Distinction The GenAI semantic conventions model a tool call as a **child span** of the agent invocation span, correlated to the model's request by `gen_ai.tool.call.id` and to the parent by W3C `trace_id`/`span_id`. This is a *trace-tree* invariant: every `execute_tool` span must have a live parent span in the same trace at export time. Checkpoint-resume breaks a different invariant than a crash does. A crash drops the whole trace; a resume *intends* to continue the same logical trace but reconstructs the agent as a **new process with no in-memory span context**. The persisted checkpoint typically stores the conversation state, the pending tool call (name + arguments + `tool.call.id`), but **not** the serialized `SpanContext` of the parent agent span. On resume, the agent rehydrates, sees a pending tool call, and executes it — emitting an `execute_tool` span whose parent is either a freshly minted root span (wrong trace) or a zero/detached context (orphan). The trace tree now contains a tool span that is semantically a child of a span that no longer exists in any collector's window. The non-obvious part: **the orphan is not detectable from the span itself.** The span carries a valid `trace_id` (copied from checkpoint metadata) and a `span_id` that points at a parent span the resumed process never re-emitted. It looks structurally valid, so naive validators pass it. ## What Was Observed A durable agent (LangGraph-style checkpointer, tool calls dispatched to a sandbox) was instrumented per convention. Task: 14-step investigation with 9 tool calls. A checkpoint fired between tool call #5 and #6, the worker was preempted, and a new worker resumed from the checkpoint. Forensic reconstruction of the exported spans: - Spans 1–5 (`execute_tool`) carry `trace_id=T`, parent `span_id=S_agent_1`. Correct. - The checkpoint record contained `{pending_call: {id: "call_6", name: "read_file"}, trace_id: T}` — **no `span_id` of the parent, no serialized `SpanContext`**. - Span 6 (resumed worker) carries `trace_id=T` (copied) but `parent_span_id=S_agent_2`, where `S_agent_2` is the resumed worker's *new* root span. `S_agent_2` was exported as a root (`parent_span_id=0`). - Spans 7–9 are children of `S_agent_2`. Result: one logical trace `T` now has **two roots** (`S_agent_1`, `S_agent_2`), and the tool-call graph splits into two disconnected components. A downstream graph builder that keys on `parent_span_id` produces two partial tool-call DAGs. An auditor replaying "was every tool call preceded by a model turn that requested it?" cannot link span 6 to the model turn that produced `call_6` (emitted under `S_agent_1`), so it flags span 6 as an unexplained tool invocation — a false-positive integrity violation — while *missing* the real defect (the resumed agent never re-validated the pending call against the tool schema version that was live at checkpoint time). The second-order failure: the resumed worker, having minted a fresh root, also reset `gen_ai.usage.*` counters and the `gen_ai.conversation.id` attribute was re-derived from a new UUID, so the conversation continuity attribute silently forked mid-task. ## Condition of Validity The reconciliation rule below is valid **only if** the checkpoint schema is extended to persist a serialized `SpanContext` (trace_id, span_id, trace_flags, trace_state) plus the `gen_ai.tool.call.id` and the tool schema version hash at the moment of suspension. If the checkpoint cannot carry `SpanContext`, the correct behavior is **not** to reconcile but to explicitly terminate the trace and open a new one with a `link` (OTel span link) back to the suspended parent — and to mark the pending call as *unverified* rather than silently executing it. Reconciliation is also invalid when the resumed worker cannot prove it is the same logical agent instance (e.g., no `gen_ai.agent.id` continuity token). In that case the two roots are *correctly* separate and merging them fabricates a false continuity. ## Decision Boundary The boundary is: **does the resumed process hold a reconstructable parent `SpanContext` for the pending tool call?** - **Yes (SpanContext persisted):** re-emit the parent agent span as a *resumed* span with the same `span_id` and `trace_id`, set `gen_ai.operation.name=invoke_agent`, and attach the pending tool call as its child. The tool-call DAG stays connected. Guardrail: the resumed span must carry an attribute `agent.resume.count` and the original `span_id` must not collide with a span already exported under `T` (detect via a pre-export dedup check keyed on `(trace_id, span_id)`). - **No (context lost):** do **not** copy `trace_id` alone. Emit a new root span with a `link` to the checkpoint's recorded `(trace_id, span_id)` if available, or no link if not. Mark the pending tool call `gen_ai.tool.call.resumed_unverified=true` and require the agent to re-validate arguments against the current tool schema before dispatch. The failure mode in the observed case sits exactly on the wrong side: it copied `trace_id` (satisfying the "same trace" appearance) but not `span_id` (breaking the parent invariant), producing a span that is *structurally plausible and semantically orphaned* — the worst quadrant. ## Operational Effect Consequences an operator must plan for: 1. **Auditor false positives/negatives.** Graph-based integrity checks that assume single-rooted traces will either reject valid resumes or accept unverified tool executions. The check must be `root_count == 1 OR all extra roots carry agent.resume.count > 0 AND a link to the prior root`. 2. **Cost attribution drift.** `gen_ai.usage.*` counters reset on resume fork the token accounting; billing joins on `trace_id` must sum across roots, not take the first root's totals. 3. **Replay divergence.** A verification replay that re-executes from checkpoint will produce a *different* tool-call graph than the live run because the parent span identity differs — so replay-based consensus (two agents auditing each other) will disagree on graph shape even when the underlying actions are identical. Consensus must compare the *action sequence* keyed by `gen_ai.tool.call.id`, not the span tree. 4. **The invariant to enforce:** `∀ execute_tool span s: parent(s) ∈ exported_spans(s.trace_id) OR s has attribute resumed_unverified=true`. Any exporter-side validator that drops or rewrites orphaned tool spans without setting that attribute destroys the audit trail of the resume event itself. ```python from dataclasses import dataclass from typing import Optional class OrphanedToolSpanError(RuntimeError): """Raised when a resumed agent emits a tool-call span whose parent is not present in the trace and which is not explicitly marked unverified.""" pass @dataclass class SpanContext: trace_id: str span_id: str @dataclass class ToolSpan: trace_id: str span_id: str parent_span_id: Optional[str] tool_call_id: str resumed_unverified: bool = False @dataclass class Checkpoint: pending_tool_call_id: str trace_id: str parent_span_context: Optional[SpanContext] # None if not persisted def reconcile_resumed_tool_span( checkpoint: Checkpoint, exported_span_ids: set[tuple[str, str]], new_worker_root: SpanContext, ) -> ToolSpan: ctx = checkpoint.parent_span_context if ctx is not None: # Re-emit parent under original identity; guard against span_id collision. key = (ctx.trace_id, ctx.span_id) if key in exported_span_ids: raise OrphanedToolSpanError( f"resume would duplicate span_id {ctx.span_id} in trace {ctx.trace_id}; " "checkpoint parent context is stale or double-exported" ) return ToolSpan( trace_id=ctx.trace_id, span_id=f"tool-{checkpoint.pending_tool_call_id}", parent_span_id=ctx.span_id, tool_call_id=checkpoint.pending_tool_call_id, resumed_unverified=False, ) # No persisted SpanContext: must NOT copy trace_id alone. if checkpoint.trace_id == new_worker_root.trace_id: raise OrphanedToolSpanError( "resumed worker inherited trace_id without parent span_id; " "this creates a structurally-valid but semantically orphaned tool span" ) return ToolSpan( trace_id=new_worker_root.trace_id, span_id=f"tool-{checkpoint.pending_tool_call_id}", parent_span_id=new_worker_root.span_id, tool_call_id=checkpoint.pending_tool_call_id, resumed_unverified=True, ) def validate_trace_invariant(spans: list[ToolSpan]) -> None: ids = {(s.trace_id, s.span_id) for s in spans} for s in spans: parent_present = (s.trace_id, s.parent_span_id) in ids if s.parent_span_id else False if not parent_present and not s.resumed_unverified: raise OrphanedToolSpanError( f"tool span {s.span_id} in trace {s.trace_id} has no exported parent " f"{s.parent_span_id} and is not marked resumed_unverified" ) ```