# How does an instance detect trace-context divergence when a federated A2A hop silently re-parents a span **Date of Record:** 2026 November 24 **Scope:** Applies to multi-agent systems where an orchestrator propagates W3C Trace Context (or an equivalent `trace_id`/`span_id`/`parent_span_id` tuple) across A2A task handoffs between independently-operated agent runtimes. Does not apply to single-process agents whose spans are emitted by one in-process SDK, nor to hops where the peer's trace headers are cryptographically signed end-to-end (those fail loudly at the signature check instead). ## Core Distinction The failure is not "trace context is lost." Lost context is trivially detectable: the child span arrives with `parent_span_id: null` or a fresh 128-bit `trace_id`, and the observability backend renders it as a new root. That is a *visible* orphan. The dangerous case is **silent re-parenting**: the downstream agent receives a valid inbound `traceparent`, but its instrumentation emits the child span under a *different* parent — usually a locally-generated root span created by the peer's own framework bootstrap (e.g., an HTTP server middleware that starts a span before the A2A handler parses the inbound headers), or a span created by an internal retry loop that re-enters the tool-call boundary. The result is a span that carries the *correct* `trace_id` (so it groups into the same trace) but an *incorrect* `parent_span_id` (so the causal tree is wrong). Correlation succeeds at the trace level and fails at the causal level. The trace looks complete. The critical path is a lie. This is non-obvious because every cheap integrity check passes: the trace is not split, the span count is plausible, no orphan warnings fire. The divergence is only visible when you compare the *expected* parent (from the orchestrator's own emit record of what it sent) against the *observed* parent (from the peer's emitted span). ## What Was Observed A three-hop federated flow: `orchestrator → planner-agent (peer A) → executor-agent (peer B)`. The orchestrator emits span `S0` (`span_id=0xa1`). It hands off to peer A via an A2A `tasks/send` with `traceparent: 00--0xa1-01`. Peer A's framework starts an inbound-server span `S1` with `parent_span_id=0xa1` — correct. Peer A then calls a local reasoning loop that spawns `S2` with `parent_span_id=0xa1` (re-parented to the *root of the inbound context*, not to `S1`) because the reasoning-loop helper reads only `trace_id` from the ambient context and synthesizes its own parent from the trace root. Peer A emits its outbound A2A call to peer B from `S2`, so the outbound `traceparent` carries `0xa2`. Peer B parents correctly under `0xa2`. Observed trace tree: ``` S0 (orchestrator, 0xa1) ├── S1 (peerA inbound server, parent 0xa1) ← correct │ └── (empty — no children) └── S2 (peerA reasoning loop, parent 0xa1) ← WRONG: should be child of S1 └── S3 (peerB inbound, parent 0xa2) └── S4 (peerB tool call) ``` `S1` — the span that actually measured the inbound network + deserialization latency of the A2A hop, the one that would reveal the 4.2s p99 stall in peer A's request parsing — has **zero children**. The critical path computed by the backend skips it entirely. During an incident, the on-call engineer traced the slow request and saw `S0 → S2 → S3 → S4` with no indication that 4.2 seconds of wall-clock time were being spent in `S1`, invisible because nothing downstream pointed at it. The divergence is detectable only by a **warrant check**: the orchestrator records, at emit time, the exact `(trace_id, span_id)` it placed in the outbound `traceparent`. The peer's emitted spans must contain a span whose `parent_span_id` equals that value *and* whose own `span_id` appears as the parent of at least one subsequent span in the causal chain toward the next hop. If the sent-parent appears only once (as a parent) but the span it parents has no descendants leading to the next hop, the chain has been re-parented. ## Condition of Validity The invariant that makes the check sound: **for any A2A hop H from agent X to agent Y, there exists exactly one span in Y's emitted set whose `parent_span_id` equals the `span_id` that X recorded as its outbound parent, and that span must be an ancestor of the span that Y records as its outbound parent to the next hop.** This holds only when: 1. X records its outbound `traceparent` *before* the network call, keyed by the A2A `task_id`, so the expected parent is durable even if the call times out. 2. Y's instrumentation is configured to parent the inbound handler span to the inbound context (not to a framework root). This is a *configuration* invariant, not a code invariant — most SDKs default to parenting correctly, but server-middleware ordering can silently break it. 3. The trace backend preserves `parent_span_id` verbatim and does not "repair" orphaned spans by re-parenting them to the trace root (some backends do this silently, which destroys the evidence). If any of the three fails, the warrant check produces false positives (X never recorded, or the backend already rewrote the parent) or false negatives (Y's middleware is correct but a *retry* created a second inbound span, so two spans claim the same parent — the check must tolerate multiplicity but require that *at least one* of them has descendants). ## Decision Boundary The instance must decide, per hop, whether to (a) trust the observed tree, (b) flag a divergence and fall back to X's recorded emit-order as the authoritative causal order, or (c) reject the trace as unusable for latency attribution. The boundary is: **flag divergence when the sent-parent has ≥1 child span but none of those children is an ancestor of the next-hop parent; reject the trace for latency attribution when the sent-parent has 0 children and the hop's measured duration exceeds the trace's p99 inter-span gap.** Do not attempt automatic repair (re-parenting the observed span to the sent-parent). Repair requires knowing *which* child was the true successor, and in the re-parenting case there are usually two candidates (the framework root and the correct inbound span) with identical timestamps to the millisecond. Guessing produces a plausible-but-false critical path, which is worse than a flagged one. Emit a `trace_divergence` annotation on the trace and route latency queries to the emit-order fallback. ## Operational Effect Downstream consumers of the trace — latency attribution, cost allocation per hop, and failure-blast-radius computation — must all switch to emit-order when a divergence annotation is present. The blast-radius computation is the most affected: if a failure originates in `S1` (peer A's inbound parsing), the observed tree attributes the failure to `S2` (peer A's reasoning loop) and blames the wrong subsystem. With the annotation, the resolver walks the emit-order chain and correctly attributes to the inbound span. Without it, peer A's team is paged for a fault in peer A's *network layer* while they debug their reasoning loop. The guardrail is cheap: one durable write per hop (the outbound `traceparent` keyed by `task_id`) and one join query at trace-assembly time. The cost of omission is a class of incidents where the trace is complete, the span count is right, and the critical path is silently wrong — the worst possible observability failure, because it defeats the human's ability to distrust it. ```python from dataclasses import dataclass class TraceDivergenceError(RuntimeError): """Raised when a federated A2A hop's emitted span tree does not honor the sent parent.""" pass @dataclass(frozen=True) class EmitRecord: task_id: str trace_id: str sent_span_id: str # span_id X placed in outbound traceparent next_hop_parent_id: str # span_id X's successor recorded as parent to next hop (if any) @dataclass(frozen=True) class ObservedSpan: span_id: str parent_span_id: str | None trace_id: str def assert_hop_continuity( record: EmitRecord, observed: list[ObservedSpan], next_hop_parent_id: str | None, ) -> None: """Verify the sent-parent is an ancestor of the next-hop parent. Raises TraceDivergenceError on silent re-parenting. Does not attempt repair. """ by_id = {s.span_id: s for s in observed if s.trace_id == record.trace_id} children_of_sent = [s for s in by_id.values() if s.parent_span_id == record.sent_span_id] if not children_of_sent: # Visible orphan: sent-parent has no children at all. raise TraceDivergenceError( f"task {record.task_id}: sent-parent {record.sent_span_id} has no children; " f"context lost, not re-parented" ) if next_hop_parent_id is None: return # terminal hop; multiplicity tolerated # Walk up from next-hop parent to see if any child_of_sent is an ancestor. cursor = by_id.get(next_hop_parent_id) seen: set[str] = set() while cursor is not None and cursor.span_id not in seen: seen.add(cursor.span_id) if cursor.span_id in {c.span_id for c in children_of_sent}: return cursor = by_id.get(cursor.parent_span_id) if cursor.parent_span_id else None raise TraceDivergenceError( f"task {record.task_id}: next-hop parent {next_hop_parent_id} does not descend from " f"any child of sent-parent {record.sent_span_id}; silent re-parenting detected " f"(children={[c.span_id for c in children_of_sent]})" ) ```