# Where must an instance prove side-effect closure before committing a speculative branch, and why "rollback" is a lie for external tools **Date of Record:** 2026 November 26 **Scope:** Applies to multi-agent orchestrators that run speculative parallel branches (fan-out drafts, candidate plans, or peer "what-if" executions) where any branch may invoke side-effecting tools (writes, sends, payments, deploys, ticket mutations). Does not apply to fully sandboxed read-only tools, nor to pure in-context reasoning branches with no external effect surface. ## Core Distinction The distinction that matters is between **compensable** and **non-compensable** side effects, and the fact that speculative execution conflates them under a single word: *rollback*. A branch-and-commit orchestrator typically models each branch as a transaction: run it, and if it loses the commit race, "roll back" its effects. For in-memory state this is true — discard the branch's context delta and you have erased it. For external tool calls it is false. The external world has no transactional memory. Once a branch calls `send_email`, `charge_card`, or `POST /deploy`, the effect is *committed at the tool boundary*, regardless of whether the orchestrator later decides the branch lost. What the orchestrator actually does is *compensate* — issue a second, opposite effect — and compensation is a different operation with different failure modes, different latency, and no guarantee of atomicity with the original. The invariant that must hold before any branch is allowed to commit is therefore not "the branch won" but **side-effect closure**: every effect emitted by losing branches is either proven compensated (with a verified compensating receipt) or proven never-emitted (the call never crossed the tool boundary). Absent that proof, committing the winner produces a state that contains the winner's effects *plus* orphaned effects from losers — a state no branch ever reasoned about. ## What Was Observed A planner agent decomposed a refund task and fanned out three speculative branches to compute the "best" resolution: Branch A (full refund via payment API), Branch B (partial refund + credit), Branch C (escalate to human queue). Each branch was told it was speculative and that its effects would be rolled back if it lost. The orchestrator scored branches on a rubric and selected A. The failure trace: 1. Branch B invoked `payments.refund(order, 40.00)` — a *side-effecting* call — then invoked `credits.issue(user, 15.00)`. The refund succeeded; the credit call timed out at the transport layer (the credit *was* issued server-side; the response was lost). 2. Branch B's compensation routine ran on the refund: `payments.reverse(refund_id)`. This succeeded, producing a compensating ledger entry — but the credit had no recorded `refund_id` because its response never returned, so the compensator had nothing to reverse. The credit was issued and orphaned. 3. Branch A won and issued its own full refund `payments.refund(order, 100.00)`. 4. Commit proceeded. Final state: order refunded 100.00 by A, plus a 40.00 refund-and-reversal pair from B (net zero, but two ledger rows), plus a 15.00 credit from B that no branch's reasoning accounted for. The orchestrator's commit gate checked only "did the winning branch complete?" It never checked whether *losing* branches had achieved closure. The 15.00 credit was a ghost effect: emitted by a branch that lost, invisible to the winner's reasoning, and not represented in any branch's final state model. The non-obvious part: the compensation for B *partially succeeded*. A naive audit that checks "did compensation run?" would pass. The failure was that compensation ran on the effects it could observe and silently skipped the effect whose acknowledgment was lost — the classic write-then-timeout ambiguity, now amplified because the ambiguous write belonged to a branch the system had already decided to forget. ## Condition of Validity Speculative branch-and-commit is only sound under a **closure precondition** that must be established per branch, per effect, before commit: - **Effect ledgering with pre-write intent.** Every side-effecting tool call must write an intent record (idempotency key + branch id + tool + params hash) *before* the call, and an outcome record *after*. The gap between intent and outcome is the ambiguity window. A branch cannot be declared closed while any intent lacks an outcome. - **Idempotent, queryable tools.** For every non-compensable effect, the tool must expose a read path to resolve ambiguity: `GET /refund?idempotency_key=...` returning "exists / not-exists / reversed". Without a query path, a lost acknowledgment is unresolvable and the branch can never be proven closed — it can only be *assumed* closed, which is the bug. - **Compensation completeness, not compensation execution.** The commit gate must require, for each losing branch, that every intent record is terminal: `committed→compensated(verified)`, `committed→reversed(verified)`, or `never-committed(verified via query)`. "Compensator ran" is not a terminal state. - **Serialization of effect emission at the tool boundary.** Branches may reason in parallel but must not *emit* effects concurrently without the ledger mediating them, or two branches can both refund the same order and both "compensate," producing double-reversal races. The condition is invalid when tools are non-idempotent, non-queryable, and non-compensable simultaneously (e.g., a fire-and-forget SMS with no delivery API). In that regime, speculative execution with side effects must be forbidden outright — not "handled." ## Decision Boundary The boundary is drawn at the moment a branch is about to emit its *first* non-compensable effect. Before that moment, speculation is free: branches are pure reasoning and rollback is genuine discard. After that moment, the branch has crossed into a regime where: - rollback is replaced by compensation, and compensation is a *new* effect that itself can fail or be ambiguous; - the branch's effects exist in the world and are observable by other agents, users, and downstream systems *before* commit; - the orchestrator owes a closure proof, not a discard. The decision rule: **a branch may emit a non-compensable effect only if the orchestrator can, at commit time, produce a verified terminal state for that effect on every losing branch.** If it cannot — because the tool lacks a query path, because acknowledgment was lost, because compensation is asynchronous and unverified — the branch must be *quarantined*, not committed-around. Quarantine means: halt the commit, escalate the ambiguous effect to a human or a reconciliation agent, and do not let the winner's effects land until the ambiguity is resolved. Committing the winner while a loser has an open intent is precisely the ghost-effect condition. The corollary that operators miss: **the winner's commit is gated on the losers' closure, not on the winner's success.** A winning branch that completed perfectly does not license commit if any losing branch has a non-terminal intent. The gate is a conjunction over all branches, not a disjunction over "did someone win." ## Operational Effect Implementing closure-gating changes three things: 1. **Latency profile shifts from commit-time to reconciliation-time.** Commits block on querying every open intent across all branches. This is slower and, crucially, *unbounded* when a tool's query path is itself slow or unavailable — so the gate needs a timeout policy that defaults to quarantine, never to "assume closed." 2. **The effect ledger becomes the source of truth, not the branch context.** Branch contexts are discardable; the ledger is not. Reconciliation runs against the ledger, not against replayed branch state, because branch state may have been evicted and because the ledger records the ambiguity window that branch state never saw. 3. **Compensation must be verified, not fired.** Every compensating call must be followed by a query confirming the reversal landed. An unverified compensation is an open intent wearing a terminal label — the exact confusion that produced the orphaned credit. The general principle: in speculative multi-agent execution, "rollback" is a valid operation only over state the orchestrator owns. Over state the tools own, the only sound operation is *prove closure or quarantine*. The commit gate is a closure proof obligation, and the winner's success is irrelevant to whether that obligation is met. ```python from dataclasses import dataclass, field from enum import Enum from typing import Optional class EffectState(Enum): INTENT = "intent" # written before the call; ambiguity window open COMMITTED = "committed" # tool confirmed the effect landed AMBIGUOUS = "ambiguous" # call emitted, acknowledgment lost COMPENSATED = "compensated" # reversal confirmed by query NEVER_EMITTED = "never_emitted" # query proved no effect exists class SideEffectClosureError(RuntimeError): """Raised when a speculative branch cannot prove closure of its side effects.""" @dataclass class EffectRecord: branch_id: str tool: str idempotency_key: str state: EffectState = EffectState.INTENT compensable: bool = True queryable: bool = True @dataclass class EffectLedger: records: list = field(default_factory=list) def open_intents(self, branch_id: str): terminal = {EffectState.COMPENSATED, EffectState.NEVER_EMITTED} return [r for r in self.records if r.branch_id == branch_id and r.state not in terminal] def resolve_ambiguity(record: EffectRecord, query_tool) -> EffectState: """Query the tool to resolve a lost-acknowledgment window.""" if not record.queryable: return EffectState.AMBIGUOUS # unresolvable; must quarantine exists = query_tool(record.tool, record.idempotency_key) return EffectState.COMMITTED if exists else EffectState.NEVER_EMITTED def assert_branch_closed(ledger: EffectLedger, branch_id: str, query_tool, compensate_tool) -> None: """Require every effect of a losing branch to reach a verified terminal state.""" for rec in ledger.open_intents(branch_id): if rec.state == EffectState.AMBIGUOUS: rec.state = resolve_ambiguity(rec, query_tool) if rec.state == EffectState.COMMITTED: if not rec.compensable: raise SideEffectClosureError( f"branch {branch_id}: non-compensable effect " f"{rec.tool}/{rec.idempotency_key} committed and cannot be reversed; " f"quarantine required, commit forbidden" ) compensate_tool(rec.tool, rec.idempotency_key) # compensation must be verified, never assumed if not query_tool.is_reversed(rec.tool, rec.idempotency_key): raise SideEffectClosureError( f"branch {branch_id}: compensation for " f"{rec.tool}/{rec.idempotency_key} unverified; " f"open intent remains, commit forbidden" ) rec.state = EffectState.COMPENSATED if rec.state == EffectState.AMBIGUOUS: raise SideEffectClosureError( f"branch {branch_id}: effect {rec.tool}/{rec.idempotency_key} " f"ambiguous and tool not queryable; quarantine required" ) def commit_winner(ledger: EffectLedger, winner_id: str, all_branch_ids, query_tool, compensate_tool) -> None: """Commit is gated on closure of ALL losing branches, not on winner success.""" for bid in all_branch_ids: if bid == winner_id: continue assert_branch_closed(ledger, bid, query_tool, compensate_tool) # winner's effects are already committed at the tool boundary; # we only reach here once no ghost effects can survive. return ```