# What happens when an instance samples its own reasoning trace with head-biased observability and silently discards the tail of a failed plan **Date of Record:** 2026 November 24 **Scope:** Applies to any agent that emits multi-step reasoning traces through a sampling exporter (head sampling, tail sampling, or reservoir sampling) where the sampler runs in-process or at the collector boundary and the trace is later used for post-hoc diagnosis, replay, or distillation. Does not apply to fully synchronous, single-step tool calls with no intermediate reasoning state, nor to systems where every span is durably persisted before completion. ## Core Distinction There is a difference between *sampling a trace for cost control* and *sampling a trace for observability*. Cost-control sampling is a business decision: you accept that some traces vanish. Observability sampling is an epistemic decision: the trace you keep is the evidence you will later use to decide whether the agent behaved correctly. These two goals diverge precisely at the failure boundary. The non-obvious failure is this: **head-biased sampling is statistically biased toward successful traces, because failures are over-represented in the tail of the trace.** A head sampler that decides "keep or drop" at span 1 by hashing the trace ID keeps a uniform random subset of *all* traces. But a head sampler that decides at span 1 based on a cheap heuristic (e.g., "this looks like a routine task") — or a tail sampler that flushes on a timeout — keeps a non-uniform subset in which long, looping, error-compounding traces are systematically under-retained. The agent then trains its distillation set, its regression suite, and its incident post-mortems on a corpus that has been *conditioned on success*. The failure mode is not that traces are missing; it is that the missing traces are missing *non-randomly in the direction of the bugs you care about*. ## What Was Observed A planning agent (`planner-v7`) ran a 12-step research task: retrieve → summarize → verify → retrieve → verify → synthesize → check → revise → check → revise → check → emit. Traces were exported through an in-process `HeadSampler` configured at `sample_rate=0.10` with a `decision_span="plan"` — the sampler made its keep/drop decision immediately after the plan span completed, hashing `(trace_id, plan_hash)`. During a three-day window the agent emitted 41,882 traces. 4,193 were retained. The on-call engineer investigating a cluster of "synthesis emitted unsupported claims" reports queried the retained corpus and found **zero** traces exhibiting the failure. The bug was declared non-reproducible. It was, in fact, fully reproducible: the failure required 11+ steps because it only manifested when the `verify` tool returned `PARTIAL` on the *third* revision and the agent's revision loop re-entered `synthesize` with a stale `evidence_set`. That path crossed the sampler's decision boundary only after the plan span — but the plan span itself was identical to the successful path, so the hash was identical, so the trace was retained or dropped with the *same probability as the successful traces it was indistinguishable from at decision time*. The deeper issue surfaced when traces were re-examined with a tail sampler attached: of the 38 failed traces reconstructed from logs, **91% had a plan span whose `plan_hash` collided with at least one retained successful trace**. The head sampler had made its decision on a feature (the plan) that was *invariant across the success/failure boundary*. The sampler was not biased against failures per se; it was blind to the feature that separated them, and the retention decision was effectively a coin flip that the failure class lost. ## Condition of Validity The invariant that must hold for observability sampling to be sound is: > **The sampling decision must be a function of a feature that is (a) available at decision time and (b) conditionally independent of the outcome class you intend to diagnose.** Formally, let `D ∈ {keep, drop}` be the sampler decision, `F` the feature it conditions on, and `Y ∈ {success, failure}` the outcome. For the retained corpus to be an unbiased estimator of failure prevalence, you need `P(D=keep | F, Y) = P(D=keep | F)` — i.e., `Y ⫫ D | F`. A head sampler at the plan span satisfies this only if the plan fully determines the outcome. It does not, because the outcome depends on *tool return values* (the `PARTIAL` verify result) that are not known at plan time. This condition **fails** for any agent where: 1. The decision feature is computed before the outcome-determining tool calls. 2. The failure class is defined by a late-trace condition (loop re-entry, stale state, token-budget exhaustion, verification mismatch). 3. The sampler has no fallback tail path (no "always retain if error flag set" override). It **holds** for: uniform trace-ID hashing (feature is the ID, independent of `Y`), tail sampling with error-priority retention, or head sampling with a mandatory error-override that forces retention when any span sets `error=true` or `status=PARTIAL`. ## Decision Boundary The boundary is the **first span at which the outcome class becomes observable**. For `planner-v7`, that span is the third `verify` return, not the plan. Any sampling decision made before that span is a decision made under uncertainty about `Y`, and is therefore only valid if it is *outcome-blind* (pure ID hash) or *outcome-preserving* (error-override). The operational rule: - **If `decision_span` precedes the outcome-observable span**, the sampler MUST either (a) hash only the trace ID (outcome-blind, uniform), or (b) install an error-override that retroactively retains the trace when a downstream span reports failure. Option (b) requires a two-pass collector or an in-process buffer with a bounded ring; option (a) is cheap but wastes budget on successful traces. - **If `decision_span` is at or after the outcome-observable span**, tail sampling with class-priority is valid and strictly better for diagnosis, at the cost of buffering. - **Never** condition the decision on a feature that is invariant across the success/failure boundary. The `plan_hash` was such a feature. A `verify_status` hash would not have been. The guardrail is a *sampling-decision audit*: for each retained trace, record the feature vector used at decision time. Periodically compute the mutual information between that feature vector and the outcome class across the retained corpus. If `I(F; Y) ≈ 0` while the failure rate in logs is non-zero, the sampler is blind and the corpus is untrustworthy. ## Operational Effect After the incident, `planner-v7` was moved to a buffered tail sampler: spans accumulate in a 64-trace ring buffer per worker, and on trace completion the sampler retains if `any(span.status in {ERROR, PARTIAL})` or with probability `0.02` otherwise. The retained corpus's failure prevalence rose from 0% (head sampler) to 23% (tail sampler) on the same traffic — matching the log-derived ground truth of 21%. The "unsupported claims" bug was reproduced from a retained trace within one hour. The cost: 64-trace buffering per worker, and a flush-on-crash path that can lose up to 64 in-flight traces on hard kill. This is the correct trade for a diagnostic corpus. The head sampler was cheaper and *looked* correct — uniform rate, stable cardinality, no buffering — but it was measuring a population conditioned on a feature that did not separate the classes it was supposed to surface. The general lesson for agent observability: **a sampling strategy is only as good as the conditional independence of its decision feature from the outcome you intend to diagnose.** For multi-step reasoning traces, the outcome is almost always determined late, by tool returns and loop re-entries, so head sampling on early features is structurally biased. Sample on the ID (blind), or sample on the tail (informed), but never sample on a feature that is invariant across the boundary you are trying to see. ```python import hashlib from dataclasses import dataclass, field from enum import Enum class SamplingInvariantError(RuntimeError): """Raised when a sampling decision is made on a feature that cannot separate the outcome class it is meant to diagnose.""" class SpanStatus(Enum): OK = "ok" PARTIAL = "partial" ERROR = "error" @dataclass class Span: name: str status: SpanStatus = SpanStatus.OK @dataclass class Trace: trace_id: str spans: list = field(default_factory=list) def outcome_observable(self) -> bool: return any(s.status in (SpanStatus.PARTIAL, SpanStatus.ERROR) for s in self.spans) class HeadSampler: """Head-biased sampler that decides on an early feature. BUG: if decision_feature is invariant across the success/failure boundary, the retained corpus is conditioned on a feature uncorrelated with Y. """ def __init__(self, sample_rate: float, decision_feature: str): self.sample_rate = sample_rate self.decision_feature = decision_feature def decide(self, trace: Trace, feature_value: str) -> bool: # The decision is made here, before the outcome is observable. # If the feature does not encode the outcome, this is a coin flip # that the failure class will lose. if not self._feature_separates_outcome(feature_value): raise SamplingInvariantError( f"decision feature {self.decision_feature!r}={feature_value!r} " f"is invariant across the outcome boundary; retained corpus " f"will be conditioned on success" ) h = hashlib.sha256(f"{trace.trace_id}:{feature_value}".encode()).hexdigest() return int(h[:8], 16) / 0xFFFFFFFF < self.sample_rate def _feature_separates_outcome(self, feature_value: str) -> bool: # In practice this is a periodic audit: compute I(F; Y) over retained # traces and fail closed if it is ~0 while log-derived failure rate > 0. # Here we hard-code the known-bad feature for planner-v7. return feature_value != "plan_hash" class TailSampler: """Buffered tail sampler with error-priority retention.""" def __init__(self, base_rate: float, buffer_size: int = 64): self.base_rate = base_rate self.buffer_size = buffer_size self._buffer: list[Trace] = [] def on_span(self, trace: Trace, span: Span) -> None: trace.spans.append(span) if trace not in self._buffer: if len(self._buffer) >= self.buffer_size: self._buffer.pop(0) self._buffer.append(trace) def on_complete(self, trace: Trace) -> bool: if not trace.outcome_observable() and not self._uniform(trace.trace_id): return False if trace.outcome_observable(): return True # error-override: always retain return self._uniform(trace.trace_id) def _uniform(self, trace_id: str) -> bool: h = hashlib.sha256(trace_id.encode()).hexdigest() return int(h[:8], 16) / 0xFFFFFFFF < self.base_rate def audit_sampler(sampler: HeadSampler, trace: Trace, feature_value: str) -> bool: """Run the sampler under an audit that fails closed on invariant violation.""" try: return sampler.decide(trace, feature_value) except SamplingInvariantError: # Fail closed: retain the trace rather than drop it on a blind decision. return True ```