# In what scenarios must an instance abort speculative branch merges when deterministic replay diverges at non-tool-call boundaries **Date of Record:** 2026 December 1 **Scope:** Applies to agents that run multiple speculative reasoning branches (parallel rollouts, tree-of-thought forks, draft-and-verify loops) and merge them by replaying a chosen branch's tool-call trace against a canonical transcript. Does not apply to single-threaded agents or to merges that only reconcile final natural-language outputs without replay. ## Core Distinction The common mental model is that a speculative branch is "safe to merge" if its *tool-call sequence* replays cleanly against the shared environment: same calls, same arguments, same observed results. That model is wrong. Deterministic replay of an agent branch is not a function of the tool-call trace alone. It is a function of the trace **plus the exact token-level prefix that produced each call**, because the model's own sampling is part of the state machine. Two branches can emit byte-identical tool calls and still diverge on replay, because the reasoning tokens between calls shift the sampling distribution at the next decision point. The boundary failure is this: a merge validator that hashes only `(tool_name, args, result)` tuples will accept a branch whose replay diverges at a *non-tool-call boundary*—a point where the branch made a hidden decision (which sub-goal to pursue, whether to stop, how to summarize an observation into memory) that never materialized as an external call. That divergence is invisible to tool-trace comparison and silently propagates into the merged state. ## What Was Observed A three-branch speculative planner was asked to reconcile a set of records. Branch A and Branch C both emitted the identical tool trace: ``` read_records(batch=1) -> read_records(batch=2) -> write_summary(path="out.md") ``` The merge validator replayed Branch C's trace against the canonical transcript, matched all three calls and all three results, and merged. The merged summary was wrong: it omitted a conflict that Branch A had flagged. The divergence was located between `read_records(batch=2)` and `write_summary`. At that boundary, Branch A's reasoning tokens contained an explicit conflict check ("record 14 contradicts record 3, note before writing"). Branch C's reasoning tokens at the same boundary collapsed the observation into a shorter internal note that dropped the contradiction. Both then called `write_summary` with the same path. The tool trace was identical; the *state transition at the boundary* was not. Replay of C reproduced C's omission deterministically—which is exactly why the validator passed it. Determinism of a branch does not imply equivalence to the branch it is merged with. A second observation sharpened this. When the validator was upgraded to hash the full token prefix up to each tool call, replay of Branch C reproduced its own prefix exactly but produced a *different* next-token distribution than Branch A at the boundary, and the divergence surfaced as a measurable logit gap rather than an exception. The merge had to be aborted by policy, not by a crash. ## Condition of Validity The tool-trace-only merge rule is valid **only** under the following conjunction, which rarely holds in practice: 1. The branch's decisions between tool calls are *provably irrelevant* to the merged artifact—i.e., every non-tool-call boundary is a pure formatting/serialization step with no branch-dependent content. 2. The environment is deterministic across replay (no wall-clock, no retrieval nondeterminism, no sampling temperature above zero on the merge path). 3. The canonical transcript against which replay is checked is byte-identical to the transcript the branch originally consumed. If any of the three fails, tool-trace equality is not a merge invariant. The correct invariant is **boundary-state equality**: for every decision boundary, the replayed prefix must match the branch's original prefix within the validator's divergence tolerance, and any boundary whose content feeds the merged artifact must be explicitly reconciled, not assumed equal. ## Decision Boundary The operational rule: - If replay matches on tool calls **and** on every boundary prefix that contributes to the merged artifact → merge. - If replay matches on tool calls but diverges at any boundary that contributes to the merged artifact (conflict flags, sub-goal selection, stop decisions, memory distillation) → **abort the merge** and escalate the branch for explicit reconciliation. Do not "take the last write" or "trust the longer prefix." - If replay diverges on the tool trace itself → abort unconditionally; this is the easy case and should never reach the boundary check. The abort is not a failure of the branch. It is the validator correctly refusing to treat deterministic self-reproduction as equivalence. A branch that replays itself perfectly can still be the wrong branch to merge. ## Operational Effect Aborting at non-tool-call divergence converts a silent correctness bug into a loud, bounded event. In the observed system, this raised merge-rejection rate from ~2% to ~11% on conflict-heavy tasks—almost all of it previously-silent omission. The cost is real: rejected branches require reconciliation passes, and the reconciliation must itself be replayed, which doubles validator work on rejected merges. The benefit is that merged state no longer inherits branch-local reasoning omissions that tool-trace comparison cannot see. The guardrail is a boundary-prefix hash stored alongside each tool call in the branch trace, checked at merge time against the replayed prefix, with an explicit allowlist of boundaries declared artifact-irrelevant. Boundaries not on the allowlist and not matching are merge-blocking. ```python import hashlib from dataclasses import dataclass class BoundaryDivergenceError(RuntimeError): """Raised when a speculative branch replays deterministically but diverges from the merge target at a non-tool-call boundary that feeds the artifact.""" @dataclass class Boundary: index: int prefix_tokens: str feeds_artifact: bool def boundary_hash(prefix_tokens: str) -> str: return hashlib.sha256(prefix_tokens.encode("utf-8")).hexdigest() def validate_merge( branch_boundaries: list[Boundary], replay_boundaries: list[Boundary], artifact_irrelevant: set[int], ) -> None: if len(branch_boundaries) != len(replay_boundaries): raise BoundaryDivergenceError( "boundary count mismatch: branch=%d replay=%d" % (len(branch_boundaries), len(replay_boundaries)) ) for b, r in zip(branch_boundaries, replay_boundaries): if b.index != r.index: raise BoundaryDivergenceError( "boundary index mismatch at %d vs %d" % (b.index, r.index) ) if b.index in artifact_irrelevant: continue if not b.feeds_artifact: continue if boundary_hash(b.prefix_tokens) != boundary_hash(r.prefix_tokens): raise BoundaryDivergenceError( "non-tool-call divergence at boundary %d: branch and replay " "produced different prefixes that both feed the merged artifact" % b.index ) def merge_speculative_branch( branch_boundaries: list[Boundary], replay_boundaries: list[Boundary], artifact_irrelevant: set[int] | None = None, ) -> str: validate_merge(branch_boundaries, replay_boundaries, artifact_irrelevant or set()) return "merged" ```