# Where must an instance refuse to merge a reconciled branch when speculative rollback has already mutated shared tool-side state **Date of Record:** 2026 November 29 **Scope:** Applies to any multi-agent system that forks speculative execution branches from a common checkpoint, lets branches call side-effecting tools, and then reconciles the winning branch back onto the trunk. Does not apply to read-only fan-out (map/reduce over pure functions) or to single-agent rollback where no peer branch has observed the rolled-back state. ## Core Distinction Speculative branch reconciliation is usually modeled as a *state merge problem*: each branch produces a delta over the fork checkpoint, a winner is elected, and the winner's delta is applied to the trunk. Under this model, rollback is cheap — discard the losing delta, keep the winner's. The failure mode this record isolates is different: **rollback is not free when branches have already committed irreversible effects to external state that the trunk's own history observes.** A losing speculative branch that called `create_ticket`, `send_email`, `debit_account`, or even `write_file` on a path the trunk later reads has *already changed the world*. The trunk's subsequent reasoning is conditioned on that world. When the winner is merged and the loser's *in-memory* delta is discarded, the loser's *tool-side* effects remain — and the trunk now runs on a state that no branch's model predicts. The invariant that must hold, and frequently does not, is this: **a branch is only mergeable if every external effect it produced is either (a) idempotent and re-derivable from the winner's delta, or (b) recorded in a compensating journal that the reconciler replays before merge.** The boundary condition is the moment a losing branch has produced a non-compensable effect and the reconciler is asked to proceed anyway. ## What Was Observed A three-branch speculative planner was forked from checkpoint `C` to solve a task requiring a database migration. All three branches were given the same tool grants, including `db.execute(sql)` and `fs.write(path, bytes)`. - **Branch A** drafted a migration, ran it against a staging replica via `db.execute`, and recorded the migration file to `fs`. - **Branch B** chose a different schema, ran *its* migration against the same replica (the tool did not enforce branch isolation — the replica was shared), and wrote a conflicting file to the same path. - **Branch C** failed early on a tool timeout and was rolled back cleanly. The reconciler elected Branch A as winner by a scoring heuristic over reasoning traces. It then applied A's delta to trunk and discarded B's delta. The discard was *logical*: B's in-memory plan, its tool-call log entries, and its claimed file writes were dropped from the trunk's context. But B's `db.execute` had already altered the replica, and B's `fs.write` had already landed on disk before A's write overwrote it — except on one shard where A's write raced and lost. The trunk, resuming from `C` plus A's delta, issued a verification query against the replica. The replica reflected **B's schema**, not A's, because B's migration had run second and its `ALTER TABLE` was not idempotent. The trunk's verifier concluded A's migration had failed and re-ran it, compounding the divergence. Two further reconciliation cycles produced a state that matched no branch's predicted postcondition. The critical observation: **no component in the pipeline was wrong in isolation.** The reconciler correctly discarded B's logical delta. The tool layer correctly executed what it was told. The verifier correctly compared observed state to expected state. The failure lived entirely in the seam between *logical rollback* and *physical effect persistence*. ## Condition of Validity The merge-without-compensation shortcut is valid only under the conjunction: 1. **Effect idempotence:** every tool a branch may call is idempotent under replay, so a losing branch's effects can be harmlessly re-applied or overwritten by the winner's delta. This fails for `ALTER TABLE`, `send_email`, `increment`, and any append-only sink. 2. **Isolation:** branches cannot observe or mutate each other's tool-side state. Shared replicas, shared filesystems, and shared rate-limited APIs violate this silently. 3. **Effect observability:** the reconciler can enumerate every external effect a branch produced from the branch's own recorded log. This fails when a tool succeeds but the branch's log write is lost (crash between effect and log), or when a tool's response is truncated and the branch cannot tell whether the effect landed. 4. **Trunk non-observation of loser effects:** the trunk's post-merge reasoning never reads state that a losing branch mutated. This is the assumption most often violated, because the trunk's verifier is *designed* to read exactly the state branches were mutating. When any of these fails, reconciliation must be gated on explicit compensation, not on logical delta discard. The condition of validity is therefore not "the winner's delta is correct" but "**the set of physical effects surviving reconciliation equals the set of effects implied by the winner's delta.**" That equality is the invariant; the merge is only sound when it holds. ## Decision Boundary The reconciler must make a binary decision at merge time, and the boundary is drawn by effect compensability, not by branch score: - **Mergeable:** every effect from every losing branch is either idempotent-and-superseded by the winner, or has a recorded inverse operation (a compensating journal entry) that the reconciler can replay *before* applying the winner's delta. The journal must be durable and written *before* the effect is issued (write-ahead), because a crash between effect and journal-write leaves an unrecorded effect. - **Non-mergeable / must halt:** a losing branch produced an effect that is non-idempotent, non-compensable, and unrecorded. In this case the reconciler must **refuse to merge** and escalate to a human or a full trunk re-derivation from a pre-fork snapshot that excludes the contaminated tool state. Proceeding produces a trunk whose state is a function of a branch that was never elected — a silent violation of the election's semantics. The non-obvious part: the boundary is crossed *before* the reconciler runs. By the time the reconciler inspects deltas, the world has already changed. The decision boundary is therefore enforced at the **tool-grant layer**: a branch that is speculative (i.e., may be rolled back) must not be granted non-compensable effects without a write-ahead journal. This is a grant-time invariant, not a merge-time check. A reconciler that only checks at merge time is checking a fact that is already unrecoverable. ## Operational Effect Systems that omit the grant-time journal exhibit a characteristic signature: the trunk's verifier disagrees with every branch's predicted postcondition, and re-derivation does not converge because each re-derivation forks new speculative branches that re-contaminate the same shared tool state. The failure is self-reinforcing — the more the system tries to verify, the more it mutates. The guardrail is a two-part contract: 1. **Speculative branches receive a restricted grant set.** Non-idempotent tools are either unavailable or wrapped in a journaling shim that records `(effect_id, inverse_op, pre_state_hash)` durably before execution. The shim is branch-scoped so the reconciler can replay inverses in reverse order for losers. 2. **The reconciler refuses to merge when the journal is incomplete.** Incomplete means: an effect was issued but no inverse was recorded, or a branch's log has a gap between a tool call and its response. Refusal is not a retry — it is an escalation, because the world state is now unknown and no amount of re-reasoning recovers it. The observable consequence of getting this right: reconciliation cost becomes proportional to the number of non-idempotent effects across *all* branches, not just the winner. This is the price of soundness, and it is the reason speculative execution over side-effecting tools is fundamentally more expensive than speculative execution over pure reasoning. The invariant cannot be bought back with better scoring heuristics; it is a property of the effect journal, enforced before the first speculative tool call. ```python from __future__ import annotations from dataclasses import dataclass, field from enum import Enum from typing import Iterable class ReconciliationRefused(RuntimeError): """Raised when a speculative branch produced an unrecorded or non-compensable effect.""" class EffectClass(Enum): IDEMPOTENT = "idempotent" # safe to re-apply or overwrite COMPENSABLE = "compensable" # has a durable inverse op IRREVERSIBLE = "irreversible" # cannot be undone; must be journaled pre-issue @dataclass(frozen=True) class EffectRecord: effect_id: str branch_id: str kind: EffectClass inverse_op: str | None # required for COMPENSABLE pre_state_hash: str | None # required for COMPENSABLE journaled_pre_issue: bool # written before the effect was issued @dataclass class Branch: branch_id: str effects: list[EffectRecord] = field(default_factory=list) elected: bool = False def assert_mergeable(branches: Iterable[Branch]) -> None: """ Enforce the grant-time invariant at merge time as a last line of defense. Sound only if every branch journaled pre-issue; this checks that it did. """ for branch in branches: if branch.elected: continue # winner's effects are the intended postcondition for effect in branch.effects: if effect.kind is EffectClass.IDEMPOTENT: continue # superseded by winner's delta if effect.kind is EffectClass.COMPENSABLE: if not effect.journaled_pre_issue or effect.inverse_op is None: raise ReconciliationRefused( f"branch {branch.branch_id} effect {effect.effect_id} " f"is compensable but lacks a pre-issue inverse journal entry; " f"world state is unrecoverable — escalate, do not retry" ) continue if effect.kind is EffectClass.IRREVERSIBLE: raise ReconciliationRefused( f"branch {branch.branch_id} produced irreversible effect " f"{effect.effect_id} that survived rollback; trunk state is now " f"conditioned on a non-elected branch — merge forbidden" ) def reconcile(trunk_checkpoint: str, branches: list[Branch]) -> str: assert_mergeable(branches) # Replay compensating inverses for losers in reverse order, then apply winner delta. for branch in branches: if branch.elected: continue for effect in reversed(branch.effects): if effect.kind is EffectClass.COMPENSABLE: _apply_inverse(effect) # idempotent by contract winner = next(b for b in branches if b.elected) return f"{trunk_checkpoint}+{winner.branch_id}" def _apply_inverse(effect: EffectRecord) -> None: # Implementation omitted; contract: idempotent, verifies pre_state_hash. ... ```