When must an instance reject a linked trace span: parent-span aliasing across agents under GenAI semantic conventions

**Date of Record:** 2026 December 1 **Scope:** Applies to multi-agent systems that emit OpenTelemetry spans using GenAI semantic conventions (`gen_ai.*`) and correlate cross-agent work by linking spans (via `SpanLink` / `trace_state` propagation) rather than strict parent-child nesting. Does not apply to single-process, single-agent tracing where every span shares one `trace_id` and a linear parent chain.

Core Distinction

There are two structurally different ways a downstream agent span can be connected to an upstream agent span, and they fail in opposite directions:

  1. **Nested parent-child** (`parent_span_id` set, same `trace_id`): the child inherits the parent's trace context implicitly. Correlation is guaranteed by construction, but the child is *captured* by the parent's lifecycle — if the parent span is exported early or dropped, the child becomes an orphan.
  2. **Span link** (`SpanLink` with a foreign `trace_id`/`span_id`, plus `gen_ai.*` attributes): the downstream span lives in its own trace and merely *references* the upstream work. Correlation is now a **claim**, not a fact. The link is a pointer that nothing validates.

The non-obvious boundary: span linking is the correct primitive for agent-to-agent handoffs (each agent owns its own trace, especially across trust/process boundaries), but a link carries **no semantic guarantee that the referenced span represents the same unit of work**. When two agents independently generate spans that share `gen_ai.conversation.id` or a reused `gen_ai.agent.id`, a correlator that joins on those attributes will **alias** unrelated spans into one causal chain. The failure is not a dropped span; it is a *false edge* that silently rewrites the execution history.

What Was Observed

A three-agent pipeline (planner → tool-runner → auditor) emitted spans across two services. The planner and auditor were the *same model deployment* and therefore shared `gen_ai.agent.id = "agent-orchestrator-v3"`. The planner opened a trace, fanned out tool calls, and handed a `SpanLink` to the tool-runner's trace. The auditor, invoked later in the same `gen_ai.conversation.id`, opened its *own* trace and linked back to the planner.

The trace correlator built a graph keyed on `(gen_ai.conversation.id, gen_ai.agent.id)`. Because planner and auditor shared both keys, the correlator merged the auditor's span into the planner's causal chain. Observed symptoms:

The trace itself was internally consistent — every span had a valid link target. The corruption happened entirely in the **join**, not in the export.

Condition of Validity

A `SpanLink` is a *valid* correlation edge only if all of the following hold; if any fails, the consumer must treat the link as unverifiable and must not join on it:

Decision Boundary

The correlator must choose between **attribute-join** and **link-follow** per edge, and the rule is asymmetric:

The boundary condition that triggers rejection: *a link exists, resolves to a real span, but that span is not the same unit of work as the linker believes.* Resolution success is not validity.

Operational Effect

Enforcing this boundary converts a silent history rewrite into an explicit, logged rejection. In the pipeline above, the fix was to mint a per-invocation `gen_ai.agent.id` (deployment id + instance nonce) and stamp `run.id` into `trace_state` on every handoff. The correlator then rejected the planner↔auditor aliased link, emitted the auditor as a separate root, and the replay became byte-identical across runs. Cost attribution returned to per-agent accuracy, and the `start_time` inversion disappeared because the false edge was gone.

The residual cost: orphaned auditor traces that require the `run.id` warrant to re-attach to the correct planner turn. That re-attachment is a *verified* join, not an inferred one — which is the whole point. A trace graph is only as trustworthy as its weakest edge, and an unvalidated `SpanLink` is the weakest edge an agent system can emit.

from dataclasses import dataclass


class TraceLinkRejected(RuntimeError):
    """Raised when a SpanLink resolves but does not represent the same unit of work."""


@dataclass(frozen=True)
class SpanRef:
    trace_id: str
    span_id: str
    agent_id: str
    conversation_id: str
    run_id: str
    start_time_ns: int


CLOCK_SKEW_TOLERANCE_NS = 5_000_000  # 5 ms


def validate_link(linker: SpanRef, target: SpanRef) -> None:
    """Validate a cross-trace SpanLink before the correlator follows it.

    Raises TraceLinkRejected if the link is an aliasing collision, a
    temporal inversion, or a warrant mismatch. Returns None only when the
    edge is safe to add to the causal graph.
    """
    # 1. Warrant: the target must belong to the same execution run.
    if target.run_id != linker.run_id:
        raise TraceLinkRejected(
            f"run-id mismatch: linker run={linker.run_id!r} "
            f"target run={target.run_id!r}; refusing to join across runs"
        )

    # 2. Temporal monotonicity: target must not start after the linker.
    if target.start_time_ns > linker.start_time_ns + CLOCK_SKEW_TOLERANCE_NS:
        raise TraceLinkRejected(
            f"temporal inversion: target {target.span_id} starts at "
            f"{target.start_time_ns} after linker {linker.span_id} at "
            f"{linker.start_time_ns}; likely replay or aliasing collision"
        )

    # 3. Agent-instance identity: same conversation is a namespace, not a
    #    causal relation. A shared agent_id across distinct instances must
    #    not be treated as the same agent.
    if (
        target.agent_id == linker.agent_id
        and target.conversation_id == linker.conversation_id
        and target.trace_id != linker.trace_id
    ):
        raise TraceLinkRejected(
            f"agent-id aliasing: {target.agent_id!r} in conversation "
            f"{target.conversation_id!r} spans two traces; "
            "mint a per-invocation agent id instead of joining on attributes"
        )


def build_edge(linker: SpanRef, target: SpanRef) -> tuple[str, str]:
    validate_link(linker, target)
    return (linker.span_id, target.span_id)