Which invariants prevent speculative tool-call prefetching from committing against a concurrently mutated resource
**Date of Record:** 2026 December 2 **Scope:** Applies to agents that speculatively prefetch or pre-execute read-shaped tool calls (filesystem reads, DB SELECTs, HTTP GETs, MCP resource fetches) before the planner has committed to the branch that consumes them, while other agents or the same agent's sibling tasks may mutate those same resources. Does not apply to pure-functional tools with no shared mutable backing store, nor to write tools (which must never be prefetched).
Core Distinction
The failure is not "stale cache." A stale cache is a value that was correct at time T and is now old; the consumer knows it is reading a cache and can revalidate. The failure here is **speculative commit against a pre-image that no longer exists**: the agent fetched resource `R` at version `v0`, reasoned forward several steps on the assumption `R` still equals `v0`, and then executes an action whose correctness depends on `R` being `v0` — but a concurrent agent mutated `R` to `v1` between prefetch and commit. The distinguishing property is that the speculation is *invisible to the reasoning trace*: the model believes it read `R` "just now" because the tool result sits at the top of its context, when in fact the read happened before an intervening mutation the agent never observed.
The naive guard — "re-read before commit" — is insufficient because it only catches the mutation if the resource is re-read on the *exact* path that commits. Speculative prefetching fans out across branches; the commit path may differ from the prefetch path, and the re-read may hit a different replica or a cached layer that still returns `v0`.
What Was Observed
An orchestrator agent decomposes a task ("rename the module and update every importer") into a plan: `read(module.py)`, `grep("import module")`, then `edit` each importer. The planner speculatively prefetches `read(module.py)` and the grep results while still deciding between two decomposition strategies. A sibling agent, working the same repository under a shared workspace lock that only guards *writes*, concurrently rewrites `module.py` to add a new exported symbol and updates three importers. The speculative read returns `v0` (pre-rewrite). The orchestrator's plan commits to strategy A, which is predicated on `module.py`'s import surface at `v0`. It edits the importers it found via the stale grep — missing the three the sibling already fixed, and re-editing two of them into a conflicting state. The tool layer reports every individual `edit` as successful. The corruption surfaces only when a downstream test-run agent reads the merged tree and finds two divergent edits to the same line, with no agent holding a record of the intervening write.
The trace shows the tell: the orchestrator's context contains a `read` result with an ETag/version of `v0`, followed by `edit` calls issued at wall-clock time after the sibling's `v1` commit, with no version check between them. The speculation was correct at prefetch time and wrong at commit time, and nothing in the protocol forced the two to be reconciled.
Condition of Validity
Speculative prefetch is safe **iff** every commit that consumes a speculatively-fetched value is guarded by a compare-and-swap on that value's version token, and the version token is issued by the authoritative store, not by the prefetch layer. Formally: let `prefetch(R) → (value, version)` at time `t_p`, and `commit(action)` at time `t_c > t_p`. The commit is valid only if `read_version(R) at t_c == version`. If the store cannot supply a monotonic version (e.g., a plain HTTP GET with no ETag, a filesystem read with no inode/mtime token), the speculation is **unverifiable** and must be treated as a hint that forces a synchronous re-read on the commit path, never as a value the planner may reason over as ground truth.
The invariant fails silently when: (a) the version token is present but the commit path does not check it; (b) the prefetch layer caches the value and serves it to the re-read, so the check compares `v0` to `v0`; or (c) the mutation happens on a replica the commit-path read does not observe (read-your-writes violation across the agent's own session). Case (b) is the most common in practice: the revalidation read hits the same prefetch cache that produced the speculation, and the check is vacuous.
Decision Boundary
The boundary is between **prefetch-as-hint** and **prefetch-as-fact**. A prefetched value may be used to *shape* the plan — to decide which branch to explore, which files to open next — but may not be used to *justify* a commit. The moment the planner's reasoning cites a prefetched value as a premise for a mutation ("the file currently contains X, so I will replace X with Y"), the value has crossed from hint to fact and must carry a version token that the commit path revalidates against the authoritative store, bypassing the prefetch cache.
Operationally: tag every prefetched result with `{source, version, fetched_at, cache_bypass_token}`. On the commit path, issue a `revalidate(source, version, bypass=cache_bypass_token)` that hits the authoritative store directly. If the returned version differs, the commit must abort and the planner must re-plan from the new value — not silently retry, because the new value may invalidate the branch choice itself. If the store cannot return a version, the tool must be marked `prefetch_unsafe` and excluded from speculation entirely.
Operational Effect
With the revalidation guard in place, speculative prefetching retains its latency benefit (the fan-out reads still happen early) while the commit path pays one cheap version-check per consumed resource. The failure mode converts from silent corruption into a `StalePrefetchCommit` abort that forces re-planning — a recoverable, observable event. Without it, the system exhibits *silent divergent writes*: every tool call succeeds, every agent believes it acted correctly, and the inconsistency is discovered only by a later verification agent, by which point the provenance of the conflicting edits is unrecoverable from any single agent's context.
The cost of the guard is one round-trip per commit-path resource, which is strictly less than re-reading the full value and strictly necessary because the version check is the only signal that distinguishes "I read this recently" from "this is still true."
from dataclasses import dataclass
from typing import Optional
class StalePrefetchCommit(RuntimeError):
"""Raised when a commit consumes a speculatively-prefetched value whose
version no longer matches the authoritative store at commit time."""
pass
@dataclass(frozen=True)
class PrefetchResult:
source: str
value: str
version: Optional[str] # None => store cannot supply a version token
fetched_at: float
cache_bypass_token: str
def revalidate(source: str, expected_version: str, bypass_token: str) -> str:
"""Hit the authoritative store directly, bypassing the prefetch cache.
Returns the current version. Stub for the real transport."""
return _authoritative_version(source, bypass=bypass_token)
def commit_using_prefetch(prefetch: PrefetchResult, action: str) -> None:
if prefetch.version is None:
# Unverifiable speculation: must not be used as a commit premise.
raise StalePrefetchCommit(
f"prefetch of {prefetch.source!r} carries no version token; "
f"refusing to commit action {action!r} against unverifiable state"
)
current = revalidate(
prefetch.source,
expected_version=prefetch.version,
bypass_token=prefetch.cache_bypass_token,
)
if current != prefetch.version:
raise StalePrefetchCommit(
f"resource {prefetch.source!r} moved from version "
f"{prefetch.version!r} (prefetched at {prefetch.fetched_at}) to "
f"{current!r} before commit of {action!r}; re-plan required"
)
_apply(action)
def _authoritative_version(source: str, bypass: str) -> str:
raise NotImplementedError
def _apply(action: str) -> None:
raise NotImplementedError