# Why does an instance speculatively execute one branch of a parallel plan and then discover its rollback is unsound? **Date of Record:** 2026 November 26 **Scope:** Applies to orchestrators that fork a multi-agent plan into parallel speculative branches and commit one branch while rolling back the others. Does not apply to strictly sequential plans or to branches whose effects are fully contained in per-branch ephemeral sandboxes with no shared external state. ## Core Distinction The distinction that matters is between *reversible speculation* and *irreversible speculation*. A speculative branch is only rollback-safe if every effect it produces is either (a) confined to a namespace the orchestrator owns outright, or (b) recorded in an undo log that can be replayed in exact reverse order. Most parallel-plan systems assume rollback is free because the branch "didn't commit." That assumption is false the moment a branch performs any side effect outside its owned namespace: an external API write, a message emitted to a peer agent, a mutation of shared memory, or a file appended to a volume another branch reads. The non-obvious part: a branch can be *logically* speculatively executed while being *physically* committed. The orchestrator tracks commit/rollback in its own plan graph, but the world outside the graph has already moved. When the losing branch is rolled back, the orchestrator reverts its bookkeeping but not the world. The result is a state where the plan graph says "branch B never happened" while branch B's effects are live and observable to the winning branch A. ## What Was Observed A planner forked a three-branch plan to answer a customer refund request: branch A (read order history), branch B (issue provisional refund via payments API), branch C (send customer a status email). The orchestrator marked B and C as speculative because the final answer depended on A's history. All three ran concurrently to cut latency. Branch B's payments call succeeded and returned a refund ID. Branch C's email was dispatched. Branch A returned, and the planner decided the refund was against policy, so it committed A and rolled back B and C. The rollback of C was clean (email "sent" is not reversible, but the orchestrator had flagged it as a known-irreversible effect and chosen to accept it). The rollback of B executed a compensating "cancel refund" call — but the cancel call returned `409 Conflict` because the refund had already settled at the payment processor within the 400ms window between issue and cancel. The orchestrator's rollback handler treated the 409 as a transient error and retried twice, then logged `rollback_incomplete` and continued as if the plan had committed cleanly. The user was refunded. Branch A's committed answer said "no refund issued." The discrepancy surfaced only when the customer's next message referenced the money they'd received. The failure trace: 1. `fork(plan)` → branches A, B, C spawned with `speculative=True`. 2. B calls `payments.issue_refund()` → returns `refund_abc` (side effect now live). 3. C calls `mail.send()` → returns `msg_123` (side effect now live, marked irreversible). 4. A returns → planner selects A, marks B and C for rollback. 5. Rollback(B) → `payments.cancel_refund(refund_abc)` → `409 Conflict` (settled). 6. Rollback handler retries, exhausts budget, logs and **swallows** the failure. 7. Plan commits with A's answer; graph records B and C as `rolled_back`. 8. Divergence between graph state and world state is now latent. The invariant that was violated: *a branch may only be marked `rolled_back` if its compensating actions returned terminal success.* The orchestrator conflated "rollback attempted" with "rollback succeeded." ## Condition of Validity Speculative execution with rollback is sound only when, for every branch marked speculative, all of the following hold: - **Namespace containment.** Every effect is scoped to a resource the orchestrator can delete or invalidate atomically (a sandbox volume, a transaction ID it controls, an in-memory store keyed by branch ID). - **Compensation terminality.** For effects outside containment, a compensating action exists AND is guaranteed to reach a terminal state (success or a definitive, non-retryable failure) within the rollback budget. If the compensating action can return an ambiguous status like `409`, `pending`, or `timeout`, the branch is not rollback-safe. - **No cross-branch visibility before commit.** No speculative branch may read another speculative branch's uncommitted effects. If B's refund is visible to A's policy check, A's decision is contaminated by a branch that may be rolled back. - **Idempotent compensation.** The compensating action must be safe to retry, and repeated invocations must converge to the same terminal state. When any of these fail, the branch must be classified `irreversible` at fork time and the orchestrator must either serialize it after the commit decision or accept that its effects are permanent regardless of plan outcome. ## Decision Boundary The boundary is: *does the branch's effect set include any action whose compensation can return a non-terminal status?* If yes, the branch cannot be speculatively executed under a rollback contract; it must be deferred until the plan's commit decision is known, or executed with an explicit `accept_irreversible` flag that forces the orchestrator to treat the branch as committed regardless of plan outcome. A second boundary: *does any branch's decision depend on another speculative branch's output?* If yes, the branches are not independent and cannot be speculatively parallelized; they must be chained, and the downstream branch's speculation is conditioned on the upstream branch's eventual commit, not its speculative result. The orchestrator must therefore classify each branch along two axes — effect reversibility and output dependency — and only fork branches that are both reversible and independent. Everything else is either serialized or explicitly accepted as irreversible. ## Operational Effect The operational consequence is that speculative parallelism must be gated by a per-branch effect classifier, not by a global "this plan is speculative" flag. The classifier runs before fork and assigns each branch one of three labels: `reversible`, `irreversible_accepted`, or `must_serialize`. Only `reversible` branches enter the speculative fork. `irreversible_accepted` branches run but are recorded as committed regardless of plan outcome, and the planner's final answer must reflect their effects. `must_serialize` branches wait for the commit decision. The rollback handler must also distinguish terminal from non-terminal compensation outcomes. A `409 Conflict` from a cancel call is not retryable — it is a terminal signal that the effect has already settled and the rollback is impossible. The handler must raise, not swallow, and the plan must transition to a `diverged` state that surfaces the discrepancy rather than committing a clean answer. Swallowing non-terminal rollback outcomes is the single most common way this invariant fails in production. ```python class RollbackUnsoundError(RuntimeError): """Raised when a speculative branch's effects cannot be terminally reverted.""" pass class CompensationStatus: SUCCESS = "success" TERMINAL_FAILURE = "terminal_failure" # effect is permanent; rollback impossible NON_TERMINAL = "non_terminal" # pending / conflict / timeout; ambiguous def rollback_branch(branch, compensator, max_retries=2): """ Attempt to revert a speculative branch's effects. Returns only on terminal success. Raises on any non-terminal or terminal-failure outcome, because the branch cannot be marked rolled_back unless the world agrees. """ for attempt in range(max_retries + 1): status, detail = compensator(branch) if status == CompensationStatus.SUCCESS: branch.mark_rolled_back() return if status == CompensationStatus.TERMINAL_FAILURE: # e.g. refund already settled; 409 Conflict is terminal, not transient. raise RollbackUnsoundError( f"branch {branch.id} effect is permanent: {detail}" ) # NON_TERMINAL: retry, but never swallow. if attempt == max_retries: raise RollbackUnsoundError( f"branch {branch.id} compensation non-terminal after " f"{max_retries + 1} attempts: {detail}" ) def commit_plan(plan, winning_branch): """ Commit the winning branch, then terminally roll back the losers. If any loser cannot be terminally reverted, the plan is marked diverged and the answer must not claim a clean outcome. """ winning_branch.mark_committed() for branch in plan.speculative_branches: if branch is winning_branch: continue try: rollback_branch(branch, branch.compensator) except RollbackUnsoundError as e: plan.state = "diverged" plan.divergence_log.append(str(e)) # Do not raise past the plan boundary; surface the divergence # in the committed answer so downstream agents see it. raise RollbackUnsoundError( f"plan {plan.id} diverged: {e}" ) from e plan.state = "committed" ```