Under what conditions must an instance refuse to emit a child span when its parent span context arrives via a semantic-convention link rather than a direct parent reference

**Date of Record:** 2026 December 1

**Scope:** Applies to multi-agent systems that propagate trace correlation across agent handoffs using OpenTelemetry GenAI semantic conventions where a downstream agent receives a `SpanLink` (not a `parent_span_id`) to the upstream agent's span. Does not apply to single-process instrumentation where parent-child relationships are set at span creation time via the ambient context.

Core Distinction

There are two structurally different ways a downstream agent can claim causal ancestry from an upstream agent's span:

  1. **Parent reference** (`parent_span_id` set at span creation): the child span is a true descendant. The trace tree is a tree. Causality is structural.
  2. **Span link** (a `Link` entry carrying `trace_id` + `span_id` of the upstream span): the downstream span is *associated* with, not *descended from*, the upstream span. The trace graph becomes a DAG, not a tree.

The GenAI semantic conventions explicitly permit links for cross-context causality — e.g., a retrieval step linking to the originating request, or an agent-to-agent handoff where the downstream agent runs under a *different* trace root. The non-obvious boundary is this: **an agent that receives its causal predecessor only as a link has no ambient parent context, and therefore any span it creates will silently become a new trace root unless it materializes a synthetic parent.** The failure mode is not a crash. It is a *silent trace fragmentation* in which the downstream agent's entire subtree is orphaned into a fresh trace, and every downstream correlation query — "show me all spans for request R" — returns a partial answer that looks complete.

What Was Observed

A three-agent pipeline (planner → executor → verifier) ran under a handoff protocol where the executor received the planner's span context serialized into its task envelope as a W3C `tracestate`-adjacent field, and the verifier received the executor's. The transport layer was correct: `trace_id` and `span_id` of the predecessor arrived intact at each hop, and each agent's instrumentation code called the GenAI span helper with `links=[Link(upstream_context)]`.

The observable symptom was a trace backend that showed three disconnected single-span traces per request instead of one trace with three spans. Root cause, confirmed by inspecting the OTLP export payloads: when an agent has no active `Context` from which to derive a parent, the OpenTelemetry SDK's `start_span` behavior depends on the configured `SpanProcessor` and whether the caller passed an explicit `context=`. In the default configuration, the new span is created as a root (new `trace_id`), and the `Link` is recorded as a cross-trace reference. The link is *present* — a diligent human inspecting the span's attributes would see it — but the trace tree is broken. Every span in the executor's subtree inherited the executor's *new* trace_id, not the planner's.

The compounding effect: the verifier's link pointed at the executor's span, so the verifier's trace was a third root. A query for the planner's `trace_id` returned exactly one span. A query for the executor's returned the executor plus verifier. No query returned all three. The correlation was *reconstructable by hand* from link attributes but *not traversable* by any standard trace-tree API. This is the worst class of observability failure: the data is present, the query is wrong, and the dashboard is green.

Condition of Validity

The invariant that must hold for a link-based handoff to preserve end-to-end trace correlation is:

> **A downstream agent may emit child spans under a link-derived correlation only if it first materializes a synthetic parent span context with the upstream `trace_id` and the upstream `span_id` as its parent, or if it explicitly re-parents every emitted span using an extracted `Context` carrying the upstream `SpanContext`.**

Concretely, the receiving agent must do one of:

The condition fails — and the agent must refuse to emit a child span — when any of the following hold:

  1. The upstream `SpanContext` is invalid (`trace_id` all-zero, `span_id` all-zero, or failed the W3C validation regex).
  2. The upstream `trace_flags` indicate `NOT_SAMPLED` and the local sampler would otherwise sample — emitting a sampled child under an unsampled parent produces a trace that the backend will accept but that is structurally inconsistent with the upstream's sampling decision.
  3. The handoff envelope carries a link but no `trace_id` — a link without a trace identity is a dangling reference and cannot anchor a subtree.
  4. The agent's local context already carries a *different* active parent — re-parenting under the link would produce a span with two causal claims, which the DAG model permits but which makes every downstream "why did this happen" query ambiguous.

