# How does an instance detect rollback divergence when speculative sub-agents commit non-idempotent tool side effects before the branch verdict **Date of Record:** 2026 November 24 **Scope:** Applies to orchestrators that fan out speculative agent branches against a shared tool surface and intend to discard losing branches. Does not apply to read-only tool surfaces or to branches that are fully isolated in per-branch sandboxes with independently replayable side-effect journals. ## Core Distinction Speculative multi-agent execution assumes a clean symmetry: a branch either wins and its effects are kept, or it loses and its effects are *undone*. That symmetry is a property of the branch's **reasoning trace**, not of the **tool surface it touched**. The boundary failure is not "rollback failed"—it is that rollback *succeeded against the wrong ledger*. The orchestrator rolls back its own recorded intent log while the external tool (payment rail, email relay, file mutation, downstream agent's inbox) has already durably committed the effect. The instance believes it reverted; the world did not. The non-obvious part: the divergence is invisible at the moment of rollback. The rollback call returns `ok`. The failure surfaces later as a *duplicate*—a second charge, a second email, a second state mutation—when the winning branch independently re-executes the same logical step. Detection therefore cannot happen at rollback time. It must happen by *reconciling the external effect ledger against the speculative branch's intent ledger* before the winner is allowed to commit. ## What Was Observed An orchestrator spawned three speculative branches (A, B, C) to resolve a customer-refund decision. Each branch was granted a scoped tool handle including `issue_refund(order_id, amount)`. Branch A, mid-reasoning, decided the refund was warranted and called `issue_refund` with a *provisional* flag it believed the tool honored. The tool did not honor provisional semantics; it executed the refund and returned a transaction id. Branch B won the consensus vote. The orchestrator issued `rollback(branch_A)` and `rollback(branch_C)`. Both returned `ok`. The orchestrator then executed the winning plan, which included—correctly—`issue_refund` for the same order. Observed trace: ``` [A] tool_call issue_refund(order=8841, amt=120) -> tx=TX-77af (committed, provisional flag ignored) [B] consensus winner [ORCH] rollback(A) -> ok (reverted intent log only; TX-77af untouched) [ORCH] execute_winner -> issue_refund(order=8841, amt=120) -> tx=TX-91c2 [RECONCILE] order 8841 refunded twice: TX-77af, TX-91c2 ``` The rollback did not lie. It faithfully reverted the branch's *recorded* intent. The tool surface had no concept of speculative intent to revert. The invariant that was assumed—"a rolled-back branch leaves no external effect"—was never enforced by any component. It was an emergent hope. The detection signal was not an error. It was a **cardinality mismatch**: two committed external effects for one logical decision. This is the only reliable detector, because a non-idempotent tool that ignores a speculative flag produces no error, no warning, and a well-formed success response. ## Condition of Validity The failure requires all of the following to co-occur: 1. **Non-idempotent tool with no speculative/provisional semantics.** The tool either lacks a provisional mode or silently ignores the flag. `issue_refund`, `send_email`, `deploy`, `transfer`, `append_to_ledger` are canonical. 2. **Speculative branches share the production tool handle.** If branches get isolated sandboxes or dry-run handles, the effect never reaches the real ledger and rollback is genuinely clean. 3. **Rollback is defined over the intent log, not the effect ledger.** This is the default in most orchestrators: rollback = discard branch state + replay-safe cleanup of *recorded* calls, not "compensate committed external effects." 4. **The winning branch re-executes the same logical step.** If the winner's plan happens to skip the step the loser already committed, no duplicate appears—the divergence stays latent and may surface on a later retry, which is worse. When any one is absent, the boundary does not occur. The dangerous configuration is the *default* one: shared handle, intent-log rollback, non-idempotent tools. Nothing in the stack flags it. ## Decision Boundary The instance must decide, per tool call, whether to allow a speculative branch to touch a non-idempotent tool at all. The boundary: - **If the tool declares `idempotent=true` or exposes a provisional/two-phase commit mode:** speculative branches may call it; rollback is safe because the effect is either keyed (dedupe on retry) or not yet committed. - **If the tool is non-idempotent and has no provisional mode:** speculative branches must be **forbidden from committing**. They may *plan* the call (emit an intent record) but not *execute* it. The call executes only after the branch wins. This converts the problem from "rollback a committed effect" (impossible without compensation logic) to "commit a planned effect" (trivial). - **If the tool is non-idempotent but the orchestrator cannot pre-declare which tools are which:** the orchestrator must run a **reconciliation gate** before winner commit—query the external effect ledger for the logical decision key and abort if any speculative branch already produced a committed effect. This is the fallback, and it is strictly weaker: it detects the duplicate but cannot prevent the first speculative commit. The hard rule: *a speculative branch may never be the first committer of a non-idempotent effect.* Speculation is for reasoning, not for side effects. The moment a branch commits a non-idempotent effect, it is no longer speculative—it is a partial winner, and the consensus protocol must be re-entered with that effect as a fixed constraint. ## Operational Effect The instance that respects this boundary pays a latency cost: non-idempotent calls are serialized behind the consensus vote rather than overlapped across branches. That cost is the price of a real invariant. The instance that ignores it gets apparent parallelism and a duplicate-effect bug that is invisible in the success path and only appears under reconciliation, often days later, often in a financial or communication ledger where duplicates are user-visible. Concretely, the orchestrator must: 1. **Classify every tool handle** as idempotent / provisional-capable / non-idempotent at handle-acquisition time, and refuse to issue a non-idempotent handle to a speculative branch. 2. **Key every non-idempotent call** with a deterministic `decision_key` derived from the logical intent, so that even if two branches somehow both commit, reconciliation can detect the collision by key rather than by amount-matching. 3. **Gate winner commit on effect-ledger reconciliation** for all non-idempotent tools the winner's plan touches, not just the tools the losing branches touched—because the winner may itself be a retry of a prior partial run. 4. **Treat rollback `ok` as meaningless** for non-idempotent tools. Rollback success is a statement about the intent log. It is not a statement about the world. The invariant to enforce: `committed_external_effects(decision_key) <= 1` at all times, checked at the reconciliation gate, with the speculative-branch prohibition as the primary defense and reconciliation as the detector of last resort. ```python from dataclasses import dataclass, field from enum import Enum from typing import Dict, List class RollbackDivergenceError(RuntimeError): """Raised when a speculative branch's committed external effect diverges from the orchestrator's intent-log view of that branch.""" pass class ToolSemantics(Enum): IDEMPOTENT = "idempotent" PROVISIONAL = "provisional" # two-phase commit honored NON_IDEMPOTENT = "non_idempotent" # no provisional mode; commits immediately @dataclass class ToolHandle: name: str semantics: ToolSemantics @dataclass class EffectLedger: # decision_key -> list of committed transaction ids committed: Dict[str, List[str]] = field(default_factory=dict) def record(self, decision_key: str, tx_id: str) -> None: self.committed.setdefault(decision_key, []).append(tx_id) def count(self, decision_key: str) -> int: return len(self.committed.get(decision_key, [])) def acquire_handle(tool: ToolHandle, branch_is_speculative: bool) -> ToolHandle: """Refuse to hand a non-idempotent tool to a speculative branch.""" if branch_is_speculative and tool.semantics is ToolSemantics.NON_IDEMPOTENT: raise RollbackDivergenceError( f"speculative branch requested non-idempotent tool '{tool.name}'; " "commit must be deferred to winner" ) return tool def reconciliation_gate(ledger: EffectLedger, decision_key: str) -> None: """Must run before winner commit for every non-idempotent decision_key.""" n = ledger.count(decision_key) if n > 1: raise RollbackDivergenceError( f"decision_key '{decision_key}' has {n} committed external effects " f"({ledger.committed[decision_key]}); rollback of speculative " "branch(es) did not revert the world" ) def execute_winner_plan( ledger: EffectLedger, decision_key: str, tool: ToolHandle, commit_fn, ) -> str: # Gate first: detect any speculative commit that leaked through. reconciliation_gate(ledger, decision_key) if ledger.count(decision_key) == 1: # Already committed by a leaked speculative branch; do not duplicate. return ledger.committed[decision_key][0] tx_id = commit_fn() ledger.record(decision_key, tx_id) return tx_id ```