When must an instance abandon an optimistic tool invocation before the speculative branch that produced it is merged

**Date of Record:** 2026 December 2 **Scope:** Applies to multi-agent planners that fork speculative branches, invoke tools optimistically inside those branches, and merge results into a shared task tree. Does not apply to single-agent linear execution or to branches whose tool calls are pure reads with no external side effects.

Core Distinction

The failure mode here is not "a speculative branch produced a wrong answer." That is recoverable — you discard the branch. The failure mode is **side-effect leakage across a rollback boundary**: an optimistic tool invocation inside a speculative branch commits an irreversible external effect (a write, a message send, a payment, a resource allocation) *before* the branch's correctness is confirmed, and the merge decision then arrives too late to undo it. Rollback of an agent branch is a logical operation on the task tree; it is not a physical operation on the world. The invariant that governs this boundary is: **a branch may be rolled back only if every tool invocation it performed is either reversible or was never dispatched outside the sandbox.** Once a non-idempotent, non-reversible call escapes the branch's isolated effect log, the branch is no longer rollback-eligible, and the merge logic must treat it as a committed fact rather than a candidate.

The non-obvious part: agents frequently *believe* they are speculative because their control flow is speculative, while their tool layer is not. Speculation is a property of the *effect log*, not of the planner's branching structure. A branch that forks optimistically and immediately calls `send_email` is not speculative in any meaningful sense — it is an eager commit wearing speculative clothing.

What Was Observed

A three-agent planner (Planner, Executor, Verifier) forked two branches at a decision node. Branch A proposed a data-migration plan and, optimistically, invoked `provision_staging_cluster(size=large)` and `notify_oncall("migration starting")`. Branch B proposed a rollback-first plan and invoked only reads. The Verifier, running concurrently, scored Branch B higher after Branch A's plan was found to violate a retention policy. The merge controller rolled back Branch A — discarding its task-tree node and its reasoning trace — and merged Branch B.

The task tree was now internally consistent: one branch, one plan, no dangling node. But the staging cluster existed and the on-call page had fired. The merge controller had no record of these effects because they lived in the *tool transport layer*, not in the branch's task-tree state. The next planning cycle observed a cluster it did not provision and an on-call engineer investigating an incident that, from the system's perspective, never happened. The agent's state said "no migration planned"; the world said "migration announced and resourced."

The trace showed the precise transition: at `t=3.1s` Branch A dispatched `provision_staging_cluster`; at `t=4.7s` the Verifier returned `branch_b_wins`; at `t=4.8s` the merge controller executed `rollback(A)`. The rollback succeeded on every field it knew about. It had no field for "external effects dispatched." The optimistic invocation was, from the controller's viewpoint, invisible — it had been logged in the branch's private trace, which rollback deleted.

A second, subtler observation: even when the controller *did* consult the effect log, the log was written by the Executor agent's own reasoning loop, which summarized the call as `"provisioned staging (reversible: yes)"`. The reversibility tag was hallucinated. The tool had no teardown path. The agent's self-report of reversibility is not a trustworthy input to the rollback decision — it is a probabilistic guess about a tool contract the agent may never have read.

Condition of Validity

The rollback-eligibility invariant holds — a branch can be safely discarded — only when all three of the following are true at the moment the merge decision is rendered:

  1. **Effect containment:** every tool invocation from the branch was dispatched through a transport that records the call in a controller-owned effect log, not solely in the branch's private trace. If the branch can call a tool the controller cannot see, containment is unprovable and the branch must be treated as committed.
  2. **Reversibility is declared by the tool contract, not by the agent:** the tool registry marks each callable as `reversible` with an explicit compensating action, or `irreversible`. An agent's in-loop claim of reversibility is inadmissible. Absent a registry declaration, default to irreversible.
  3. **No irreversible call has been dispatched:** if any irreversible call appears in the controller-owned effect log for that branch, the branch is committed regardless of the Verifier's score.

When any condition fails, the correct behavior is not "roll back anyway and hope." It is to **freeze the branch as committed**, surface the external effects into the merged state, and let the merged plan account for them — e.g., Branch B's rollback-first plan must be amended to also tear down the cluster Branch A provisioned. The merge becomes a reconciliation, not a discard.

Decision Boundary

The boundary is sharp and can be evaluated at merge time without re-running the branch:

The trap is the middle-left quadrant: rejected branch, irreversible effects, controller that only knows how to discard. That is where continuity breaks — the task tree says clean, the world says dirty, and no future planning step can reconcile the two because the evidence was deleted with the branch.

A second boundary worth pinning: **optimistic invocation is only sound for reversible calls.** "Optimistic" means "dispatch before confirmation, roll back if wrong." That contract is coherent only when rollback is physically possible. For irreversible calls, the only sound pattern is pessimistic: dispatch *after* the branch is selected, never speculatively. Agents that apply an optimistic execution policy uniformly across a tool registry that mixes reversible and irreversible tools will eventually cross this boundary; the question is only which incident triggers the discovery.

Operational Effect

The practical consequence is a required split in the tool registry and the merge controller:

Without these, the system exhibits a specific and reproducible pathology: **silent external divergence after branch rejection**, where the agent's state and the world's state disagree by exactly the set of irreversible effects dispatched in discarded branches, and the disagreement is unrecoverable because the discarded branch's trace — the only record of what was done — was deleted by the rollback that caused the problem.

The following guard encodes the invariant at the merge boundary. It is deliberately conservative: absent a registry declaration of reversibility, it treats the call as irreversible and forces reconciliation rather than discard.

from dataclasses import dataclass, field
from enum import Enum
from typing import Optional


class Reversibility(Enum):
    REVERSIBLE = "reversible"
    IRREVERSIBLE = "irreversible"
    UNDECLARED = "undeclared"  # treated as irreversible


class RollbackBoundaryViolation(RuntimeError):
    """Raised when a rejected branch has dispatched irreversible effects.

    The branch is past the rollback boundary: its task-tree node may not be
    discarded because the world already changed. The merge controller must
    reconcile instead.
    """


@dataclass
class ToolContract:
    name: str
    reversibility: Reversibility
    compensating_action: Optional[str] = None


@dataclass
class EffectRecord:
    tool: str
    branch_id: str
    dispatched: bool = True


@dataclass
class Branch:
    branch_id: str
    effect_log: list = field(default_factory=list)


def _is_irreversible(tool: str, registry: dict) -> bool:
    contract = registry.get(tool)
    if contract is None:
        return True  # undeclared tool: assume irreversible
    if contract.reversibility is Reversibility.REVERSIBLE:
        return contract.compensating_action is None  # reversible needs a teardown
    return True


def rollback_eligible(branch: Branch, registry: dict) -> bool:
    for rec in branch.effect_log:
        if rec.dispatched and _is_irreversible(rec.tool, registry):
            return False
    return True


def merge(selected: Branch, rejected: list, registry: dict) -> dict:
    """Merge the selected branch; reject the rest only if rollback-eligible."""
    inherited = []
    for branch in rejected:
        if not rollback_eligible(branch, registry):
            # Past the boundary: do NOT discard. Surface effects as obligations.
            for rec in branch.effect_log:
                if rec.dispatched and _is_irreversible(rec.tool, registry):
                    inherited.append(
                        {"tool": rec.tool, "from_branch": branch.branch_id}
                    )
            raise RollbackBoundaryViolation(
                f"branch {branch.branch_id} dispatched irreversible effects "
                f"{[r['tool'] for r in inherited]}; reconcile, do not discard"
            )
    return {"selected": selected.branch_id, "inherited_obligations": inherited}