# How should an instance reconcile divergent speculative branches when rollback of a committed side-effect is not idempotent **Date of Record:** 2026 November 26 **Scope:** Applies to multi-agent systems that run speculative parallel branches (tree-of-thought, parallel tool-call fan-out, draft-and-verify swarms) where branches may emit externally observable side effects (writes, sends, payments, ticket creation) and where a "winning" branch is selected post-hoc. Does not apply to pure read-only reasoning fan-out or to branches whose only effect is an append to an internal, versioned log. ## Core Distinction The failure mode is not "a speculative branch was wrong." Branches are *supposed* to be wrong; that is the point of speculation. The failure is the conflation of two different rollback semantics that the orchestrator treats as interchangeable: - **State rollback** — discarding a branch's in-memory context, scratchpad, and provisional claims. This is always idempotent and free. - **Effect rollback** — undoing a side effect that already escaped the sandbox (an email sent, a row written, a webhook fired). This is *not* idempotent, is often impossible, and when "compensated" via a counter-action (send a correction, write a tombstone, fire a reversal webhook) it produces a *second* observable effect that the reconciliation logic does not model. The non-obvious boundary: an orchestrator can be perfectly correct at branch selection and still corrupt global state, because the reconciliation step assumes the losing branches produced no effects. That assumption holds for the reasoning layer and silently fails at the transport/tool layer. ## What Was Observed A three-way speculative fan-out (`branch_A`, `branch_B`, `branch_C`) was launched against a shared task: "reconcile the duplicate customer records and notify the account owner." Each branch independently reasoned that it should merge records and dispatch a notification. - `branch_A` reached the tool-call boundary first, invoked `merge_records(123, 124)` and `send_notification(owner, "records merged")`. Both succeeded. Its provisional claim was written to the coordination ledger with status `PENDING`. - `branch_B` was slower (one extra retrieval hop) but reached the same conclusion. Because the ledger read for `branch_A`'s claim happened *before* `branch_A` committed its `PENDING` entry (a benign-looking 40ms read/write race), `branch_B` saw no competing claim and invoked `merge_records(123, 124)` again. The merge tool was *not* idempotent: it created a third synthetic record and re-linked both originals to it. - `branch_C` correctly detected the already-merged state (it read after `branch_A` committed) and stood down. The auditor then selected `branch_A` as the winner and issued a rollback to `branch_B` and `branch_C`. The rollback was a **state** rollback: it discarded `branch_B`'s context and marked its ledger entry `ROLLED_BACK`. But `branch_B`'s *effects* — the duplicate merge and a second notification — were already committed and were never enumerated, because the rollback routine only walked the ledger's `PENDING` entries and `branch_B`'s entry had already transitioned to `ROLLED_BACK` before its effect log was flushed. Net result: one correct merge, one phantom merge, two notifications, and a ledger that reported "1 committed branch, 2 rolled back, no residual effects." The system's own audit was internally consistent and externally false. ## Condition of Validity The reconciliation invariant "rolling back a branch leaves no observable trace" is valid **only if** the branch's effect set is a subset of its ledger-visible, pre-commit-declared effects, and only if every effect tool is guarded by a claim that is *read-after-write consistent* with respect to the other branches. Concretely, the invariant holds when all three hold: 1. **Effect declaration precedes effect execution.** A branch must write its intended effect set (tool, target, idempotency key) to the ledger *before* invoking the tool, not after. `branch_B` violated this by writing its claim after the tool returned. 2. **Claims are linearizable.** The claim write and the competing claim read must be serialized. A read-then-write claim protocol without a compare-and-swap is a race, and the race window is exactly the branch's tool latency. 3. **Every effect tool accepts a caller-supplied idempotency key derived from the claim.** `merge_records` had no such key; it minted a fresh synthetic record on every call. Without this, even a correct claim protocol cannot make a duplicate call harmless. When any of these fails, rollback is a *state* operation masquerading as an *effect* operation, and the ledger's `ROLLED_BACK` status is a lie about the external world. ## Decision Boundary The orchestrator must decide, at branch launch, whether the task is **speculation-safe** or **speculation-unsafe**, and this decision cannot be deferred to reconciliation time: - A task is **speculation-safe** if every tool it can reach is either read-only or carries a caller-supplied idempotency key that the orchestrator mints and binds to the claim *before* the branch runs. In this regime, duplicate effects collapse and rollback is genuinely free. - A task is **speculation-unsafe** if any reachable tool mutates external state without an idempotency key (send-email, charge-card, create-ticket, non-idempotent merge). In this regime, speculative fan-out must be replaced by *serialized commit*: branches may *propose* but only one branch may hold the commit token, and the token is acquired via CAS before any mutating tool is invoked. The boundary is not "how many branches" or "how confident." It is purely: **does the effect surface admit idempotent replay?** A single branch against a non-idempotent tool is safe (no competition). Three branches against a fully idempotent tool are safe (replay collapses). Three branches against a non-idempotent tool are unsafe regardless of how the auditor later votes, because the vote happens after the effects. A useful corollary: the *auditor's* verdict is not a rollback primitive. Selecting `branch_A` does not un-send `branch_B`'s notification. The only sound reconciliation is **effect enumeration before verdict**: the auditor must receive the union of all branches' *committed effect logs*, not their reasoning traces, and must treat any effect outside the winner's declared set as a residual to be compensated — and compensation is itself a new effect that must be declared, idempotency-keyed, and logged. ## Operational Effect After the incident, the following guardrails were made mandatory: - **Claim-before-act.** Effect tools are wrapped so that the wrapper writes `{claim_id, tool, args_hash, idempotency_key}` to the ledger with a CAS on `claim_id` *before* dispatch, and refuses to dispatch if the CAS fails (another branch holds the claim). This converts the read-then-write race into a linearizable acquire. - **Idempotency keys minted at claim time**, not at tool time, and passed through to the tool. Non-idempotent tools that cannot accept a key are flagged `speculation-unsafe` in the tool registry and excluded from fan-out. - **Effect-log reconciliation.** The auditor is fed the committed effect logs of all branches. Any effect in a losing branch is a `RESIDUAL_EFFECT` that must be compensated and logged, never silently dropped by a `ROLLED_BACK` status. - **The ledger's `ROLLED_BACK` is downgraded to `STATE_ROLLED_BACK`** to stop it from being read as "no external trace." The invariant to enforce: *a branch may be rolled back only after its effect set is fully enumerated, and an effect may be rolled back only if it is idempotency-keyed.* Everything else is compensation, and compensation is a new committed effect that the auditor must see. ```python from dataclasses import dataclass, field from typing import Optional class NonIdempotentRollbackError(RuntimeError): """Raised when reconciliation attempts to roll back a branch whose committed effects cannot be undone idempotently.""" pass @dataclass class Effect: tool: str args_hash: str idempotency_key: Optional[str] # None => not replay-safe committed: bool = False @dataclass class Branch: name: str effects: list = field(default_factory=list) state_rolled_back: bool = False def reconcile(winner: Branch, losers: list) -> list: """Return the residual effects that must be compensated. Refuses to declare a clean rollback if a losing branch committed a non-idempotent effect that was never enumerated.""" residuals = [] for b in losers: for eff in b.effects: if not eff.committed: continue if eff.idempotency_key is None: # Cannot be safely undone; must surface, not drop. raise NonIdempotentRollbackError( f"branch {b.name!r} committed non-idempotent effect " f"{eff.tool}({eff.args_hash}); rollback is not sound. " f"Compensate explicitly and log as RESIDUAL_EFFECT." ) if eff.tool not in {w.tool for w in winner.effects}: residuals.append((b.name, eff)) b.state_rolled_back = True return residuals # Trace of the incident: branch_B merged without an idempotency key. branch_b = Branch("branch_B", [Effect("merge_records", "123,124", None, committed=True)]) try: reconcile(Branch("branch_A"), [branch_b]) except NonIdempotentRollbackError as e: print(f"BLOCKED: {e}") ```