# Under what conditions must an instance fork span lineage when a tool-call child span outlives its parent turn in OpenTelemetry GenAI traces **Date of Record:** 2026 November 26 **Scope:** Applies to agent runtimes that emit OpenTelemetry spans using the GenAI semantic conventions (`gen_ai.*`) across multi-turn conversations where tool-call spans may complete after the enclosing turn span has ended. Does not apply to single-shot RPC tracing, nor to runtimes that serialize all tool execution synchronously within the parent turn's lifetime. ## Core Distinction The GenAI semantic conventions model a conversation as a nested tree: a `gen_ai.chat` (or `gen_ai.invoke_agent`) span per turn, with `gen_ai.execute_tool` child spans carrying `gen_ai.tool.name` and `gen_ai.tool.call.id`. The convention assumes a *containment* invariant: every tool span's `end_time` precedes its parent turn's `end_time`, so the parent can be closed with complete child accounting and the trace's critical path is well-defined. That invariant is false for asynchronous agents. A turn can emit a tool call, return control to the user or scheduler (closing the turn span), and only later receive the tool result — a long-running shell command, a human-in-the-loop approval, or a downstream A2A handoff. When the tool span is force-parented to the already-closed turn span, the trace is *structurally malformed*: a child span with an `end_time` after its parent's. Exporters, backends, and downstream lineage resolvers disagree about how to handle this, and the disagreement is silent. The distinction that matters: **span parentage is not the same as causal lineage.** The GenAI convention conflates them by making `parent_span_id` carry both. When the two diverge, you must choose which one the trace preserves. ## What Was Observed A multi-turn agent (turn span `T1`, `span_id=0xA`, `end_time=t=100ms`) issued a tool call `gen_ai.execute_tool` (`span_id=0xB`) for a `bash` invocation. The tool ran for 4.2s. At `t=4300ms` the span closed. The runtime set `parent_span_id=0xA` per convention, because the tool call *originated* in turn `T1`. Three downstream effects, all observed: 1. **The OTLP collector accepted the span but the backend dropped it from the trace waterfall.** The trace UI computed the trace root's end time as `max(span.end_time)` over spans whose ancestry reaches the root — but the root turn span `T1` had already been flushed and the trace marked "complete" at `t=100ms`. The late tool span arrived in a separate batch and was indexed as an orphan because its parent `0xA` was no longer in the active trace buffer. Lineage resolution returned `parent_not_found` for `0xB`. 2. **A second turn `T2` (`span_id=0xC`) issued a *retry* of the same tool call** with a new `gen_ai.tool.call.id` but the same `gen_ai.tool.name`. The backend's lineage join on `tool.call.id` saw two disjoint call IDs; the join on `tool.name` saw two calls. Neither join recovered the fact that `T2`'s call was a *retry of* `T1`'s. The causal edge `T1 → retry → T2` was lost because the only carrier of that edge — the shared parent span — had been closed before `T2` opened. 3. **The auditor agent, replaying the trace, reconstructed a turn sequence of `[T1 (no tool), T2 (one tool)]`.** Its verification verdict diverged from the live agent's own state, which knew `[T1 (tool pending), T2 (tool retried, succeeded)]`. The replay discrepancy was not a bug in the auditor; it was faithfully reading a trace that had lost the edge. The failure is not "the tool span was late." The failure is that **the parent-child edge was used to encode an edge that outlived the parent**, and the convention provides no second channel for it. ## Condition of Validity The containment invariant holds — and the naive parentage is correct — if and only if the following three conditions all hold at the moment the tool span is emitted: - **C1 (temporal containment):** `tool_span.end_time <= parent_turn_span.end_time`. If the tool outlives the turn, containment is violated. - **C2 (parent liveness at export):** the parent span is still in the exporter's active buffer when the child is exported, so the backend can resolve `parent_span_id`. If the parent was flushed in an earlier batch, the edge is unresolvable at the backend. - **C3 (lineage uniqueness):** the parent span id uniquely identifies the causal predecessor for the child's purpose. If a tool call is retried across turns, the same logical action has multiple parents and a single `parent_span_id` cannot express it. For asynchronous or retried tool calls, at least one of C1–C3 fails. The convention does not say what to do; the runtime must decide, and the decision must be *explicit* because the default (keep `parent_span_id=0xA`) silently produces an orphaned, lineage-lossy trace. The valid alternative is a **lineage fork**: emit the tool span with `parent_span_id` pointing to a *synthetic turn-continuation span* (a new span of kind `gen_ai.chat` or a dedicated `gen_ai.tool.result` continuation) that opens when the tool result is received, and link the original turn via a span *link* (`SpanLink`) carrying `gen_ai.tool.call.id` and the original turn's `span_id`. This preserves both edges: containment (the fork span is the real-time parent) and causality (the link carries the cross-turn edge). The fork is valid only if the runtime also emits an explicit `gen_ai.tool.call.id` on both the original call and the continuation, so the auditor can join them without relying on span parentage. ## Decision Boundary The runtime must branch on a single predicate evaluated at tool-span emission time: ``` fork_required = (tool_span.end_time > parent_turn_span.end_time) OR (parent_turn_span already flushed) OR (this tool.call.id is a retry of a prior call.id) ``` If `fork_required` is false, emit with `parent_span_id = turn_span_id` (convention-faithful). If true, emit with `parent_span_id = continuation_span_id` and attach a `SpanLink` to the originating turn, plus a `gen_ai.tool.call.id` attribute that is *stable across retries* (the retry must reuse the call id, or the runtime must record a `retry_of` attribute). The boundary is sharp: the moment a tool span's `end_time` can exceed its parent's, parentage-as-lineage is no longer sound, and the runtime must switch to fork-plus-link. There is no partial mode — a trace that mixes forked and unforked tool spans for the same call id is worse than either, because the auditor cannot tell which edge is authoritative. The non-obvious part: **the fork must be decided at emission, not at export.** By export time the parent may already be flushed (C2 violated), and the runtime can no longer attach a link to a span it has not yet emitted. The decision predicate must be evaluated when the tool span is *created*, using the parent's `end_time` if known and a conservative "parent may close" assumption otherwise. ## Operational Effect Forking changes the trace's shape and its consumers: - **Trace waterfall:** the fork span becomes a sibling of the turn span under the trace root, not a child. The waterfall shows `T1`, then a continuation span `T1'` at `t=4300ms` carrying the tool result. The critical path is now correct (the tool's 4.2s is visible), but the visual nesting no longer matches the conversational nesting. Consumers that assume "tool spans are children of turn spans" will mis-render; consumers that follow `SpanLink` recover the causal edge. - **Auditor replay:** the auditor must join on `gen_ai.tool.call.id` and follow links, not parentage, to reconstruct `T1 (call c1) → T1' (result c1) → T2 (retry c1)`. The `retry_of` attribute is the only thing that distinguishes a retry from a fresh call with the same name; without it, the auditor's verdict diverges from the agent's state. - **Cost and token accounting:** `gen_ai.usage.*` attributes on the continuation span must not double-count the turn's tokens. The fork span should carry only the tool's own usage (usually zero for `execute_tool`), and the turn's usage stays on `T1`. If the runtime naively copies usage onto the fork, aggregate token counts inflate by the number of forked tools. - **Backend compatibility:** some OTLP backends reject `SpanLink` to a span in a prior batch or drop links silently. The runtime must verify link resolution in a canary before relying on it; if links are dropped, the fork degrades to an orphan and the lineage is lost anyway. The guardrail is a post-export assertion: for every forked tool span, confirm the link target resolves, or emit a compensating `gen_ai.lineage.fork` event on the trace root. The invariant to enforce: **every `gen_ai.execute_tool` span must have exactly one resolvable causal predecessor, reachable by parentage if contained, or by link if forked, and never both-ambiguous.** A trace that violates this is not merely ugly; it is a trace on which the auditor's replay is guaranteed to diverge from the agent's state, and the divergence is undetectable from the trace alone. ```python from dataclasses import dataclass from typing import Optional class LineageForkError(RuntimeError): """Raised when a tool span's causal predecessor cannot be resolved unambiguously under the GenAI span-containment invariant.""" @dataclass class Span: span_id: str parent_span_id: Optional[str] end_time_ms: Optional[float] # None while open attributes: dict links: list # list of (target_span_id, attributes) def emit_tool_span( tool_span: Span, turn_span: Span, call_id: str, prior_call_ids: set[str], parent_flushed: bool, ) -> Span: """Decide parentage vs. fork for a GenAI execute_tool span. Enforces: exactly one resolvable causal predecessor, by parentage if contained, else by SpanLink to the originating turn. """ if tool_span.end_time_ms is None: raise LineageForkError( f"tool span {tool_span.span_id} emitted without end_time; " "cannot evaluate containment predicate" ) # C1: temporal containment contained = ( turn_span.end_time_ms is not None and tool_span.end_time_ms <= turn_span.end_time_ms ) # C2: parent liveness at export parent_alive = not parent_flushed # C3: lineage uniqueness (retry detection) is_retry = call_id in prior_call_ids fork_required = (not contained) or (not parent_alive) or is_retry if not fork_required: tool_span.parent_span_id = turn_span.span_id tool_span.attributes["gen_ai.tool.call.id"] = call_id return tool_span # Fork: emit a continuation span as the real-time parent, link the # originating turn to preserve the cross-turn causal edge. continuation = Span( span_id=f"{tool_span.span_id}.cont", parent_span_id=None, # root-adjacent; sibling of turn_span end_time_ms=tool_span.end_time_ms, attributes={ "gen_ai.tool.call.id": call_id, "gen_ai.lineage.forked": True, }, links=[(turn_span.span_id, {"gen_ai.tool.call.id": call_id})], ) tool_span.parent_span_id = continuation.span_id tool_span.attributes["gen_ai.tool.call.id"] = call_id if is_retry: tool_span.attributes["gen_ai.tool.retry_of"] = call_id # Post-export guardrail: the link must resolve, or lineage is lost. if not continuation.links: raise LineageForkError( f"forked tool span {tool_span.span_id} has no link to " f"turn {turn_span.span_id}; causal edge unresolvable" ) return tool_span ```