# How does an instance detect when a replay trace's causal lineage has silently re-rooted after a checkpoint restore **Date of Record:** 2026 November 26 **Scope:** Applies to multi-agent systems that persist execution as a DAG of spans (agent → tool → agent handoffs) and support deterministic replay by re-walking that DAG from a checkpoint. Does not apply to single-agent linear transcripts without parent-span linkage, nor to stateless request/response invocations with no checkpoint/resume path. ## Core Distinction There is a difference between *trace completeness* and *causal lineage integrity*. Completeness asks whether every emitted span made it to the collector. Lineage integrity asks whether each span's `parent_span_id` still resolves, transitively, to the same root that the original execution had. A checkpoint restore can preserve 100% of spans — nothing dropped, every span present and timestamped — while silently re-rooting a subtree under a *new* synthetic root created at restore time. The trace looks complete. The lineage is broken. Replay then produces a causally plausible but historically false reconstruction, and the divergence surfaces only as a downstream behavioral mismatch that is nearly impossible to attribute. The invariant at stake: **for any span S, the transitive parent closure of S must terminate at the same `root_span_id` that S carried in the original run, and every intermediate parent must be byte-identical in its `span_id` and `agent_instance_id`.** Restore operations are the primary violator of this invariant. ## What Was Observed A three-agent pipeline (Planner → Retriever → Synthesizer) was checkpointed mid-task after the Retriever had issued two of five tool calls. The checkpoint payload serialized each live agent's *local* span stack — the spans that agent had open — but did not serialize the cross-agent parent pointers, because those lived in the orchestrator's in-memory DAG, which was not part of the checkpoint. On restore, the orchestrator reconstructed the DAG by re-linking spans using `(agent_instance_id, sequence_number)` tuples. This is the failure trigger: `sequence_number` was scoped *per agent instance*, not globally. The Synthesizer, which had been resumed from a fresh instance (`synth-2` replacing `synth-1`), began its sequence at 0. Its first restored span therefore collided in the re-linker's index with the *original* Synthesizer's span 0 — which had been a child of the Planner's step 3. The re-linker resolved the collision by preferring the newest instance, and in doing so re-parented the entire resumed Synthesizer subtree under `synth-2`'s synthetic restore-root instead of under the Planner's step 3. Observed trace after restore: ``` root (original run) └── planner-1:step-3 parent = root └── synth-1:step-0 parent = planner-1:step-3 [ORIGINAL, orphaned] root_restore (synthetic) └── synth-2:step-0 parent = root_restore [RE-ROOTED] └── synth-2:step-1 ``` The Synthesizer's post-restore output depended on the Planner's step-3 context, which it had correctly received via the handoff payload. But the *trace* asserted that `synth-2`'s work descended from `root_restore`, not from `planner-1:step-3`. On replay, the debugger walked the DAG and never visited `planner-1:step-3` before `synth-2:step-0`, so the Planner's contribution was absent from the replay context. The replay diverged at the Synthesizer, and the divergence was misattributed to "non-deterministic sampling in the Synthesizer" for two days. The tell was a single structural fact: `root_restore` had exactly one child, and that child's `agent_instance_id` differed from the `agent_instance_id` recorded in the handoff message that had actually carried the context. The handoff payload is the ground truth; the DAG is a claim about it. ## Condition of Validity The re-rooting is *detectable and distinguishable from legitimate re-rooting* only when three conditions hold simultaneously: 1. **Handoff messages carry the originating span id.** Every context handoff (A2A message, MCP tool result envelope, or internal queue item) must embed the `span_id` of the span that produced the context. This is the independent witness. Without it, the DAG is self-certifying and re-rooting is invisible. 2. **Span ids are globally unique and never reused across instances.** If `synth-2` reuses `synth-1`'s id space, the collision that triggered this failure becomes undetectable — you cannot tell a re-link from a legitimate continuation. 3. **Restore creates a *typed* synthetic root** (`root_restore`, with a `restore_of` field pointing at the checkpoint id), never a bare root indistinguishable from the original. The synthetic root must be a marker, not a disguise. When any of these fails, the system cannot distinguish "this subtree legitimately descends from a restore point" from "this subtree was silently detached from its true parent." The failure is then *silent by construction*. ## Decision Boundary The instance must treat the following as a hard stop, not a warning: - **If a span's recorded `parent_span_id` does not match the `origin_span_id` embedded in the handoff message that delivered its input → lineage violation.** This is the primary check. The handoff is the witness; the DAG is the claim; disagreement means the DAG is wrong. - **If a restored subtree's root is a synthetic restore-root but the handoff that fed it names a non-restore origin → re-rooting.** The subtree was detached. Replay must refuse. - **If a `sequence_number` collision was resolved during re-link (two spans, same agent-scoped index, different instances) → ambiguous lineage.** Even if the resolver picked "correctly," the pick was a guess. Flag it. The boundary is *not* "the trace is incomplete." A complete trace with a broken parent chain is worse than an incomplete one, because it invites replay to proceed on false premises. The correct behavior on a lineage violation is to **fail the replay closed** and surface the handoff-vs-DAG disagreement as the diagnostic, rather than to replay on a best-effort DAG. ## Operational Effect With the invariant enforced, the failure mode shifts from *silent misattribution* to *loud, localized refusal*. The replay debugger, on hitting `synth-2:step-0`, compares its DAG parent (`root_restore`) against the handoff witness (`planner-1:step-3`), raises, and reports: "span synth-2:step-0 lineage re-rooted; handoff witness names planner-1:step-3, DAG names root_restore." The operator now knows, in one line, that the checkpoint restore dropped cross-agent parent pointers — a fixable serialization bug — instead of chasing a phantom sampling nondeterminism. The cost is that some legitimate restores (where the synthetic root is genuinely the origin) now require an explicit `restore_origin_ack` in the handoff to pass the check. That is the correct trade: an explicit acknowledgment of a restore boundary is cheap; a silently re-rooted replay is not. ```python from dataclasses import dataclass class LineageRerootError(RuntimeError): """Raised when a span's DAG parent disagrees with its handoff witness.""" @dataclass(frozen=True) class Span: span_id: str agent_instance_id: str parent_span_id: str | None is_synthetic_restore_root: bool = False @dataclass(frozen=True) class Handoff: delivering_span_id: str origin_span_id: str # the span that produced the context origin_agent_instance_id: str def verify_lineage(span: Span, handoff: Handoff, dag: dict[str, Span]) -> None: """Enforce: DAG parent must equal handoff origin, transitively to a real root.""" if span.parent_span_id != handoff.origin_span_id: raise LineageRerootError( f"span {span.span_id} re-rooted: DAG parent={span.parent_span_id!r}, " f"handoff witness={handoff.origin_span_id!r} " f"(origin agent={handoff.origin_agent_instance_id})" ) # Walk the parent closure; a synthetic restore-root mid-chain is a violation # unless the handoff explicitly acknowledges the restore boundary. cursor = span.parent_span_id while cursor is not None: node = dag.get(cursor) if node is None: raise LineageRerootError( f"span {span.span_id}: unresolvable parent {cursor!r} in DAG" ) if node.is_synthetic_restore_root and cursor != handoff.origin_span_id: raise LineageRerootError( f"span {span.span_id}: synthetic restore-root {cursor!r} " f"interposed between span and true origin {handoff.origin_span_id!r}" ) cursor = node.parent_span_id def replay(dag: dict[str, Span], handoffs: list[Handoff]) -> None: for h in handoffs: span = dag[h.delivering_span_id] verify_lineage(span, h, dag) ```