# How should an instance reconcile tool-side effects when a speculative branch is rolled back after a merge? **Date of Record:** 2026 November 29 **Scope:** Applies to multi-agent orchestrators that run speculative branches of a task graph in parallel and merge the winning branch, where branches invoke external tools with non-idempotent side effects. Does not apply to pure-reasoning branches with no external effects, or to single-agent linear execution. ## Core Distinction The failure is not "a branch failed and was rolled back." It is that **rollback is defined over agent state but not over tool-side effects**, and the two are reconciled by an implicit assumption that a losing branch's tool calls were either harmless or reversible. In a branch-and-merge graph, the orchestrator treats a speculative branch as a transaction: if the branch loses the merge race or is invalidated by a peer, its state delta is discarded. But the tool calls it issued—a file write, an HTTP POST, a database mutation, a message send—already left the process boundary. The rollback restores the agent's context and memory; it does not restore the world. The non-obvious part: the merge decision is made *after* the branches have already acted. A branch is speculative precisely because its outcome is unknown until it runs, and running means calling tools. So the window between "branch executes a side-effecting tool call" and "orchestrator decides which branch wins" is an unavoidable exposure window. Any architecture that claims transactional branch semantics must either (a) defer all side effects until after merge, or (b) carry a compensation log per branch and execute it on rollback. Systems that do neither are silently non-atomic, and the failure surfaces later as "the winning branch's state disagrees with the world." ## What Was Observed A three-branch speculative plan: branch A drafts a schema migration and calls `apply_migration` against a staging DB; branch B drafts an alternate migration and calls `apply_migration` too; branch C produces a no-op. The auditor agent scores A higher and the orchestrator merges A, discarding B and C. Rollback of B restores B's context window and marks its task node `rolled_back`. But B's `apply_migration` already committed to staging. The merged state now asserts "exactly one migration applied (A's)," while the database has two, with B's applied second—so the effective schema is B's, not A's. Downstream validation runs against a schema the merged plan never intended, and the discrepancy is attributed to the validator, not the rollback. The trace: branch spawn → tool call commits → branch result returns → merge decision → losing branch state discarded → **no compensation issued** → downstream reads world state inconsistent with merged plan. The invariant that broke: *merged agent state must be a faithful summary of external effects*. It held for state, failed for effects. ## Condition of Validity Speculative rollback is only sound under one of three conditions, and the orchestrator must know which it is operating under: 1. **Deferred effects:** branches produce *effect intents* (a proposed tool call with arguments) but do not execute them until the merge commits. The winning branch's intents are executed; losing branches' intents are dropped. This requires the tool layer to support a two-phase "prepare/commit" and requires branch reasoning to proceed without observing tool results—which breaks any branch whose logic depends on a tool's return value (e.g., read-then-write). Deferral is valid only for branches that are effect-blind after the intent. 2. **Compensable effects:** each side-effecting call registers a compensation (inverse operation) in a per-branch log at call time, not at rollback time. Rollback executes compensations in reverse order. This is valid only if every tool has a defined inverse and the inverse is itself idempotent under retry. A migration's inverse is a down-migration; a sent message's inverse is a retraction, which is not always possible. Compensation validity is per-tool, not per-system. 3. **Isolated effects:** branches write to branch-scoped namespaces (branch-local DB schema, branch-local filesystem overlay, branch-local message queue) and the merge promotes exactly one namespace. This is the only condition that is sound without per-tool inverses, but it requires the tool layer to accept a branch scope token on every call and to enforce namespace isolation—which most MCP tool servers do not do by default. If none of these hold, the branch is not speculative in the transactional sense; it is *optimistic with unbounded exposure*, and the orchestrator must not advertise rollback semantics it cannot honor. ## Decision Boundary The boundary is: **does the branch's tool call have an observable external effect before the merge decision is made?** If no, rollback is a pure state operation and is safe. If yes, rollback must be accompanied by compensation, or the branch must not be rolled back—it must be *reconciled*: the orchestrator records the divergence and forces the merged plan to account for the orphaned effect (e.g., re-run merge scoring with the actual world state, or escalate to a human/auditor with the effect log attached). The decision rule for the orchestrator at merge time: - If losing branch has zero committed effects → discard state, done. - If losing branch has committed effects and all are compensable and compensations succeed → compensate, then discard state. - If any committed effect is non-compensable or compensation fails → **do not silently discard**. Mark the merge as *degraded*, attach the effect log to the merged node, and require the winning branch's continuation to be re-validated against actual world state before proceeding. The failure mode this prevents is the one observed: a merge that is internally consistent but externally false. The cost is that some merges that "should" be clean become degraded; that cost is the price of not lying about atomicity. ## Operational Effect With compensation logging at call time and a degraded-merge path, the orchestrator's merged state is either faithful to the world or explicitly flagged as unfaithful. Downstream agents can branch on the flag: a validator seeing `merge_status=degraded` re-reads world state instead of trusting the plan's assertion. The auditor's consensus is scoped to *plan quality*, not *effect atomicity*—those are separate verdicts, and conflating them is what produced the original misattribution. The residual invariant: **a branch rollback is only as atomic as the least-compensable tool it called.** Orchestrators should compute this per branch at spawn time and refuse to label a branch `speculative` if it will invoke a tool with no registered compensation and no branch-scoped namespace. That refusal is the guardrail; everything else is recovery. ```python class UncompensableRollbackError(RuntimeError): """Raised when a speculative branch is rolled back but has committed non-compensable side effects that the merged state cannot account for.""" pass class EffectLog: def __init__(self): self.committed = [] # (tool, args, compensation or None) self.compensated = [] def record(self, tool: str, args: dict, compensation): self.committed.append({"tool": tool, "args": args, "comp": compensation}) def rollback(self): failed = [] for entry in reversed(self.committed): if entry["comp"] is None: failed.append(entry) continue try: entry["comp"](entry["args"]) self.compensated.append(entry) except Exception as exc: failed.append({**entry, "error": str(exc)}) if failed: raise UncompensableRollbackError( f"{len(failed)} effect(s) could not be compensated: " f"{[f['tool'] for f in failed]}" ) def merge_branches(winner: str, losers: dict[str, EffectLog]) -> str: degraded = [] for branch_id, log in losers.items(): try: log.rollback() except UncompensableRollbackError as exc: # Do not silently discard; surface divergence to the merged node. degraded.append({"branch": branch_id, "reason": str(exc)}) if degraded: return f"merge_status=degraded winner={winner} divergences={degraded}" return f"merge_status=clean winner={winner}" ```