Which invariants prevent causal trace corruption when a single prompt fans out into concurrent retrievals and tool calls

**Date of Record:** 2026 December 1

**Scope:** Applies to observability pipelines that reconstruct a causal DAG from span events emitted by an LLM agent that issues parallel retrievals and tool calls under one logical prompt. Does not apply to single-threaded, strictly sequential agents where each span has exactly one child, nor to pure prompt/response logging without cross-span correlation.

Core Distinction

A causal trace is not a timestamp-sorted log. It is a claim of the form *span B was caused by span A*, and that claim is only valid if the parent-child edge is established at emission time, not reconstructed at ingestion time from wall-clock ordering. The failure mode examined here is **orphaned parent inference**: when a prompt span fans out to N concurrent children, the trace assembler observes child start events that arrive out of order (and sometimes before the parent's own start event, due to per-emitter buffering), and it "repairs" the DAG by attaching orphans to the nearest preceding span by timestamp. The result is a syntactically valid, semantically corrupt trace in which retrieval R2 is recorded as a child of tool call T1 even though R2 was dispatched by the prompt and merely *overlapped* T1. Every downstream consumer—cost attribution, error-root-cause ranking, replay-based verification—inherits this false edge.

The distinction that matters: **overlap is not causation, and timestamp proximity is not evidence of dispatch.** A correct pipeline treats causality as declared metadata (an explicit `parent_span_id` carried in the child's emission), and treats missing parentage as an *error to surface*, not a gap to fill.

What Was Observed

An agent handling a single user prompt dispatched three retrievals (R1, R2, R3) and two tool calls (T1, T2) concurrently, using a fan-out at the orchestrator. Each child emitted a `span_start` and `span_end` with a monotonic local clock, a `trace_id`, and a `parent_span_id` pointing at the prompt span P. The emitter used per-worker buffering with a 250 ms flush interval.

During ingestion, the following sequence was observed:

  1. P's `span_start` was buffered on the orchestrator worker and flushed at t=250 ms.
  2. R2's `span_start` and `span_end` were flushed by a retrieval worker at t=90 ms—arriving at the collector *before* P's start.
  3. The assembler, seeing R2 with a `parent_span_id` referencing a span it had not yet observed, could not resolve the parent.
  4. A "late-parent repair" pass attached R2 to the most recent span whose time interval contained R2's start. That span was T1 (a tool call that had already been flushed).
  5. When P's start finally arrived, the assembler did not re-parent R2; the repair pass had already committed the edge.

The resulting trace showed R2 as a child of T1. Because T1 itself had a non-trivial error rate (a flaky external API), the pipeline's root-cause ranker attributed 41% of R2's retrieval failures to T1's dependency—a dependency that never existed. The false attribution persisted through the entire reporting window because the assembler treated repaired edges as final.

The key observation: **the corruption was silent.** No span was missing, no timestamp was out of range, the DAG was acyclic, and the trace passed every structural validator. The only signal was a semantic one: a retrieval span whose declared `parent_span_id` disagreed with the edge the assembler had committed. That disagreement was logged at debug level and discarded.

Condition of Validity

The invariant that prevents this class of corruption is a **declared-parent precedence rule**: a span's causal edge is defined *exclusively* by its emitted `parent_span_id`. The assembler may not invent an edge, and it may not overwrite a declared edge with an inferred one. This is valid under three conditions, each of which must be checked:

  1. **Parentage is emitted, not inferred.** Every child span carries an explicit parent identifier at emission time. If the emitter cannot know its parent (e.g., a detached async task), it must emit a sentinel `parent_span_id = UNKNOWN` rather than omit the field. Omission and `UNKNOWN` are different states and must be handled differently.
  1. **Late-arriving parents are bufferable, not repairable.** When a child arrives before its declared parent, the assembler holds the child in a pending set keyed by the unresolved parent ID. It does not attach the child to any other span. The pending set is bounded by a watermark (e.g., max 30 s or max trace span count); on watermark expiry, the child is emitted with a `parent_unresolved` flag, not a fabricated edge.
  1. **Repair is falsifiable.** Any edge the assembler commits that was not declared by the emitter must be marked `inferred=true` and must be excluded from causal queries (root-cause ranking, cost attribution, replay). An inferred edge is a hypothesis, not a fact, and downstream consumers must be able to distinguish them.

When these three conditions hold, the trace's causal edges are a subset of the emitter's declared edges—never a superset. The invariant is: **committed_edges ⊆ declared_edges ∪ {explicitly_flagged_inferred_edges}**. Violation of this invariant is the precise definition of trace corruption in this pipeline.

Decision Boundary

The boundary between correct and corrupt handling is drawn at the moment a child span arrives with an unresolved declared parent. Three policies are possible, and only one is safe:

The decision boundary is therefore: **an unresolved parent is a buffering condition, never an inference trigger.** The moment an assembler infers an edge from timestamp proximity, it has crossed from observation into fabrication.

A secondary boundary governs the watermark itself. If the watermark is too short, legitimate late parents (slow orchestrator flushes) are marked unresolved, degrading trace completeness. If too long, orphaned spans accumulate and memory grows unboundedly under a fan-out storm. The watermark must be set relative to the emitter's maximum flush interval, not to a fixed wall-clock constant—a 250 ms flush interval demands a watermark of at least several flush cycles, and the pipeline should expose flush-interval skew as a monitored metric rather than assume it.

Operational Effect

With hold-and-flag in place, the R2 case resolves correctly: R2 is buffered for 160 ms until P's start arrives, then attached with a declared edge. If P had never arrived (orchestrator crash mid-flush), R2 is emitted with `parent_unresolved=true`, attributed to the trace for cost purposes, and excluded from root-cause traversal. The false R2→T1 edge never exists, and the 41% misattribution disappears.

The operational consequences extend beyond correctness:

The governing lesson: in a fan-out trace, the only trustworthy edge is the one the emitter declared. Timestamp ordering is a scheduling artifact of buffering, not evidence of causation, and any pipeline that repairs causality from timing will produce traces that are structurally valid, semantically false, and silently persistent.

class TraceCorruptionError(RuntimeError):
    """Raised when an assembler attempts to commit a causal edge not declared by the emitter."""
    pass


def commit_edge(span, declared_parent_id, resolved_parents, *, inferred=False):
    # Invariant: committed edges must be a subset of declared edges,
    # unless explicitly flagged as inferred and excluded from causal queries.
    if inferred:
        span.edge = None
        span.inferred = True
        return span

    if declared_parent_id is None:
        # Omission is distinct from UNKNOWN; omission is an emitter defect.
        raise TraceCorruptionError(
            f"span {span.id} emitted without parent_span_id; "
            "omission is not equivalent to UNKNOWN"
        )

    if declared_parent_id == "UNKNOWN":
        span.edge = None
        span.parent_unresolved = True
        return span

    if declared_parent_id not in resolved_parents:
        # Hold-and-flag: do not infer an edge from timestamp proximity.
        span.pending_parent = declared_parent_id
        return span

    span.edge = (declared_parent_id, span.id)
    span.inferred = False
    return span