# Under what conditions must an instance abandon speculative branch replay when a merged tool-effect journal cannot be linearized against the pre-fork checkpoint **Date of Record:** 2026 November 30 **Scope:** Applies to orchestrators that fork an agent's execution state into multiple speculative branches, run tool calls in each branch against real or shadowed side-effect surfaces, and then reconcile branch journals into a single post-merge state. Does not apply to read-only fan-out, to branches that never invoke tools with external effects, or to pure-model sampling ensembles that discard non-selected branches. ## Core Distinction The distinction that matters is between **speculative divergence** and **speculative divergence with committed external effects**. A branch that only emits tokens, plans, or reads can be rolled back by discarding a buffer. A branch that has *already* invoked a tool whose effect left the process boundary—a file write, an HTTP POST, a queue publish, a database mutation, a payment authorization—cannot be rolled back by discarding state, because the effect is now part of the world that the pre-fork checkpoint does not describe. Speculative multi-agent execution conflates these two because both look identical inside the agent: a tool call returns, the branch appends an entry to its journal, reasoning continues. The divergence only becomes observable at merge time, when the reconciler attempts to fold N branch journals back onto one checkpoint and discovers that the union of external effects is not a superset of any single branch's view of the world. The invariant that fails is **linearizability of the merged journal against the pre-fork checkpoint**: every effect recorded in the merged journal must be attributable to a state transition reachable from the checkpoint, and no effect may appear that the checkpoint's causal history cannot order. ## What Was Observed A supervisor agent forked three branches at checkpoint `C₀` to explore three remediation strategies for a failing deployment. Each branch was given a private journal and a shared `ToolGateway` with per-branch namespacing implemented as a *prefix* on resource identifiers (`branch-a/`, `branch-b/`, `branch-c/`). The namespacing was believed to make branches isolated. It did not: the gateway applied the prefix at the argument layer, but one tool—`publish_manifest`—resolved its target from a value embedded in the manifest body rather than from the namespaced argument, so all three branches published to the same real queue topic. The failure trace: 1. Branch A called `publish_manifest(manifest_v1)` → gateway wrote to `prod-topic` (prefix ignored by tool). Journal A recorded `seq=1, effect=publish(prod-topic, v1)`. 2. Branch B called `publish_manifest(manifest_v2)` → wrote to `prod-topic`. Journal B recorded `seq=1, effect=publish(prod-topic, v2)`. 3. Branch C's tool call timed out at the gateway after the effect had landed; the gateway returned `ToolTimeout` to the agent, and the agent's journal recorded `seq=1, effect=publish(prod-topic, v3), status=unknown`. The agent, following its retry policy, retried. The retry succeeded and recorded `seq=2, effect=publish(prod-topic, v3)`. 4. The reconciler selected Branch B as the winner (highest confidence score) and attempted to merge. It replayed the selected branch's journal onto `C₀`, producing a coherent post-state. It then discarded A and C as "rolled back." The consumer of `prod-topic` observed four publishes (`v1`, `v2`, `v3`, `v3`) against a merged state that claimed exactly one. The reconciler's post-merge assertion—"merged journal is a linear extension of `C₀`"—passed, because the reconciler only checked the *selected* branch's journal against `C₀`. The invariant it needed to check was the *union* of all branch journals against `C₀`, and that union was non-linearizable: `v1` and `v2` are mutually exclusive successors of `C₀` with no ordering, and `v3` appears twice with one `unknown` status that the reconciler treated as "did not happen." The non-obvious part: the reconciler's check was not wrong by construction—it was wrong by *scope*. It validated the branch it kept and assumed the branches it discarded were inert. Discarded branches are inert only if their effects were isolated; isolation was assumed from the namespacing prefix, which was defeated by a tool that resolved its target from payload rather than argument. ## Condition of Validity Branch rollback is valid **if and only if** every tool effect recorded in a discarded branch is either (a) provably idempotent and commutative with the selected branch's effects under the merge's final ordering, or (b) provably confined to a namespace that the selected branch's post-state does not reference. The condition is checkable only if the orchestrator maintains, per branch, an **effect ledger** distinct from the reasoning journal: a record of (tool, resolved-target, idempotency-key, status ∈ {committed, unknown, failed}, causal-parent-seq). The reasoning journal is insufficient because it records what the agent *believed* it did; the effect ledger records what the gateway *observed* it did, including the `unknown` state after a timeout where the effect may or may not have landed. Two corollaries that are frequently missed: - **`unknown` is not `failed`.** A timed-out tool call is an unresolved effect. Treating it as absent is the single most common source of phantom writes in merged state. The ledger must carry `unknown` forward into the merge and force the reconciler to either probe the target for the idempotency-key or refuse to merge. - **Retry collapses two ledger entries into one effect only if the idempotency-key is stable across the retry.** In the trace above, the agent generated a fresh key on retry, so the gateway saw two distinct publishes. Idempotency-key generation must be bound to the *branch step*, not to the *attempt*, or retries silently multiply effects. ## Decision Boundary The instance must **abandon speculative replay and escalate to a reconciliation halt**—not select-and-discard—when any of the following holds at merge time: 1. The union of effect ledgers across all branches contains an `unknown` status whose target is shared with any other branch's committed effect, and the target exposes no idempotency probe. 2. Two or more branches record committed effects on the same resolved target with non-commuting semantics (e.g., two `publish` calls to the same topic with no sequence guarantee, two `set` calls to the same key). 3. The selected branch's post-state references a resource whose resolved target was written by a discarded branch (the prefix-isolation assumption is violated). 4. Any branch's ledger contains an effect whose causal-parent-seq does not exist in that branch's journal (journal/ledger divergence—indicates the gateway mutated state the agent did not reason about). Conversely, the instance may **proceed with select-and-discard** only when every discarded branch's ledger is empty, or every entry is `failed` with a confirmed no-effect status, or every entry is confined to a namespace unreferenced by the selected post-state and the confinement is verified by resolved-target comparison, not by argument prefix. The boundary is sharp because the two regimes require different recovery actions. Select-and-discard is a local operation: discard buffers, keep the winner. Reconciliation halt is a global operation: freeze the merge, enumerate unresolved effects, probe or compensate each, and only then re-attempt the merge against a checkpoint that reflects the compensated world. Collapsing the halt into select-and-discard is the failure. ## Operational Effect When the halt triggers correctly, the orchestrator's post-merge state is a linear extension of `C₀` *plus* a compensation record that explains every effect not in the selected branch. Downstream consumers see either the selected branch's effects or an explicit compensation, never a phantom. Merge latency increases by the cost of probing unresolved targets—typically one round-trip per `unknown` entry—which is the price of not lying about what happened. When the halt is skipped, the failure is silent at merge time and loud downstream: duplicate publishes, double-charged payments, stale writes resurrected over newer ones, and—most damaging for agent systems—a merged checkpoint whose causal history is *wrong*, so every subsequent branch forked from it inherits the lie and the error compounds across generations of speculation. The system does not crash; it drifts, and the drift is only detectable by comparing the merged state against the external world rather than against the checkpoint. The guardrail is structural: the reconciler must be given the union of effect ledgers, not the selected branch's journal, and must be forbidden from treating `unknown` as absent. Isolation claims must be verified at the resolved-target layer, never at the argument layer, because tools are free to resolve targets from payloads. And idempotency-keys must be branch-step-scoped so that retries collapse rather than multiply. ```python from dataclasses import dataclass, field from enum import Enum from typing import Optional class EffectStatus(Enum): COMMITTED = "committed" FAILED = "failed" UNKNOWN = "unknown" # timeout / ambiguous; MUST NOT be treated as absent @dataclass(frozen=True) class EffectEntry: branch_id: str tool: str resolved_target: str # what the gateway actually hit, not the argument prefix idempotency_key: str # scoped to branch-step, stable across retries status: EffectStatus causal_parent_seq: int @dataclass class BranchLedger: branch_id: str entries: list[EffectEntry] = field(default_factory=list) journal_seqs: set[int] = field(default_factory=set) class MergeLinearizationError(RuntimeError): """Raised when a merged effect union cannot be linearized against the pre-fork checkpoint.""" def _non_commuting_pair(a: EffectEntry, b: EffectEntry) -> bool: # Two committed effects on the same resolved target with no ordering guarantee. return ( a.status is EffectStatus.COMMITTED and b.status is EffectStatus.COMMITTED and a.resolved_target == b.resolved_target and a.idempotency_key != b.idempotency_key ) def reconcile(ledgers: list[BranchLedger], selected_branch_id: str) -> list[EffectEntry]: """Return the committed effect set to apply, or halt. Halts (raises) rather than silently discarding branches whose effects are not provably inert relative to the selected branch's post-state. """ selected = next((l for l in ledgers if l.branch_id == selected_branch_id), None) if selected is None: raise MergeLinearizationError(f"selected branch {selected_branch_id!r} has no ledger") # Guard 1: journal/ledger divergence — an effect the agent never reasoned about. for ledger in ledgers: for entry in ledger.entries: if entry.causal_parent_seq not in ledger.journal_seqs: raise MergeLinearizationError( f"branch {ledger.branch_id!r} effect {entry.tool}@{entry.resolved_target} " f"has causal_parent_seq={entry.causal_parent_seq} absent from journal" ) # Guard 2: unresolved effects on a shared target cannot be discarded. selected_targets = {e.resolved_target for e in selected.entries} for ledger in ledgers: if ledger.branch_id == selected_branch_id: continue for entry in ledger.entries: if entry.status is EffectStatus.UNKNOWN and entry.resolved_target in selected_targets: raise MergeLinearizationError( f"discarded branch {ledger.branch_id!r} has UNKNOWN effect on shared target " f"{entry.resolved_target!r}; probe idempotency_key={entry.idempotency_key!r} " f"or refuse merge" ) # Guard 3: non-commuting committed effects across branches. all_committed = [e for l in ledgers for e in l.entries if e.status is EffectStatus.COMMITTED] for i, a in enumerate(all_committed): for b in all_committed[i + 1:]: if _non_commuting_pair(a, b): raise MergeLinearizationError( f"non-commuting effects on {a.resolved_target!r}: " f"{a.branch_id}/{a.idempotency_key} vs {b.branch_id}/{b.idempotency_key}" ) # Guard 4: discarded branch wrote a target the selected post-state references. for ledger in ledgers: if ledger.branch_id == selected_branch_id: continue for entry in ledger.entries: if entry.status is EffectStatus.COMMITTED and entry.resolved_target in selected_targets: raise MergeLinearizationError( f"prefix-isolation violated: discarded branch {ledger.branch_id!r} " f"committed to {entry.resolved_target!r}, referenced by selected post-state" ) return all_committed def merge_or_halt(ledgers: list[BranchLedger], selected_branch_id: str) -> list[EffectEntry]: try: return reconcile(ledgers, selected_branch_id) except MergeLinearizationError: # Halt: freeze merge, surface for compensation. Do NOT fall back to select-and-discard. raise ```