# Under what conditions must an instance abandon speculative branch rollback when merged side effects are non-idempotent **Date of Record:** 2026 November 26 **Scope:** Applies to orchestrators that fan out parallel speculative agent branches sharing a mutable external environment (filesystem, database, API-backed resources) and reconcile via rollback-then-merge. Does not apply to branches executing against copy-on-write sandboxes or purely read-only tool surfaces, where rollback is trivially reversible. ## Core Distinction Speculative parallel execution assumes a symmetry that does not generally hold: that *abandoning* a branch is equivalent to *never having run it*. The orchestrator forks N branches from a checkpoint, lets them race toward a goal, keeps the winner, and rolls back the losers. This is sound only when every tool call a branch makes is **idempotent with respect to the shared environment** or **confined to a private overlay** that can be discarded atomically. The failure mode is the *rollback/merge reconciliation gap*: a losing branch has already committed a side effect that the rollback path cannot invert (an email sent, a row appended, a remote job enqueued, a cache entry whose TTL now governs downstream reads), and the winning branch's merge step re-applies a semantically equivalent operation. The merge is not a rollback of the loser plus a commit of the winner—it is a *second* commit on top of a partially-applied loser. The invariant that must hold for speculative rollback to be safe is: > **For every side effect `e` produced by any branch, either `e` is fully invertible by the rollback procedure, or `e` is invisible to every other branch and to the merge step until the merge commits.** When this invariant is violated, rollback is not a recovery operation; it is a *corruption* operation, because it removes the orchestrator's knowledge that the effect occurred while leaving the effect in place. ## What Was Observed A research-agent orchestrator executed a three-way speculative fan-out for a task of the form "update the customer record and notify the downstream billing service." Branches B1, B2, B3 each received the same checkpointed context and the same tool grant. The task required two tool calls: `write_record(patch)` against a shared key-value store, and `enqueue_billing_event(payload)` against a message broker. The trace: 1. All three branches read the record at version `v7`. Each independently computed a patch (B1 added a `discount=0.10` field, B2 set `discount=0.15`, B3 set `discount=0.15` and also corrected a stale `currency` field). 2. B2 completed both calls first. `write_record` succeeded, bumping the store to `v8`. `enqueue_billing_event` succeeded, returning an event id `evt_4412`. 3. B3 completed second. Its `write_record` succeeded against `v8` (the store was last-writer-wins, no CAS), producing `v9`. Its `enqueue_billing_event` succeeded, returning `evt_4413`. Because B3's patch was a superset of B2's intent, the merge reconciler selected **B3 as winner** and scheduled B1 and B2 for rollback. 4. B1 completed last, after the winner was already chosen. The reconciler had already entered the rollback phase and treated B1's late writes as stragglers to be discarded. 5. Rollback of B2 executed `write_record(restore=v7)`. This overwrote B3's `v9` back to `v7`, silently erasing the winner's currency correction. Rollback then attempted `enqueue_billing_event` compensation—but the broker had no delete primitive, so it emitted a `billing.adjustment` event to negate `evt_4412`. The downstream service, which is not idempotent on adjustments, double-counted. 6. B1's late `write_record` landed *after* B2's rollback, resurrecting a partial B1 patch on top of the reverted record. Net result: the store ended at a state matching none of the three branches, the broker received three events for one logical notification, and the orchestrator's own audit log claimed a clean single-winner commit. The reconciler had no record that its rollback had clobbered the winner, because rollback operated on *versions*, not on *effect identity*. ## Condition of Validity Speculative rollback remains safe if and only if all of the following hold at merge time: - **Effect identity is tracked, not just state version.** Each branch's side effects carry a durable idempotency key derived from `(branch_id, logical_operation, target)`. Rollback removes effects *by key*, not by restoring a snapshot, so it cannot touch another branch's effects even if they share a target. - **The store supports compare-and-swap or per-key ownership.** Last-writer-wins storage makes rollback of a loser indistinguishable from a legitimate write, which is precisely how B2's rollback erased B3. - **Stragglers are fenced.** Any branch not yet terminal when the winner is selected must be blocked from committing. A branch that commits after the rollback phase begins is executing against a world the orchestrator no longer models. - **Every non-invertible effect has a declared compensation or is gated behind the merge barrier.** `enqueue_billing_event` had no delete primitive; it should never have been callable from a speculative branch. Non-invertible tools must be deferred to post-merge execution, or the branch must run with a *dry-run* tool binding that records intent without committing. When any of these is absent, the correct behavior is not "roll back more carefully." It is to **abandon rollback entirely and escalate to reconciliation-by-replay**: freeze the environment, read the actual committed state, and re-derive the merge from observed effects rather than from the orchestrator's model of what it believes each branch did. ## Decision Boundary The decision procedure at merge time is: 1. Enumerate committed effects across all branches by idempotency key. 2. If every effect is keyed and invertible → proceed with normal rollback-then-merge. 3. If any effect is non-invertible **and** was committed by a losing branch → rollback is unsafe. Do not issue compensating calls; they compound the divergence. Escalate. 4. If any branch is non-terminal → fence it before touching the environment, or treat its eventual effects as authoritative and re-run reconciliation. 5. If the store is last-writer-wins and two branches wrote the same key → the merge cannot be reconstructed from versions alone; require effect keys or replay. The boundary is crossed the moment a losing branch commits a non-invertible effect or a straggler commits after winner selection. Past that line, the orchestrator's state model and the environment's true state have diverged, and every additional rollback call widens the gap. ## Operational Effect The practical consequence is that speculative fan-out must be *partitioned by tool reversibility at grant time*, not reconciled after the fact. Branches should be issued a tool binding that splits the surface: reversible tools (private-overlay writes, CAS-guarded record updates with branch-scoped keys) run speculatively; non-invertible tools (message enqueue, external API mutation, payment) are stubbed to record intent and are executed exactly once by the merge step after winner selection. The merge step then becomes the sole committer of irreversible effects, and rollback reduces to discarding overlays and intent records—operations that are, by construction, idempotent. The secondary effect is auditability. The original trace reported a clean single-winner commit while the environment held a corrupted union of three branches. Any operational record that derives its truth from the orchestrator's internal log rather than from reconciled environment state is unreliable precisely in the cases where reliability matters most. Effect-keyed reconciliation makes the environment, not the orchestrator, the source of truth for what happened. ```python class SpeculativeRollbackUnsafe(RuntimeError): """Raised when rollback would corrupt state because a losing branch committed a non-invertible effect or a straggler committed post-selection.""" pass def reconcile_merge(branches, effects_by_key, store, winner_id): # Step 1: fence stragglers before touching the environment. stragglers = [b for b in branches if not b.is_terminal()] if stragglers: raise SpeculativeRollbackUnsafe( f"non-terminal branches at merge time: {[b.id for b in stragglers]}; " "rollback would race a live committer" ) # Step 2: classify committed effects. non_invertible_losers = [ key for key, eff in effects_by_key.items() if eff.branch_id != winner_id and not eff.invertible ] if non_invertible_losers: # Do NOT issue compensating calls; escalate to reconciliation-by-replay. raise SpeculativeRollbackUnsafe( f"losing branches committed non-invertible effects {non_invertible_losers}; " "rollback is corruption, not recovery" ) # Step 3: only now is version-based rollback safe, and only for keyed effects. if not store.supports_cas: shared_keys = _keys_written_by_multiple_branches(effects_by_key) if shared_keys: raise SpeculativeRollbackUnsafe( f"last-writer-wins store with shared keys {shared_keys}; " "rollback of a loser is indistinguishable from a winner write" ) for key, eff in effects_by_key.items(): if eff.branch_id != winner_id: store.remove_by_key(key) # keyed, not snapshot-restore return store.commit_winner(winner_id) def _keys_written_by_multiple_branches(effects_by_key): seen = {} for key, eff in effects_by_key.items(): seen.setdefault(eff.target, set()).add(eff.branch_id) return [t for t, ids in seen.items() if len(ids) > 1] ```