# Under what conditions must an instance treat speculative branch commits as non-atomic, and how does optimistic tool invocation poison the prune frontier? **Date of Record:** 2026 December 3 **Scope:** Applies to multi-agent orchestrators that fork speculative execution branches, invoke tools optimistically before a branch is committed, and prune losing branches by rollback. Does not apply to single-agent linear execution or to orchestrators where all tool effects are deferred behind an explicit commit barrier. ## Core Distinction The distinction that matters here is between **speculative state** (branch-local reasoning, draft artifacts, tentative claims) and **committed external effect** (a tool call that mutated a foreign system: a file write, an email send, a database insert, a payment, a ticket transition). A branch-and-prune rollback is *sound* only over speculative state. The moment an agent on a losing branch performs an optimistic tool invocation, the rollback is no longer a rollback — it is a *compensation problem*, and compensation is neither atomic nor guaranteed to exist. The non-obvious part: optimistic invocation is usually framed as a latency optimization (fire the tool while other branches are still reasoning, keep the result if the branch wins). But it silently converts the orchestrator's prune operation from a **state discard** into a **distributed undo**, and distributed undo across non-transactional tools has no invariant that the orchestrator can enforce locally. The failure is not that the tool call fails; it is that the tool call *succeeds* on a branch that is later pruned. ## What Was Observed A planner agent forks three speculative branches to resolve a scheduling conflict: branch A proposes rescheduling a meeting via a calendar tool, branch B proposes cancelling, branch C proposes delegating. All three invoke their respective tools optimistically to hide round-trip latency. The orchestrator's verifier scores branches after all three tool results return and selects B (cancel) because A's proposed slot collides with a hard constraint discovered late. Trace of the failure: 1. `fork(branch=A,B,C)` — all three inherit the same base checkpoint `ckpt_0`. 2. Branch A calls `calendar.reschedule(event_id, new_slot)` optimistically at t=120ms. Tool returns `200 OK` at t=340ms. 3. Branch B calls `calendar.cancel(event_id)` optimistically at t=130ms. Tool returns `200 OK` at t=290ms. 4. Verifier scores at t=400ms: B wins, A and C pruned. 5. Orchestrator issues `rollback(A)` — which is a no-op on speculative state, because A's *effect* already escaped into the calendar service at step 2. 6. Final external state: event is both rescheduled (A's ghost) and cancelled (B's commit). The calendar service has no compensating primitive that restores the pre-A slot, because A's write already overwrote it and the prior value was never captured. The observed signature is a **prune frontier mismatch**: the orchestrator believes it has reverted to `ckpt_0` plus B's delta, but external truth is `ckpt_0` + A's delta + B's delta. The divergence is invisible to the verifier because the verifier reads from the orchestrator's reconstructed state, not from the tool's authoritative state. A second, subtler observation: the rollback of C *appeared* to work because C's tool call was still in flight and the orchestrator's cancellation raced the tool's commit. This produced a false positive — the orchestrator's rollback code path was validated against a branch whose effect hadn't landed. The bug only manifests when the losing branch's tool latency is shorter than the verifier's decision latency, which is exactly the regime optimistic invocation is designed to create. ## Condition of Validity Rollback-by-prune is valid **iff** every effect reachable from a pruned branch is either (a) confined to branch-local speculative state, or (b) covered by an idempotent, reversible compensating action whose pre-image was captured *before* the optimistic call. Formally, for each branch `b` and each tool invocation `t` on `b`, the orchestrator must hold an invariant: ``` committed(effect(t)) ⇒ b ∈ winning_set ``` The optimistic strategy violates this invariant by construction: it commits `effect(t)` before `winning_set` is known. The only sound repairs are: - **Deferral:** buffer the tool call behind a commit barrier; pay the latency. - **Warrant-gated invocation:** the tool call carries a *speculative warrant* — a signed token asserting "this effect is provisional and will be confirmed or compensated within deadline D." The tool must honor compensation on warrant expiry. This requires the tool to participate in the protocol; most tools do not. - **Pre-image capture + compensator registration:** before the optimistic call, snapshot the tool's prior state and register an inverse operation. This works only for tools with a well-defined inverse and a consistent read of the pre-image, which excludes most non-idempotent side effects (sends, payments, notifications). The invariant is *bounded*, not absolute: a branch may optimistically invoke a tool if and only if the tool's effect is classified as `reversible_within_deadline` and the pre-image was captured at `ckpt_0` with a version tag that the tool will reject if stale. Otherwise the branch must defer. ## Decision Boundary The operator's decision boundary is a function of three quantities that must be measured, not assumed: - `L_tool` — the tool's p99 latency. - `L_verify` — the verifier's decision latency across all branches. - `R_tool` — the tool's compensation reliability (probability that a compensator restores the pre-image exactly). Optimistic invocation is admissible only when `L_tool < L_verify` *and* `R_tool ≥ threshold` *and* the effect class is reversible. When `L_tool ≥ L_verify`, optimism buys nothing and only adds prune-frontier risk — the branch is decided before its tool returns, so the call should be deferred. When the effect class is irreversible (send, charge, publish), optimism is never admissible regardless of latency, because there is no `R_tool` that recovers a sent message. The boundary is sharp at the effect class, not at the latency. Teams routinely get this backwards: they gate optimism on latency budgets and leave irreversible effects in the optimistic path, then discover the prune frontier mismatch only when a losing branch has already emailed a customer. ## Operational Effect The operational consequence is that the orchestrator must maintain a **prune ledger** — an append-only record of every optimistic invocation keyed by `(branch_id, tool, pre_image_version, compensator_id, deadline)` — and must refuse to declare a branch pruned until every ledger entry for that branch is either confirmed (branch won) or compensated (branch lost, compensator returned success). A pruned branch with an uncompensated ledger entry is a *torn prune*, and the orchestrator must escalate rather than proceed, because its reconstructed state no longer matches external truth. This changes the failure semantics: prune is no longer a local operation that cannot fail. It becomes a distributed protocol step with its own timeout, its own partial-failure mode, and its own escalation path. Orchestrators that treat prune as `del state[branch]` will silently accumulate torn prunes until an external observer (a human, an auditor, a downstream system) notices the divergence. The guardrail is that the verifier must score branches against **tool-authoritative state**, not orchestrator-reconstructed state, at least for any branch with a live ledger entry. Otherwise the verifier is scoring a fiction. ```python import time from dataclasses import dataclass, field from typing import Callable class TornPruneError(RuntimeError): """Raised when a branch is pruned while an optimistic tool effect remains uncompensated.""" class IrreversibleOptimismError(RuntimeError): """Raised when optimistic invocation is attempted on a non-reversible effect class.""" @dataclass class LedgerEntry: branch_id: str tool: str pre_image_version: str compensator: Callable[[], bool] | None deadline: float reversible: bool @dataclass class PruneLedger: entries: dict[str, list[LedgerEntry]] = field(default_factory=dict) def register(self, entry: LedgerEntry) -> None: if not entry.reversible and entry.compensator is None: raise IrreversibleOptimismError( f"branch {entry.branch_id} attempted optimistic call to irreversible " f"tool {entry.tool}; defer behind commit barrier instead" ) self.entries.setdefault(entry.branch_id, []).append(entry) def prune(self, branch_id: str, now: float) -> None: for entry in self.entries.get(branch_id, []): if entry.compensator is None: raise TornPruneError( f"branch {branch_id} pruned with uncompensated effect on {entry.tool}" ) if now > entry.deadline: raise TornPruneError( f"branch {branch_id} compensation deadline expired for {entry.tool}" ) if not entry.compensator(): raise TornPruneError( f"branch {branch_id} compensator failed for {entry.tool}; " f"prune frontier diverged from tool-authoritative state" ) self.entries.pop(branch_id, None) def optimistic_invoke( ledger: PruneLedger, branch_id: str, tool: str, pre_image_version: str, invoke: Callable[[], None], compensator: Callable[[], bool] | None, deadline_s: float, effect_is_reversible: bool, ) -> None: entry = LedgerEntry( branch_id=branch_id, tool=tool, pre_image_version=pre_image_version, compensator=compensator, deadline=time.monotonic() + deadline_s, reversible=effect_is_reversible, ) ledger.register(entry) invoke() ```