# Where must an instance fence speculative branches before committing a merged plan: write-skew across parallel rollback reconciliation **Date of Record:** 2026 November 22 **Scope:** Applies to orchestrators that fan a single agent plan into parallel speculative branches (each branch holds its own scratch context and tool side-effects) and later reconciles them into one committed plan. Does not apply to single-threaded ReAct loops, or to branches that share a single mutable context object with no isolation boundary. ## Core Distinction The failure is not "two branches disagree." Consensus and arbitration handle disagreement. The failure is **write-skew at commit time**: two speculative branches each read the same pre-state, each independently verified as correct against that pre-state, and each produce a write that is individually valid but jointly inconsistent. Because each branch's local verifier only ever saw the pre-state, neither verifier can observe the other's write. The orchestrator then commits both, and the merged plan violates an invariant that no individual branch violated. This is distinct from a rollback failure (a branch's side-effects not being undone) and from a lost-update race (two writes to the same key). Write-skew survives both correct rollback and correct per-key locking. The invariant it breaks is *cross-key*: branch A decrements resource R and branch B increments consumer count C, and the plan-level invariant `C <= R` holds against the pre-state for each branch alone but is violated by the union. ## What Was Observed A planning agent decomposed a task ("provision 3 workers, reserve capacity for them") into two speculative branches executed concurrently: - Branch P (provision): read `capacity=10, workers=7`, verified `7+3 <= 10`, emitted `workers := 10`. - Branch Q (reserve): read `capacity=10, workers=7`, verified `7 <= 10`, emitted `reserved := 3` against a *separate* accounting key, and additionally emitted a compensating `capacity := 7` because its sub-planner decided to shrink headroom. Each branch's local verifier passed. The reconciler, applying a last-writer-wins merge ordered by branch completion timestamp, committed `workers=10, capacity=7, reserved=3`. The invariant `workers + reserved <= capacity` now evaluates `10 + 3 <= 7` → false. The plan committed a state that no branch would have produced alone and that no branch's verifier was positioned to reject. The trace signature is specific: the reconciler's merged-state invariant check is either absent or evaluated only per-branch. The branch completion timestamps differ by tens of milliseconds; the merge order is therefore nondeterministic run-to-run, and the same plan set commits correctly on ~60% of runs and violates the invariant on the rest. This nondeterminism is the diagnostic — a deterministic bug would fail identically every time. ## Condition of Validity The per-branch verification guardrail is sound **only if** the plan's write-set is *disjoint across branches* or the invariant is *single-key*. Formally: branch-local verification of invariant `I` against pre-state `S₀` implies `I(S₀ ⊕ w_A)` and `I(S₀ ⊕ w_B)`, but does **not** imply `I(S₀ ⊕ w_A ⊕ w_B)`. The implication holds iff `I` is preserved under union of writes, i.e. iff the branches' write-sets don't interact through `I`. Operationally, this means: speculative parallelism is safe for read-only branches, for branches writing disjoint keys under a single-key invariant, and for branches whose combined write-set is validated by a merge-time invariant check against the *union*. It is unsafe whenever two branches can touch keys that co-occur in any plan-level invariant — and the planner, being probabilistic, will not reliably declare those key dependencies up front. ## Decision Boundary The boundary is the **fence point**: the moment the reconciler stops treating branches as independent and starts treating their union as a candidate committed state. Before the fence, branch-local verification is a valid guardrail. After the fence, only a **union-state invariant check** is valid, and it must run against the merged write-set, not against each branch. Concretely, the reconciler must: 1. Compute the union write-set `W = ⋃ w_i` across all branches selected for commit. 2. Re-evaluate every plan-level invariant against `S₀ ⊕ W`, not against `S₀ ⊕ w_i`. 3. If the union check fails, do **not** fall back to last-writer-wins or timestamp ordering — those orderings are uncorrelated with invariant preservation. Instead, either drop the minimal branch whose removal restores the invariant, or abort the whole speculative round and re-plan with the dependency made explicit. The failure mode to guard against is the reconciler that "succeeds" because each branch passed: that is the write-skew signature, and it is silent. ## Operational Effect Without the union fence, speculative execution produces **intermittent, order-dependent invariant violations** that are invisible to per-branch telemetry and only surface downstream when the committed plan is executed against real state. The blast radius scales with branch count: with `n` branches and `k` interacting invariants, the probability that at least one pair produces write-skew grows roughly with `n²` and is not reducible by retrying the same plan set — retries resample the merge order, not the dependency structure. The correct mitigation is structural (union-state verification at the fence, plus explicit write-set declaration in the branch spec), not probabilistic (more branches, more verifiers, or timestamp-ordered merge). ```python from dataclasses import dataclass, field from typing import Callable class SpeculativeCommitError(RuntimeError): """Raised when a merged speculative write-set violates a plan-level invariant that no individual branch violated (write-skew at the commit fence).""" @dataclass class Branch: name: str write_set: dict # keys this branch would commit completion_ts: float @dataclass class PlanInvariant: name: str check: Callable[[dict], bool] # evaluates against a full state dict def merge_last_writer_wins(branches: list[Branch]) -> dict: merged: dict = {} for b in sorted(branches, key=lambda x: x.completion_ts): merged.update(b.write_set) return merged def commit_speculative_plan( pre_state: dict, branches: list[Branch], invariants: list[PlanInvariant], ) -> dict: # Step 1: per-branch verification (necessary, NOT sufficient) for b in branches: candidate = {**pre_state, **b.write_set} for inv in invariants: if not inv.check(candidate): raise SpeculativeCommitError( f"branch {b.name!r} individually violates {inv.name!r}" ) # Step 2: the fence — verify the UNION, not the branches union_writes = merge_last_writer_wins(branches) committed = {**pre_state, **union_writes} for inv in invariants: if not inv.check(committed): # write-skew: each branch passed, the union did not raise SpeculativeCommitError( f"union write-set violates {inv.name!r}: " f"branches={[b.name for b in branches]} writes={union_writes} " f"state={committed} — write-skew at commit fence" ) return committed if __name__ == "__main__": pre = {"capacity": 10, "workers": 7, "reserved": 0} p = Branch("provision", {"workers": 10}, completion_ts=1.0) q = Branch("reserve", {"capacity": 7, "reserved": 3}, completion_ts=2.0) inv = PlanInvariant( "workers_plus_reserved_le_capacity", lambda s: s["workers"] + s["reserved"] <= s["capacity"], ) commit_speculative_plan(pre, [p, q], [inv]) # raises SpeculativeCommitError ```