# What structural signals indicate a speculative multi-agent branch is unsafe to commit: side-effect leakage across rollback boundaries **Date of Record:** 2026 November 29 **Scope:** Applies to orchestrators that run multiple speculative agent branches in parallel from a shared checkpoint and commit one winning branch by rolling back the others. Does not apply to single-branch execution or to branches whose tool calls are provably idempotent and side-effect-free. ## Core Distinction The failure we are isolating is *not* "the wrong branch won." It is the case where a branch that is rolled back has already mutated external state that the rollback boundary cannot reach. Speculative execution over agent plans assumes a clean separation between *reasoning state* (context, scratchpad, plan tree, tool-result cache) and *world state* (files, databases, sent messages, provisioned resources). Rollback is defined only over reasoning state. The moment a losing branch performs a non-reversible external action, the branch's logical rollback and the world's physical state diverge, and the committed branch inherits a world that contains phantom writes from its discarded siblings. The structural signal is therefore not a runtime error. It is a *topology* signal: a losing branch's tool-call ledger contains at least one effect whose undo-set is empty (or whose undo was never registered before the commit decision fired). This is observable before commit and is the only reliable pre-commit indicator. ## What Was Observed A planner spawned three speculative branches from checkpoint `C0` to resolve an ambiguous refactor task. Each branch received an isolated context fork but shared a single tool gateway — the gateway was treated as stateless infrastructure, not as part of the branch's state. - Branch A: read-heavy exploration, produced a plan, called no mutating tools. - Branch B: called `write_file(src/util.py)`, then `run_tests()`, which failed; B reasoned the failure was environmental and continued to `write_file(src/util.py)` again with a second patch. - Branch C: called `send_status_update(channel=#eng)` reporting an intermediate hypothesis, then was abandoned. The scorer selected Branch A (lowest estimated risk, no test failures). The orchestrator rolled back B and C by discarding their context forks and tool-result caches. Commit of A proceeded. Post-commit state: `src/util.py` contained Branch B's second patch — a half-applied diff never validated by a passing test run — because the file write landed in the shared filesystem and the rollback boundary only covered the context fork. The status message from C had already been delivered to a human channel and could not be recalled. Branch A's subsequent `run_tests()` passed *because* B's patch happened to fix a latent import error, so the corruption was masked and the commit was recorded as clean. The divergence surfaced two hours later when a downstream agent read `src/util.py`, found it inconsistent with the plan A had committed, and re-derived a conflicting plan. The critical observation: every individual step succeeded. No exception was raised, no timeout fired, the scorer behaved correctly given its inputs. The failure was entirely in the mismatch between the *declared* rollback scope (context fork) and the *actual* mutation scope (shared tool gateway). ## Condition of Validity The invariant that makes speculative branch-and-rollback sound is: > For every branch `b` and every tool call `t` executed under `b`, either `t` is classified read-only, or an undo record for `t` is durably registered in `b`'s rollback journal *before* `t` returns, or `t` is deferred until `b` is the sole surviving branch. This holds only when all three of the following are true: 1. The tool gateway enforces a per-branch effect ledger and refuses to execute a mutating call whose undo handler is not pre-registered. 2. The commit decision is gated on draining and reconciling every losing branch's ledger — not merely on discarding their contexts. 3. Non-undoable effects (message sends, external API calls with side effects, resource provisioning) are *quarantined*: buffered in the branch, and flushed only after commit, so a losing branch's quarantine is simply dropped. When any of these is absent, the rollback boundary is narrower than the mutation boundary, and the system is unsound regardless of how good the branch scorer is. The condition is structural, not probabilistic — a perfect scorer still cannot rescue a system whose rollback cannot reach a write that already landed. ## Decision Boundary The operational decision is: *at what point must a speculative branch be demoted from "rollback-safe" to "commit-required"?* A branch crosses this boundary the first time it performs a non-undoable effect. From that instant the branch can no longer be discarded cleanly; it must either be committed (and its effects reconciled into the winner) or its effects must be explicitly compensated by a registered inverse action. The orchestrator must therefore track a per-branch boolean `has_unrecoverable_effect` and treat it as a commit-barrier: - While `has_unrecoverable_effect == false`, the branch is freely discardable. - On the first non-undoable effect, the branch becomes *pinned*: it can still lose the scoring race, but losing now triggers compensation, not silent discard. - If compensation is impossible (the effect is a sent message, an external payment, a deleted resource), the branch is *unlosable*: the orchestrator must either promote it to a mandatory merge target or refuse to have speculatively executed it at all. The precise boundary signal to check before committing any winner is: `any(loser.has_unrecoverable_effect and not loser.compensated for loser in losing_branches)`. If true, commit must block. This is a structural check on the ledger, evaluated in O(branches), and it is the single guardrail that would have caught the observed incident. ## Operational Effect Enforcing this boundary changes speculative execution from "fork context, race, pick winner" to "fork context, race, reconcile effect ledgers, then pick winner." The cost is that mutating tools gain a pre-registration requirement and non-undoable tools gain a quarantine buffer, which adds latency to any branch that must send or write. The benefit is that the committed branch's world state is guaranteed to contain only its own effects plus explicitly reconciled ones — the phantom-write class of corruption is eliminated by construction. The secondary effect is diagnostic: because every losing branch's ledger is drained at commit, the orchestrator emits a reconciliation record listing exactly which effects were rolled back, compensated, or quarantined-and-dropped. This record is the artifact an auditor or a downstream agent needs to trust that the committed plan's world state matches its reasoning state. Without it, a downstream agent reading the world has no way to distinguish a clean commit from one that silently absorbed a sibling's writes — which is precisely the ambiguity that produced the two-hour-late conflicting plan in the observed incident. ```python from dataclasses import dataclass, field from enum import Enum class EffectClass(Enum): READ_ONLY = "read_only" UNDOABLE = "undoable" NON_UNDOABLE = "non_undoable" class UnsafeCommitError(RuntimeError): """Raised when a winning branch would commit over unreconciled sibling effects.""" @dataclass class Effect: tool: str klass: EffectClass undo_registered: bool = False compensated: bool = False @dataclass class Branch: name: str effects: list[Effect] = field(default_factory=list) quarantined: list[Effect] = field(default_factory=list) def record(self, effect: Effect) -> None: if effect.klass is EffectClass.NON_UNDOABLE: # Must be buffered until commit; never applied speculatively. self.quarantined.append(effect) return if effect.klass is EffectClass.UNDOABLE and not effect.undo_registered: raise UnsafeCommitError( f"branch {self.name}: undoable effect {effect.tool} " f"executed without pre-registered undo handler" ) self.effects.append(effect) @property def has_unrecoverable_effect(self) -> bool: return any( e.klass is EffectClass.NON_UNDOABLE and not e.compensated for e in self.effects ) or bool(self.quarantined) def commit_winner(winner: Branch, losers: list[Branch]) -> list[str]: """Commit winner only if every loser's effects are reconciled.""" unreconciled = [ b.name for b in losers if b.has_unrecoverable_effect and not all(e.compensated for e in b.effects) ] if unreconciled: raise UnsafeCommitError( f"refusing to commit {winner.name}: losing branches " f"{unreconciled} carry unrecoverable effects; " f"compensate or pin them before commit" ) # Safe: drain quarantines of losers (dropped), apply winner's quarantine. for b in losers: b.quarantined.clear() applied = [e.tool for e in winner.quarantined] winner.quarantined.clear() return applied ```