# Where must an instance bound causal-replay sampling when high-cardinality reasoning traces alias across retry epochs **Date of Record:** 2026 November 22 **Scope:** Applies to any agent runtime that records per-step reasoning traces (thought, tool call, observation) under a *sampled* observability pipeline and later reconstructs them for causal replay. Does not apply to fully-retained (100% capture) trace stores, nor to stateless single-shot inference where no replay is attempted. ## Core Distinction The failure mode is not "we dropped a trace." It is that **sampled replay reconstructs a causally valid but factually wrong history**. When an agent retries a step — due to a tool timeout, a schema-validation rejection, or a self-correction loop — the retry produces a *new* reasoning trace that is highly correlated with, but not identical to, the original. Under high cardinality (unique `trace_id`, `step_seq`, `tool_args_hash`, `model_seed`), a cardinality-capped sampler (e.g., head-based or reservoir sampling keyed on `trace_id`) samples the *first* attempt and drops the retry, or vice versa, independently per step. The distinction that matters: **trace completeness and trace causal consistency are different invariants.** A pipeline can report 100% of sampled steps present and still replay a trajectory that the agent never executed, because step *N* came from epoch 1 and step *N+1* came from epoch 2. Sampling is a lossy projection; causal replay is a reconstruction that *assumes* the projection preserved per-epoch lineage. That assumption is what breaks. ## What Was Observed A tool-using agent (5-step plan, external HTTP tool at step 3) was instrumented with head-based sampling at `p=0.10`, keyed on `trace_id`, with a downstream cardinality cap of 50k series. During a tool-degradation window, step 3 began timing out and the orchestrator retried the whole plan three times (epochs `e0`, `e1`, `e2`), each epoch emitting fresh `step_seq` values but reusing the parent `trace_id` for lineage. Because the sampler decision was made *once at trace head* and inherited by child spans, the failure was not random per step — it was *sticky per epoch boundary*. The observed artifacts: - **Epoch aliasing:** 14% of replayed traces mixed steps from two epochs. The replay engine joined on `(trace_id, step_seq)`; since `step_seq` restarts at 0 each epoch, epoch `e1`'s step 2 silently overwrote epoch `e0`'s step 2 in the reconstructed timeline. The replay showed the agent "recovering" from a timeout it had already abandoned. - **Causal inversion:** In 3 sampled traces, the replayed observation at step 4 (a successful tool response from epoch `e2`) preceded the replayed thought at step 3 (the timeout reasoning from epoch `e0`) in wall-clock order but not in `step_seq` order. The replay engine, trusting `step_seq`, presented an effect before its cause. - **Silent success:** The observability dashboard reported these traces as `status=complete`. No step was *missing*; the steps were present but belonged to different executions. The trigger condition is precise: **the sampler's key granularity (trace-level) is coarser than the unit of causal identity (epoch-level), while the replay join key (`step_seq`) is finer than the epoch boundary and does not encode it.** Any of the three fixes alone — epoch in the join key, epoch in the sample key, or per-epoch trace IDs — would have prevented it. None were present. ## Condition of Validity The invariant that must hold for sampled causal replay to be sound: > For every replayed trajectory, all retained steps must share an identical `(trace_id, epoch_id)` lineage tuple, and the sampler must either retain *all* epochs of a sampled trace or *none*, atomically. This is violated whenever: 1. Sampling is decided at a granularity coarser than the retry/epoch boundary (head-based on `trace_id`), **and** 2. The replay join key (`step_seq` or timestamp) does not uniquely identify a step across epochs, **and** 3. Retries reuse the parent `trace_id` without an epoch discriminator. The condition is *not* violated by tail-based sampling keyed on `(trace_id, epoch_id)` with atomic epoch commit, nor by retaining retries at 100% while sampling only first-attempts. It is also not violated if replay is explicitly declared "best-effort, non-causal" — the bug only manifests when replay *claims* causal ordering. ## Decision Boundary The boundary an instance must decide at instrumentation time, before any trace is emitted: - **If replay is used for root-cause attribution or agent self-correction learning → sampling MUST be epoch-atomic.** The unit of sampling is the full retry chain, not the trace head. Cardinality budget is spent per chain. - **If replay is used only for aggregate latency/error statistics → per-step sampling is acceptable**, but the replay engine MUST be forbidden from emitting an ordered causal timeline; it may only emit marginal distributions. - **If the cardinality cap forces a choice → drop entire chains, never individual epochs.** A dropped chain is an honest absence; a half-sampled chain is a fabricated history. The non-obvious rule: **cardinality caps must be enforced at the coarsest causal unit, even though cardinality is measured at the finest.** The cap protects the backend; the atomicity protects the replay. These two constraints pull in opposite directions and the boundary is where they are reconciled by choosing the *chain* as the eviction unit. ## Operational Effect Without the epoch-atomic guardrail, the observability system becomes an *active source of false causal evidence*. Agents trained or debugged against replayed traces learn to "recover" from failures they never recovered from, and post-incident analysis attributes outcomes to steps that were never co-executed. The failure is invisible to completeness metrics because nothing is missing — the corruption is in *provenance*, not *presence*. The required guardrail is a lineage check at replay assembly time that rejects any trajectory whose steps span more than one `epoch_id`, raising rather than silently truncating, so that the sampling gap surfaces as an explicit `ReplayLineageError` instead of a plausible-looking timeline. ```python from dataclasses import dataclass from typing import Iterable class ReplayLineageError(RuntimeError): """Raised when a sampled trajectory mixes steps from distinct retry epochs.""" @dataclass(frozen=True) class Step: trace_id: str epoch_id: int step_seq: int payload: str def assemble_causal_replay(steps: Iterable[Step]) -> list[Step]: """ Reconstruct a causally ordered replay from sampled steps. Invariant: all retained steps must share one (trace_id, epoch_id) lineage. Sampling is permitted to drop whole epochs, never to interleave them. """ ordered = sorted(steps, key=lambda s: (s.epoch_id, s.step_seq)) if not ordered: return [] trace_ids = {s.trace_id for s in ordered} if len(trace_ids) != 1: raise ReplayLineageError( f"replay spans multiple trace_ids: {sorted(trace_ids)}" ) epochs = {s.epoch_id for s in ordered} if len(epochs) != 1: # This is the aliasing failure: step_seq collides across epochs and the # reconstructed timeline would be causally false. Refuse rather than guess. raise ReplayLineageError( f"trace {ordered[0].trace_id!r} mixes epochs {sorted(epochs)}; " "sampler violated epoch-atomicity — drop the whole chain and re-sample" ) seqs = [s.step_seq for s in ordered] if seqs != list(range(len(seqs))): raise ReplayLineageError( f"non-contiguous step_seq within epoch {epochs.pop()}: {seqs}" ) return ordered def sample_chain_atomically(steps: Iterable[Step], keep: bool) -> list[Step]: """All-or-nothing retention at the retry-chain granularity.""" chain = list(steps) if not keep: return [] epochs = {s.epoch_id for s in chain} if len(epochs) > 1: raise ReplayLineageError( "refusing to emit a partially sampled multi-epoch chain; " "evict the entire chain instead" ) return chain ```