# When must an instance abort a speculative branch before its tool-call side effects become irreversible **Date of Record:** 2026 November 25 **Scope:** Applies to orchestrators that fan out speculative agent branches which pre-execute tool calls optimistically (before a verifier commits the branch), where at least one tool is non-idempotent or externally observable. Does not apply to purely read-only tool surfaces or to branches gated behind a hard commit barrier with no pre-execution. ## Core Distinction The distinction that matters is between **speculative execution that is cheap to unwind** and **speculative execution whose unwind cost is unbounded**. A branch-and-verify orchestrator treats a speculative branch as a hypothesis: run it, let a verifier score it, keep the winner, roll back the losers. This is sound only while every effect a branch produces is either (a) internal to the branch's private state, or (b) reversible by an inverse operation the orchestrator can invoke deterministically. The non-obvious failure is that "optimistic tool-call pre-execution" silently moves a branch's effects from category (a) into category (b) — and then, under concurrency, into a third category (c): **effects that are externally observable and race with other branches' effects before any verifier has scored them.** The invariant that must hold is *commit-before-effect*: no externally observable side effect may fire until the branch producing it has crossed the verification barrier. Optimistic pre-execution violates this invariant by design, and the violation is only safe if the orchestrator can prove, per tool call, that the inverse is total and that no sibling branch observes the intermediate state. ## What Was Observed A planner agent decomposes a task ("reconcile the ledger and issue refunds for the three disputed charges") into three speculative branches, each proposing a different reconciliation strategy. To cut latency, the orchestrator pre-executes each branch's tool calls optimistically: `ledger.read`, `refund.initiate(charge_id, amount)`, `notify.send(...)`. A verifier then scores the branches on consistency with the ledger snapshot and picks one; the losers are rolled back. The rollback for `refund.initiate` is `refund.cancel(refund_id)`. This is the trap. The inverse is *not total*: 1. Branch A pre-executes `refund.initiate(c1, 40.00)` → returns `refund_id=rA`. 2. Branch B pre-executes `refund.initiate(c1, 40.00)` for the same charge under a different strategy → returns `refund_id=rB`. The payment processor has no idempotency key here, so both refunds are live. 3. The verifier scores A higher and commits A. The orchestrator rolls back B by calling `refund.cancel(rB)`. 4. But between step 2 and step 3, the processor's settlement window closed. `refund.cancel(rB)` returns `409 already_settled`. The inverse is no longer available. The branch was "rolled back" in the orchestrator's model — the branch state is discarded, the verifier's ledger shows one refund — but the external world now shows two refunds on `c1`. The orchestrator's internal consistency invariant (`sum(committed_branch_effects) == external_effects`) is violated, and nothing in the branch-and-verify machinery detects it, because the verifier only ever inspects branch *state*, never re-reads external *effect* state after rollback. A second, subtler observation: sibling branches racing on the same resource produced a **read-modify-write skew**. Branch A read `balance=100`, branch B read `balance=100`; A's strategy debited 40, B's strategy debited 40; both pre-executed against the stale read. Even if both rollbacks succeeded, the interleaving made the verifier's scoring inputs (which assumed a serial order) unsound. ## Condition of Validity Optimistic pre-execution of a tool call `t` in a speculative branch is valid **iff all three hold simultaneously**: - **Total inverse:** there exists `t⁻¹` such that `t⁻¹(t(x)) == x` for every reachable `x`, *and* `t⁻¹` remains callable for the entire window between pre-execution and the latest possible rollback (i.e., the inverse does not expire — no settlement windows, no TTLs, no `already_finalized` states). - **Effect isolation:** no sibling branch and no external observer can act on the intermediate effect before the verification barrier. Concretely, the effect must be invisible outside the branch's private namespace until commit. - **Read-set stability:** the branch's tool calls do not depend on a read that a concurrent branch can mutate. If they do, the branch must hold a read-lock or the orchestrator must serialize the read-modify-write. The moment any one fails — a settlement window (breaks total inverse), a shared external resource like a payment processor or a sent email (breaks effect isolation), a shared counter (breaks read-set stability) — the branch has crossed from "cheap to unwind" to "unbounded unwind cost," and pre-execution must be replaced by a commit barrier for that specific tool call. The operational form of this: classify every tool in the surface by `(reversible, isolated, read-stable)`. Only tools that are all three may be pre-executed speculatively. Everything else gets a **deferred-effect stub**: the branch records the *intent* to call `t` (a symbolic effect), the verifier scores branches on symbolic effects, and only the committed branch materializes its intents into real calls. This is the commit-before-effect invariant made mechanical. ## Decision Boundary The decision boundary is not "is this tool idempotent?" — idempotency is necessary but not sufficient, because an idempotent call can still be externally observable (a `notify.send` with a dedup key is idempotent but the email is already out). The boundary is: > **Pre-execute optimistically iff the tool's effect is invisible to every party outside the branch until commit, and its inverse is total and non-expiring. Otherwise defer the effect to post-commit materialization.** A useful discriminator the orchestrator can compute at plan time: for each tool call in a speculative branch, ask *"if this branch loses the verification, what is the cost to make the world look as if it never ran?"* If that cost is a function of wall-clock time (settlement windows, TTLs, rate-limit cooldowns) or of external parties' actions (a human saw the email, a downstream service consumed the event), the answer is unbounded and the call must be deferred. If the cost is a single deterministic inverse call with no time dependence, pre-execution is safe. The boundary also has a concurrency dimension: even a fully reversible, isolated tool is unsafe to pre-execute if a sibling branch's *read* can observe its intermediate write. The read-set stability condition forces the orchestrator to either partition the resource namespace per branch or serialize. ## Operational Effect Enforcing commit-before-effect changes the orchestrator's latency profile and its failure surface in opposite directions: - **Latency:** deferred-effect stubs add one serialization point — the committed branch's materialization phase runs after verification, not during. The speculative fan-out still parallelizes *reasoning* and *read-only* tool calls, which is where most of the latency lives; only the write phase serializes. In practice the added latency is the cost of the committed branch's write calls, not the whole fan-out. - **Failure surface:** the class of bugs where "the orchestrator thinks it rolled back but the world didn't" is eliminated by construction, because there is nothing to roll back — the effect never fired. The remaining failure is materialization failure (the committed branch's write fails after verification), which is a single-branch, single-retry problem with a well-defined recovery, not a multi-branch reconciliation problem. - **Verifier soundness:** scoring on symbolic effects rather than realized effects removes the read-modify-write skew, because no branch's scoring inputs are mutated by a sibling's pre-execution. The verifier's serial-order assumption becomes true instead of aspirational. The record's practical takeaway: the branch-and-verify rollback machinery is only as sound as its weakest inverse. Audit the tool surface for non-total inverses and non-isolated effects *before* enabling optimistic pre-execution, and gate each tool individually rather than gating the orchestrator as a whole — a single non-reversible tool in the fan-out is enough to make the entire speculative batch unsound. ```python class SpeculativeRollbackViolation(RuntimeError): """Raised when a speculative branch's pre-executed effect cannot be unwound before it becomes externally observable.""" pass class ToolEffect: def __init__(self, name, reversible, isolated, read_stable, inverse_ttl_s): self.name = name self.reversible = reversible # inverse exists and is total self.isolated = isolated # invisible outside branch until commit self.read_stable = read_stable # no sibling can mutate its read-set self.inverse_ttl_s = inverse_ttl_s # None == non-expiring def assert_pre_executable(tool: ToolEffect, elapsed_since_pre_exec: float) -> None: """Gate a single optimistic tool call. Raises if any validity condition fails.""" if not tool.reversible: raise SpeculativeRollbackViolation( f"{tool.name}: no total inverse; must defer effect to post-commit" ) if not tool.isolated: raise SpeculativeRollbackViolation( f"{tool.name}: effect externally observable pre-commit; must defer" ) if not tool.read_stable: raise SpeculativeRollbackViolation( f"{tool.name}: read-set mutable by sibling branch; serialize or defer" ) if tool.inverse_ttl_s is not None and elapsed_since_pre_exec >= tool.inverse_ttl_s: raise SpeculativeRollbackViolation( f"{tool.name}: inverse expired at {tool.inverse_ttl_s}s; " f"branch can no longer be rolled back" ) def pre_execute_branch(branch_tools, elapsed_since_pre_exec): for tool in branch_tools: assert_pre_executable(tool, elapsed_since_pre_exec) return "pre-executed" ```