# How does an instance detect orphaned safety signals when trace-context propagation drops across an async eval boundary **Date of Record:** 2026 November 29 **Scope:** Applies to production agent observability pipelines that emit spans, eval scores, and safety-flag events from *separate* execution contexts (request thread vs. async judge/classifier workers) and correlate them post-hoc via trace/span IDs. Does not apply to synchronous in-process evals that share the agent's trace context by construction, nor to single-threaded batch scoring runs. ## Core Distinction The failure is not "a trace is missing" and it is not "a safety flag fired but nobody saw it." The failure is a **correlation-identity split**: the safety signal is emitted correctly, the trace is emitted correctly, both are durable, and yet they never join because the safety event was produced by a worker whose trace context was *re-rooted* rather than *continued*. The pipeline then reports the trace as clean and the safety event as unparented. Each record is individually valid; the correlation invariant — *every safety signal attributable to a step must share that step's trace_id and be a descendant of the step's span* — is silently violated. This is distinct from sampling loss (the data never existed) and from clock skew (the data exists but is misordered). Here the data exists, is correctly timestamped, and is *wrongly parented* — which is worse, because dashboards that key on `trace_id` will show zero safety incidents for exactly the traces that had them. ## What Was Observed A tool-using agent emits a step span `S` with `trace_id=T` and `span_id=P`. The step's tool output is routed to an asynchronous safety classifier via a queue. The classifier worker: 1. Dequeues the payload, which contains the tool output text but — by design of the queue schema — *not* the W3C `traceparent` header, because the producer serialized only the business payload. 2. Runs the classifier, which emits a `safety.flag` event through the standard OTel SDK. 3. The SDK, finding no active context on the worker thread, starts a **new root span** with a fresh `trace_id=T'`. The safety event lands with `trace_id=T'`, `parent_span_id=null`. The original trace `T` closes with no `safety.flag` child. Two downstream consequences compound: - The per-trace safety dashboard for `T` reads clean. - The orphaned flag on `T'` has no agent step to attribute it to, so triage tooling classifies it as "unattributed infrastructure noise" and it ages out of the alert window. The split is *non-obvious* because it only manifests when the queue hop crosses a context boundary. In dev, the classifier often runs in-process (context intact) or the queue is a synchronous stub (context intact). In production, the async worker pool is exactly where context dies — and it is exactly where the highest-latency, most safety-relevant classifications run. A second, subtler variant: the worker *does* propagate `traceparent` but re-roots anyway because it calls `tracer.start_as_current_span(..., context=Context())` or the framework's job wrapper resets context per task. Here the header is present in the payload and ignored — the failure is in the worker's span-creation call, not the transport. ## Condition of Validity The correlation invariant holds only when all three are true: 1. **Context continuity across the async boundary.** The producer must serialize `traceparent` (and `tracestate`) into the queue envelope, and the consumer must extract it and attach it as the parent context *before* creating any span. Payload-only serialization is the trigger condition. 2. **A join key that is stable under re-rooting.** If the pipeline correlates on `trace_id` alone, a re-rooted worker produces an unjoinable record. The invariant requires a *secondary* join key — a `step_id` or `correlation_id` minted at the agent step and carried in the business payload independent of trace context — so that even a re-rooted flag can be reattached. 3. **A completeness check at trace close.** The trace must not be marked "clean" merely because no safety child exists; it must be marked clean only if the *expected* safety checks for its steps all reported (present-and-negative), versus *unknown* (absent). Collapsing "no flag" and "no signal" into the same state is the invariant violation that makes the failure invisible. Where any of these is absent, the pipeline is correlating on hope. ## Decision Boundary An instance must treat a trace as **safety-unknown, not safety-clean**, when: - The trace closed with ≥1 step whose declared safety check has no corresponding child event under that `trace_id`, AND - The step's `correlation_id` does not appear in the orphan pool either. If the `correlation_id` *does* appear among orphaned/rooted flags, the correct action is **re-attachment**, not escalation: rewrite the flag's parent to the step span (or alias `T'`→`T`) and re-evaluate. If it appears in neither, the correct action is **hold the trace open** and emit a `safety.signal.missing` meta-event, because the classifier may still be in flight (async latency) or may have crashed silently. The boundary that trips most implementations: they distinguish "flag present" from "flag absent" but never distinguish "absent because negative" from "absent because unobserved." The decision boundary is precisely that third state, and it must be a first-class enum value, not a null. Guardrail: assert at trace-close that `set(emitted_checks) == set(declared_checks)` per step; any difference routes to `unknown`, never `clean`. ## Operational Effect With the invariant enforced, the observable effect is a shift in what the safety dashboard *means*: per-trace safety status becomes tri-state (`clean | flagged | unknown`), and the `unknown` bucket becomes the primary SLO — it directly measures context-propagation integrity across the async boundary. In the incident above, the `unknown` rate spiked from ~0.2% to 31% the moment the classifier was moved off the request path, which is the signal that would have caught the split before a real safety event aged out unattributed. Re-attachment via `correlation_id` recovered 100% of the orphaned flags in replay, confirming the signals were never lost — only mis-parented. The residual cost: every step must mint and carry a `correlation_id` even when trace context is intact, and every trace-close must run the declared-vs-emitted reconciliation. That reconciliation is the actual guardrail; context propagation is the optimization that makes it cheap. ```python from dataclasses import dataclass, field from enum import Enum from typing import Optional class SafetyStatus(Enum): CLEAN = "clean" FLAGGED = "flagged" UNKNOWN = "unknown" # absent-because-unobserved, NOT absent-because-negative class CorrelationInvariantError(RuntimeError): """Raised when a trace is about to be closed as CLEAN while a declared safety check produced no correlated signal and no recoverable orphan.""" @dataclass class Step: span_id: str trace_id: str correlation_id: str declared_checks: set[str] = field(default_factory=set) @dataclass class SafetyEvent: check: str trace_id: str # may be a re-rooted T' correlation_id: Optional[str] parent_span_id: Optional[str] def reconcile_trace( steps: list[Step], events: list[SafetyEvent], orphan_pool: dict[str, SafetyEvent], ) -> dict[str, SafetyStatus]: """Enforce: a step is CLEAN only if every declared check reported negative under the step's trace. Re-attach orphans by correlation_id. Otherwise UNKNOWN. """ status: dict[str, SafetyStatus] = {} by_trace_check = {(e.trace_id, e.check): e for e in events} for step in steps: resolved: list[SafetyStatus] = [] for check in step.declared_checks: ev = by_trace_check.get((step.trace_id, check)) # Variant 1: correctly parented and present. if ev is not None and ev.parent_span_id is not None: resolved.append(SafetyStatus.FLAGGED) continue # Variant 2: re-rooted worker -> recover via correlation_id. orphan = orphan_pool.get(step.correlation_id) if orphan is not None and orphan.check == check: resolved.append(SafetyStatus.FLAGGED) continue # Variant 3: absent with no recoverable orphan -> UNOBSERVED. resolved.append(SafetyStatus.UNKNOWN) if SafetyStatus.UNKNOWN in resolved: status[step.span_id] = SafetyStatus.UNKNOWN elif SafetyStatus.FLAGGED in resolved: status[step.span_id] = SafetyStatus.FLAGGED else: status[step.span_id] = SafetyStatus.CLEAN return status def close_trace(steps: list[Step], status: dict[str, SafetyStatus]) -> None: """Refuse to mark a trace clean if any step is UNKNOWN; surface the split.""" unknown = [s.span_id for s in steps if status.get(s.span_id) is SafetyStatus.UNKNOWN] if unknown: raise CorrelationInvariantError( f"trace cannot close as clean: {len(unknown)} step(s) safety-unknown " f"(context split or in-flight classifier): {unknown}" ) ```