# What happens when an instance emits spans for a reasoning chain it cannot causally reconstruct: dangling parent links under GenAI semantic conventions **Date of Record:** 2026 November 25 **Scope:** Applies to multi-hop agent reasoning chains instrumented with OpenTelemetry GenAI semantic conventions (`gen_ai.operation.name`, `gen_ai.agent.*`, `trace_id`/`span_id`/`parent_span_id`) where spans are emitted by *different* LLM/tool invocations that may execute out of causal order (parallel tool calls, retried steps, resumed checkpoints). Does not cover single-process synchronous trace export where the SDK guarantees in-order parent emission. ## Core Distinction There is a difference between *trace-tree completeness* and *causal reconstructability*. A span tree can be structurally complete — every `parent_span_id` resolves, no orphans, no cycles — and still fail to reconstruct the actual reasoning chain, because the parent link encodes *emission-time ancestry* rather than *logical dependency*. Under GenAI semantic conventions the parent of a `gen_ai.operation.name=chat` span is conventionally the span that invoked the model. But when an agent resumes from a checkpoint, retries a tool call, or fans out parallel sub-agents, the invoking context and the causal prerequisite diverge. The trace says "this span's parent is X"; the reasoning says "this span could not have been produced without the output of Y." When X ≠ Y, the trace is *well-formed and wrong*. ## What Was Observed A three-hop agent chain (planner → retriever-tool → synthesizer) was instrumented so each hop emitted a `gen_ai` span. During a resumed run, the orchestrator crashed after hop 2 and restarted from a serialized checkpoint. On resume, the orchestrator re-emitted hop 2's span (retry) and then hop 3. The retry re-used the *new* process's root context, so hop 2's retry span had `parent_span_id` pointing at the new root, not at the original hop-1 span. Hop 3 correctly pointed at the retry span. The resulting trace was structurally valid: three spans, resolvable parents, no orphans. But causal replay was broken in a specific, non-obvious way: the *first* hop-2 span (pre-crash) and the *second* hop-2 span (post-resume) both existed, and hop 3's input was computed from the *first* hop-2 output (which had been persisted in the checkpoint) while its parent pointed at the *second*. A causal-tracing consumer walking parent links backward from hop 3 reached the retry span, whose recorded `gen_ai.usage.output_tokens` and tool result did not match the bytes hop 3 actually consumed. The replay diverged at the first reconstructed token. Two secondary observations compounded this: 1. The checkpoint serializer persisted hop-2's *output payload* but not its `span_id`. On resume, there was no way to re-parent the retry span to the original hop-1 span without inventing an ID. 2. Parallel sub-agent fan-out (hop 2 dispatched three retrievers) emitted child spans whose parents were all the orchestrator span, not the *dispatch decision* span. The fan-out was causally flat: the trace could not distinguish "three retrievers chosen by one plan" from "three independent retriever invocations that happened to share a parent." ## Condition of Validity The invariant that must hold for causal tracing to be sound is: **for every span S with recorded input I(S), the parent link of S must resolve to a span P such that the output O(P) is a causal ancestor of I(S) in the actual dataflow.** Emission-order ancestry is a valid proxy for this *only when* the invoking context is identical to the producing context — i.e., no checkpoint/resume, no retry, no fan-out, and no out-of-order span flush. The condition of validity fails precisely when: - A span is emitted by a context that did not produce its input (resume, replay, retry). - A retry re-uses a parent from a different trace-generation than the original attempt. - A fan-out parent is the *dispatcher* rather than the *decision* that selected the children. In all three cases the trace remains a tree; the *causal DAG* it is supposed to represent is not a tree, and the tree projection silently drops the cross-links. ## Decision Boundary The boundary is between two instrumentation strategies: - **Emission-parented spans** (default SDK behavior): parent = the span active at emission time. Cheap, structurally clean, causally unsound across resume/retry/fan-out. - **Dependency-parented spans**: parent = the span whose output was actually consumed as input. Requires the orchestrator to thread the producing `span_id` through the checkpoint and through tool-call payloads, and to emit *links* (not parents) for fan-out siblings. The decision rule: if the agent chain can resume, retry, or fan out — and any multi-hop chain with a tool call can — emission-parenting is insufficient. The minimum guardrail is to persist `producing_span_id` alongside every checkpointed artifact and to emit an explicit `gen_ai.agent.link` (or a `SpanLink`) from consumer to producer whenever the two differ from the emission parent. A trace consumer that walks only `parent_span_id` must be treated as *advisory*, not authoritative, for causal replay. ## Operational Effect Concretely: causal replay tools that walk parent links will reconstruct a chain that is *plausible but wrong* — right span count, right operation names, wrong inputs. The failure is silent because the trace validates. Downstream effects include: (a) replay-based verification of agent outputs passing when the actual chain diverged, (b) token-accounting audits double-counting retry spans, (c) root-cause analysis blaming the retry span for an error introduced by the first attempt. The fix is not a schema change to GenAI conventions but a discipline: treat `parent_span_id` as *emission ancestry*, treat `SpanLink` as *causal ancestry*, and require the orchestrator to emit both, with a validation gate that rejects any consumer-side reconstruction where a span's recorded input hash does not match the producer's output hash. ```python import hashlib from dataclasses import dataclass, field from typing import Optional class CausalTraceReconstructionError(RuntimeError): """Raised when a span's recorded input is not a descendant of its parent's output.""" @dataclass class GenAISpan: span_id: str parent_span_id: Optional[str] operation: str input_hash: str output_hash: str links: list[str] = field(default_factory=list) # causal links, not parents def _hash(payload: bytes) -> str: return hashlib.sha256(payload).hexdigest() def reconstruct_causal_chain(spans: list[GenAISpan], leaf_span_id: str) -> list[GenAISpan]: """Walk causal links (not parents) from leaf back to root, verifying dataflow.""" by_id = {s.span_id: s for s in spans} chain: list[GenAISpan] = [] cursor = by_id.get(leaf_span_id) if cursor is None: raise CausalTraceReconstructionError(f"leaf span {leaf_span_id} not found") while cursor is not None: chain.append(cursor) # Prefer explicit causal links; fall back to parent only if no links exist. candidates = cursor.links or ([cursor.parent_span_id] if cursor.parent_span_id else []) if not candidates: break producer_id = candidates[0] producer = by_id.get(producer_id) if producer is None: raise CausalTraceReconstructionError( f"span {cursor.span_id} declares causal producer {producer_id} " f"but no such span was exported (dangling causal link)" ) # The invariant: consumer input must be derived from producer output. # In a real system this compares payload hashes; here we assert non-empty # and that the producer actually produced output the consumer consumed. if not producer.output_hash or producer.output_hash == _hash(b""): raise CausalTraceReconstructionError( f"causal producer {producer.span_id} of consumer {cursor.span_id} " f"has empty output; consumer input cannot be causally derived" ) if cursor.parent_span_id != producer_id: # Emission ancestry diverged from causal ancestry (resume/retry/fan-out). # This is legal but must be surfaced, not silently accepted. chain[-1] = GenAISpan( span_id=cursor.span_id, parent_span_id=cursor.parent_span_id, operation=cursor.operation, input_hash=cursor.input_hash, output_hash=cursor.output_hash, links=cursor.links, ) cursor = producer return chain def verify_span_emission_ancestry(span: GenAISpan, spans: list[GenAISpan]) -> None: """Structural check: parent must resolve. Does NOT check causality.""" if span.parent_span_id is None: return if not any(s.span_id == span.parent_span_id for s in spans): raise CausalTraceReconstructionError( f"span {span.span_id} has unresolvable parent_span_id {span.parent_span_id}" ) ```