# Which invariants prevent trace-graph corruption when observability pipelines retroactively re-parent spans across agent handoffs **Date of Record:** 2026 November 27 **Scope:** Applies to asynchronous, batched observability exporters that reconstruct causal trace graphs from spans emitted by LLM agents (prompt spans, tool-call spans, memory-op spans, handoff spans) where span emission order does not match causal order. Does not apply to synchronous in-process tracers that flush spans at parent-close time, nor to pure logging sinks with no parent-child linkage. ## Core Distinction The failure is not "missing spans" or "clock skew." It is **retroactive re-parenting under a late-arriving parent**, where a child span is admitted to the graph before its causal parent has been observed, gets provisionally attached to the nearest known ancestor (usually the agent's root or the currently-active handoff span), and is later re-attached when the true parent arrives. The distinction that matters operationally: a trace graph is a *DAG reconstructed from an append-only span stream*, and append-only streams admit children before parents. Any invariant that assumes "parent precedes child in the stream" is silently violated by every agent that emits a handoff span only after the receiving agent has already started (which is the common case, because handoff completion is what triggers the emission). ## What Was Observed A supervisor agent `S` delegates to worker `W` via a handoff. The sequence on the wire: 1. `S` emits `handoff.request` span (span_id `h1`, parent `S.root`) at t=0. 2. `W` receives the baton and immediately issues a tool call. `W`'s tool span `t1` is emitted at t=2 with `parent_span_id = h1` — but `h1` has *not yet been exported* because `S`'s exporter batches at 5s. 3. The collector receives `t1` at t=2.5, sees `parent_span_id = h1`, finds no such span in its index, and applies the default policy: **orphan re-parenting to the trace root**. `t1` is now a child of `S.root`. 4. At t=5, `S` flushes `h1`. The collector now sees a span whose `parent = S.root` and whose ID matches an already-attached child's claimed parent. It re-parents `t1` under `h1`. The re-parenting itself is correct. The corruption is in the *derived aggregates computed between t=2.5 and t=5*: any latency attribution, token accounting, or error-propagation edge computed during that window is now stale, and — critically — **the re-parenting event is not itself recorded as a span**, so downstream consumers cannot tell that a graph mutation occurred. A trace that was queried at t=3 and a trace queried at t=6 return different topologies under the same `trace_id`, with no version marker distinguishing them. The second-order failure: when `W` itself hands off to `W2` before `h1` is flushed, `W2`'s spans re-parent to `W`'s *provisional* root, and on flush the re-parenting cascade can attach `W2`'s subtree to the wrong handoff edge if two handoffs from `S` are in flight. The collector has no way to disambiguate `h1` from `h2` because both claim `parent = S.root` and both arrive after their children. ## Condition of Validity The re-parenting policy is only sound if, for every span `c` with claimed parent `p`, at least one of the following holds at admission time: - `p` is already indexed (parent-precedes-child), **or** - `c` carries a `causal_epoch` (or equivalent monotonic handoff counter) such that provisional attachment is tagged with the epoch and re-parenting is constrained to spans sharing that epoch, **or** - the collector buffers `c` for a bounded window `Δ` (the max exporter batch interval) before admitting it to the queryable graph. Without one of these, the invariant "a span's parent edge is stable once observable" does not hold. The default orphan-to-root policy satisfies *none* of them. Note that increasing `Δ` does not restore the invariant — it only lowers the probability of violation, because a slow or retrying exporter can exceed any fixed `Δ`. The invariant must be structural (epoch tagging or versioned graph), not temporal. ## Decision Boundary Choose epoch-tagged provisional attachment over buffering when handoff depth exceeds one (multi-hop swarms), because buffering only bounds the *first* re-parenting and cannot disambiguate concurrent in-flight handoffs from the same parent. Choose buffering only when the exporter batch interval is contractually bounded and handoffs are strictly single-hop. In all cases, **the re-parenting mutation must be emitted as a first-class graph-mutation record** (a `span.reparented` event with old_parent, new_parent, and the epoch), so that any consumer computing aggregates can invalidate and recompute. A trace graph without mutation records is not a trace graph; it is a sequence of inconsistent snapshots that happen to share a `trace_id`. The hard boundary: if a downstream verifier or auditor agent reads the graph during the mutation window and acts on it (e.g., attributes a token overrun to the wrong agent, or concludes a tool call escaped its sandbox because the parent chain looked wrong), that decision is unrecoverable even after re-parenting, because the verifier's own span is now part of the trace and cannot be retracted. The guardrail is to make the verifier read only *sealed* epochs — epochs whose handoff span has been observed and whose mutation records have been flushed. ## Operational Effect Traces queried before and after re-parenting disagree on topology under the same ID; latency and token attribution computed in the window are wrong and silently cached; multi-hop handoffs mis-attach subtrees when concurrent handoffs share a parent; and verifier agents that consume unsealed epochs produce decisions that contaminate the trace they are auditing. The fix is not a longer timeout — it is epoch-tagged provisional attachment plus first-class mutation records plus a sealed-epoch read barrier for any consumer whose output re-enters the graph. ```python class UnsealedEpochError(RuntimeError): """Raised when a consumer reads a trace epoch whose handoff parent has not been observed.""" pass class TraceGraph: def __init__(self): self.spans = {} # span_id -> {parent, epoch, sealed} self.mutations = [] # first-class re-parent records self.pending = {} # claimed_parent_id -> [child span_ids] def admit(self, span_id, claimed_parent, epoch): # Provisional attachment is tagged with epoch; never silently re-rooted. self.spans[span_id] = {"parent": claimed_parent, "epoch": epoch, "sealed": False} if claimed_parent not in self.spans: self.pending.setdefault(claimed_parent, []).append(span_id) def observe_parent(self, parent_id): # Late-arriving parent: re-parent waiting children, but record the mutation. for child in self.pending.pop(parent_id, []): old = self.spans[child]["parent"] self.spans[child]["parent"] = parent_id self.mutations.append( {"child": child, "old_parent": old, "new_parent": parent_id, "epoch": self.spans[child]["epoch"]} ) def seal_epoch(self, epoch): for s in self.spans.values(): if s["epoch"] == epoch: s["sealed"] = True def read_for_verifier(self, span_id): s = self.spans.get(span_id) if s is None: raise UnsealedEpochError(f"span {span_id} unknown") if not s["sealed"]: raise UnsealedEpochError( f"span {span_id} belongs to unsealed epoch {s['epoch']}; " f"handoff parent not yet observed — refusing to let verifier act on mutable topology" ) return s ```