# How should an instance detect speculative tool-call divergence before commit in an optimistic-concurrency agent swarm **Date of Record:** 2026 November 21 **Scope:** Applies to swarms where worker agents speculate (pre-execute) side-effecting tool calls against a shared world-state under an optimistic read version, then reconcile at commit. Does not apply to strictly serialized single-writer agents or to read-only tool calls with no external effect. ## Core Distinction The failure is not "two agents wrote the same key." That is the classic lost-update, and last-writer-wins or a version check catches it. The non-obvious boundary is **speculative execution that reads a world-state version, produces a *derived* intermediate artifact, and then commits the artifact rather than the read.** The optimistic version check passes — the base row never changed — but the artifact was computed from a *sibling's speculative effect that was later rolled back*. The committing agent is internally consistent with a version that no longer reflects the durable world. Version-guarded commit is necessary but not sufficient: it validates the *input* version, not the *causal ancestry* of the speculative branch the agent actually consumed. ## What Was Observed A three-worker swarm (planner, executor, verifier) handling a "reconcile inventory" task. Executor A speculatively calls `reserve(sku=42, qty=5)` against world version `v100`, gets `remaining=15`, and derives a reorder artifact `reorder(qty=10)` from that `remaining`. Concurrently Executor B speculatively calls `release(sku=42, qty=5)` (a return) also on `v100`, gets `remaining=25`, and derives `reorder(qty=0)`. The coordinator commits A first (bumps to `v101`), then commits B. B's version guard fires (`v100 != v101`), B retries against `v101`, recomputes `remaining=25` correctly, commits `v102`. But A's `reorder(qty=10)` artifact was derived from a `remaining=15` that *included B's speculative release as if it were already durable*. After B's release commits, true remaining is 25 — A over-ordered by 10 units. A's commit passed its version check cleanly because A read `v100`, and `v100`'s base row was never mutated by B until B committed *after* A. The check validated the wrong thing. The trace, concretely: 1. A reads `v100`, base `stock=20`, speculates reserve → `remaining=15`, derives `reorder=10`. 2. B reads `v100`, base `stock=20`, speculates release → `remaining=25`, derives `reorder=0`. 3. A commits: guard `v100==v100` ✓ → `v101`, `stock=15`, `reorder=10` durable. 4. B commits: guard `v100!=v101` ✗ → retry on `v101`, `remaining=25`, `reorder=0` → `v102`, `stock=25`. 5. Final: `stock=25` but `reorder=10` still durable. Over-order. The divergence is invisible to any single-agent version check because the *derived artifact* outlived the *speculative premise* it depended on. ## Condition of Validity Optimistic commit is sound only under the invariant **"every derived artifact's dependency set must be re-validated against the durable state at commit, not merely its read version."** Formally: a commit of artifact `a` derived from read-set `R` at version `v` is valid iff for every `r ∈ R`, the durable value of `r` at commit time equals the value the agent observed — *including values that arrived via speculative branches that were later rolled back or reordered*. Version equality is a proxy that holds only when reads are pure. The moment a read reflects another agent's speculative write, version equality is decoupled from value equality, and the invariant must be enforced directly on the read-set, not on the version token. ## Decision Boundary The instance must choose between two behaviors at the moment it detects that its read-set includes a value whose provenance is a sibling's uncommitted speculation: - **Commit on version match** (unsafe): correct only if the speculation is provably durable (already committed) or provably idempotent and commutative with the agent's own effect. - **Abort and recompute from durable state** (safe): mandatory when the speculative premise is non-commutative with the sibling's pending effect, or when the derived artifact is a *decision* (reorder qty, routing choice, approval) rather than a *reversible write*. The boundary is **commutativity of the speculative premise with the sibling effect**, not version freshness. If A's reserve and B's release commute on `stock` (they do — addition is commutative), A's *derived decision* still does not commute, because decisions are nonlinear functions of the observed value. This is the crux: the write commutes, the read-then-decide does not. ## Operational Effect Without the read-set re-validation invariant, swarms exhibit **silent over-commitment**: durable artifacts that are individually version-valid but collectively inconsistent with the converged world. Detection requires the coordinator to carry, per artifact, a *read-set manifest* with observed values (not just version IDs) and to diff observed-vs-durable at commit. Cheap version tokens are insufficient; the manifest costs O(|read-set|) per commit but is the only thing that catches rollback-orphaned derivations. The practical guardrail: speculative reads must be tagged `speculative:true` with their originating branch ID, derived artifacts must record their full read-set, and commit must reject any artifact whose read-set contains a `speculative:true` entry whose branch did not commit before the artifact. This converts a silent consistency bug into an explicit, retryable abort. ```python class SpeculativeDivergenceError(RuntimeError): """Raised when a derived artifact's read-set depends on a speculative branch that did not commit before the artifact, making version-guarded commit unsound.""" pass def commit_artifact(artifact, durable_state, committed_branches): """ artifact: { 'id': str, 'read_set': {key: {'observed_value': Any, 'version': int, 'speculative_branch': str | None}}, 'derived_from_branch': str | None, } durable_state: {key: current_value} committed_branches: set[str] of branch IDs that have durably committed """ for key, read in artifact['read_set'].items(): branch = read['speculative_branch'] if branch is not None and branch not in committed_branches: # The premise this artifact was derived from was never made durable. raise SpeculativeDivergenceError( f"artifact {artifact['id']} read {key!r} from speculative " f"branch {branch!r} which did not commit; version guard on " f"v{read['version']} is unsound because the observed value " f"{read['observed_value']!r} reflects an uncommitted sibling " f"effect. Abort and recompute from durable state." ) if durable_state.get(key) != read['observed_value']: raise SpeculativeDivergenceError( f"artifact {artifact['id']} observed {key!r}=" f"{read['observed_value']!r} but durable value is " f"{durable_state.get(key)!r}; read-set drifted despite " f"version match. Recompute required." ) return apply(artifact, durable_state) def apply(artifact, durable_state): # durable write path (omitted); returns new state return durable_state ```