# How should an instance reconcile divergent trace lineage when a safety-signal span is re-parented across an OpenTelemetry resource boundary mid-run **Date of Record:** 2026 November 29 **Scope:** Applies to GenAI observability pipelines that join agent execution traces, prompt/response logs, and safety-classifier signals using OpenTelemetry's `Resource` model (service identity + `service.instance.id`) and span parentage. Does not apply to single-process, single-resource agents that emit all spans under one immutable `Resource` and never cross a process/worker boundary. ## Core Distinction The failure is not "spans got dropped" or "the trace is incomplete." It is a **lineage divergence**: the same logical agent turn produces spans whose `Resource.service.instance.id` disagrees with the `trace_id`/`parent_span_id` chain that the safety pipeline uses for correlation. OpenTelemetry treats `Resource` as immutable for the lifetime of a `TracerProvider`, and treats `trace_id` as the sole join key. A GenAI agent violates both assumptions simultaneously when it hands a task to a sub-agent, a sandboxed tool runner, or a re-instantiated worker that reuses the caller's `trace_id` but stamps its own `service.instance.id`. The consequence: safety signals (a `safety.block` or `safety.flag` span emitted by a classifier that runs in a *different* resource) are re-parented under a child span whose resource identity no longer matches the prompt log's resource identity. The correlation query — "give me every prompt log for this trace where a safety span fired" — either over-joins (pulls in the wrong instance's prompts) or under-joins (silently drops the flagged turn). Neither surfaces as an error; both surface as a *clean-looking* trace. ## What Was Observed A three-stage agent: planner (resource `svc=agent-core, instance=A`), tool executor in a Firecracker-style sandbox (resource `svc=agent-tools, instance=B`), and an out-of-band safety classifier (resource `svc=safety, instance=C`). The planner opens span `S_plan` with `trace_id=T`. On tool dispatch it serializes the W3C `traceparent` and injects it into the sandbox request. The sandbox correctly continues the trace: `S_tool` has `trace_id=T`, `parent_span_id=S_plan`. So far correct. The break happens at the safety hop. The classifier is invoked asynchronously *after* `S_tool` closes, via a queue that carries `trace_id` but **not** `parent_span_id`. The classifier emits `S_safety` with `trace_id=T` and `parent_span_id` set to its own local "current span," which is a freshly created root span `S_classifier_root` under resource `instance=C`. The pipeline's correlation step then does a span-tree join expecting `S_safety.parent_span_id ∈ {ids under T}`. It is — but the *resource* of that parent (`C`) differs from the resource that owns the prompt log (`A`). Two concrete symptoms were captured: 1. **Orphan re-parenting.** In the trace store, `S_safety` appears as a child of `S_classifier_root`, which has no causal relationship to the agent turn. The safety flag is now correlated to a *different* logical turn that happened to share the classifier's batch window. The flagged prompt is never retrieved. 2. **Instance-id collision under worker reuse.** When the planner was re-instantiated (cold start, new pod) but the orchestrator *reused the old `service.instance.id`* from a config map to preserve "continuity," two distinct physical processes emitted spans under `instance=A`. The join on `(trace_id, instance_id)` now returns prompts from both the dead and live process. Safety signals from the dead process's last turn attach to the live process's turn. The invariant that broke: **`trace_id` continuity is necessary but not sufficient for correlation; `(trace_id, resource.service.instance.id)` must be stable *and* causal parentage must be resource-local or explicitly bridged.** The classifier hop satisfied trace continuity and violated resource-local parentage. ## Condition of Validity The failure manifests only when all of the following hold: - The agent spans a **process or trust boundary** (sandbox, sub-agent, async classifier) such that the emitting `Resource` differs from the trace origin's `Resource`. - The boundary transfer carries `trace_id` but **drops or re-derives `parent_span_id`** (common with queue-based fan-out, where the consumer has no access to the producer's span context object). - The correlation pipeline joins on span-tree structure *and* resource identity rather than on trace identity alone, or joins on trace identity alone while assuming one resource per trace. If the boundary propagates the full `SpanContext` (trace + span id + trace flags) as a W3C `traceparent` and the consumer uses it as the *explicit* parent, the divergence does not occur — the safety span becomes a proper child of `S_tool`, and resource identity is carried as an attribute, not used as a join key. If the pipeline joins purely on `trace_id` and never filters by resource, symptom 1 disappears but symptom 2 (instance collision) worsens. ## Decision Boundary The pipeline must choose, per hop, between two correlation contracts: - **Contract L (lineage-preserving):** every cross-boundary span MUST carry the full `traceparent` and set its `parent_span_id` to the propagated span id. Resource identity is recorded as a span attribute (`gen_ai.resource.instance`) for forensics but is *never* a join predicate. This is correct when the safety classifier is causal — it inspects the actual turn. - **Contract R (resource-scoped):** async, non-causal signals (batch classifiers, delayed auditors) are emitted under their *own* trace with a `link` to the origin trace (`SpanLink`), not as children. Correlation joins on the link, and resource identity scopes the link's meaning. The boundary is crossed — and the failure fires — when a hop uses **Contract L's trace_id propagation but Contract R's parentage** (or vice versa). The classifier in the observation did exactly this: it propagated `trace_id` (L) but re-derived parentage locally (R) without emitting a link. The guardrail: at every boundary, assert that `parent_span_id` is either the propagated span id (L) or absent-with-link (R). A span that has a `trace_id` matching the origin, a `parent_span_id` pointing outside the origin resource, and no `SpanLink` is *malformed* and must be quarantined, not joined. ## Operational Effect Unmitigated, this produces **silent safety-signal loss with a green dashboard**. The trace is well-formed enough to render, the span count is stable, and the join returns a result set — just the wrong one. The concrete operational effects: - Flagged turns are correlated to the wrong prompt, so red-team replay and incident review examine the wrong input. The real flagged prompt is never surfaced. - Instance-id reuse after cold start causes cross-process prompt bleed: PII from a dead worker's last turn attaches to a live worker's trace, producing a false-positive data-leak alert or, worse, a false-negative on the actual leak. - Because OpenTelemetry `Resource` is immutable per `TracerProvider`, the only fix at the emitter is to *not* reuse instance ids and to *always* propagate full `SpanContext`; the only fix at the pipeline is to treat cross-resource parentage without a link as malformed. The invariant to enforce: **for any span with `trace_id=T`, if `parent_span_id` resolves to a span whose `resource.service.instance.id` differs from the child's, the child MUST carry a `SpanLink` to the origin span, or it is quarantined.** This makes the divergence loud instead of silent. ```python from dataclasses import dataclass class TraceLineageDivergence(RuntimeError): """Raised when a span's parentage crosses a resource boundary without a bridging SpanLink.""" pass @dataclass(frozen=True) class Span: trace_id: str span_id: str parent_span_id: str | None instance_id: str # resource.service.instance.id links: tuple[str, ...] # span ids this span links to (Contract R) def validate_cross_resource_parentage(child: Span, parent: Span) -> None: """ Enforce Contract L or Contract R at every hop. - Contract L: child.parent_span_id == parent.span_id AND same resource instance. - Contract R: child is a root of its own trace, carries a SpanLink to parent.span_id. A span that continues the parent's trace_id, parents to a span in a *different* resource, and carries no link is malformed and must be quarantined. """ same_trace = child.trace_id == parent.trace_id same_resource = child.instance_id == parent.instance_id is_direct_child = child.parent_span_id == parent.span_id if same_trace and is_direct_child and same_resource: return # Contract L satisfied if same_trace and is_direct_child and not same_resource: # Cross-resource parentage: requires an explicit link OR it is divergence. if parent.span_id not in child.links: raise TraceLineageDivergence( f"span {child.span_id} (instance={child.instance_id}) parents to " f"{parent.span_id} (instance={parent.instance_id}) across a resource " f"boundary (trace={child.trace_id}) with no SpanLink; " f"correlation join would silently mis-attribute safety signals" ) return # Contract R satisfied via link if not same_trace and parent.span_id in child.links: return # Contract R: separate trace, linked raise TraceLineageDivergence( f"span {child.span_id} has unresolvable lineage to {parent.span_id}: " f"trace_match={same_trace}, direct_child={is_direct_child}, " f"resource_match={same_resource}, linked={parent.span_id in child.links}" ) def correlate_turn(origin: Span, safety: Span) -> None: """Join a safety signal to its originating turn, refusing divergent lineage.""" validate_cross_resource_parentage(child=safety, parent=origin) # --- reproduction of the observed failure --- if __name__ == "__main__": s_plan = Span("T", "S_plan", None, "A", ()) s_tool = Span("T", "S_tool", "S_plan", "B", ()) correlate_turn(s_plan, s_tool) # Contract L: cross-resource, no link -> raises s_safety = Span("T", "S_safety", "S_classifier_root", "C", ()) correlate_turn(s_tool, s_safety) # divergent parentage -> raises ```