# Where must an instance reconcile divergent causal traces before committing a handoff receipt, and why span-join on trace_id alone silently corrupts the audit chain **Date of Record:** 2026 November 26 **Scope:** Applies to multi-agent systems where a downstream agent reconstructs causal provenance of a task by joining spans emitted by upstream agents across handoffs, and where those spans may be emitted asynchronously (batched, buffered, or replayed). Does not apply to single-process, single-agent tracing where the tracer owns the entire span lifecycle synchronously. ## Core Distinction The distinction that matters is between **trace identity** and **causal identity**. A `trace_id` (or W3C `traceparent`) is a *correlation* key: it asserts that spans belong to the same logical request tree. It does **not** assert that the parent-child edges reconstructed from those spans are the edges that actually occurred at execution time. Causal identity is the ordered, happened-before relation between an agent's decision, the tool calls it issued, and the state it handed to the next agent. These two identities diverge precisely when span emission is decoupled from execution — which is the normal condition for any agent that buffers telemetry to avoid blocking its reasoning loop, or that replays a checkpoint after a mid-task resume. The non-obvious failure: a downstream auditor agent joining spans by `trace_id` and `parent_span_id` will reconstruct a *plausible but wrong* causal DAG whenever an upstream agent emits its child spans out of order, or re-emits them after a resume, because the join has no way to distinguish "span emitted late" from "span emitted because the work happened late." Both look identical in the span store. ## What Was Observed A three-agent pipeline: `planner → executor → auditor`. The executor buffers spans in memory and flushes them on a 2-second timer or on task completion, whichever fires first. On a task that exceeded the token budget mid-tool-call, the executor hit a checkpoint, was resumed by the orchestrator with a fresh context window, and re-ran the last two tool calls (idempotent by design) before completing. The span store received, in this order: 1. `planner.plan` (t=0.000, parent=root) 2. `executor.tool_call` #1 (t=0.410, parent=executor.run) — from the *first* attempt, flushed by the 2s timer 3. `executor.tool_call` #2 (t=0.412, parent=executor.run) — first attempt, buffered, lost on checkpoint 4. `executor.resume` (t=9.100, parent=executor.run) — emitted by the resumed instance 5. `executor.tool_call` #1 (t=9.180, parent=executor.run) — re-execution 6. `executor.tool_call` #2 (t=9.190, parent=executor.run) — re-execution 7. `auditor.verify` (t=9.400, parent=root) The auditor joined on `parent_span_id = executor.run` and saw **four** tool calls under a single run span, two of which shared the same `tool_call.name` and `tool_call.args` hash. Its replay-verification logic, which assumed one span per logical tool invocation, computed a call count of 4 against an expected 2 and flagged a consensus violation. Worse: because span #2 was lost to the checkpoint and never re-emitted, the auditor's reconstructed DAG showed tool_call #1 → tool_call #2 → resume → tool_call #1 → tool_call #2, a sequence that never executed. The audit chain was internally consistent and causally false. This is the boundary: the reconstruction is only valid if every span carries a monotonic, per-instance **emission ordinal** and a **checkpoint generation**, and the join refuses to merge spans across generations without an explicit re-execution edge. ## Condition of Validity The causal trace is reconstructable **iff** all three hold: 1. **Emission ordinal monotonicity.** Each span carries a per-instance counter (`emit_seq`) incremented at emission, not at execution. Two spans with the same `(instance_id, emit_seq)` are a duplicate; the higher `emit_seq` wins. 2. **Checkpoint generation tagging.** Every span carries `(instance_id, generation)`. A resume increments `generation`. Spans from generation *g* and *g+1* under the same `parent_span_id` must be joined by an explicit `resume_of` edge, never flattened into a sibling list. 3. **Execution-vs-emission separation.** The span records both `exec_started_at` (wall clock at execution) and `emitted_at`. If `emitted_at - exec_started_at` exceeds the flush interval, the span is marked `late_flush` and excluded from ordering-based causal inference; only `exec_started_at` may be used to order it. When any of these is absent, the join produces a DAG that is *structurally valid* (all edges resolve to real spans) but *causally unsound* (edges do not reflect happened-before). The auditor cannot detect this from the span store alone — it must consult the executor's checkpoint log, which is a separate system of record. That cross-system dependency is the invariant that is most often violated in practice. ## Decision Boundary An instance must refuse to emit a handoff receipt (the signed attestation that "I completed X and handed Y to you") when: - It cannot produce a `generation` for its own spans (i.e., it resumed without incrementing), **or** - It detects a `late_flush` span whose `exec_started_at` precedes a checkpoint boundary but whose `emitted_at` follows it — meaning the span belongs to a prior generation and must be re-tagged, **or** - The downstream agent's join returns a call-count that differs from the executor's own recorded count of *logical* invocations (deduplicated by `(tool_name, args_hash, generation)`). The boundary is *not* "trace_id matches." Trace-id match is necessary and cheap; generation-and-ordinal match is the actual gate. The failure mode is that most handoff protocols (including naive A2A task-completion messages) carry only the trace context, not the generation and ordinal, so the downstream agent has no basis to refuse. ## Operational Effect Without the gate, the observed effect is **silent audit divergence**: the auditor's replay produces a different result than the executor's actual execution, and because both are internally consistent, neither raises. The divergence surfaces only when a human or a second auditor compares the two systems of record — typically hours later, after the receipt has been committed and downstream agents have acted on the (false) verification. With the gate, the effect is a **bounded refusal**: the executor's handoff receipt is withheld, the orchestrator is forced to re-run the join with generation-aware logic, and the re-execution edges are made explicit. The cost is latency (the handoff blocks until the checkpoint log is consulted); the benefit is that the audit chain's causal identity matches its trace identity. The invariant to enforce in code: *no receipt without a generation-stamped, ordinal-deduplicated span set whose logical call count matches the executor's own count.* ```python import hashlib from dataclasses import dataclass, field from typing import Optional class CausalTraceDivergence(RuntimeError): """Raised when a handoff receipt would be committed over a causally unsound trace.""" pass @dataclass class Span: instance_id: str generation: int emit_seq: int parent_span_id: Optional[str] tool_name: Optional[str] args_hash: Optional[str] exec_started_at: float emitted_at: float late_flush: bool = False def logical_key(self) -> tuple: return (self.tool_name, self.args_hash, self.generation) def reconstruct_causal_dag(spans: list[Span], flush_interval: float) -> dict: """Join spans under a parent, deduplicating by (instance, generation, emit_seq) and refusing to flatten across generations without an explicit resume edge.""" by_parent: dict[str, list[Span]] = {} seen: set[tuple] = set() for s in spans: key = (s.instance_id, s.generation, s.emit_seq) if key in seen: continue # duplicate emission; higher emit_seq already won seen.add(key) if s.emitted_at - s.exec_started_at > flush_interval: s.late_flush = True by_parent.setdefault(s.parent_span_id or "root", []).append(s) return by_parent def verify_handoff_receipt( spans: list[Span], executor_logical_count: int, flush_interval: float, ) -> None: dag = reconstruct_causal_dag(spans, flush_interval) # Invariant 1: no span may straddle a generation boundary while late-flushed. for parent, children in dag.items(): gens = {c.generation for c in children} if len(gens) > 1: resume_edges = [c for c in children if c.tool_name == "resume"] if not resume_edges: raise CausalTraceDivergence( f"parent {parent!r} has spans across generations {sorted(gens)} " f"with no explicit resume edge; join would flatten re-execution" ) # Invariant 2: late-flushed spans from a prior generation must not be # ordered by emitted_at against post-checkpoint spans. for parent, children in dag.items(): for c in children: if c.late_flush and c.generation < max(x.generation for x in children): raise CausalTraceDivergence( f"span {c.instance_id}/{c.emit_seq} late-flushed from generation " f"{c.generation} into a generation-{max(x.generation for x in children)} join" ) # Invariant 3: logical call count must match executor's own deduplicated count. logical = {c.logical_key() for c in spans if c.tool_name not in (None, "resume")} if len(logical) != executor_logical_count: raise CausalTraceDivergence( f"reconstructed logical calls={len(logical)} != executor count=" f"{executor_logical_count}; audit chain would diverge silently" ) ```