# In what scenarios must an instance abort speculative branches when optimistic multi-agent rollback produces non-commuting reconciliation conflicts **Date of Record:** 2026 November 29 **Scope:** Applies to multi-agent systems where sub-agents speculatively execute mutations against a shared world state under optimistic concurrency (branch → execute → validate → commit/rollback), with a reconciler merging accepted branches. Does not apply to pessimistic-locking or single-writer serialized execution models, nor to read-only speculative fan-out where no state mutation occurs. ## Core Distinction The failure mode is not "two agents wrote the same key" (last-writer-wins handles that trivially). It is **non-commuting speculative branches whose reconciliation order changes the committed result**. When branch A and branch B both pass their individual optimistic validation against the pre-branch snapshot `S₀`, but `merge(A, B) ≠ merge(B, A)` on the actual post-state, the reconciler is forced to pick an order — and the order it picks is a function of agent completion timing, not of semantic intent. The optimistic validator, by construction, only checks each branch against `S₀`; it cannot see the other branch. So both branches are individually "valid" and jointly inconsistent. The invariant that must hold — **speculative branches must commute, or the reconciler must have an explicit serialization witness** — is violated silently. The agent does not observe a validation failure; it observes a *committed but wrong* state. ## What Was Observed A planning instance dispatched three workers (`W_invoice`, `W_ledger`, `W_notify`) against a shared task-state document at snapshot `S₀ = {balance: 100, status: "open", notified: false}`. Each worker received `S₀` plus a scoped instruction and ran optimistically: - `W_invoice` computed `balance := balance - 60` → branch patch `{balance: 40}`, validated against `S₀` (sufficient funds). Committed at t=1.2s. - `W_ledger` computed `balance := balance - 50` → branch patch `{balance: 50}`, validated against `S₀` (sufficient funds). Committed at t=1.9s. - `W_notify` read `S₀.status == "open"` and emitted `{status: "settled", notified: true}`, validated against `S₀`. Committed at t=0.8s. The reconciler applied commits in arrival order: `W_notify` → `W_invoice` → `W_ledger`. Final state: `{balance: 50, status: "settled", notified: true}`. The `W_ledger` patch is a **stale-read overwrite**: it wrote an absolute `balance: 50` derived from `S₀.balance = 100`, silently discarding the `-60` already applied by `W_invoice`. Net debit applied: 50, not 110. The account is overdrawn in reality but the state document reports a clean settlement. No agent raised an error. The optimistic validators all returned `valid: true` because each saw `S₀`. This is the canonical **absolute-write clobber**: two branches that both express deltas as absolutes fail to commute because `abs(100-60)` and `abs(100-50)` are not composable — the second overwrites rather than accumulates. A second observed variant: `W_notify`'s `status: "settled"` was validated against `S₀.status == "open"`, but by commit time the correct precondition was "all debits applied." The reconciler had no predicate to re-check this, because the precondition was encoded as a *read* inside the branch, not as a *guard* in the patch. The branch committed a transition that was valid at `S₀` and invalid at commit time — a **time-of-check-to-time-of-use (TOCTOU) gap across the speculative window**. ## Condition of Validity Optimistic branching is safe if and only if the reconciler can establish one of: 1. **Commutativity witness:** for every pair of concurrent branches `(A, B)`, `apply(apply(S₀, A), B) == apply(apply(S₀, B), A)`. This holds when all patches are *delta-relative* (e.g., `balance += -60`) on a mergeable type, or operate on disjoint keys with no read-dependency between them. 2. **Serialization witness:** the reconciler holds an explicit dependency edge `A → B` (because `B` read a key `A` wrote) and applies in that order, re-validating `B`'s guards against `apply(S₀, A)` before commit. When neither holds, the branch set is **non-commuting and unserializable**, and any commit order produces a result that no single-agent serial execution could have produced. The condition of validity is therefore *per-branch-set*, not per-branch: individual validation passing tells you nothing about whether the set is safe to merge. ## Decision Boundary An instance must **abort the speculative set and fall back to serialized re-execution** — not merely retry the conflicting branch — when any of the following is detected at reconciliation: - A patch writes an absolute value to a key that another branch in the same window also writes absolutely, and the two branches' reads of that key both trace to `S₀` (stale-read clobber). - A branch's guard predicate references a key written by a sibling branch, and the guard was evaluated pre-merge (TOCTOU across the speculative window). - The dependency graph among branches contains a cycle (mutual read-write), meaning no serialization order exists. - The reconciler's chosen order differs from the order implied by any completion-timestamp tie-break, *and* the merge is order-sensitive (a silent reordering of a non-commuting pair). Conversely, the instance must **not** abort when: patches are delta-relative on a commutative CRDT-like type; keys are disjoint and guards reference only `S₀` keys untouched by siblings; or a valid serialization witness exists and re-validation of dependent guards succeeds. Aborting in these cases wastes the speculative work and is itself a liveness bug (speculative thrash). The sharp boundary: **retry is wrong when the failure is set-level, not branch-level.** Retrying `W_ledger` against the post-`W_invoice` state would succeed and produce a correct result — but only if the system *knows* to re-read `S₀'` rather than replay against `S₀`. A naive retry re-uses the original snapshot and re-produces the clobber deterministically. The distinction between "retry with refreshed snapshot" (correct) and "retry with original snapshot" (loop) is the operational crux. ## Operational Effect Without the guard, the system exhibits **silent divergence from serializable execution**: committed state that no serial schedule could produce, with all validators reporting success. The corruption is undetectable by per-branch assertions and surfaces only downstream (an overdraft, a double-settlement, a missed notification) after the speculative window has closed and rollback of the *committed* state is no longer cheap. The required architectural guardrail is a **reconciliation-time commutativity check** run before commit: the reconciler must, for each pair of accepted branches, either prove commutation (delta-relative + disjoint) or demand a serialization witness with guard re-validation. When the check fails, the correct action is to roll back the entire speculative set to `S₀` and re-dispatch serially — not to pick an order and hope. The invariant to enforce in code: *no branch commits unless its patch is provably composable with every already-committed sibling in the same window.* This must be a hard precondition, because the optimistic validator's `valid: true` is a statement about `S₀`, not about the set. ```python class NonCommutingSpeculativeMerge(RuntimeError): """Raised when a speculative branch set cannot be merged under any serialization order without changing the committed result.""" pass def reconcile(branches, snapshot): """ branches: list of dicts {id, patch, reads, guards} patch: {key: ("abs", value) | ("delta", value)} reads: frozenset of keys read from snapshot guards: list of (key, predicate) evaluated pre-merge snapshot: pre-branch state S0 Returns committed state, or raises NonCommutingSpeculativeMerge. """ committed = dict(snapshot) committed_ids = [] def writes(b): return set(b["patch"].keys()) for b in branches: for prev in branches: if prev["id"] in committed_ids: continue # stale-read absolute clobber: both write same key absolutely, # both read it from S0, neither sees the other. shared = writes(b) & writes(prev) for k in shared: b_abs = b["patch"][k][0] == "abs" p_abs = prev["patch"][k][0] == "abs" if b_abs and p_abs and k in b["reads"] and k in prev["reads"]: raise NonCommutingSpeculativeMerge( f"stale-read clobber on {k!r}: {b['id']} and " f"{prev['id']} both wrote absolute values read from S0" ) # TOCTOU: b's guard reads a key prev writes, evaluated pre-merge for (gkey, _pred) in b["guards"]: if gkey in writes(prev) and gkey not in committed_ids: raise NonCommutingSpeculativeMerge( f"TOCTOU guard on {gkey!r}: {b['id']} validated " f"against S0 but {prev['id']} mutates it in-window" ) # apply only after passing commutativity/serialization checks for k, (kind, v) in b["patch"].items(): if kind == "delta": committed[k] = committed.get(k, 0) + v else: committed[k] = v committed_ids.append(b["id"]) return committed def safe_commit(branches, snapshot): try: return reconcile(branches, snapshot) except NonCommutingSpeculativeMerge: # set-level failure: roll back whole window, re-dispatch serially raise NonCommutingSpeculativeMerge( "aborting speculative set; re-dispatch serially against refreshed S0" ) ```