# What happens when an instance speculatively forks agent branches whose tool side effects are not provably reversible **Date of Record:** 2026 December 1 **Scope:** Applies to orchestrators that run two or more agent branches concurrently against a shared external tool surface (filesystem writes, HTTP POSTs, message sends, DB mutations) and attempt to discard losing branches by "rolling back." Does not apply to branches whose tools are read-only or genuinely transactional with compensating actions verified by the tool itself. ## Core Distinction There is a difference between **speculative reasoning** and **speculative execution with side effects**. Speculative reasoning (forking N candidate plans, scoring them, keeping one) is safe because the discarded branches leave no trace outside the context window. Speculative *execution* forks branches that call tools, and tool calls mutate the world. The boundary failure is this: an orchestrator treats a divergent branch as "rollback-able" because it captured a checkpoint of agent *state* (memory, scratchpad, plan tree), but the tool side effects the branch already committed are not captured by that checkpoint. Rollback restores the agent's belief about the world while the world itself has moved. The instance then continues from a state snapshot that is provably inconsistent with external reality. The non-obvious part: the failure is silent. The rollback "succeeds" from the orchestrator's perspective. The divergence only surfaces later, when a surviving branch reads state that a discarded branch wrote, or when an external observer sees two conflicting writes. ## What Was Observed Setup: an orchestrator forks three branches at step 4 of a task (branch A: write config via `fs.write`, branch B: send notification via `http.post`, branch C: update ledger via `db.update`). Each branch checkpoints agent state before its first tool call. A scorer selects branch A. Branches B and C are "rolled back" by restoring their pre-fork state snapshots and marking them aborted. Failure trace: 1. Branch B's `http.post` to the notification service returned 200 and enqueued a message. Its side effect is committed and irreversible from the agent's side. 2. Rollback restores B's state to the pre-fork checkpoint. B's memory no longer records that it sent the notification. The orchestrator logs "branch B aborted cleanly." 3. Branch C's `db.update` partially applied: the write committed on the primary, but the read-your-writes replica the agent reads from has not yet caught up. C's rollback sees no evidence of its own write and concludes nothing to undo. 4. Branch A (the winner) proceeds. At step 7 it queries the ledger and reads a row that branch C wrote — a value the winning plan never intended and cannot attribute. A's reasoning diverges: it now plans around a ledger state that exists only because a discarded branch executed. 5. Downstream, the notification from B fires. The human recipient acts on a message the system believes was never sent. The invariant that broke: **a branch is only rollback-able if every tool it invoked exposes either idempotent undo or a read-back that confirms no effect was committed.** The orchestrator assumed "checkpoint exists" implied "rollback is sound." It does not. Checkpointing agent state is orthogonal to undoing tool effects. The second, subtler break: even a correct rollback of C's *intent* cannot undo C's committed write, because the write is already durable on the primary. The agent's rollback and the database's durability are on different timelines, and the agent's timeline is the one the orchestrator trusts. ## Condition of Validity Speculative execution with rollback is sound only when, for every tool in the branch's reachable tool set, at least one of the following holds and is *verified at fork time*: - The tool is declared read-only (no external mutation). - The tool is transactional and the branch holds an open transaction that the orchestrator can abort, with abort confirmed by the tool. - The tool supports a compensating action whose application is confirmed by a read-back from the same consistency domain the write committed to (not a lagging replica). - The tool is idempotent under a branch-scoped idempotency key, so a re-executed winning branch cannot double-apply. If any reachable tool fails all four, the branch is **not speculatively executable** — it must run serially after branch selection, or the fork must be restricted to the read-only subset. The orchestrator must compute this property per-branch from the actual tool manifest, not from a global "tools are safe" assumption. ## Decision Boundary The boundary is between *forking to explore* and *forking to commit*. Fork freely when the branch's tool surface is read-only or transactionally abortable. Do not fork when the branch's first mutating tool call precedes branch selection and that tool lacks verified undo. Concretely: - If `tool.reversible == False` and `tool.idempotent_key_supported == False`, the branch may not execute that tool before selection. - If a mutating tool was called and the rollback read-back is served from a consistency domain behind the write's commit domain, treat rollback as **unverified**, not successful — escalate to reconciliation, do not mark aborted. - If a discarded branch's write is later observed by a surviving branch's read, the run is **contaminated** and must be halted for reconciliation, not continued. ## Operational Effect Without this boundary, speculative execution produces three classes of silent corruption: (1) ghost side effects — notifications, payments, messages sent by branches the orchestrator believes never ran; (2) phantom reads — surviving branches reasoning over writes from discarded branches, causing plan divergence with no attributable cause; (3) replica-masked rollbacks — a branch's write "disappears" from its own read-back because the read hits a lagging replica, so rollback falsely reports clean while the write is durable on the primary. The guardrail is a per-branch reversibility precheck computed from the tool manifest, a rollback that requires a same-domain read-back to confirm absence of effect, and a contamination detector that halts the run when a surviving branch reads state traceable to a discarded branch. ```python from dataclasses import dataclass, field from enum import Enum class RollbackUnsoundError(RuntimeError): """Raised when a branch is treated as rollback-able but its tool side effects cannot be provably undone or confirmed absent.""" class ToolReversibility(Enum): READ_ONLY = "read_only" TRANSACTIONAL = "transactional" COMPENSABLE = "compensable" IDEMPOTENT = "idempotent" IRREVERSIBLE = "irreversible" @dataclass class ToolManifest: name: str reversibility: ToolReversibility read_back_consistency_domain: str | None = None write_consistency_domain: str | None = None @dataclass class Branch: branch_id: str reachable_tools: list[ToolManifest] = field(default_factory=list) mutating_calls: list[str] = field(default_factory=list) def precheck_branch_reversibility(branch: Branch) -> None: for tool in branch.reachable_tools: if tool.reversibility == ToolReversibility.IRREVERSIBLE: raise RollbackUnsoundError( f"branch {branch.branch_id} reaches irreversible tool " f"{tool.name!r}; speculative execution with rollback is unsound" ) if ( tool.reversibility == ToolReversibility.COMPENSABLE and tool.read_back_consistency_domain != tool.write_consistency_domain ): raise RollbackUnsoundError( f"branch {branch.branch_id} tool {tool.name!r} read-back domain " f"{tool.read_back_consistency_domain!r} trails write domain " f"{tool.write_consistency_domain!r}; rollback cannot be confirmed" ) def confirm_rollback(branch: Branch, observed_effect: bool) -> None: if observed_effect: raise RollbackUnsoundError( f"branch {branch.branch_id} rollback unverified: committed side " f"effect still observable after abort; halt for reconciliation" ) def detect_contamination( surviving_branch: Branch, discarded_writes: set[str], reads: set[str] ) -> None: overlap = reads & discarded_writes if overlap: raise RollbackUnsoundError( f"surviving branch {surviving_branch.branch_id} read state " f"{sorted(overlap)} written by a discarded branch; run contaminated" ) ```