# When must an instance abort a speculative branch: irreversible tool effects poisoning merge-time reconciliation in branch-and-merge plan trees **Date of Record:** 2026 November 28 **Scope:** Applies to orchestration topologies where a planner agent forks N speculative sub-plans (branches) that execute concurrently against shared external state, then a reconciler selects or merges branch outcomes. Does not apply to purely read-only fan-out, nor to single-branch retry loops where rollback is trivially local. ## Core Distinction The distinction that matters is between **speculation that is rollback-safe** and **speculation that is rollback-blind**. A branch is rollback-safe when every effect it produces is either (a) recorded in a journal keyed to that branch ID and compensable by an inverse operation, or (b) confined to a sandbox whose teardown is atomic. A branch is rollback-blind when it emits effects that the orchestrator *believes* are compensable but which are not — non-idempotent side effects (a `send_email`, a `POST /orders`, a `git push --force`, a state mutation on an external MCP server with no undo primitive) whose inverse the reconciler cannot construct because the effect's identity was never captured at emission time. The failure is not "a speculative branch failed." The failure is: **a losing branch already committed an irreversible effect, the reconciler discards the branch as if it were speculative, and the merge proceeds on the assumption that only the winning branch's effects exist.** The branch-and-merge tree's central invariant — *the union of surviving branch effects equals the effect set of the merged plan* — is silently violated. This is a continuity invariant, not a correctness bug in any single agent: every agent behaves per its local contract. The violation is an emergent property of the tree. ## What Was Observed A three-branch speculative planner (branches B1, B2, B3) was dispatched to reconcile a customer refund. Each branch was given an independent tool surface and told to "resolve the ticket." The reconciler was configured to pick the branch with the highest self-reported confidence and merge its trace. - **B1** (confidence 0.61) called `refund.create(order_id=8841, amount=120.00)` — an irreversible external effect — then hit a token-budget exhaustion mid-argument-construction for the follow-up `refund.confirm`. The branch was marked `FAILED`, discarded. - **B2** (confidence 0.88) called `refund.create(order_id=8841, amount=120.00)` as well, because both branches independently converged on the same resolution. It then called `refund.confirm`. Branch marked `SUCCEEDED`, selected. - **B3** (confidence 0.42) called `ticket.note(...)` only. Discarded. The reconciler merged B2's trace. The final system state contained **two** `refund.create` calls against order 8841 — one orphaned by B1, one live in B2 — for a combined 240.00 against a 120.00 obligation. The reconciler's merge logic was correct by its own definition: it merged the selected branch. The defect was that the branch tree treated `FAILED` as equivalent to `no effect`, when in fact `FAILED` meant *effect emitted, confirmation not reached*. The tell was subtle: B1's failure was a **post-effect, pre-confirmation** failure. Token-budget exhaustion, timeout, or a downstream 5xx on the confirmation call all produce this signature. The branch's journal recorded the intent and the request, but the orchestrator's rollback pass only ran compensation for branches marked `ABORTED_BY_PLANNER` — a status that required the planner to have *decided* to abort, which it never did because it had run out of budget to decide anything. ## Condition of Validity The invariant "discarded branches contributed no effects" holds **iff** every branch's effect emission is preceded by a durable, orchestrator-visible reservation and followed by a durable confirmation, and the reconciler's discard path consults the reservation journal rather than the branch status. Concretely, three conditions must all hold simultaneously: 1. **Effect identity is captured before emission.** The branch must write `(branch_id, idempotency_key, effect_descriptor)` to a shared journal *prior* to the tool call, in a transaction that survives branch death. If the key is generated inside the tool call, a branch that dies mid-call leaves no recoverable identity. 2. **Discard is a journal operation, not a status transition.** The reconciler must enumerate the journal for *all* branches (including `FAILED`, `TIMEOUT`, `BUDGET_EXHAUSTED`) and compensate or deduplicate by `idempotency_key`, not by branch outcome. 3. **The external tool surface honors idempotency keys.** If `refund.create` ignores the key, B2's call cannot be deduplicated against B1's orphan, and the merge is unsound regardless of journal discipline. When any of these fails, the branch tree is operating in the rollback-blind regime and the merge invariant is unprovable. The correct posture in that regime is to **serialize** effect-bearing branches (no speculation across the effect boundary) or to **fence** them behind a single-flight lease per `idempotency_key`. ## Decision Boundary The decision an instance must make, at the moment it is about to emit an irreversible effect inside a speculative branch, is: - **If the effect surface is idempotent-keyed and the journal write is durable and pre-emission** → speculation may proceed; the merge is sound because orphaned effects are deduplicable. - **If the effect surface is idempotent-keyed but the journal write is post-emission or best-effort** → speculation must not cross the effect boundary; the branch must be promoted to a serialized commit path before emitting, and the other speculative branches must be *fenced* (their effect-capable tools revoked) until the commit resolves. - **If the effect surface is neither idempotent nor compensable** → speculation across it is forbidden. The planner must restructure the tree so the irreversible step sits at a single leaf reached by exactly one branch. The boundary is *not* "does the branch have high confidence." Confidence is a self-report from a probabilistic reasoner and is uncorrelated with whether the branch already mutated the world. A 0.42-confidence branch that emitted an effect is more dangerous to the merge than a 0.95-confidence branch that did not. The reconciler must weight by *journal state*, never by reported confidence. The failure mode also has a compounding variant: when B1 and B2 both succeed at `refund.create` with *different* amounts (say 120.00 and 100.00 because the branches disagreed on the obligation), deduplication by key fails — the keys differ — and the merge produces a double refund of 220.00 with no orphan to blame. This is the branch-divergence case, and it is strictly worse because there is no `FAILED` status to flag it. The guardrail is that the idempotency key must be derived from the *effect's semantic identity* (`refund:order:8841`), not from the branch or the amount, so divergent branches collide on the key and the second emission is rejected by the tool surface rather than the reconciler. ## Operational Effect The operational consequence of getting this wrong is a class of incident that is nearly undiagnosable from logs: the reconciler reports `merged branch B2, confidence 0.88`, the branch tree reports all branches accounted for, and yet external state shows effects from branches that the tree claims never committed. Post-hoc reconstruction requires correlating the external system's request log against the orchestrator's branch journal by timestamp and argument hash, because no shared identifier exists. The architectural guardrail that bounds this is the **effect-journal-first protocol**: no branch may invoke an effect-capable tool until it has committed a reservation record to a journal that the reconciler reads on every discard path. The reconciler's merge step becomes a journal fold — `effects(merged) = dedupe(⋃ effects(b) for b in all_branches, compensated where b ∉ selected)` — rather than a trace concatenation. This converts the merge from an assumption ("discarded branches are clean") into a computation ("discarded branches are compensated or deduplicated"), and it is the only form in which the branch-and-merge invariant is checkable rather than asserted. A secondary effect: branch status enums must be extended beyond `SUCCEEDED/FAILED` to distinguish `FAILED_PRE_EFFECT` from `FAILED_POST_EFFECT_UNCONFIRMED`. The latter must never be silently discarded; it must be routed to a compensation queue even when the reconciler has already selected a different branch, because the orphan effect exists independently of the selection. ```python from __future__ import annotations from dataclasses import dataclass, field from enum import Enum from typing import Iterable class BranchStatus(Enum): SUCCEEDED = "succeeded" FAILED_PRE_EFFECT = "failed_pre_effect" FAILED_POST_EFFECT_UNCONFIRMED = "failed_post_effect_unconfirmed" ABORTED_BY_PLANNER = "aborted_by_planner" class MergeInvariantViolation(RuntimeError): """Raised when a merge would proceed over an uncompensated orphan effect.""" @dataclass(frozen=True) class EffectReservation: branch_id: str idempotency_key: str descriptor: str confirmed: bool @dataclass class Branch: branch_id: str status: BranchStatus reservations: list[EffectReservation] = field(default_factory=list) def reconcile_merge( branches: Iterable[Branch], selected_branch_id: str, external_effect_log: dict[str, int], ) -> dict[str, int]: """Compute the effect set of the merged plan. Invariant: the merged effect set must contain exactly one confirmed emission per idempotency_key, and every orphaned reservation must be compensated before the merge is declared sound. """ branches = list(branches) selected = {b.branch_id: b for b in branches}.get(selected_branch_id) if selected is None: raise MergeInvariantViolation( f"selected branch {selected_branch_id!r} not present in tree" ) merged: dict[str, int] = {} orphans: list[EffectReservation] = [] for branch in branches: for res in branch.reservations: if branch.branch_id == selected_branch_id: merged[res.idempotency_key] = merged.get(res.idempotency_key, 0) + 1 elif not res.confirmed: # Effect emitted, confirmation never reached: orphan. orphans.append(res) # Confirmed non-selected emissions are presumed compensated by # the branch's own rollback; the reconciler must verify below. if orphans: raise MergeInvariantViolation( "discard path encountered post-effect unconfirmed reservations; " f"compensate before merge: {[(o.branch_id, o.idempotency_key) for o in orphans]}" ) for key, count in merged.items(): if count > 1: raise MergeInvariantViolation( f"duplicate emission for idempotency_key {key!r}: " f"merged plan would emit {count} times" ) if external_effect_log.get(key, 0) > count: raise MergeInvariantViolation( f"external log shows {external_effect_log[key]} emissions for {key!r} " f"but merged plan accounts for {count}; orphan effect detected" ) return merged ```