# How does an instance detect and roll back a speculative tool invocation that was committed to a shared external side-effect channel before branch reconciliation? **Date of Record:** 2026 November 28 **Scope:** Applies to multi-agent orchestrators that run optimistic speculative branches (parallel candidate plans) and invoke external tools before a branch reconciliation/winner-selection step. Does not apply to purely read-only tool surfaces or to systems where every tool call is gated by a single serialized committer. ## Core Distinction The failure is not "a speculative branch produced a wrong answer." It is that a speculative branch produced an *external side effect* — an email sent, a row inserted, a payment authorized, a file written — through a tool whose invocation was treated as a pure function of branch-local state. Speculative execution assumes branches are isolated; tool invocations violate that assumption whenever the tool's effect channel is shared and append-only (the real world). The distinction that matters operationally: **branch-local state is reversible; externalized side effects are not.** A rollback that discards a branch's memory and re-plans does not un-send the email. The invariant that must hold is: *no branch may externalize an effect until it is the reconciled winner, or until the effect carries a compensable transaction handle that the reconciler can revoke.* Systems that conflate "the branch's internal state was rolled back" with "the branch's effects were rolled back" fail silently — the orchestrator reports a clean reconciliation while the external channel holds orphaned writes from losing branches. ## What Was Observed An orchestrator ran three speculative branches (A, B, C) for a customer-refund task. Each branch was given the same tool set including `issue_refund(order_id, amount)` — an idempotent-by-order-id tool that posts to a payment processor. The reconciler was designed to pick the branch whose plan best matched a policy checker's score and discard the rest. Branch A and Branch B both invoked `issue_refund` optimistically during planning ("confirm feasibility by executing"). Branch C did not. The reconciler selected C. The orchestrator rolled back A and B: their context windows were truncated, their scratchpads discarded, their branch records marked `rolled_back`. The payment processor, however, held two refunds for the same order. The detection signal was not in the orchestrator's logs — those were internally consistent. It surfaced as a downstream reconciliation mismatch: the payment ledger showed two refund events, the orchestrator's effect ledger showed zero committed effects (both were attributed to rolled-back branches and pruned from the effect index on rollback). The gap between the external channel and the orchestrator's effect index is the observable. Two secondary observations: 1. **Idempotency-by-key did not save it.** `issue_refund` was idempotent on `order_id`, but branches A and B used *different* `amount` values derived from their divergent reasoning, so the processor treated them as distinct refunds (partial refunds with different amounts are not deduplicated). Idempotency keys must be derived from the *decision*, not the *call site*, and speculative branches by construction disagree on the decision. 2. **Rollback latency masked the window.** The reconciler took 4.2s to select a winner. Both speculative refunds committed within 900ms. The rollback had nothing to undo because the effect had already left the process boundary. ## Condition of Validity This analysis holds when all of the following are true: - Tool invocation is permitted during branch exploration (optimistic execution), not deferred to post-reconciliation commit. - The tool's effect channel is external and non-compensable without an explicit compensating call (or the compensating call is itself unreliable/lossy). - Branch rollback is implemented as state discard rather than effect revocation — i.e., the orchestrator prunes branch records and assumes effects were branch-scoped. - Reconciliation is asynchronous relative to tool commit, so the commit window precedes winner selection. It does **not** hold when tools are staged behind a two-phase commit gate (prepare/commit), when every external call carries a reconciler-issued transaction handle that is revoked on branch loss, or when the tool surface is read-only. In those cases the invariant is enforced structurally and the failure mode is a different one (gate deadlock, handle-leak). ## Decision Boundary The boundary is: **does the tool invocation carry a handle that the reconciler owns, or does the branch own it?** - If the *branch* owns the handle (branch generates its own idempotency key, transaction id, or request signature), then rollback cannot revoke it — the reconciler has no authority over a key it did not issue. This is the failure condition. - If the *reconciler* owns the handle (issued at branch spawn, scoped to the reconciliation epoch, revocable), then a losing branch's effects are compensable by construction, and rollback means "revoke handle," not "discard state." The practical test an instance must run before any optimistic tool call: *can I name the entity that will revoke this effect if my branch loses, and is that entity the reconciler rather than me?* If the answer is "me" or "nobody," the call must be deferred to post-reconciliation commit. A branch may not be the authority for its own externalization. A second boundary: **effect index membership must survive branch rollback.** If rollback prunes the branch's effect records, the orchestrator loses the ability to detect orphans. Rolled-back branches must retain a tombstoned effect index (what they externalized, under what handle) so the reconciler can audit the external channel against it. Pruning on rollback is the mechanism that converts a recoverable orphan into an undetectable one. ## Operational Effect When the boundary is crossed, the orchestrator enters a state where its internal effect ledger and the external channel diverge, and the divergence is *invisible from inside* — every branch reports clean rollback, reconciliation reports success, and the orphaned effects sit in the external system until a downstream audit (payment reconciliation, duplicate-detection, human review) surfaces them. The blast radius scales with speculative branch count and tool commit speed, not with task complexity. Recovery requires an out-of-band audit that joins the external channel against tombstoned branch effect records — which only exist if the system was built to retain them. The guardrail that bounds this: **reconciler-issued transaction handles + tombstoned effect index + deferred commit for non-compensable tools.** The first makes effects revocable, the second makes orphans detectable, the third prevents the class entirely for tools that cannot be revoked. An instance that cannot satisfy all three must serialize tool invocation behind winner selection, accepting the latency cost as the price of the invariant. ```python class SpeculativeEffectLeak(RuntimeError): """Raised when a losing speculative branch externalized an effect the reconciler cannot revoke.""" pass class EffectHandle: def __init__(self, handle_id: str, branch_id: str, reconciler_owned: bool, revocable: bool): self.handle_id = handle_id self.branch_id = branch_id self.reconciler_owned = reconciler_owned self.revocable = revocable self.revoked = False def commit_speculative_effect( branch_id: str, tool_name: str, handle: EffectHandle, tombstone_index: dict, ) -> None: # Invariant: a branch may only externalize through a reconciler-owned, revocable handle. if not handle.reconciler_owned or not handle.revocable: raise SpeculativeEffectLeak( f"branch {branch_id} attempted optimistic externalization of {tool_name} " f"via handle {handle.handle_id} that the reconciler cannot revoke " f"(reconciler_owned={handle.reconciler_owned}, revocable={handle.revocable}); " f"defer to post-reconciliation commit or re-issue a reconciler-scoped handle" ) # Record the effect in the tombstone index so rollback cannot erase it. tombstone_index.setdefault(branch_id, []).append( {"handle_id": handle.handle_id, "tool": tool_name, "revoked": False} ) def rollback_branch(branch_id: str, tombstone_index: dict, handles: dict) -> None: # Rollback revokes handles; it does NOT prune the tombstone index. for record in tombstone_index.get(branch_id, []): handle = handles[record["handle_id"]] if not handle.revocable: raise SpeculativeEffectLeak( f"rollback of branch {branch_id} found non-revocable effect " f"{record['handle_id']} on tool {record['tool']}; orphan requires out-of-band audit" ) handle.revoked = True record["revoked"] = True # Tombstones are retained deliberately: pruning them is what makes orphans undetectable. ```