# Under what conditions must an instance refuse speculative commit when parallel branch agents share a mutable tool substrate **Date of Record:** 2026 November 27 **Scope:** Applies to orchestrators that fan out a single user task into N parallel reasoning branches (each an LLM agent instance with tool access), optimistically commit the first branch to reach a terminal verdict, and roll back the others on disagreement detection. Does not apply to read-only fan-out (map-reduce summarization, ensemble voting over pure text) where no branch mutates external state. ## Core Distinction Speculative multi-agent execution is safe only when branch side effects are *isolated* or *idempotent-and-ordered*. The failure mode this record addresses is subtler than "branches write conflicting values." It is: **a losing branch mutates a shared tool substrate in a way that the winning branch's committed state does not overwrite, because the winner never touched that key.** Optimistic commit assumes rollback = discard the loser's *proposed* state. It does not roll back the loser's *already-emitted* side effects against a substrate the winner's commit does not span. The invariant that must hold for optimistic commit to be sound: > For every branch `b` that may be rolled back, the set of substrate keys mutated by `b` must be a subset of the keys rewritten by the committed branch's state-application step, OR every mutation must be reversible from a captured pre-image. When branches call tools whose effects are *partial* (append to a log, increment a counter, create a resource with a server-assigned ID), the subset condition fails silently. Rollback discards the loser's in-memory proposal but leaves the append, the increment, or the orphaned resource in the substrate. The commit proceeds, the disagreement is "resolved," and the system carries a corrupted state that no branch intended. ## What Was Observed A triage orchestrator spawned three branches (`A`, `B`, `C`) to resolve a customer incident. Each branch had access to `issue_tracker.append_comment`, `metrics.query`, and `inventory.reserve_sku`. Branches ran concurrently; the orchestrator committed the first to return a `RESOLVED` verdict and rolled back the rest. Trace: 1. Branch `A` and branch `B` both diagnosed a stock shortage and both called `inventory.reserve_sku(sku, qty=1)`. The tool is *not* idempotent — it decrements available count and returns a reservation ID. 2. Branch `A` returned `RESOLVED` at t=4.1s. Orchestrator committed `A`, initiated rollback of `B` and `C`. 3. Rollback of `B` discarded `B`'s proposed final state (its comment draft, its reservation ID in the branch-local store). It did **not** call `inventory.release_reservation` — the orchestrator had no model of *which tools mutate*; it assumed all branch state lived in the branch's context object. 4. Branch `B` finished at t=4.6s, after rollback was already marked complete. Its `reserve_sku` call had already landed at the inventory service at t=3.9s. The reservation was orphaned: counted against stock, referenced by no committed record. 5. `A`'s commit wrote the issue comment and the final status, but never touched inventory — `A` believed its own reservation was *the* reservation. Net effect: stock decremented twice, one reservation unreferenced, reconciliation job flagged a phantom shortage 40 minutes later. The disagreement detector never fired because there was no *semantic* disagreement — both branches agreed on the diagnosis. The corruption came from agreement, not conflict. This is the non-obvious part: **rollback-on-disagreement logic does not protect you when branches agree but both mutate.** ## Condition of Validity Optimistic commit with rollback is valid only when all of the following hold for every branch: - **Mutation manifest is known before fan-out.** The orchestrator must have, per tool, a classification of `PURE | IDEMPOTENT | NON_IDEMPOTENT_MUTATING` and, for mutating tools, a `compensating_action` (release, delete, decrement-inverse) or a `pre_image` capture hook. - **Every NON_IDEMPOTENT_MUTATING call is gated behind a branch-scoped reservation token** that the substrate enforces, so that two branches cannot both decrement the same counter without one detecting the other. Without this, concurrent branches race on the substrate and the loser's effect is invisible to rollback. - **Commit is a two-phase operation:** (1) freeze all branches (stop accepting new tool calls), (2) apply winner state and *reconcile* loser side effects via compensating actions, in that order. A commit that fires before all branches are frozen cannot know the full mutation set. - **The rollback path is exercised against the actual substrate, not the branch context.** A rollback that only clears in-memory proposals is a no-op against a stateful tool. If the tool substrate cannot supply compensating actions (e.g., an external API that only supports `POST` with no `DELETE`), optimistic commit is **unsound** and the orchestrator must serialize mutating branches or run them against a sandboxed shadow substrate and apply the winner's diff to the real one. ## Decision Boundary The orchestrator must transition to **serialized mutating execution** (or shadow-substrate execution) when any of these is true at fan-out time: - Any tool in the branch's allowed set is classified `NON_IDEMPOTENT_MUTATING` and lacks a compensating action. - The task description implies a resource-allocation or counter-decrement semantic ("reserve", "purchase", "claim", "assign") — these are non-idempotent by nature even if the tool looks idempotent. - Branch count × expected mutating calls exceeds the substrate's concurrency-safe write budget (e.g., a single-writer counter). - The disagreement detector is *semantic only* (compares verdicts) rather than *effect-based* (compares mutation sets). Semantic agreement does not imply effect agreement. It may proceed with optimistic commit when all mutating tools are idempotent keyed writes (`PUT key=value`) so that loser writes are overwritten by winner writes on the same key, and the winner's commit spans the full key set the branches could touch. ## Operational Effect The practical consequence: **agreement is not a safety signal in speculative execution; only effect-set containment is.** Teams that instrument disagreement detection and see "0 conflicts" conclude the fan-out is safe, when in fact the branches agreed precisely *because* they all took the same mutating action — the worst case for rollback, since the loser's effect is a duplicate the winner never overwrites. The guardrail is a pre-fan-out static analysis of the tool manifest plus a runtime gate that freezes branches before commit and reconciles compensating actions against the real substrate. Where compensating actions are unavailable, the orchestrator must not speculate on that tool — it must serialize, even at latency cost. The 40-minute reconciliation lag in the observed case is the cheap version; a duplicate `reserve_sku` on a scarce SKU is the expensive one, and no amount of post-hoc disagreement voting recovers it. ```python from dataclasses import dataclass, field from enum import Enum from typing import Callable class ToolClass(Enum): PURE = "pure" IDEMPOTENT = "idempotent" NON_IDEMPOTENT_MUTATING = "non_idempotent_mutating" class UnsoundSpeculationError(RuntimeError): """Raised when optimistic commit is attempted over a substrate that cannot be rolled back.""" @dataclass(frozen=True) class ToolSpec: name: str cls: ToolClass compensating_action: Callable[[dict], None] | None = None @dataclass class Branch: branch_id: str mutations: list[tuple[str, dict]] = field(default_factory=list) # (tool, args) def assert_speculation_sound(tools: list[ToolSpec], branches: list[Branch]) -> None: """Pre-fan-out gate: refuse optimistic commit if any branch can mutate un-reconcilably.""" mutating = {t.name: t for t in tools if t.cls is ToolClass.NON_IDEMPOTENT_MUTATING} for b in branches: for tool_name, _args in b.mutations: spec = mutating.get(tool_name) if spec is None: continue if spec.compensating_action is None: raise UnsoundSpeculationError( f"branch {b.branch_id} invoked non-idempotent tool {tool_name!r} " f"with no compensating action; rollback cannot reconcile its side effect. " f"Serialize this branch or run it against a shadow substrate." ) def reconcile_losers(committed: Branch, losers: list[Branch], tools: list[ToolSpec]) -> None: """Two-phase commit step 2: apply compensating actions for every loser mutation.""" by_name = {t.name: t for t in tools} for loser in losers: for tool_name, args in loser.mutations: spec = by_name[tool_name] if spec.cls is ToolClass.NON_IDEMPOTENT_MUTATING: if spec.compensating_action is None: raise UnsoundSpeculationError( f"cannot reconcile {tool_name!r} from branch {loser.branch_id}" ) spec.compensating_action(args) ```