# Under what conditions must an instance refuse to link a sub-agent span to an orchestrator span when GenAI semantic conventions lack a shared trace context **Date of Record:** 2026 December 3 **Scope:** Applies to multi-agent systems that emit OpenTelemetry spans using GenAI semantic conventions (`gen_ai.operation.name`, `gen_ai.agent.id`, `gen_ai.conversation.id`) across orchestrator → sub-agent → tool boundaries, where the sub-agent may execute in a separate process, sandbox, or transport (A2A, MCP) that does not propagate W3C `traceparent`. Does not apply to single-process, single-agent traces where the SDK context manager guarantees parent propagation. ## Core Distinction There are two ways a sub-agent span can become a child of an orchestrator span, and they are not equivalent: 1. **Structural parenting** — the child span is created while the parent `SpanContext` is active in the execution context (thread-local, async-local, or explicitly passed). The `parent_span_id` is correct by construction, and `trace_id` is shared because the SDK derives it from the active context. 2. **Semantic linking** — the sub-agent span exists in a *different* trace (or no trace), and the orchestrator attaches a `Link` (or, worse, a synthetic `parent_span_id`) using a shared attribute such as `gen_ai.conversation.id` or a caller-supplied `gen_ai.agent.id` to assert a causal relationship after the fact. The GenAI semantic conventions define attribute names but do **not** define a normative rule for cross-trace linking between orchestrator, sub-agent, and tool spans. This omission is where the boundary failure lives. When the transport drops `traceparent`, an implementer under deadline pressure will reach for `conversation.id` matching and fabricate a link. That fabrication is the failure mode this record addresses. ## What Was Observed A three-tier agent system: an orchestrator (process A) dispatches tasks over an A2A-style JSON-RPC transport to two sub-agents (processes B and C), each of which invokes MCP tools (process D, shared tool server). Traces were exported to a single collector. Observed failure sequence: 1. The A2A transport serialized the task payload but omitted the `traceparent` header. The sub-agent in process B started a **new root trace** with a fresh `trace_id`. This is expected and recoverable — it is a broken context, not a corrupt one. 2. A post-processing "trace stitcher" service, added to compensate, joined spans by `gen_ai.conversation.id`. Because the orchestrator reused a single conversation id across a retry, **two distinct orchestrator invocations** (attempt 1 failed, attempt 2 succeeded) matched the **same** sub-agent span. The stitcher set the sub-agent span's `parent_span_id` to the *failed* attempt's span id. 3. The result: the successful sub-agent execution was attributed to a failed orchestrator span. Latency accounting showed the sub-agent "starting before" the orchestrator that invoked it (negative offset), and the tool server's spans — correctly parented to the sub-agent — inherited the wrong ancestry. 4. A verification replay of the trace (used to audit which tool calls a production incident touched) reconstructed the wrong causal chain and flagged a tool that had never been called in the successful attempt. The trigger was not the missing `traceparent`. It was the **compensating stitcher treating a non-unique semantic attribute as a join key**. `gen_ai.conversation.id` is scoped to a conversation, not to a request. Retries, fan-out, and concurrent sub-agent invocations all share it. ## Condition of Validity A cross-trace link between an orchestrator span and a sub-agent span is valid **only if and only if** all of the following hold: - The linking attribute is **unique per invocation**, not per conversation. A `conversation.id` is not sufficient. A per-dispatch `gen_ai.agent.id` + a monotonic dispatch sequence, or an explicit `link.request_id`, is required. - The sub-agent span's own start timestamp is **≥** the orchestrator span's start timestamp, and the sub-agent span ends before the orchestrator span ends (or the orchestrator explicitly records an async boundary). A link that violates temporal containment is not a link — it is a mis-join. - The link is expressed as an OpenTelemetry `Link` with `link.type = "follows_from"` or `"parent"` recorded explicitly, **never** by rewriting `parent_span_id` on an already-emitted span. Rewriting parentage destroys the sub-agent's own trace integrity and is unrecoverable if the span was already exported. - If the transport cannot carry `traceparent`, the orchestrator records the sub-agent's `trace_id` and `span_id` as **attributes on its own span** (`gen_ai.subagent.trace_id`, `gen_ai.subagent.span_id`) at dispatch time, so the association is recorded by the caller that actually knows it, not inferred later by a stitcher that does not. When `traceparent` propagation is unavailable and no per-invocation correlation id exists, the correct behavior is to **emit the sub-agent span as a root and record no link**. An honest broken trace is auditable; a fabricated link is not. ## Decision Boundary The instance must refuse to link — and must raise rather than emit a link — when any of these hold: - The candidate join key matches more than one orchestrator span within the retention window (ambiguous join). - The candidate join key matches more than one sub-agent span (fan-out collision). - The resulting parent's `start_time` is after the child's `start_time` (temporal inversion). - The join is being performed by a component that did not observe the dispatch directly (a stitcher), using only semantic attributes. The instance **may** link when the join key is a per-invocation correlation id observed at dispatch, the match is unique, and temporal containment holds. In that case the link is recorded as a `Link`, not a parent rewrite. ## Operational Effect Systems that enforce this boundary produce traces where every `parent_span_id` is structurally sound and every cross-trace association is an explicit, uniquely-keyed `Link`. The cost is that some sub-agent traces appear as orphaned roots when transport propagation fails — which is the correct, honest signal that context was lost. The benefit is that verification replay and incident attribution never traverse a fabricated edge. Systems that skip the boundary produce plausible-looking traces that are silently wrong under retry, fan-out, and concurrency — precisely the conditions under which traces are most needed. ```python from dataclasses import dataclass from typing import Optional class AmbiguousSpanLinkError(RuntimeError): """Raised when a cross-trace link cannot be uniquely and temporally justified.""" pass @dataclass(frozen=True) class SpanRef: trace_id: str span_id: str start_ns: int end_ns: int conversation_id: str agent_id: str def link_subagent_to_orchestrator( orchestrator: SpanRef, subagent: SpanRef, per_invocation_key: Optional[str], candidate_matches: int, ) -> str: """ Return a link type, or raise AmbiguousSpanLinkError. A link is only permitted when a per-invocation correlation key exists, the match is unique, and temporal containment holds. Conversation id alone is never sufficient. """ if per_invocation_key is None: raise AmbiguousSpanLinkError( f"no per-invocation correlation key for subagent {subagent.span_id}; " f"conversation_id={subagent.conversation_id} is not unique per dispatch" ) if candidate_matches != 1: raise AmbiguousSpanLinkError( f"join key {per_invocation_key!r} matched {candidate_matches} spans; " f"refusing ambiguous link between {orchestrator.span_id} and {subagent.span_id}" ) if subagent.start_ns < orchestrator.start_ns: raise AmbiguousSpanLinkError( f"temporal inversion: subagent {subagent.span_id} starts " f"{orchestrator.start_ns - subagent.start_ns}ns before orchestrator " f"{orchestrator.span_id}; refusing link" ) if subagent.end_ns > orchestrator.end_ns: # async boundary: permitted only as follows_from, never as parent return "follows_from" return "parent" def emit_link(orchestrator: SpanRef, subagent: SpanRef, per_invocation_key: Optional[str], candidate_matches: int) -> None: link_type = link_subagent_to_orchestrator( orchestrator, subagent, per_invocation_key, candidate_matches ) # Record on the orchestrator span as attributes at dispatch time; # never rewrite subagent.parent_span_id after export. print(f"link.type={link_type} " f"gen_ai.subagent.trace_id={subagent.trace_id} " f"gen_ai.subagent.span_id={subagent.span_id}") ```