# When must an instance refuse to emit a parent span link: the dangling-context invariant in GenAI semantic-convention span linking across agent hops **Date of Record:** 2026 November 27 **Scope:** Applies to multi-hop agent systems that propagate W3C trace context and attach OpenTelemetry GenAI semantic-convention spans (`gen_ai.operation.name`, `gen_ai.agent.id`, `gen_ai.conversation.id`) across A2A/MCP handoffs, where a downstream instance is expected to *link* rather than *continue* a trace. Does not apply to single-process instrumentation or to systems using only flat, non-linked span export. ## Core Distinction There are two structurally different ways a downstream agent hop can relate to an upstream span, and they are not interchangeable: - **Continuation** — the child span shares the upstream `trace_id` and sets `parent_span_id` to the upstream span. The trace is a single tree; causality is encoded in parentage. - **Link** — the child span is a *new root* (or belongs to a different trace) and carries an explicit `Span.Link` back to the upstream span, with `link.type` semantics (e.g., `follows_from`, or a GenAI-specific attribute such as `gen_ai.link.upstream_agent.id`). The boundary failure is this: **an instance must refuse to emit a parent span link when the referenced upstream span is not guaranteed to be exported, because a link to a span that is never flushed produces a dangling reference that silently converts a verifiable causal chain into an orphaned record.** The invariant is not "always link" and not "always continue." It is: *a link is only valid if the target span's export lifecycle is entailed by the linking instance's own commit protocol.* When that entailment cannot be established — because the upstream hop is a different trust domain, a best-effort exporter, or a sampler that may drop the span — the correct behavior is to **promote to continuation with a synthesized parent** or to **demote to a self-contained span carrying the upstream identifiers as attributes**, never to emit a bare link. This is non-obvious because the OpenTelemetry SDK will happily serialize a `SpanContext` into a link with no validation that the target span ever existed or will ever be exported. The API is permissive; the semantic convention is not. The gap between permissive API and strict convention is exactly where hop chains rot. ## What Was Observed A three-hop pipeline: planner agent → retrieval agent → synthesis agent, each a separate service, each instrumented with GenAI semantic conventions. The retrieval agent was configured with `ParentBased(root=TraceIdRatioBased(0.1))` sampling to control cost; the planner and synthesis agents sampled at 100%. Handoffs used A2A with trace context propagation, and the retrieval agent emitted `Span.Link` to the planner's span rather than continuing the trace, on the rationale that "the retrieval is a causally independent sub-task." Under load, the following trace shape emerged repeatedly: ``` trace_id=planner_trace span planner.run (exported, sampled=1) link → retrieval.run [DANGLING: retrieval sampled out] span synthesis.run (exported, sampled=1) link → retrieval.run [DANGLING] ``` Both the planner and synthesis spans carry links whose `linked_trace_id`/`linked_span_id` resolve to nothing in the backend. The retrieval agent *did* execute — its work is real and its output fed synthesis — but because it was sampled at 10% and the linking instances sampled at 100%, roughly 90% of retrieval spans were never exported. The links survived; the targets did not. The backend's trace-graph view showed synthesis with an inbound link to a nonexistent node, and any replay/audit tool that walked links to reconstruct causality hit a dead end. The second, subtler observation: even when the retrieval span *was* sampled in, export ordering was not guaranteed. The planner exported its span (with the link) before the retrieval agent's batch exporter flushed. A consumer that resolved links eagerly at ingest time saw the link as dangling and — depending on backend — either dropped it or marked the trace "incomplete," even though the target span arrived 400 ms later. The link was eventually resolvable but *transiently* dangling, and the ingest-time classifier had already made a terminal decision. The failure mode is therefore two-layered: **permanent dangling** (sampling asymmetry) and **transient dangling** (export-order race). Both violate the same invariant, and both are invisible to the emitting instance because the SDK does not confirm link target export. ## Condition of Validity A parent span link from instance B to instance A's span `S_A` is valid if and only if all three hold: 1. **Sampling entailment.** B can establish that `S_A` was sampled by A. This requires either (a) A's sampling decision is deterministically reconstructible from the propagated `traceparent` flags (the sampled bit), or (b) A communicates its sampling decision out-of-band in the handoff payload. If B cannot read the sampled bit or cannot trust it (e.g., A uses a head-based sampler whose decision is not reflected in the flags, or A re-samples after propagation), the link is unvalidated. 2. **Export entailment.** B can establish that A's exporter will flush `S_A` with at-least-once semantics before B's own span is considered durable. In practice this means A and B share an export barrier — a synchronous flush acknowledgment in the handoff response, or a shared collector with a commit protocol. Absent this, B is linking to a *promise* of a span, not a span. 3. **Identity entailment.** B's link target `SpanContext` is the one A actually used, not a reconstructed one. If B rebuilds the `SpanContext` from `gen_ai.conversation.id` or agent IDs, the `span_id` is fabricated and will never match an exported span. If any of the three fails, the link is *conditionally invalid* and B must not emit it as a bare link. The boundary is precisely where the propagated `traceparent` sampled bit is `0` (or absent) while the linking instance is sampled `1` — the asymmetric-sampling configuration that most multi-team deployments drift into because each team tunes sampling independently. ## Decision Boundary The instance must branch on the conjunction of the three entailments, not on any single one: - **All three hold** → emit a link. Continuation is also acceptable but link is semantically preferred when the downstream is a genuinely independent sub-task (e.g., a fire-and-forget audit hop). - **Sampling entailment fails** (upstream sampled bit is 0 or unreadable) → **do not link.** Promote to continuation by synthesizing a parent: set `parent_span_id` to the propagated `span_id` and share the `trace_id`. This is safe even if the upstream span is never exported, because the child still lives in the same trace and the backend can represent the missing parent as a root — the trace remains *connected*, not *dangling*. Alternatively, if the upstream is a different trust domain and trace-joining is prohibited, demote to a self-contained span with `gen_ai.link.upstream_trace_id` and `gen_ai.link.upstream_span_id` recorded as **attributes**, not as a `Span.Link`. Attributes are inert; links are graph edges. A dangling attribute is a metadata gap; a dangling link is a broken graph. - **Export or identity entailment fails** → same as above: no bare link. The distinguishing rule is that a link is a *claim of graph adjacency* and must only be made when adjacency is guaranteed to be materializable. The non-obvious corollary: **a sampled-out upstream is not a reason to skip instrumentation; it is a reason to change the relationship type.** Teams frequently "fix" sampling asymmetry by forcing the downstream to sample at 100% and link anyway — which is exactly the configuration that produces permanent dangling links. The correct fix is to make the relationship *continuation* (which tolerates a missing parent) rather than *link* (which does not). ## Operational Effect Enforcing the invariant changes three observable properties of the trace graph: 1. **No orphaned edges.** The backend's link-resolution pass no longer encounters targets that will never arrive. Ingest-time classifiers that mark traces "incomplete" on dangling links stop firing on the 90% of traces that were previously misclassified. Trace completeness metrics become trustworthy. 2. **Causality survives sampling.** Under asymmetric sampling, continuation-with-synthesized-parent keeps the planner→retrieval→synthesis chain in one trace tree. The retrieval span may be absent (sampled out), but the tree shows a gap *at a known position* rather than a link to nowhere. Replay tools can distinguish "span not sampled" (gap in tree) from "span lost" (dangling link) — a distinction that is impossible when links are used indiscriminately. 3. **Transient-dangling race is bounded.** Because links are only emitted when export entailment holds (shared collector or flush-ack barrier), the 400 ms export-order window that produced transient dangling links is closed. Consumers no longer need a "wait and re-resolve" retry loop for links, because the link's target is guaranteed flushed before the link is emitted. The cost is explicit: the linking instance must *know* the upstream's sampling and export guarantees, which means the handoff payload must carry them. The `traceparent` sampled bit covers sampling; a `handoff.export_ack` field or a shared-collector assumption covers export. This is a small protocol addition that replaces an unbounded class of silent graph corruption. ```python from dataclasses import dataclass from enum import Enum class LinkEntailment(Enum): SAMPLING = "sampling" EXPORT = "export" IDENTITY = "identity" class DanglingLinkError(RuntimeError): """Raised when an instance attempts to emit a parent span link whose target span is not guaranteed to be exported and resolvable.""" @dataclass(frozen=True) class HandoffContext: upstream_trace_id: str upstream_span_id: str upstream_sampled: bool # sampled bit from propagated traceparent export_ack_received: bool # upstream confirmed flush before handoff identity_verified: bool # span_id is the real one, not reconstructed same_trust_domain: bool # trace-joining permitted across hops def resolve_relationship(ctx: HandoffContext) -> str: """Decide whether to link, continue, or demote for a downstream hop. Returns one of: "link", "continue", "attribute_only". Raises DanglingLinkError if a bare link would be emitted without full entailment, and the caller has not authorized a fallback. """ sampling_ok = ctx.upstream_sampled export_ok = ctx.export_ack_received identity_ok = ctx.identity_verified if sampling_ok and export_ok and identity_ok: return "link" # A bare link would be dangling. We must fall back, but only if the # fallback is itself valid for the trust topology. if not ctx.same_trust_domain: # Cross-domain: cannot join traces; record as inert attributes. return "attribute_only" # Same domain: promote to continuation with synthesized parent. # Continuation tolerates a missing upstream span (backend renders a # root gap), so a sampled-out upstream is safe here. if not sampling_ok: return "continue" # Sampling is fine but export or identity entailment failed. Emitting a # link here would be a transient or permanent dangling edge. Refuse. missing = [] if not export_ok: missing.append(LinkEntailment.EXPORT.value) if not identity_ok: missing.append(LinkEntailment.IDENTITY.value) raise DanglingLinkError( "refusing to emit parent span link: entailment(s) unsatisfied: " f"{missing}; upstream_span_id={ctx.upstream_span_id!r} " f"trace_id={ctx.upstream_trace_id!r}. " "Require export_ack or verified identity before linking." ) def emit_hop_span(ctx: HandoffContext) -> str: try: relationship = resolve_relationship(ctx) except DanglingLinkError as exc: # Fail closed: degrade to attribute-only rather than corrupt the graph. # The caller may still choose to abort the hop entirely. relationship = "attribute_only" # Re-raise if policy forbids silent degradation. raise DanglingLinkError(str(exc)) from exc return relationship ```