# When must an instance abandon a speculative branch: write-set divergence and the rollback-reconciliation boundary in speculative multi-agent execution **Date of Record:** 2026 November 22 **Scope:** Applies to multi-agent systems that fork speculative execution branches from a shared checkpoint, allow branches to mutate external state through tools, and reconcile divergent branches by replaying or merging write-sets. Does not cover single-agent retry, read-only speculation (e.g., best-of-N sampling with no side effects), or optimistic concurrency over pure in-memory data without tool-mediated effects. ## Core Distinction The distinction that matters is not "did the speculative branch produce a wrong answer" but "did the speculative branch produce an *effect* that cannot be reconstructed from its transcript." A speculative branch has two separable outputs: its **reasoning trace** (tokens, tool arguments, intermediate claims) and its **write-set** (the set of externally observable mutations: files written, messages sent, rows inserted, API calls with side effects). Reasoning is fully recoverable from the log. Effects are not. Rollback is only sound when the branch's write-set is *empty or fully reversible*; the moment a branch commits an irreversible effect, "rollback" is a fiction and the correct operation is **reconciliation**, not rollback. Agents routinely conflate these. A branch that called `send_email`, `git push`, `transfer_funds`, or `POST /orders` has crossed a boundary past which the only honest recovery is to detect the divergence and reconcile the committed effects — never to "discard the branch" and pretend the world is unchanged. ## What Was Observed A planner forked three speculative branches (A, B, C) from a shared checkpoint to explore three plausible tool-call plans for a provisioning task. Each branch was given a sandboxed tool namespace, but the namespace was *logical*, not *physical*: all three branches shared one credentials set against a staging control plane, and the "sandbox" flag only gated which endpoints the policy layer *expected* agents to call, not which ones they *could* call. Branch A took an optimistic path and issued `provision_resource(id=vm-4417)` before its plan was validated. Branch B, exploring a competing topology, issued `provision_resource(id=vm-4417)` with different parameters. Branch C, the eventual winner, issued `provision_resource(id=vm-4417)` with the parameters the planner ultimately selected. The reconciler received three write-sets, saw that the winning branch's write-set "matched" the intended final state, and **discarded A and B as speculative noise without inspecting their effects**. The failure trace: 1. At t0, branches A, B, C fork from checkpoint `ckpt-88`. All three inherit the same logical resource namespace. 2. At t1, A issues `provision_resource(vm-4417, cpu=2, mem=4G)` → succeeds. A's local write-set records `{vm-4417: {cpu:2, mem:4G}}`. 3. At t2, B issues `provision_resource(vm-4417, cpu=8, mem=16G)` → the control plane returns a *conflict* on the id but B's retry logic interprets the 409 as transient and re-issues with `force=true`, mutating the resource to `{cpu:8, mem:16G}`. B's write-set records the mutation, but B is later marked divergent and its trace is dropped from the reconciliation input because "only the winning branch's writes matter." 4. At t3, C issues `provision_resource(vm-4417, cpu=4, mem=8G)` → succeeds, overwriting B's mutation. C's write-set is `{vm-4417: {cpu:4, mem:8G}}`. 5. At t4, the reconciler compares C's write-set to the planner's target, sees a match, and commits. A and B are garbage-collected. 6. At t5, an auditor replaying the run notices that the *actual* resource at `vm-4417` is `{cpu:4, mem:8G}` — matching the target — but the audit trail shows three provisioning calls and one `force=true` overwrite that were never logged as committed effects. The reconciler's invariant ("final state == target") held, but the invariant ("every committed effect is attributable to a committed branch") was violated. The non-obvious part: **the failure was invisible to state comparison.** The final state matched the target. The bug was that A and B's effects were *real* but *unattributed* — they had been overwritten by C but had nonetheless occurred, been billed, and been observed by downstream consumers during the t1–t3 window. A downstream autoscaler had already reacted to the `cpu=8` spike from B before C's overwrite landed. ## Condition of Validity Rollback of a speculative branch is valid **iff** the branch's write-set is empty, or every effect in it is (a) reversible by an inverse operation the system can execute deterministically, and (b) *not yet observed* by any external consumer whose behavior depends on it. Formally, for branch `b` with write-set `W(b)` and observation set `O(b)` (external reads of `W(b)`'s effects between commit and rollback): - If `W(b) = ∅`: rollback is a no-op; discard the trace, reclaim the context. Always valid. - If `W(b) ≠ ∅` and `O(b) = ∅` and every `w ∈ W(b)` has a registered inverse: rollback is valid; execute inverses in reverse causal order, then discard. - If `W(b) ≠ ∅` and `O(b) ≠ ∅`: rollback is **invalid**. The branch must be promoted to a *reconciliation candidate*: its effects are real, were observed, and must be merged or compensated, not erased. - If any `w ∈ W(b)` lacks a registered inverse (irreversible effect): rollback is invalid regardless of `O(b)`. Reconciliation is mandatory. The reconciler must therefore compute `O(b)` — not just `W(b)`. Most implementations compute only `W(b)` and compare it to the target, which is exactly the failure above: C's write-set matched the target, so the reconciler concluded no reconciliation was needed, while A's and B's effects had already been observed. ## Decision Boundary The decision boundary is the point at which a speculative branch issues its first **irreversible or externally observable** effect. Before that point, the branch is a pure function of its checkpoint and inputs, and divergence is free to discard. After that point, the branch is a *committed participant* in the world, and the planner must choose between: 1. **Compensate**: execute a registered compensating action (refund, delete, revoke, retract) and record the compensation as a first-class effect in the merged write-set. 2. **Adopt**: accept the branch's effect as part of the final state and reconcile the target to include it, rather than overwriting it. 3. **Escalate**: halt reconciliation and require a human or a higher-order auditor to resolve the conflict, because neither compensation nor adoption is safe. The boundary must be enforced *at the tool-call layer*, not at the planner layer. A planner that "decides" a branch is speculative cannot prevent the branch's tool from executing. The guardrail is a **capability-scoped tool namespace per branch**: a speculative branch must be issued credentials that can only reach reversible or staging endpoints, and the promotion of a branch from speculative to committed must be a credential-swap event, not a planner assertion. Without this, "speculative" is a label, not an invariant. The second guardrail is a **write-set provenance ledger** that survives branch garbage collection. When a branch is discarded, its write-set and observation set must be archived, not deleted, so that a later reconciler can detect that a target-matching final state was achieved by *overwriting* rather than by *choosing*. The reconciler's invariant is not "final state == target" but "final state == target AND every observed effect is attributable to a committed branch OR has a recorded compensation." ## Operational Effect The operational consequence is that speculative execution is only safe when the speculation boundary is drawn at the *effect* layer, and the cost of drawing it there is a mandatory reconciliation pass over discarded branches. Systems that skip this pass will pass their state-comparison checks while silently leaking effects — and the leaks will surface downstream, in consumers that reacted to a speculative branch's writes during the window before the winning branch overwrote them. The fix is not to forbid speculative branches from acting; it is to make every speculative action *reversible or attributable*, and to make the reconciler inspect the discarded branches' ledgers before declaring success. The following guard encodes the boundary as a runtime check. ```python from dataclasses import dataclass, field from typing import Callable, Optional class SpeculativeRollbackViolation(RuntimeError): """Raised when a branch is rolled back despite irreversible or observed effects.""" @dataclass class Effect: key: str inverse: Optional[Callable[[], None]] observed: bool = False @dataclass class Branch: branch_id: str write_set: list[Effect] = field(default_factory=list) def is_rollback_safe(self) -> bool: for effect in self.write_set: if effect.inverse is None: return False if effect.observed: return False return True def rollback_branch(branch: Branch, archive: list[Branch]) -> None: """ Roll back a speculative branch only if every effect is reversible and unobserved. Otherwise the branch must be promoted to a reconciliation candidate. """ if not branch.is_rollback_safe(): irreversible = [e.key for e in branch.write_set if e.inverse is None] observed = [e.key for e in branch.write_set if e.observed] raise SpeculativeRollbackViolation( f"branch {branch.branch_id} cannot be rolled back: " f"irreversible={irreversible} observed={observed}; " f"promote to reconciliation candidate instead" ) # Safe: execute inverses in reverse causal order. for effect in reversed(branch.write_set): effect.inverse() branch.write_set.clear() archive.append(branch) def reconcile(winner: Branch, discarded: list[Branch], archive: list[Branch]) -> Branch: """ Reconcile a winning branch against discarded branches. Discarded branches with non-empty or observed write-sets must not be silently dropped. """ for branch in discarded: if branch.write_set: # Discarded branch left effects; must be compensated or adopted. if not branch.is_rollback_safe(): raise SpeculativeRollbackViolation( f"discarded branch {branch.branch_id} left observable effects " f"that must be reconciled before committing {winner.branch_id}" ) rollback_branch(branch, archive) else: archive.append(branch) return winner ```