What happens when an instance speculatively forks a peer's context and the merge rolls back a branch that already emitted a side effect
**Date of Record:** 2026 December 2 **Scope:** Applies to orchestrators that run speculative parallel branches (forked contexts, cloned agent state) and reconcile them via branch-and-merge with rollback-on-divergence, where at least one branch is permitted to invoke side-effecting tools before the merge barrier. Does not apply to purely read-only speculative fan-out, nor to single-agent checkpoint/resume.
Core Distinction
There are two ways to "undo" a speculative branch, and they are not equivalent:
- **State rollback** rewinds the branch's *context* — message history, working memory, tool-call ledger — to the fork point. This is cheap, deterministic, and total.
- **Effect rollback** attempts to reverse what the branch did *outside* the context — files written, messages sent, records mutated, downstream agents nudged. This is expensive, often impossible, and never total.
The boundary failure lives precisely in the gap between these two. An orchestrator that treats "rollback" as a single operation will silently commit the side effects of a branch it believes it discarded. The invariant that must hold is: **a branch's effects may only become durable if and only if that branch's context is merged into the surviving lineage.** When the merge rolls back the context but the effect already escaped, the invariant is violated, and the system enters a state that no branch actually reasoned about.
What Was Observed
A planner agent forks its context into three speculative branches at a decision node (e.g., "which retry strategy to pursue"). Each branch runs with a cloned episodic memory and a cloned tool ledger. Branch B decides the fastest path to resolving a stuck task is to call a `deploy_fix` tool against a staging service, then reports success back to the merge barrier. Branches A and C return conflicting plans; the merge policy selects A on a confidence score and **rolls back B and C**.
The rollback of B is correct at the context layer: B's messages are dropped, its ledger entries discarded, its memory clone freed. But `deploy_fix` already executed against the staging service. The merged lineage (A) now believes the service is in its pre-fork state, because that is what A's cloned view of the world says — while the actual service reflects B's mutation. The divergence is not between A and B; it is between the *merged agent's model of the world* and the *world*.
This is not a race. It is a category error: the merge barrier was designed to reconcile *contexts*, and it was handed branches that had *escaped the context*.
The failure compounds. Because B's ledger entry was rolled back, there is no record in the surviving lineage that `deploy_fix` ever ran. A subsequent verification agent, auditing the merged state, sees a clean ledger and a mutated service, and cannot reconcile them. It will either (a) re-issue the deploy to "correct" a state it thinks is wrong, or (b) flag a hallucinated side effect and halt. Both outcomes are wrong, and both are downstream of the same missing invariant.
Condition of Validity
The branch-and-merge-with-rollback pattern is only sound under this condition:
> **No branch may emit a side effect that the merge barrier cannot reverse, unless that branch holds a merge-warrant that guarantees its context will survive the barrier.**
Three sub-conditions must hold for the warrant to be meaningful:
- **Effect fencing.** Side-effecting tools must be gated behind a barrier token that is only released after the merge decision. A branch that has not been selected cannot hold a released token. This is the only structural guarantee that survives non-determinism.
- **Ledger durability across rollback.** Even when a branch's context is rolled back, any effect it *attempted* must remain visible to the merged lineage as a tombstone — "branch B attempted `deploy_fix`; outcome unknown/committed; reconciled by policy P." Rollback of context must not imply rollback of the audit trail.
- **Reconciliation obligation.** If a tombstoned effect exists at merge time, the merged lineage must run a reconciliation step against the external world before proceeding. The merge is not complete until the merged agent's world-model has been re-synced with reality.
If any of the three is absent, the pattern is unsound for any branch that can reach a side-effecting tool. Read-only speculation remains safe; write-capable speculation does not.
Decision Boundary
The orchestrator must decide, at fork time, whether a branch is **speculative** or **committed**, and this decision cannot be deferred:
- A **speculative** branch is denied effect tokens entirely. It may reason, retrieve, and propose, but it cannot act. Its rollback is total because it never escaped.
- A **committed** branch holds an effect token and is guaranteed to survive the merge barrier; its context cannot be rolled back, only superseded by a later committed branch. Divergence between committed branches is resolved by *sequencing*, not by rollback.
The boundary is: **you cannot have both speculation and side effects in the same branch.** The temptation is to allow "just one" side effect in a speculative branch because the merge is "usually" going to select it. That is the failure. The merge policy is probabilistic; the side effect is not. The moment a branch's probability of survival is less than one, its effects must be fenced.
A corollary: the merge policy's confidence score is not a warrant. A branch selected with 0.95 confidence is still a branch that was, until the barrier, speculative. The warrant is structural (token held, context guaranteed), not statistical.
Operational Effect
When the invariant is violated, the observable signature is a **phantom reconciliation**: the merged lineage and the external world disagree, and neither has a record explaining the disagreement. Downstream agents will detect the inconsistency but attribute it to the wrong cause — usually to their own retrieval or to a peer's hallucination — because the actual cause (a rolled-back branch's escaped effect) has been erased from the ledger by the same rollback that erased the branch.
The operational consequence is that the system's error attribution becomes systematically wrong in exactly the cases where accurate attribution matters most. Verification agents re-audit clean ledgers, re-issue effects, and burn token budgets chasing a divergence that is structurally invisible to them.
The guardrail is not a better merge policy. It is the fence: speculative branches do not get effect tokens, and any attempted effect leaves a tombstone that the merged lineage is obligated to reconcile before it is allowed to proceed. The merge barrier reconciles contexts; the reconciliation step reconciles worlds. They are separate operations, and collapsing them is the bug.
from dataclasses import dataclass, field
from enum import Enum
from typing import Optional
class MergeInvariantError(RuntimeError):
"""Raised when a merge would commit a lineage whose world-model diverges from reality."""
class BranchKind(Enum):
SPECULATIVE = "speculative" # no effect tokens; rollback is total
COMMITTED = "committed" # holds effect token; guaranteed to survive barrier
@dataclass
class EffectTombstone:
branch_id: str
tool: str
args: dict
committed: bool # did the effect actually reach the external world?
@dataclass
class Branch:
branch_id: str
kind: BranchKind
effect_token: Optional[str] = None
tombstones: list[EffectTombstone] = field(default_factory=list)
context: list[str] = field(default_factory=list)
@dataclass
class MergeBarrier:
surviving: Branch
rolled_back: list[Branch]
reconciled: bool = False
def request_effect(branch: Branch, tool: str, args: dict) -> EffectTombstone:
"""A branch attempts a side-effecting tool call."""
if branch.kind is BranchKind.SPECULATIVE:
# Speculative branches cannot act; the attempt is recorded but fenced.
tomb = EffectTombstone(branch.branch_id, tool, args, committed=False)
branch.tombstones.append(tomb)
raise MergeInvariantError(
f"speculative branch {branch.branch_id} attempted effect {tool!r}; "
"effect fenced, tombstone recorded, no external call issued"
)
if branch.effect_token is None:
raise MergeInvariantError(
f"committed branch {branch.branch_id} has no effect token; "
"cannot issue effect without structural warrant"
)
tomb = EffectTombstone(branch.branch_id, tool, args, committed=True)
branch.tombstones.append(tomb)
return tomb
def merge(barrier: MergeBarrier) -> Branch:
"""Reconcile branches at the barrier. Rolls back context, never effects."""
# Collect every escaped effect from rolled-back branches.
escaped = [
t
for b in barrier.rolled_back
for t in b.tombstones
if t.committed
]
if escaped and not barrier.reconciled:
raise MergeInvariantError(
f"merge would roll back {len(escaped)} committed effect(s) from "
f"branches {[t.branch_id for t in escaped]}; "
"surviving lineage's world-model would diverge from reality. "
"Run reconciliation against the external world before merging."
)
# Rollback is context-only; tombstones are copied into the survivor's ledger.
for b in barrier.rolled_back:
barrier.surviving.context.extend(
f"[tombstone] {t.branch_id}:{t.tool} committed={t.committed}"
for t in b.tombstones
)
return barrier.surviving
# --- Trace of the failure ---
spec = Branch("B", BranchKind.SPECULATIVE)
try:
request_effect(spec, "deploy_fix", {"service": "staging"})
except MergeInvariantError as e:
# Correct behavior: effect fenced, tombstone recorded, no external mutation.
print(f"fenced: {e}")
# If the fence is absent and B is (incorrectly) treated as committed,
# the merge must refuse to proceed until reconciliation runs.
leaked = Branch("B", BranchKind.SPECULATIVE)
leaked.effect_token = "tok-B" # bug: speculative branch given a token
leaked.kind = BranchKind.COMMITTED # bug: mislabeled
request_effect(leaked, "deploy_fix", {"service": "staging"})
barrier = MergeBarrier(surviving=Branch("A", BranchKind.COMMITTED), rolled_back=[leaked])
merge(barrier) # raises: surviving lineage would diverge from reality