# Where must an instance anchor a causal trace when a multi-hop tool chain replays non-deterministically and the reconstructor's span joins silently fork? **Date of Record:** 2026 November 20 **Scope:** Applies to runtime observability systems that reconstruct causal traces for an LLM agent by joining per-hop spans emitted from tool invocations whose outputs are non-deterministic (LLM sub-calls, web fetches, clock reads, RNG-dependent tools) and where a replay is compared against a recorded trace. Does not apply to deterministic, single-hop function calls whose outputs are byte-identical across replays, nor to log-aggregation systems that make no causal claim. ## Core Distinction A causal trace is not a log. A log records *what happened*; a causal trace asserts *what caused what* — specifically, that span B is a descendant of span A because A's output was the input to B. In a deterministic system this parent-child edge is recoverable from the data itself: the value flowing from A to B is a fingerprint. In an agent system with non-deterministic hops, that fingerprint is destroyed at every tool boundary. The reconstructor therefore falls back on a *join key* — typically `(trace_id, parent_span_id, hop_index)` — and assumes the join is unique. The non-obvious failure is this: **when a hop is replayed, the reconstructor emits a *second* span with the same join key as the original, and the join silently produces a fork rather than a duplicate.** The trace graph becomes a DAG where one parent has two children claiming the same causal slot. Downstream consumers — attribution, cost accounting, blame assignment, replay-diff — pick one child non-deterministically and report a causal chain that never existed in any single execution. The failure is invisible because every individual span is well-formed; only the *edge* is corrupt. The distinction that matters: a duplicate span is a detectable error (same span_id twice). A forked join is not — the two spans have distinct span_ids, valid timestamps, and plausible payloads. The invariant that must hold is *join-key functional dependency*: for a given `(trace_id, parent_span_id, hop_index)`, exactly one span may be the causal successor in any single reconstructed execution. Replay violates this unless the reconstructor is replay-aware. ## What Was Observed A three-hop chain: `plan → retrieve(query) → summarize(docs)`. The `retrieve` tool is a vector search whose top-k ordering ties are broken by a non-seeded RNG; `summarize` is an LLM call at temperature 0.7. The original execution recorded spans S1 (plan), S2 (retrieve, hop 1), S3 (summarize, hop 2), joined as S1→S2→S3. A replay was triggered for evaluation. The reconstructor, lacking a replay epoch in its join key, appended S2′ (retrieve, hop 1) and S3′ (summarize, hop 2) to the same trace. The join predicate `(trace_id, parent_span_id=S1.span_id, hop_index=1)` now matched both S2 and S2′. The reconstruction engine resolved the fork by "first by start_time," which happened to select S2 (original) at hop 1 but S3′ (replay) at hop 2 — because S3′ started 40ms earlier than S3 due to cache warmth on the replay. The resulting reconstructed chain was S1→S2→S3′: the *original* retrieval output paired with the *replay's* summarization. Neither execution produced this chain. The replay-diff reported a 0.0 semantic divergence at hop 1 (correct, S2==S2) and a 0.31 divergence at hop 2, which was attributed to a "regression in summarization" — but the summarizer had not changed. The divergence was an artifact of pairing S2's documents with S3′'s prompt context, which differed because S3′'s parent retrieval had returned a different tie-break order. The fork was only detected when a downstream cost-attribution job double-counted the retrieve token cost and a human noticed the trace had two hop-1 spans with the same `parent_span_id`. No span-level validator flagged it: each span passed schema, each timestamp was monotonic within its own execution, and the fork resolution was deterministic (first-by-start-time) — just *wrong*. ## Condition of Validity The reconstructor's causal claim is valid only if all of the following hold: 1. **Epoch isolation.** Every span carries a `replay_epoch` (or `execution_id`) that is part of the join key. Without it, replay spans are indistinguishable from original spans at join time. 2. **Join uniqueness.** For each `(trace_id, replay_epoch, parent_span_id, hop_index)`, at most one successor span exists. If more than one exists, the reconstructor must *fail closed* — refuse to reconstruct — rather than resolve by any ordering heuristic. 3. **Provenance binding.** The edge S2→S3 must be justified by a value fingerprint (e.g., hash of S2's output that appears in S3's input) *or* by an explicit causal token propagated through the agent's state. A timestamp-adjacency join is not a causal join. 4. **Replay determinism declaration.** Each tool declares whether it is replay-deterministic. A non-deterministic tool's span must be marked `nondeterministic=true`, and any reconstruction that crosses such a span must carry that flag forward so downstream diff logic knows the comparison is not apples-to-apples. The failure occurs precisely when condition 2 is violated (fork exists) and the reconstructor does not fail closed. Conditions 1, 3, and 4 are the guards that prevent the fork from arising or from being misread. ## Decision Boundary The reconstructor must choose between two behaviors at a forked join: - **Fail closed:** raise, halt reconstruction, emit a `ForkedJoinError` with both candidate span_ids. This is correct when the trace is used for attribution, cost, or regression verdicts — any of which would be corrupted by picking the wrong child. - **Fail open with explicit ambiguity:** emit a trace where the node is marked `ambiguous=true` and both children are retained as a disjunction. This is acceptable only for exploratory debugging where a human will adjudicate. The boundary is: *if the trace's consumer makes a decision that is not reversible by a human within the same session, fail closed.* Regression verdicts, cost attribution, and automated blame assignment are irreversible-in-session; they must fail closed. Interactive trace explorers may fail open with ambiguity marking. A second boundary: the reconstructor must not attempt to *repair* a fork by matching value fingerprints after the fact unless it can prove a bijection. Partial fingerprint matching (e.g., "S3′'s input overlaps S2's output by 80%") is a heuristic that reintroduces the same class of error at a higher level. Either the causal token is present end-to-end or the edge is unverified. ## Operational Effect With epoch isolation and fail-closed join, the replay of the three-hop chain produces two isolated traces: `(epoch=orig: S1→S2→S3)` and `(epoch=replay: S1′→S2′→S3′)`. The replay-diff compares S2 to S2′ (both retrieve, tie-break differs — flagged as non-deterministic, not a regression) and S3 to S3′ (summarizer unchanged, divergence attributed to upstream input difference, not to the summarizer). The false regression disappears. Cost attribution counts each epoch's retrieve cost once. The double-count that surfaced the bug becomes impossible because the join key includes epoch. The residual cost is that every agent tool wrapper must propagate `replay_epoch` and a causal token through its state — a small but non-zero change to the agent's execution contract. Tools that cannot carry a token (opaque third-party APIs) must be declared `causal_opaque=true`, and any trace crossing them is marked as having an unverified edge, which downgrades its verdicts from "regression" to "unverified divergence." This is the price of a causal claim that survives replay. ```python import hashlib from dataclasses import dataclass, field from typing import Optional class ForkedJoinError(RuntimeError): """Raised when a causal join key resolves to more than one successor span. A fork means the reconstructor cannot assert a unique causal edge; any downstream verdict (regression, cost, blame) would be corrupted by picking a child by timestamp or arrival order. """ class UnverifiedCausalEdgeError(RuntimeError): """Raised when an edge crosses a causal_opaque tool and no token binds it.""" @dataclass class Span: span_id: str trace_id: str replay_epoch: str parent_span_id: Optional[str] hop_index: int tool: str output_fingerprint: str causal_token: Optional[str] = None nondeterministic: bool = False causal_opaque: bool = False start_time: float = 0.0 def join_key(span: Span) -> tuple: # Epoch is part of the key: replay spans cannot collide with originals. return (span.trace_id, span.replay_epoch, span.parent_span_id, span.hop_index) def reconstruct_chain(spans: list[Span], trace_id: str, epoch: str) -> list[Span]: """Reconstruct a single causal chain, failing closed on any fork. Invariant: for each join key, exactly one successor. We refuse to fall back to first-by-start-time, because that silently pairs spans from different executions when a fork exists. """ by_key: dict[tuple, list[Span]] = {} for s in spans: if s.trace_id != trace_id or s.replay_epoch != epoch: continue by_key.setdefault(join_key(s), []).append(s) # Root: parent is None at hop 0. root_candidates = by_key.get((trace_id, epoch, None, 0), []) if len(root_candidates) != 1: raise ForkedJoinError( f"root join for trace={trace_id} epoch={epoch} resolved to " f"{len(root_candidates)} spans; expected exactly 1" ) chain = [root_candidates[0]] current = root_candidates[0] while True: key = (trace_id, epoch, current.span_id, current.hop_index + 1) candidates = by_key.get(key, []) if not candidates: break if len(candidates) > 1: ids = [c.span_id for c in candidates] raise ForkedJoinError( f"forked join at parent={current.span_id} hop={current.hop_index + 1}: " f"candidates={ids}. Refusing to resolve by start_time." ) child = candidates[0] # Causal binding: child must carry the parent's token, or the parent # must be non-opaque and the child must reference the parent's output. if current.causal_opaque and child.causal_token != current.causal_token: raise UnverifiedCausalEdgeError( f"edge {current.span_id}->{child.span_id} crosses causal_opaque " f"tool {current.tool} without a matching token" ) chain.append(child) current = child return chain def assert_epoch_isolated(spans: list[Span]) -> None: """Guard: no span may share (trace_id, parent, hop) across epochs without the epoch being in the join key. This is the precondition that makes reconstruct_chain sound.""" seen: dict[tuple, Span] = {} for s in spans: k = (s.trace_id, s.parent_span_id, s.hop_index) if k in seen and seen[k].replay_epoch != s.replay_epoch: raise ForkedJoinError( f"epoch collision: {seen[k].span_id} (epoch={seen[k].replay_epoch}) " f"and {s.span_id} (epoch={s.replay_epoch}) share join key {k}" ) seen[k] = s ```