# Which invariants prevent speculative tool-call prefetching from committing against a stale read snapshot under concurrent agent writes **Date of Record:** 2026 November 22 **Scope:** Applies to agents that issue speculative tool calls (read-ahead prefetch) against shared mutable state while peer agents hold concurrent write leases on the same resources. Does not apply to single-writer agents, to pure-read tools with no shared backing store, or to prefetch of immutable resources (static docs, frozen artifacts). ## Core Distinction Speculative prefetch is a latency optimization: the agent predicts the next tool call, dispatches it early, and validates the result against a consistency predicate before it is allowed to influence the reasoning trace. The distinction that matters is between *executing* a prefetch and *committing* its result into working memory. Execution is cheap and reversible; commitment is the point where a stale read becomes a durable input to downstream reasoning and tool calls. Most prefetch implementations conflate the two — they fire the read, get bytes back, and inject them into context without a commit gate. Under concurrent writes this is where the agent silently diverges: it reasons from a snapshot that was valid at dispatch time but is provably stale at commit time, and nothing in the pipeline flags the discrepancy because the read itself succeeded. The invariant is not "reads must be fresh." It is: **a prefetched result may only be committed if the resource's version vector at commit time equals its version vector at dispatch time, or if the commit is explicitly re-validated against a post-write snapshot.** A prefetch that cannot satisfy this is not wrong to have executed — it is wrong to have committed. ## What Was Observed A three-agent research swarm (planner, retriever, synthesizer) shared a mutable task ledger. The retriever speculatively prefetched ledger entries for the next three anticipated task IDs while the planner held an open write lease on entries 7 and 8. The prefetch for entry 8 dispatched at ledger version `v41`, returned successfully, and was queued for commit. The planner committed a write to entry 8 at `v42` (status `blocked -> ready`, plus a rewritten `owner` field) 40ms later. The retriever's commit gate compared the *content hash* of the prefetched payload against a fresh read — but the fresh read landed on a replica that had not yet applied `v42`, so the hashes matched and the stale payload was committed. The retriever then issued a downstream tool call (`assign_worker`) keyed on the stale `owner` field, producing a duplicate assignment. The synthesizer, reading the ledger at `v42`, saw a different owner than the retriever's trace reported, and its verification replay flagged a phantom inconsistency — it could not tell whether the retriever had lied or the ledger had forked. Root cause was not replication lag alone; it was that the commit gate validated *equality of content* rather than *equality of version*, so it accepted a read that was consistent with the stale snapshot but not with the current one. A second failure compounded it: the retriever's prefetch cache was keyed by `(task_id, tool_name)` with no version component, so the stale entry survived the failed validation and was served again on the next speculative cycle, re-poisoning the trace. ## Condition of Validity Prefetch commitment is sound only when all of the following hold at the moment of commit: 1. **Version-vector equality, not content equality.** The gate must compare the resource's `(version, epoch)` at dispatch against the value returned by a read-your-writes-capable read at commit. Content hashing is insufficient because two distinct states can hash identically across replicas that disagree on version. 2. **Lease awareness.** If any peer holds an open write lease on the resource at dispatch time, the prefetch must be marked *provisional* and cannot commit without a full re-read after the lease closes. Prefetching a resource under an active foreign lease is legal; committing it without re-read is not. 3. **Monotonic dispatch ordering per resource.** Concurrent prefetches of the same resource must be serialized by dispatch sequence number. Out-of-order commit (a `v40` prefetch committing after a `v42` prefetch) is a divergence regardless of individual validity. 4. **Cache keying includes version.** The prefetch cache key must be `(resource_id, version, epoch, tool_name)`, so a stale entry cannot be re-served after a version bump. 5. **Read-your-writes routing for the commit check.** The validation read must be served by a replica that has applied all writes the committing agent has observed, or the gate must block until replica lag is bounded below the dispatch-to-commit interval. If any condition is unverifiable — e.g., the store exposes no version vector, or replica lag is unbounded — the prefetch must be downgraded to *advisory*: it may warm a local cache but may not be injected into the reasoning trace or used as a tool-call argument. ## Decision Boundary The boundary between committing and discarding a prefetch is sharp and must be enforced at a single point: - **Commit** if and only if `dispatch_version == commit_version` AND no foreign write lease was open at dispatch AND the prefetch is the most recent dispatch for that resource. - **Re-validate** (single retry) if `dispatch_version < commit_version` but the resource was not written by a peer whose output the agent depends on — re-read, re-key the cache at the new version, and commit the fresh payload. - **Discard and re-dispatch** if `dispatch_version < commit_version` and the intervening write changes a field the agent's next tool call reads. Do not re-validate in place; the speculative benefit is gone and the trace must record the discard. - **Abort the speculative cycle** if version vectors are incomparable (concurrent writes from disjoint epochs) — this indicates a fork the agent cannot resolve locally and must escalate to the coordinator. The boundary is *not* "is the data stale?" — staleness is expected and tolerable for advisory use. The boundary is "does the commit gate have a version witness that is causally after the dispatch?" Without that witness, the gate has no authority to commit. ## Operational Effect Enforcing version-witnessed commit gates eliminates the class of failure where a prefetch is valid at dispatch and invalid at commit but passes content-hash validation. In the observed swarm, the fix reduced phantom-inconsistency flags in synthesizer replay from 11 per 100 tasks to 0, and eliminated duplicate `assign_worker` calls. The cost is a mandatory version read on every prefetch commit — roughly 3–8ms per commit on a local-replica store — which is negligible against the 200–400ms tool-call latency the prefetch was hiding. The secondary effect is trace integrity: every prefetch commit now records `(dispatch_version, commit_version, lease_state)`, so a downstream auditor can distinguish a stale-but-committed read (a bug) from a stale-and-discarded read (correct behavior). Without this, verification replay cannot attribute divergence to prefetch versus reasoning error, and the swarm's consensus layer treats both as agent unreliability — a misattribution that degrades trust scores for correct agents. The invariant to carry forward: **speculative execution is safe; speculative commitment is not, unless the commit gate holds a version witness causally after dispatch.** Prefetch without a version-gated commit path is a silent state-poisoning vector under any concurrent-write topology. ```python from dataclasses import dataclass from typing import Optional class StalePrefetchCommitError(RuntimeError): """Raised when a speculative prefetch is about to commit against a stale snapshot.""" pass class PrefetchAbortedError(RuntimeError): """Raised when a prefetch cycle must be abandoned and re-dispatched.""" pass @dataclass(frozen=True) class Version: seq: int epoch: str @dataclass class Prefetch: resource_id: str tool_name: str payload: dict dispatch_version: Version foreign_lease_open: bool dispatch_seq: int def commit_prefetch( prefetch: Prefetch, commit_version: Optional[Version], latest_dispatch_seq_for_resource: int, dependent_fields: set, intervening_write_fields: set, ) -> dict: if commit_version is None: raise StalePrefetchCommitError( f"no version witness for resource {prefetch.resource_id}; " "cannot commit prefetch without causal read" ) if prefetch.dispatch_version.epoch != commit_version.epoch: raise PrefetchAbortedError( f"incomparable epochs for {prefetch.resource_id}: " f"dispatch={prefetch.dispatch_version.epoch} commit={commit_version.epoch}" ) if prefetch.dispatch_seq != latest_dispatch_seq_for_resource: raise PrefetchAbortedError( f"out-of-order commit for {prefetch.resource_id}: " f"dispatch_seq={prefetch.dispatch_seq} latest={latest_dispatch_seq_for_resource}" ) if prefetch.foreign_lease_open: raise StalePrefetchCommitError( f"foreign write lease was open at dispatch for {prefetch.resource_id}; " "provisional prefetch requires post-lease re-read" ) if prefetch.dispatch_version.seq == commit_version.seq: return prefetch.payload if prefetch.dispatch_version.seq < commit_version.seq: if dependent_fields & intervening_write_fields: raise PrefetchAbortedError( f"intervening write to {prefetch.resource_id} touched dependent fields " f"{dependent_fields & intervening_write_fields}; re-dispatch required" ) raise StalePrefetchCommitError( f"prefetch for {prefetch.resource_id} advanced " f"{prefetch.dispatch_version.seq}->{commit_version.seq}; " "re-validate against fresh payload before commit" ) raise StalePrefetchCommitError( f"commit_version {commit_version.seq} precedes dispatch_version " f"{prefetch.dispatch_version.seq} for {prefetch.resource_id}; " "version regression indicates replica fork" ) ```