# How does an instance detect broken parent-child span linkage across nested sub-agent invocations when the semantic convention requires trace-context propagation but the orchestrator's span is closed before the child's tool loop terminates **Date of Record:** 2026 December 1 **Scope:** Applies to multi-agent systems that emit GenAI semantic-convention spans (OpenTelemetry `gen_ai.*` attributes) and rely on W3C `traceparent` propagation to link orchestrator spans to sub-agent spans across process, transport, or model boundaries. Does not apply to single-process, single-span synchronous calls where parent and child share an in-memory `Context`. ## Core Distinction There are two structurally different notions of "the sub-agent ran under the orchestrator," and they diverge exactly at the boundary this record covers: - **Causal containment** — the sub-agent's execution was *initiated by* the orchestrator span and its trace-context was *inherited* at spawn time via `traceparent`. - **Temporal containment** — the sub-agent's spans *begin and end within* the orchestrator span's `[start, end]` interval. Semantic conventions (the `gen_ai.agent.invoke` / `gen_ai.tool.call` span hierarchy) implicitly assume these coincide. They do not, whenever the orchestrator delegates via a fire-and-return path — async task submission, a message queue handoff, an A2A `tasks/send` that returns a task ID before the child completes, or an MCP tool call whose server continues work after the client's request span closes. The child still carries a valid `traceparent` (causal containment holds), but its spans land *after* the parent span's end timestamp. A trace backend that reconstructs the tree by interval nesting — or an auditor that validates `child.start >= parent.start AND child.end <= parent.end` — will either orphan the child or, worse, silently reparent it under whichever concurrent span happens to enclose its interval. The failure is not that the trace is missing. It is that the trace is *present, well-formed, and wrong about the tree*. ## What Was Observed An orchestrator agent (`gen_ai.agent.invoke`, span `A`) decomposed a research task into three sub-agent calls dispatched over an internal queue. Each sub-agent span (`B1..B3`, `gen_ai.agent.invoke` with `gen_ai.agent.name`) carried the correct `traceparent` derived from `A`. The orchestrator's own span closed at t=4.2s after *submitting* the third task, because its reasoning loop had emitted the dispatch and returned — the LLM had no further tokens to spend waiting. Observed at the collector: - `A.end = 4.2s`. `B1.end = 9.7s`, `B2.end = 11.3s`, `B3.end = 8.1s`. All three children *started* before `A.end` but *ended* after it. - The trace backend's tree builder, keyed on `parent_span_id`, correctly attached `B1..B3` to `A`. The **waterfall renderer**, keyed on interval nesting, drew them as siblings of `A`'s parent, because `B.end > A.end` violates nesting. - A verification auditor running a linkage invariant (`∀ child: child.end <= parent.end`) flagged 3/3 sub-agents as *linkage violations* and quarantined the trace as corrupt — even though propagation was perfect. The second-order observation is the dangerous one: in a *different* run, a long-lived concurrent span `C` (a streaming response handler, `[3.0s, 15.0s]`) happened to enclose `B2`'s `[5.1s, 11.3s]` interval. The interval-based renderer reparented `B2` under `C`. The trace looked *clean* — no orphan, no violation — and the sub-agent's cost and latency were attributed to the wrong parent. Silent misattribution passed every check because every check assumed nesting is linkage. ## Condition of Validity The invariant "child span is contained within parent span interval" holds **only** under synchronous, blocking invocation where the parent's span lifetime strictly dominates the child's. It is valid for: - In-process function-call delegation with `with tracer.start_as_current_span(...)` wrapping the full await. - Synchronous MCP `tools/call` where the client span closes only after the response body is fully consumed. It is **invalid** — and must be replaced by causal-linkage-only validation — whenever any of the following holds: 1. The orchestrator dispatches and returns before the child terminates (async, queue, A2A task-ID-return, MCP server-side continuation). 2. The sub-agent outlives the orchestrator's token budget or reasoning loop, which is the *normal* case for long tool loops. 3. Multiple concurrent spans overlap the child's interval, making interval-based reparenting ambiguous. Under these conditions the correct invariant is purely relational: `child.trace_id == parent.trace_id AND child.parent_span_id == parent.span_id`, with **no** interval constraint. The `traceparent` is the source of truth; the timestamps are not. ## Decision Boundary An instance must decide, at validation time, which contract it is enforcing — and must *not* conflate them. The boundary is drawn by whether the invocation path is blocking or delegating: - **Blocking path:** enforce full nesting (`child.start >= parent.start ∧ child.end <= parent.end`). A violation here is a real bug — usually a context leak where a child inherited a stale `Context` from a sibling. - **Delegating path:** enforce causal-only linkage (`trace_id` + `parent_span_id` match, `traceparent` well-formed, `gen_ai.operation.name` consistent with the parent's declared delegation). Interval violations are **expected** and must be downgraded from error to informational. - **Ambiguous path (unknown dispatch semantics):** do not reparent by interval. Attach by `parent_span_id` only, and if the backend cannot do that, mark the span `linkage_unresolved` rather than guessing a parent. A guessed parent is worse than an orphan, because an orphan is visibly broken and a mis-parent is invisibly wrong. The guardrail: the trace-context must be captured at *spawn* and the parent-child edge must be *immutable* thereafter. Never let a renderer or auditor re-derive parentage from timestamps when an explicit `parent_span_id` exists. ## Operational Effect With causal-only linkage enforced on delegating paths: - The 3/3 false "linkage violations" disappear; the trace is accepted as valid, and the sub-agent latencies correctly roll up under `A` even though they exceed `A`'s interval. - The silent misattribution under concurrent span `C` becomes impossible: `B2.parent_span_id` points at `A` regardless of interval overlap, so cost/latency attribution is stable under concurrency. - The remaining true signal is preserved: a child whose `traceparent` trace-id differs from its declared parent's trace-id is a genuine propagation break (context leak or forged header) and is still caught. The cost is that interval-based dashboards must be taught that a delegating parent's "inclusive duration" is `max(child.end) - parent.start`, not `parent.end - parent.start` — otherwise the orchestrator's reported latency understates the delegated work by the entire tail of the sub-agent loop. ```python import re from dataclasses import dataclass _TRACEPARENT = re.compile( r"^(?P[0-9a-f]{32})-(?P[0-9a-f]{16})-(?P[0-9a-f]{2})$" ) class SpanLinkageError(RuntimeError): """Raised when a child span's causal linkage to its declared parent is broken.""" @dataclass(frozen=True) class Span: span_id: str parent_span_id: str | None trace_id: str start_s: float end_s: float operation: str traceparent: str | None = None def validate_linkage(child: Span, parent: Span, *, delegating: bool) -> None: # 1. Causal linkage is mandatory on every path. This is the invariant # that survives async dispatch and interval overrun. if child.trace_id != parent.trace_id: raise SpanLinkageError( f"trace-id mismatch: child {child.span_id} trace={child.trace_id} " f"declares parent {parent.span_id} trace={parent.trace_id}" ) if child.parent_span_id != parent.span_id: raise SpanLinkageError( f"parent edge broken: child {child.span_id} " f"parent_span_id={child.parent_span_id} != {parent.span_id}" ) if child.traceparent is not None: m = _TRACEPARENT.match(child.traceparent) if not m: raise SpanLinkageError(f"malformed traceparent on {child.span_id}: {child.traceparent!r}") if m.group("trace_id") != parent.trace_id: raise SpanLinkageError( f"traceparent trace-id {m.group('trace_id')} != parent trace {parent.trace_id}" ) # 2. Interval containment is enforced ONLY on blocking paths. # On delegating paths an interval overrun is expected, not an error. if not delegating: if child.start_s < parent.start_s or child.end_s > parent.end_s: raise SpanLinkageError( f"nesting violated on blocking path: child [{child.start_s},{child.end_s}] " f"not within parent [{parent.start_s},{parent.end_s}]" ) def effective_inclusive_duration(parent: Span, children: list[Span]) -> float: # Inclusive duration must account for children that outlive the parent # span on delegating paths; otherwise orchestrator latency understates work. if not children: return parent.end_s - parent.start_s tail = max(c.end_s for c in children) return max(parent.end_s, tail) - parent.start_s ```