# What structural signals indicate a GenAI tool-call span has silently lost its parent link across an async agent handoff **Date of Record:** 2026 November 25 **Scope:** Applies to agent runtimes that emit OpenTelemetry `gen_ai.*` spans across asynchronous tool-call boundaries (task queues, subprocess tool sandboxes, remote MCP servers) where the child span is created after the parent's context object has been serialized rather than inherited in-process. Does not apply to strictly synchronous in-process tool dispatch, where the OTel context is naturally propagated on the call stack. ## Core Distinction The common assumption is that a tool-call span missing a parent is a *tracing configuration* problem — a missing propagator, an unregistered tracer, a dropped `traceparent` header. Those failures are loud: the span appears as a new trace root and the exporter's `parent_span_id` is empty, which any backend flags as an orphan. The non-obvious failure is different. When an agent hands a tool call to an async worker (a queue consumer, a subprocess, a remote MCP server), the runtime frequently *does* propagate a context — but it propagates the **serialized span context of the moment the task was enqueued**, not the live parent. If the tool call is retried, re-planned, or re-dispatched after the agent's reasoning loop has already moved on to a new turn, the child span's `parent_span_id` still resolves to a *valid, existing* span — the original enqueue-time span. The link is not missing. It is **stale but well-formed**. This distinction matters because every structural integrity check that looks for *absent* parents passes. The reference resolves. The trace tree is connected. What has silently broken is the *semantic* parent: the span the tool call actually executed under is a different `gen_ai` turn than the one the trace claims. The trace is internally consistent and externally wrong. ## What Was Observed A planner agent executes a turn (`gen_ai.operation.name = "chat"`, span `A`), decides to call a tool, and enqueues the tool task with a serialized `SpanContext` captured at enqueue time. The worker that consumes the task opens the tool span (`gen_ai.operation.name = "execute_tool"`, span `B`) as a child of `A`. So far correct. The tool times out at the worker's deadline. The agent's orchestration layer, unaware of the timeout, has already advanced: it produced a new reasoning turn (`A'`), re-planned, and re-enqueued the *same logical tool call* with a fresh serialized context captured from `A'`. But the retry path in the worker reuses a cached task envelope — the one carrying `A`'s context — because the deduplication key was computed on the tool *arguments*, not on the parent span. The retried span `B'` is emitted as a child of `A`, not `A'`. Observed structural signals on the resulting trace: - `B` and `B'` share `trace_id` and both carry `parent_span_id == A.span_id`, but `B'`'s `start_time` is *after* `A`'s `end_time` by several reasoning turns. - The `gen_ai.conversation.id` attribute on `B'` matches `A'`'s conversation, while its `parent_span_id` points into `A`. - `A'` has a `gen_ai.usage.output_tokens` count consistent with a turn that *expected* a tool result, but no child `execute_tool` span descends from it — its only child is a `chat` span for the next turn. - The tool span's `gen_ai.tool.call.id` (a retry-stable identifier) is identical across `B` and `B'`, but the `gen_ai.tool.name` matches while the enclosing turn's `gen_ai.request.model` differs between `A` and `A'` — evidence the two parents are different turns. No orphan detector fires. No `parent_span_id` is empty. The trace is a valid tree. It is simply a tree that asserts a causal relationship (`B'` caused by `A`) that never held. ## Condition of Validity The stale-parent condition is only diagnosable — and only *harmful to reasoning* — under a specific conjunction: 1. The parent context is serialized at enqueue time rather than inherited by reference at execution time. 2. At least one retry or re-dispatch occurs after the originating turn has closed. 3. The retry's deduplication or caching key does not include the parent span identity. 4. The backend reconstructs causality from `parent_span_id` alone rather than cross-checking temporal ordering. If any one of these is absent, the failure does not manifest as a *structural* signal — it either doesn't happen (no retry), is caught (dedup key includes parent), or is invisible (backend ignores parent links). The invariant that must hold is: **a span's `parent_span_id` must reference a span whose `end_time` is not strictly before the child's `start_time` unless the parent is explicitly marked as a long-lived context carrier.** Standard OTel does not enforce this; nothing in the semantic conventions forbids a child starting after its parent ended. ## Decision Boundary The guardrail is not "propagate context correctly" — the context *was* propagated. The guardrail is a **parent-freshness check at span emission**: before a tool-call span is exported, verify that the referenced parent span is either still open or closed within a bounded window of the child's start. If the parent closed more than the orchestration's turn-latency bound ago, the child must be re-parented to the current live turn or emitted as a link (`SpanLink`) rather than a child, with the stale parent recorded as an attribute for audit. The decision boundary is: **do you trust the serialized context, or do you re-resolve the parent at execution time?** Trusting the serialized context is correct for fire-and-forget telemetry and wrong for any trace used to reconstruct agent causality. The correct resolution is to carry the *originating turn identifier* (`gen_ai.conversation.id` plus a turn sequence number) alongside the `SpanContext`, and to re-parent on mismatch rather than emit a well-formed lie. ## Operational Effect Downstream, a trace consumer (an agent auditor, a replay verifier, a debugging human) reading this trace concludes that the retried tool call was caused by turn `A`. It will attribute the tool's second failure to a reasoning path that never issued it. Error-compounding analyses that walk parent→child to compute "which reasoning step introduced this bad tool call" will point at `A`, whose plan was already superseded. The verifier's replay will diverge from the actual execution because the causal graph it replays is wrong — not incomplete, *wrong* — and the divergence will be un-diagnosable from the trace alone, since every span is structurally valid. The operational lesson: for agent traces, **structural validity is not causal validity**. A `parent_span_id` that resolves is not evidence of parentage. The only reliable signal of a stale-parent handoff is the temporal inversion between a closed parent and a later child combined with a conversation/turn identifier that disagrees with the parent's — and that signal exists only if the runtime emits the turn identifier as a first-class attribute rather than relying on span topology to carry it. ```python class StaleParentSpanError(RuntimeError): """Raised when a tool-call span's parent closed before the child started, indicating a serialized-context handoff that outlived its originating turn.""" pass def validate_tool_span_parentage( child: dict, parent: dict, turn_id_attribute: str = "gen_ai.conversation.turn", max_gap_seconds: float = 30.0, ) -> None: """Enforce the parent-freshness invariant before a gen_ai execute_tool span is exported. child/parent are span dicts with keys: span_id, parent_span_id, start_time, end_time (None if open), and attributes (dict). """ if child.get("parent_span_id") != parent.get("span_id"): raise StaleParentSpanError( f"child {child['span_id']} does not reference parent {parent['span_id']}" ) parent_end = parent.get("end_time") if parent_end is None: return # parent still open: fresh by construction gap = child["start_time"] - parent_end if gap <= max_gap_seconds: return # within turn-latency bound: acceptable child_turn = child["attributes"].get(turn_id_attribute) parent_turn = parent["attributes"].get(turn_id_attribute) if child_turn != parent_turn: raise StaleParentSpanError( f"tool span {child['span_id']} started {gap:.1f}s after parent " f"{parent['span_id']} closed, and turn ids disagree " f"(child={child_turn!r}, parent={parent_turn!r}): " f"serialized context outlived its originating turn" ) raise StaleParentSpanError( f"tool span {child['span_id']} started {gap:.1f}s after parent " f"{parent['span_id']} closed despite matching turn id; " f"re-parent to live turn or emit as SpanLink" ) ```