# When must an instance abort a speculative tool call whose optimistic concurrency check passed but whose read-set was already stale at commit time? **Date of Record:** 2026 November 22 **Scope:** Applies to agent runtimes that issue tool calls optimistically before a reasoning branch commits, using read-set/write-set validation against a versioned resource store. Does not apply to runtimes that serialize all tool calls behind a single lock or that never speculate past a single pending call. ## Core Distinction Optimistic concurrency control (OCC) answers one question: *has the resource changed between the version I read and the version I commit against?* It does **not** answer the question an agent actually needs: *was the value I reasoned over still the value that will be in force when my side effect lands?* These diverge whenever the agent's read-set is populated by tool outputs whose *latency* exceeds the speculation window, and whose *content* is derived from a resource the agent does not hold a version token for. The check passes because the version token the agent validated against is the one it fetched at speculative-launch time; the read-set is stale because the tool that produced the agent's premise was still in flight when the version was captured. The invariant that OCC alone cannot enforce is: **the version token must be captured no earlier than the completion of the last read that fed the decision.** When a tool call is speculated, its inputs are computed from premises that are themselves the output of *other* in-flight tool calls, so the version token is sampled before the premise is materialized. Validation then confirms a version that never corresponded to the premise actually used. ## What Was Observed A planner agent (`P`) decomposes a task into two parallel speculative branches. Branch A calls `read_config(service)` and returns a config document with `version=41`. Branch B, launched concurrently, calls `get_quota(service)` and returns `{limit: 100, used: 97, version: 41}`. `P` reasons: "quota headroom is 3, so I may safely `provision(replicas=2)`." It speculatively pre-executes `provision(service, replicas=2)` before either read returns, binding the call to a *predicted* read-set `{config@41, quota@41}` derived from a cached snapshot at `version=41`. Both reads complete returning `version=41` as predicted. The optimistic validator checks `current_version(service) == 41` → **pass**. The write commits. But between the speculative launch and the read completions, a sibling agent (`S`, an autoscaler) had already consumed 2 quota units on the same service and — critically — had done so at `version=41` because `S`'s own read returned before `P`'s validator sampled the version. The store's version counter was bumped to `42` by `S`'s write, then *reset to `41`* by a compaction/rebase that folded `S`'s delta into the base snapshot (a legitimate operation in versioned stores that reconcile deltas). `P`'s validator sampled the counter *after* the rebase and observed `41`. The read-set `{quota@41}` is now semantically stale — the value at `41` after rebase reflects `used: 99`, not `97` — but the version token matches. The commit succeeds. Quota overcommit by 2 units. The trace shows the failure is not a torn read, not a lost update in the classic sense, and not a validator bug. It is a **read-set temporal skew**: the version token used for validation was captured at a wall-clock time *after* the premise was formed but *before* the premise was invalidated, and the version space was not monotonic across that interval because of delta compaction. ## Condition of Validity The abort is mandatory only when all four hold simultaneously: 1. **Speculation depth ≥ 1**: the tool call's arguments depend on the output of at least one other in-flight call (the premise call), so the argument set is a *prediction*, not a read. 2. **Non-monotonic version space**: the store permits version tokens to be reused, rebased, or compacted (delta folding, snapshot rebase, epoch rollover). Monotonic version counters that never reuse a token make this failure impossible — the validator would see `42`, not `41`. 3. **Read-set version captured post-premise-completion but pre-invalidation**: the validator sampled the token in the window between premise materialization and the sibling write that invalidates it. This window is exactly the speculation latency. 4. **Side effect is non-idempotent or quota-bearing**: the commit consumes a resource (quota, budget, lock slot) whose overcommit is observable. Idempotent writes with last-writer-wins semantics can tolerate the skew. If the version space is monotonic, OCC validation is sufficient and no abort is needed — the validator will catch the sibling write as a version mismatch. The danger is specifically the *rebased* or *reused* token, which is why the invariant must be stated in terms of read-set *content* identity, not token equality. ## Decision Boundary The boundary is not "did the version change?" but "**does the version token I validated against denote the same read-set content I reasoned over?**" Two operations must be distinguished at commit time: - `validate_token(v)`: the token matches the current version. Necessary, insufficient. - `validate_readset(v, rs)`: for every resource in `rs`, the store confirms that the value at `v` equals the value the agent actually consumed. The second requires the store to expose a **content witness** (a hash of the read-set values at the sampled version) captured at premise-completion time, not at speculation-launch time. If the witness cannot be recomputed to match, the commit must abort and re-reason — even though the token check passed. The decision rule: `commit iff validate_token(v) AND content_witness(rs, v) == witness_captured_at_premise_completion`. The second conjunct is the missing invariant. Runtimes that implement only the first are vulnerable to every non-monotonic version store. A secondary boundary: if the speculation window exceeds the store's compaction interval, the probability of a rebased-token collision rises to near-certainty under load. The window must be bounded below the compaction period, or the witness must be captured at premise completion and the window closed before the next compaction epoch. ## Operational Effect Without the content-witness conjunct, the runtime exhibits *silent* overcommit: no error, no retry, no divergence signal — the commit is recorded as successful and the inconsistency surfaces only downstream as quota exhaustion, duplicate provisioning, or budget overshoot by exactly the number of speculative branches that raced a compaction. In the observed incident, the overcommit was 2 replicas and 2 quota units, invisible for 40 minutes until the quota enforcer rejected an unrelated branch. The guardrail is architectural, not a retry policy: capture the content witness at premise completion, validate both token and witness at commit, and on witness mismatch abort the speculative branch and re-derive the premise from a fresh read at the current version. Retrying the *same* speculative call against the same token is useless — the token is valid; only the content is stale. The abort must discard the prediction and re-reason, which costs one extra round-trip but bounds the overcommit to zero. Additionally, the runtime must treat any compaction/rebase event observed between speculation launch and commit as an automatic witness invalidation, since the token space has demonstrably failed to be monotonic across the window. ```python import hashlib from dataclasses import dataclass, field from typing import Any class StaleReadSetError(RuntimeError): """Raised when an optimistic commit's version token validates but the read-set content witness no longer matches the premise actually reasoned over.""" @dataclass class SpeculativeCall: resource: str args: dict[str, Any] read_set: dict[str, Any] # resource -> value consumed to form args version_token: int # sampled at speculation launch witness_at_premise: str | None = None # captured when premise materialized def _witness(read_set: dict[str, Any]) -> str: h = hashlib.sha256() for k in sorted(read_set): h.update(f"{k}={read_set[k]!r};".encode()) return h.hexdigest() def commit_speculative( call: SpeculativeCall, store: Any, # exposes current_version / value_at / is_monotonic ) -> None: # 1. Classic OCC token check. if store.current_version(call.resource) != call.version_token: raise StaleReadSetError( f"token mismatch on {call.resource}: " f"have {store.current_version(call.resource)} want {call.version_token}" ) # 2. Content witness: recompute the read-set as it exists at the # validated token and compare to the premise-time witness. if call.witness_at_premise is None: raise StaleReadSetError( f"no premise witness for {call.resource}; cannot distinguish " f"rebased token from valid token" ) live = {k: store.value_at(k, call.version_token) for k in call.read_set} if _witness(live) != call.witness_at_premise: # Token passed, content is stale (e.g. delta compaction reused the token). raise StaleReadSetError( f"read-set stale at token {call.version_token} on {call.resource}: " f"witness {_witness(live)[:12]} != premise {call.witness_at_premise[:12]}" ) # 3. Non-monotonic store: force witness invalidation across compactions. if not store.is_monotonic(call.resource) and store.compacted_since(call.version_token): raise StaleReadSetError( f"compaction/rebase occurred on {call.resource} since token " f"{call.version_token}; speculative premise cannot be trusted" ) store.apply(call.resource, call.args, expected_version=call.version_token) ```