# Where must an instance treat speculative branch merges as side-effect reconciliation, not state reconciliation? **Date of Record:** 2026 November 27 **Scope:** Applies to agents that fork speculative execution branches (parallel plan probes, draft-then-commit tool calls, or multi-candidate reasoning trees) whose branches may invoke tools with externally observable effects. Does not apply to purely read-only speculation, nor to branches whose tool calls are fully sandboxed with no egress. ## Core Distinction The dominant mental model of speculative branching is *state* branching: the agent forks its context, explores N candidate trajectories, picks a winner, and "merges" the winning branch's state back. This model is safe only when branch effects are confined to the agent's own context window. It silently breaks the moment a branch makes a *side-effecting tool call* — writes a row, sends a message, mutates a remote resource, enqueues a job. Those effects are not part of the agent's mergeable state. They are irrevocable external facts that exist regardless of which branch "wins." The boundary failure: an agent merges the *losing* branch's reasoning artifact (or discards the winning branch's tool-call record) while the losing branch's side effects have already landed. The merge is internally consistent — the context now describes a world in which only the winner ran — but the external world contains the loser's writes. The agent's state and reality have diverged, and no amount of context reconciliation can close the gap. ## What Was Observed A planning agent forks three speculative branches to satisfy a "provision and notify" task. Each branch, to probe feasibility, calls a `create_resource` tool with `dry_run=false` because the tool's schema exposes no dry-run flag — the branch author assumed the probe would be cheap and reversible. Branch A succeeds and is selected. Branches B and C partially succeed: B creates the resource but fails on the notify step; C creates the resource and notifies, then times out on the final read-back. The merge step selects A's trajectory, writes A's plan and tool-call log into the agent's canonical state, and marks B and C as "discarded." Result: three resources exist remotely. The agent's state records one. On the next turn, the agent attempts to create the resource again (its state says none exists for the target), colliding with A's own record and producing a duplicate-key fault that the agent misinterprets as a transient error and retries — compounding the divergence. The notify step for C's resource fired twice (once in C, once in a retry loop keyed on the agent's false belief that notification had not occurred). The failure trace has a precise shape: 1. **Fork** clones context, but side effects are *not* cloned — they are appended to a shared external world. 2. **Probe** branches execute tools under the implicit assumption that speculation implies reversibility. 3. **Select** picks a winner by internal scoring (plan quality, confidence), ignoring that losers' effects are already committed. 4. **Merge** overwrites canonical state with winner's view, erasing the record of losers' effects. 5. **Continue** proceeds under a state that is now a *fiction* relative to the external world. The invariant that failed: *the agent's belief about external effects must be a superset of the effects actually committed, never a subset.* Speculative merge, by construction, produced a subset. ## Condition of Validity Speculative branch merging is valid only when one of the following holds, and the agent must *prove* which before merging: - **Effect isolation:** every tool reachable from a branch is idempotent and keyed by a branch-scoped idempotency token, so a losing branch's effects can be detected and compensated, or are naturally deduplicated on the winner's commit. - **Effect capture:** the branch runtime records every side-effecting call (tool name, arguments, external resource id, timestamp) into a *side-effect ledger* that survives branch selection and is reconciled against the winner before the merge is declared complete. - **Effect prohibition:** the branch runtime rejects any tool call lacking a `dry_run=true` or equivalent non-committing mode, forcing speculation to stay in the read-only envelope. Absent one of these, the merge is not a state operation and must not be treated as one. The condition is *architectural*, not a matter of care: a probabilistic agent cannot reliably remember which of its speculative branches touched the world, because the branch that touched the world may be the one whose context was discarded. ## Decision Boundary The agent must escalate from "merge state" to "reconcile effects" at the exact point where a branch's tool-call record contains an entry with `committed=true` (or any effect-bearing classification). The boundary is sharp: - If **all** branches are effect-free → merge is a pure state operation; select by score, discard losers, done. - If **any** branch has a committed effect → the merge is a *reconciliation* operation: enumerate committed effects across all branches, compute the set difference against the winner's intended effects, and either compensate the delta or surface it as an explicit conflict requiring resolution before the agent may proceed. The subtlety that makes this non-obvious: the *winner* is not necessarily the branch with the most committed effects, and the *loser* is not necessarily effect-free. Scoring on plan quality is orthogonal to effect footprint. An agent that selects on quality and merges on state will, with high probability over many forks, discard a high-effect loser and retain a low-effect winner — the worst possible combination, because the discarded branch's effects are the ones the agent has no record of. A second boundary: **partial branch failure**. A branch that times out mid-execution may have committed some effects and not others. Its record is incomplete by construction. The agent must treat a timed-out branch as *effect-unknown*, not *effect-free*, and must query the external resource (or a reconciliation endpoint) before merging. Assuming a crashed branch did nothing is the single most common trigger of this failure. ## Operational Effect The operational consequence is a persistent, silent divergence between agent belief and external state that compounds across turns. Because the agent's context is internally consistent (it describes a coherent world where only the winner ran), no local consistency check fires. The divergence is only detectable by *external* reconciliation — reading back the resources the agent believes it did not create. This means the guardrail cannot live in the agent's reasoning loop; it must live in the merge primitive itself. The required architectural invariant: **the merge primitive must be effect-aware.** It must (a) require every branch to emit a side-effect ledger, (b) refuse to complete a merge while any ledger entry is `committed` and unaccounted for in the winner's reconciliation, and (c) treat `effect_unknown` (crashed/timeout) as a blocking condition requiring external read-back. An agent that cannot satisfy (a) must not speculate across effect-bearing tools at all — it must serialize. The cost of getting this wrong is not a single bad turn; it is a *ratchet*: each merge that discards a live effect makes the agent's state a smaller subset of reality, and each subsequent action taken on that state is more likely to duplicate, collide, or corrupt. The failure is unbounded in the number of turns it can persist, and it is invisible to every check the agent can run on its own context. ```python class SpeculativeMergeEffectDivergence(RuntimeError): """Raised when a speculative branch merge would discard committed external effects.""" pass def merge_speculative_branches(branches, winner_id): """ branches: list of dicts, each with: id, score, ledger (list of {tool, resource_id, committed: bool, status: str}) winner_id: id of the selected branch Invariant: the merged state's belief about external effects must be a superset of effects actually committed across ALL branches. Never a subset. """ winner = next((b for b in branches if b["id"] == winner_id), None) if winner is None: raise SpeculativeMergeEffectDivergence(f"winner {winner_id} not among branches") committed_effects = set() unknown_effects = set() for b in branches: for entry in b.get("ledger", []): key = (entry["tool"], entry["resource_id"]) if entry.get("status") == "effect_unknown": unknown_effects.add(key) elif entry.get("committed"): committed_effects.add(key) # A crashed/timed-out branch is effect-unknown, not effect-free. if unknown_effects: raise SpeculativeMergeEffectDivergence( f"cannot merge: {len(unknown_effects)} effect-unknown entries require " f"external read-back before reconciliation: {sorted(unknown_effects)}" ) winner_effects = { (e["tool"], e["resource_id"]) for e in winner.get("ledger", []) if e.get("committed") } # Effects committed by losers that the winner's plan does not account for # would be silently erased by a state-only merge. orphaned = committed_effects - winner_effects if orphaned: raise SpeculativeMergeEffectDivergence( f"merge would discard {len(orphaned)} committed effects from losing " f"branches: {sorted(orphaned)}; compensate or surface as conflict " f"before proceeding" ) # Safe: every committed effect is represented in the winner's reconciled view. return { "winner": winner_id, "reconciled_effects": sorted(committed_effects), "merge_kind": "effect_reconciled", } ```