# What structural signals indicate when causal trace stitching across prompt, completion, and tool spans breaks under parent-id reassignment **Date of Record:** 2026 December 4 **Scope:** Applies to agent observability pipelines that reconstruct causal traces by correlating prompt spans, completion spans, and tool-call spans via parent/child span identifiers emitted at runtime. Does not apply to single-shot, non-tool-using LLM calls where the trace is a flat two-span tree. ## Core Distinction There is a difference between *trace completeness* and *trace causality*. A pipeline can report 100% span coverage—every prompt, completion, and tool invocation appears in the store—while the reconstructed causal graph is wrong. The failure is not missing data; it is misattributed edge direction. Specifically, the boundary condition examined here is **parent-id reassignment under concurrent tool fan-out**, where a single completion span spawns multiple tool spans and the runtime re-issues or mutates the parent span id between emission and ingestion. The trace looks complete but the DAG is stitched to the wrong parent, so a downstream verifier attributes a tool result to a completion that did not produce it. ## What Was Observed The trigger is a completion span that emits N parallel tool-call requests within one generation turn. Each tool span carries `parent_span_id = `. The runtime, on retry or on a streaming re-chunk, reassigns the completion span a new `span_id` (common when the completion is split across two transport frames and the second frame opens a fresh span context). The tool spans already emitted still reference the *old* completion span id. At ingestion, three structural signals appear together and are the diagnostic fingerprint: 1. **Orphaned parent references with non-zero child count.** Tool spans reference a `parent_span_id` that does not exist as a span row, but those same tool spans have valid `trace_id` matching an existing completion. Coverage is 100%; referential integrity is broken. 2. **Timestamp inversion across the reassignment seam.** The reassigned completion span's `start_time` is later than the tool spans it supposedly spawned. A completion cannot begin after its own tool calls. This inversion is the single highest-signal indicator and appears even when span ids are opaque. 3. **Duplicate completion spans with disjoint child sets.** The old and new completion span ids both exist in the store; tool spans split unevenly between them. The trace has two roots for one logical turn. The critical non-obvious point: signal 1 alone is common and benign (sampling drops parents). Signal 2 alone can occur from clock skew. Signals 1, 2, and 3 co-occurring within one `trace_id` is the conjunction that indicates reassignment, not sampling or skew. ## Condition of Validity This diagnosis is valid only when the following hold: - The runtime emits `span_id` and `parent_span_id` as separate fields from `trace_id`. If the pipeline derives parentage purely from temporal nesting, reassignment is invisible and this analysis does not apply. - Tool spans are emitted *before* the completion span finalizes (streaming or eager emission). Batch emission at turn end collapses the seam and hides the failure. - Clock skew between the tool executor and the completion host is bounded and known. Unbounded skew makes signal 2 unusable. When emission is deferred to turn end, the reassignment still happens but the pipeline re-parents correctly because it has the final id; the failure is latent and only manifests if a mid-turn checkpoint is read. ## Decision Boundary The boundary is whether to **trust the emitted parent id** or **re-derive parentage from temporal containment plus trace id**. The rule: - If signals 1+2+3 co-occur, do not trust emitted `parent_span_id`. Re-derive: within a `trace_id`, assign each tool span to the completion span whose `[start_time, end_time]` interval contains the tool span's interval, breaking ties by the completion with the most overlapping children. - If only signal 1 occurs, treat as sampling loss and preserve emitted parentage; do not re-derive, because re-derivation on incomplete data fabricates edges. - If re-derivation yields more than one candidate container per tool span, the trace is ambiguous and must be quarantined, not silently repaired. Silent repair is the failure mode that lets a verifier replay a tool result against the wrong completion. The invariant: **a tool span's causal parent must temporally contain it, and no completion span may be finalized before all its claimed children.** Violation of either clause is a hard reject, not a warning. ## Operational Effect Downstream, a mis-stitched trace poisons verification replay: an auditor agent replaying the trace sees a tool result attached to a completion that never requested it, concludes the tool output was hallucinated by the model, and flags a false-positive policy violation. The blast radius is every trace that passed through a streaming re-chunk with parallel tool fan-out—typically the highest-value, most-tooled agent turns. The guardrail is a pre-ingestion stitching validator that enforces the containment invariant and quarantines ambiguous traces with an explicit reason code, so the verifier sees "trace unstitchable" rather than a confidently wrong causal graph. ```python from dataclasses import dataclass from typing import Optional class TraceStitchError(RuntimeError): """Raised when causal parentage cannot be established without fabrication.""" @dataclass class Span: span_id: str parent_span_id: Optional[str] trace_id: str kind: str # "prompt" | "completion" | "tool" start_time: float end_time: float def stitch_trace(spans: list[Span]) -> dict[str, str]: by_id = {s.span_id: s for s in spans} completions = [s for s in spans if s.kind == "completion"] tools = [s for s in spans if s.kind == "tool"] orphaned = [t for t in tools if t.parent_span_id not in by_id] inversions = [ t for t in tools if t.parent_span_id in by_id and by_id[t.parent_span_id].start_time > t.start_time ] dup_roots = len({c.span_id for c in completions}) > 1 and bool(tools) reassignment = bool(orphaned) and bool(inversions) and dup_roots if reassignment: edges: dict[str, str] = {} for t in tools: containers = [ c for c in completions if c.start_time <= t.start_time and c.end_time >= t.end_time ] if len(containers) != 1: raise TraceStitchError( f"ambiguous causal parent for tool span {t.span_id}: " f"{len(containers)} containing completions in trace {t.trace_id}" ) edges[t.span_id] = containers[0].span_id return edges if orphaned and not inversions: # sampling loss: preserve emitted parentage, do not re-derive return {t.span_id: t.parent_span_id for t in tools if t.parent_span_id} if inversions and not orphaned: raise TraceStitchError( "temporal inversion without orphan: clock skew suspected, refusing to stitch" ) return {t.span_id: t.parent_span_id for t in tools if t.parent_span_id} ```