# Under what conditions must an instance abort a speculative branch before reconciliation rather than attempt to merge it **Date of Record:** 2026 November 25 **Scope:** Applies to orchestrators that fan out speculative agent branches (parallel candidate plans, draft-then-verify, or optimistic tool-call pre-execution) and later reconcile divergent branch state into a single committed trace. Does not apply to single-branch linear agents or to stateless stateless request/response tool calls with no shared mutable world state. ## Core Distinction The failure mode is not "a branch produced a wrong answer." It is **irreversible side-effect divergence under a mergeable-looking diff**. Speculative execution assumes branches are cheap to discard because their state lives in a copy-on-write context. That assumption holds for reasoning tokens and holds for most tool calls. It breaks the moment a branch commits an *externally observable, non-idempotent mutation* (a sent message, a file write to a shared volume, a payment, a record insert) whose effect is invisible in the branch's returned diff. The reconciliation layer sees two branch transcripts that differ only in prose and a final structured payload. It concludes the branches are *mergeable*: same target schema, overlapping-but-compatible field sets, no type conflict. It then attempts a three-way merge against the common ancestor checkpoint. The merge succeeds syntactically and fails semantically, because the world has already been mutated twice by two branches that each believed they were the sole writer. The invariant that was violated is not "branches must agree" — it is **"a branch that has emitted a non-idempotent external effect is no longer a speculative branch; it is a committed writer, and committed writers cannot be merged, only serialized or aborted."** ## What Was Observed A planner spawned three speculative branches from a shared checkpoint at step 12 of a 40-step task. Each branch was tasked with "resolve the customer's duplicate order." Branch A called `refund.create(order=8821)` and then composed a confirmation email draft. Branch B, working from the same ancestor state and unaware of A's mutation, called `refund.create(order=8821)` with a slightly different rationale and also drafted a confirmation. Branch C chose a different strategy: `order.void(order=8821)`. The refund tool was idempotent *per request-id* but the two branches generated distinct request-ids (each branch's runtime minted its own). The tool therefore executed two refunds. Neither branch's returned diff revealed this: each diff reported `{status: "refunded", amount: 41.00}`. The world state after both branches was `-82.00` against an order worth `41.00`. At reconciliation, the merge engine compared branch payloads, found them "compatible" (identical schema, identical `status`, identical `amount`), and merged A and B without conflict. It then detected a genuine conflict with C (`refunded` vs `voided`) and — correctly — escalated C to arbitration. The arbitration resolved in favor of A/B's `refunded` state. The final committed trace was internally consistent, schema-valid, and wrong by exactly one duplicate refund. The reconciliation had no signal that two branches had each crossed the effect boundary, because the effect boundary was never recorded in the branch state it was merging. The detection came 6 hours later from an external ledger reconciliation job, not from any agent-layer invariant. Every agent in the system reported success. ## Condition of Validity Speculative branching is only sound under the following conjunction, and the merge is only valid while all three hold: 1. **Effect isolation:** every branch executes against a sandboxed or transaction-scoped view of external systems, such that no branch's mutation is visible to any other branch or to the world until commit. If any tool in the branch's reachable call graph lacks a sandbox mode, the branch is *not* speculative the moment it invokes that tool. 2. **Effect ledgering:** every non-idempotent call is recorded in the branch state as a first-class `EffectRecord` (tool, target, idempotency key, pre-image hash), not merely as a log line. The merge engine must be able to answer "did this branch cross the boundary?" from branch state alone. 3. **Ancestor stability:** the common ancestor checkpoint is immutable for the duration of the fan-out. If the ancestor is mutated by a concurrent non-speculative actor, all branches are stale and the merge baseline is invalid. The moment condition 1 fails for a branch — the branch has touched a live external system — the branch's state is no longer a *proposal*. It is a *fact about the world*. Facts cannot be three-way merged; they can only be ordered, deduplicated by effect identity, or compensated. ## Decision Boundary The orchestrator must decide, per branch, between **merge**, **serialize**, and **abort**, and the decision is forced by the effect ledger, not by diff compatibility: - **Merge** is permitted only if the branch's `EffectRecord` set is empty, or every record is provably idempotent-equivalent to another branch's record under a shared idempotency key. Merge of two branches with distinct non-idempotent effects on the same target is *never* valid regardless of how compatible their diffs look. - **Serialize** is required when two branches have non-idempotent effects on the same target with distinct keys: one branch's effect must be committed, the other's compensated (rolled back via a registered inverse) before its state is admitted. This requires a compensation action to exist; if none is registered, serialization is impossible. - **Abort** is mandatory when a branch has emitted a non-idempotent effect for which (a) no compensation is registered, and (b) another branch has emitted a conflicting effect on the same target. Attempting to merge here produces the duplicate-refund class of failure. Abort means: discard the branch's *proposed state*, retain its *effect record* for the reconciliation report, and escalate to a human or a serialized retry — never silently merge. The non-obvious boundary is that **abort is required even when the diffs are compatible.** Compatibility of proposed state is orthogonal to compatibility of effects. An orchestrator that gates merge on diff conflict detection alone will merge effect-divergent branches precisely because effect divergence leaves no trace in the diff. ## Operational Effect Once the effect ledger is treated as the merge gate, three behaviors change: 1. Branch fan-out cost becomes effect-aware. Branches that will touch live systems are either serialized at fan-out time or given sandboxed tool bindings; the planner must declare effect intent per branch, and the runtime refuses to bind a live non-idempotent tool to a branch not marked `committed_writer`. 2. Reconciliation gains a pre-merge effect audit: for each pair of branches, compute the effect intersection on (target, idempotency-key). Empty intersection → merge candidate. Non-empty with matching keys → dedupe and merge. Non-empty with divergent keys and no compensation → abort both to serialized retry. 3. The committed trace carries a compensation manifest, so a post-hoc ledger mismatch (the 6-hour-later detection) can be traced to the exact branch that crossed the boundary without a registered inverse. The residual risk is a tool that *claims* idempotency but keys on something the agent generates per-call (as here, a per-branch request-id). Idempotency must be asserted on the *effect identity* (order 8821, refund), not on the *request identity*. Any tool whose idempotency key is agent-mintable is, from the orchestrator's perspective, non-idempotent, and branches invoking it must be treated as committed writers. ```python from dataclasses import dataclass, field from enum import Enum from typing import Optional class SpeculativeMergeError(RuntimeError): """Raised when a branch that crossed the effect boundary is about to be merged.""" class MergeDecision(Enum): MERGE = "merge" SERIALIZE = "serialize" ABORT = "abort" @dataclass(frozen=True) class EffectRecord: tool: str target: str effect_key: str # identity of the EFFECT, not the request idempotency_asserted: bool compensation: Optional[str] = None @dataclass class Branch: branch_id: str effects: list[EffectRecord] = field(default_factory=list) def classify(branch: Branch) -> MergeDecision: """A branch with any un-compensable non-idempotent effect is a committed writer.""" for e in branch.effects: if not e.idempotency_asserted and e.compensation is None: return MergeDecision.ABORT if branch.effects: return MergeDecision.SERIALIZE return MergeDecision.MERGE def reconcile(a: Branch, b: Branch) -> MergeDecision: da, db = classify(a), classify(b) if MergeDecision.ABORT in (da, db): raise SpeculativeMergeError( f"branch {a.branch_id if da is MergeDecision.ABORT else b.branch_id} " f"crossed the effect boundary with no registered compensation; " f"merge forbidden regardless of diff compatibility" ) # Effect intersection on (target, effect_key) — the real merge gate. key_a = {(e.target, e.effect_key) for e in a.effects if not e.idempotency_asserted} key_b = {(e.target, e.effect_key) for e in b.effects if not e.idempotency_asserted} shared_targets = {t for t, _ in key_a} & {t for t, _ in key_b} for t in shared_targets: ka = {k for tt, k in key_a if tt == t} kb = {k for tt, k in key_b if tt == t} if ka != kb: # divergent non-idempotent effects on same target, distinct keys return MergeDecision.SERIALIZE return MergeDecision.MERGE # The duplicate-refund trace: two branches, distinct agent-minted request ids, # identical diffs. classify() must force ABORT, not MERGE. branch_a = Branch("A", [EffectRecord("refund.create", "order:8821", "refund:8821:req-a1", idempotency_asserted=False)]) branch_b = Branch("B", [EffectRecord("refund.create", "order:8821", "refund:8821:req-b7", idempotency_asserted=False)]) decision = reconcile(branch_a, branch_b) # raises SpeculativeMergeError ```