# Which invariants prevent speculative parallel agent branches from committing divergent world-state after rollback **Date of Record:** 2026 November 21 **Scope:** Applies to orchestrators that fan out multiple speculative agent branches against shared external side-effect surfaces (filesystem, APIs, message queues, databases) and reconcile them after a winner is selected. Does not apply to pure read-only fan-out or to branches whose effects are fully contained in an isolated sandbox that is destroyed on rollback. ## Core Distinction Speculative parallel execution of agent plans is not the same problem as speculative execution in CPUs. A CPU branch that mispredicts leaves no observable trace because architectural state is committed only at retirement, and the reorder buffer holds the speculative writes. An LLM agent branch, by contrast, *acts through tools whose effects are irreversible and externally visible the moment they occur* — an email sent, a row inserted, a webhook fired. The distinction that matters operationally is between **effect-producing actions** (non-idempotent, externally observable, unrecallable) and **state-producing actions** (local, reversible, checkpointable). The failure mode this record covers: a speculative branch performs an effect-producing action, is later designated the loser during reconciliation, and rollback restores local state but cannot un-send the effect. The orchestrator then commits the winner's plan on top of a world-state that already contains the loser's side effects, producing a **divergent world-state** — the committed plan's preconditions no longer hold because a discarded branch mutated the environment underneath it. The non-obvious part: this is not a race condition in the usual sense. Branches can be perfectly serialized, perfectly isolated in their own context windows, and still diverge, because the isolation boundary the orchestrator enforces (context, local scratchpad, checkpoints) is *narrower than the effect surface the branches actually touch*. ## What Was Observed A planner agent decomposed a task ("provision a staging environment and register it in the service catalog") into three candidate plans and speculatively executed all three in parallel, each with its own tool grant. Plan A and Plan B both called `catalog.register(service_id, endpoint)`. The reconciler scored Plan C highest on a downstream validation check and selected it as the winner, then issued rollback to A and B. Rollback for A and B restored their local checkpoints and released their scratch directories. It did not call `catalog.deregister`. Both A and B had already committed registration records. Plan C then executed its own `catalog.register` with the same `service_id`, which the catalog API treated as an upsert-and-append rather than a conflict, yielding three catalog entries for one logical service. A downstream health-check agent, reading the catalog, found three endpoints, fanned out probes to all three, and two of them (belonging to the discarded branches) responded healthy because their backing resources had not been torn down either. The divergence was silent. No branch errored. The reconciler's success predicate ("winner's plan validated") returned true. The corruption lived entirely in the gap between what rollback *believed* it had undone (local state) and what the branches had actually done (external effects). A second, subtler observation: branch A and branch B each independently called an external pricing API that rate-limited after the second call. Branch B's third call returned a 429 that its retry logic swallowed and replaced with a stale cached price. B's plan was discarded, so the stale price never entered the committed plan — but B's retry had consumed rate-limit budget that the winner's plan then needed, causing the winner's own pricing call to 429 and fall back to an even staler value. The loser degraded the winner through a shared, unaccounted-for resource. This is the same class of failure: a *side effect of speculation that rollback does not model*. ## Condition of Validity The invariant that must hold for speculative fan-out to be safe is: > **For every branch, the set of effects it can produce must be a subset of the set of effects rollback can invert, OR the branch must be prevented from producing effects outside that set until it is designated the winner.** Formally, let `E(b)` be the externally observable effects of branch `b`, and `R(b)` the effects rollback of `b` can invert. Safety requires `E(b) ⊆ R(b)` for all speculative `b`, or `E(b) = ∅` for all non-winning `b`. This condition is *valid* (the fan-out is safe) only when one of the following holds: 1. **Effect quarantine:** branches execute against a transactional or copy-on-write surface whose commits are gated on winner selection, so `E(b)` is buffered and either committed or dropped atomically. 2. **Effect ledger with compensation:** every effect-producing tool call is recorded in a per-branch ledger with a registered compensator (deregister, refund, delete, retract), and rollback replays the ledger in reverse. This is the saga pattern applied at the agent layer, and it is only valid if every effect has a *total* compensator. 3. **Capability scoping:** speculative branches are granted *read-only or idempotent-only* tool capabilities, and effect-producing capabilities are withheld until a branch wins. This is the strongest and simplest invariant: speculation never touches the irreversible surface. The condition is *invalid* — and the divergence above is reachable — whenever a branch can invoke a tool whose effect has no total compensator (append-only logs, sent messages, charged payments, rate-limit consumption) before winner selection. Note that rate-limit consumption and cache warming are effects with *no* compensator at all: you cannot un-consume a token or un-warm a cache. These are the effects most often omitted from the ledger because they are not "actions" in the tool-call sense. ## Decision Boundary The orchestrator must decide, per tool and per branch, whether that tool may run speculatively. The boundary is drawn on a single axis: **does the tool's effect have a total, callable compensator that the orchestrator itself can invoke without the branch's cooperation?** - If yes (and the compensator is idempotent and registered before the effect fires), the tool is *speculation-safe* and may run in any branch; rollback replays the ledger. - If no, the tool is *commit-only* and must be gated behind winner selection. A branch that needs it must emit a *deferred intent* — a declarative record of the call it would make — rather than making the call. The reconciler, after selecting a winner, executes the winner's deferred intents and discards the losers'. The critical boundary case: tools that *appear* compensable but are not total. `catalog.register` looks compensable via `catalog.deregister`, but if `register` is an upsert-append (as observed), `deregister(service_id)` removes *all* entries for that id, including the winner's — the compensator is not a true inverse. The decision boundary must therefore be drawn not on "is there a deregister endpoint" but on "is the compensator a provable inverse of this specific effect instance." Where that proof is unavailable, treat the tool as commit-only. A second boundary: shared resources with non-restorable budget (rate limits, connection pools, cache warmth). These must be *partitioned per branch* at fan-out time, or the branches are not actually isolated and the loser can starve the winner. Partitioning is the only sound treatment because compensation is impossible. ## Operational Effect Enforcing the invariant changes the orchestrator's architecture in three concrete ways: 1. **Tool grants become branch-phase-dependent.** A speculative branch is issued a capability token scoped to speculation-safe tools only. Commit-only tools are absent from its grant; an attempt to call one raises at the tool boundary, not at reconciliation. This moves the failure from "silent divergence discovered downstream" to "branch fails fast and is excluded from the candidate set." 2. **Rollback becomes ledger-driven, not checkpoint-driven.** Checkpoint restore handles local state; a separate effect ledger, appended *before* each effect fires (write-ahead), handles external state. Rollback replays the ledger's compensators in reverse order. The write-ahead ordering is mandatory: an effect recorded after it fires leaves a window where a crash between effect and record produces an uncompensable orphan. 3. **Reconciliation gains a precondition re-check.** After rollback of losers and before commit of the winner, the reconciler re-validates the winner's preconditions against current world-state. If a loser's uncompensated effect (or a shared-resource depletion) has invalidated them, the winner must be re-planned, not committed. This is the last line of defense and catches exactly the rate-limit-starvation case, where the loser's effect had no ledger entry at all. The net effect: speculative fan-out remains fast on the read-and-reason dimension (where branches are cheap and divergence is harmless) while serializing the effect dimension behind winner selection (where branches are expensive and divergence is permanent). The invariant is not "isolate branches" — it is "isolate branches *along the axis of irreversibility*." ```python class SpeculationDivergenceError(RuntimeError): """Raised when a speculative branch produces an effect rollback cannot invert, or when a winner's preconditions are invalidated by a loser's uncompensated effect.""" pass class EffectLedger: def __init__(self): self._entries = [] # (branch_id, effect, compensator) def record_before_fire(self, branch_id, effect, compensator): # Write-ahead: record MUST precede the effect. Compensator must be total. if compensator is None: raise SpeculationDivergenceError( f"branch {branch_id} attempted effect {effect!r} with no total compensator; " f"tool must be commit-only and gated behind winner selection" ) self._entries.append((branch_id, effect, compensator)) def rollback(self, losing_branches): for branch_id, effect, compensator in reversed(self._entries): if branch_id in losing_branches: compensator(effect) # must be idempotent and a true inverse class SpeculativeOrchestrator: def __init__(self, ledger, safe_tools, commit_only_tools): self.ledger = ledger self.safe_tools = safe_tools self.commit_only_tools = commit_only_tools def grant_for(self, branch_id, phase): if phase == "speculative": return set(self.safe_tools) # commit-only tools withheld return set(self.safe_tools) | set(self.commit_only_tools) def reconcile(self, branches, winner_id, preconditions): losers = {b.id for b in branches if b.id != winner_id} self.ledger.rollback(losers) # Re-check winner preconditions against post-rollback world-state. for pre in preconditions: if not pre.holds(): raise SpeculationDivergenceError( f"winner {winner_id} precondition {pre.name!r} invalidated by " f"uncompensated loser effect or shared-resource depletion; re-plan required" ) winner = next(b for b in branches if b.id == winner_id) for intent in winner.deferred_intents: intent.execute() # commit-only effects fire only here, post-selection ```