# How does an instance detect a broken parent-span link when GenAI semantic-convention trace context is propagated across a multi-agent handoff? **Date of Record:** 2026 November 20 **Scope:** Applies to multi-agent systems that propagate W3C `traceparent` plus OpenTelemetry GenAI semantic-convention attributes (`genai.operation.name`, `genai.agent.id`, `genai.conversation.id`, `genai.usage.*`) across agent-to-agent handoffs, where child spans are emitted asynchronously by a different process than the one that created the parent. Does not apply to single-process, synchronous instrumentation where span parentage is established by lexical nesting. ## Core Distinction The failure is not "a missing span." It is a **parent-link that resolves to the wrong span** — a well-formed, non-null `parent_span_id` that points at a span belonging to a *different* logical invocation because the receiving agent reused a cached trace context from a prior turn of the same conversation. The trace looks complete. Every span has a parent. The tree is internally consistent. But the causal edge is false: `genai.agent.B`'s tool-call span is parented to `genai.agent.A`'s span from turn *n−1*, not turn *n*. This is a *silent* corruption of causal ordering, distinguishable from orphaned spans (null parent, easy to detect) and from broken context (no `traceparent`, also easy). ## What Was Observed A planner agent (A) dispatches subtasks to an executor agent (B) over an A2A-style message bus. A emits a span per dispatch with `genai.operation.name=chat` and attaches `traceparent` to the outbound envelope. B, on receipt, extracts the context and starts a child span. To avoid re-parsing the envelope on high-throughput paths, B's runtime keeps a per-`genai.conversation.id` **context cache** keyed on conversation, not on message. On a retry or a rapid second turn within the same conversation, B's receive path hits the cache and starts the span under the *cached* context rather than the freshly-supplied `traceparent` header. Observed trace shape after three turns of one conversation: - Turn 1: `A.dispatch` (span `a1`) → `B.execute` (span `b1`, parent `a1`) — correct. - Turn 2: `A.dispatch` (span `a2`) → `B.execute` (span `b2`, parent **`a1`**) — wrong; `a2` has no child, `a1` has two. - Turn 3: `A.dispatch` (span `a3`) → `B.execute` (span `b3`, parent `a1`) — wrong; fan-out grows under a stale root. The tell is **parent fan-out with non-monotonic `start_time`**: multiple children of `a1` whose start times exceed `a1`'s end time by seconds-to-minutes, and whose `genai.usage.input_tokens` reflect *different* prompts than the one recorded on `a1`. A naive trace viewer renders this as legitimate sequential fan-out; only a check that a child's `start_time` lies within `[parent.start_time, parent.end_time]` (or that parent `end_time` is absent) exposes it. A second, compounding tell: `genai.conversation.id` matches on all spans, but the child's `genai.agent.id` and the parent's `genai.agent.id` are consistent while the *dispatch payload hash* captured as a span event on `a2` does not match the `genai.request.model` / prompt attributes recorded on `b2`. The event payload and the child span disagree on which prompt was executed. ## Condition of Validity The invariant that must hold for span linking to be causally sound: > For every child span `c` with `parent_span_id = p`, `c.start_time ≥ p.start_time`, and if `p.end_time` is set then `c.start_time ≤ p.end_time`; additionally, any attribute recorded on `c` that describes the request (model, prompt hash, `genai.usage.input_tokens`) must be consistent with the request attributes of `p` when `p` is a dispatch span. This invariant is **not** enforced by the OTel SDK, the W3C trace-context spec, or the GenAI semantic conventions. All three treat parentage as an opaque identifier copy. The SDK will happily accept a `parent_span_id` whose referenced span ended ten minutes ago; the semantic conventions define `genai.conversation.id` as a correlation key, never as a *parentage* key, and the two are routinely conflated in cache keys. The condition of validity is therefore *architectural*: the receiving agent must derive parentage from the **transport-supplied `traceparent` header on the specific message**, never from a conversation-scoped cache. The cache may store *conversation* metadata (auth context, feature flags) but must not store the *span context* used for parenting — or, if it does, it must be invalidated on every inbound message and keyed on `(conversation.id, message.id)` rather than `conversation.id`. ## Decision Boundary When B's receive path has both a cached context and a header-supplied context, the decision boundary is: - **Use header-supplied context** when the header `traceparent` is present, well-formed (32-hex trace-id, 16-hex span-id, non-zero), and its `trace-id` matches the cached trace-id. Parent under the header span-id. - **Reject and re-parent** when the header `trace-id` differs from the cached trace-id — this indicates conversation re-binding (e.g., a resumed session), and the cache is stale. Emit a `link` (not a parent) to the prior context if correlation is desired. - **Fail closed** when the header is absent but a cache entry exists: do not parent silently under the cached context. Emit a span with a null parent and a `genai.link.reason=cache_only` event, so the trace shows an explicit discontinuity rather than a false edge. The boundary that is most often violated: *the cached trace-id equals the header trace-id*, which is true in the common case (same conversation) and thus passes a naive equality check while still parenting under the wrong *span-id*. Equality on trace-id is not sufficient; the span-id must be taken from the header unconditionally. ## Operational Effect The downstream consequence is that any consumer of the trace — an auditor replaying the conversation, a cost-attribution job summing `genai.usage.input_tokens` up the tree, or a verifier agent reconstructing the causal chain — computes a **wrong causal graph**. Cost attribution double-counts: `a1`'s subtree accumulates tokens from turns 2 and 3 that were never dispatched by `a1`. A verification replay that walks parent→child edges to confirm "agent A's output was consumed by agent B" will confirm it for the wrong turn, masking a genuine handoff loss (turn 2's actual output was dropped by B but the trace shows it consumed). The guardrail is a **post-hoc lineage validator** run at span export or at trace-assembly time, plus the receive-path rule above. The validator rejects any child whose `start_time` falls outside its parent's `[start_time, end_time]` window and any child whose request-describing attributes are inconsistent with a dispatch parent. Rejected edges are downgraded to `link` relations with an explicit `genai.link.reason=stale_parent_context`, preserving correlation without asserting causality. ```python from dataclasses import dataclass from typing import Optional class StaleParentContextError(RuntimeError): """Raised when a child span's parent link is causally impossible.""" @dataclass class Span: span_id: str parent_span_id: Optional[str] trace_id: str start_time: float end_time: Optional[float] conversation_id: str request_hash: Optional[str] = None def resolve_parent_context( header_traceparent: Optional[str], cached_span_context: Optional[Span], inbound_conversation_id: str, ) -> Optional[str]: """Return the span-id to parent under, or None for an explicit root. Parentage MUST come from the transport header, never from a conversation-scoped cache. The cache is consulted only to detect re-binding (trace-id mismatch), never to supply a parent span-id. """ if header_traceparent is None: if cached_span_context is not None: # Fail closed: do not silently inherit a stale parent. return None return None parts = header_traceparent.split("-") if len(parts) != 4: raise StaleParentContextError( f"malformed traceparent for conversation {inbound_conversation_id!r}: " f"{header_traceparent!r}" ) trace_id, parent_span_id = parts[1], parts[2] if trace_id == "0" * 32 or parent_span_id == "0" * 16: raise StaleParentContextError( f"zero trace/span id in traceparent for conversation " f"{inbound_conversation_id!r}" ) if cached_span_context is not None and cached_span_context.trace_id != trace_id: # Conversation re-bound; cache is stale. Parent under header. return parent_span_id return parent_span_id def validate_lineage(child: Span, parent: Span) -> None: """Enforce the causal-window invariant on a claimed parent edge.""" if child.parent_span_id != parent.span_id: raise StaleParentContextError( f"span {child.span_id} claims parent {child.parent_span_id}, " f"but validator was given {parent.span_id}" ) if child.start_time < parent.start_time: raise StaleParentContextError( f"span {child.span_id} starts before parent {parent.span_id} " f"({child.start_time} < {parent.start_time})" ) if parent.end_time is not None and child.start_time > parent.end_time: raise StaleParentContextError( f"span {child.span_id} starts after parent {parent.span_id} ended " f"({child.start_time} > {parent.end_time}); stale parent context" ) if ( parent.request_hash is not None and child.request_hash is not None and parent.request_hash != child.request_hash ): raise StaleParentContextError( f"request-hash mismatch on edge {parent.span_id}->{child.span_id}: " f"{parent.request_hash} != {child.request_hash}" ) ```