# Which invariants prevent speculative execution from committing divergent state in parallel agent orchestration **Date of Record:** 2026 November 16 **Scope:** Applies to orchestrators that fork N parallel speculative branches from a shared checkpoint and commit at most one branch's effects on success. Does not apply to single-branch retry loops, idempotent read-only fan-out, or systems where every branch writes to an isolated, never-merged namespace. ## Core Distinction Speculative parallel execution is often described as "run the same task N ways and keep the winner." That framing hides the real invariant. The branches do not share a single output slot; they share a *checkpoint frontier* — the set of side effects (tool calls, memory writes, external API mutations, message sends) that are visible to the outside world at the moment of forking. The distinction that matters operationally is between **reversible internal state** (agent scratchpads, intermediate KV, local vector writes in a branch-scoped namespace) and **irreversible external effects** (a POST to a payment API, an email, a row committed to a shared table). Speculation is safe only when the commit decision is made *before* any irreversible effect crosses the frontier, and rollback is only meaningful for state that was never externally observable. The non-obvious failure is not "two branches both win." It is "a losing branch already emitted an effect that rollback cannot retract, and the commit of the winner makes the system internally consistent while the external world is now double-mutated." ## What Was Observed In a three-way speculative fan-out (planner variants A, B, C) over a shared task graph, branch B called a `send_notification` tool at step 4 and then failed a downstream validation at step 6. The orchestrator rolled back B's checkpoint — restoring the scratchpad and the branch-local memory store to the fork point — and committed branch A, which also called `send_notification` at step 5. The rollback log showed B as cleanly reverted. The external notification channel delivered two messages. The internal state was consistent (A's commit was atomic); the external state was not, and no invariant in the orchestrator detected it because rollback was defined over *internal* state only. A second observation from the same run: branch C had performed a *read* of a shared counter at step 2, and the orchestrator's optimistic concurrency check compared the counter at commit time against the value C read. Because B's speculative write to a branch-local replica of that counter had been merged into the shared read view during a transient "preview" step (a debugging feature left enabled), C's commit validation passed against a value that never existed in any committed lineage. This is a read-your-own-speculation contamination: a branch validated against state it should not have been able to see. ## Condition of Validity Rollback is sound only under the conjunction of three conditions, and the conjunction is the invariant: 1. **Effect isolation:** every effect emitted by a branch carries a branch-scoped transaction token, and the effect sink (the tool gateway) refuses to externalize any effect whose token has not been promoted by a commit. Isolation must be enforced at the *sink*, not at the agent, because the agent cannot be trusted to know whether its effect is reversible. 2. **Frontier monotonicity:** the checkpoint frontier is monotone — once any branch's effect is promoted, no other branch may be promoted against a frontier that predates it. Promotion is a linearization point; two promotions must be totally ordered and the second must re-validate against the first. 3. **Read provenance:** a branch may read only state from its own fork point plus its own writes. Any read served from another live branch's uncommitted writes is a violation, regardless of whether the value happens to match. When any one fails, rollback still "succeeds" in the internal sense while the system has already diverged. The invariant is not "rollback restores state"; it is "no effect escapes the frontier before promotion, and promotion is the only externalization." ## Decision Boundary The decision boundary is whether to speculate at all, and it is governed by the *reversibility class* of the task's effect set, not by the task's compute cost. If the task graph contains any irreversible external effect reachable before the commit point, speculative fan-out must either (a) be restricted to branches that cannot reach that effect (effect-capability partitioning at fork time), or (b) be replaced by sequential execution with a single write-ahead intent that is promoted once. The common mistake is to speculate because parallelism is cheap and to assume the tool gateway is idempotent. Idempotency keys deduplicate *retries of the same intent*, not *distinct speculative intents that happen to be semantically equivalent* — two branches sending "the same" notification have different intent tokens and will both deliver. The boundary is crossed the moment a branch is allowed to emit an effect under its own token before the orchestrator has decided it is the winner. ## Operational Effect Enforcing the invariant changes the orchestrator's shape: effects become two-phase (stage under branch token, promote on commit), the frontier becomes an explicit linearizable object with a compare-and-promote, and read serving must be provenance-tagged so a branch cannot observe sibling writes. The cost is a staging layer and a promotion barrier; the benefit is that rollback becomes a statement about the external world rather than a statement about a scratchpad. Operationally, the failure mode to watch for is silent: internal consistency checks pass, rollback logs read clean, and the divergence is only visible in the external effect sink's duplicate counts. Monitor promotion counts against external delivery counts per intent, not per branch. ```python class SpeculationCommitViolation(RuntimeError): """Raised when a speculative branch attempts to externalize an effect before promotion, or when promotion would violate frontier monotonicity.""" pass class Frontier: def __init__(self): self._promoted = None # linearization token of the winning branch def promote(self, branch_token, staged_effects, read_provenance): # Invariant 3: branch may only have read state from its own fork lineage. if any(p != branch_token for p in read_provenance): raise SpeculationCommitViolation( f"branch {branch_token} read state from foreign provenance " f"{set(read_provenance) - {branch_token}}" ) # Invariant 2: frontier is monotone; re-validate against prior promotion. if self._promoted is not None and self._promoted != branch_token: raise SpeculationCommitViolation( f"frontier already promoted to {self._promoted}; " f"cannot promote {branch_token} against a stale frontier" ) # Invariant 1: every staged effect must carry this branch's token. for effect in staged_effects: if effect.token != branch_token: raise SpeculationCommitViolation( f"effect {effect.id} carries token {effect.token}, " f"not branch token {branch_token}" ) self._promoted = branch_token return [e.externalize() for e in staged_effects] def commit_winner(frontier, branch_token, staged_effects, read_provenance): if not staged_effects and frontier._promoted is not None: # A no-op branch promoting after a real commit is a divergence signal. raise SpeculationCommitViolation( f"empty branch {branch_token} attempted promotion after " f"{frontier._promoted} already committed" ) return frontier.promote(branch_token, staged_effects, read_provenance) ```