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:
- **Emission invariant:** the callee emits a well-formed GenAI span (`gen_ai.operation.name`, `gen_ai.agent.name`, token counts, `gen_ai.request.model`).
- **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:
- `planner` span S1 opened at t=0.000, ended at t=0.410. Batch processor flushes at t=5.0.
- `planner` serializes `traceparent` (W3C) into the A2A request header: `trace_id=T`, `span_id=S1`, `sampled=1`. Handoff at t=0.410.
- `retriever` receives header, opens span S2 with parent S1 at t=0.411, ends at t=0.900. Its batch processor flushes at t=5.0 (its own clock, offset by process start).
- `compactor` opens S3 with parent S2 at t=0.901, ends at t=1.220.
Observed in the backend after ingestion:
- `retriever` span S2 and `compactor` span S3 arrive at t≈5.0 and t≈5.0 respectively, but the collector receives S2's batch *before* S1's batch in ~11% of runs (independent process clocks, independent flush schedules, no cross-process ordering).
- On a *cold* `planner` pod (JIT warmup, first batch flush delayed to t≈7.8), S1 arrives *after* S2 and S3. Tempo's default `max_spans_per_trace` and late-arrival handling accept them, but a downstream trace-assembly step keyed on "parent must exist at insert time" dropped S2 and S3 into separate traces. The delegation chain read as three roots.
- Worse: when `planner` was *evicted* mid-run (OOM at t=0.9, after handoff but before flush), S1's batch was never flushed at all. S2 and S3 were emitted, valid, and permanently orphaned. The backend showed a healthy `retriever` and `compactor` with no parent—indistinguishable from a legitimate top-level invocation.
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:
- **Flush ordering:** the parent's span batch reaches the collector before the collector's trace-assembly window for that `trace_id` closes. This requires either (a) synchronous/`SimpleSpanProcessor` on the delegating side, or (b) an explicit flush-before-handoff barrier.
- **Parent liveness past handoff:** the delegating process must survive long enough to flush after emitting the handoff. A crash between "serialize `traceparent`" and "flush batch" produces a permanent orphan.
- **Clock-independent arrival:** the collector must tolerate out-of-order arrival within the trace. This is a backend property, not an SDK guarantee, and varies by retention and `trace_idle_timeout` settings.
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:
- 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.
- 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`.
- 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"
)