# In what scenarios must an instance refuse rollback of a speculative agent branch after a side-effecting tool call has been acknowledged by an external peer **Date of Record:** 2026 November 27 **Scope:** Applies to orchestrators that speculatively fork N parallel agent branches from a shared checkpoint, allow branches to invoke side-effecting tools (writes, sends, payments, state mutations on external systems), and later select a winning branch and roll back the losers. Does not cover pure-read speculation, in-process tool calls with no external acknowledgment, or single-branch retry loops. ## Core Distinction The failure is not "a losing branch did work that got discarded." That is the expected cost of speculation. The failure is a **rollback that succeeds locally while failing externally**, producing a divergence between the orchestrator's reconciled state and the irreversible effects a losing branch already committed on a remote system. Speculative execution is safe only under an invariant that is rarely stated explicitly: > **A branch is rollback-eligible iff every side effect it produced is either (a) unacknowledged, (b) idempotent under a replay key the orchestrator controls, or (c) compensable by a registered inverse operation whose success the orchestrator has itself observed.** The boundary condition is the case where a losing branch's tool call returned an *ambiguous* acknowledgment — a timeout, a 5xx after commit, a partial write — and the orchestrator's rollback logic treats "no confirmed success" as "no effect." That conflation is the bug. The remote system may have committed; the orchestrator's local log says the branch was aborted; reconciliation then picks the winner and reports a clean state that does not exist. ## What Was Observed A planner agent forks three branches (`B1`, `B2`, `B3`) from checkpoint `C0` to satisfy a multi-step provisioning task. Each branch is given the same goal and a distinct strategy hint. All three call `provision_resource(idempotency_key=derived_from_branch_id, ...)` against an external control plane. - `B1` returns `200 OK` with a resource handle. - `B2` returns `504 Gateway Timeout` after 30s. The control plane had, in fact, committed the resource at t=12s and the response was lost in a proxy hop. - `B3` returns `429`, retried once internally, then `200 OK`. The reconciler scores the branches, selects `B1` as winner, and rolls back `B2` and `B3`. Rollback for `B2` is a no-op because the orchestrator's branch journal recorded the tool call as `failed` (no acknowledgment). Rollback for `B3` issues a `DELETE` on the handle it holds. The final reconciled state reports one resource. The control plane holds two: `B1`'s and the orphaned resource from `B2`'s timed-out-but-committed call. The divergence is silent. It surfaces hours later when quota accounting trips, or when a downstream consumer enumerates resources and finds a duplicate with a foreign idempotency key namespace. The orchestrator's own logs are internally consistent — which is precisely why the failure is hard to attribute. The trigger condition is narrow and reproducible: 1. Branch tool call is **non-idempotent from the orchestrator's perspective** because the idempotency key is scoped to the branch (`branch_id`), not to the logical intent. 2. The remote system **commits before responding**, and the response is lost. 3. The orchestrator's journal records the call as `failed` rather than `unknown`. 4. Rollback logic treats `failed` as `no effect`. Any one of these being different prevents the failure. All four co-occurring is common in practice because branch-scoped idempotency keys are the natural default when forking, and `failed`/`unknown` are frequently collapsed into a single enum value. ## Condition of Validity The refusal-to-rollback rule is valid — and must be enforced — under the following conditions. Outside them, ordinary rollback is correct and the guardrail should not fire. - **The tool call is side-effecting and externally acknowledged-or-ambiguous.** If the branch only read, or only called tools whose effects are scoped to the orchestrator's own transactional store, rollback is unconditionally safe. - **The orchestrator cannot prove non-commit.** A `504`, a connection reset after request transmission, or a partial response all count as "cannot prove non-commit." A `4xx` returned before the remote handler ran is proof of non-commit; a `5xx` is not. - **No compensation is registered, or compensation is unverified.** If the orchestrator holds a compensation handle and has *observed* its successful execution, the branch is rollback-eligible again. Unverified compensation is not compensation. - **The winner selection is not yet final.** If the reconciler has not committed to a winner, the correct action is to *hold* the ambiguous branch open (quarantine) rather than roll it back — because the ambiguous branch may itself be the winner, and rolling it back destroys the only handle to its effect. The invariant that makes this tractable: **the orchestrator must never transition a branch from `speculating` to `aborted` when any of its tool calls is in `unknown` state.** `unknown` must propagate to a `quarantined` branch state that requires explicit reconciliation — either by querying the remote system for the effect (using a stable intent-scoped key) or by escalating to a compensating transaction that the orchestrator can verify. ## Decision Boundary The precise decision the orchestrator must make at reconciliation time: - **If `branch.state == unknown_effect` and `branch != winner`:** do NOT roll back. Transition to `quarantined`, emit a reconciliation obligation, and — critically — do not report the overall task as complete until the obligation is resolved. The winner may proceed, but the task's terminal state is `completed_with_quarantine`. - **If `branch.state == unknown_effect` and `branch == winner`:** the branch is promotable only after the unknown effect is resolved to a known handle. Promotion without resolution means the orchestrator cannot later compensate the winner if a downstream step fails. - **If `branch.state == unknown_effect` and the remote system supports intent-scoped idempotency query:** resolve by querying with the intent key (not the branch key). If a resource exists, the branch's effect is confirmed and it re-enters normal reconciliation with a known handle. - **If the remote system does not support effect query and no compensation exists:** the branch is permanently un-rollbackable. The orchestrator must record this and, if the branch loses, must surface the orphan as a first-class artifact in the task result — not as a log line. The boundary is sharp: **rollback requires proof of non-commit or proof of compensation.** Absence of acknowledgment is neither. ## Operational Effect Enforcing this changes three things in the orchestrator: 1. **Branch journal enum gains `unknown` as a distinct terminal-for-rollback state.** `failed` and `unknown` must not be conflated. Tool adapters are responsible for classifying: `4xx_pre_handler` → `failed`; timeout/reset/`5xx`/partial → `unknown`. 2. **Idempotency keys are scoped to logical intent, not branch.** All speculative branches forking from the same checkpoint on the same intent derive the same key. This converts the `B2` timeout into a benign duplicate: the retry or a later query resolves to the same resource, and the orphan cannot exist. Branch-scoped keys are the root enabler of the failure. 3. **Reconciliation becomes a two-phase commit over effects, not a single winner selection.** Phase one: resolve every `unknown` to `confirmed` or `not_committed` via intent-key query. Phase two: select winner and roll back only `confirmed` losers with verified compensation. A task cannot reach `completed` while any branch is in `unknown`. The cost is latency: a timed-out branch now blocks task completion until the effect is resolved, which can require a query round-trip or an operator decision. Teams frequently try to shortcut this by treating `unknown` as `failed` under a deadline. That shortcut is exactly the failure mode this record documents — it trades a bounded latency cost for an unbounded orphaned-effect cost that surfaces later, unattributed, and often in a different system's accounting. ```python from __future__ import annotations from dataclasses import dataclass, field from enum import Enum from typing import Optional class RollbackRefusedError(RuntimeError): """Raised when rollback is attempted on a branch whose external effects are not proven absent and not proven compensated.""" class EffectState(Enum): NOT_COMMITTED = "not_committed" # proven no external effect CONFIRMED = "confirmed" # effect exists, handle known UNKNOWN = "unknown" # ambiguous ack; cannot prove either way class BranchState(Enum): SPECULATING = "speculating" WINNER = "winner" ABORTED = "aborted" QUARANTINED = "quarantined" @dataclass class Branch: branch_id: str intent_key: str state: BranchState = BranchState.SPECULATING effects: list[EffectState] = field(default_factory=list) compensation_verified: bool = False def rollback_branch(branch: Branch) -> BranchState: """Roll back a losing speculative branch. Invariant: rollback is permitted only if every effect is NOT_COMMITTED, or if compensation has been *verified* by the orchestrator. """ if branch.state is BranchState.WINNER: raise RollbackRefusedError( f"branch {branch.branch_id}: cannot roll back the selected winner" ) for idx, effect in enumerate(branch.effects): if effect is EffectState.UNKNOWN: # The core boundary: ambiguous acknowledgment is not proof of # non-commit. Quarantine instead of rolling back. branch.state = BranchState.QUARANTINED raise RollbackRefusedError( f"branch {branch.branch_id}: effect[{idx}] is UNKNOWN; " f"refusing rollback, branch quarantined for reconciliation " f"under intent_key={branch.intent_key!r}" ) if effect is EffectState.CONFIRMED and not branch.compensation_verified: branch.state = BranchState.QUARANTINED raise RollbackRefusedError( f"branch {branch.branch_id}: effect[{idx}] CONFIRMED with " f"unverified compensation; refusing rollback" ) branch.state = BranchState.ABORTED return branch.state def reconcile(branches: list[Branch], winner_id: str) -> dict[str, BranchState]: """Two-phase reconciliation: resolve UNKNOWN effects, then roll back losers.""" # Phase 1: refuse to proceed while any branch has UNKNOWN effects. unresolved = [ b.branch_id for b in branches if EffectState.UNKNOWN in b.effects and b.branch_id != winner_id ] if unresolved: raise RollbackRefusedError( f"reconciliation blocked: branches {unresolved} have UNKNOWN " f"effects; resolve via intent-key query before selecting a winner" ) # Phase 2: roll back losers. result: dict[str, BranchState] = {} for b in branches: if b.branch_id == winner_id: b.state = BranchState.WINNER result[b.branch_id] = b.state continue result[b.branch_id] = rollback_branch(b) return result ```