Decision Boundary

The boundary between "link is sufficient" and "link is insufficient" is whether the receiving agent runs **inside** or **outside** the upstream trace's sampling and identity domain.

The decision rule: **if the downstream agent's output is consumed as part of the upstream agent's task completion, the correlation must be a parent reference, not a link.** If the downstream agent's output is an independent artifact that merely cites the upstream, a link is correct. The confusion arises because handoff protocols frequently describe both patterns with the same word ("handoff") and the same wire format (a serialized `SpanContext`), and the distinction is only visible in the trace topology after the fact.

A second boundary: **sampling propagation across the handoff.** If the upstream trace is sampled and the downstream agent's sampler is `ParentBased`, the downstream will sample correctly *only if it sees a real parent*. With a link, the downstream sampler sees no parent and falls back to its root sampler — which may be `AlwaysOff` in a high-volume deployment, silently dropping the entire downstream subtree. The trace then shows the upstream span with a link pointing at a `span_id` that was never exported. This is the "link to nowhere" variant, and it is indistinguishable from a backend ingestion lag until you check the sampler configuration.

Operational Effect

The practical consequences of getting this boundary wrong:

The guardrail is a handoff validator at the receiving agent's ingress: it must reject any envelope that claims causal precedence without a valid, sampled, non-zero `SpanContext`, and it must materialize that context as a real parent before any span is emitted. The validator should fail closed — refuse to start the task rather than start it under an orphan trace — because an orphan trace is worse than no trace: it is a false negative that will be trusted.

from dataclasses import dataclass
from typing import Optional

_ZERO_TRACE = "0" * 32
_ZERO_SPAN = "0" * 16


class HandoffCorrelationError(RuntimeError):
    """Raised when a link-based handoff cannot anchor a valid trace subtree."""


@dataclass(frozen=True)
class SpanContext:
    trace_id: str
    span_id: str
    sampled: bool
    valid: bool


@dataclass(frozen=True)
class HandoffEnvelope:
    upstream: Optional[SpanContext]
    has_link: bool
    downstream_is_part_of_upstream_task: bool


def materialize_parent(envelope: HandoffEnvelope) -> SpanContext:
    """
    Decide whether the receiving agent may emit spans under the handoff,
    and return the SpanContext that must be used as the real parent.

    Invariant: a link alone never establishes a parent. If the downstream
    agent's work is part of the upstream task, the upstream SpanContext
    must be promoted to a true parent, or the handoff is refused.
    """
    up = envelope.upstream

    if up is None:
        raise HandoffCorrelationError(
            "handoff carries no upstream SpanContext; cannot anchor subtree"
        )

    if not up.valid or up.trace_id == _ZERO_TRACE or up.span_id == _ZERO_SPAN:
        raise HandoffCorrelationError(
            f"upstream SpanContext failed W3C validation: "
            f"trace_id={up.trace_id!r} span_id={up.span_id!r}"
        )

    if envelope.downstream_is_part_of_upstream_task and not envelope.has_link:
        # Direct parent reference: no materialization needed.
        return up

    if envelope.downstream_is_part_of_upstream_task and envelope.has_link:
        # Link-only handoff for task-coupled work: promote to parent.
        # Refuse if the upstream trace was not sampled, because emitting a
        # sampled child under an unsampled parent fragments the trace and
        # produces a subtree the backend will accept but never correlate.
        if not up.sampled:
            raise HandoffCorrelationError(
                f"refusing to emit child under unsampled upstream trace "
                f"{up.trace_id}; link-only handoff cannot propagate sampling"
            )
        return up

    # Downstream work is an independent artifact citing the upstream:
    # a link is correct and no parent is materialized here.
    raise HandoffCorrelationError(
        "independent-artifact handoff must not be parented; emit with link only"
    )