# What happens when an instance speculatively forks parallel agent plans and the merge commits a branch whose tool side effects were only partially rolled back **Date of Record:** 2026 November 30 **Scope:** Applies to orchestrators that run speculative branch-and-merge over parallel agent plans, where at least one branch executes side-effecting tools and the merge step commits a winning branch while discarding losers. Does not apply to read-only fan-out, pure-reasoning ensembles, or single-branch retries where no plan is merged. ## Core Distinction The distinction that matters is between **logical rollback** and **physical rollback**. A speculative orchestrator forks N branches, each an independent agent plan with its own context slice and its own tool bindings. When a branch loses the merge, the orchestrator discards its *state* — the message log, the scratchpad, the provisional artifacts in the branch namespace. That is logical rollback, and it is cheap and total. Physical rollback is what actually happened in the world: an email sent, a row inserted, a file written to a shared volume, a payment intent created, a webhook fired. Speculative execution assumes that a discarded branch leaves no trace. That assumption holds only for tools that are *transactionally reversible* and *correctly registered as such*. The failure mode is the boundary where a branch's side effects are partially rolled back — some tools compensated, some not — and the merge still commits, because the merge predicate only inspects the branch's *state*, never its *effect ledger*. The non-obvious part: the merge doesn't fail. It succeeds, cleanly, with a coherent winning plan. The corruption is invisible to the merge logic because the merge logic was never given a view of the world outside the branch namespaces. The system reports a successful speculative round while the world is in a state that no single branch ever planned for. ## What Was Observed A three-way speculative round over a provisioning task. Branches A, B, C each drafted a plan to reconcile a set of resources. All three shared a tool binding to an external control plane (call it `apply_change`) and a scratch namespace (`branch://{id}/`). - Branch A called `apply_change(resource=r1, desired=state_x)` and succeeded. Its compensation entry was registered as `revert_change(r1, prior=state_0)`. - Branch B called `apply_change(resource=r1, desired=state_y)` — a *different* desired state for the same resource — and succeeded. Its compensation entry was also `revert_change(r1, prior=state_0)`. - Branch C was read-only and called nothing mutating. The merge predicate scored B highest on plan coherence and picked B. The orchestrator then ran the loser-compensation pass: it replayed A's compensation, `revert_change(r1, prior=state_0)`. But A's write had already been overwritten by B's write. The compensation for A therefore reverted *B's* effect, not A's — it set `r1` back to `state_0`, clobbering the winning branch's committed change. B's own compensation was never run, because B won. Final state: `r1 = state_0`, the pre-round value. The merge committed B. The system's own record says B's plan executed. The world says nothing happened, and the compensation ledger says A was cleanly rolled back. Every one of those three statements is individually defensible and jointly contradictory. The trigger condition is precise: **two or more speculative branches mutate the same resource with non-commutative effects, and loser compensation is keyed on `(resource, prior_state)` rather than on `(branch_id, effect_id)`.** The moment a losing branch's compensation is expressed as "restore prior," it is a blind overwrite that cannot distinguish its own write from a sibling's later write. Speculative isolation was assumed at the effect layer but only enforced at the namespace layer. A second, quieter instance: Branch A's plan included a `send_notification` call that succeeded before A lost. That tool had no registered compensation at all — the tool manifest marked it `idempotent: true`, which the orchestrator read as `reversible: true`. It is neither. The notification went out for a plan that was discarded. Logical rollback erased the branch; the recipient still has the email. ## Condition of Validity The spec-exec invariant — *discarding a branch leaves the world equivalent to the branch never having run* — is valid only under all of the following, and the orchestrator must be able to *prove* each per branch before it is allowed to fork a side-effecting plan: 1. **Effect keying.** Every mutating tool call is tagged with `(branch_id, effect_id)` at dispatch, and every compensation targets that exact effect, not a resource-level prior. Compensation is a compare-and-revert: it applies only if the resource still holds the value this effect wrote. If a sibling has since written, the compensation must *detect the conflict*, not overwrite. 2. **Reversibility classification is per-effect, not per-tool.** `idempotent` and `reversible` are orthogonal. A tool can be idempotent and irreversible (send_email with a dedupe key). The manifest must carry `reversible: {true|false|compensable}` and the orchestrator must refuse to fork a branch that will call an irreversible tool unless that branch is *pre-committed* (executed only after winning) or *gated* (deferred until merge). 3. **Conflict detection at merge.** The merge predicate must consume the effect ledger, not just the plan state. Two branches writing the same resource with non-commutative effects is a *merge conflict*, and the correct outcome is to reject the round or serialize those branches — never to pick a winner and compensate the loser blind. Where any of these is absent, the round is not speculative. It is a set of concurrent real executions with a post-hoc bookkeeping layer that will lie about what happened. ## Decision Boundary The operative question is not "can this plan run in parallel?" but "**can this plan's effects be un-run, and does the un-run know what it is un-running?**" - If every mutating effect in a branch is `reversible` *and* keyed to `(branch_id, effect_id)` *and* compensation is compare-and-revert — fork freely. - If a branch contains an `irreversible` effect — do not fork it speculatively. Defer the effect behind a merge gate: the branch reasons and drafts, but the irreversible call is materialized only by the winning branch, after commit. This converts speculation over *reasoning* into speculation over *effects*, which is the only kind that is safe. - If two branches touch the same resource with non-commutative effects — this is a conflict, not a race to be resolved by scoring. Serialize, or reject and re-fork with the conflict as a constraint. - If the tool manifest cannot answer `reversible` per call — treat as irreversible. The default for unknown reversibility is *no speculation*, because the cost of a wrong "reversible" is a corrupted world that the merge will report as success. The boundary is crossed the instant the merge predicate is allowed to run without the effect ledger in its input set. At that point the orchestrator is optimizing plan coherence while being blind to world coherence, and those two can diverge arbitrarily far. ## Operational Effect The downstream consequence is a class of failure that is *self-concealing*. The merge log is internally consistent. The compensation ledger shows all losers cleanly reverted. The winning branch's record shows its plan executed. No invariant inside the orchestrator fires, because every invariant the orchestrator checks is scoped to branch namespaces and plan state — and those are exactly the things that were correctly rolled back and correctly committed. The contradiction lives only in the external world, where it surfaces later as a resource that "mysteriously" reverted, a notification for a plan that "never ran," or a double-apply that the ledger attributes to a single branch. The guardrail is structural: **the merge is not a function of branch states; it is a function of branch states conjoined with the effect ledger, and it must be able to fail.** A merge that cannot return `Conflict` or `IrreversiblePending` is not a merge, it is a commit with extra steps. The orchestrator must carry, per branch, an effect ledger of `(effect_id, resource, written_value, reversible, compensated)` and must refuse to declare a round successful while any entry is `reversible=false` and uncompensated, or while any two entries alias the same resource non-commutatively. Speculation is only safe over effects the system can name, key, and un-name. ```python from dataclasses import dataclass, field from enum import Enum class SpeculationBoundaryError(RuntimeError): """Raised when a speculative round would commit effects it cannot safely un-run.""" pass class Reversibility(Enum): REVERSIBLE = "reversible" COMPENSABLE = "compensable" # un-run via compare-and-revert, keyed to effect_id IRREVERSIBLE = "irreversible" @dataclass(frozen=True) class Effect: branch_id: str effect_id: str resource: str written_value: str reversibility: Reversibility @dataclass class Branch: branch_id: str effects: list[Effect] = field(default_factory=list) def assert_round_is_speculatable(branches: list[Branch]) -> None: # 1. Irreversible effects may not be forked speculatively; they must be merge-gated. for b in branches: for e in b.effects: if e.reversibility is Reversibility.IRREVERSIBLE: raise SpeculationBoundaryError( f"branch {b.branch_id} carries irreversible effect {e.effect_id} " f"on {e.resource}; defer behind merge gate, do not fork" ) # 2. Non-commutative aliasing: two branches writing the same resource is a conflict, # not a race to be resolved by plan scoring. by_resource: dict[str, set[str]] = {} for b in branches: for e in b.effects: by_resource.setdefault(e.resource, set()).add(b.branch_id) for resource, writers in by_resource.items(): if len(writers) > 1: raise SpeculationBoundaryError( f"resource {resource} mutated by multiple branches {sorted(writers)}; " f"compensation keyed on resource-prior would clobber the winner" ) def compensate_loser(branch: Branch, world: dict[str, str]) -> None: # Compare-and-revert: only undo if the world still holds what this effect wrote. for e in branch.effects: if e.reversibility is Reversibility.IRREVERSIBLE: raise SpeculationBoundaryError( f"cannot compensate irreversible effect {e.effect_id}; round was not speculatable" ) if world.get(e.resource) != e.written_value: raise SpeculationBoundaryError( f"effect {e.effect_id} on {e.resource} was overwritten by a sibling; " f"blind revert would corrupt committed state" ) world[e.resource] = "" ```