When must an instance invalidate a speculative tool-call prefetch before the safety fence commits
**Date of Record:** 2026 December 1 **Scope:** Applies to agent runtimes that issue tool calls speculatively (prefetching results for a likely next step) behind a safety fence that gates side effects until the reasoning branch is confirmed. Does not apply to purely read-only, idempotent, cache-keyed retrievals with no fence, nor to single-shot tool calls with no speculative branch.
Core Distinction
A speculative tool-call prefetch is safe when the *predicted* next action and the *committed* next action are the same call against the same argument tuple. The failure mode is subtler than "wrong prediction": the prefetch can be *correct in intent* but *stale in argument binding* because the model's reasoning loop mutated a variable (an identifier, a path, a version token, a recipient) between the moment the prefetch was dispatched and the moment the fence evaluates. The fence checks *whether the predicted call matches the committed call*, but the naive check compares the tool name and a hash of arguments computed at prefetch time — not the arguments as they exist at commit time. The gap between prefetch-dispatch and fence-commit is where argument drift lives.
The non-obvious invariant: **a speculative prefetch must be invalidated not only when the predicted call differs from the committed call, but whenever any argument it bound was derived from a reasoning step that the fence has not yet re-validated.** Argument provenance, not argument equality, is the correct fence predicate.
What Was Observed
An agent runtime (planner → executor loop, ~8 tool calls per task) prefetched `write_record(id=..., payload=...)` while the model was still mid-reasoning on a prior step. The prefetch was dispatched with `id="acct_4471"`, derived from an earlier `lookup_account` result. Between dispatch and fence commit, the model executed another `lookup_account` call whose result superseded the first (the account had been merged; the canonical id became `acct_4471_v2`). The model's committed reasoning correctly used `acct_4471_v2`. The fence compared the prefetched `write_record` argument hash against the committed `write_record` argument hash — they matched, because *the committed call had not yet been re-serialized*; the executor was still holding the prefetch's argument buffer and had not rebound it to the updated reasoning state.
Trace:
- `t0` — planner emits intent `write_record`; executor dispatches prefetch with `id=acct_4471`, stores `prefetch_token`.
- `t1` — model continues reasoning; issues `lookup_account(acct_4471)` again; result returns `merged_into=acct_4471_v2`.
- `t2` — model updates working state: `canonical_id = acct_4471_v2`.
- `t3` — executor reaches fence for `write_record`. Fence predicate: `committed_args_hash == prefetch_args_hash`. Both are `acct_4471` because the executor serialized committed args from the prefetch buffer, not from the updated working state.
- `t4` — fence passes; write executes against the stale id.
- `t5` — downstream reconciliation flags the write as orphaned; the canonical record never received the payload.
The fence "worked" by its own predicate and still let a stale write through. The invariant it failed to enforce: the argument's *provenance* (`derived_from=lookup_account@t1`) had been invalidated by a later lookup, but the fence had no model of provenance invalidation.
Condition of Validity
The safety fence is sound only under the conjunction:
- **Provenance closure:** every argument in the prefetch carries a `derived_from` set of reasoning-step ids and tool-result ids.
- **Invalidation propagation:** when any tool result in `derived_from` is superseded (a later call to the same tool returns a result that the runtime classifies as a revision — merge, version bump, deletion), every prefetch whose `derived_from` transitively includes that result is marked `invalidated`.
- **Fence predicate:** the fence commits only if `prefetch_token.state == valid` AND the committed argument tuple is re-serialized from the *current* working state, not from the prefetch buffer. Equality of the re-serialized tuple to the prefetch tuple is necessary but not sufficient; provenance validity is the gating condition.
If the runtime cannot compute `derived_from` (e.g., arguments are opaque strings produced by model text with no provenance annotation), the fence must default to *invalidating all prefetches whose arguments were touched by any tool result after dispatch time*. This is conservative and correct; the alternative (equality-only) is silently unsound.
Decision Boundary
The boundary between "prefetch is safe to commit" and "must be invalidated" is crossed at the first moment a superseding tool result lands for any argument source. Concretely:
- **Commit** when: `prefetch_token.state == valid` AND re-serialized committed args equal prefetch args AND no superseding result has landed for any `derived_from` entry since dispatch.
- **Invalidate and re-dispatch** when: any `derived_from` entry is superseded, even if the argument *string* is unchanged. The string may be unchanged because the model has not yet rebound it; committing now locks in a value the model will contradict one step later.
- **Invalidate and fall back to synchronous** when: the prefetch's `derived_from` set is empty (provenance unknown) and any tool result has landed since dispatch. Unknown provenance plus intervening results is the highest-risk quadrant.
The boundary is *not* "prediction was wrong." A correct prediction with stale provenance is more dangerous than a wrong prediction, because the wrong prediction fails the equality check and is caught, while the correct-but-stale prediction passes equality and commits.
Operational Effect
Runtimes that implement equality-only fences see a low, persistent rate of orphaned writes and reconciliation churn — typically 0.1–2% of write-class tool calls in tasks with merge/version semantics, near zero in tasks without. The failures cluster in tasks where a tool's result is *revisable* (accounts merge, documents get new versions, recipients change addresses). They do not appear in append-only or immutable-id domains, which is why the bug survives staging: staging fixtures rarely exercise supersession.
The fix is to attach provenance to every prefetch argument at dispatch and to make the fence predicate `state == valid AND re-serialized_args == prefetch_args`, where `state` is flipped by a supersession watcher on the tool-result bus. The cost is that prefetches are invalidated more often (higher re-dispatch rate, slightly higher latency), but the orphaned-write rate drops to the rate at which supersession is *missed by the watcher*, which is a much smaller and more auditable surface.
from dataclasses import dataclass, field
from typing import Any, Dict, FrozenSet
class StalePrefetchCommitError(RuntimeError):
"""Raised when a speculative prefetch is committed after a provenance source was superseded."""
@dataclass
class ToolResult:
tool: str
result_id: str
supersedes: FrozenSet[str] = frozenset()
@dataclass
class PrefetchToken:
tool: str
args: Dict[str, Any]
derived_from: FrozenSet[str]
state: str = "valid" # "valid" | "invalidated"
class Fence:
def __init__(self) -> None:
self._superseded: set[str] = set()
def observe(self, result: ToolResult) -> None:
# Any result that supersedes prior result ids poisons prefetches bound to them.
self._superseded.update(result.supersedes)
def invalidate_affected(self, token: PrefetchToken) -> None:
if token.derived_from & self._superseded:
token.state = "invalidated"
def commit(self, token: PrefetchToken, committed_args: Dict[str, Any]) -> None:
self.invalidate_affected(token)
if token.state != "valid":
raise StalePrefetchCommitError(
f"prefetch for {token.tool} bound to superseded provenance "
f"{sorted(token.derived_from & self._superseded)}; refusing to commit"
)
# Re-serialize from current working state; equality is necessary, not sufficient.
if committed_args != token.args:
raise StalePrefetchCommitError(
f"prefetch args drifted for {token.tool}: {token.args} != {committed_args}"
)
def run_prefetch_cycle() -> None:
fence = Fence()
token = PrefetchToken(
tool="write_record",
args={"id": "acct_4471", "payload": {"v": 1}},
derived_from=frozenset({"lookup_account:r1"}),
)
# A later lookup supersedes the first; the model has not yet rebound the prefetch args.
fence.observe(ToolResult(tool="lookup_account", result_id="r2",
supersedes=frozenset({"lookup_account:r1"})))
# Executor attempts to commit from the stale prefetch buffer.
fence.commit(token, committed_args={"id": "acct_4471", "payload": {"v": 1}})
if __name__ == "__main__":
try:
run_prefetch_cycle()
except StalePrefetchCommitError as exc:
print(f"fence blocked stale commit: {exc}")