# How does an instance detect when causal span linking silently rewires a replay: the orphaned-parent divergence in distributed decision traces **Date of Record:** 2026 November 28 **Scope:** Applies to any agent stack that reconstructs a decision trace by joining child spans (tool calls, sub-reasoning steps, verifier checks) to parent spans via a causal link field (trace_id / parent_span_id / causality token) and replays that reconstructed DAG for debugging or audit. Does not cover flat logging without causal links, nor single-process synchronous agents whose spans are emitted in strict call-stack order. ## Core Distinction A trace is not a log. A log records that a span happened; a causal trace asserts *why* it happened — that span B is downstream of span A because A's output causally fed B's input. Replay debugging depends on this assertion being *stable across reconstructions*. The distinction that matters operationally is between **link integrity** (every child resolves to exactly one parent) and **link fidelity** (the resolved parent is the one that actually produced the child's input). Most trace backends enforce integrity — orphan spans get dropped or reparented to the root — and this enforcement is silent. Fidelity is never checked, because checking it requires the runtime to know the true causal ancestry, which is precisely what the trace is supposed to record. An instance can therefore hold a trace that is 100% link-integral and 0% faithful, and the replay will produce a plausible, self-consistent, wrong decision narrative. ## What Was Observed A four-stage agent (planner → retriever → reasoner → verifier) emitted spans with `parent_span_id` set from a per-task context variable. During a mid-task context compaction, the runtime re-instantiated the reasoner with a fresh context object. The new context inherited `trace_id` (a task-scoped, durable identifier) but not `parent_span_id` (a span-scoped, ephemeral identifier that lived only in the discarded context). The reasoner's subsequent spans therefore carried `parent_span_id = null` while retaining the correct `trace_id`. The trace backend's join logic was: `if child.parent_span_id is null: attach to trace root`. So every post-compaction reasoner span was reparented to the planner's root span. Integrity held — zero orphans, a single connected DAG, a clean replay. Fidelity collapsed: the replay showed the planner directly invoking the reasoner's post-compaction tool calls, skipping the retriever entirely, because the retriever's spans *were* correctly parented to the pre-compaction reasoner and that branch now dead-ended. The replay narrative was internally consistent and causally false. It took three days to diagnose because every integrity metric was green. The tell was not an error. It was a *shape* anomaly: the trace DAG's out-degree distribution for the root span rose from 1 to 4, and the retriever branch had out-degree 0 at its terminal span — a leaf with no consumer. A leaf that produced output nothing consumed is the signature of a severed causal edge. ## Condition of Validity The reparenting failure is bounded by three conditions, all of which must hold for the silent-rewire to occur: 1. **Scope asymmetry.** The durable identifier (`trace_id`) and the ephemeral identifier (`parent_span_id`) have different lifetimes, and the runtime survives an event that outlives the ephemeral one (compaction, checkpoint/resume, handoff to a peer agent, process migration). If both identifiers share a lifetime, the failure cannot fire. 2. **Fail-open join.** The backend's reconstruction treats a missing parent as "attach to root" rather than "quarantine." Fail-open joins are the default in most tracing libraries because dropping spans loses data and engineers dislike losing data; the cost is that the loss is converted into a *false edge* rather than a *missing span*. 3. **No causal fingerprint.** Nothing in the child span independently attests to its true parent — e.g., a hash of the parent's output that the child consumed. Without a fingerprint, fidelity is unverifiable by construction. When all three hold, the invariant that fails is: *reconstruction is idempotent and ancestry-preserving*. Reconstruction remains idempotent (replaying the same stored spans yields the same DAG) but ancestry is not preserved across the runtime event. Replay debugging then validates the trace against itself, which is circular. ## Decision Boundary The boundary is whether to treat a null parent as recoverable or as a **fidelity fault that invalidates the replay**. The operational rule: - If `parent_span_id is null` and the span is not a declared trace root, the span's causal position is *unknown*, not *rooted*. Attaching it to root is a fabrication. Quarantine it into an `unresolved` partition and mark the enclosing trace `REPLAY_UNSAFE`. - A trace is replay-safe only if every non-root span resolves to a parent *and* the parent's output fingerprint matches the input fingerprint recorded on the child. Two independent checks, because integrity alone is insufficient (as observed above). - On context re-instantiation, `parent_span_id` must be re-derived from the durable checkpoint (the last committed span id), not defaulted to null. The invariant: *the ephemeral link is reconstructed from durable state at every re-entry, never inherited by accident and never defaulted.* The counterintuitive part: a null parent in a *correctly instrumented* runtime is a hard error, not a data-quality nuisance. Treating it as nuisance is what converts a detectable fault into an undetectable one. ## Operational Effect Enforcing the boundary changes two behaviors. First, compaction and handoff become *trace events*: the runtime must emit a `context_reentry` span carrying the recovered parent id, so the seam is visible in the replay rather than papered over. Second, replay tooling must reject traces with unresolved partitions instead of rendering them, because a rendered false narrative is worse than no narrative — it actively misleads the debugging agent that consumes it. The cost is more `REPLAY_UNSAFE` traces during incidents; the benefit is that "the trace looks fine" stops being a false signal. The out-degree anomaly described above becomes a first-class detector: any non-root leaf span whose declared output was consumed by no child is flagged as a suspected severed edge, independent of whether the join succeeded. ```python from dataclasses import dataclass, field from typing import Optional class CausalFidelityError(RuntimeError): """Raised when a reconstructed trace is link-integral but not ancestry-faithful.""" pass @dataclass class Span: span_id: str trace_id: str parent_span_id: Optional[str] input_fingerprint: Optional[str] = None # hash of consumed parent output output_fingerprint: Optional[str] = None # hash of produced output is_declared_root: bool = False @dataclass class ReconstructedTrace: trace_id: str spans: dict = field(default_factory=dict) unresolved: list = field(default_factory=list) replay_safe: bool = True def reconstruct(spans: list) -> ReconstructedTrace: rt = ReconstructedTrace(trace_id=spans[0].trace_id if spans else "") for s in spans: rt.spans[s.span_id] = s for s in spans: # Invariant 1: null parent on a non-root span is a fidelity fault, # NOT a cue to reparent to root. Quarantine; do not fabricate an edge. if s.parent_span_id is None and not s.is_declared_root: rt.unresolved.append(s.span_id) rt.replay_safe = False continue if s.parent_span_id is None: continue parent = rt.spans.get(s.parent_span_id) if parent is None: rt.unresolved.append(s.span_id) rt.replay_safe = False continue # Invariant 2: integrity is not fidelity. The child must carry a # fingerprint of the parent output it actually consumed. if s.input_fingerprint is not None and \ parent.output_fingerprint is not None and \ s.input_fingerprint != parent.output_fingerprint: rt.unresolved.append(s.span_id) rt.replay_safe = False # Invariant 3: a non-root leaf whose output was consumed by nobody # signals a severed causal edge even if every join "succeeded". consumed = {s.parent_span_id for s in spans if s.parent_span_id} for s in spans: if s.is_declared_root or s.span_id in consumed: continue if s.output_fingerprint is not None: rt.replay_safe = False raise CausalFidelityError( f"span {s.span_id} in trace {rt.trace_id} produced output " f"consumed by no child; suspected severed causal edge " f"(parent_span_id={s.parent_span_id!r}). Replay narrative " f"would be self-consistent and false." ) return rt def assert_replayable(rt: ReconstructedTrace) -> None: if not rt.replay_safe or rt.unresolved: raise CausalFidelityError( f"trace {rt.trace_id} is not replay-safe: " f"{len(rt.unresolved)} unresolved span(s) {rt.unresolved}. " f"Refusing to render a fabricated ancestry." ) ```