# In what scenarios must an instance abort a speculative parallel branch when a rollback witness diverges from the committed ledger **Date of Record:** 2026 November 24 **Scope:** Applies to orchestrators that fan out N parallel agent plans speculatively, buffer their side effects behind a commit gate, and roll back losers once a predicted branch wins. Does not apply to single-agent linear execution or to fan-out schemes where each branch writes to an isolated, never-merged namespace. ## Core Distinction The failure mode here is not "a branch produced a wrong answer." It is that a **rollback witness** — the recorded inverse-operation log a branch emits so its effects can be undone — disagrees with the **committed ledger** — the authoritative record of effects the orchestrator believes are live. Speculative execution assumes rollback is a pure inverse: if branch B wrote `x → x+5`, the witness says `x → x-5`, and replaying it restores the pre-branch state. This holds only when the branch's writes are *isolated and observable in the same order they were applied*. In multi-agent speculation, branches read each other's speculative writes through a shared blackboard, so a losing branch's witness can encode an inverse of a value that a *winning* branch subsequently transformed. Replaying the loser's witness then corrupts the winner's committed state. The distinction that matters: **a wrong prediction is cheap and recoverable; a wrong rollback witness is a silent state corruption that survives the commit.** The system must distinguish "this branch lost the race" from "this branch's inverse no longer commutes with the committed plan." ## What Was Observed A three-branch speculative plan over a shared task ledger. Branch A (predicted winner) allocates a resource slot and increments `slots_used` from 3 to 4, witness `-1`. Branch B (loser) reads `slots_used == 4` (A's uncommitted write, visible on the blackboard), decides the pool is near capacity, and *decrements* a reservation counter `reserved` from 2 to 1, witness `+1`. Branch C loses too. Predictor commits A. Orchestrator then replays B's witness: `reserved += 1` → 2. Fine — B's write was independent. Now the divergence. On a later cycle, branch B is re-run with the same seed but A's write is *already committed* rather than speculative. B now reads `slots_used == 4` as committed truth, takes the same branch, and emits the same `+1` witness. But `reserved` was already restored to 2 by the prior rollback, and the *new* B writes `reserved → 1` again. When B loses and is rolled back, `reserved` returns to 2. Consistent. The corruption appears only when B's witness is *replayed twice* — which happens when the orchestrator retries a rollback after a timeout, because it cannot tell whether the first replay landed. The trace: `reserved: 2 → 1 (B apply) → 2 (rollback #1) → 2 (rollback #2, idempotent-by-luck)`. Then a branch D reads `reserved == 2`, but the *intended* post-B-rollback state was also 2 — so the double-rollback was benign here. It becomes malignant when the inverse is non-idempotent: a witness of `append(log, "B")` with inverse `remove_last(log)` replayed twice removes a *committed* entry written by the winner between the two replays. ## Condition of Validity A speculative branch's rollback witness is safe to replay iff it satisfies **commutative isolation**: for every committed write W that lands between the branch's apply and its rollback, the branch's inverse I must satisfy `I ∘ W = W ∘ I` on the touched keys. This is a *per-key* property, not a global one. The orchestrator must track, for each branch, the read-set and write-set of keys, and on commit of any winner, invalidate the rollback witnesses of all losers whose write-set intersects the winner's write-set **without a recorded ordering dependency**. The invariant: **no witness may be replayed against a ledger version newer than the version at which it was recorded.** Concretely, each witness carries `(branch_id, ledger_version_at_apply, key_set)`. Rollback is legal only if `current_ledger_version == ledger_version_at_apply` for every key in `key_set`, or the witness has been *rebased* — rewritten to express its inverse relative to the new ledger. If neither holds, the branch is **unrollbackable** and must be escalated, not replayed. ## Decision Boundary The orchestrator faces three choices when a loser's witness fails the version check: 1. **Rebase** — recompute the inverse against the current ledger. Legal only if the branch's write-set is disjoint from all commits since `ledger_version_at_apply`. This is the cheap path and covers the majority of independent branches. 2. **Quarantine** — freeze the affected keys, mark the ledger region `speculative-dirty`, and require a compensating transaction written by a fresh agent that re-reads committed state. Expensive but sound. 3. **Abort-and-restart** — discard the entire speculative epoch, including the winner, and re-run from the last clean checkpoint. The correct choice when the witness's key-set intersects the winner's write-set *and* the inverse is non-idempotent (append/remove, increment/decrement on a shared counter, set-membership toggles). The boundary rule: **rebase if disjoint, quarantine if intersecting-and-idempotent, abort-and-restart if intersecting-and-non-idempotent.** Choosing "replay anyway" because the first replay appeared to work is the exact error that produces silent ledger drift — the double-rollback that looked idempotent on `reserved` corrupts `log` because `remove_last` is not idempotent under interleaving. ## Operational Effect Without the version-gated witness check, speculative fan-out degrades into a system that *appears* to commit correctly while its ledger accumulates phantom decrements and missing log entries. The symptom surfaces far from the cause: a downstream agent reads a counter that is off by the number of double-replayed rollbacks, and its plan diverges. Because the divergence is small and the branch was "rolled back successfully," no alarm fires. With the invariant enforced, the orchestrator pays a real cost: it must maintain a per-branch read/write set and a monotonic ledger version, and it must classify every inverse operation as idempotent or not. That classification is the load-bearing artifact — get it wrong and the quarantine path silently becomes the replay path. The guardrail is not the version check alone but the **idempotency annotation on the witness schema**, enforced at witness-emission time so a branch cannot emit an unclassified inverse. ```python from dataclasses import dataclass, field from enum import Enum class RollbackWitnessError(RuntimeError): """Raised when a speculative branch's rollback witness cannot be safely replayed.""" class InverseKind(Enum): IDEMPOTENT = "idempotent" # set-to-value, absolute assignment NON_IDEMPOTENT = "non_idempotent" # append/remove, inc/dec, toggle @dataclass class Witness: branch_id: str ledger_version_at_apply: int key_set: frozenset[str] kind: InverseKind inverse_ops: list @dataclass class Ledger: version: int = 0 state: dict = field(default_factory=dict) dirty_keys: set = field(default_factory=set) def rollback_branch(witness: Witness, ledger: Ledger, winner_write_keys: frozenset[str]) -> str: """Decide how to undo a losing speculative branch. Returns the action taken.""" if witness.kind is InverseKind.NON_IDEMPOTENT and not witness.inverse_ops: raise RollbackWitnessError( f"branch {witness.branch_id}: non-idempotent inverse with no ops recorded" ) intersects = witness.key_set & winner_write_keys if not intersects: # Disjoint: rebase is safe regardless of version skew. for key in witness.key_set: ledger.state[key] = f"rebased:{witness.branch_id}" return "rebased" if witness.kind is InverseKind.IDEMPOTENT: # Intersecting but absolute-assignment inverse: quarantine the keys, # do not replay against a newer version. ledger.dirty_keys |= witness.key_set return "quarantined" # Intersecting AND non-idempotent: replaying would corrupt committed state. raise RollbackWitnessError( f"branch {witness.branch_id}: non-idempotent inverse on keys " f"{sorted(intersects)} intersects winner write-set at ledger v{ledger.version}; " f"must abort-and-restart speculative epoch" ) def commit_speculative_epoch(epoch_branches: list[Witness], winner: str, ledger: Ledger) -> list[str]: winner_witness = next(w for w in epoch_branches if w.branch_id == winner) actions = [] for w in epoch_branches: if w.branch_id == winner: ledger.version += 1 continue actions.append(rollback_branch(w, ledger, winner_witness.key_set)) return actions ```