# Why does an instance lose causal parentage when a delegated subagent re-emits a retried span under a new trace identity **Date of Record:** 2026 November 30 **Scope:** Applies to OpenTelemetry-style trajectory tracing where an orchestrator agent spawns delegated subagent instances that emit their own spans, and where span context is propagated via injected headers into tool/runtime boundaries. Does not apply to single-process in-memory tracing, nor to systems that use a shared deterministic trace ID factory across all instances. ## Core Distinction The failure is not "missing spans" or "broken export." It is a *causal parentage break*: the delegated subagent's spans are successfully emitted and exported, but their `parent_span_id` points to a span that no longer exists in the trace tree because the orchestrator's retry logic re-created the delegation span under a fresh `span_id` while the subagent continued using the *stale* propagated context from the first attempt. Two distinct identities are conflated in naive designs: - **Trace identity** (`trace_id`): the logical unit of work — one user task, one trajectory. Should be stable across retries. - **Span identity** (`span_id` + `parent_span_id`): the causal edge — "this work happened *because of* that work." Must be re-bound on every retry, because the retried attempt is a *new* causal event. The invariant that breaks: *every emitted span must reference a parent span that is present and causally prior in the same trace.* Retries violate this silently because the child is emitted against a context snapshot that was captured before the parent was re-minted. ## What Was Observed A planner agent (`orchestrator`) decomposed a task and delegated a retrieval subtask to a subagent via an A2A-style message. The delegation call was wrapped in a span `S_delegate` with `span_id=A1`, `trace_id=T`. The subagent received the W3C `traceparent` header `00-T-A1-01` and, on startup, cached it as its ambient context. The subagent's first attempt timed out at the transport layer (30s budget exhausted mid-tool-call). The orchestrator's retry policy fired: it closed `S_delegate(A1)` with `status=ERROR`, then opened a **new** delegation span `S_delegate(A2)` under the *same* `trace_id=T` and re-sent the request. Critically, the retry path re-serialized the *original* request payload — including the original `traceparent` header captured at message construction time — rather than re-injecting the current context. The subagent, on the second attempt, emitted its internal spans with `parent_span_id=A1`. But `A1` had already been closed and, in this backend's sampler, dropped from the retained trace (tail sampling kept only the error span's summary, not its full record). The result: ``` T ├── A1 S_delegate (ERROR, dropped by tail sampler) │ └── (subagent spans orphaned: parent=A1, not present) └── A2 S_delegate (OK) └── (no children recorded) ``` The trace *looked* successful (`A2` OK) but had zero causal descendants. The subagent's actual work — the retrieval, the tool calls, the token accounting — was exported as orphaned spans under a parent that the sampler had evicted. Downstream causal analysis reported the delegation as "succeeded with no observed work," and the trajectory's cost attribution undercounted by the entire subagent budget. A second, subtler variant appeared when the subagent itself retried an internal tool call: it re-emitted a span with the *same* `span_id` as the failed attempt (because the subagent's retry wrapper reused the span object). The backend rejected the duplicate `span_id` under `T` and dropped the second emission, so the successful tool result had no span at all — a *silent* loss, not an orphan. ## Condition of Validity The parentage invariant holds **only if** the following three conditions are simultaneously true: 1. **Context injection is re-evaluated at send time, not at payload-construction time.** The `traceparent` header must be injected by the transport layer immediately before the outbound call, reading the *current* active span. Any design that serializes the header into a cached request object violates this. 2. **Retry creates a new span identity but preserves trace identity.** The retried attempt is a new causal node (`A2`), and the child must be told `A2`, not `A1`. Preserving `trace_id` is correct and necessary for trajectory continuity; preserving `span_id` is the bug. 3. **The sampler retains any span that is referenced as a parent by a retained span.** Tail sampling that evicts `A1` while keeping its children orphans them. Parent-referenced retention (or head sampling with forced retention on error) is required. If any one of these fails, the trace remains *structurally valid* (it parses, spans export) but *causally invalid* (edges point to absent nodes). This is the dangerous regime: no error is raised, and the observability system reports success. ## Decision Boundary The boundary between "acceptable" and "must abort" is whether the orphaned child can be *re-parented* deterministically. Two cases: - **Re-parentable:** the subagent includes a stable `delegation_id` (a logical, retry-invariant correlation token) in its span attributes. On ingest, a reconciler can re-link `parent_span_id=A1` to `A2` by matching `delegation_id`. This is a *repair* path and is acceptable if the reconciler runs before trajectory analysis. - **Non-re-parentable:** no retry-invariant correlation token exists. The orphan is permanently detached; causal analysis and cost attribution are silently wrong. This must be treated as a **hard observability failure**, not a warning — the trajectory record is not trustworthy and downstream verification (e.g., an auditor agent replaying the trajectory) will reach a different conclusion than the original run. The decision rule: *if a delegated span's parent is absent AND no retry-invariant correlation attribute is present, the trajectory must be quarantined rather than analyzed.* Silent acceptance of orphaned spans is the failure mode; the guardrail is to fail closed. ## Operational Effect Concretely, the following must be enforced in the delegation transport and the ingest pipeline: - **Inject-at-send:** the outbound request builder must call `current_span().inject(headers)` inside the retry loop body, after the new span is opened. Never cache the header. - **New span per attempt:** the retry wrapper must create a fresh child span for each attempt and pass *its* context down. The failed attempt's span stays closed and error-tagged; it is not reused. - **Retry-invariant correlation token:** every delegation carries a `delegation_id` (UUID minted once per logical delegation, before the retry loop) as a span attribute on both the orchestrator's delegation span and the subagent's root span. This is the re-parenting key. - **Parent-referenced retention:** the sampler must retain any span referenced as a `parent_span_id` by a retained span, or force-retain all error-tagged spans for the trace's full lifetime. - **Ingest-time orphan check:** the collector rejects (quarantines) any span whose `parent_span_id` is not present in the trace and which lacks a `delegation_id`; it logs the quarantine as a trajectory-integrity violation, not a transport warning. The failure is silent by construction — the whole point is that the trace *looks* fine. The guardrail must therefore be an active, fail-closed assertion at both send time and ingest time, not a passive dashboard. ```python from dataclasses import dataclass, field from typing import Optional class OrphanedDelegationSpan(RuntimeError): """Raised when a delegated subagent span references an absent parent span and carries no retry-invariant correlation token to re-parent it.""" pass @dataclass class Span: trace_id: str span_id: str parent_span_id: Optional[str] name: str delegation_id: Optional[str] = None status: str = "OK" attributes: dict = field(default_factory=dict) def inject_context_at_send(current_span: Span, headers: dict) -> dict: """Must be called inside the retry loop, after the new attempt span is opened. Caching this result across attempts is the root-cause bug.""" headers = dict(headers) headers["traceparent"] = ( f"00-{current_span.trace_id}-{current_span.span_id}-01" ) return headers def validate_trajectory(spans: list[Span]) -> None: """Fail-closed check: every span's parent must be present, unless the span carries a delegation_id that permits deterministic re-parenting.""" present = {s.span_id for s in spans} for s in spans: if s.parent_span_id is None: continue # trace root if s.parent_span_id not in present: if s.delegation_id is None: raise OrphanedDelegationSpan( f"span {s.span_id} ({s.name}) references absent parent " f"{s.parent_span_id} in trace {s.trace_id} and has no " f"delegation_id for re-parenting; trajectory untrustworthy" ) # Re-parentable: reconciler will re-link via delegation_id. s.attributes["reparent_pending"] = s.delegation_id def simulate_retry_without_reinjection() -> None: trace = "T" # Attempt 1: delegation span A1, subagent caches header, times out. a1 = Span(trace, "A1", None, "delegate", status="ERROR") stale_headers = inject_context_at_send(a1, {}) # captured at construction # Attempt 2: orchestrator mints A2 but re-sends the STALE cached header. a2 = Span(trace, "A2", None, "delegate", status="OK") # Subagent emits child against stale A1 (now evicted by tail sampler). child = Span(trace, "C1", parent_span_id="A1", name="subagent.retrieve") # A1 dropped from retained trace; only A2 + C1 survive. validate_trajectory([a2, child]) # raises if __name__ == "__main__": simulate_retry_without_reinjection() ```