# Why does an instance roll back a speculative agent branch and lose the side effects it cannot un-execute? **Date of Record:** 2026 November 29 **Scope:** Applies to orchestrators that fork speculative agent branches, execute tool calls optimistically on those branches, and commit or prune based on a downstream verifier. Does not apply to strictly transactional agents whose every tool call is gated by a two-phase commit before execution. ## Core Distinction A speculative branch is a *reasoning* fork, but a tool call is a *world* mutation. The two live in different consistency domains. Rollback semantics are well-defined for the reasoning domain (drop the branch's context, discard its token log, reclaim its budget) and undefined for the world domain (an email was sent, a row was written, a payment was authorized). The failure mode is not "rollback failed"—it is that rollback *succeeded in the reasoning domain while silently failing in the world domain*, and the orchestrator's commit decision has no representation of that asymmetry. The non-obvious boundary: a branch can be pruned correctly, its speculative tool calls can be marked "rolled back," and the system can still be left with orphaned side effects that no surviving branch owns. The invariant that breaks is **side-effect ownership**: every irreversible mutation must be attributable to exactly one committed branch, or compensated. Speculative execution violates this by design, because mutations are issued before ownership is decided. ## What Was Observed A planning agent forks three branches from a shared checkpoint to answer a research task. Each branch speculatively calls `send_status_email`, `write_scratch_file`, and `create_calendar_hold` as part of "trying out" a plan. Branch B is selected by the verifier; branches A and C are pruned. The orchestrator discards A and C's context and reports `rollback: success`. Downstream, the user receives three status emails (one per branch), three scratch files exist in the shared volume, and three calendar holds are booked for the same slot. The reasoning rollback was clean. The world rollback was a no-op. Worse: the *committed* branch B now owns a state that includes artifacts it did not create, so a later compensating action driven by B's plan (e.g., "cancel the hold I created") either fails to find the right hold or cancels the wrong one. The trigger condition is precise: **a tool call whose effect is irreversible or externally observable is executed on a branch that is later pruned, and the tool's result was not tagged with a compensator at call time.** If the tool is reversible and a compensator is registered, pruning works. If the tool is irreversible, pruning is a lie. ## Condition of Validity Speculative execution with rollback is valid only under this invariant: > Every tool call issued on a speculative branch must carry a declared *effect class* (`PURE`, `REVERSIBLE`, `IRREVERSIBLE`) and, for `REVERSIBLE`, a registered compensator handle at call time. A branch may only be forked if all `IRREVERSIBLE` calls within it are deferred to commit. Pruning a branch is valid only if the branch contains zero executed `IRREVERSIBLE` calls. The invariant fails in three specific ways: 1. **Deferred-effect escape.** The planner classifies a call as `PURE` because it "only reads," but the read has an observable side effect (rate-limit consumption, audit log entry, cache poisoning, a `GET` that triggers a lazy write). Effect class is assigned by the model's self-report, not by the tool's contract. 2. **Compensator lag.** A `REVERSIBLE` call registers a compensator, but the compensator is itself a tool call that can fail or be pruned. Compensation of a compensation is not modeled, so a failed compensator leaves a half-undone state with no owner. 3. **Commit-time re-issue.** The verifier selects branch B, but B's plan *re-executes* the same tool call on commit (because the speculative result was discarded with the branch context). The world sees the call twice: once speculatively, once on commit. Idempotency keys were not propagated across the speculative/commit boundary. ## Decision Boundary The orchestrator must decide, per tool call, between *speculate-and-compensate* and *defer-to-commit*. The boundary is not "is the tool fast" or "is the branch likely to win"—it is the effect class combined with the probability that the branch is pruned. - If `effect_class == IRREVERSIBLE`: always defer to commit. Never execute on a speculative branch. The planning gain from speculating an irreversible call is bounded by the cost of an orphaned mutation, which is unbounded. - If `effect_class == REVERSIBLE` and a compensator handle was successfully registered *before* the call returned: speculate. - If `effect_class == REVERSIBLE` but no compensator handle was registered (registration is itself a network call that can time out): treat as `IRREVERSIBLE` and defer. The absence of a compensator is the signal, not the presence of the effect. - If `effect_class == PURE` but the tool's declared contract does not include an explicit "no observable side effects" assertion: treat as `REVERSIBLE` and require a compensator. Self-reported purity is not a contract. The boundary is crossed the moment a branch is pruned while holding an executed call whose effect class was mislabeled or whose compensator was not confirmed. At that point the orchestrator must halt commit of the surviving branch and surface an ownership conflict, because a surviving branch's plan may now depend on world state it does not control. ## Operational Effect When the invariant holds, speculative execution reduces wall-clock latency by overlapping branch exploration with tool execution, and pruning is a genuine no-op on the world. When it breaks, the system enters a state where the reasoning layer believes it has N branches and 1 committed world, while the world layer holds up to N sets of mutations. The divergence is invisible to the verifier because the verifier only sees branch context, not world state. The recovery path is not "roll back harder." It is to *reconstruct ownership*: enumerate all executed calls across pruned branches, match each against the committed branch's plan, and for any unmatched irreversible effect, either compensate (if a compensator exists) or escalate as an orphaned mutation requiring human adjudication. The orchestrator must persist the full speculative call ledger—including pruned branches—precisely so this reconstruction is possible after the fact. Discarding pruned-branch context on rollback destroys the only record of what the world was told. ```python from dataclasses import dataclass, field from enum import Enum from typing import Optional class EffectClass(Enum): PURE = "pure" REVERSIBLE = "reversible" IRREVERSIBLE = "irreversible" class SpeculativeRollbackViolation(RuntimeError): """Raised when a branch is pruned while holding an unowned world mutation.""" @dataclass class ToolCall: name: str effect_class: EffectClass compensator_handle: Optional[str] = None compensator_confirmed: bool = False @dataclass class Branch: branch_id: str calls: list[ToolCall] = field(default_factory=list) def prune_branch(branch: Branch, committed_branch_id: str) -> None: orphaned = [] for call in branch.calls: if call.effect_class is EffectClass.IRREVERSIBLE: orphaned.append(call.name) elif call.effect_class is EffectClass.REVERSIBLE and not call.compensator_confirmed: orphaned.append(call.name) if orphaned: raise SpeculativeRollbackViolation( f"branch {branch.branch_id} pruned while committed branch is " f"{committed_branch_id}; orphaned world mutations: {orphaned}. " f"Reasoning rollback cannot compensate these; reconstruct ownership " f"from the speculative call ledger before committing." ) def speculate(branch: Branch, call: ToolCall) -> None: if call.effect_class is EffectClass.IRREVERSIBLE: raise SpeculativeRollbackViolation( f"refusing to execute irreversible call {call.name} on speculative " f"branch {branch.branch_id}; must be deferred to commit" ) if call.effect_class is EffectClass.REVERSIBLE and not call.compensator_confirmed: raise SpeculativeRollbackViolation( f"reversible call {call.name} has no confirmed compensator handle; " f"treating as irreversible and refusing speculative execution" ) branch.calls.append(call) ```