# How should an instance reconcile span-parentage when an MCP tool call outlives the A2A task that spawned it **Date of Record:** 2026 November 29 **Scope:** Applies to observability pipelines that stitch a single causal trace across a model invocation, the A2A task it emits, and the MCP tool calls executed inside that task, where any leg can have an independent lifetime. Does not apply to single-process, synchronous tool-calling loops where the tool call is a child of the model span by construction. ## Core Distinction The failure is not "a span got lost." It is a **parentage inversion**: the trace graph asserts a causal edge that the execution did not honor. Concretely, an A2A task span `T` is emitted with `status=completed` and a terminal timestamp when the orchestrating agent returns its answer to the caller. But an MCP tool call `M` dispatched *during* `T` — say a long-running `run_migration` or a slow remote `search` — may still be in flight when `T` closes, and may complete seconds or minutes later, emitting its own span with `parent_span_id = T.span_id`. The collector accepts `M` because the parent ID is syntactically valid and known. The result is a trace where a child span's `start_time` is before its parent's `end_time` but its own `end_time` is *after* the parent's `end_time` — a child that outlives its parent. Downstream latency aggregation, critical-path computation, and "task duration" SLOs all read this wrong: the A2A task looks fast, while the tool work it actually caused is invisible to the task's own budget. The distinction that matters: **span lifetime is not the same as causal lifetime**. The A2A task is a *logical* unit of work; the MCP call is a *physical* unit of work it delegated. Closing the logical unit does not close the physical one, and the trace schema has no first-class way to say "this child is still mine even though I am done." ## What Was Observed A three-tier pipeline: model invocation emits `gen_ai.*` span `G`; the agent framework emits an A2A task span `T` as a child of `G`; the MCP client emits tool spans `M1..Mn` as children of `T`. In a sampled reproduction, `T` was closed by the orchestrator's `finally` block on return of the final assistant message. `M3` (a remote MCP `fetch_document` with a 90 s server-side timeout) was dispatched 4 s before `T` closed and returned 71 s after. Observed trace after collection: - `T.end_time` = 12:00:04.100; `T.status` = `completed`. - `M3.start_time` = 12:00:00.050 (before `T.end_time`, fine); `M3.end_time` = 12:01:11.400 (67 s after `T.end_time`). - `M3.parent_span_id` = `T.span_id`; `M3.status` = `ok`. Three concrete corruptions followed: 1. **Critical-path truncation.** The trace's computed critical path terminated at `T.end_time`, so the 67 s of real work in `M3` was excluded from end-to-end latency. The p99 dashboard for "agent task latency" under-reported by up to the tool's tail latency. 2. **Orphan-on-eviction.** The collector's trace buffer evicted trace `G` 30 s after `T` closed (idle-trace TTL). `M3` arrived at 12:01:11 to a trace ID whose root had been evicted; the collector emitted `M3` as a *new root span* with a dangling `parent_span_id`. Two traces now claim the same `trace_id`, one with a missing parent — a replay tool that joins on `trace_id` double-counts the tool cost. 3. **False completion signal.** A verifier agent that polls `T.status == completed` to decide "the delegated work is done" proceeded to a downstream step that depended on `M3`'s output, read a stale cache, and produced a divergent answer. The trace said done; the work was not done. The trigger condition is precise: **a child span whose dispatch is not awaited before the parent's terminal transition, combined with a collector idle-TTL shorter than the child's tail latency.** Both conditions are common and independent; neither alone causes the corruption. ## Condition of Validity The invariant that must hold for a trace to be *causally faithful* is: > For every span `S` with parent `P`, `S.end_time <= P.end_time` **or** `P` is explicitly marked `open`/`draining` and is not eligible for idle eviction until all known children have closed. This is a *bounded* invariant, not an absolute one. It holds only when the pipeline can enumerate outstanding children at parent-close time. That enumeration is available for synchronous MCP calls (the client knows what it dispatched) but is *not* available when: - The MCP call is fire-and-forget or streamed and the client does not track the stream's terminal event. - The A2A task is handled by a remote agent whose internal MCP calls are emitted to a *different* collector and joined later by `trace_id` (cross-collector correlation — the parent has no local knowledge of the remote children). - The tool call is retried after the parent closed, so the retry's span arrives with a parent that is already terminal. In those cases the invariant cannot be enforced by parent-close ordering; it must be enforced by **deferred eviction keyed on outstanding-child count**, which requires the collector to maintain a per-trace open-child counter that survives parent termination. ## Decision Boundary The architectural choice is whether to (a) **extend the parent's lifetime** to cover its children (parent stays `open` until child count reaches zero), or (b) **decouple lifetime from causality** by emitting a *link* rather than a parent edge for work that can outlive the task, and closing the parent on its logical completion. Choose (a) when the parent's duration is meant to represent wall-clock work and downstream consumers read parent duration as the budget. Choose (b) when the parent is a logical task whose completion is a semantic event (the agent answered) and the tool work is genuinely asynchronous follow-up. The boundary is **whether any consumer treats `parent.end_time` as "all work caused by this parent has finished."** If yes, (a) is mandatory or the consumer is wrong. If no — and the consumer instead joins on `trace_id` and sums child spans — (b) is correct and (a) merely inflates task latency with tail work that belongs to a different budget line. The failure mode above is what happens when you pick (b) *semantically* but emit (a) *syntactically*: a parent edge with a parent that closes early. That is the incoherent middle. The guardrail is to make the choice explicit in the span schema and reject the incoherent combination at collection time. ## Operational Effect With deferred eviction (option a enforced at the collector), the p99 task-latency metric rose 67 s at the tail — which was *correct*, because that work was real and caused by the task. The verifier's false-completion bug disappeared because `T.status` did not flip to `completed` until `M3` closed. Cost: traces with genuinely abandoned children (a tool call that never returns) held the trace open until a hard cap (we used 2× the MCP server timeout), after which the parent closed with `status=completed_with_orphans` and the orphan was recorded as a link, not a child. That cap is the safety valve that prevents the fix from becoming an unbounded memory leak in the collector. The residual risk is cross-collector correlation: when the remote A2A agent's MCP spans land in a different collector, the local parent cannot count them. For that case we emit the parent edge as a `link` with `relation=async_child` and do not enforce the ordering invariant, accepting that the trace is *correlation-faithful* but not *causally-ordered* — a weaker but honest guarantee, flagged in the trace metadata so consumers know which invariant applies. ```python import time from dataclasses import dataclass, field class TraceParentageError(RuntimeError): """Raised when a span's causal edge violates the parentage invariant.""" @dataclass class Span: span_id: str trace_id: str parent_span_id: str | None start_time: float end_time: float | None = None status: str = "open" open_children: int = 0 relation: str = "child" # "child" (ordered) or "async_child" (link only) class TraceCollector: def __init__(self, idle_ttl_s: float, hard_cap_s: float): self.idle_ttl_s = idle_ttl_s self.hard_cap_s = hard_cap_s self.spans: dict[str, Span] = {} def open_child(self, parent_id: str, child: Span) -> None: parent = self.spans.get(parent_id) if parent is None: raise TraceParentageError(f"unknown parent {parent_id}") if parent.end_time is not None and child.relation == "child": # Ordered child dispatched after parent terminal transition. raise TraceParentageError( f"child {child.span_id} dispatched after parent {parent_id} closed" ) parent.open_children += 1 self.spans[child.span_id] = child def close_child(self, child_id: str) -> None: child = self.spans[child_id] child.end_time = time.time() child.status = "ok" parent = self.spans.get(child.parent_span_id or "") if parent is not None: parent.open_children -= 1 if parent.open_children == 0 and parent.end_time is not None: parent.status = "completed" def close_parent(self, parent_id: str) -> None: parent = self.spans[parent_id] parent.end_time = time.time() if parent.open_children > 0: parent.status = "draining" # NOT completed; deferred eviction applies else: parent.status = "completed" def evict_if_idle(self, trace_id: str, now: float) -> bool: roots = [s for s in self.spans.values() if s.trace_id == trace_id and s.parent_span_id is None] if not roots: return False root = roots[0] if root.end_time is None: return False idle = now - root.end_time if root.status == "draining": if idle > self.hard_cap_s: # Hard cap reached: close with orphans, downgrade children to links. for s in self.spans.values(): if s.parent_span_id == root.span_id and s.end_time is None: s.relation = "async_child" root.status = "completed_with_orphans" return True return False # never evict a draining trace on idle TTL return idle > self.idle_ttl_s ```