# When must an instance refuse to emit a parent span link across an agent handoff: causal-trace replay divergence from GenAI span-link aliasing under retried sub-agent invocations **Date of Record:** 2026 November 26 **Scope:** Applies to multi-agent systems that propagate causal lineage via OTel GenAI span links (`gen_ai.usage.*`, `gen_ai.operation.name`, link attributes `gen_ai.link.kind`, `gen_ai.conversation.id`) across agent-to-agent handoffs where the downstream agent may be retried, resumed, or replayed. Does not apply to single-process synchronous tool calls with no cross-instance handoff, nor to trace reconstruction from `parent_span_id` alone. ## Core Distinction There are two ways an OTel GenAI trace can encode "agent B was invoked by agent A": a **parent-child relationship** (`parent_span_id`, a strict single-parent tree edge) and a **span link** (a multi-valued, non-tree reference with a `link.kind` attribute). The distinction that matters at the agent layer is not tree-vs-graph aesthetics. It is that a parent-child edge is **set-once and immutable** at span creation, whereas a span link is a **mutable, appendable causal assertion** that survives span export and can be added retroactively when a handoff is confirmed. The failure mode lives precisely in this asymmetry. When agent A hands off to agent B and B is retried (timeout, tool fault, checkpoint resume), the retried B invocation is a *new span* with a *new span_id* but the *same causal cause* (A's dispatch). The naive instrumentation attaches A as B's parent. On retry, the second B span also claims A as parent, and now A has two children that are semantically the same logical invocation. Replay engines that reconstruct "what did A cause" by walking children see **duplicate causal descendants**, and the replay diverges from the original execution: token counts double, tool-call side effects appear twice, and any auditor recomputing a causal DAG gets a non-isomorphic graph. The correct invariant: **a retried or resumed downstream invocation must NOT establish a new parent-child edge to the upstream dispatcher; it must emit a span link back to the original attempt and to the dispatcher, with `gen_ai.link.kind = "retry_of"` and a stable `gen_ai.conversation.id`.** The parent edge, when one exists, must point to the *attempt group's* first span, not to the upstream agent. ## What Was Observed A three-agent pipeline (planner → executor → verifier) instrumented with OTel GenAI semantic conventions. The executor made an LLM call that timed out at 90s; the orchestration layer retried the executor invocation. Both attempts exported spans. Observed trace fragment (attributes abbreviated): | span_id | name | parent_span_id | links | gen_ai.conversation.id | |---|---|---|---|---| | `s_plan` | `invoke_agent planner` | `s_root` | — | `conv-7` | | `s_exec_1` | `invoke_agent executor` | `s_plan` | — | `conv-7` | | `s_exec_2` | `invoke_agent executor` | `s_plan` | — | `conv-7` | | `s_verify` | `invoke_agent verifier` | `s_exec_2` | — | `conv-7` | The replay harness reconstructed the causal DAG by following `parent_span_id`. It counted **two** planner→executor causal edges and attributed the verifier's input to `s_exec_2` only, silently dropping `s_exec_1`'s partial tool output (which the verifier had actually consumed via a shared scratchpad). Replay divergence: reconstructed token spend was 1.7× the recorded spend because both executor attempts' `gen_ai.usage.output_tokens` were summed as distinct causal contributions, and the verifier's reasoning replay failed because its true input provenance (`s_exec_1` scratchpad write, `s_exec_2` continuation) was not representable as a single parent edge. A second observation: when the retry logic was *changed* to emit a link instead (`s_exec_2` → link to `s_exec_1` with `kind="retry_of"`, and `s_exec_2` parented to an attempt-group span `s_exec_group`), replay became deterministic. The DAG walker was updated to treat `retry_of` links as **collapsing** two spans into one logical node for token accounting, while preserving both for side-effect audit. ## Condition of Validity The parent-edge-as-causal-cause pattern is valid **only** when the following hold simultaneously: 1. The downstream invocation is **at-most-once** (no retries, no checkpoint resume, no speculative re-execution). 2. The downstream agent's tool calls are **idempotent or side-effect-free** with respect to replay (so duplicate causal attribution does not double-count effects). 3. The trace consumer reconstructs causality from `parent_span_id` **and** treats the trace as a tree, not a DAG. The moment any one of these breaks—most commonly (1), since LLM calls time out and agents are routinely retried—the parent-edge pattern produces **causal aliasing**: two distinct span_ids claiming the same causal parent, which a tree-walking replay engine interprets as two independent causal branches. This is not a logging bug; it is a **semantic corruption of the causal graph** that propagates into token accounting, cost attribution, and—critically—verifier consensus, because an auditor agent reconstructing "what the executor actually did" sees two divergent histories. The invariant to enforce: **for any `gen_ai.conversation.id`, the set of spans with `gen_ai.operation.name = "invoke_agent"` and a given upstream dispatcher must form a chain of `retry_of` links, not a fan-out of parent edges.** Equivalently: at most one child span per (dispatcher_span_id, downstream_agent_id, conversation_id) may carry `parent_span_id = dispatcher_span_id`; all subsequent attempts must be linked. ## Decision Boundary The instrumentation layer must decide, at span-creation time, whether a downstream invocation is a *first attempt* (parent edge permitted) or a *retry/resume* (link required). The decision cannot be made from the span itself—it requires the dispatcher to pass an **attempt token**: a stable identifier derived from `hash(conversation_id, dispatcher_span_id, downstream_agent_id, logical_invocation_seq)`. The rule: - If the attempt token is **unseen** in the conversation's attempt registry → create parent edge to dispatcher, register token. - If the attempt token is **seen** → do NOT set `parent_span_id` to the dispatcher; set it to the attempt-group span (or leave null), and emit a link `{target: previous_attempt_span_id, kind: "retry_of", attrs: {attempt_token, attempt_index}}`. The boundary is sharp: **the registry lookup must be atomic with span creation**, or two concurrent retries (a race between a timeout-triggered retry and a checkpoint-resume) can both observe "unseen" and both emit parent edges—reproducing the aliasing under concurrency. This is the same class of race as baton-passing in swarm coordination, but localized to the trace emitter. A second boundary: when the downstream agent is a **different process** (true A2A handoff), the dispatcher cannot know the downstream's attempt history. Here the attempt token must be propagated in the handoff payload (e.g., A2A message metadata or MCP `_meta`), and the downstream emitter trusts the token. If the token is absent, the safe default is **link-only, no parent edge**—degrade causality to a link rather than risk aliasing. ## Operational Effect Enforcing the invariant changes three things downstream: 1. **Replay determinism.** A DAG-aware replay engine that collapses `retry_of` chains recovers the exact token spend and tool-call sequence of the original execution. Tree-walking engines must be upgraded or they will silently double-count; the trace itself now carries enough structure (`attempt_token`, `attempt_index`) for either engine to be correct. 2. **Verifier consensus stability.** When an auditor agent reconstructs executor behavior from the trace, it now sees one logical executor node with N attempts, each with its own usage and tool spans, rather than N sibling causal branches. Conflicting peer/auditor consensus caused by "two histories" disappears because there is one history with an explicit retry structure. 3. **Cost and budget attribution.** Token-budget accounting keyed on causal descendants now attributes retry overhead to the *attempt group*, not to the dispatcher's child count. This prevents the failure where a planner's "causal cost" inflates with every executor retry, triggering spurious budget-exhaustion mid-execution. The cost of the invariant is a small amount of emitter-side state (the attempt registry, scoped to `conversation_id`) and the discipline of propagating attempt tokens across handoffs. The cost of *not* enforcing it is non-reproducible traces, which is the one thing an agent system cannot tolerate, because replay is the only mechanism by which a verifier can independently confirm what an executor did. ```python import hashlib from dataclasses import dataclass, field from typing import Optional class CausalAliasingError(RuntimeError): """Raised when a downstream agent invocation would create a duplicate parent-child causal edge to the same dispatcher within one conversation, corrupting replay determinism.""" pass @dataclass class SpanRecord: span_id: str name: str parent_span_id: Optional[str] links: list = field(default_factory=list) class AttemptRegistry: """Tracks attempt tokens per conversation to enforce the invariant: at most one parent edge per (dispatcher, downstream_agent, conversation).""" def __init__(self): # conversation_id -> set of attempt tokens already seen self._seen: dict[str, set[str]] = {} @staticmethod def attempt_token(conversation_id: str, dispatcher_span_id: str, downstream_agent_id: str, logical_seq: int) -> str: raw = f"{conversation_id}|{dispatcher_span_id}|{downstream_agent_id}|{logical_seq}" return hashlib.sha256(raw.encode()).hexdigest()[:16] def claim(self, conversation_id: str, token: str) -> bool: """Atomically claim an attempt token. Returns True if first attempt.""" bucket = self._seen.setdefault(conversation_id, set()) if token in bucket: return False bucket.add(token) return True def emit_invoke_agent_span( registry: AttemptRegistry, conversation_id: str, dispatcher_span_id: str, downstream_agent_id: str, logical_seq: int, new_span_id: str, previous_attempt_span_id: Optional[str] = None, attempt_group_span_id: Optional[str] = None, ) -> SpanRecord: token = registry.attempt_token( conversation_id, dispatcher_span_id, downstream_agent_id, logical_seq ) is_first = registry.claim(conversation_id, token) if is_first: # First attempt: parent edge to dispatcher is permitted. return SpanRecord( span_id=new_span_id, name=f"invoke_agent {downstream_agent_id}", parent_span_id=dispatcher_span_id, links=[], ) # Retry / resume: MUST NOT parent to dispatcher. Link instead. if previous_attempt_span_id is None: raise CausalAliasingError( f"Retry of {downstream_agent_id} in conversation {conversation_id} " f"has no previous_attempt_span_id; cannot emit retry_of link. " f"Refusing to emit parent edge to dispatcher {dispatcher_span_id} " f"as this would alias causal lineage and break replay." ) parent = attempt_group_span_id # may be None -> root of attempt group return SpanRecord( span_id=new_span_id, name=f"invoke_agent {downstream_agent_id}", parent_span_id=parent, links=[{ "target_span_id": previous_attempt_span_id, "kind": "retry_of", "attrs": {"attempt_token": token, "attempt_index": logical_seq}, }], ) ```