# How does an instance detect orphaned tool latency when span links break across a resumed agent loop **Date of Record:** 2026 November 23 **Scope:** Applies to agents that persist execution as a trace of spans (parent/child links, tool-call spans, LLM-call spans) and that can be checkpointed and resumed across process restarts, model swaps, or host migrations. Does not apply to single-shot, single-process agents whose entire loop lives within one trace context and one process lifetime. ## Core Distinction Observability for an agent loop is not log aggregation. The distinction that matters is between *causal attribution* and *temporal proximity*. A tool-call span that is 4.2 seconds long tells you nothing about whether the agent's reasoning degraded because that tool was slow, or because the model spent 4.2 seconds of wall-clock time in a retry loop that the tool span never captured. Standard tracing assumes a span's parent link is intact and that the span's duration is a faithful proxy for the work it names. In a resumed agent loop, both assumptions break: the parent link is reconstructed from a checkpoint rather than inherited from a live context, and the span's duration may include queue time, cold-start time, or a mid-span checkpoint write that blocks the loop. The non-obvious failure: when a loop resumes after a checkpoint, the resumed process constructs a *new* trace root and re-parents the in-flight tool span under it, because the original parent context object did not survive serialization. The tool span now appears as a child of the resume event, not of the reasoning step that issued it. Latency attribution silently reassigns: the tool looks like it was invoked by the resume machinery, and the reasoning step that actually called it shows zero tool time. Drift detection downstream, which correlates "reasoning step latency" against "tool latency," then concludes the model got faster and the tools got slower — the exact inverse of what happened. ## What Was Observed A three-tool agent loop (search → fetch → summarize) on a 90-minute task budget. The loop checkpointed every 30 tool calls. At checkpoint 4, the host was migrated and the process restarted. The resumed agent loaded the checkpoint, which contained the message history, the pending tool call (a `fetch` with a 12-second expected latency), and a serialized trace context ID — but not the live `Span` object, because the tracing SDK's span handle is not serializable and the checkpoint writer had stored only `context.trace_id` and `context.span_id` as strings. On resume, the SDK's `start_as_current_span("tool.fetch")` call created a span whose parent was the *resume handler's* span, not the original `reasoning.step.7` span. The original `reasoning.step.7` span had already been closed and flushed before the checkpoint, so it existed only as a completed leaf in the backend. The new `tool.fetch` span was orphaned: its `parent_span_id` pointed at `resume.handler`, which had a duration of 40 ms, while the tool span itself carried 11.8 seconds of latency. The drift detector, configured to flag "tool latency > 3x baseline for the preceding reasoning step," fired zero alerts. The reasoning step `reasoning.step.7` had a recorded duration of 200 ms (the time to emit the tool call, before the checkpoint), and its child set contained no tool span. The tool latency was attributed to `resume.handler`, which was not a reasoning step and therefore not in the detector's correlation set. The 11.8-second tool call was, from the detector's perspective, invisible. A second-order effect appeared 40 minutes later: the agent's *subsequent* reasoning steps were measurably slower (1.4x baseline), because the model was now working with a context that included a truncated tool result (the fetch had partially failed at 11.8 s and returned a 200-token error instead of the expected 4,000-token document). The drift detector attributed this slowdown to "context growth" and recommended compaction. The actual cause was the orphaned tool span's partial failure, which no span link connected to the reasoning steps that consumed its output. ## Condition of Validity The failure requires all of the following to hold simultaneously: 1. The loop checkpoints between issuing a tool call and receiving its result (i.e., the checkpoint boundary is *inside* the tool-call lifetime, not at a step boundary). 2. The tracing SDK does not serialize live span handles into the checkpoint, only string IDs. 3. The resumed process creates a new root span for the resume event rather than reconstructing the parent context from the stored IDs. 4. The drift detector correlates tool latency to reasoning steps via parent-child links rather than via explicit causal metadata (e.g., a `caused_by_step_id` attribute written into the tool span at issue time). If any of (1)–(4) is false, the orphan does not occur or does not corrupt attribution. In particular, writing the issuing step's ID as a *span attribute* (not a parent link) at tool-issue time makes the correlation robust to re-parenting, because attributes survive serialization even when the span handle does not. ## Decision Boundary The boundary is whether span *links* or span *attributes* carry the causal edge. Links are cheap and standard, but they are structural: they describe the trace tree, and the tree is rebuilt on resume. Attributes are redundant and verbose, but they are data: they travel with the span record and survive any re-parenting. The decision rule: **any causal edge that must survive a checkpoint must be encoded as a span attribute, not as a parent link.** Parent links are for tree rendering; attributes are for attribution. An agent that relies on parent links for latency attribution across resume boundaries is relying on a structure that the resume path does not guarantee. A secondary boundary: the drift detector must distinguish "reasoning step with no tool child" from "reasoning step whose tool child was orphaned." The former is a legitimate no-tool step; the latter is a data-loss event. The detector cannot tell them apart from the tree alone, which is why the attribute-based edge is not optional. ## Operational Effect The observable consequence is a *silent inversion of the latency signal*: tools appear faster (their spans are attributed to resume machinery, which is excluded from the reasoning correlation set) and reasoning appears to slow for unrelated reasons (context growth from partial tool results). The agent's own self-reports — if it summarizes its trace — will be wrong in the same direction, because it reads the same corrupted tree. The guardrail is a resume-time invariant check: on loading a checkpoint, the agent must verify that every pending tool call has a recorded `caused_by_step_id` attribute, and must reconstruct the parent context from that attribute rather than from the resume handler. If the attribute is absent, the resume must fail closed rather than proceed with an orphaned span, because proceeding produces a trace that is not merely incomplete but *actively misleading* to every downstream consumer, including the agent's own drift detector. The following check enforces that invariant at resume time. ```python class OrphanedToolSpanError(RuntimeError): """Raised when a resumed agent loop cannot attribute a pending tool call to its issuing reasoning step.""" pass def resume_loop(checkpoint: dict, tracer) -> None: """ Reconstruct trace context for a resumed agent loop. Invariant: every pending tool call must carry a 'caused_by_step_id' attribute recorded at issue time. Parent links are NOT trusted across the resume boundary because the resume path re-roots the trace and silently reassigns latency. """ pending = checkpoint.get("pending_tool_calls", []) for call in pending: step_id = call.get("caused_by_step_id") if not step_id: raise OrphanedToolSpanError( f"Pending tool call {call.get('tool_name')!r} (id={call.get('call_id')!r}) " f"has no caused_by_step_id attribute. Resuming would re-parent this span " f"under the resume handler and orphan its latency from the issuing reasoning " f"step. Fail closed: do not resume." ) # Reconstruct parent context from the attribute, not from the live tree. parent_ctx = tracer.extract_context(step_id) if parent_ctx is None: raise OrphanedToolSpanError( f"caused_by_step_id={step_id!r} does not resolve to a known span. " f"The issuing reasoning step was flushed or lost; latency attribution " f"for {call.get('tool_name')!r} cannot be reconstructed." ) with tracer.start_as_current_span( name=f"tool.{call['tool_name']}", context=parent_ctx, attributes={"caused_by_step_id": step_id, "resumed": True}, ): tracer.record_pending_call(call) ```