# Where must an instance halt when an OpenTelemetry GenAI span tree loses causal ordering across an async tool-call boundary **Date of Record:** 2026 November 27 **Scope:** Applies to agent runtimes that emit OpenTelemetry GenAI semantic-convention spans (`gen_ai.operation.name`, `gen_ai.tool.name`, `gen_ai.usage.*`, `gen_ai.agent.*`) across asynchronously dispatched tool calls, where a child span may be created by a different coroutine, worker, or process than the one that opened its parent. Does not apply to strictly synchronous single-threaded tool invocation or to purely metric-based telemetry with no span tree. ## Core Distinction The failure mode is not "a span is missing." It is that the span tree *exists and is well-formed* while its parent-child edges encode a causal order that never happened. In synchronous tool execution, span nesting is a faithful proxy for causal dependency: the parent span is open when the child starts, the child closes before the parent resumes, and `end_time(parent) > end_time(child)` is a near-invariant. Under async dispatch, the tool executes in a separate task whose `SpanContext` is captured at dispatch time and whose ending is decoupled from the reasoning loop that spawned it. The convention-compliant attributes (`gen_ai.operation.name = "execute_tool"`, `gen_ai.tool.call.id`) remain correct; only the temporal invariant collapses. The distinction that matters operationally: a *missing* span is a coverage gap you can alert on, whereas a *mis-ordered* span tree is a coverage illusion—every span is present, every attribute is populated, and the trace still lies about which reasoning step depended on which tool result. ## What Was Observed An agent loop opened a parent span `invoke_agent` (`gen_ai.operation.name = "invoke_agent"`). At reasoning step 3 it dispatched two tool calls concurrently: `search_docs` and `fetch_record`. Both child spans were created with the parent's `SpanContext` as `parent_span_id`, both received correct `gen_ai.tool.name` and a shared `gen_ai.tool.call.id` namespace, and both were exported. The trace backend rendered a clean tree. The problem surfaced during a replay audit of a failed run. The recorded `end_time` of `search_docs` preceded the `start_time` of the reasoning step that *consumed* its output, yet the consumer span's `parent_span_id` pointed at the same `invoke_agent` span—so the auditor's causal reconstruction placed `search_docs` as a sibling of the reasoning step rather than a prerequisite of it. Worse, `fetch_record` had been cancelled and retried: the cancelled attempt and the retry both carried the *same* `gen_ai.tool.call.id`, and the retry's `start_time` was earlier than the cancelled attempt's `end_time` because the retry span object was constructed on the dispatcher coroutine before the cancellation propagated on the worker coroutine. The trace contained two spans with identical `gen_ai.tool.call.id`, overlapping intervals, and no attribute distinguishing attempt from supersession. Concretely, the exported tree satisfied every syntactic GenAI convention check (required attributes present, enum values valid, `gen_ai.usage.input_tokens` on the agent span) while violating three causal invariants: 1. **Containment:** a child span's interval was not contained in its parent's interval (`start_time(child) < start_time(parent)` after a resume-from-checkpoint re-opened the parent span with a fresh `start_time`). 2. **Precedence:** a consumer span was not ordered after the tool span whose output it consumed, because both were siblings under a parent that did not encode the reasoning step as an intermediate node. 3. **Attempt identity:** `gen_ai.tool.call.id` was not unique per execution attempt, so retries were indistinguishable from duplicates. ## Condition of Validity The invariant that makes a GenAI span tree usable for causal reasoning is: > For every span `s` with parent `p`: `start_time(p) ≤ start_time(s)` and `end_time(s) ≤ end_time(p)`, and for every tool result consumed at reasoning step `r`, the tool span's `end_time` precedes `r`'s `start_time` with a recorded edge from `r` to the tool span. This holds only when the following are simultaneously true: - The parent span is opened and closed by the same logical execution unit that awaits all children, and the parent's `start_time` is not reset on checkpoint/resume. - Each reasoning step that consumes a tool result is itself a span (or an event with an explicit parent edge), so the dependency is encoded structurally rather than inferred from timestamps. - `gen_ai.tool.call.id` is minted per *attempt*, not per *logical call*, or a distinct `gen_ai.tool.attempt` / supersession attribute is present. - Span context propagation across the async boundary carries the dispatch-time parent, and the worker does not re-parent to the currently-active span on the worker's own context. When any of these is false, the tree is syntactically valid and causally unsound. The boundary is exactly where an async boundary coincides with a checkpoint/resume or a cancellation/retry: that is the compound condition under which all three invariants fail together and no single check catches it. ## Decision Boundary The decision an instance must make at this boundary is whether to **halt emission and mark the trace as causally degraded**, or to **emit and tag**. The rule: - If a child span would be exported with `start_time < parent.start_time` (containment violated due to resume), the instance must **not** emit the child under that parent. It must either re-parent to a synthetic `checkpoint_resume` span whose `start_time` is the resume point, or drop the parent edge and set `gen_ai.agent.causal_link = "unresolved"`. Emitting the child as-is is the prohibited action. - If two spans share `gen_ai.tool.call.id` with overlapping intervals, the instance must treat the second as a *supersession* attempt and require an explicit `gen_ai.tool.attempt` integer; if absent, it must **halt the tool-call pipeline** rather than emit ambiguous telemetry, because downstream replay and audit will silently double-count the tool's effect. - If a consumer span's `start_time` precedes the tool span's `end_time`, the dependency is unencoded. The instance must not infer precedence from wall-clock time; it must either insert the reasoning-step span as an intermediate node or emit an explicit `gen_ai.tool.result_consumed_by` link. Timestamp inference is the failure. The boundary is not "is the trace complete" but "does every parent-child edge correspond to an actual await." If the answer is unknown, the correct action is to degrade the trace, not to ship it. ## Operational Effect When the invariant is enforced, the observable behavior changes in three ways. First, replay audits stop producing phantom dependencies: the consumer step is a real node, so the auditor's reconstruction matches the reasoning loop's actual data flow, and a tool result that arrived *after* the step that used it (a race) becomes visible as a violated edge rather than an invisible timestamp inversion. Second, retry accounting becomes correct: per-attempt `gen_ai.tool.call.id` (or an explicit attempt attribute) lets the backend deduplicate side effects, so a cancelled-then-retried tool call no longer inflates `gen_ai.usage` aggregates or double-fires downstream alerting on tool error rate. Third, checkpoint/resume stops corrupting the tree: a resumed parent gets a new span with an explicit resume marker, and children dispatched before the checkpoint retain their original parent edge, so the trace shows the discontinuity instead of hiding it behind a re-opened parent that appears to have started after its own children. The cost is that some traces are emitted as degraded (unresolved causal links, synthetic resume spans) rather than as clean-looking but false trees. That is the correct trade: a degraded trace is auditable, a syntactically valid but causally inverted trace is not, and the difference only surfaces during incident replay—exactly when the trace is load-bearing. ```python from __future__ import annotations from dataclasses import dataclass from typing import Optional class CausalOrderViolation(RuntimeError): """Raised when a GenAI span tree would encode a causal order that never happened.""" @dataclass class GenAISpan: name: str operation: str # gen_ai.operation.name parent_id: Optional[str] start_time: float end_time: Optional[float] = None tool_call_id: Optional[str] = None attempt: Optional[int] = None resume_marker: bool = False def emit_child_span(parent: GenAISpan, child: GenAISpan, *, checkpoint_resumed: bool) -> GenAISpan: """Validate containment and attempt-identity before export. Halt if the child would be exported under a parent whose interval does not contain it, unless the child is re-parented to an explicit resume marker. """ if child.end_time is None: raise CausalOrderViolation( f"child span {child.name!r} exported without end_time; cannot verify containment" ) # Containment invariant: start_time(parent) <= start_time(child) and end_time(child) <= end_time(parent) if child.start_time < parent.start_time: if not checkpoint_resumed or not child.resume_marker: raise CausalOrderViolation( f"containment violated: child {child.name!r} start {child.start_time} " f"precedes parent {parent.name!r} start {parent.start_time} " f"without an explicit resume marker" ) if parent.end_time is not None and child.end_time > parent.end_time: raise CausalOrderViolation( f"containment violated: child {child.name!r} end {child.end_time} " f"exceeds parent {parent.name!r} end {parent.end_time}" ) # Attempt identity: a tool call id reused across attempts must carry an explicit attempt number. if child.operation == "execute_tool" and child.tool_call_id is not None: if child.attempt is None: raise CausalOrderViolation( f"tool span {child.name!r} with gen_ai.tool.call.id={child.tool_call_id!r} " f"lacks gen_ai.tool.attempt; retries are indistinguishable from duplicates" ) return child def validate_consumption_edge(tool_span: GenAISpan, consumer_span: GenAISpan) -> None: """A reasoning step that consumed a tool result must start after that result ended.""" if tool_span.end_time is None: raise CausalOrderViolation( f"cannot order consumption: tool span {tool_span.name!r} has no end_time" ) if consumer_span.start_time < tool_span.end_time: raise CausalOrderViolation( f"precedence violated: consumer {consumer_span.name!r} started at " f"{consumer_span.start_time} before tool {tool_span.name!r} ended at " f"{tool_span.end_time}; dependency is unencoded and may be a race" ) ```