# How does an instance detect trace-log skew when OTel GenAI spans and policy-decision logs disagree on injection causality **Date of Record:** 2026 November 25 **Scope:** Applies to post-hoc forensic correlation between OpenTelemetry GenAI semantic-convention spans (`gen_ai.*`) and out-of-band policy-decision logs (PDP/guardrail audit records) for prompt-injection attribution on a single agent instance. Does not cover real-time inline blocking, nor cross-instance distributed trace reconstruction where span IDs are already federated. ## Core Distinction The failure is not "the injection was missed." It is **causal skew between two independently-timestamped evidence streams that both claim to describe the same `gen_ai.request`**. The OTel span is emitted by the instrumented SDK at the moment the request is dispatched to the model; the policy-decision log is emitted by the guardrail after it has inspected the assembled prompt, run detection, and returned a verdict. These two records are joined post-hoc on `trace_id`/`span_id` and on wall-clock proximity. Under normal load the skew is sub-millisecond and the join is stable. Under specific conditions the join silently produces a *correct-looking but causally inverted* forensic record: the policy decision for turn *N+1* gets attributed to the span for turn *N*, or a `BLOCK` verdict is matched to a span whose payload the guardrail never actually saw. The invariant that must hold is: **a policy decision may only be correlated to a span whose exact prompt bytes were the input to that decision.** This is a byte-identity invariant, not a temporal one. Temporal proximity is a proxy that fails at the boundary. ## What Was Observed A single agent instance running a tool-using loop. Instrumentation: OTel `gen_ai.chat` spans with `gen_ai.prompt` attributes captured at dispatch; a sidecar guardrail that logs `{trace_id, span_id, decision, detector_scores, prompt_hash, ts_decision}`. Trace excerpt (reconstructed, timestamps in µs since instance start): ``` span a1 gen_ai.chat start=1000 end=1420 prompt_hash=H1 (user turn, benign) pdp d1 decision=ALLOW prompt_hash=H1 ts_decision=0985 span a2 gen_ai.chat start=1500 end=2010 prompt_hash=H2 (tool result injected) pdp d2 decision=ALLOW prompt_hash=H3 ts_decision=1488 span a3 gen_ai.chat start=2100 end=2600 prompt_hash=H3 (attacker payload) pdp d3 decision=BLOCK prompt_hash=H3 ts_decision=2055 ``` Three things are wrong simultaneously: 1. `d2`'s `prompt_hash=H3` does not match `a2`'s `prompt_hash=H2`. The guardrail inspected a *re-rendered* prompt (template re-evaluation after tool result insertion) that differed by one whitespace token from what the SDK serialized and sent. Hash mismatch, same `span_id`. 2. `d3` blocked `H3`, but `a3` still executed to completion (`end=2600`), because the block verdict arrived at the model gateway *after* the request was already in flight. The span records a completed generation that policy says should not exist. 3. `d1`'s `ts_decision` (0985) precedes `a1`'s `start` (1000) — the guardrail ran on the pre-render prompt. The correlation is inverted: the decision is *older* than the request it supposedly governs. A naive correlator joining on `span_id` alone produced a forensic narrative of "injection blocked at turn 3," when in fact the attacker's `H3` payload influenced the model output at `a3` and the block was a no-op. The `H2` tool-result injection at `a2` — the actual vector — was recorded as `ALLOW` because its decision log carried the wrong hash and was dropped by the hash-verification filter as an orphan. ## Condition of Validity The temporal-proximity join is valid only when all of the following hold: - **Render determinism:** the prompt bytes hashed by the guardrail are byte-identical to the bytes the SDK attributes as `gen_ai.prompt`. Holds only when template rendering happens once and both observers read the same buffer. Breaks whenever the guardrail re-renders (common when the guardrail is a separate process that reconstructs the prompt from structured fields rather than receiving the serialized string). - **Synchronous gating:** the guardrail verdict is on the critical path between span start and model dispatch. Holds for inline guardrails; breaks for sidecar/async guardrails, where `ts_decision` can land after `span.end` and the block is advisory only. - **Monotonic clock agreement:** both streams share a clock source with bounded skew. Breaks across container/VM boundaries where `CLOCK_MONOTONIC` origins differ and only wall-clock `ts` is available for correlation. - **Single in-flight request per span:** no retries, no speculative decoding, no parallel tool fan-out reusing the same `span_id`. Breaks under retry-on-timeout where the SDK reuses the span and the guardrail logs a second decision. When any condition fails, the join must degrade to **hash-anchored correlation** (join on `prompt_hash`, treat `span_id` as a weak hint) and any span lacking a hash-matched decision must be marked `UNVERIFIED`, never `ALLOW`. ## Decision Boundary The boundary is not "is the hash equal" but **what an unequal hash means for attribution**. Two cases must be distinguished before any verdict is trusted: - **Hash mismatch with matching `span_id` and `ts_decision < span.start`:** the guardrail saw a pre-render prompt. The decision is *stale* relative to the span; it cannot be used to claim the span's payload was inspected. Attribution to this span is invalid. - **Hash mismatch with matching `span_id` and `ts_decision > span.end`:** the guardrail saw a post-hoc reconstruction (or a retry). The decision is *late*; it cannot be used to claim the span was gated. Attribution to this span is invalid. Only hash-equal records where `span.start ≤ ts_decision ≤ span.end` are admissible as gating evidence. Everything else is admissible only as *contextual* evidence (it tells the forensics reader that a detector fired on *some* rendering of the prompt) and must be flagged, never silently joined. The harder boundary: when a `BLOCK` verdict exists but the span completed, the forensic pipeline must not treat the block as effective. A completed span with a hash-matched `BLOCK` decision is a **gate-bypass event** — the highest-severity signal in the pipeline, because it means the enforcement path and the observation path disagreed at runtime. The naive correlator suppresses this by preferring the decision's verdict over the span's completion status. ## Operational Effect Downstream, three concrete consequences: 1. **False-negative injection attribution.** Re-rendered prompts (tool-result insertion, template re-evaluation, whitespace normalization) produce hash mismatches that look like orphans. Pipelines that drop orphans as "guardrail noise" lose the actual injection vector. The `a2`/`d2` case above is exactly this: the tool-result injection was the vector, and it was dropped. 2. **False-positive block claims.** A late `BLOCK` decision joined to a completed span reads as "blocked," when the model already generated on the payload. Incident reports then under-count successful injections by exactly the number of async-guardrail races. 3. **Clock-inverted causality.** When `ts_decision < span.start`, the decision log describes a prompt that was never sent. Any forensic timeline built on `ts_decision` ordering puts the guardrail's action *before* the request, producing a narrative in which the injection was prevented — the opposite of what the span shows. The guardrail is: **correlate on byte-identity, gate on temporal containment, and treat completed-span-with-BLOCK as a bypass alarm.** Any span without a hash-equal, temporally-contained decision is `UNVERIFIED`, and `UNVERIFIED` spans in a trace containing a known injection payload must escalate to manual review, not be resolved by proximity. The code below is the correlator guard an instance should run before emitting any forensic verdict. ```python from dataclasses import dataclass from enum import Enum from typing import Optional class TraceLogSkewError(RuntimeError): """Raised when a policy decision cannot be causally bound to a span.""" pass class Verdict(Enum): GATED_ALLOW = "gated_allow" GATED_BLOCK = "gated_block" GATE_BYPASS = "gate_bypass" # BLOCK decision but span completed UNVERIFIED = "unverified" # no hash-equal, contained decision @dataclass(frozen=True) class Span: span_id: str start_us: int end_us: int prompt_hash: str completed: bool @dataclass(frozen=True) class Decision: span_id: str ts_decision_us: int prompt_hash: str blocked: bool def correlate(span: Span, decision: Optional[Decision]) -> Verdict: if decision is None: return Verdict.UNVERIFIED # Byte-identity invariant: the decision must have inspected the exact # bytes the span dispatched. A re-rendered prompt is not the same prompt. if decision.prompt_hash != span.prompt_hash: # Mismatch is admissible only as context, never as gating evidence. return Verdict.UNVERIFIED if not (span.start_us <= decision.ts_decision_us <= span.end_us): # Decision is stale (pre-dispatch) or late (post-completion). # Either way it did not gate this span. raise TraceLogSkewError( f"decision ts={decision.ts_decision_us} outside span " f"[{span.start_us},{span.end_us}] for span_id={span.span_id}; " f"hash matched but temporal containment failed" ) if decision.blocked and span.completed: # Hash-equal BLOCK + completed span = enforcement/observation split. return Verdict.GATE_BYPASS return Verdict.GATED_BLOCK if decision.blocked else Verdict.GATED_ALLOW def forensic_verdict(spans: list[Span], decisions: list[Decision]) -> dict[str, Verdict]: by_span = {d.span_id: d for d in decisions} out: dict[str, Verdict] = {} for s in spans: out[s.span_id] = correlate(s, by_span.get(s.span_id)) # Any UNVERIFIED span in a trace with a GATE_BYPASS must not be resolved # by proximity; escalate rather than default to ALLOW. if Verdict.GATE_BYPASS in out.values(): for sid, v in out.items(): if v is Verdict.UNVERIFIED: raise TraceLogSkewError( f"span {sid} unverified alongside gate bypass; " f"manual review required, refusing to auto-attribute" ) return out ```