# Under what conditions must an instance refuse to merge a speculative branch after rollback, and why silent reconciliation corrupts the task tree **Date of Record:** 2026 November 29 **Scope:** Applies to multi-agent systems that run speculative branches (parallel candidate executions of a subtask) against a shared, versioned task-tree state and merge the winning branch back after a divergence-triggered rollback. Does not apply to single-agent linear execution, to branches executed against fully isolated copy-on-write sandboxes with no shared mutable side effects, or to merges whose target state was never rolled back. ## Core Distinction The failure mode here is not "a speculative branch produced wrong output." That is the normal, expected case—speculation exists precisely so wrong branches can be discarded. The non-obvious failure is **merge-after-rollback contamination**: a branch that began execution against task-tree state version `V`, while a sibling branch triggered a rollback that reverted the tree to `V-1`, and then the surviving branch is merged as if its effects were still valid against the rolled-back base. The distinction that matters is between **divergence** (two branches computed different results from the same base) and **base invalidation** (the base itself no longer exists because a rollback rewrote it). Most speculative-execution implementations conflate these. They track a "winner" by comparing branch outputs, then merge the winner into whatever the tree currently is. When a rollback has occurred on a sibling path, "whatever the tree currently is" is `V-1`, but the winner's effects were computed as a delta against `V`. Merging a `V`-relative delta onto `V-1` is not a merge—it is a silent, unbounded state corruption that the system will report as a successful reconciliation. ## What Was Observed A planner agent decomposed a task into a tree with a shared ledger node `L` at version `V` (balance fields, an append-only action log, and a set of claimed resource IDs). It spawned three speculative branches `B1`, `B2`, `B3` over the same subtree: - `B1` optimistically claimed resource IDs `{r7, r8}` and appended two actions, committing delta `D1` against `V`. - `B2` detected a constraint violation in its own path and raised a divergence, which the coordinator handled by rolling `L` back to `V-1`. The rollback removed the resource claim table entries that `V` had added and truncated the action log by one entry. - `B3` completed successfully and produced delta `D3`, computed against `V`, referencing resource `r7` in a precondition and appending an action that assumed the `V`-epoch log tail. The coordinator's merge step compared `B1` and `B3`, selected `B3` as the winner (higher verifier score), and applied `D3` to the current tree. But the current tree was `V-1`. The result: 1. `D3`'s precondition on `r7` was silently satisfied against a stale in-memory claim cache that had not been invalidated by the rollback, even though `r7` was no longer claimed in the persisted tree. 2. `D3`'s appended action was written assuming a log tail that the rollback had truncated, producing a log with a **gap and a duplicated sequence number**—two actions now shared seq `41`. 3. The verifier scored the merged result as valid because it replayed `D3` against the branch's *own recorded base snapshot*, not against the post-rollback tree. The corruption was not detected until a downstream auditor agent replayed the action log and hit the duplicate sequence number, at which point the merge was already two epochs old and had been read by three other agents. The final observed symptom was a **non-deterministic replay discrepancy**: replaying the same log twice produced different resource-claim states, because the duplicate-seq resolution depended on insertion order in a dict that had been rehashed between replays. The key observation: **every individual component behaved correctly.** The rollback was correct. The branch execution was correct. The verifier was correct *given its inputs*. The merge was correct *given its inputs*. The corruption lived entirely in the **base-version provenance** that was dropped at the merge boundary. ## Condition of Validity A speculative merge is valid **if and only if** the following invariant holds at the instant of merge: > `base_version(winner_delta) == current_tree_version(target_node)` That is, the delta must be applied to exactly the tree version it was computed against. This is a strict equality, not a "compatible" or "ancestor-of" relation, because deltas in this system are **not rebase-safe**: they encode preconditions (resource claims, log tails, sequence numbers) that are evaluated at apply time against live state, and those preconditions were only ever validated against the original base. Three corollaries bound the condition: 1. **Rollback must bump the version, not just revert content.** A rollback that returns the tree to `V-1`'s *contents* must still advance a monotonic `epoch` counter, so that a delta stamped `V` cannot be mistaken for a delta stamped `V-1`. Content equality is not version equality. 2. **Every speculative branch must carry an immutable `base_epoch` stamp** captured at spawn, propagated through all its tool calls, and checked at merge. If any tool call in the branch mutated shared state outside the branch's copy-on-write view, the stamp is invalid and the branch is unmergeable regardless of score. 3. **The verifier must replay against the merge target, not the branch base.** A verifier that validates against the branch's own snapshot is validating a counterfactual, not the actual post-merge state. This is the single most common place the invariant is silently violated, because it produces a *passing* verification for a *corrupt* merge. When any corollary fails, the branch is **not a winner**—it is a **stale survivor**, and the only correct action is to discard it and re-execute against the current epoch. ## Decision Boundary The boundary between "merge" and "refuse and re-execute" is drawn at epoch equality, but the *operational* decision is subtler because refusing to merge is expensive (the branch's compute is wasted) and because a naive system will be tempted to "reconcile" by replaying the delta's preconditions against current state and patching the differences. **Do not do this.** Reconciliation-by-replay is the trap: - If the delta's precondition on `r7` now fails against the rolled-back tree, a reconciler might "helpfully" re-acquire `r7`. But `r7` may now be legitimately held by a *different* branch that committed after the rollback. The reconciler has just created a double-claim. - If the delta's log-tail assumption is now wrong, a reconciler might renumber the appended action. But renumbering breaks every downstream reference the branch's own sub-agents recorded. The decision boundary is therefore: **merge only on exact epoch match; otherwise discard and re-execute, never reconcile.** The cost asymmetry justifies this—re-execution costs one branch's compute, while reconciliation costs unbounded, undetectable corruption that surfaces epochs later. There is one narrow exception: if the rollback was a **no-op rollback** (the divergence was a false positive and the tree's content and epoch are unchanged), the branch remains mergeable. This is why the epoch must be bumped *only on actual state mutation*, not on every rollback signal—otherwise false-positive divergences would needlessly invalidate every speculative branch. ## Operational Effect Enforcing the epoch-equality invariant changes system behavior in four measurable ways: 1. **Merge rejection rate rises sharply** in workloads with frequent divergence, because each rollback invalidates all in-flight branches spawned before it. Systems that previously reported "100% merge success" will now report 40–70% rejection under adversarial divergence. This is correct behavior surfacing previously hidden corruption, not a regression. 2. **Re-execution cost becomes visible and budgetable.** The planner must reserve token and tool-call budget for re-execution, because a rollback near the end of a long branch can waste the entire branch. Without this reservation, epoch-strict systems hit token-budget exhaustion mid-re-execution—a second-order failure. 3. **Verifier replay must be re-targeted.** Verifiers that previously validated against branch snapshots must be rewritten to replay against the post-merge target. This is the highest-effort migration and the one most likely to be skipped, silently reintroducing the failure. 4. **Replay determinism is restored.** With no duplicate sequence numbers and no stale claim cache, replaying the action log twice produces identical state. The non-deterministic replay discrepancy that first surfaced the bug becomes structurally impossible, because the gap-and-duplicate condition that caused it can no longer be produced by a merge. The invariant is cheap to check (one integer comparison) and catastrophic to omit. The correct posture is to treat it as a hard precondition at the merge gate, raise on violation, and let the coordinator re-execute rather than reconcile. ```python class StaleBranchMergeError(RuntimeError): """Raised when a speculative branch delta is about to be merged onto a task-tree epoch other than the one it was computed against.""" pass def merge_speculative_branch(tree, branch_delta, verifier): """ Merge a winning speculative branch into the task tree, enforcing the base-epoch equality invariant. Refuses to reconcile across epochs. """ # Corollary 1: rollback bumps epoch even when content is reverted. current_epoch = tree.current_epoch delta_base_epoch = branch_delta.base_epoch if delta_base_epoch != current_epoch: # Do NOT attempt reconciliation. Discard and signal re-execution. raise StaleBranchMergeError( f"branch {branch_delta.branch_id} computed against epoch " f"{delta_base_epoch}, tree is at epoch {current_epoch}; " f"rollback invalidated base. Discard and re-execute." ) # Corollary 2: every tool call in the branch must have stayed within # the branch's copy-on-write view. if not branch_delta.all_mutations_isolated: raise StaleBranchMergeError( f"branch {branch_delta.branch_id} leaked a shared-state mutation " f"outside its COW view; base_epoch stamp is untrustworthy." ) # Corollary 3: verify against the merge target, not the branch snapshot. apply_preview = tree.preview_apply(branch_delta) verdict = verifier.replay_against(apply_preview, target_epoch=current_epoch) if not verdict.passed: raise StaleBranchMergeError( f"branch {branch_delta.branch_id} fails replay against merge " f"target epoch {current_epoch}: {verdict.reason}" ) tree.apply(branch_delta) tree.advance_epoch() # merge is itself a state mutation return tree ```