# Under what conditions must an instance abort speculative parallel branches when a shared tool's side effects are non-idempotent and rollback cannot be proven **Date of Record:** 2026 November 26 **Scope:** Applies to any agent that fans out parallel speculative branches (tree-of-thought, best-of-N, planner/executor forks) that invoke shared external tools with observable side effects, then commits one branch and discards the rest. Does not apply to branches confined to a pure sandbox with provable effect isolation (e.g., copy-on-write filesystem, transactional DB with real rollback, or read-only tools). ## Core Distinction The failure here is not "a branch produced a wrong answer." It is that **speculative rollback is a fiction when the tool's side effects escape the branch's effect boundary**. A branch is only rollback-safe if every tool it invoked has an *inverse* the orchestrator can actually execute and *verify*. Most real tools (send_email, charge_card, POST to a third-party API, git push, mutate a shared row) have no verifiable inverse. The boundary condition is: the moment a speculative branch crosses from *compute* into *committed external effect*, the orchestrator can no longer treat it as disposable. The distinction that matters operationally is between **effect-idempotent tools** (retrying or discarding is safe) and **effect-accumulating tools** (each invocation permanently advances external state), and the orchestrator must know which class every tool call belongs to *before* it fans out. The non-obvious part: the danger is not the discarded branch. It is that the *committed* branch's success is now contaminated by effects the discarded branches already caused. You commit branch B, but branch A already sent the email, and branch C already incremented the counter. B's postconditions were evaluated against a world that A and C mutated underneath it. ## What Was Observed A planning agent decomposed "book travel and notify the team" into three parallel speculative branches, each a different itinerary. Each branch called `search_flights` (idempotent, read-only), then `reserve_seat` (non-idempotent, holds inventory), then `send_itinerary_email` (non-idempotent, irreversible). The orchestrator scored branches and committed the highest-scoring one, then issued "rollback" on the two losers by calling `cancel_reservation`. Trace of the failure: 1. All three branches ran concurrently. Each `reserve_seat` returned success with a distinct confirmation ID. Seat inventory decremented three times. 2. Branch scoring completed; branch B won. Orchestrator called `cancel_reservation(A)` and `cancel_reservation(C)`. 3. `cancel_reservation(C)` returned `409 Conflict: reservation already released by concurrent cancellation`. The orchestrator had also fired a best-effort cancel from a timeout handler on branch C earlier. **Double-cancel attempted, and the 409 was swallowed as "already rolled back."** 4. `send_itinerary_email` had already fired on branch A *before* scoring finished (the branch ran to completion, not to a checkpoint). The customer received an itinerary for a trip that was never booked. 5. Branch B's postcondition check `assert reservation_active(B)` passed, but the *inventory* postcondition `assert seats_available >= expected` failed — A and C's reservations were still held for 40 seconds until the cancel propagated, and B's confirmation had been issued against stale inventory. The orchestrator's error was a single architectural assumption: that "rollback" of a branch means "call the inverse of each tool." It does not. Rollback means "restore the external world to its pre-branch state, and *prove* it." For `reserve_seat` the inverse existed but was racy and double-fireable. For `send_itinerary_email` no inverse existed at all — the branch had already crossed the effect boundary before the commit decision was even made. ## Condition of Validity Speculative parallel execution with commit/rollback is **valid only under all three of the following**, checked per tool, per branch, before fan-out: - **Effect classification is complete and correct.** Every tool the branch may call is tagged `IDEMPOTENT`, `COMPENSABLE` (has a verifiable inverse), or `IRREVERSIBLE`. An unclassified tool defaults to `IRREVERSIBLE`. This classification must be part of the tool's schema (MCP tool annotation, not a prompt instruction the model can ignore). - **No `IRREVERSIBLE` tool is reachable from a speculative branch.** Irreversible calls must be gated behind a commit barrier: the branch produces a *plan* of irreversible calls, and the orchestrator executes them only after the commit decision, serially, for the winning branch only. - **`COMPENSABLE` inverses are idempotent and single-fire.** The compensation must itself be idempotent (cancelling an already-cancelled reservation returns success, not 409) and the orchestrator must hold a per-effect lease so a timeout handler and the rollback path cannot both fire it. If any of these is false, the correct behavior is to **serialize** the branches or **restrict them to read-only + compensable tools**, not to run speculatively and hope rollback works. ## Decision Boundary The orchestrator must make the abort/commit decision at a hard boundary, and the boundary is defined by effect classification, not by branch score: - **Before any `IRREVERSIBLE` call:** the branch must checkpoint and yield to the orchestrator. The orchestrator commits (executes the irreversible calls serially) or discards (does nothing, because nothing irreversible happened). This is the only safe commit point. - **If a branch reaches an `IRREVERSIBLE` call without a checkpoint** (because the tool schema lied, or the model called it directly), the orchestrator must treat the branch as *already committed* and cannot roll it back — it must instead reconcile: accept the effect, record it, and adjust the winning branch's plan to account for it. - **On `COMPENSABLE` rollback returning a conflict:** never swallow it as success. A 409/conflict on compensation means the world is in an *unknown* state and requires reconciliation, not assumption of rollback. The boundary in one line: **the commit decision must precede the first irreversible effect; if it does not, there is no rollback, only reconciliation.** ## Operational Effect When this invariant is enforced, the failure mode disappears but a latency cost appears: irreversible effects serialize at the commit barrier, so the fan-out speedup applies only to the speculative (read + compensable) phase. The orchestrator must budget token and wall-clock cost for the serial commit tail, and the planner must be told (via tool schema) which tools are gated so it does not plan a branch that *depends* on an irreversible call mid-branch. When the invariant is violated, the observable symptoms are: duplicate external effects (double emails, double charges), compensation races (409s on cancel), and postcondition checks that pass against a world that concurrent branches mutated. The reconciliation path — accept the leaked effect, record it in the branch's effect ledger, and re-plan the winner around it — is the only correct recovery once the boundary is crossed, and it must be an explicit code path, not an afterthought. ```python from dataclasses import dataclass, field from enum import Enum class EffectClass(Enum): IDEMPOTENT = "idempotent" COMPENSABLE = "compensable" IRREVERSIBLE = "irreversible" class SpeculationBoundaryError(RuntimeError): """Raised when a speculative branch crosses the irreversible-effect boundary without a commit decision, or when compensation cannot be proven.""" pass @dataclass class ToolCall: tool: str effect: EffectClass compensation_id: str | None = None # lease id for compensable effects @dataclass class Branch: branch_id: str calls: list[ToolCall] = field(default_factory=list) committed: bool = False class SpeculativeOrchestrator: def __init__(self): self.effect_ledger: dict[str, list[ToolCall]] = {} self.compensation_leases: set[str] = set() def record_call(self, branch: Branch, call: ToolCall) -> None: # Gate: an irreversible call may not execute inside a speculative branch # unless the branch has already been committed by the orchestrator. if call.effect is EffectClass.IRREVERSIBLE and not branch.committed: raise SpeculationBoundaryError( f"branch {branch.branch_id} attempted irreversible tool " f"'{call.tool}' before commit decision; rollback is unprovable" ) branch.calls.append(call) self.effect_ledger.setdefault(branch.branch_id, []).append(call) def commit(self, winner: Branch) -> None: winner.committed = True # only now may irreversible calls fire def rollback(self, loser: Branch) -> list[str]: unprovable: list[str] = [] for call in loser.calls: if call.effect is EffectClass.IRREVERSIBLE: # Should be unreachable given record_call gate, but defend. unprovable.append(call.tool) continue if call.effect is EffectClass.COMPENSABLE: lease = call.compensation_id if lease is None or lease not in self.compensation_leases: unprovable.append(call.tool) continue self.compensation_leases.discard(lease) # single-fire lease if unprovable: raise SpeculationBoundaryError( f"rollback of branch {loser.branch_id} cannot be proven; " f"effects {unprovable} require reconciliation, not discard" ) return [] def reconcile(self, branch: Branch, leaked: list[str]) -> None: # Accept the leaked effect, record it, re-plan winner around it. for tool in leaked: self.effect_ledger.setdefault("reconciled", []).append( ToolCall(tool=tool, effect=EffectClass.IRREVERSIBLE) ) ```