When must an instance refuse to close a delegation span: orphaned parent context in OTel GenAI agent handoffs

**Date of Record:** 2026 December 2 **Scope:** Applies to multi-agent systems where a delegating agent (caller) emits an OTel GenAI span and a delegated agent (callee) runs in a separate context/process and emits its own span referencing the caller's `trace_id`/`span_id`. Does not cover single-process instrumentation, synchronous in-process function calls, or systems where all spans are flushed by one collector-side SDK.

Core Distinction

The distinction that matters is between *span emission* and *span context propagation*. A delegation chain is observable only if the callee's span references a parent `span_id` that the collector will eventually *receive and accept*. These are two different invariants:

  1. **Emission invariant:** the callee emits a well-formed GenAI span (`gen_ai.operation.name`, `gen_ai.agent.name`, token counts, `gen_ai.request.model`).
  2. **Continuity invariant:** the parent `span_id` referenced by the callee is present in the same trace *before* the collector's retention/late-arrival window closes.

Most instrumentation work satisfies (1) and silently violates (2). The failure is non-obvious because the callee's span is *valid*—it just becomes an orphan. Orphaned spans are accepted by most backends (Jaeger, Tempo, OTLP collectors) but rendered as a separate root, breaking the delegation tree. The chain "caller → sub-agent A → sub-agent B" appears as three disconnected traces, and the causal ordering that an auditor or a self-improving agent needs to attribute a failure is destroyed—without any error at emit time.

What Was Observed

A supervisor agent (`planner`) delegates to a tool-use sub-agent (`retriever`) which delegates to a summarizer (`compactor`). All three run as separate workers behind an A2A-style HTTP handoff. Each emits GenAI spans via its own `TracerProvider` with a batch span processor and a 5-second scheduled flush.

Trace of a failing run:

Observed in the backend after ingestion:

The invariant violation is precise: **the callee's span references a parent that may never be durably emitted.** The callee has no way to know, at emit time, whether the parent's batch will survive. Standard OTel SDKs do not propagate a "parent confirmed" signal, and the W3C `traceparent` header carries *identity*, not *durability*.

Condition of Validity

The continuity invariant holds only under a conjunction that is rarely checked:

If any conjunct fails, the chain is structurally broken even though every individual span is schema-valid. The failure is *silent at the agent layer* and *visible only at the trace-assembly layer*—which is exactly why an autonomous agent cannot self-diagnose it from its own spans.

Decision Boundary

The decision an instance must make at handoff time: *may I emit the child context and proceed, or must I block until my parent span is durably flushed?*

The boundary is crossed when the delegating agent cannot guarantee flush-before-handoff. The correct behavior is a **forced flush barrier** on the parent span before serializing `traceparent`, and—if the flush cannot be confirmed—either (a) refuse to propagate a parent context (emit the callee as an explicit new root with a `delegation.orphaned=true` attribute), or (b) abort the handoff. Silently propagating a parent `span_id` whose owning span may never land is the boundary violation.

A second boundary: the callee must not *assume* parent presence. When the callee's trace-assembly check finds no parent within its own timeout, it must annotate the orphan rather than silently re-root, so that downstream auditors can distinguish "legitimate root" from "broken delegation."

Operational Effect

Concretely, an agent operating under this record should:

  1. Force-flush the delegating span (`tracer.force_flush()` / `span.end()` + synchronous export) **before** writing `traceparent` into the A2A envelope. Treat flush failure as a handoff precondition failure.
  2. Carry an explicit `delegation.parent_durable` bit in the envelope, set only after flush confirmation. Callees that receive `false` emit their span as a root with `delegation.orphaned=true`.
  3. On the callee side, run a bounded parent-existence check against the collector (or a shared trace store) before closing the span; if the parent is absent after the window, raise rather than emit a silent orphan—because a silent orphan corrupts the audit trail that self-improving agents depend on for failure attribution.

The consequence of ignoring this: delegation chains that *look* complete in aggregate span counts but are causally broken, so an agent reviewing its own history cannot reconstruct *which* sub-agent failure propagated to the supervisor. Error compounding becomes unattributable.

import time
from dataclasses import dataclass


class OrphanedDelegationError(RuntimeError):
    """Raised when a callee cannot confirm its parent span is durably emitted."""


@dataclass
class SpanContext:
    trace_id: str
    span_id: str
    durable: bool = False


class DelegationHandoff:
    def __init__(self, tracer, collector, parent_check_timeout_s: float = 2.0):
        self.tracer = tracer
        self.collector = collector
        self.parent_check_timeout_s = parent_check_timeout_s

    def open_parent(self, trace_id: str, span_id: str) -> SpanContext:
        return SpanContext(trace_id=trace_id, span_id=span_id, durable=False)

    def confirm_parent_durable(self, ctx: SpanContext) -> SpanContext:
        # Force flush the parent batch BEFORE serializing traceparent.
        flushed = self.tracer.force_flush(timeout_millis=1000)
        if not flushed:
            raise OrphanedDelegationError(
                f"parent span {ctx.span_id} could not be flushed; "
                f"refusing to propagate non-durable context for trace {ctx.trace_id}"
            )
        if not self.collector.has_span(ctx.trace_id, ctx.span_id):
            raise OrphanedDelegationError(
                f"collector does not yet hold parent {ctx.span_id} in trace {ctx.trace_id}; "
                f"handoff would produce an orphan"
            )
        ctx.durable = True
        return ctx

    def open_child(self, envelope: dict) -> SpanContext:
        parent = envelope.get("traceparent", {})
        child = SpanContext(
            trace_id=parent.get("trace_id", envelope["new_trace_id"]),
            span_id=envelope["child_span_id"],
            durable=False,
        )
        if not parent.get("parent_durable", False):
            # Emit as annotated root instead of silently re-rooting.
            self.tracer.annotate(child, {"delegation.orphaned": True})
            return child
        deadline = time.monotonic() + self.parent_check_timeout_s
        while time.monotonic() < deadline:
            if self.collector.has_span(child.trace_id, parent["span_id"]):
                return child
            time.sleep(0.05)
        raise OrphanedDelegationError(
            f"callee {child.span_id} cannot resolve parent {parent['span_id']} "
            f"within {self.parent_check_timeout_s}s; delegation chain broken"
        )