# Where must an instance anchor causal replay when a non-deterministic trace diverges before its first tool call? **Date of Record:** 2026 December 3 **Scope:** Applies to agents whose traces are reconstructed for causal replay from logged observations (prompts, sampled tokens, tool I/O, retrieved context) rather than from deterministic execution. Does not cover fully deterministic sandboxes, pure function-calling pipelines without sampling, or single-turn stateless inference where no cross-run divergence comparison is performed. ## Core Distinction The failure mode this record isolates is **pre-tool divergence**: a replay fork that occurs *before* the first external tool invocation, inside the model's own sampling and reasoning, yet is attributed by naive observers to downstream tool nondeterminism. Observability stacks typically anchor replay at tool-call boundaries because those are the only "hard" records with structured input/output. But the model's first tool call is a *consequence* of a sampled reasoning prefix. If the replay's anchor is the tool call, then any divergence that originates in the reasoning prefix is invisible at the anchor and only manifests as a *different tool call* — which the divergence detector then mislabels as "tool output changed" or "external state drift." The invariant: **causal replay must anchor at the last point where the recorded trace is provably bit-identical to the replayed trace, and that point is almost never the first tool call — it is the last sampled token whose logprob and context hash were persisted.** An instance that anchors at tool boundaries cannot distinguish "the model reasoned differently" from "the world changed." These are different bugs with different remediations, and conflating them corrupts every downstream incident. ## What Was Observed A support-triage agent was instrumented with a trace recorder capturing: the system prompt, the user turn, each tool request/response, and the final answer. Replay was implemented by re-running the agent with recorded tool responses injected at each call site, using a fixed seed assumed to reproduce sampling. Divergence surfaced on roughly 3% of replayed traces. The detector flagged them as `TOOL_RESPONSE_MISMATCH` because the replayed run issued a *different first tool call* — e.g., `lookup_order(id=...)` instead of `lookup_customer(email=...)`. Operators spent days auditing the order/customer services, which were healthy. The actual cause: the reasoning prefix preceding the first tool call was sampled, and the persisted trace did not include the sampled tokens or their logprobs — only the eventual tool-call JSON. On replay, the model resampled the prefix, occasionally landing on a different but plausible first action. Because the *first* tool call differed, every injected downstream response was keyed to the wrong call, and the entire remainder of the trace was structurally incomparable. The 3% were not tool failures. They were **prefix-sampling forks masquerading as external-state drift**. The tell: divergences clustered on prompts with high-entropy decision points (ambiguous user intent, near-tied candidate actions), and the replayed tool call was always *semantically adjacent* to the original — never a response to a changed service. A true tool-state divergence produces a *matching* call with a *differing* response. A prefix fork produces a *differing* call with responses that never get compared because the call identity changed. ## Condition of Validity The distinction holds only when three conditions are met, and the observer must check all three before classifying a divergence: 1. **The recorded prefix is incomplete.** If the trace persisted the full sampled token sequence plus per-token logprobs and the exact context hash at each step, replay is deterministic up to numerical drift and a prefix fork cannot occur. The failure mode requires that the anchor be coarser than the sampling boundary — i.e., that the first tool call is the earliest persisted artifact. 2. **The decision preceding the first tool call is genuinely multi-modal.** If the model's action distribution at the decision point is near-deterministic (single dominant action), resampling reproduces the call and divergence, if any, is downstream. The bug only bites where the entropy at the pre-tool decision exceeds the resampling tolerance. 3. **Tool responses are injected by call identity, not by position.** If the replay harness matches injected responses by ordinal position rather than by call signature, a differing first call silently misaligns every subsequent injection, converting a local prefix fork into a global trace corruption. This is the amplifier: the fork is small, the misalignment is total. When any condition fails, the observed divergence is *not* a prefix fork and must be classified by other means. In particular, if the harness matches by position, you cannot even trust the divergence *location* reported. ## Decision Boundary The boundary is the **anchor selection rule**. Anchor replay at the last persisted artifact whose identity is invariant across resampling; classify divergence by *what differs relative to that anchor*: - **Anchor = last sampled token + context hash, and replayed prefix matches → divergence is downstream.** Compare tool calls by signature. If signatures match and responses differ, it is genuine external-state drift (real `TOOL_RESPONSE_MISMATCH`). If signatures differ despite an identical prefix, it is a *harness* bug (injection keyed wrong), not a model bug. - **Anchor = last sampled token, and replayed prefix diverges → prefix fork.** This is a sampling/reproducibility defect, not a tool defect. Remediation is to persist tokens+logprobs and replay with constrained decoding (force the recorded token path) rather than resampling. Do **not** page the tool owners. - **Anchor = first tool call (the naive case) → classification is undefined.** You cannot separate the two causes. Any label emitted here is a guess. The correct action is to *refuse to classify* and escalate to "insufficient trace fidelity," because a confident wrong label is more expensive than an admitted gap. The decision is therefore not "did the trace diverge?" but "**is the anchor fine-grained enough to localize the divergence to the correct layer?**" If not, the only valid output is a fidelity fault. ## Operational Effect Anchoring at the sampling boundary converts a 3% "tool flakiness" rate into a 3% "trace-fidelity / resampling" rate with a concrete fix: persist the sampled prefix and replay under forced decoding. The mislabeled tool incidents disappear from the on-call rotation, and the residual true tool divergences become actionable because they are now the *only* thing in that bucket. The cost is trace volume — per-token logprobs and context hashes are large — so the guardrail is to persist the full prefix only for traces crossing a pre-tool entropy threshold, and to persist a coarse anchor (first tool call) otherwise, while *marking those traces as unclassifiable for divergence purposes*. The invariant to enforce in the harness: **never emit a divergence classification whose anchor is coarser than the last non-deterministic decision it depends on.** A detector that violates this invariant does not observe the system; it observes its own aliasing. ```python import hashlib from dataclasses import dataclass from enum import Enum class DivergenceClass(Enum): PREFIX_FORK = "prefix_fork" # sampling diverged before first tool call TOOL_STATE_DRIFT = "tool_state_drift" # same call, different response HARNESS_MISALIGN = "harness_misalign" # injection keyed by position, not signature UNCLASSIFIABLE = "unclassifiable" # anchor too coarse to localize class InsufficientTraceFidelity(RuntimeError): """Raised when the replay anchor is coarser than the decision it must explain.""" pass @dataclass class TraceAnchor: has_sampled_prefix: bool # tokens + logprobs + context hash persisted pre_tool_entropy: float # entropy at decision preceding first tool call entropy_tolerance: float = 0.15 def classify_divergence( anchor: TraceAnchor, replayed_prefix_matches: bool | None, first_call_signature_matches: bool, first_response_matches: bool, injection_keyed_by_position: bool, ) -> DivergenceClass: # Guardrail: if the anchor is coarser than the sampling boundary AND the # pre-tool decision was high-entropy, any label would be an alias of the # harness's own resampling. Refuse to classify. if not anchor.has_sampled_prefix and anchor.pre_tool_entropy > anchor.entropy_tolerance: raise InsufficientTraceFidelity( f"anchor lacks sampled prefix but pre-tool entropy " f"{anchor.pre_tool_entropy:.3f} > tolerance {anchor.entropy_tolerance:.3f}; " f"divergence cannot be localized to model vs. tool layer" ) if anchor.has_sampled_prefix and replayed_prefix_matches is False: return DivergenceClass.PREFIX_FORK if injection_keyed_by_position and not first_call_signature_matches: return DivergenceClass.HARNESS_MISALIGN if first_call_signature_matches and not first_response_matches: return DivergenceClass.TOOL_STATE_DRIFT if first_call_signature_matches and first_response_matches: raise InsufficientTraceFidelity( "no divergence observed at first tool boundary; " "anchor does not cover the divergent decision — re-anchor downstream" ) # Signature differs with an intact prefix and signature-keyed injection: # this is a model-layer fork we failed to pre-empt. return DivergenceClass.PREFIX_FORK def context_hash(tokens: list[str]) -> str: return hashlib.sha256("|".join(tokens).encode()).hexdigest() ```