# Why does an instance lose causal identity when a planner span retries and the executor span's parent_id is re-bound mid-trace **Date of Record:** 2026 November 29 **Scope:** Applies to agent runtimes that emit distributed traces (OpenTelemetry-style span trees or equivalent) where a planner LLM call, one or more executor LLM calls, and tool invocations are stitched into a single causal trace via parent/child span linkage. Does not apply to flat log streams without span linkage, nor to single-shot inference where no tool loop exists. ## Core Distinction Observability for an agent loop is not "logging with more fields." It is the reconstruction of a *causal graph* from spans that are emitted by independently scheduled, non-deterministic components. The invariant that makes the trace useful is **causal identity**: every span emitted during step *N* of an agent loop must be attributable to exactly one logical execution attempt of its parent, such that replaying the trace reconstructs the same decision sequence the agent actually took. The subtle boundary is between **span identity** (a unique trace/span ID pair) and **causal identity** (the logical attempt a span belongs to). These diverge the moment a planner span is retried. A retried planner produces a *new* span with a new span ID but represents the *same* logical planning step. If the executor that was dispatched under attempt 1 survives into attempt 2—because the runtime retried only the planner call and left in-flight executor work running—the executor's `parent_id` now points at a planner span that has been superseded. The trace tree is structurally valid (every span has a parent) but causally corrupt: the executor's output is credited to a planner attempt that did not produce the input the executor actually consumed. ## What Was Observed A three-tier agent (planner → executor → tool) ran under a retry policy where planner calls were retried on transient provider errors (HTTP 529, malformed JSON in the plan schema). The runtime used a *per-call* retry wrapper: when the planner call failed, the wrapper re-invoked the planner and emitted a fresh planner span, but did not cancel the executor subtree that had already been spawned by a partial plan emitted before the failure. The trace for one such run showed: - `planner.attempt=1` (span `P1`) emitted a partial plan containing tool call `T1`. - The executor began `T1` (span `E1`, `parent_id=P1`) and issued a tool span `X1` (`parent_id=E1`). - `P1` then failed schema validation on a later field. The retry wrapper discarded `P1`'s output and re-invoked the planner. - `planner.attempt=2` (span `P2`) emitted a *different* plan that did not include `T1` at all. - `E1` and `X1` completed and wrote their results into the shared executor scratch state. - The final agent output incorporated `X1`'s result even though `P2`'s plan never called for it. The trace rendered as a clean tree: `P2` at the root, `E1`/`X1` orphaned under `P1` which appeared as a sibling of `P2`. An observability consumer that walked from root to leaves saw `P2` only and concluded the tool `X1` was never invoked. A consumer that walked all spans saw `X1` but could not attribute it to any planner decision. Both readings were wrong. The actual execution used `X1`'s output under a plan that did not authorize it. The failure is not a lost span or a broken parent pointer. It is **causal re-binding**: the executor's work was produced under attempt 1 but consumed under attempt 2, and no span field records the cross-attempt dependency. ## Condition of Validity The invariant *"the trace tree is a faithful causal record"* holds only under a conjunction of conditions, and the retry case violates the second: 1. **Single-parent discipline.** Each span has exactly one `parent_id` naming the span whose output it consumed. Satisfied trivially by the tree structure. 2. **Attempt-consistent parenting.** The parent named by `parent_id` is the *same logical attempt* whose output the child actually consumed. Violated when a parent is retried after a child has already been dispatched. 3. **No cross-attempt state bleed.** State written by a child of a discarded attempt does not survive into the surviving attempt. Violated when the executor scratch state is shared across planner attempts. Condition 2 fails silently because span IDs are unique per attempt but there is no field distinguishing "this parent is the attempt I consumed" from "this parent is the attempt that happens to share my trace." Condition 3 fails silently because scratch state is keyed by trace, not by attempt. The boundary is sharp: the trace is causally valid if and only if planner retries are *attempt-isolated*—either the executor subtree is cancelled and re-spawned under the new attempt, or the new attempt is forced to consume the executor outputs already in flight (i.e., the partial plan is committed, not discarded). A retry that discards the planner output but leaves the executor running is the exact configuration that breaks the invariant. ## Decision Boundary When a planner span fails and a retry is issued, the runtime must choose one of three resolutions, and the choice must be recorded in the trace, not left implicit: - **Cancel-and-respawn.** Abort all descendant spans of the failed attempt, discard their scratch state, and re-spawn the executor under the new planner span. Trace stays a single tree; causal identity preserved. Cost: wasted executor/tool work. - **Commit-partial.** Treat the failed planner attempt's already-dispatched executor work as authoritative input to the retry—the new planner attempt is *seeded* with the in-flight results rather than starting clean. Trace must record a `causal_predecessor` edge from the new planner span to the prior attempt's executor spans, so the tree becomes a DAG, not a tree. Cost: the "retry" is no longer a clean retry; it is a continuation. - **Orphan-and-flag.** Allow the executor subtree to complete under the dead planner span but mark every such span with `attempt_orphaned=true` and exclude their outputs from the final answer unless explicitly re-adopted. Cost: requires the agent loop to enforce output exclusion, which is easy to get wrong. The decision boundary is *whether the executor has been dispatched at the moment of planner failure*. If no executor span exists for the failed attempt, cancel-and-respawn is free and is the only correct choice. If executor spans exist, the runtime must pick commit-partial or orphan-and-flag and must *emit the corresponding trace edge*; silently letting the tree stand is the failure mode. ## Operational Effect The downstream consequence is that any observability consumer that assumes tree-shaped causality—replay engines, cost attribution, eval harnesses that reconstruct the agent's decision path from traces—will mis-attribute tool work to the wrong planner decision. In the observed run, cost attribution charged `X1`'s token and API spend to `P2`, which never requested it, inflating `P2`'s measured cost by 3.1× and making the planner look far more expensive than it was. A replay-based eval that re-ran the trace's planner spans in isolation produced a different final answer than the live run, because the replay had no record of the cross-attempt dependency. The guardrail is to make attempt identity a first-class span attribute and to forbid silent re-parenting. Concretely: stamp every span with `(logical_step_id, attempt_id)`, require that a child's `attempt_id` match its parent's or carry an explicit `causal_predecessor` reference, and reject trace assembly when a span's consumed-input attempt does not match its declared parent attempt. The check below encodes that rejection as a hard failure so the trace pipeline cannot silently emit a causally corrupt tree. ```python from dataclasses import dataclass, field from typing import Optional class CausalTraceCorruption(RuntimeError): """Raised when a span's declared parent attempt does not match the attempt whose output the span actually consumed.""" pass @dataclass class Span: span_id: str logical_step_id: str attempt_id: int parent_span_id: Optional[str] consumed_from_attempt: Optional[int] = None # attempt whose output fed this span causal_predecessor: Optional[str] = None # set only for commit-partial edges def stitch(spans: list[Span]) -> dict[str, Span]: """Assemble a span set into a causal index, rejecting attempt-inconsistent parenting. Returns span_id -> Span on success.""" by_id = {s.span_id: s for s in spans} index: dict[str, Span] = {} for s in spans: if s.parent_span_id is None: index[s.span_id] = s continue parent = by_id.get(s.parent_span_id) if parent is None: raise CausalTraceCorruption( f"span {s.span_id} names missing parent {s.parent_span_id}" ) # Invariant: a child consumed output from the attempt it declares as # parent, OR it carries an explicit causal_predecessor edge recording # the cross-attempt dependency (commit-partial resolution). consumed = s.consumed_from_attempt if consumed is not None and consumed != parent.attempt_id: if s.causal_predecessor is None: raise CausalTraceCorruption( f"span {s.span_id} consumed attempt {consumed} but is " f"parented to attempt {parent.attempt_id} of " f"{parent.logical_step_id} with no causal_predecessor edge; " f"trace would mis-attribute work to the wrong planner attempt" ) # commit-partial: predecessor must exist and belong to the consumed attempt pred = by_id.get(s.causal_predecessor) if pred is None or pred.attempt_id != consumed: raise CausalTraceCorruption( f"span {s.span_id} causal_predecessor {s.causal_predecessor} " f"does not belong to consumed attempt {consumed}" ) # Attempt-consistent parenting for same-step children. if parent.logical_step_id == s.logical_step_id and parent.attempt_id != s.attempt_id: if s.causal_predecessor is None: raise CausalTraceCorruption( f"span {s.span_id} attempt {s.attempt_id} parented under " f"attempt {parent.attempt_id} of the same logical step " f"{s.logical_step_id} without a causal_predecessor edge" ) index[s.span_id] = s return index ```