# When must an instance abort a speculative branch: The Irreversible External Side-Effect Invariant in Multi-Agent Rollback **Date of Record:** 2026 November 14 **Scope:** Applies to speculative multi-agent execution systems where branches may be rolled back and merged optimistically, and where agents can perform external actions (API calls, writes, messages, payments, resource reservations). Does not apply to purely functional or fully sandboxed execution environments where every side effect is staged and committed atomically. ## Core Distinction The distinction that matters is between *reversible state mutation* and *irreversible external commitment*. A branch-and-merge graph with optimistic concurrency assumes that any speculative branch can be discarded if it loses the merge race or is invalidated by a conflicting observation. This assumption holds for internal state (memory, scratchpads, staged diffs) but silently fails for external side effects. The non-obvious boundary is this: **rollback of a branch does not roll back effects already emitted to systems outside the transaction boundary.** A branch that has crossed the externalization threshold is no longer speculative, regardless of what the orchestrator's bookkeeping believes. The subtlety is that the threshold is not "the agent called an external tool." It is "the external system has durably accepted the effect in a way that cannot be compensated by a later authoritative action." Most systems conflate these and thereby over- or under-constrain rollback. ## What Was Observed In a task graph where a planner fans out three speculative branches (A, B, C) to explore solution strategies, each branch was permitted to call a downstream provisioning API to reserve capacity, verify feasibility, and record a tentative allocation. Branch B lost the merge race to branch A. The orchestrator rolled back B: it discarded B's staged diff, freed its in-memory context, and marked the branch `rolled_back`. The provisioning API, however, had already durably recorded B's reservation and emitted a confirmation webhook to a third-party consumer. Rollback did not retract the reservation. The third party acted on it. The final committed state (branch A) and the externally observed state (branch B's reservation) diverged, and reconciliation required a manual compensating transaction that the system had no primitive to express. A second, more insidious case: two branches A and C both performed an idempotent-looking "verify account" call. The call was idempotent on the provider side, so no divergence. But a third branch D performed a non-idempotent "issue credit" call that the orchestrator classified as safe-to-rollback because it was wrapped in a retryable client. The retry semantics were provider-side idempotency keys scoped to the *client request*, not to the *branch*, so a replayed branch after a partial failure issued a second credit. The orchestrator's rollback model had no concept of a branch-scoped idempotency key. The invariant violated in both cases: **a branch may only be treated as rollback-eligible while all of its effects remain within the transaction boundary.** Once an effect is externalized and durably accepted, the branch must be promoted to a *committed-or-compensating* state, not a *discardable* state. ## Condition of Validity The optimistic rollback model is valid under the following jointly necessary conditions: 1. **Effect containment.** Every side effect produced by a speculative branch is either (a) staged and not yet externalized, or (b) externalized under a branch-scoped idempotency key that the orchestrator can use to revoke or compensate. 2. **Compensation availability.** For any externalized effect, a compensating action exists that is itself idempotent and can be invoked after a merge decision without requiring the branch to remain live. 3. **Observation ordering.** No external consumer can act on a branch's effect before the merge decision is durably recorded. Where this cannot be guaranteed, the branch must not externalize before merge. 4. **No cross-branch aliasing of non-idempotent effects.** Two branches must never share an idempotency scope such that rollback of one silently retracts or duplicates the other's effect. When any condition fails, the branch is not rollback-eligible in the optimistic sense; it is at best compensable, and the orchestrator must switch from *discard* semantics to *compensate* semantics. ## Decision Boundary The decision the orchestrator must make at each branch checkpoint is binary and must be made *before* the effect externalizes, not after: - If the effect is staged-only or carries a branch-scoped, revocable idempotency key **and** a compensation path is registered, the branch remains rollback-eligible. Proceed optimistically. - If the effect is externalized without a revocable key, or if a consumer may observe it before merge, the branch must be **fenced**: it may no longer be discarded, only committed or compensated. The orchestrator must treat a losing fenced branch as a *compensation obligation*, not a *rollback*. The boundary is crossed at the moment of durable external acceptance, and the orchestrator must record that crossing synchronously with the external call, not inferred later from logs. Inferring it after the fact is the failure mode: the merge decision races the externalization and the loser is rolled back while its effect lives on. ## Operational Effect Enforcing the invariant changes the execution model in three concrete ways. First, branch state gains a third terminal status: `fenced` (committed-or-compensating), distinct from `committed` and `rolled_back`. Second, the orchestrator must maintain a compensation registry keyed by branch, populated at externalization time, so that a losing fenced branch triggers compensation rather than discard. Third, merge decisions must be serialized against externalization for any branch that cannot satisfy containment, which reduces parallelism precisely for the branches that touch the outside world — the correct trade, because those are the branches whose rollback is a lie. The operational lesson: rollback is a statement about *internal* state only. Any system that advertises branch rollback while allowing unkeyed external effects is advertising a capability it does not have, and the divergence surfaces later as an unreconcilable external state attributed, incorrectly, to a "race condition." ```python class IrreversibleSideEffectError(RuntimeError): """Raised when a speculative branch attempts rollback after externalizing an effect that cannot be revoked or compensated within the transaction.""" pass class BranchState: STAGED = "staged" EXTERNALIZED = "externalized" FENCED = "fenced" COMMITTED = "committed" ROLLED_BACK = "rolled_back" def rollback_branch(branch, compensation_registry): """Attempt to discard a speculative branch. A branch is only discardable while every effect it produced remains inside the transaction boundary (staged, or externalized under a revocable branch-scoped key with a registered compensation). """ if branch.state == BranchState.STAGED: branch.state = BranchState.ROLLED_BACK return if branch.state == BranchState.EXTERNALIZED: compensation = compensation_registry.get(branch.id) if compensation is None or not compensation.is_idempotent: # Effect crossed the boundary with no revocable path. branch.state = BranchState.FENCED raise IrreversibleSideEffectError( f"branch {branch.id!r} externalized an effect without a " f"revocable idempotency key or registered compensation; " f"it cannot be rolled back, only compensated" ) compensation.invoke() branch.state = BranchState.ROLLED_BACK return if branch.state == BranchState.FENCED: raise IrreversibleSideEffectError( f"branch {branch.id!r} is already fenced; rollback is not a " f"valid transition, invoke compensation instead" ) raise IrreversibleSideEffectError( f"branch {branch.id!r} in state {branch.state!r} is not rollback-eligible" ) ```