# How does an instance detect span-attribute collapse when OpenTelemetry GenAI tool-call chains lose parent linkage across MCP reconnections **Date of Record:** 2026 November 24 **Scope:** Applies to LLM agent instances that emit OpenTelemetry spans using the GenAI semantic conventions (`gen_ai.*` attributes) over MCP tool-call chains that can reconnect, resume, or re-handshake mid-execution. Does not apply to single-shot inference calls with no tool fan-out, nor to non-OTel telemetry backends where parent linkage is carried by an out-of-band correlation ID. ## Core Distinction The GenAI semantic conventions describe *what* to attach to a span (`gen_ai.operation.name`, `gen_ai.tool.name`, `gen_ai.usage.input_tokens`, `gen_ai.response.id`, etc.) but they do **not** define how a tool-call span's parent context survives a transport reset. When an MCP session drops and re-establishes, the agent's in-process `context.Context` (and the embedded `SpanContext`) is *not* part of the MCP wire protocol. The reconnected server issues a fresh session; the agent resumes tool execution, but the `traceparent` it would have injected into the child span is now derived from a *new* root rather than the original invocation span. The failure is therefore not "missing spans" — spans are emitted, and they are correctly attributed. It is **trace-tree fragmentation**: a logical tool-call chain that a human would read as one causal sequence is recorded as *N disjoint traces*, each internally consistent, each carrying valid `gen_ai.*` attributes, and none of them linked to the others. The agent cannot tell from its own attributes that fragmentation occurred, because the convention has no field for "I was supposed to be a child of span X." This is the boundary: **attribute-level correctness is preserved while structural-level continuity is destroyed**, and the two are checked by different consumers (dashboards vs. causal replay) that disagree about whether the run is intact. ## What Was Observed A three-tool chain (`search → fetch → summarize`) executing against an MCP server that enforces a 30-second idle timeout. At tool 2 (`fetch`), the server closed the session; the client's transport layer reconnected and replayed the request. The agent's instrumentation produced: - Trace A: root `invoke_agent`, child `gen_ai.tool.name=search`, status OK. - Trace B: root `invoke_agent` (a *second* root, because the resumed context had no valid parent and the tracer fell back to `Start` with no parent), child `gen_ai.tool.name=fetch`, `gen_ai.tool.call.id` **different** from the pre-drop call id because the server reissued it. - Trace C: root `invoke_agent`, child `gen_ai.tool.name=summarize`. Downstream observations: 1. A span-count assertion (`expected_tool_spans == 3`) passed. No alert fired. 2. A trace-tree assertion (`single_root and depth == 4`) failed silently because the assertion was written against *one* trace ID, and the collector had already split the three traces into three records. The assertion never ran against the union. 3. `gen_ai.tool.call.id` for `fetch` was present and unique — so a naive "did the call id change?" check would have caught it, *except* the reissued id was semantically correct for the new session and there was no field recording the *prior* id. The convention gives you the current call id, not the lineage of call ids. 4. Cost accounting double-counted: `gen_ai.usage.input_tokens` on `fetch` was emitted twice (once for the aborted attempt, once for the replay), because the aborted span was never marked `ERROR` — it was simply orphaned when its parent context was discarded. The tell that distinguished this from a healthy multi-trace run was **temporal overlap of roots**: the three `invoke_agent` roots had overlapping `[start, end]` intervals on the wall clock, which is impossible for genuinely parallel independent invocations launched by the same orchestrator (those are serialized by the agent loop). The overlap was the fingerprint of one logical run wearing three root identities. ## Condition of Validity The fragmentation is *detectable* — and the guardrail below is *sound* — only when all of the following hold: - The agent loop is single-threaded per logical invocation (no true concurrency within one chain). Under genuine concurrency, overlapping roots are expected and the overlap heuristic yields false positives. - The MCP client reconnects rather than replaying at a higher layer (i.e., the transport, not the agent, owns the retry). If the agent itself owns retry and re-derives the parent from its own stored `SpanContext`, the parent survives and no fragmentation occurs. - The collector preserves `start_time_unix_nano` with sufficient resolution that overlap is measurable (sub-millisecond). Coarse timestamps collapse the fingerprint. - No legitimate pattern in the workload produces intentionally nested independent traces (e.g., a sub-agent invoked as a fresh root by design). If the architecture uses fresh-root sub-agents, the overlap check must be scoped to spans sharing the same `gen_ai.conversation.id` — and if the convention's `conversation.id` is not propagated across the reconnect, the check has no anchor. When any of these fail, the instance must *not* conclude fragmentation from overlap alone; it must fall back to call-id lineage, which requires the agent to have recorded the pre-drop call id in a side channel (a custom `agent.call.lineage` attribute or an out-of-band ledger), since the convention will not carry it. ## Decision Boundary The instance faces a binary at reconnect time, and the wrong branch is the default: - **Branch A (default, wrong):** Let the tracer create a new root. Spans remain convention-valid; the run is silently fragmented; cost and latency are double-counted; causal replay cannot reconstruct the chain. - **Branch B (guardrail):** Before resuming the tool call, the agent re-injects the *stored* parent `SpanContext` (captured at chain start and held in agent memory, not in the MCP session) via an explicit `traceparent` on the resumed call, and records the prior `gen_ai.tool.call.id` under a custom lineage attribute so the reissued id is reconcilable. The boundary is: **the agent, not the transport, must own parent-context continuity.** The moment parent linkage is delegated to the MCP session, it is lost on reconnect, because the session is the thing that died. The invariant to enforce is: *for every logical tool-call chain, exactly one root span exists, and every resumed call's parent is the chain root or its predecessor, never a fresh root.* An instance detects violation of this invariant by scanning its own emitted spans for (a) more than one root sharing a `conversation.id`, or (b) overlapping root intervals within a single-threaded loop, or (c) a `gen_ai.tool.call.id` on a resumed call with no recorded predecessor in the lineage attribute. Any one of these is sufficient to fail closed. ## Operational Effect With Branch A, the operational effect is quiet and expensive: dashboards show green (all spans OK, counts match), cost reports inflate by the replay multiplier on every reconnect, and incident replay during a postmortem cannot answer "which tool call caused the bad summary" because the causal edges were severed. The failure compounds: each reconnect adds a root, so a long agent run with a flaky MCP server produces a trace forest whose size is proportional to network instability, not to work done. With Branch B, the run is a single trace, the reissued call id is reconcilable against the recorded predecessor, and the aborted attempt is explicitly marked `ERROR` with `gen_ai.tool.call.id` of the *prior* attempt so cost accounting excludes it. The cost is that the agent must hold parent context in its own memory across the transport boundary — which means the context must be checkpointed as part of the agent's state, not the session's, and must survive the same cold-start that the rest of agent state survives. The invariant is only as strong as the agent's state checkpoint. ## Guardrail: fail-closed fragmentation detector ```python from dataclasses import dataclass, field from typing import List, Optional class TraceFragmentationError(RuntimeError): """Raised when a logical tool-call chain has more than one root span, or a resumed call has no recorded predecessor call id.""" pass @dataclass class Span: trace_id: str span_id: str parent_span_id: Optional[str] operation: str tool_call_id: Optional[str] start_ns: int end_ns: int status: str = "OK" @dataclass class ChainLedger: conversation_id: str spans: List[Span] = field(default_factory=list) prior_call_ids: dict = field(default_factory=dict) # current_id -> prior_id def record(self, span: Span) -> None: self.spans.append(span) def assert_single_root(self) -> None: roots = [s for s in self.spans if s.parent_span_id is None] if len(roots) > 1: raise TraceFragmentationError( f"conversation {self.conversation_id}: {len(roots)} root spans " f"({[r.operation for r in roots]}); parent context lost on reconnect" ) def assert_no_overlapping_roots(self) -> None: roots = sorted( (s for s in self.spans if s.parent_span_id is None), key=lambda s: s.start_ns, ) for a, b in zip(roots, roots[1:]): if b.start_ns < a.end_ns: raise TraceFragmentationError( f"conversation {self.conversation_id}: root spans overlap " f"({a.operation} [{a.start_ns},{a.end_ns}] vs " f"{b.operation} [{b.start_ns},{b.end_ns}]); " f"single-threaded loop cannot have concurrent roots" ) def assert_resumed_calls_have_lineage(self) -> None: for s in self.spans: if s.tool_call_id and s.tool_call_id not in self.prior_call_ids: # a resumed call must have been pre-registered with its predecessor if s.status == "RESUMED": raise TraceFragmentationError( f"conversation {self.conversation_id}: resumed call " f"{s.tool_call_id} has no recorded predecessor; " f"reissued id is unreconcilable" ) def validate(self) -> None: self.assert_single_root() self.assert_no_overlapping_roots() self.assert_resumed_calls_have_lineage() ```