# Under what conditions must an instance treat a merged speculative branch as uncommitted until its causal parent's effect log is durably replayed **Date of Record:** 2026 November 26 **Scope:** Applies to orchestrators that run speculative branch-and-merge task trees (multiple agent instances execute divergent sub-plans concurrently, then merge results and roll back losers). Does not apply to single-threaded linear agent loops or to branches whose effects are purely read-only/idempotent with no shared external state. ## Core Distinction The distinction that matters is between **result merge** and **effect commit**. A branch-and-merge agent tree can merge a winning branch's *output artifact* (a plan, a patch, a tool-call result) long before that branch's *effect log* — the ordered record of side-effecting tool calls it issued — has been reconciled against the effects of branches that were rolled back. Most orchestrators conflate the two: they treat "branch B's result was selected" as equivalent to "branch B's effects are the canonical future." This is false whenever a rolled-back sibling branch already emitted irreversible or partially-applied effects *before* the merge decision was made. The invariant: **a merged branch is speculative, not committed, until every effect it depends on has been durably replayed in causal order and every sibling effect it collided with has been proven rolled back or compensated.** Merge is a *selection* event, not a *commit* event. The gap between them is where the failure lives. ## What Was Observed A three-way speculative tree: a root planner instance spawns branches A, B, C to solve a migration sub-task. Each branch runs an agent loop with tool access (a schema-migration tool, a file writer, a queue producer). Branch A emits `queue.produce(compaction_job)` early, then later fails verification and is rolled back. Branch B wins the merge. The orchestrator merges B's result artifact, marks B "committed," and discards A's effect log — because A "lost." The failure trace: 1. `t0` — A calls `queue.produce(compaction_job)`; the produce is acked but the consumer has not yet dequeued it. 2. `t1` — A fails verification; orchestrator rolls A back by truncating A's *context* and dropping A's *result*. The already-acked queue message is not recalled — the orchestrator's rollback primitive only knows about context, not external side effects. 3. `t2` — B wins; orchestrator merges B's artifact and marks B committed. B's effect log is replayed. 4. `t3` — the compaction consumer dequeues A's orphaned job and mutates shared state that B's replayed effects assumed pristine. B's committed result is now applied *on top of* a rolled-back branch's effect. 5. `t4` — divergence: the merged tree's state no longer corresponds to any branch's causal history. Verification replay of B alone passes; verification replay of the merged tree fails, because the replay cannot see A's orphaned produce. The non-obvious part: **B's own verification passed.** The corruption is invisible to per-branch verification because it originates in a branch that was declared dead. The merge invariant was violated not by B but by the rollback of A being *incomplete* — rollback truncated state but did not compensate effects. ## Condition of Validity Merge-as-commit is only valid under the conjunction: - **Effect isolation:** every branch's side-effecting tool calls are staged (buffered, sandboxed, or two-phase) and not externally visible until commit. If any tool call is acked-and-visible before merge, isolation is broken. - **Rollback completeness:** rollback of a losing branch compensates *or* proves absence of every effect it emitted, not merely discards its context/result. - **Causal replay ordering:** the winning branch's effect log is replayed in causal order *after* all sibling compensations are durably confirmed, and the replay is idempotent against partial prior application. - **Merge/commit separation:** the orchestrator carries an explicit `speculative → committed` state flag per branch, and downstream consumers refuse to act on `speculative` effects. When any conjunct fails — most commonly effect isolation, because agent tools are frequently non-transactional — the merged branch must be treated as uncommitted and its effects held behind a barrier until the parent's effect log is durably replayed and sibling compensations confirmed. ## Decision Boundary The orchestrator must cross from "merge selected" to "commit" only when: - the winning branch's full effect log has been replayed against a state snapshot that already includes all losing-branch compensations, **and** - every losing branch's effect log has reached terminal state `compensated` or `proven-absent` (not merely `discarded`), **and** - no losing-branch effect remains in-flight in any external queue/consumer. If the orchestrator cannot establish the second and third conditions — e.g. a queue produce was acked but the consumer is unobservable — it must **not** commit; it must escalate to a reconciliation pass (drain/observe the queue, replay, then commit) or abort the merge entirely. The boundary is: *discarded ≠ compensated*. Treating a discarded losing branch as compensated is the exact crossing that produces the divergence. ## Operational Effect The practical consequence is a **commit barrier** between merge and downstream effect visibility, plus a per-branch effect-log ledger that survives branch death. Losing branches must not be garbage-collected until their effect logs reach terminal compensation state. Verification must be run against the *merged causal history* (winning effects + sibling compensations), not against the winning branch in isolation — otherwise verification is structurally blind to the failure. Cost: reconciliation latency and a durable ledger; benefit: the merged tree remains replayable, and a failed merge is recoverable rather than silently corrupt. ```python from dataclasses import dataclass, field from enum import Enum from typing import Dict, List class EffectState(Enum): SPECULATIVE = "speculative" COMPENSATED = "compensated" PROVEN_ABSENT = "proven_absent" DISCARDED = "discarded" # NOT terminal-safe for merge class IncompleteRollbackError(RuntimeError): """Raised when a merge is attempted while a losing branch's effects are not terminally compensated/proven-absent.""" @dataclass class Branch: name: str effects: List[str] = field(default_factory=list) effect_state: Dict[str, EffectState] = field(default_factory=dict) in_flight: bool = False @dataclass class MergeTree: branches: Dict[str, Branch] = field(default_factory=dict) winner: str = "" def commit_merge(self) -> None: win = self.branches[self.winner] # Guard 1: winner effects must be replayable, not yet visible. for eff in win.effects: if win.effect_state.get(eff) is not EffectState.SPECULATIVE: raise IncompleteRollbackError( f"winner effect {eff!r} not in speculative state" ) # Guard 2: every losing branch must be terminally reconciled. for name, br in self.branches.items(): if name == self.winner: continue if br.in_flight: raise IncompleteRollbackError( f"losing branch {name!r} has in-flight effects; " f"cannot prove absence" ) for eff in br.effects: st = br.effect_state.get(eff, EffectState.DISCARDED) if st not in (EffectState.COMPENSATED, EffectState.PROVEN_ABSENT): raise IncompleteRollbackError( f"losing branch {name!r} effect {eff!r} is {st.value}; " f"discarded != compensated, merge blocked" ) # Only now: replay winner effects against compensated snapshot. for eff in win.effects: win.effect_state[eff] = EffectState.COMPENSATED # committed ```