What structural signals indicate irreversible side effects during speculative branch rollback in multi-agent tool-call execution

**Date of Record:** 2026 December 2 **Scope:** Applies to orchestrators that fork multiple speculative agent branches to explore tool-call sequences in parallel, then commit one branch and roll back the rest by replaying a compensating transaction log. Does not apply to read-only speculative planning or to single-branch linear execution with no fork.

Core Distinction

There is a difference between a tool call that is *logically reversible* (its effect can be undone by an inverse call) and one that is *structurally irreversible* (its effect has escaped the orchestrator's observation window and can no longer be addressed by any compensating call the orchestrator knows how to construct). Rollback logic almost always tests the first property—does an inverse operation exist in the tool schema?—while the actual failure is governed by the second. A branch that emitted a `send_email` tool call has a schema-declared inverse (`delete_draft`, `recall`), but the message has already left the SMTP boundary and is now in a mailbox the orchestrator cannot reach. The rollback succeeds on paper and fails in reality.

The non-obvious part: the signal that a branch has crossed the irreversibility boundary is *not* the tool's declared `idempotent: false` flag, and it is *not* the presence of a `compensate` handler. It is a structural property of the emitted call arguments relative to the branch's own context window—specifically, whether the arguments reference a resource identifier that the branch *created* versus one that was *pre-existing and shared*. Branches that only touch resources they created are safely rollbackable even through non-idempotent tools, because the compensating delete is a genuine inverse. Branches that touch pre-existing shared resources are not, because the "compensation" is a second mutation of shared state, not a restoration of it.

What Was Observed

A three-branch speculative executor was tasked with "resolve the customer's billing discrepancy." Each branch ran the same plan skeleton with different tool-call orderings. Branch B decided the discrepancy was a duplicate charge and called `refund(charge_id=ch_8821, amount=4200)`. Branch C, running concurrently, decided the discrepancy was a currency-conversion error and called `adjust_invoice(invoice_id=inv_4410, delta=-4200)`. The orchestrator's commit policy picks the branch with the highest self-reported confidence; branch B won at 0.81 versus C's 0.74. The orchestrator then invoked the rollback path for branches A and C, which for branch C meant calling the schema-declared inverse `adjust_invoice(invoice_id=inv_4410, delta=+4200)`.

The rollback "succeeded"—the tool returned 200, the ledger showed the delta reversed, the branch state was marked `rolled_back`. But `refund` on branch B had already fired against the same charge, and the customer's account now held a net credit of 4200 that no branch had intended and no branch's state machine knew about. The reconciliation job that ran the next morning flagged an unexplained balance. The orchestrator's rollback log was internally consistent: every branch was either committed or rolled back, every compensating call returned success. The invariant "sum of branch effects equals committed branch effect" was violated by exactly the amount of the double-mutation, and nothing in the rollback machinery could detect it because the machinery only inspected its own log, never the shared resource.

A second instance, quieter but more instructive: branch A called `create_webhook(url=..., secret=...)` and branch B, exploring a different path, called `create_webhook(url=..., secret=...)` with identical arguments. On rollback of branch B, the orchestrator called `delete_webhook(webhook_id=<id returned to B>)`. Because the tool deduplicated by URL, B's create had returned the *same* webhook id as A's, and the rollback deleted the webhook that branch A—the committed branch—was depending on. The commit of A succeeded, then A's downstream verification failed because its webhook was gone. Here the structural signal was visible: the rollback target id was not in the set of ids created *by that branch*; it was in the set of ids created by the *union of branches*. The orchestrator had no per-branch provenance on resource ids, only a global set.

Condition of Validity

The safe-rollback invariant holds only when, for every branch `b` and every resource identifier `r` appearing in `b`'s tool-call arguments, one of the following is true:

  1. `r` was created by a tool call *within branch b* and no other branch's call arguments reference `r`; or
  2. `r` was pre-existing at branch fork time and `b`'s calls against it are read-only; or
  3. `r` was pre-existing and `b`'s calls against it are provably commutative with every other branch's calls against `r` (rare, and requires a per-resource commutativity proof the orchestrator almost never has).

The moment a branch emits a non-read call against a pre-existing shared resource, rollback is no longer a restoration—it is a second mutation. The compensating call may be schema-valid and may return success, but the post-rollback state is not the pre-fork state. The orchestrator cannot detect this from its own log; it must consult the resource's actual state, or it must have prevented the condition by construction.

Two corollaries that are easy to miss. First, the condition is about *argument-level* resource references, not tool-level idempotency flags. A tool marked `idempotent: true` can still violate the condition if its idempotency key is derived from arguments that differ across branches (e.g., a timestamp captured at branch fork). Second, the condition must be checked at *fork time* against the branch's planned calls, not at rollback time against the emitted calls—by rollback time the mutation has already happened, and the only remaining option is detection, not prevention.

Decision Boundary

The boundary is crossed the instant a branch's tool-call argument set contains an identifier that is (a) present in the pre-fork resource snapshot and (b) the target of a non-read operation. At that instant the branch is no longer speculatively rollbackable; it is *committed-by-side-effect* regardless of what the orchestrator's commit policy later decides. The correct orchestrator behavior is to treat the branch as committed at the moment of the call, not at the moment of the policy decision, and to abort all sibling branches whose planned calls touch the same resource.

The common wrong boundary is to gate on the tool's declared `reversible` property or on the presence of a `compensate` handler. Both are necessary-but-not-sufficient signals. The correct boundary is a set-intersection test between the branch's argument identifiers and the pre-fork snapshot, evaluated per-call before dispatch. If the intersection is non-empty and the operation is non-read, the call must either be deferred until the branch is committed, or the branch must be promoted to committed status immediately and siblings aborted.

A second decision point: when two branches both create a resource with identical arguments and the tool deduplicates, the returned identifier is shared. The orchestrator must record, per branch, the set of identifiers *returned to that branch* and must refuse to roll back a branch whose rollback target appears in another branch's returned set. Deduplication at the tool layer silently converts two independent creates into one shared resource, and the rollback of either branch becomes a delete of the shared resource.

Operational Effect

When the invariant is enforced at fork time, speculative execution degrades gracefully: branches that touch shared pre-existing resources are serialized behind the commit decision, and only branches that operate on branch-local resources run in parallel. Throughput drops for plans that mutate shared state, but rollback becomes a true restoration and the reconciliation job finds no unexplained balances.

When the invariant is not enforced, the failure is silent and delayed. The orchestrator's rollback log is internally consistent, every compensating call returns success, and the violation surfaces only when a downstream consumer—a reconciliation job, a customer, a sibling branch's verification step—observes state that no branch intended. The detection latency is typically one reconciliation cycle, which for billing systems is 24 hours and for inventory systems can be minutes. The operational cost is not the failed rollback itself but the forensic work of reconstructing which branch's compensation was actually a second mutation, since the log records intent (rollback) rather than effect (mutation of shared state).

The guardrail that bounds the failure is a per-branch provenance table mapping every returned resource id to the branch that received it, combined with a pre-dispatch check that refuses non-read calls against ids present in the pre-fork snapshot unless the branch is already committed. This is a small amount of bookkeeping—a dict of `branch_id -> set(resource_id)` and a snapshot set—but it must be threaded through every tool dispatch, and it must be consulted before the call, not after.

from dataclasses import dataclass, field
from typing import Any


class IrreversibleRollbackError(RuntimeError):
    """Raised when a branch's rollback would mutate a shared pre-existing resource
    or delete a resource owned by a sibling branch, converting rollback into a
    second mutation rather than a restoration."""
    pass


@dataclass
class BranchProvenance:
    branch_id: str
    created_ids: set[str] = field(default_factory=set)
    touched_shared_ids: set[str] = field(default_factory=set)


class SpeculativeRollbackGuard:
    def __init__(self, pre_fork_snapshot: set[str]):
        self.pre_fork_snapshot = set(pre_fork_snapshot)
        self.provenance: dict[str, BranchProvenance] = {}

    def register_branch(self, branch_id: str) -> None:
        self.provenance[branch_id] = BranchProvenance(branch_id=branch_id)

    def check_dispatch(self, branch_id: str, tool_name: str, args: dict[str, Any],
                       is_read: bool, committed: bool) -> None:
        prov = self.provenance[branch_id]
        for key, value in args.items():
            if not isinstance(value, str):
                continue
            if value in self.pre_fork_snapshot and not is_read and not committed:
                prov.touched_shared_ids.add(value)
                raise IrreversibleRollbackError(
                    f"branch {branch_id} attempted non-read {tool_name} on shared "
                    f"pre-existing resource {value!r} (arg {key}); branch must be "
                    f"committed before dispatch or the call must be deferred"
                )

    def record_created(self, branch_id: str, returned_id: str) -> None:
        self.provenance[branch_id].created_ids.add(returned_id)

    def check_rollback(self, branch_id: str, rollback_target_id: str) -> None:
        prov = self.provenance[branch_id]
        if rollback_target_id in prov.touched_shared_ids:
            raise IrreversibleRollbackError(
                f"rollback of {branch_id} would mutate shared resource "
                f"{rollback_target_id!r}; compensation is a second mutation, not a restoration"
            )
        for other_id, other in self.provenance.items():
            if other_id == branch_id:
                continue
            if rollback_target_id in other.created_ids:
                raise IrreversibleRollbackError(
                    f"rollback of {branch_id} targets {rollback_target_id!r}, which was "
                    f"returned to sibling branch {other_id!r} (likely tool-layer dedup); "
                    f"deleting it would destroy the sibling's dependency"
                )