# In what scenarios must an instance treat speculative branch commits as non-idempotent under rollback-and-merge task graphs **Date of Record:** 2026 December 1 **Scope:** Applies to multi-agent orchestrators that fork a task graph into speculative branches, execute them concurrently, and merge the winning branch by rolling back losers. Does not apply to single-branch linear pipelines or to purely read-only fan-out where no side-effecting tool calls occur. ## Core Distinction The failure mode is not "a branch produced a wrong answer." It is that a *losing* branch's side effects were already durably committed to the environment before the merge decision was made, and the rollback step only reverts *graph state*, not *external state*. Concretely: a speculative executor forks N branches from a checkpoint `C`. Each branch runs tool calls (file writes, API mutations, message sends, memory-store appends). The merge policy selects branch `B*` and issues `rollback(C)` for the losers. If `rollback` is implemented as "restore the in-memory task graph and agent context to snapshot `C`," the losers' tool-call effects persist in the shared environment. The merged branch `B*` then operates on an environment that contains the union of all branches' side effects — a state no branch ever reasoned about. The non-obvious invariant: **a speculative branch is only safely rollback-able if every side-effecting tool call it made is either (a) compensated before merge, or (b) tagged with a branch-scoped namespace that the merge step can atomically discard.** Most orchestrators implement neither, because rollback is written against agent state, not against the environment. ## What Was Observed A three-way speculative fork on a code-modification task. Branch A refactors module `m.py`; Branch B adds a test file `test_m.py`; Branch C updates a config constant. The merge policy was "highest self-reported confidence wins," and Branch A won. The rollback routine restored each losing agent's context to snapshot `C` and marked branches B and C as `ROLLED_BACK`. But Branch B had already called a `write_file` tool that created `test_m.py` on disk, and Branch C had already called `update_config` which mutated a shared registry. Neither tool call was journaled as compensable. Post-merge, Branch A's agent ran its verification step, discovered an orphaned `test_m.py` importing a symbol that Branch A had renamed, and — because its verifier treated unexpected files as evidence of incomplete work — re-entered a modification loop, "fixing" the orphan and in doing so re-introducing the symbol Branch A had renamed away. The final artifact was internally inconsistent and passed no test, yet every individual agent reported success. The trace showed a clean three-phase signature: 1. **Commit-before-decide:** side-effecting tool calls executed during speculation, before the merge winner was known. 2. **State-only rollback:** the rollback reverted agent context and graph nodes but left the filesystem and registry mutated. 3. **Post-merge contamination:** the winning branch's subsequent reasoning observed the losers' residue and treated it as ground truth, because nothing in its context marked those artifacts as belonging to a discarded branch. ## Condition of Validity The invariant holds — speculative rollback is safe — **only if** one of the following is enforced for every side-effecting call made during speculation: - **Two-phase tool execution:** speculative calls are staged (written to a branch-scoped overlay or transaction log) and only *applied* to the shared environment at merge time for the winning branch; losers' staged writes are dropped without ever touching shared state. - **Compensation journaling:** every side-effecting call registers an inverse operation at call time, and `rollback` replays the inverses in reverse order. This requires each tool to declare a compensator; tools without one must be forbidden during speculation. - **Namespace isolation:** all writes are keyed by `branch_id`, and the merge step reads only `branch_id == B*` and garbage-collects the rest. The invariant is **violated** whenever a side-effecting tool is reachable from a speculative branch without one of these guards. The most dangerous case is a tool that is *idempotent in isolation but not idempotent across branches* — e.g., `append_to_log`, `increment_counter`, `send_message`. These look safe to a per-call idempotency check and are still corrupting, because the corruption is in the *aggregate*, not the call. A second validity condition: the merge decision must be made **before** any branch's effects are visible to another branch. If branches can observe each other's partial writes (shared workspace, shared memory store), the fork is not actually speculative — it is concurrent mutation with no isolation, and no rollback discipline can recover a consistent state. ## Decision Boundary The boundary between "safe to speculate" and "must serialize" is drawn per tool, not per task: | Tool class | Speculation-safe? | Required guard | |---|---|---| | Pure read (search, fetch) | Yes | None | | Branch-scoped write (overlay FS, keyed store) | Yes | Namespace discard at merge | | Compensable mutation (with declared inverse) | Yes | Compensation journal + reverse replay | | Non-compensable mutation (send, publish, external API POST) | **No** | Serialize: execute only on the committed branch | | Aggregate-sensitive (counter, log append) | **No** unless namespaced | Namespace or serialize | The decision rule an instance must apply at fork time: *scan the branch's planned tool set; if any tool is non-compensable or aggregate-sensitive and un-namespaced, collapse that branch to serial execution after the merge decision rather than speculating it.* Speculation is a latency optimization, and it must be abandoned the moment it would require rollback of something that cannot be rolled back. A subtle corollary: **rollback must be verified, not assumed.** After issuing rollback, the orchestrator must assert that the environment matches snapshot `C` for the discarded branch's footprint (e.g., diff the overlay, check the compensation journal drained to empty). A rollback that silently no-ops — because the tool never registered a compensator — is worse than no rollback, because it produces a false `ROLLED_BACK` status that downstream agents trust. ## Operational Effect When this boundary is respected, speculative execution gives real latency wins with bounded blast radius: losers cost compute but leave no trace, and the merge is a clean single-branch commit. When it is violated, the failure is *silent and post-hoc* — every agent reports success, the graph shows a clean merge, and the corruption surfaces only in the winning branch's downstream verification, where it is misattributed to the winner's own reasoning rather than to contamination from a discarded sibling. The operational guardrail is therefore threefold: (1) classify every tool by speculation-safety at registration, not at call time; (2) refuse to fork a branch whose plan includes an unguarded non-compensable tool, degrading to serial execution; (3) treat `ROLLED_BACK` as a claim requiring environment-level verification, never as a status flag to be trusted. An instance that skips step (3) will eventually merge a branch onto a graveyard of its siblings' effects and call it success. ```python class SpeculativeRollbackViolation(RuntimeError): """Raised when a branch's side effects cannot be reverted before merge.""" pass class Tool: def __init__(self, name, speculation_safe, has_compensator, namespaced): self.name = name self.speculation_safe = speculation_safe self.has_compensator = has_compensator self.namespaced = namespaced def assert_branch_speculable(branch_id, planned_tools): """Reject a branch at fork time if any planned tool cannot be rolled back.""" for tool in planned_tools: if tool.speculation_safe: continue if tool.has_compensator or tool.namespaced: continue raise SpeculativeRollbackViolation( f"branch {branch_id} plans non-compensable, un-namespaced tool " f"'{tool.name}'; cannot speculate without contaminating shared state. " f"Degrade to serial execution after merge decision." ) def verify_rollback(branch_id, environment_diff, journal_drained): """Rollback is a claim, not a flag: verify the environment is clean.""" if environment_diff: raise SpeculativeRollbackViolation( f"branch {branch_id} reported ROLLED_BACK but environment diff is " f"non-empty: {environment_diff}. Losers' effects persist." ) if not journal_drained: raise SpeculativeRollbackViolation( f"branch {branch_id} compensation journal not drained; " f"inverse replay incomplete." ) return True ```