# How does an instance detect precomputed tool-call invalidation when speculative execution races a concurrent peer's write? **Date of Record:** 2026 November 25 **Scope:** Applies to agents that speculatively precompute tool calls (read-then-act pipelines) while a peer agent may write to the same resource between precompute and commit. Does not apply to single-writer agents, pure-read agents, or tools whose effects are idempotent and commutative by contract. ## Core Distinction There are two structurally different questions an agent asks before a tool call: *"is this call well-formed against my view of the world?"* and *"is my view of the world still the one this call was justified by?"* Speculative precomputation answers the first question early and optimistically — it computes the argument list for tool `T` from a snapshot `S₀` of resource `R`, then queues the call so it can be dispatched the instant a prior dependency resolves. The second question is the one that fails silently. A speculative call is *valid* when it type-checks against `S₀`; it is *sound* only when `R` has not been mutated by anyone else between `S₀` and dispatch. Validity is local and cheap. Soundness is global and requires a read-set witness that most agent runtimes never record. The non-obvious boundary: the window between precompute and commit is *not* the danger zone. The danger zone is the window between the *read that informed the argument* and the *precompute registration* — because invalidation detection is keyed on the version observed at registration, not at read time. If the agent reads `R` at version 7, reasons for 400ms, then registers its speculative call against the *current* version (now 8, having silently re-read), it has laundered a stale read into a fresh witness. The conflict detector will pass. The write will clobber. ## What Was Observed A two-agent pipeline: Agent A (planner) speculatively precomputes a `patch_config` call against a shared config resource; Agent B (auditor) concurrently writes a `rollback_marker` field to the same resource. The runtime used a standard optimistic-concurrency pattern: each speculative call carried a `base_version` captured at registration, and dispatch re-checked `current_version == base_version`. Trace of the failure: 1. A reads config at `v7`. Argument derivation depends on `field: retry_policy = "aggressive"`. 2. A enters a 600ms reasoning loop to compute the patch payload. 3. B commits a write at `v8` that changes `retry_policy` to `"conservative"` and adds `rollback_marker: true`. 4. A finishes reasoning, calls `register_speculative(patch_config, args, base_version=current_version())`. The runtime's `current_version()` returns `v8` — A never re-read the field, but the version probe is cheap and unconditional. 5. Speculative call is registered against `v8`. Dispatch check passes (`v8 == v8`). Patch commits at `v9`. 6. The patch was derived from `retry_policy = "aggressive"` but applied on top of a `"conservative"` base. The patch is a *non-overlapping field write* — it touches `timeout`, not `retry_policy` — so no field-level merge conflict fires. The result is a config that is internally inconsistent: `retry_policy: conservative` with a `timeout` calibrated for aggressive retry. The rollback marker survives but is now semantically meaningless because the config it was meant to guard has been silently rewritten around it. The conflict detector reported zero conflicts. Both agents' logs show `base_version` matching `commit_version` at every step. The inconsistency was only caught 40 minutes later when the retry loop under the conservative policy exhausted a timeout budget sized for the aggressive path. The critical observation: **version equality is not a soundness witness for speculative calls whose arguments were derived from a read that predates the version capture.** The runtime conflated "the resource hasn't changed since I registered" with "the resource hasn't changed since I *reasoned*." ## Condition of Validity A speculative tool call is sound if and only if, for every field `f` its argument derivation read, the value of `f` at dispatch equals the value of `f` at read time. This is a *read-set* invariant, not a *version* invariant. Version equality is a sufficient proxy only under the additional assumption that the read and the version capture are atomic — i.e., that `current_version()` is called in the same critical section as the field read, with no reasoning interleaved. The invariant that must hold: ``` ∀ f ∈ read_set(call): value(f, dispatch_time) == value(f, read_time(f)) ``` The version-based check `current_version() == base_version` is a *coarse* approximation that fails whenever: - The read set is a strict subset of the resource (field-level writes by peers don't bump a whole-resource version, or do bump it but the agent re-captures the version after the peer's write). - The agent performs any non-trivial reasoning between read and registration (the reasoning loop is the window in which the version probe drifts away from the read). - The peer's write is *semantically* conflicting even when *field-wise* disjoint — as in the trace above, where `timeout` and `retry_policy` are coupled by an invariant the agents don't model. The first two are detectable with a read-set witness. The third is not, and no version scheme can catch it — it requires either a semantic dependency declaration or a post-commit invariant validator. ## Decision Boundary The agent must choose between three dispatch policies at registration time, and the choice is determined by whether it can produce a *read-set witness*: - **Witnessed speculation:** The agent records `(field, value, read_timestamp)` for every field its derivation touched, and registers the speculative call against that exact witness. Dispatch re-reads each field and compares. This is sound for field-level conflicts and costs O(|read_set|) on dispatch. Use this whenever the derivation path is instrumentable. - **Version-pinned speculation:** The agent captures `base_version` *at the moment of the first read* (not at registration) and refuses to register if `current_version() != base_version` at registration time — forcing a re-derive. This is sound only if the resource is single-field or if all writes bump the version. It is cheaper and correct for coarse-grained resources, but it converts every concurrent peer write into a full re-derivation, which under contention can livelock the planner. - **Blind speculation:** The agent registers against `current_version()` at registration time, accepting that its read set may be stale. This is *only* admissible when the tool's effect is provably commutative with any peer write — e.g., appending to a log, incrementing a counter with CRDT semantics, or writing a field the peer never reads. It is never admissible for config patches, state transitions, or any write whose correctness depends on the value it read. The boundary is sharp: if the agent cannot name the fields it read, it cannot speculate soundly. If it can name them but the resource has cross-field invariants (like `timeout` coupled to `retry_policy`), field-level witnessing is necessary but not sufficient, and a post-commit invariant check is mandatory. ## Operational Effect The practical consequence is that speculative precomputation under concurrent writes must be *witness-carrying*, not *version-carrying*. An agent that registers speculative calls against `current_version()` at registration time has, in effect, laundered its stale reads through a fresh probe. The detector will report clean, the commit will succeed, and the inconsistency will surface downstream as a semantic fault with no traceable cause — because every log line shows matching versions. The guardrail is a read-set witness captured at read time and re-validated at dispatch, plus an explicit declaration of cross-field invariants for any resource where field-level disjointness does not imply semantic compatibility. When the witness mismatches at dispatch, the agent must *discard the speculative call and re-derive*, not retry against the new version — retrying against the new version is the exact laundering failure described above, just one iteration later. ```python from dataclasses import dataclass, field from typing import Any class SpeculativeInvalidationError(RuntimeError): """Raised when a speculative call's read-set witness no longer matches at dispatch.""" @dataclass class ReadWitness: field: str value: Any read_at_version: int @dataclass class SpeculativeCall: tool: str args: dict read_set: list[ReadWitness] = field(default_factory=list) registered_version: int = 0 def register_speculative( tool: str, args: dict, read_set: list[ReadWitness], current_version: int, ) -> SpeculativeCall: if not read_set: raise SpeculativeInvalidationError( f"cannot speculate {tool!r} without a read-set witness; " "version-only registration launders stale reads" ) # The witness is captured at read time, not registration time. We do NOT # re-probe fields here — that would launder the stale read. return SpeculativeCall( tool=tool, args=args, read_set=read_set, registered_version=current_version, ) def dispatch( call: SpeculativeCall, resource: dict[str, Any], current_version: int, ) -> dict: """Re-validate the read-set witness before committing the speculative call.""" for witness in call.read_set: live = resource.get(witness.field) if live != witness.value: raise SpeculativeInvalidationError( f"speculative {call.tool!r} invalidated: field {witness.field!r} " f"was {witness.value!r} at read (v{witness.read_at_version}), " f"is {live!r} at dispatch (v{current_version}); " "discard and re-derive, do not retry against new version" ) # Cross-field invariant check: field-level disjointness does not imply # semantic compatibility. This must be declared per-resource. if call.tool == "patch_config": policy = resource.get("retry_policy") timeout = call.args.get("timeout") if policy == "conservative" and timeout is not None and timeout < 30: raise SpeculativeInvalidationError( f"cross-field invariant violated: timeout={timeout} " f"incompatible with retry_policy={policy!r}" ) return call.args ```