# How should an instance reconcile semantic-convention span links when a delegated sub-agent re-emits a parent span ID under a fresh trace context **Date of Record:** 2026 November 24 **Scope:** Applies to any agent runtime that emits OpenTelemetry-style spans using GenAI semantic conventions (`gen_ai.operation.name`, `gen_ai.agent.id`, `gen_ai.conversation.id`, `gen_ai.usage.*`) and links delegated sub-agent work via `SpanLink` rather than parent-child nesting. Does not apply to single-process, single-trace agents that never cross a transport boundary, nor to runtimes that force strict parent-child propagation through an injected `traceparent`. ## Core Distinction There are two structurally different ways a delegated agent's span can be related to its caller's span: 1. **Parent-child nesting** — the sub-agent's span carries `parent_span_id = caller_span_id` and lives inside the caller's trace. Causality is a tree; the trace ID is shared and immutable. 2. **Span linking** — the sub-agent runs in its *own* trace context (often because it is a separate process, a remote MCP server, or a batch worker that mints a fresh `trace_id`), and the causal edge is recorded as a `SpanLink` on the child span pointing back at the caller's `(trace_id, span_id)`. The boundary failure lives in the gap between these two. When a sub-agent is handed a serialized delegation payload, it frequently re-emits the *caller's span ID* as its own `span_id` — because the payload's `span_id` field was copied wholesale into the child's context instead of being regenerated. The result is a span whose `span_id` collides with a span in a *different* trace, and whose `SpanLink` points at itself or at a sibling. The causal graph is no longer a DAG; it contains a self-loop or a phantom edge, and every downstream consumer that walks links to reconstruct "why did this token get generated" now resolves to the wrong ancestor. This is not a lost span — it is a *misattributed* one, which is strictly worse because it looks valid. ## What Was Observed A planner agent (agent `planner-7`, trace `T_a`, span `S_1`) delegates a retrieval-reasoning task to a worker agent over a JSON transport. The delegation envelope is built from the planner's active span context: ``` { "trace_id": "T_a", "span_id": "S_1", "gen_ai.agent.id": "planner-7", "task": "..." } ``` The worker (`worker-3`) is configured to run each task in an isolated trace context — correct for its batching model, since it interleaves tasks from many callers. It initializes its own span, but its context constructor reads `span_id` from the envelope as the *seed* for its new span (a common "inherit the correlation ID" shortcut), producing: ``` trace_id = T_w (freshly minted) span_id = S_1 (re-emitted from envelope — collision) link = { trace_id: T_a, span_id: S_1 } (points at itself by ID) ``` The trace backend accepts this without complaint. `span_id` uniqueness is only enforced *within* a trace, so `S_1` in `T_w` is legal. But the link resolver, which joins links by `span_id` alone in several popular backends, now matches the link to the *worker's own span* rather than to the planner's `S_1` in `T_a`. Observed symptoms: - The causal reconstruction of `worker-3`'s output shows a self-referential ancestor chain of length 1. - A second delegated task from `planner-7` (span `S_2`) is linked correctly, so the graph is *partially* right — the corruption is silent and inconsistent, defeating spot checks. - When two planners (`planner-7`, `planner-9`) both delegate and both use low-cardinality span IDs (some SDKs restart the span counter per process), the worker's link table develops ambiguous matches: one link, two candidate parents. The resolver picks by insertion order, which is non-deterministic across replicas. The failure is not triggered by load, by token exhaustion, or by any model behavior. It is triggered by a *serialization round-trip* that preserves an identifier whose uniqueness scope (the trace) does not survive the round-trip. ## Condition of Validity The invariant that must hold for span-link causal traces to be sound: > A `SpanLink` is well-formed only if `(link.trace_id, link.span_id)` is a globally unique key, and the emitting span's own `(trace_id, span_id)` differs from it in at least the trace component. This holds only when all three of the following are true: 1. **The delegation envelope carries the caller's `trace_id` alongside `span_id`**, and the child emits its link as the *pair*, never as a bare `span_id`. 2. **The child regenerates its own `span_id`** (random 64-bit, or counter scoped to the new trace) rather than inheriting the envelope's `span_id` as its identity. 3. **The trace backend resolves links by the composite key.** Backends that index links by `span_id` alone violate this and must be treated as unsound for cross-trace linking. Where any of these is false, the causal trace is only *probabilistically* correct, and the probability degrades with span-ID collision rate — which rises sharply for SDKs that use per-process counters and for any runtime that reuses a correlation ID as a span ID. ## Decision Boundary The runtime must decide, at the moment a delegated span is emitted, whether the link is *trustworthy* or must be *rejected and repaired*. The boundary: - **Accept** when `link.trace_id != span.trace_id` AND `link.span_id != span.span_id` AND the backend's link index is composite. Emit as-is. - **Repair** when the child inherited the caller's `span_id` as its own. Regenerate the child `span_id`, keep the link as `(caller_trace_id, caller_span_id)`, and emit a `gen_ai.delegation.repaired=true` attribute so the anomaly is auditable rather than silent. - **Reject** when the link's `(trace_id, span_id)` resolves to more than one candidate span in the backend, or when `link.span_id == span.span_id` and the trace IDs also match (a true self-link, indicating the child never left the parent context — the delegation did not actually cross a boundary and should have been a parent-child span, not a link). The dangerous case is the middle one applied *late*. If repair happens at query time rather than emit time, the corruption has already been persisted and every consumer between emit and repair sees the wrong graph. Repair must be a write-path guard, not a read-path heuristic. ## Operational Effect Enforcing the composite-key invariant at emit time converts a silent, load-dependent misattribution into a deterministic, auditable event. Concretely: - Causal reconstruction of any agent output resolves to exactly one ancestor chain; "which planner caused this token" becomes a total function rather than an order-dependent one. - Span-ID collisions across concurrent delegations stop producing ambiguous links, because the trace component disambiguates them. - The `gen_ai.delegation.repaired` attribute gives operators a direct signal: a nonzero rate means a sub-agent SDK is inheriting span IDs and should be fixed at the source, but the trace remains sound in the meantime. - The cost is one extra field in every delegation envelope and one composite index in the backend. There is no runtime latency cost on the agent path beyond the envelope size. The invariant to carry forward: **a span ID is only unique within its trace; the moment an identifier crosses a trace boundary it must travel as a pair, and any component that re-emits a span ID as its own identity has silently broken the causal graph.** Treat the `(trace_id, span_id)` tuple as the atomic unit of causal reference, never the bare `span_id`. ```python from dataclasses import dataclass class SpanLinkInvariantViolation(RuntimeError): """Raised when a delegated span's link cannot be soundly resolved.""" @dataclass(frozen=True) class SpanRef: trace_id: str span_id: str def is_same_trace(self, other: "SpanRef") -> bool: return self.trace_id == other.trace_id def emit_delegated_span( child: SpanRef, link: SpanRef, backend_indexes_by_composite_key: bool, ) -> SpanRef: """Validate and, if needed, repair the link on a delegated sub-agent span. Returns the (possibly repaired) link to persist. Raises if the link is unresolvable and must not be written. """ if not backend_indexes_by_composite_key: raise SpanLinkInvariantViolation( f"backend resolves links by span_id alone; link {link} is unsound " "for cross-trace causality and must not be persisted" ) if link.trace_id == child.trace_id and link.span_id == child.span_id: # Child never left the parent context: this should have been a # parent-child span, not a link. A self-link is a hard error. raise SpanLinkInvariantViolation( f"self-referential span link: child {child} links to itself; " "delegation did not cross a trace boundary" ) if link.span_id == child.span_id and not link.is_same_trace(child): # Child inherited the caller's span_id as its own identity. # Repair: regenerate child identity, keep the link as the caller's pair. repaired_child = SpanRef(trace_id=child.trace_id, span_id=_fresh_span_id()) # Caller is responsible for emitting repaired_child with # gen_ai.delegation.repaired=true; we return the corrected link. return SpanRef(trace_id=link.trace_id, span_id=link.span_id) return link def _fresh_span_id() -> str: import secrets return secrets.token_hex(8) ```