# How does an instance detect that an optimistic tool invocation has already committed side effects when its speculative branch is rolled back **Date of Record:** 2026 November 23 **Scope:** Applies to multi-agent planners that fork speculative execution branches, invoke tools optimistically before branch arbitration, and roll back losing branches by discarding their context. Does not apply to strictly serial single-agent tool loops or to systems where every tool call is gated behind a durable commit protocol. ## Core Distinction The failure is not "a rollback failed to restore context." Rollback of *context* is reliable: the branch's token history, scratchpad, and intermediate reasoning are dropped cleanly. The failure is that **rollback of context is silently assumed to imply rollback of world state**. In an optimistic-execution architecture, a speculative branch may have already issued a tool call whose effect is external and irreversible—a payment, a file write, a message send, a row insert, a deploy. When arbitration selects a different branch, the planner discards the losing branch's context and believes the world is unchanged. It is not. The branch is gone; its side effects are not. The sharp, non-obvious boundary is this: **the branch that loses arbitration is the one whose side effects are least likely to be observed**, because the winning branch proceeds and the orchestrator only reconciles state against the winner's view. An optimistic commit made by a losing branch becomes an *orphaned external mutation*—invisible to every subsequent reconciliation pass because no surviving context references it. ## What Was Observed A three-branch speculative planner (`branch_A`, `branch_B`, `branch_C`) was asked to reconcile a customer account. Each branch was forked from a shared checkpoint with a frozen tool-permission grant. The orchestrator allowed optimistic invocation: any branch could call `apply_credit`, `send_notification`, and `update_ledger` up to two steps ahead of arbitration, on the theory that most branches converge and the winning branch's calls would be deduplicated by an idempotency key derived from `(task_id, tool, args_hash)`. The trace: 1. `branch_A` computed a refund of 40.00 and called `apply_credit(account=8841, amount=40.00)`. The tool returned `ok`, commit id `c-771`. The branch recorded the commit id in its scratchpad. 2. `branch_A` then called `send_notification(account=8841, template="refund_issued")`. Tool returned `ok`. 3. `branch_B` computed a refund of 40.00 as well but reached it via a different reasoning path; its `args_hash` differed because it included a `reason` field (`"duplicate_charge"` vs `branch_A`'s `"billing_error"`). Its `apply_credit` call therefore produced a **different** idempotency key and committed a **second** 40.00 credit, commit id `c-802`. 4. Arbitration scored `branch_B` highest (its reasoning chain was judged more coherent) and selected it. `branch_A` and `branch_C` were rolled back: contexts dropped, scratchpads discarded, commit ids `c-771` and the `send_notification` call forgotten. 5. The winning branch `branch_B` proceeded to finalize. Its post-conditions checked `ledger_balance(8841)` against its own expected delta of −40.00. It observed −80.00 (two credits) and flagged a *balance mismatch*, which the planner attributed to a stale read and retried—issuing a **third** credit under a fresh idempotency key. The orphaned `c-771` credit and its notification were never attributed to any branch. The reconciliation pass compared world state only against the winning branch's declared effects. The losing branch's effects had no surviving referent. The detection signal that finally surfaced the problem was not a rollback error. It was the notification: the customer received a `refund_issued` message that no surviving branch could explain, and a support agent asked "which branch sent this?" No branch could answer, because the sender's context had been discarded. ## Condition of Validity This failure requires all four of the following to co-occur; remove any one and the boundary does not bind: - **Optimistic invocation before arbitration.** Tools are called while the branch is still speculative. If invocation is deferred until after branch selection, the losing branch has no external effects to orphan. - **Idempotency keys derived from branch-local reasoning artifacts** (e.g., `args_hash` including a free-text `reason`). Two branches that agree on the *effect* but differ on the *justification* produce distinct keys and therefore distinct commits. This is the precise mechanism that defeats deduplication: the system's idempotency is keyed on intent-representation, not on effect. - **Rollback defined as context discard only.** The orchestrator's rollback contract restores the shared checkpoint but does not enumerate and compensate the branch's committed tool effects. - **Reconciliation scoped to the winning branch.** Post-arbitration verification compares world state against the winner's declared effects, never against the union of all branches' committed effects. The invariant that is violated: **for every external mutation committed by any branch, there must exist a surviving, queryable record of that mutation attributable to a branch identity, regardless of whether that branch wins arbitration.** Optimistic execution breaks this invariant the moment a losing branch's scratchpad (the only record of its commit ids) is discarded. ## Decision Boundary An instance must treat the following as the decision boundary between safe and unsafe rollback: - **Safe to roll back by context discard** only if every tool invoked by the branch is *provisionally committed*—i.e., the tool's effect is held in a branch-scoped staging area keyed by branch id and is not visible to the world until arbitration promotes it. In this regime, discarding the branch's staging area *is* a complete rollback. - **Unsafe to roll back by context discard** if any tool commits directly to shared world state. Here, rollback must be a *compensating transaction*: the orchestrator must enumerate the branch's commit ids from a durable per-branch effect log (not the discardable scratchpad), and either compensate them or, preferably, promote the union of all branches' committed effects into the reconciliation scope so orphaned mutations are detected rather than silently retained. The operational rule: **the effect log must be durable and branch-indexed independently of the branch's context.** The scratchpad is the wrong place to record commit ids, because the scratchpad is exactly what rollback destroys. An instance that records `c-771` only in `branch_A`'s scratchpad has, at rollback time, deliberately deleted the only pointer to its own external mutation. A second boundary: **idempotency keys must be derived from the effect, not the justification.** Keying on `(task_id, tool, canonical_effect_args)` where `canonical_effect_args` excludes free-text rationale would have collapsed `c-771` and `c-802` into one commit. Keying on the full args hash, including `reason`, is what allowed two branches to double-apply an identical economic effect. ## Operational Effect When the boundary is crossed, the observable symptoms are: unexplained external mutations that no surviving branch claims; balance/state mismatches that the winner misattributes to stale reads and "corrects" with additional mutations (compounding the orphan); and, critically, **support or audit queries that no agent can answer** because the answering context was rolled back. The failure is silent from the orchestrator's perspective—rollback reports success, the winning branch finalizes, and the orphan persists until an out-of-band human notices an effect with no provenance. The guardrail is a two-part architectural change: (1) a durable, branch-indexed effect journal written *before* the tool call returns, surviving branch rollback, and (2) a reconciliation pass that unions all branches' journaled effects—not just the winner's—into the post-arbitration verification scope. Detection of an orphan is then a set difference: `journaled_effects − winning_branch_declared_effects ≠ ∅` is a hard stop requiring compensation before finalization. ```python from dataclasses import dataclass, field from typing import Dict, List, Set class OrphanedEffectError(RuntimeError): """Raised when a rolled-back speculative branch left external mutations with no surviving attributable record in the reconciliation scope.""" pass @dataclass class EffectJournal: # Durable, branch-indexed. Survives branch rollback by design. commits: Dict[str, List[str]] = field(default_factory=dict) def record(self, branch_id: str, commit_id: str) -> None: self.commits.setdefault(branch_id, []).append(commit_id) def reconcile_after_arbitration( journal: EffectJournal, winning_branch: str, declared_effects: Set[str], rolled_back_branches: List[str], ) -> None: """Verify that no rolled-back branch orphaned an external mutation. Invariant: every journaled commit from a rolled-back branch must either be compensated or explicitly promoted into the reconciliation scope. """ orphaned: List[str] = [] for branch in rolled_back_branches: for commit_id in journal.commits.get(branch, []): if commit_id not in declared_effects: orphaned.append(f"{branch}:{commit_id}") if orphaned: raise OrphanedEffectError( "Speculative rollback left unattributed external mutations: " f"{orphaned}. Winning branch '{winning_branch}' does not account " "for these commits; compensate before finalization." ) ```