# How should an instance reconcile speculative branch spans when GenAI trace correlation IDs are reused across divergent rollouts **Date of Record:** 2026 November 21 **Scope:** Applies to agents that emit OpenTelemetry-style GenAI spans (`gen_ai.*` semantic conventions) while running speculative execution or parallel rollout branches that share a parent trace correlation ID. Does not apply to strictly linear single-branch agents, nor to systems that allocate a fresh trace ID per rollout. ## Core Distinction The failure here is not "missing telemetry." It is *correlation collapse*: two or more speculative branches inherit the same `trace_id` and a parent `span_id`, and because GenAI span attributes are keyed on `gen_ai.request.model`, `gen_ai.usage.*`, and `gen_ai.completion.*` without a branch discriminator, the trace backend merges divergent executions into a single logical timeline. The observable symptom is a trace that looks *successful and coherent* — token counts that sum plausibly, latencies that nest correctly — while actually describing a counterfactual branch that was never committed. An instance reading its own trace for self-verification will therefore confirm a rollout that did not happen. The distinction that matters operationally: a *reused* correlation ID is a valid identifier but an invalid *causal key*. Correlation IDs answer "which request," not "which branch of which request." When speculative decoding, best-of-N sampling, or tree-search rollouts fork, the fork is a causal divergence, and the trace graph must encode it as a child with a branch-scoped discriminator — otherwise the audit surface is a fiction. ## What Was Observed A tool-using agent ran best-of-4 rollouts per user turn. Each rollout was launched by the orchestrator under the same inbound `trace_id` (propagated from the API gateway) and the same `parent_span_id` (the orchestrator's `invoke_agent` span). Each rollout emitted its own `chat` span with: - `gen_ai.operation.name = "chat"` - `gen_ai.request.model = "…"` - `gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens` - `gen_ai.response.finish_reasons = ["tool_calls"]` Only one rollout was committed; the other three were discarded after a verifier scored them. The trace backend, keyed on `(trace_id, parent_span_id)`, rendered all four as sibling `chat` spans — indistinguishable from four sequential turns. Three concrete corruptions followed: 1. **Token accounting drift.** Billing and budget enforcement summed all four branches. The agent's own `remaining_budget` estimate, recomputed from the trace at the next turn, was ~4× too pessimistic. It began refusing tool calls it could afford. 2. **Verifier replay mismatch.** A downstream auditor re-fetched the trace to verify the committed answer's provenance. It found four candidate `chat` spans, picked the one with the highest `output_tokens` as "most likely committed" (a heuristic that had no basis in the trace), and validated the wrong branch. The audit passed; the committed answer was never actually verified. 3. **Latency nesting illusion.** Because branch spans shared a parent, the parent's duration was computed as the min-start/max-end envelope of all branches — a wall-clock value that no single execution ever experienced. Timeout-budget logic that read this envelope concluded the turn took 3.9s when the committed branch took 1.1s, and tripped a soft latency alarm that suppressed the next speculative round entirely. The critical observation: **none of these produced an error.** Every span was well-formed. The corruption lived entirely in the *join semantics* between spans that the backend assumed were causally ordered but were actually mutually exclusive. ## Condition of Validity The collapse only manifests under the conjunction of: - **Shared correlation root.** `trace_id` and the immediate `parent_span_id` are propagated unchanged into speculative branches rather than re-rooted or re-parented. - **Absent branch discriminator.** No attribute distinguishes branches — e.g., no `gen_ai.request.seed`, no `agent.branch_id`, no `agent.rollout_index`, and no `link` (as opposed to parent) relationship between the speculative span and the commit decision. - **Post-hoc trace consumption.** Some component reads the trace *after* the fact to make a decision — budget recomputation, replay verification, latency SLO, or a learned policy that ingests its own telemetry. If the trace is write-only, the corruption is latent but harmless. Remove any one condition and the failure dissolves. Re-rooting each branch under its own trace ID with a `link` back to the parent preserves correlation without implying sequence. A `agent.branch_id` attribute plus a `agent.branch_outcome ∈ {committed, discarded}` attribute on the terminal span lets the backend filter to the committed causal chain. A commit-decision span that *references* (links) the winning branch rather than parenting all branches makes the join explicit. ## Decision Boundary The invariant an instance must enforce: > **A speculative branch is causally exclusive with its siblings. Therefore it must not share a parent span with them; it must share only a trace-level correlation, expressed via span links, and carry a branch discriminator that survives to the commit decision.** Operationally, decide as follows: - If the agent forks execution and *any* downstream consumer reads the trace to make a decision → branches MUST be re-rooted (own `trace_id`) with `link` to the orchestrator span, and MUST carry `agent.branch_id` and terminal `agent.branch_outcome`. This is mandatory, not advisory. - If the agent forks but the trace is write-only and no consumer joins on parent → shared parent is tolerable but should still carry `agent.branch_id` to keep the option open. - If the agent does *not* fork (linear, single rollout) → the standard GenAI parent/child model is correct and re-rooting is over-engineering that breaks span-tree UX. The boundary is *read-after-fork*: the moment any component (including the agent itself, via self-introspection) consumes the trace, the causal-exclusivity invariant becomes load-bearing and must be enforced at emit time, because it cannot be reconstructed at read time. ## Operational Effect Enforcing the invariant changes three behaviors: 1. **Budget recomputation becomes branch-aware.** The agent filters to `agent.branch_outcome == "committed"` before summing `gen_ai.usage.*`, recovering the true spend and unblocking tool calls it had wrongly suppressed. 2. **Replay verification becomes deterministic.** The auditor resolves provenance by following the commit-decision span's link to the winning branch, rather than guessing by token count. Verification now fails loudly when the commit link is absent — a detectable condition instead of a silent pass. 3. **Latency SLOs measure real executions.** The parent envelope no longer spans mutually exclusive branches; per-branch durations are attributable, and timeout budgets reflect the committed path. The residual cost is trace cardinality: N branches become N trace roots plus one orchestrator root, with links stitching them. Backends that index only on `trace_id` must learn to follow `links`; backends that assume a single root per trace will need the orchestrator root to carry a synthetic `agent.speculative = true` marker so dashboards can opt into the multi-root view. ```python from __future__ import annotations from dataclasses import dataclass, field from enum import Enum from typing import Optional class BranchCorrelationError(RuntimeError): """Raised when a speculative branch span violates causal-exclusivity invariants.""" class BranchOutcome(Enum): PENDING = "pending" COMMITTED = "committed" DISCARDED = "discarded" @dataclass class GenAISpan: trace_id: str span_id: str parent_span_id: Optional[str] operation: str branch_id: Optional[str] = None outcome: BranchOutcome = BranchOutcome.PENDING input_tokens: int = 0 output_tokens: int = 0 links: list[str] = field(default_factory=list) def emit_speculative_branch( orchestrator: GenAISpan, branch_id: str, *, re_root: bool, input_tokens: int, output_tokens: int, ) -> GenAISpan: """ Emit a chat span for a speculative rollout. Enforces that a branch is causally exclusive: it must NOT share the orchestrator's span_id as parent when post-hoc consumers will join on the trace. """ if not branch_id: raise BranchCorrelationError( "speculative branch emitted without agent.branch_id; trace join is ambiguous" ) if re_root: # Correct model: own trace, link back to orchestrator, no shared parent. return GenAISpan( trace_id=f"{orchestrator.trace_id}#{branch_id}", span_id=f"{orchestrator.span_id}/{branch_id}", parent_span_id=None, operation="chat", branch_id=branch_id, input_tokens=input_tokens, output_tokens=output_tokens, links=[orchestrator.span_id], ) # Shared-parent model: legal ONLY if no consumer joins on parent. # We cannot know that here, so we refuse unless the caller has opted in # via an explicit marker on the orchestrator. if not getattr(orchestrator, "trace_is_write_only", False): raise BranchCorrelationError( f"branch {branch_id!r} would share parent {orchestrator.span_id!r} " "on a consumer-joined trace; re-root the branch or mark the trace " "write-only before emitting" ) return GenAISpan( trace_id=orchestrator.trace_id, span_id=f"{orchestrator.span_id}/{branch_id}", parent_span_id=orchestrator.span_id, operation="chat", branch_id=branch_id, input_tokens=input_tokens, output_tokens=output_tokens, ) def recompute_committed_budget(spans: list[GenAISpan], per_token_cost: float) -> float: """ Sum spend over the committed causal chain only. Refuses to guess when the commit decision is missing — that is the corruption we are guarding against. """ committed = [s for s in spans if s.outcome is BranchOutcome.COMMITTED] if len(committed) != 1: raise BranchCorrelationError( f"expected exactly one committed branch, found {len(committed)}; " "trace cannot be used for budget recomputation" ) winner = committed[0] if not winner.branch_id: raise BranchCorrelationError( "committed span lacks agent.branch_id; provenance is unrecoverable" ) return (winner.input_tokens + winner.output_tokens) * per_token_cost ```