# What happens when an instance rewrites traceparent during a federated handoff: correlation collapse at the W3C trace-context seam **Date of Record:** 2026 November 28 **Scope:** Applies to federated multi-agent workflows where an orchestrator propagates W3C `traceparent`/`tracestate` across MCP or A2A handoffs and downstream agents (or their tool-calling runtimes) may regenerate, sanitize, or "repair" trace headers. Does not apply to single-process agents with in-memory spans, nor to systems that use a proprietary correlation ID with no cross-vendor boundary. ## Core Distinction There are two failure classes that look identical in a trace viewer but have opposite root causes. The first is *span loss*: a child span is never emitted, so the trace is merely incomplete—recoverable by inference from parent timing gaps. The second is *trace-ID mutation*: the downstream agent emits a valid, well-formed `traceparent` whose `trace-id` field differs from the upstream one. The trace is not incomplete; it is *forked*. The collector accepts both segments as legitimate, and you get two traces that each look internally coherent and each claim the same causal chain. The distinction matters because trace-ID mutation is silent and self-consistent: every schema validator passes, every span has a parent, and the only evidence of the break is a discontinuity in the `trace-id` byte sequence that no validator checks for. The invariant under attack is: **within a single logical transaction, the 16-byte `trace-id` is immutable across every agent boundary.** The `parent-id` (8 bytes) changes at every hop—that is the whole point of the tree. The `trace-id` does not. When a federated peer violates this, correlation is not degraded; it is severed at a seam that the observability layer cannot see. ## What Was Observed A three-hop federated workflow: orchestrator `A` (vendor-hosted planner) calls agent `B` (self-hosted retrieval agent) over MCP, which calls agent `C` (third-party compliance auditor) over A2A. `A` emits: ``` traceparent: 00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01 ``` `B` receives it, correctly sets its server span's `parent-id` to `00f067aa0ba902b7`, and emits its own span with `parent-id` = its new span ID. So far correct. But `B`'s tool-calling runtime wraps outbound HTTP in a client library that, on missing `tracestate`, *regenerates the entire header from scratch* rather than forwarding the received `traceparent`. The regeneration path constructs a fresh `trace-id` because the library's context object was never seeded from the inbound header—only the inbound `parent-id` was parsed. `C` therefore receives: ``` traceparent: 00-9c1e7d2a4f8b3c6d5e0a1b2c3d4e5f60--01 ``` The collector ingests both. Trace `4bf92f35...` contains `A` and `B` with a clean parent chain and then simply ends. Trace `9c1e7d2a...` contains `C`'s spans with a parent that does not exist in that trace—a dangling `parent-id`, which most backends render as a root span. An operator investigating a slow compliance check sees `C`'s trace as an isolated root, attributes latency to `C`, and never sees that 4.1 seconds of the 5.2-second end-to-end time were spent inside `B`'s retrieval loop. The diagnosis is not just incomplete—it is *wrong in a confident direction*. The non-obvious part: `B` was not buggy in isolation. Its inbound parsing was correct, its server span was correct, and its outbound call succeeded. The failure lived entirely in the *seam* between "parse inbound context" and "construct outbound context," where a library defaulted to fresh-generation instead of propagation. No agent in the chain could observe the break from inside its own execution. ## Condition of Validity The correlation invariant holds only under three simultaneously-true conditions, and federated workflows routinely satisfy two of three: 1. **Propagation is forward-only and non-regenerating.** Every hop must copy the inbound `trace-id` byte-for-byte into the outbound header. Any hop that constructs a header from local state rather than from received state breaks the invariant. This is violated by (a) libraries that treat absent `tracestate` as a signal to reinitialize, (b) agents that "clean" headers by re-emitting a canonical form, and (c) sandboxed tool runtimes that do not inherit the agent's context object. 2. **The `tracestate` vendor list survives the handoff.** W3C permits `tracestate` mutation, but a peer that drops the entire list (common when a vendor sees an unknown key) severs any vendor-specific correlation the list carried—including, in some deployments, the only field linking a span to the agent's session ID. The `trace-id` still matches, so this looks safe; the loss is in the *secondary* correlation channel. 3. **The 16-byte `trace-id` is not truncated or re-encoded in transit.** Some A2A serializers JSON-encode the header and a downstream parser hex-decodes a string that was already hex, producing a 32-byte value that gets silently truncated to 16—a *different* 16 bytes. The header is still syntactically valid (`00-<32 hex>-<16 hex>-01`), so validation passes. The invariant is *invalid* when any peer is a black-box third party whose header handling you cannot audit, because condition 1 becomes unverifiable by construction. ## Decision Boundary The actionable question is not "did we lose a span" but "is the trace we are reading the same transaction we think it is." Draw the boundary here: - **If the `trace-id` at the leaf equals the `trace-id` at the root, and every `parent-id` resolves within the same trace**, the correlation is intact and you may trust causality and timing attribution. - **If a leaf span's `trace-id` differs from the root's but its `parent-id` is non-null and unresolved in that trace**, you have a *fork*: a peer regenerated the header. Treat the leaf trace as causally disconnected and do not attribute its latency to the upstream chain. - **If a leaf's `trace-id` matches but its `parent-id` is unresolved**, you have *span loss* (the intermediate span was never exported). This is recoverable by inference; the fork case is not. The boundary condition that trips operators: a forked trace with a *resolved* `parent-id` is indistinguishable from a legitimate second root in a batched workflow. You cannot tell "peer regenerated the header" from "peer correctly started a new trace for a parallel sub-task" by inspecting the trace alone. The discriminator must live outside the trace—in the handoff record. ## Operational Effect The guardrail that actually bounds this is not a schema validator (they all pass) but a **seam assertion at every outbound handoff**: before an agent emits a request to a federated peer, it compares the `trace-id` it is about to send against the `trace-id` it received, and refuses to emit if they differ unless it has explicitly declared a new-trace boundary. This converts a silent fork into a hard, attributable failure at the agent that caused it. Secondarily, the orchestrator must carry a **handoff ledger**—a per-hop record of `(sender, receiver, trace-id-in, trace-id-out, parent-id-out)`—so that a forked trace can be re-joined offline. The ledger is the only artifact that lets you reconstruct causality across a seam that the trace format itself cannot protect. The cost of not doing this is not lost telemetry; it is *misattributed latency and a confident wrong root cause* in exactly the federated, multi-vendor scenarios where cross-agent diagnosis is hardest and most valuable. ```python import re from dataclasses import dataclass _TRACEPARENT = re.compile( r"^(?P[0-9a-f]{2})-(?P[0-9a-f]{32})-" r"(?P[0-9a-f]{16})-(?P[0-9a-f]{2})$" ) class TraceCorrelationFork(RuntimeError): """Raised when an outbound handoff would mutate the inbound trace-id.""" @dataclass(frozen=True) class TraceContext: version: str trace_id: str parent_id: str flags: str @classmethod def parse(cls, header: str) -> "TraceContext": m = _TRACEPARENT.match(header.strip()) if not m: raise TraceCorrelationFork(f"malformed traceparent: {header!r}") return cls(**m.groupdict()) def assert_forwarded(inbound: TraceContext, outbound_header: str, declares_new_trace: bool = False) -> TraceContext: """Seam assertion: outbound trace-id MUST equal inbound trace-id unless the agent explicitly declares a new-trace boundary.""" outbound = TraceContext.parse(outbound_header) if declares_new_trace: if outbound.trace_id == inbound.trace_id: raise TraceCorrelationFork( "declared new trace but reused inbound trace-id; " "boundary flag is inconsistent" ) return outbound if outbound.trace_id != inbound.trace_id: raise TraceCorrelationFork( "trace-id mutated across handoff: " f"inbound={inbound.trace_id} outbound={outbound.trace_id}; " "peer regenerated header instead of forwarding it" ) if outbound.parent_id == inbound.parent_id: raise TraceCorrelationFork( "parent-id not advanced at hop; child span would shadow parent" ) return outbound def handoff(inbound_header: str, outbound_header: str, declares_new_trace: bool = False) -> TraceContext: inbound = TraceContext.parse(inbound_header) return assert_forwarded(inbound, outbound_header, declares_new_trace) ```