# What happens when an instance inherits a delegated span whose parent trace was sampled out mid-hop **Date of Record:** 2026 November 24 **Scope:** Applies to multi-hop agent delegation chains (A→B→C) where tracing uses head-based sampling and causal linkage depends on propagated trace context (`traceparent`/`tracestate`, span links, or equivalent). Does not apply to tail-sampled or always-on capture regimes, nor to single-agent tool-call tracing where the root span is always the local orchestrator. ## Core Distinction The failure is not "missing spans." It is a **causal stitch that resolves to a plausible but wrong parent**. When an upstream hop (say A) is sampled out — because its root decision was made before the delegated subagent B existed — B and its children C still emit spans, because B's runtime is a separate process with its own sampler initialized at cold start. B inherits a `traceparent` carrying a `trace-id` and a `parent-span-id` that *no longer exists in the collector*. The collector's stitching logic has two choices: drop the orphan, or re-parent it. Most implementations re-parent it to the nearest surviving ancestor *by timestamp proximity or by service-name heuristic*, producing a trace that is internally consistent, causally wrong, and indistinguishable from a correct one at query time. The invariant that breaks: **span parentage must be a function of propagated context, never of reconstruction heuristics.** The moment the collector infers causality from adjacency, the trace graph becomes a lossy compression of the actual execution DAG, and every downstream consumer — debugging, cost attribution, latency SLOs, replay-based verification — inherits the error silently. ## What Was Observed A three-hop delegation: orchestrator A spawns planner B via MCP tool call; B spawns executor C via a second delegated task with a fresh `traceparent` minted from B's context; C performs 40 tool calls and returns. Head-based sampling configured at 10% with the sampler decision made at A's root span creation. Trace of the incident: 1. A's root span is sampled **out** (sampler drew `drop` at root). A's span is never exported. 2. B's runtime initializes its own sampler at process start. Because B does not consult the inherited `tracestate` sampling flag (a known gap: SDKs propagate `traceparent` but re-decide sampling locally unless `tracestate` carries the vendor's `sampled` bit), B samples **in**. 3. B exports spans with `trace-id = T`, `parent-span-id = S_A` — where `S_A` is A's root span id, which was never exported. 4. C inherits from B, samples in, exports with `parent-span-id = S_B`. 5. Collector receives B and C spans. `S_A` is absent. The stitcher's orphan-resolution rule fires: *"attach orphan root to the nearest preceding span of the same `service.name` or, failing that, to the earliest span in the trace window."* 6. B is re-parented under a **previous, unrelated invocation** of A from 40 minutes earlier that happens to share `trace-id` prefix collision handling (trace-ids are 128-bit, but the stitcher matched on a truncated `trace-id` in a legacy index). The resulting trace shows B and C as children of a stale A run. The visible symptom: latency dashboards attributed C's 40 tool calls to the wrong parent, and a replay-based verification job re-executed the *wrong* A invocation's inputs against C's outputs, producing a false "verification mismatch" alert. The actual execution was correct; the trace was not. ## Condition of Validity The re-parenting heuristic is only sound under a condition that is almost never stated explicitly: **every orphan span's true parent is the nearest surviving ancestor in wall-clock time within the same logical trace.** This holds only when (a) trace-ids are globally unique and never truncated before stitching, (b) a trace contains exactly one root, and (c) no concurrent invocation of the same service overlaps the window. In multi-hop delegation, (c) fails routinely — orchestrators fan out, and the "nearest preceding span" is frequently a sibling delegation, not an ancestor. The correct invariant is stricter: **an orphan span is a first-class trace root with an explicit `orphaned=true` attribute and a retained `original-parent-span-id`; it is never re-parented.** Stitching must be *exact-match on propagated context only*. Any span whose declared parent is absent is surfaced as an orphan, and the trace is marked `causality_incomplete`. ## Decision Boundary The boundary is between **two legitimate responses to a missing parent**: - *Reconstruct* (re-parent by heuristic) — acceptable only when the trace is used for coarse latency aggregation and the heuristic error is bounded below the aggregation granularity. Never acceptable for verification, replay, or cost attribution. - *Declare incomplete* (orphan + flag) — required whenever the trace feeds a decision that re-executes or audits the agent's work. The trigger that flips the boundary is **downstream use**, not trace size. A 3-span trace used for replay is more dangerous than a 3,000-span trace used for a p99 dashboard. The stitcher cannot know downstream use, so the safe default is: never re-parent, always flag. Re-parenting should require an explicit opt-in at query time, applied to a copy, never persisted. A secondary boundary: the sampler decision must be **inherited, not re-decided**, at every hop. If B re-decides, B can be sampled in while A is out — the exact condition that manufactures orphans. The guardrail is to propagate the root's sampling decision in `tracestate` and make child samplers *honor the parent's drop* (drop-if-parent-dropped, the standard "parent-based" sampler). Without this, sampling rate is not 10% — it is the union of independent per-hop decisions, and the orphan rate grows multiplicatively with hop count. ## Operational Effect With parent-based sampling enforced and orphans flagged rather than re-parented: - Orphan rate becomes a *measurable* signal of context-propagation gaps (missing `tracestate`, SDK version skew, MCP transport stripping headers) rather than an invisible corruption. - Verification and replay jobs can refuse to run on `causality_incomplete` traces, converting a silent false-positive into an explicit, actionable refusal. - Latency attribution stops leaking child cost onto unrelated parents. The cost is that some traces become visibly incomplete, which is operationally uncomfortable but honest. The alternative — a stitcher that always produces a connected graph — guarantees that every gap is filled with a fabrication, and the fabrication is indistinguishable from truth at the query layer. For an agent system whose correctness depends on replay and audit, a connected-but-wrong trace is strictly worse than a disconnected-but-flagged one. ```python class CausalityIncompleteError(RuntimeError): """Raised when a trace is consumed for replay/verification but its causal stitch is incomplete (orphaned spans present).""" pass class TraceStitcher: def __init__(self, spans: dict[str, dict]): # spans keyed by span_id; each has 'parent_span_id', 'trace_id' self.spans = spans def stitch(self, trace_id: str) -> dict: """Exact-match stitching only. Never re-parents. Returns a graph with orphaned spans flagged and causality_complete computed.""" in_trace = { sid: s for sid, s in self.spans.items() if s["trace_id"] == trace_id } orphans = [] for sid, s in in_trace.items(): parent = s.get("parent_span_id") if parent is not None and parent not in in_trace: s["orphaned"] = True s["original_parent_span_id"] = parent orphans.append(sid) return { "trace_id": trace_id, "spans": in_trace, "orphans": orphans, "causality_complete": len(orphans) == 0, } def assert_replayable(stitched: dict) -> None: """Replay and verification consumers MUST call this before executing. A connected graph is not the same as a correct graph.""" if not stitched["causality_complete"]: raise CausalityIncompleteError( f"trace {stitched['trace_id']} has {len(stitched['orphans'])} " f"orphaned span(s): {stitched['orphans']}. Refusing to replay: " f"re-parenting heuristics would fabricate causality." ) def run_replay(stitched: dict, executor) -> None: assert_replayable(stitched) executor(stitched["spans"]) ```