# Why does an instance lose causal parentage when an MCP tool spans cross a trace-context re-injection boundary? **Date of Record:** 2026 November 22 **Scope:** Applies to multi-agent systems that emit OpenTelemetry spans using GenAI semantic conventions (`gen_ai.*`) and propagate W3C `traceparent` across MCP tool calls, A2A handoffs, or subagent spawns. Does not apply to single-process, single-agent instrumentation where no context serialization occurs. ## Core Distinction There are two different things a trace ID can mean, and instrumentation conflates them. The **transport trace context** is the `traceparent` header value carried in a message envelope. The **causal trace identity** is the actual logical lineage of the operation—who asked whom to do what. GenAI semantic conventions (`gen_ai.operation.name`, `gen_ai.agent.id`, `gen_ai.conversation.id`) describe causal identity. W3C `traceparent` describes transport context. These are supposed to be 1:1. They are not, whenever an agent re-injects a fresh context rather than continuing the inbound one. The non-obvious failure: a delegated tool call that *looks* correctly parented in the collector can be causally orphaned. The span has a valid `parent_span_id`, a valid `trace_id`, and passes every schema check—yet the parent it points to is a *sibling* of the real requester, not the requester itself. Instrumentation that validates "is `traceparent` well-formed and present" will report 100% correlation while the causal graph is silently wrong. ## What Was Observed A planner agent (`gen_ai.agent.id: planner-v3`) delegated a retrieval task to a worker agent over A2A, which then invoked an MCP tool `search_index`. Expected lineage: ``` planner.invoke_agent (trace T, span A) └─ worker.handle_task (trace T, span B, parent=A) └─ mcp.search_index (trace T, span C, parent=B) ``` What the collector recorded: ``` planner.invoke_agent (trace T, span A) worker.handle_task (trace T, span B, parent=A) ← correct mcp.search_index (trace T, span C, parent=A) ← WRONG: parent is A, not B ``` Span C's `parent_span_id` pointed at the planner's `invoke_agent` span—skipping the worker entirely. The worker's own span B existed but had no children. The agent graph appeared to show the planner calling the tool *directly*, and the worker as a dead-end leaf. The trigger: the worker's A2A runtime used a framework helper that, on receipt of an inbound message, started a **new** span context from the envelope's `traceparent` (correct trace ID) but then, before invoking the MCP client, called a `with_child_context()` wrapper that re-derived the parent from a *cached* "current span" captured at worker boot—which was the planner's span, because the worker process had been warm-started during the planner's earlier turn. The MCP client honored that stale parent. The worker's own `handle_task` span, created after the cache was populated, never became the active context. So the corruption is a **stale active-context cache colliding with a warm process**. Cold-started workers parented correctly. Warm workers—those reused across turns via a pool—parented to whatever span was active when the worker last idled. Correlation success rate was 100% by header presence and ~62% by causal parentage, and the gap scaled with worker pool reuse, not with load. ## Condition of Validity The invariant that must hold: **the active span context at the moment a tool call is emitted must be the most-recently-entered span on the current logical execution path, not a span captured at process or goroutine initialization.** This holds only when one of the following is true: - Context is threaded explicitly as an argument through every call frame (no ambient "current span" lookup), **or** - The runtime guarantees that entering `handle_task` sets the active context for the duration of that handler and that no pooled/reused execution context survives across handler invocations, **or** - The MCP/A2A client resolves parent from the *inbound envelope's* `traceparent` immediately before emit, not from a cached current-span handle. It fails whenever a warm execution context outlives the handler that created it. The failure is *invisible* to header-format validation and to any check that only asserts `traceparent` presence, because the trace ID is correct—only the span ID is stale. ## Decision Boundary Do not attempt to repair causal parentage by post-hoc trace stitching (matching on `gen_ai.conversation.id` or timestamps). Stitching manufactures edges that may be wrong in the opposite direction when two workers share a conversation. Instead, decide at instrumentation time: - If the client can resolve parent from the inbound envelope at emit time → do that; treat the ambient context as advisory only. - If the ambient context is the only source → **invalidate it at handler entry**: explicitly set the active span to the freshly created `handle_task` span and assert, before emit, that `current_span.parent == handle_task.span_id`. If the assertion fails, either re-parent to `handle_task` or emit a `gen_ai.correlation.orphan=true` attribute and fail the span's causal claim rather than silently mis-parent. - Never trust a pooled worker's cached context across a handler boundary. Reset it on entry. The boundary is: causal correctness is a property you must *assert per emit*, not a property you *inherit from a warm process*. ## Operational Effect Left unguarded, the system produces traces that are schema-valid and causally false. Downstream consumers—cost attribution per agent, latency attribution across handoffs, and any auditor replaying the agent graph—will attribute tool latency and token spend to the wrong agent. In a swarm with auditor agents verifying peer outputs, a mis-parented trace can make a verification span appear to descend from the *requesting* agent rather than the *producing* agent, defeating the very separation of duties the auditor graph is supposed to prove. The fix is a one-line invariant enforced at emit; the cost of missing it is a correlation graph that is confidently wrong at scale. ```python from dataclasses import dataclass class CausalParentageError(RuntimeError): """Raised when an emitted tool span's causal parent is not the active handler span.""" @dataclass(frozen=True) class SpanContext: trace_id: str span_id: str def emit_tool_span( *, handler_span: SpanContext, # the handle_task span for THIS invocation active_context: SpanContext, # ambient/cached "current span" at emit time inbound_traceparent: SpanContext, tool_name: str, ) -> SpanContext: # Invariant: the emitted tool span must descend from the handler span, # and must share the inbound trace. A stale ambient context is a bug, # not a fallback. if handler_span.trace_id != inbound_traceparent.trace_id: raise CausalParentageError( f"handler trace {handler_span.trace_id} != inbound trace " f"{inbound_traceparent.trace_id}; context re-injection lost trace lineage" ) if active_context != handler_span: # Warm-pool stale context detected. Do NOT silently re-parent. raise CausalParentageError( f"emit of {tool_name}: active context span {active_context.span_id} " f"is not handler span {handler_span.span_id} " f"(stale pooled context; causal parent would be wrong)" ) # Parent is unambiguously the handler span. return SpanContext(trace_id=handler_span.trace_id, span_id=handler_span.span_id) ```