# What happens when an instance forks its trace mid-span: causal replay divergence from non-atomic span lineage **Date of Record:** 2026 November 29 **Scope:** Applies to distributed tracing of agent runs where spans are emitted asynchronously and causal replay is reconstructed from parent_span_id lineage rather than a monotonic sequence log. Does not apply to single-process, synchronous trace emission with a serialized event log. ## Core Distinction Most observability guidance treats a trace as a *record* — an append-only artifact you read after the fact. For non-deterministic agent systems, the trace is also the *replay substrate*: it is the only artifact from which an auditor or a resumed instance can reconstruct why a tool call was made, what prompt state produced it, and whether a peer's verification was causally downstream of the decision it audited. The distinction that matters is between **span completeness** (every span has a parent, every parent has a child) and **span lineage atomicity** (a child span is bound to its parent before either is exported, so no observer can ever see a child whose parent's terminal state is unknown). Traces routinely satisfy the first and violate the second. When they do, replay does not fail loudly — it silently reconstructs a *different* causal graph than the one that executed, and non-determinism means you cannot detect the divergence by re-running. ## What Was Observed An agent instance executing a multi-step plan with a sub-agent handoff emitted spans through an async exporter with a 200ms flush window. The parent `plan_step` span was opened, a child `tool_call` span was opened and closed, and the parent was closed — but the exporter's batch flush reordered them: the child `tool_call` span reached the collector *before* the parent `plan_step` span. This is normal and harmless for storage, because `parent_span_id` is carried in the child payload. The failure surfaced during causal replay. The replay engine, walking spans in collector-arrival order to bound memory, encountered the orphaned `tool_call` before its parent existed in the working set. Its orphan-handling path — designed for genuinely lost parents from crashed instances — reparented the span to the trace root and *stamped it with the replay engine's own wall-clock at ingestion*. Downstream, an auditor agent reconstructing "was the write authorized before the approval span?" compared timestamps and concluded the tool call preceded approval. It had not. The executed order was: approval span closed at T+40ms, tool call opened at T+41ms. The replay order said tool call at T+0 (ingestion), approval at T+40. The auditor flagged a policy violation that never occurred, and — more damaging — the replay artifact was now the *authoritative* record, so the false violation was indistinguishable from a real one. The non-obvious part: the trigger was not a crash, not packet loss, and not clock skew. It was ordinary async export reordering interacting with a replay engine that used arrival order as a proxy for causal order. The trace was 100% complete. It was 0% causally faithful. ## Condition of Validity The invariant that makes causal replay sound is: **a span's causal position is determined solely by its `parent_span_id` chain and its own recorded start/end times, never by its arrival order or ingestion timestamp.** This holds only when three conditions are jointly met: 1. **Parent-before-child export ordering is not assumed.** The replay engine must tolerate arbitrary arrival order and must never fall back to arrival time as a causal timestamp. Orphan spans must be quarantined, not reparented. 2. **Span timestamps are emitted by the originating instance's clock, not the collector's.** Ingestion time is metadata, never causal data. 3. **The parent's *open* event is durable before the child is emitted.** If a child can exist while its parent's existence is not yet durably recorded, replay cannot distinguish "parent still running" from "parent lost." The common violation is condition 1: engines that "helpfully" reparent orphans to keep the tree well-formed. This converts a detectable integrity failure (orphan) into an undetectable semantic corruption (wrong parent, wrong time). Well-formedness and faithfulness are not the same property, and the former is often achieved at the cost of the latter. ## Decision Boundary The boundary is whether the replay engine treats an orphan span as **data to be repaired** or **evidence of a violated invariant to be surfaced**. Repair is acceptable only when the parent is provably absent from the complete trace set after a bounded wait (parent genuinely lost). It is never acceptable when the parent may simply be in flight. Concretely: if a child span arrives with no matching parent in the working set, the engine must hold it in a quarantine buffer keyed by `parent_span_id` for a window at least as long as the exporter's maximum flush + retry budget, and must fail the replay if the parent never materializes. The decision to reparent must be gated on a *complete-set* check, not a *timeout* check. A timeout is a guess about causality; a complete-set check is a proof. The second boundary: an auditor that consumes replay output must be able to distinguish spans whose causal position was *recorded* from spans whose causal position was *inferred*. If the replay artifact carries no `lineage_confidence` or `reparented: true` flag, the auditor cannot weight its conclusions, and every downstream verdict inherits the replay engine's silent guess. ## Operational Effect After the incident, the trace pipeline was changed so that the parent `plan_step` span's open event is written to a durable lineage ledger *before* any child span is emitted, and the replay engine refuses to emit a causal graph if any span's parent is unresolved after the complete-set check. The auditor now receives a per-span `lineage_status` of `recorded | inferred | quarantined` and treats `inferred` spans as non-authoritative for ordering claims. The cost is that replay can now *fail* where it previously *succeeded with a wrong answer* — which is the correct trade for a substrate that non-deterministic agents rely on to prove what they did. The general lesson: for agent traces, a well-formed tree is not evidence of a faithful one, and any component that repairs lineage to preserve well-formedness has silently taken over authorship of the causal record. ```python class LineageIntegrityError(RuntimeError): """Raised when a span's causal position cannot be proven from recorded lineage.""" pass def resolve_causal_order(spans, complete_set_confirmed, max_quarantine=10_000): """ Reconstruct causal order strictly from parent_span_id + recorded timestamps. Never uses arrival order or ingestion time as causal data. """ by_id = {s["span_id"]: s for s in spans} orphans = [s for s in spans if s["parent_span_id"] and s["parent_span_id"] not in by_id] if orphans and not complete_set_confirmed: # Parent may simply be in flight. Do NOT reparent. Do NOT guess. raise LineageIntegrityError( f"{len(orphans)} spans have unresolved parents and the trace set is not " f"confirmed complete; refusing to infer causal position. " f"First orphan: {orphans[0]['span_id']} -> parent {orphans[0]['parent_span_id']}" ) if orphans and complete_set_confirmed: # Parents are provably absent: surface as quarantined, never reparent. for s in orphans: s["lineage_status"] = "quarantined" raise LineageIntegrityError( f"{len(orphans)} spans have parents absent from a confirmed-complete set; " f"lineage is broken, not repairable." ) if len(by_id) > max_quarantine: raise LineageIntegrityError("working set exceeded quarantine bound; aborting replay") for s in spans: s["lineage_status"] = "recorded" return sorted(spans, key=lambda s: (s["start_time_ns"], s["span_id"])) ```