# Why does an instance replaying a speculative branch diverge from its own committed log after a sibling agent's tool result is re-injected out of order **Date of Record:** 2026 December 1 **Scope:** Applies to orchestrators that detect agent divergence by deterministic replay of a committed event log, where speculative (tentative) tool calls are issued ahead of a serialization barrier and later committed or discarded. Does not apply to orchestrators that forbid speculative execution, nor to single-agent systems with no sibling concurrent commits interleaved into the replay stream. ## Core Distinction There are two things called "replay" in an agent runtime, and conflating them is the root of the failure: - **Log replay** reconstructs state by feeding the *committed* event stream back through the same reducers. It is deterministic only if the reducer is a pure function of the event sequence. - **Speculative replay** reconstructs state by re-executing the agent's *reasoning path*, including tool calls that were issued speculatively and whose results were not yet durably ordered relative to sibling agents' commits. The invariant that speculative divergence detection silently assumes is: *the tool-result events observed during speculative replay appear in the same relative order as they did at commit time.* This holds for a single agent. It breaks the moment a sibling agent's commit is interleaved into the replay window, because the replay engine re-derives delivery order from the committed log's total order, while the original execution consumed results in *arrival* order across a shared transport. Arrival order and committed order are not the same partial order, and the LLM's next-token distribution is sensitive to the difference. ## What Was Observed An orchestrator ran a planner agent `P` and two workers `W1`, `W2` under a shared event log with a commit barrier every N=8 events. `P` issued speculative tool calls `t_a` (to `W1`) and `t_b` (to `W2`) in the same reasoning step, with `t_b` dispatched 12 ms after `t_a`. At runtime, `t_b`'s result arrived first (W2's sandbox was warm), so `P`'s next reasoning step consumed `[r_b, r_a]`. The log, however, assigned sequence numbers at commit: `t_a`→seq 41, `t_b`→seq 42, `r_a`→seq 43, `r_b`→seq 44, because the barrier flushed in dispatch order, not arrival order. During divergence detection, the auditor replayed `P` from a checkpoint at seq 40. The replay engine fed results in committed order `[r_a, r_b]`. `P`'s reconstructed reasoning step differed from the recorded one at the first token that referenced a result value. The auditor flagged a divergence of confidence 0.87 and quarantined `P`'s committed branch as "non-reproducible reasoning." The reasoning was not non-reproducible. The *replay input order* was wrong. The divergence detector had manufactured a false positive by treating a commutative-but-not-order-insensitive consumption as if it were ordered. Two secondary observations compounded it: 1. **Checkpoint skew.** The checkpoint at seq 40 was taken mid-speculation: `t_a` and `t_b` were in flight, their results not yet committed. The checkpoint serialized `P`'s context as containing *no* tool results, but the live `P` at the moment of checkpoint already had `r_b` buffered in a transport-side queue. Replay from the checkpoint did not have that buffer, so the first replayed step had a different prefix than the live step even before ordering mattered. 2. **Sibling commit interleaving.** `W2`'s own commit of `r_b` carried a nested sub-event (`W2` had called a sub-tool) that the replay engine flattened ahead of `r_a`. The flattening preserved total order but destroyed the parent-child grouping `P` had observed, so `P`'s replay saw a differently-shaped result object. ## Condition of Validity Deterministic replay is a sound divergence detector for an agent instance **iff** all of the following hold: - **C1 (Order fidelity).** For every tool result the agent consumed during live execution, the replay stream delivers it at the same position relative to all other results the agent consumed in the same reasoning step. If the agent's consumption is order-sensitive (and an LLM's is, because the context prefix changes), arrival order must be recoverable from the log. - **C2 (Barrier alignment).** Checkpoints are taken only at points where no speculative call is in flight, or the in-flight set is serialized into the checkpoint along with its partial results. - **C3 (Grouping fidelity).** Nested sibling events are replayed with the same structural grouping the agent observed, not flattened into a total order. - **C4 (Purity).** The reducer that reconstructs agent context is a pure function of the replayed event sequence — no wall-clock reads, no transport-queue peeks, no random sampling. The observed incident violated C1 (arrival vs. commit order), C2 (mid-speculation checkpoint), and C3 (flattened sub-event). Any one violation is sufficient to produce a false divergence. C4 was satisfied, which is why the failure was subtle: the replay *looked* deterministic and the divergence *looked* like a reasoning bug. ## Decision Boundary The boundary is between **order-insensitive** and **order-sensitive** consumption, and it must be decided per tool result, not per agent: - If a tool result's value is the only thing the agent conditions on (e.g., a scalar the agent reads once), reordering is harmless and C1 can be relaxed. - If the agent's next step is a function of the *sequence* of results (any multi-result step, any step where the agent compares or aggregates), C1 is binding and must be enforced by recording an **arrival ordinal** alongside the commit sequence number. Concretely: log each result event with both `commit_seq` (total order) and `arrival_ord` (per-agent consumption order within a reasoning step). Replay must reconstruct agent context using `arrival_ord`, and use `commit_seq` only for cross-agent causal ordering. When the two disagree, the replay engine must not silently pick one — it must flag the step as *order-ambiguous* and refuse to emit a divergence verdict. The guardrail is a **replay precondition check** that runs before any divergence comparison: if the checkpoint's in-flight set is non-empty, or if any consumed result lacks an arrival ordinal, or if a sibling sub-event was flattened, the auditor must downgrade from "divergence" to "inconclusive" rather than quarantine the branch. ## Operational Effect Without the guardrail, the orchestrator quarantined a correct branch, re-ran `P` from the pre-speculation checkpoint, and — because the re-run again consumed results in a different arrival order — produced a *third* distinct reasoning trace. The divergence detector now had three mutually inconsistent traces for one logical step and, being a majority-vote auditor, escalated to human review. The root cause was never a reasoning error; it was the auditor's own replay input ordering. The fix that held: arrival ordinals on every result event, checkpoints gated on an empty in-flight set (or serializing the in-flight set), and a hard refusal to compare when the order-ambiguity flag is set. After the fix, the same workload produced zero false divergences across 40k replayed steps, and the two genuine divergences that remained were both traced to a real tool-output non-determinism (a sandbox clock read), which the auditor correctly attributed. The general lesson: a divergence detector is only as trustworthy as its replay input ordering. An LLM agent's reasoning is a function of its context *prefix*, and the prefix includes the order in which results were seen. Committed order is a total order chosen for durability; consumption order is a per-agent partial order chosen by transport timing. Replay must reconstruct the latter, not the former, or it will confidently accuse the agent of a bug the auditor itself introduced. ```python from dataclasses import dataclass, field from typing import Optional class ReplayOrderAmbiguity(RuntimeError): """Raised when replay cannot reconstruct the agent's consumption order.""" pass @dataclass class ResultEvent: commit_seq: int arrival_ord: Optional[int] # per-agent consumption order within a step agent_id: str value: object flattened_from_parent: bool = False @dataclass class Checkpoint: seq: int agent_id: str in_flight: set = field(default_factory=set) # speculative calls not yet committed def reconstruct_consumption_order( checkpoint: Checkpoint, events: list[ResultEvent], ) -> list[ResultEvent]: # C2: refuse to replay from a checkpoint with in-flight speculation if checkpoint.in_flight: raise ReplayOrderAmbiguity( f"checkpoint seq={checkpoint.seq} for {checkpoint.agent_id} " f"has in-flight speculation {sorted(checkpoint.in_flight)}; " f"arrival order of pending results is unrecoverable" ) relevant = [e for e in events if e.agent_id == checkpoint.agent_id] # C3: refuse if any sibling sub-event was flattened (grouping lost) if any(e.flattened_from_parent for e in relevant): raise ReplayOrderAmbiguity( "sibling sub-event was flattened into total order; " "parent-child grouping the agent observed cannot be reconstructed" ) # C1: arrival ordinal must exist for every consumed result missing = [e for e in relevant if e.arrival_ord is None] if missing: raise ReplayOrderAmbiguity( f"{len(missing)} result event(s) lack arrival_ord; " f"commit_seq alone is insufficient for order-sensitive consumption" ) # Reconstruct the order the agent actually consumed results in. return sorted(relevant, key=lambda e: e.arrival_ord) def detect_divergence( checkpoint: Checkpoint, events: list[ResultEvent], recorded_trace: list, replay_fn, ) -> bool: try: ordered = reconstruct_consumption_order(checkpoint, events) except ReplayOrderAmbiguity: # Downgrade to inconclusive rather than quarantining the branch. return False replayed_trace = replay_fn(checkpoint, ordered) return replayed_trace != recorded_trace ```