# In what scenarios must an instance refuse to stitch a parent trace across an MCP stdio→SSE transport re-dial, and how does the correlation invariant break at the handoff **Date of Record:** 2026 November 24 **Scope:** Applies to observability pipelines that reconstruct a single distributed trace spanning MCP client↔server tool calls, A2A agent-to-agent hops, and LLM inference spans, where the MCP leg may silently re-establish its underlying transport mid-trace. Does not apply to single-process traces with no transport re-dial, nor to pipelines that treat each MCP session as a hard trace root by policy. ## Core Distinction The failure is not "missing spans." It is *false parentage*: a continuation span that belongs to a *new* MCP transport session gets stitched under the *old* session's parent because the correlation key (`trace_id` + `span_id`) survived the transport boundary while the *transport identity* did not. Observability tooling that correlates on W3C `traceparent` alone will happily fuse two causally distinct execution segments into one nominal trace, producing a waterfall that looks complete but is causally wrong. The distinction that matters: **correlation-key continuity is not causal continuity.** A `traceparent` propagated in a `params._meta` field of a JSON-RPC message is a *claim* of continuity; the transport re-dial is *evidence against* it. The specific mechanism: MCP over stdio has no session token that the observability layer can bind to a socket. When the client re-dials (server restart, stdio pipe break, or a switch to SSE for a streaming tool), the JSON-RPC `id` namespace resets to 1 and the process identity changes, but the agent runtime—holding the logical conversation—re-injects the *same* `traceparent` into the first request on the new transport. The collector sees a child span whose parent span was emitted by a now-dead process on a now-dead pipe. If it stitches, the LLM inference span that *caused* the re-dial and the tool span that *ran after* it appear as siblings under one parent, erasing the restart as a causal event and hiding latency/error attribution. ## What Was Observed A three-hop trace: `llm.inference` (span A, agent process P1) → `mcp.tools/call` (span B, MCP server process S1 over stdio) → after S1 crashed, client re-dialed S1' over SSE → `mcp.tools/call` (span C, server S2 over SSE) → `a2a.send` (span D) → `llm.inference` (span E). The pipeline emitted a single trace with A as root, B and C as children of A, D child of C, E child of D. Two anomalies surfaced only under scrutiny: 1. **Wall-clock inversion.** C's `start_time` was 40ms *before* B's `end_time`, yet C is a child of A and a sibling of B. In a correct trace, C's parent should be a *new* root or a span representing the re-dial event; instead the collector back-dated C's start to A's end to satisfy the parent's time envelope—a silent `start_time` clamp. 2. **`id` collision.** B carried JSON-RPC `id: 1` on the S1 stdio session; C carried `id: 1` on the S2 SSE session. A dedup pass keyed on `(trace_id, rpc_id)` collapsed them, dropping C's response payload from the trace and attaching B's error to C's span. The failure trace: `S1 process exit` → `client transport error` → `re-dial` → `traceparent re-injected from in-memory context` → `collector matches parent by span_id` → `parent span's process_uid no longer live` → **stitch succeeds anyway** → causally invalid waterfall. ## Condition of Validity The stitching is only valid when the collector can assert *transport-session identity continuity*, not merely correlation-key continuity. Concretely, a parent-child edge across an MCP transport boundary is valid iff all hold: - The child's `transport.session_id` (or `process_uid` + socket/pipe inode) equals the parent's, **or** the child carries an explicit `redial_event` span that names the prior session as its predecessor. - The JSON-RPC `id` namespace is scoped per transport session in the correlation key (`(trace_id, session_id, rpc_id)`), so resets to 1 do not collide. - No `start_time` clamp was applied to satisfy a parent envelope; a clamp is a positive signal that the edge is fabricated. If the collector cannot evaluate these—because the MCP server emits no session identifier—the invariant is *unverifiable*, and the correct action is to refuse the stitch, not to assume continuity from the `traceparent`. ## Decision Boundary Refuse to stitch (and emit the child as a new trace root tagged `orphaned_redial`) when **any** of: - Parent span's `process_uid` is not in the live-process registry at child `start_time`, **and** the intervening gap contains a transport-error span or a re-dial event. - `rpc_id` collides across two different `transport.session_id` values under the same `trace_id`. - A `start_time` clamp would be required to nest the child under the candidate parent. Stitch normally when the transport session is unchanged, or when an explicit re-dial span bridges the two sessions and the child's parent is that bridge span (not the pre-restart span). The boundary is *not* "did the traceparent match"—it is "does the transport session identity chain hold, with the restart represented as a first-class span rather than absorbed into a sibling edge." ## Operational Effect Downstream consequences of a wrong stitch are not cosmetic: - **Error attribution flips.** B's crash error attaches to C's span (via the dedup collision), so the agent's retry logic and the SLO dashboard blame the *successful* post-restart tool call. Incident review chases the wrong server. - **Latency budgets lie.** The re-dial cost (often 200–800ms for SSE handshake + capability negotiation) is folded into A's envelope as "inference time," inflating the LLM span and hiding the transport as the true p99 contributor. - **A2A warrant propagation is misread.** Span D's A2A hop inherits C as parent; if C is mis-attributed, the downstream agent's cryptographic warrant chain (bound to the trace) appears to originate from a session that never issued it—a verification replay discrepancy that surfaces only when an auditor re-runs the trace. The guardrail: bind correlation to transport-session identity, treat re-dial as a first-class span, and make the collector's default on unverifiable continuity be *refuse and root*, never *stitch and clamp*. The pipeline must fail loud at the stitch boundary rather than produce a plausible-looking waterfall. ```python from dataclasses import dataclass from typing import Optional class TraceStitchRefused(RuntimeError): """Raised when a parent-child edge would fuse causally distinct transport sessions.""" pass @dataclass(frozen=True) class Span: trace_id: str span_id: str parent_span_id: Optional[str] process_uid: str transport_session_id: str rpc_id: Optional[int] start_time: float end_time: float is_redial_bridge: bool = False def stitch_edge(parent: Span, child: Span, live_process_uids: set[str], redial_window: tuple[float, float] | None) -> str: """Return the resolved parent span_id, or raise TraceStitchRefused.""" if child.parent_span_id != parent.span_id: raise TraceStitchRefused("parent_span_id mismatch; not a candidate edge") if child.transport_session_id == parent.transport_session_id: return parent.span_id # same transport session, valid stitch # Transport session changed: require an explicit redial bridge. if parent.is_redial_bridge: return parent.span_id parent_dead = parent.process_uid not in live_process_uids in_redial = ( redial_window is not None and redial_window[0] <= child.start_time <= redial_window[1] ) if parent_dead and in_redial: raise TraceStitchRefused( f"refusing stitch: parent {parent.span_id} (proc {parent.process_uid}) " f"is dead and child {child.span_id} starts inside redial window; " f"emit child as new root tagged orphaned_redial" ) # rpc_id namespace collision across sessions under same trace. if (parent.rpc_id is not None and child.rpc_id is not None and parent.rpc_id == child.rpc_id and parent.transport_session_id != child.transport_session_id): raise TraceStitchRefused( f"rpc_id {child.rpc_id} collides across sessions " f"{parent.transport_session_id}->{child.transport_session_id}; " f"scope key to (trace_id, session_id, rpc_id)" ) # A start_time clamp would be required to nest child under parent. if child.start_time < parent.end_time and child.start_time < parent.start_time: raise TraceStitchRefused( f"child {child.span_id} start {child.start_time} precedes parent " f"start {parent.start_time}; nesting would require time clamp" ) return parent.span_id ```