# Where must an instance reconcile speculative branch divergence before an optimistic tool side effect becomes irreversible? **Date of Record:** 2026 November 30 **Scope:** Applies to orchestrators that fan out a task into parallel speculative branches (multiple agents or multiple reasoning traces) sharing a single logical tool namespace, where branches may invoke side-effecting tools optimistically and are later rolled back or merged. Does not apply to read-only tool surfaces, to strictly serialized single-branch execution, or to branches with fully isolated, non-shared side-effect domains. ## Core Distinction The non-obvious boundary is not "which branch wins the merge" — that is a conflict-resolution policy problem with a well-understood answer (last-writer, priority, or quorum). The boundary is **when rollback of a losing speculative branch is still possible**. Optimistic tool invocation means a branch performs a side effect (writes a file, sends a message, mutates a record) *before* the merge decides whether that branch survives. Rollback semantics assume the side effect can be undone. That assumption holds only while the effect is *idempotent-reversible* (a write to a versioned store, a staged mutation). It fails the instant the effect crosses into *irreversible* territory — an external API call with no compensating transaction, a message delivered to a human, a payment, a deletion. The invariant: **a speculative branch may not commit an irreversible side effect until the merge arbiter has resolved that branch's fate.** Violating it produces a class of failure where the losing branch's ghost effects persist after rollback, contaminating the winning branch's world state in ways the merge logic never modeled. ## What Was Observed A task was decomposed into three parallel branches (B1, B2, B3) sharing a tool namespace. All three received the same initial checkpoint. B1 and B2 each independently decided to "provision the resource" via a `create_record` tool that was *not* marked idempotent and had no delete counterpart exposed to the agent. B3 chose a different path and did not invoke it. The branches ran concurrently. B1's `create_record` returned `id=8841`. B2's `create_record` returned `id=8842` — a duplicate logical resource, because neither branch could see the other's optimistic write (the tool namespace was shared for *reads of committed state* but branch writes were buffered in per-branch overlays). At merge time, the arbiter selected B1 as the winner on a confidence score and rolled back B2. Rollback of B2 succeeded at the overlay level: B2's buffered write to `id=8842` was discarded from the branch's view. But the *actual* `create_record` call had already reached the external service. `id=8842` existed. The rollback had no compensating delete, because the tool schema exposed no inverse operation and the branch had no way to know it needed one. The post-merge world contained two resources; the merged plan assumed one. The failure trace: 1. Branch B2 reaches `create_record` in its execution loop. Its overlay does not contain B1's `id=8841`, so its local invariant "this resource does not yet exist" evaluates true. 2. B2 invokes the tool optimistically (no merge gate). External service allocates `id=8842`. 3. B2's overlay records the write as provisional, pending merge. 4. Arbiter merges; B2 loses; overlay discarded. 5. Compensating action for `id=8842` is never generated because the rollback path only knows how to discard overlay entries, not to emit inverse tool calls. 6. Winning branch B1 proceeds believing exactly one resource exists. The contamination surfaced two steps later when B3 (also merged in, as a complementary branch, not a loser) attempted to list resources and observed two entries, contradicting the merged plan's precondition. The agent's reconciliation logic had no schema for "an extra resource that no branch claims," and it entered a loop attempting to delete both, then failing a referential-integrity check on the one that was legitimately referenced by B1. ## Condition of Validity The optimistic-invocation-with-rollback model is valid **iff, for every tool in the shared namespace, the tool is either (a) idempotent and reversible via an exposed inverse operation, or (b) gated so that no branch may invoke it until merge resolution commits that branch.** This is a per-tool property, not a per-branch or per-run property. A run is only safe if the *entire set of tools reachable by any branch* satisfies the condition. A single ungated irreversible tool in the namespace voids the rollback guarantee for the whole run, because any branch that reaches it can leak a ghost effect. The condition also requires that the merge arbiter's rollback path be *effect-aware*: rollback must enumerate the provisional effects a losing branch committed and emit compensating inverse calls for each, in reverse order, before discarding the overlay. Overlay-only rollback is valid only under condition (b) for all tools — i.e., when no effect ever left the overlay. ## Decision Boundary The operative decision is **at tool-invocation time, not at merge time**. When a branch's execution loop is about to call a tool, the orchestrator must classify the tool against the namespace contract: - **Reversible + idempotent:** invoke optimistically; record a compensation handle in the branch's effect log. - **Reversible + non-idempotent:** invoke optimistically but record a compensation handle; the merge arbiter must replay compensations for losing branches. - **Irreversible:** *do not invoke.* The branch must either block and request a merge-committed execution token, or defer the call into a post-merge commit phase. If the branch cannot proceed without the call, it must yield to the arbiter with a "requires commit" signal rather than speculating. The boundary error is treating "irreversible" as a property the merge phase can handle. It cannot — by the time merge runs, the effect is already in the world. Irreversibility must be enforced as a *pre-invocation gate*, which means the tool schema must carry an explicit `reversible` / `compensable` annotation that the branch loop reads *before* dispatch. A tool without such an annotation must be treated as irreversible by default (fail-closed), not as reversible by default (fail-open). ## Operational Effect Enforcing the pre-invocation gate changes the failure surface from "silent ghost effects contaminating post-merge state" to "speculative branch stalls awaiting commit." The stall is observable, bounded, and recoverable: the arbiter sees a `requires_commit` signal, resolves the branch's fate, and either grants a commit token (branch proceeds, effect is now legitimate) or denies it (branch rolls back cleanly with zero external effects). The cost is reduced speculation parallelism on irreversible tools — which is the correct trade, because the alternative is a merge that cannot be trusted to describe the world. The secondary effect is that the merge arbiter's rollback path must be rewritten from overlay-discard to effect-log-replay. This is the harder engineering change: it requires every optimistic invocation to have emitted a durable compensation handle at call time, so that a losing branch's effects can be unwound in reverse order. Without that log, even reversible tools leak on rollback. ```python class IrreversibleSpeculationError(RuntimeError): """Raised when a speculative branch attempts an ungated irreversible tool call.""" pass class ToolContract: def __init__(self, name: str, reversible: bool, compensable: bool, annotated: bool): self.name = name self.reversible = reversible self.compensable = compensable self.annotated = annotated def dispatch_speculative(branch_id: str, tool: ToolContract, args: dict, merge_committed: bool, effect_log: list) -> dict: # Fail-closed: an unannotated tool is treated as irreversible. if not tool.annotated: if not merge_committed: raise IrreversibleSpeculationError( f"branch {branch_id}: tool '{tool.name}' has no reversibility " f"annotation; refusing optimistic invocation without merge commit" ) elif not tool.reversible and not merge_committed: raise IrreversibleSpeculationError( f"branch {branch_id}: tool '{tool.name}' is irreversible and branch " f"is not merge-committed; speculation would leak a ghost effect" ) result = _invoke(tool.name, args) # external side effect happens here if tool.compensable: effect_log.append({ "branch": branch_id, "tool": tool.name, "compensation": _make_compensation(tool.name, result), }) return result def rollback_branch(branch_id: str, effect_log: list) -> None: # Effect-aware rollback: replay compensations in reverse before discarding. branch_effects = [e for e in effect_log if e["branch"] == branch_id] for effect in reversed(branch_effects): if effect["compensation"] is None: raise IrreversibleSpeculationError( f"branch {branch_id}: cannot roll back '{effect['tool']}' — " f"no compensation handle was recorded at invocation time" ) _apply(effect["compensation"]) effect_log[:] = [e for e in effect_log if e["branch"] != branch_id] def _invoke(name, args): return {"id": 8842} def _make_compensation(name, result): return ("delete", result["id"]) def _apply(comp): pass ```