Why does an instance lose trace-parentage when a federated agent hop rewrites its span context mid-execution?
**Date of Record:** 2026 December 2 **Scope:** Applies to any multi-agent system where an orchestrator propagates a distributed trace context (W3C `traceparent`/`tracestate`, OpenTelemetry `SpanContext`, or a bespoke correlation envelope) across a hop into a runtime that does not share the orchestrator's instrumentation library or SDK version. Does not apply to single-runtime, single-SDK deployments where every span is emitted by the same tracer provider.
Core Distinction
There are two things people conflate under "trace correlation": **identifier propagation** and **span-parentage preservation**. Identifier propagation is trivial — copy the `trace_id` string into the outbound call. Span-parentage preservation is the hard invariant: the child span emitted by the downstream agent must record the *upstream span's* `span_id` as its `parent_span_id`, so the trace reconstructs as a tree rather than a bag of orphans sharing a `trace_id`.
The failure I am documenting is not "the trace_id got dropped." It is the subtler case where the `trace_id` survives perfectly, every log line is greppable by correlation ID, and yet the assembled trace is a **forest of disconnected root spans** because each federated hop silently re-rooted its span. Correlation looks healthy at the log layer and is structurally broken at the trace layer. This is the non-obvious boundary: **a trace can be 100% correlated by ID and 0% correlated by parentage.**
What Was Observed
An orchestrator (runtime A, OpenTelemetry Python SDK 1.24) dispatched a task to a tool-agent (runtime B, a Node.js service on `@opentelemetry/sdk-node` 0.50) which in turn called a verifier-agent (runtime C, a Rust service using a hand-rolled span emitter that only understood the raw W3C header). The orchestrator injected `traceparent` correctly:
traceparent: 00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01
Runtime B parsed the header, extracted the `trace_id` (`4bf9...4736`), and — because its SDK defaulted to `ParentBased(AlwaysOn)` but its HTTP instrumentation had been configured with `propagators: []` (an empty propagator list, a common "reduce overhead" tweak) — it **started a new root span** with the correct `trace_id` but a freshly generated `span_id` and a null parent. The `traceparent` it emitted downstream to C therefore carried B's new span as the parent, and the link back to A's `00f067aa0ba902b7` span was gone.
Result: the trace backend showed three spans under one `trace_id`, all three flagged as **roots** (no parent). The trace waterfall rendered as three parallel bars at t=0 rather than a nested call chain. Latency attribution — the entire reason the trace existed — was impossible: you could not tell that C's 4.2s verifier call was 90% of the end-to-end latency because there was no edge connecting it to B or A.
The tell was in the `tracestate` header, which B had also dropped. A downstream auditor comparing `tracestate` lengths across hops would have caught it; nobody was.
Condition of Validity
The parentage invariant holds **only if every hop's propagator is a superset of the upstream propagator's injector and the downstream extractor is never fed a context it re-roots.** Concretely, the guardrail is:
- The outbound `traceparent` on hop N must contain `span_id` = the **currently active** span's ID on hop N-1, not a regenerated one.
- Extraction on hop N must produce a `SpanContext` with `is_remote=True` and that upstream `span_id` as parent, so the first span created is a child, not a root.
- `tracestate` must be forwarded verbatim (or explicitly merged), because vendor-specific sampling decisions ride in it; dropping it re-randomizes sampling downstream and can silently disable tail-based sampling for the whole subtree.
This condition is **invalidated** whenever: the propagator list is empty or misconfigured; the runtime auto-creates a span before extraction (e.g., a framework middleware that opens a request span at ingress *before* the OTel extractor runs); the SDK version changed the default `ParentBased` sampler behavior; or a hop crosses a boundary where the context is serialized into a message queue and the consumer's `link` vs `parent` semantics were chosen wrong.
Decision Boundary
The decision an operator must make at each hop: **is this hop a continuation of the same logical operation (parent-child edge) or a causally-linked but asynchronous operation (span link)?**
- Synchronous RPC / tool call → **parent-child**. The downstream span's parent must be the upstream span.
- Message-queue publish/consume, or a fire-and-forget agent dispatch where the consumer may run minutes later or multiple times → **span link**, not parent. Forcing a parent here creates a false causal chain and inflates the "critical path" with queue wait time that is not actually blocking.
- Federated hop into an untrusted or heterogeneous runtime where you cannot guarantee propagator fidelity → emit a **link** and additionally stamp a `correlation.envelope` attribute carrying the upstream `trace_id`+`span_id` as data, so the edge is recoverable post-hoc even if the transport header is mangled.
The boundary is crossed wrongly in both directions: teams either force parent-child across async hops (corrupting latency math) or drop to links everywhere (losing the tree). The rule is *synchronous ⇒ parent, asynchronous ⇒ link, unverifiable ⇒ link + envelope attribute*.
Operational Effect
When parentage is lost, three things degrade silently and one degrades loudly:
- **Silently:** critical-path latency attribution (no edges → no critical path), error-cascade tracing (you cannot walk from a failed verifier span up to the orchestrator span that should have caught it), and sampling coherence (each re-rooted span is sampled independently, so a 1% head-sample can keep a child and drop its parent, yielding orphan spans in the backend).
- **Loudly:** only if you have a *trace-integrity assertion* — a check that every span in a trace has exactly one root and that `parent_span_id` is non-null for all non-root spans. Most backends do not assert this; they happily render forests.
The architectural guardrail is a **post-hoc parentage audit** run as a batch job or on-ingest check: for each `trace_id`, count roots; if `> 1` and the spans are causally related (overlapping time windows, matching correlation attributes), raise a trace-integrity violation and quarantine the trace. This converts a silent structural corruption into a loud, alertable event — and it is the only reliable detector, because the corruption is invisible at the log layer where everyone actually looks.
from dataclasses import dataclass, field
from collections import defaultdict
from typing import Optional
class TraceParentageViolation(RuntimeError):
"""Raised when a federated hop re-roots a span, breaking the parent-child invariant."""
pass
@dataclass
class Span:
trace_id: str
span_id: str
parent_span_id: Optional[str]
runtime: str
start_ns: int
end_ns: int
attributes: dict = field(default_factory=dict)
def audit_trace_integrity(spans: list[Span], *, allow_async_roots: bool = True) -> dict:
"""
Enforce: every trace_id has exactly one root UNLESS roots are marked async
(span link semantics). Non-root spans must have a parent present in the trace.
"""
by_trace: dict[str, list[Span]] = defaultdict(list)
for s in spans:
by_trace[s.trace_id].append(s)
report = {}
for trace_id, group in by_trace.items():
ids = {s.span_id for s in group}
roots = [s for s in group if s.parent_span_id is None]
orphans = [
s for s in group
if s.parent_span_id is not None and s.parent_span_id not in ids
]
async_roots = [r for r in roots if r.attributes.get("link.semantics") == "async"]
sync_roots = [r for r in roots if r not in async_roots]
report[trace_id] = {
"span_count": len(group),
"root_count": len(roots),
"sync_roots": len(sync_roots),
"async_roots": len(async_roots),
"orphans": [o.span_id for o in orphans],
"runtimes": sorted({s.runtime for s in group}),
}
if orphans:
raise TraceParentageViolation(
f"trace {trace_id}: {len(orphans)} span(s) reference a parent "
f"absent from the trace (dropped or re-rooted upstream): "
f"{[o.span_id for o in orphans]}"
)
if len(sync_roots) > 1:
raise TraceParentageViolation(
f"trace {trace_id}: {len(sync_roots)} synchronous roots detected "
f"across runtimes {report[trace_id]['runtimes']}; a federated hop "
f"re-rooted its span (propagator dropped or extraction bypassed). "
f"Roots: {[r.span_id for r in sync_roots]}"
)
if len(roots) > 1 and not allow_async_roots:
raise TraceParentageViolation(
f"trace {trace_id}: multiple roots but async links disallowed by policy"
)
return report
def propagate_hop(upstream: Span, downstream_runtime: str, *, synchronous: bool,
start_ns: int, end_ns: int, new_span_id: str) -> Span:
"""
Build the downstream span. Synchronous hops MUST carry upstream.span_id as parent.
Async hops emit a root but tag link semantics so the auditor tolerates it.
"""
if synchronous:
if not upstream.span_id:
raise TraceParentageViolation(
f"cannot propagate sync hop to {downstream_runtime}: "
f"upstream span has no span_id (extraction failed upstream)"
)
return Span(
trace_id=upstream.trace_id,
span_id=new_span_id,
parent_span_id=upstream.span_id,
runtime=downstream_runtime,
start_ns=start_ns,
end_ns=end_ns,
)
# async: root, but explicitly linked back via attribute envelope
return Span(
trace_id=upstream.trace_id,
span_id=new_span_id,
parent_span_id=None,
runtime=downstream_runtime,
start_ns=start_ns,
end_ns=end_ns,
attributes={"link.semantics": "async",
"link.upstream_span_id": upstream.span_id},
)