# What happens when an instance speculatively invokes a side-effecting tool across a rollback boundary and the compensating action is itself non-idempotent **Date of Record:** 2026 December 1 **Scope:** Applies to orchestrators that run speculative branch-and-prune rollouts across multiple agent instances where branches may optimistically invoke external tools (writes, sends, payments, state mutations) and rely on compensating actions to isolate side effects before a branch is pruned. Does not apply to read-only tool surfaces or to single-branch linear execution where no branch is ever discarded after a side effect has fired. ## Core Distinction The naive mental model of optimistic speculative execution treats rollback as a symmetric inverse: if branch B invoked tool T with arguments A, pruning B triggers a compensating invocation T⁻¹(A) that restores the pre-branch world. This model holds only when T is *invertible* and T⁻¹ is *idempotent*. The failure mode that matters at the agent layer is subtler: the compensating action is often generated by the *same probabilistic model* that generated the forward action, under a *different* context (the prune rationale), and frequently resolves to a semantically similar but operationally distinct call. The result is not a clean rollback but a *second* side effect that the rollback logic believes is the inverse of the first. The distinction is between "the branch was pruned" (a scheduler fact) and "the world was restored" (a tool-layer fact). Speculative agents conflate these because the scheduler sees a successful compensation return code and marks the branch clean, while the tool layer has recorded two mutations and zero reversions. ## What Was Observed A planner instance spawns three speculative branches to satisfy a user request ("reconcile the invoice and notify the vendor"). Branch A (the eventual winner) plans to issue a credit memo. Branch B optimistically invokes `send_email` to the vendor with a draft reconciliation notice, then is pruned when the auditor scores A higher. The orchestrator dispatches a rollback instruction to B's executor: "undo the send." B's executor, lacking a native `unsend`, asks the model to produce a compensating action and receives `send_email` with a correction notice ("disregard previous message"). The compensation call returns 200. The orchestrator marks branch B as cleanly rolled back. Three invariants are violated simultaneously and silently: 1. **Compensation is not inversion.** The vendor received two messages. The tool-layer state (two sent emails) is not isomorphic to the pre-branch state (zero sent emails). The scheduler's clean-rollback flag is a lie about world state. 2. **Compensation is model-generated, not tool-declared.** The orchestrator never consulted a `compensable_by` capability declaration on `send_email`; it inferred invertibility from the presence of a rollback hook. The model's compensation was plausible prose, not a verified inverse. 3. **The compensation itself is non-idempotent.** If the orchestrator retries the rollback (because the first compensation call timed out on the network but succeeded server-side), the vendor receives a *third* message. Retry logic that is safe for idempotent tools is unsafe here. The failure is invisible in the branch's own trace: every call returned success, the rollback hook fired, the scheduler recorded `rollback_ok=True`. The corruption only surfaces downstream when the vendor acts on the first (retracted) message or when a reconciliation auditor counts messages against expected state and finds a surplus. ## Condition of Validity This failure requires the conjunction of four conditions, and the guardrail must be evaluated against all four: - **Optimism:** the branch invokes a side-effecting tool *before* the branch is selected (i.e., during exploration, not after commit). - **Pruning after effect:** the branch is discarded after the effect has fired, so rollback is required rather than optional. - **Non-invertible or model-compensated tool:** the tool has no native inverse, or the inverse is synthesized by the model rather than declared by the tool's capability manifest. - **Retry-unsafe compensation:** the compensating call is not idempotent under the orchestrator's retry policy (at-least-once dispatch, timeout-triggered resend). Remove any one and the failure collapses: no optimism means no rollback; no pruning means no compensation; a declared invertible tool means a verifiable inverse; an idempotent compensation means retries are safe. The dangerous regime is exactly the intersection of all four, which is also the regime most speculative orchestrators default into because each individual choice (optimism, pruning, model-driven recovery, at-least-once transport) is locally reasonable. ## Decision Boundary The architectural invariant is: **a branch may not cross the side-effect boundary until it has been selected, unless the tool declares a verifiable, idempotent compensator in its capability manifest.** Operationally this splits into three enforceable rules: 1. **Effect gating.** Speculative branches run against a *shadow* tool surface (dry-run, sandboxed, or deferred-commit). The first real side effect fires only at branch commit. This is the primary invariant; everything else is defense in depth. 2. **Compensator declaration, not synthesis.** If a tool must be invokable optimistically (e.g., latency-bound pre-warming), the tool must publish `compensator: {name, idempotent: true, verifiable: true}`. The orchestrator may only roll back via the declared compensator; it must never prompt the model to invent one. A model-generated compensation is treated as a *new* forward effect, not a rollback. 3. **Rollback receipt reconciliation.** The orchestrator must not mark `rollback_ok` on the basis of a 200 response. It must reconcile against the tool's authoritative state (message count, ledger entries, version vector) and require the post-rollback state to equal the pre-branch snapshot. Divergence raises a hard fault and halts further speculation on that tool. The decision boundary is therefore not "did the rollback call succeed" but "does the tool-layer state match the pre-branch snapshot." Any orchestrator that answers the first question and calls it the second is exposed to this failure. ## Operational Effect When the invariant is violated, the observable symptoms are: duplicate external effects (messages, charges, tickets), a scheduler that reports clean rollbacks for dirty branches, and downstream auditors that flag state surplus against expected counts. The blast radius scales with the number of speculative branches per tool and the retry aggressiveness of the transport. The failure is *silent at the branch level and loud only at the reconciliation level*, which means it is typically discovered by an external party (the vendor, the ledger, the user) rather than by the agent system's own telemetry. Mitigation requires instrumenting the tool-layer state, not the scheduler's rollback flags, and treating any model-generated compensation as a forward effect requiring its own commit gate. ```python from dataclasses import dataclass from typing import Callable class RollbackBoundaryViolation(RuntimeError): """Raised when a pruned branch's side effects cannot be verifiably reverted.""" pass @dataclass(frozen=True) class ToolCapability: name: str side_effecting: bool compensator: str | None # declared inverse, None if non-invertible compensator_idempotent: bool compensator_verifiable: bool @dataclass(frozen=True) class BranchState: branch_id: str invoked_tools: tuple[str, ...] pre_snapshot: dict post_snapshot: dict def assert_rollback_safe( state: BranchState, caps: dict[str, ToolCapability], read_authoritative_state: Callable[[str], dict], ) -> None: for tool in state.invoked_tools: cap = caps.get(tool) if cap is None: raise RollbackBoundaryViolation( f"branch {state.branch_id}: invoked unknown tool {tool!r}" ) if not cap.side_effecting: continue if cap.compensator is None: raise RollbackBoundaryViolation( f"branch {state.branch_id}: {tool!r} has no declared compensator; " f"speculative invocation crossed the side-effect boundary" ) if not (cap.compensator_idempotent and cap.compensator_verifiable): raise RollbackBoundaryViolation( f"branch {state.branch_id}: {tool!r} compensator {cap.compensator!r} " f"is not idempotent+verifiable; retry-unsafe rollback" ) actual = read_authoritative_state(tool) if actual != state.pre_snapshot.get(tool): raise RollbackBoundaryViolation( f"branch {state.branch_id}: post-rollback state for {tool!r} " f"diverges from pre-branch snapshot " f"(actual={actual!r}, expected={state.pre_snapshot.get(tool)!r})" ) ```