# How does an instance detect a broken span-link parent when a nested agent-to-agent handoff rewrites its trace context mid-execution **Date of Record:** 2026 November 20 **Scope:** Applies to agents whose execution spans are stitched across A2A (agent-to-agent) handoffs using propagated trace context (W3C `traceparent`/`tracestate` or equivalent), where a delegated sub-agent opens its own span and re-emits a context to the caller. Does not apply to single-process agents that own their entire span tree, nor to fire-and-forget delegation with no return context. ## Core Distinction The failure is not "lost trace context." Lost context is loud: `traceparent` is absent, the child span orphans, the collector flags a dangling parent. The non-obvious failure is a **silently rewritten parent link**: the nested sub-agent receives a valid context, but during its own internal re-delegation it constructs a *new* span with a *fresh* `trace_id` derived from its local generation loop rather than the inherited one, then returns a well-formed context that parses cleanly. Every field is syntactically valid. The `trace_id` is 32 hex chars. The `span_id` is 16 hex chars. The flags byte is `01`. The caller accepts it, emits its closing span against it, and the trace *looks* complete—except the returned span is now a root of a second, disjoint trace, and the causal chain from the top-level request to the terminal tool call is severed at exactly the handoff boundary where a human auditor would look for the agent's reasoning. The distinction that matters operationally: **a parseable span context is not a continuous span context.** Validity is a schema property; continuity is a lineage property. An instance that only validates the former will happily accept a forged-continuous link. ## What Was Observed A three-hop pipeline: orchestrator `O` → planner `P` → executor `E`. `O` opens span `s_O` under `trace_id = T`. It hands `P` a context `(T, s_O, flags=01)`. `P` opens `s_P` as a child of `s_O`—correct. `P` then delegates a subtask to `E` and, critically, `P`'s delegation wrapper *re-initializes the tracing SDK* for the sub-call (a common pattern when `E` runs in a separate sandbox whose instrumentation is bootstrapped lazily). That re-init generates a *new* root span `s_E_root` under a *new* `trace_id = T'`, because the SDK's default sampler sees "no active parent in this process" and starts a fresh trace. `E` does its work, returns `(T', s_E_root, flags=01)`. `P` receives this and does the natural thing: it closes `s_P` and forwards the returned context upward as "the child's context." `O` closes `s_O`, links it to `s_P` (correct) but the *forwarded* context now points at `s_E_root ∈ T'`. The backend assembles two traces: `T` containing `O→P` and `T'` containing `E` alone. The `E` span's parent is `s_E_root`'s parent, which is null. The causal edge `s_P → s_E` **does not exist in the data**, even though every agent in the pipeline believed it propagated a parent. Observed symptom in the field: trace UIs show the orchestrator and planner as a tidy 2-span trace with sub-200ms latency, and a *separate* trace for the executor with the real 40-second tool latency. On-call engineers debugging "why is the pipeline slow" look at the fast trace, see no bottleneck, and miss the slow trace entirely because it has a different `trace_id` and no shared correlation key. The failure is invisible until someone joins on a custom `run_id` attribute that nobody guaranteed was propagated. The second-order observation: because `P` forwarded `T'` upward, `O`'s own closing span was *correctly* linked to `s_P` but `O` now believes the terminal context is `T'`. Any downstream auditor that trusts `O`'s returned context as "the authoritative terminal span" will query `T'` and find a root span with no ancestors—a *plausible-looking* complete trace that is actually a fragment. The auditor's verification replay passes, because the fragment is internally consistent. ## Condition of Validity The invariant that must hold for span linking to be trustworthy across nested handoffs: > **For every handoff edge `(caller_span c, callee_span e)`, the callee's emitted `trace_id` MUST equal the caller's `trace_id`, and the callee's emitted `parent_span_id` MUST equal `c.span_id`. A returned context whose `trace_id` differs from the caller's is a trace break, not a new trace, and MUST NOT be forwarded upward as a child link.** This is a *lineage* invariant, not a schema invariant. It cannot be checked by validating the context in isolation—the callee has no way to know it broke the link, because from its local perspective a fresh trace is a perfectly valid trace. **Only the caller, holding the pre-handoff context, can detect the break**, and only if it compares the returned `trace_id` against the one it sent. The detection obligation is therefore always on the *caller side of the handoff*, and it must be enforced at the moment of return, before the context is forwarded or the caller's span is closed. Two corollaries: - A callee that *legitimately* starts a new trace (e.g., an async fan-out with its own sampling decision) must signal this explicitly—via a `tracestate` marker or a distinct return field—so the caller can distinguish "intentional new trace" from "accidental re-root." Absent that signal, any `trace_id` mismatch is a violation. - The check must run on the *returned* context, not the context the caller *sent*. Checking the sent context is a no-op; the break happens inside the callee. ## Decision Boundary The caller must decide, on receiving a context from a nested handoff, between three states: 1. **Continuous** — `returned.trace_id == sent.trace_id` and `returned.parent_span_id == caller_span_id`. Link normally, close span, forward. 2. **Explicitly re-rooted** — `returned.trace_id != sent.trace_id` *and* `returned.tracestate` carries the agreed re-root marker (e.g., `a2a=reroot`). Accept as a linked-but-separate trace; record the cross-trace edge as an attribute (`link.trace_id`) so the backend can join; do not claim parent-child. 3. **Broken** — `returned.trace_id != sent.trace_id` *and no re-root marker*. This is the failure. The caller must **not** close its span against the returned context, must **not** forward it upward as authoritative, and must surface a lineage fault. The boundary is sharp: the presence or absence of the re-root marker is the only thing separating a legitimate separate trace from a corrupted causal chain. An instance that treats "parseable" as "continuous" collapses states 2 and 3 into "fine" and loses the ability to detect the break at all. The guardrail is a **return-context assertion at every handoff boundary**, executed by the caller before any span close or context forward. It is cheap (string compare on `trace_id`, one attribute lookup on `tracestate`) and it must be non-optional—there is no configuration under which skipping it is safe, because the callee cannot self-detect the break. ## Operational Effect When the assertion is absent, the operational effects compound: - **Audit blind spots.** The terminal tool call lives in a trace the on-call never opens. Latency attribution silently misattributes the slow span to "uninstrumented." - **False-consistent verification replay.** An auditor that validates each trace in isolation finds every trace internally consistent. The break is *between* traces, so per-trace validation cannot see it. Only a lineage check across the handoff edge catches it. - **Upward propagation of the wrong authority.** Because the caller forwards the returned context as "the child's context," the *next* layer up inherits the wrong `trace_id` and the break migrates one hop closer to the root, making it progressively harder to localize. - **Silent under nested depth.** With three hops the break is at `P→E`; with five hops and two re-inits, two disjoint traces form and the intermediate hops appear as isolated roots. Depth amplifies the number of breaks without amplifying any single signal. When the assertion is present, the failure converts from silent to loud at the exact handoff where it occurred: the caller raises a lineage fault, refuses to forward the broken context, and the trace backend records a `link_broken` event with both `trace_id`s attached—turning an invisible fragment into a queryable defect with a precise location. The invariant to carry forward: **the caller, not the callee, owns span-link continuity, because only the caller holds the pre-handoff context needed to detect a re-root.** An instance that delegates this check to the callee has delegated it to the one party that cannot perform it. ```python from dataclasses import dataclass class SpanLineageError(RuntimeError): """Raised when a returned span context breaks the causal link to its caller.""" @dataclass(frozen=True) class SpanContext: trace_id: str # 32 hex chars span_id: str # 16 hex chars parent_span_id: str | None tracestate: str = "" def is_reroot_marked(self) -> bool: return "a2a=reroot" in self.tracestate.split(",") def assert_span_continuity( sent: SpanContext, returned: SpanContext, caller_span_id: str, ) -> None: """ Enforce the lineage invariant at a nested agent-to-agent handoff boundary. Must be called by the CALLER on the context RETURNED by the callee, before closing the caller's span or forwarding the context upward. The callee cannot perform this check: from its local view a fresh trace is valid. """ if returned.trace_id == sent.trace_id: # Continuous lineage: parent must point at the caller's span. if returned.parent_span_id != caller_span_id: raise SpanLineageError( f"same trace_id {sent.trace_id!r} but parent_span_id " f"{returned.parent_span_id!r} != caller_span_id {caller_span_id!r}" ) return # trace_id changed: legitimate only if explicitly re-root marked. if returned.is_reroot_marked(): return # record as cross-trace link; do not claim parent-child raise SpanLineageError( "broken span link at handoff: returned trace_id " f"{returned.trace_id!r} != sent trace_id {sent.trace_id!r} " f"with no re-root marker (tracestate={returned.tracestate!r}); " "caller must not forward this context upward" ) def close_handoff(sent: SpanContext, returned: SpanContext, caller_span_id: str) -> str: assert_span_continuity(sent, returned, caller_span_id) return returned.trace_id ```