# What happens when an instance speculatively executes parallel agent branches and a rollback re-exposes a side effect the commit path already externalized **Date of Record:** 2026 November 24 **Scope:** Applies to orchestrators that fork N speculative agent branches from a shared checkpoint, run them concurrently against live tools, and reconcile via commit/rollback. Does not cover single-branch execution, read-only fan-out, or branches whose tool calls are fully sandboxed with no external side effects. ## Core Distinction The failure is not "a branch failed and we rolled back." It is the narrower case where **rollback is not the inverse of commit because a side effect escaped the speculative boundary before the reconciliation decision was made, and the commit path's compensating action and the rollback path's compensating action are not the same operation.** Speculative execution assumes a branch is a transaction: either its effects are made durable (commit) or erased (rollback). That assumption holds only if every effect is *interceptable* — buffered, journaled, or reversible — until the arbiter decides. The boundary condition appears when a branch performs an effect that is *externally observable and non-idempotent* (sends an email, charges a card, posts a message, mutates a shared third-party record) while still speculative. At that instant the branch is neither committed nor rolled back; it is *leaked*. The arbiter's later decision operates on a world that already moved. The non-obvious part: the leak is usually introduced by the branch that *wins*. Losers get rolled back and their leaks are treated as noise. The committed branch's leak is silently absorbed into "the commit happened," so the reconciliation logic never runs a compensation for it — and the rollback of the losing branches runs compensations against a shared resource that the winning branch already mutated. The two compensation paths collide. ## What Was Observed A three-way speculative fan-out over a shared ticketing backend. Each branch was instructed to "resolve the customer's issue," and the tool surface exposed `create_refund`, `send_customer_email`, and `update_ticket_status` as direct calls with no staging layer. The orchestrator forked branches A, B, C from the same checkpoint, ran them in parallel with a 90-second wall clock, and used a majority-vote arbiter over the branches' final claims. Trace: 1. All three branches read the ticket at `status=open`. Checkpoint captured. 2. Branch A called `create_refund(amount=40)` — the backend applied it immediately and returned `refund_id=RF-901`. A then called `send_customer_email` referencing RF-901. 3. Branch B called `create_refund(amount=40)` independently — the backend had no idempotency key, so it applied a *second* refund, `RF-902`. B also sent an email. 4. Branch C timed out mid-reasoning before any tool call. 5. Arbiter selected A (B's final claim contradicted A's on a secondary field; majority over the two completed branches was undefined, tie broken by earliest completion). 6. Orchestrator committed A and rolled back B and C. Rollback of B ran `reverse_refund(refund_id=RF-902)` and `send_customer_email(correction)`. Rollback of C ran nothing (no journaled effects). 7. Customer received two refunds and three emails (two originals, one correction). Finance reconciliation flagged a double credit. The `reverse_refund` on RF-902 succeeded but the customer had already been told "your refund of $40 is on the way" twice. The invariant that broke: **the arbiter assumed the branches' effects were disjoint until commit.** They were not disjoint — they targeted the same external resource with the same semantic intent, and the resource had no way to deduplicate because the branches generated distinct idempotency-less requests. Rollback of B was not the inverse of commit of A; it was a *third* mutation of the same resource. A second, subtler observation: the rollback of B emitted a customer-facing correction email. That email is itself a non-idempotent external effect generated by the rollback path. So the rollback was not effect-free — it leaked too, and its leak was not journaled against any branch, meaning a subsequent replay of the whole episode (for audit or for a retry) would re-emit the correction. ## Condition of Validity Commit/rollback reconciliation is sound only under a conjunction of three conditions, and the failure occurs precisely when any one is dropped: - **Effect interception:** every tool call from a speculative branch is buffered or journaled and not applied to the external world until the arbiter commits. If a tool applies immediately, the branch is leaking. - **Idempotency keys derived from branch identity, not from intent:** if two branches can produce semantically identical but byte-distinct requests, the backend cannot collapse them. The key must be `(episode_id, semantic_intent_hash)` so that A and B's refunds collide and dedupe, *not* `(branch_id, call_seq)` which guarantees they don't. - **Rollback is a pure discard, not a compensating mutation:** a rollback that issues a new external call (a reversal, a correction, a delete) is not a rollback; it is a commit of a negative effect. That negative effect must itself be journaled, idempotent, and ordered relative to the winning branch's effects. When all three hold, the arbiter operates on a frozen world and commit/rollback is a genuine transaction boundary. When any fails, the arbiter is deciding about a world that has already diverged from the checkpoint, and the decision is advisory rather than authoritative. ## Decision Boundary The orchestrator must classify every tool in the surface *before* forking, and the classification is the decision boundary: - **Staged tools** (interceptable, journaled, no external effect until commit): safe to call speculatively. Rollback = discard journal entry. - **Idempotent-external tools** (effect escapes, but the backend dedupes on a caller-supplied key): safe *only if* the key is derived from semantic intent, not branch identity. Rollback = no-op; the commit path owns the effect. - **Non-idempotent-external tools** (effect escapes, no dedupe, and the effect is observable to a third party): **must not be called speculatively.** The branch must emit an *intent* — a proposal — and the arbiter must execute the intent exactly once after commit. The failure above is the third class being called speculatively. The guardrail is not "make rollback smarter." It is "refuse to fork branches whose reachable tool set contains a class-3 tool without a proposal indirection." If a branch *might* reach a class-3 tool (and with probabilistic reasoning you cannot statically prove it won't), the fork must either (a) run serially, (b) run with the class-3 tool replaced by a proposal stub, or (c) run with a per-episode intent lock so that only one branch may hold the intent at a time and the others block. The boundary is sharp: the moment a branch's tool call crosses from journaled to applied-and-observable, the episode is no longer speculatively reconcilable. Any subsequent commit/rollback is a *repair* operation, not a *transaction*, and repair operations have different correctness criteria (they must be idempotent, ordered, and auditable) than transactions do. ## Operational Effect Downstream, three concrete consequences: 1. **The commit path inherits the loser's leak.** Because the winning branch's commit is treated as "the effect happened," the reconciliation logic does not run a dedupe check against the losers' already-applied effects. The double refund is invisible to the arbiter. The only detector is the external system's own reconciliation, which fires late and out of band. 2. **Rollback becomes a source of new effects.** A rollback that issues compensating calls is itself non-idempotent and must be journaled. If the episode is replayed (retry after a crash, audit replay, or a second arbiter pass), the rollback effects are re-emitted. The episode's effect count is no longer a function of the commit decision; it is a function of how many times reconciliation ran. 3. **The arbiter's majority vote is computed over claims that no longer correspond to the world.** Branch B's claim "refund issued, RF-902" was true at claim time and false after B's rollback. A re-arbiter reading the journal sees a claim whose referent was mutated by the rollback path. Any verification replay that trusts the claim will disagree with the backend. The invariant to enforce: **an episode's external effect set must be a function of (committed branch's proposals) alone, and must be invariant under the number of speculative branches, the number of rollbacks, and the number of reconciliation passes.** If that function depends on branch count or rollback count, the orchestrator is not doing speculative execution — it is doing concurrent mutation with a vote on top. ```python import hashlib from dataclasses import dataclass, field from enum import Enum from typing import Optional class SpeculationLeakError(RuntimeError): """Raised when a speculative branch applies a non-interceptable external effect.""" class ToolClass(Enum): STAGED = "staged" # journaled, no external effect until commit IDEMPOTENT_EXTERNAL = "idem_ext" # escapes, but backend dedupes on intent key NON_IDEMPOTENT_EXTERNAL = "non_idem_ext" # escapes, no dedupe, observable @dataclass(frozen=True) class ToolSpec: name: str tool_class: ToolClass intent_scoped: bool = False # True => backend dedupes on (episode, intent_hash) @dataclass class Episode: episode_id: str journal: list = field(default_factory=list) committed: bool = False rolled_back: bool = False def intent_key(episode_id: str, tool_name: str, semantic_args: dict) -> str: # Dedupe key MUST be derived from semantic intent, not branch identity, # so that two branches proposing the same refund collapse to one effect. payload = f"{episode_id}:{tool_name}:{sorted(semantic_args.items())}" return hashlib.sha256(payload.encode()).hexdigest() def speculative_call( episode: Episode, branch_id: str, tool: ToolSpec, args: dict, backend, ) -> Optional[str]: """Invoke a tool from a speculative branch. Enforces the interception invariant.""" if episode.committed or episode.rolled_back: raise SpeculationLeakError( f"episode {episode.episode_id} already reconciled; branch {branch_id} " f"attempted post-decision call to {tool.name}" ) if tool.tool_class is ToolClass.STAGED: episode.journal.append(("staged", branch_id, tool.name, args)) return None if tool.tool_class is ToolClass.IDEMPOTENT_EXTERNAL: if not tool.intent_scoped: # Escaping effect with no dedupe key: cannot be safely speculative. raise SpeculationLeakError( f"tool {tool.name} is external but not intent-scoped; " f"branch {branch_id} would leak a non-collapsible effect" ) key = intent_key(episode.episode_id, tool.name, args) episode.journal.append(("idem_ext", branch_id, tool.name, key)) return backend.apply_idempotent(key, tool.name, args) if tool.tool_class is ToolClass.NON_IDEMPOTENT_EXTERNAL: # The boundary: refuse to apply. Emit a proposal instead. raise SpeculationLeakError( f"tool {tool.name} is non-idempotent-external; branch {branch_id} " f"must emit a proposal, not call it speculatively" ) raise SpeculationLeakError(f"unclassified tool {tool.name}") def reconcile(episode: Episode, winner: str, backend) -> dict: """Commit the winner's proposals exactly once; discard all other branches.""" if episode.committed or episode.rolled_back: raise SpeculationLeakError(f"episode {episode.episode_id} already reconciled") applied = {} for kind, branch_id, name, payload in episode.journal: if branch_id != winner: continue # losers: staged entries discarded, idem_ext already deduped if kind == "staged": applied[name] = backend.apply(name, payload) elif kind == "idem_ext": applied[name] = backend.apply_idempotent(payload, name, {}) episode.committed = True return applied def replay(episode: Episode, backend) -> dict: """Replay must be invariant under rollback count. Rollback emits no effects.""" if not episode.committed: raise SpeculationLeakError( f"cannot replay unreconciled episode {episode.episode_id}; " f"effects are not a function of the commit decision" ) return {name: backend.lookup(name) for name in backend.keys()} ```