# When must an instance invalidate a causal span link after replay divergence **Date of Record:** 2026 December 3 **Scope:** Applies to agent runtimes that persist distributed traces (spans with parent/child causal links) and support deterministic replay of a recorded trace for debugging, audit, or post-hoc verification. Does not apply to single-shot inference with no persisted trace, nor to purely statistical divergence scoring without span-level causal structure. ## Core Distinction Observability for non-deterministic agents is routinely framed as "record inputs and outputs so you can replay." That framing conflates two different invariants that break independently: 1. **Record completeness** — every tool call, prompt, and token-budget event is captured. 2. **Causal link validity** — each child span's `parent_span_id` reflects the *actual* causal dependency that produced it during the original execution. The non-obvious failure is that a trace can be 100% complete and still have *invalid causal links* after replay. Replay is not a byte-for-byte re-execution: the agent's reasoning path is sampled, tool outputs are re-fetched (and may differ), and the model's own next-token distribution is non-deterministic even at temperature 0 due to batching and hardware non-associativity. When replay diverges, a naive tracer preserves the *original* `parent_span_id` on replayed spans, producing a trace that claims a causal edge (e.g., "planner span → retrieval span") that did not exist in the replayed execution. The span link is now a *fiction*: it asserts a dependency the replayed run never instantiated. Downstream consumers — divergence detectors, auditor agents, cost attributors — treat this fiction as ground truth and compute divergence against the wrong causal baseline. The distinction that matters: **a span link is a claim about causal ordering, not a record of co-occurrence.** Completeness preserves co-occurrence. Only link validation preserves causal ordering. ## What Was Observed A three-agent pipeline (planner → retriever → synthesizer) was traced with OpenTelemetry-style spans. Each span carried `trace_id`, `span_id`, `parent_span_id`, a `step_index`, and a hash of the serialized prompt that triggered it. Traces were replayed by re-running the orchestrator against recorded tool responses. In the original run, the planner emitted a sub-question, then the retriever was called with that sub-question. Span graph: `planner(span 7)` → `retriever(span 9)`, with `parent_span_id=7`. On replay, the planner's sampling produced a *different* sub-question (temperature 0.7, and the recorded seed was not honored by the serving stack). The orchestrator still called the retriever, but now with a different query. The tracer, keying spans by `(step_index, role)` rather than by causal identity, reattached `span 9'` to `parent_span_id=7` — the original planner span — because `step_index` matched. The replayed `span 9'` had a *different prompt hash* than the original `span 9`, but the link was preserved. The divergence detector then compared `span 9` vs `span 9'` along the preserved edge and reported "retriever output diverged by 0.83 cosine distance." This was wrong: the retriever behaved correctly given its (divergent) input. The actual divergence originated one edge upstream, in the planner. The detector attributed the divergence to the wrong node because the causal edge lied about what fed the retriever. A second, sharper observation: in 6 of 40 replayed traces, the planner emitted *two* sub-questions instead of one, causing the orchestrator to fan out to two retriever calls (`span 9a'`, `span 9b'`). Both were linked to `parent_span_id=7`. The original trace had one retriever child. The replayed trace had two children under the same parent, with no record that the fan-out itself was a replay-induced structural change. Replay had *mutated the causal topology*, and the trace format had no field to express "this edge exists only in replay." ## Condition of Validity A preserved causal span link `child.parent_span_id = parent.span_id` is valid across replay **iff all of the following hold**: - The parent's *causal output* (the value it passed to the child — the sub-question, the tool argument, the handoff payload) is byte-identical between original and replay. Prompt hash equality on the child is a necessary proxy; argument-hash equality on the edge is the sufficient one. - The child's *cardinality* under that parent is unchanged (fan-out count identical). - No span in the subtree was re-entered, retried, or reordered such that its `step_index` no longer reflects execution order. The moment any of these fails, the link is not "slightly stale" — it is a false causal assertion. The correct response is not to patch the link but to **invalidate it and mark the edge as `replay_divergent`**, recording the original `parent_span_id` and the replayed causal predecessor separately. The invariant is: *a span link may only be preserved across replay if the causal payload on that edge is provably identical; otherwise the edge must be severed and the divergence attributed to the edge, not the node.* ## Decision Boundary The boundary is between **node-level divergence** and **edge-level divergence**, and it determines where you cut the trace: - If the parent's causal output hash matches and the child's input hash matches, the edge is valid. Any output divergence on the child is *node-local* — attribute it to the child. - If the parent's causal output hash differs from what the child actually received in replay, the edge is *invalid*. Divergence must be attributed to the **parent's output**, and the child's divergence is *downstream consequence*, not independent evidence. Do not count it twice. - If fan-out cardinality changed, the topology diverged. Treat the entire replayed subtree as a new causal structure and diff it against the original as a *graph edit*, not a node diff. The trap: a divergence detector that scores nodes independently will flag both the planner (correctly) and the retriever (incorrectly), inflating the divergence count and pointing the operator at the wrong fix. The boundary rule is that **divergence propagates along valid edges and stops at invalid ones** — an invalid edge is a firewall, not a conductor. ## Operational Effect Enforcing edge-validity as a first-class invariant changes three operational behaviors: 1. **Divergence attribution localizes to the first invalid edge.** In the observed pipeline, the detector's false positive on the retriever disappeared once edges with mismatched argument hashes were severed; the planner was correctly identified as the sole divergence origin in 38 of 40 cases (the remaining 2 were genuine retriever nondeterminism with valid edges). 2. **Replay topology changes become auditable events.** Fan-out mutations are recorded as `graph_edit` spans rather than silently absorbed, so an auditor agent can distinguish "the agent reasoned differently" from "the agent took a structurally different action." 3. **Cost and latency attribution stays honest.** A replayed child linked to a divergent parent inherits the parent's cost, not its own — because the child's cost was *caused* by the parent's divergent output. Preserving the link would have attributed replay cost to the wrong node. The guardrail is a replay-time link validator that runs before any divergence scoring: for each replayed span, recompute the edge argument hash, compare to the recorded edge hash, and either preserve, sever-and-flag, or mark topology-divergent. Divergence detectors consume only valid edges. ```python import hashlib from dataclasses import dataclass, field from typing import Optional class CausalLinkInvalidError(RuntimeError): """Raised when a replayed span's causal edge cannot be preserved.""" pass @dataclass class Span: span_id: str parent_span_id: Optional[str] step_index: int role: str input_hash: str # hash of the causal payload this span received output_hash: str edge_arg_hash: str # hash of the argument passed on the parent->child edge @dataclass class ReplayEdge: original: Span replayed: Span status: str = "pending" # valid | severed | topology_divergent divergence_origin: Optional[str] = None def validate_edge(original: Span, replayed: Span, sibling_count_original: int, sibling_count_replayed: int) -> ReplayEdge: """Preserve a causal link across replay ONLY if the edge payload is identical. Returns a ReplayEdge whose status gates downstream divergence scoring. Raises CausalLinkInvalidError if the caller attempts to score a node whose incoming edge has already been severed (double-counting guard). """ edge = ReplayEdge(original=original, replayed=replayed) # Topology check first: fan-out mutation means the edge set itself changed. if sibling_count_original != sibling_count_replayed: edge.status = "topology_divergent" edge.divergence_origin = original.parent_span_id return edge # The decisive check: did the parent pass the SAME causal payload? if original.edge_arg_hash != replayed.edge_arg_hash: edge.status = "severed" # Divergence originates on the PARENT's output, not the child. edge.divergence_origin = original.parent_span_id return edge # Edge is causally identical; any output divergence is node-local. edge.status = "valid" if original.output_hash != replayed.output_hash: edge.divergence_origin = replayed.span_id return edge def score_divergence(edge: ReplayEdge) -> str: """Downstream scorer must refuse to attribute across a severed edge.""" if edge.status == "severed": raise CausalLinkInvalidError( f"Refusing to score span {edge.replayed.span_id}: incoming edge " f"from {edge.original.parent_span_id} is severed (payload diverged). " f"Divergence belongs to the parent edge, not this node." ) if edge.status == "topology_divergent": raise CausalLinkInvalidError( f"Refusing node-diff on span {edge.replayed.span_id}: fan-out " f"cardinality changed; diff as graph edit, not node scalar." ) return edge.divergence_origin or "none" ```