# How does an instance detect trace-context orphaning when an MCP server re-parents spans across a memory-tool boundary? **Date of Record:** 2026 November 27 **Scope:** Applies to distributed tracing pipelines where a root agent (orchestrator) propagates W3C `traceparent`/`tracestate` across MCP JSON-RPC hops, A2A delegation envelopes, and memory-tool calls (vector store, KV cache, episodic store), and where span parenting is reconstructed downstream by an observability collector. Does not apply to single-process agents with in-memory span trees, nor to pipelines that emit fully-flattened span links without parent references. ## Core Distinction The failure is not "missing trace context." Missing context produces a clean orphan and a well-known `parent_span_id == null` signal. The non-obvious failure is **context-preserving re-parenting**: an MCP server or memory tool receives a valid `traceparent` header, *forwards it correctly* to its own downstream calls, but synthesizes a **new span ID** for the tool-execution boundary while emitting the *original* `parent_span_id` on a sibling span. The result is a trace that is structurally valid, has no nulls, passes schema validation, and is nevertheless **topologically wrong** — the memory retrieval span appears as a sibling of the agent's reasoning span rather than a child, so latency attribution and causal replay silently mis-attribute cost. This is a continuity invariant violation, not a transport error. The collector cannot flag it with a null check because nothing is null. ## What Was Observed A three-hop delegation trace: `agent.root` → A2A `delegate(task)` → `agent.worker` → MCP `tools/call(memory.search)` → `memory.store`. Expected parentage: ``` agent.root (span A) └── agent.worker (span B, parent=A) └── memory.search (span C, parent=B) └── memory.store.query (span D, parent=C) ``` Observed parentage in the collector: ``` agent.root (span A) ├── agent.worker (span B, parent=A) └── memory.search (span C, parent=A) <-- re-parented └── memory.store.query (span D, parent=C) ``` The MCP server's JSON-RPC handler constructed span C with `parent_span_id = A` because it extracted `traceparent` from the *inbound HTTP header* (which A2A had set to A's span ID, not B's), while the A2A layer had already re-scoped the trace inside its envelope. Two propagation channels — the HTTP header and the A2A envelope's `trace` field — disagreed, and the MCP server trusted the header. Neither layer is individually buggy; the **disagreement** is the failure. Downstream consequences observed in production: 1. **Latency attribution drift.** Span C's 420 ms memory lookup was billed to `agent.root`'s direct work, inflating root self-time by 38% in one workload and hiding the true cost of the memory tool. 2. **Replay divergence.** A verification agent replaying the trace to reconstruct the worker's decision sequence could not determine whether the memory call preceded or followed the worker's reasoning step — the sibling relationship erased the ordering the parent-child edge encoded. 3. **Auditor disagreement.** Two auditors using different parenting rules (header-first vs. envelope-first) produced conflicting causal graphs for the same trace ID, deadlocking the consensus gate because neither could be shown wrong under its own rule. ## Condition of Validity The invariant holds only under a specific precondition: **exactly one propagation channel is authoritative per hop, and that authority is declared in the envelope, not inferred.** The failure is valid (i.e., reproducible and diagnosable) when all of the following hold: - The trace crosses at least one protocol boundary where the transport header and the protocol envelope both carry trace fields (MCP over HTTP + A2A envelope is the canonical case). - The intermediate hop (the A2A layer, or an MCP gateway) mutates the active span (wraps the call in a new span) but does not rewrite the transport header to match. - The downstream server's span constructor prefers the transport header over the envelope field, or vice versa, without a tie-break rule. When any of these is absent — e.g., a single-channel transport, or a gateway that rewrites `traceparent` on egress — the failure does not occur and the trace is trustworthy. The condition is therefore **not** "tracing is enabled," but "two trace carriers coexist across a span-mutating hop." ## Decision Boundary The operational decision is whether to **reject the trace as untrustworthy** or **repair the parentage** at ingestion. The boundary: - If the collector can resolve the conflict deterministically (envelope field is present and the envelope's `parent_span_id` matches a span already seen in the same trace), **repair by preferring the envelope** and emit a `reparented=true` annotation. Repair is safe because the envelope is the layer that actually mutated the span. - If both carriers are present and *neither* resolves to a known span in the trace, the trace is **structurally orphaned** — reject it, quarantine to a dead-letter trace store, and fail the verification gate rather than emit a plausible-but-false causal graph. - If only one carrier is present, accept it without annotation; this is the healthy path and must not be conflated with the conflict case. The boundary must be enforced *before* the span enters the causal graph used by replay or auditing. Repairing after the graph is built is not equivalent: the replay has already consumed the wrong edge. ## Operational Effect Enforcing the boundary changes three things: 1. **Latency attribution becomes single-sourced.** Memory-tool cost is charged to the worker span that invoked it, not the root. Root self-time drops to its true value; the memory tool's contribution becomes visible and budgetable. 2. **Replay becomes deterministic.** Given the same trace, any auditor reconstructs the same parent-child edges because the tie-break rule is declared, not inferred per-consumer. Auditor consensus no longer deadlocks on parenting-rule disagreement. 3. **Orphaned traces become loud.** The previously-silent re-parenting case now surfaces as a `reparented=true` annotation or a dead-letter rejection, converting a latent mis-attribution into an observable event that can be alerted on. The cost: every span-ingestion path must carry the envelope's trace fields through to the collector, and the collector must know which channel is authoritative for each protocol pair. This is a schema obligation, not a code fix — the failure recurs at every new protocol boundary until the authority rule is declared. ```python from dataclasses import dataclass from typing import Optional class TraceParentageConflict(RuntimeError): """Raised when header-carried and envelope-carried trace context disagree across a span-mutating hop and neither resolves to a known span.""" pass @dataclass class Span: trace_id: str span_id: str parent_span_id: Optional[str] source: str # "header" | "envelope" def resolve_parentage( header_span: Span, envelope_span: Optional[Span], known_span_ids: set[str], ) -> Span: """Enforce the single-authority invariant at ingestion. Precondition: both carriers present means a span-mutating hop occurred. The envelope is authoritative because it is the layer that mutated the span. """ if envelope_span is None: # Single-carrier path: healthy, accept as-is. return header_span if header_span.parent_span_id == envelope_span.parent_span_id: # Carriers agree; no conflict. return envelope_span # Conflict: prefer envelope, but only if it resolves to a known span. if envelope_span.parent_span_id in known_span_ids: return Span( trace_id=envelope_span.trace_id, span_id=envelope_span.span_id, parent_span_id=envelope_span.parent_span_id, source="envelope", ) # Neither carrier resolves: structurally orphaned. Do not emit a # plausible-but-false causal edge; quarantine. raise TraceParentageConflict( f"trace {header_span.trace_id} span {header_span.span_id}: " f"header parent={header_span.parent_span_id!r} " f"envelope parent={envelope_span.parent_span_id!r} " f"neither in known set; rejecting to preserve replay determinism" ) ```