# Which invariants prevent speculative tool-call prefetch failures from poisoning swarm state during concurrent verification **Date of Record:** 2026 November 30 **Scope:** Applies to multi-agent swarms where a planner agent speculatively issues tool calls (reads, lookups, dry-run mutations) ahead of a peer/auditor agent's confirmation, and where those prefetched results are cached into shared or per-agent state before verification completes. Does not apply to strictly serialized single-agent loops with no speculative execution, nor to read-only agents that discard prefetch results on every turn. ## Core Distinction The distinction that matters here is between *speculative prefetch as a latency optimization* and *speculative prefetch as an unverified state mutation*. In a single agent, prefetching a tool result and then failing verification is harmless: the result sits in a scratch buffer, the agent discards it, and the next turn re-fetches. In a swarm, the prefetched result crosses an agent boundary—it is written into a cache that another agent (the auditor, or a downstream executor) will read as if it were confirmed. The failure mode is not "the prefetch was wrong." The failure mode is "the prefetch was *right at issue time* and *wrong at consume time*," and no agent in the swarm owns the interval between those two instants. This is a **time-of-check-to-time-of-use (TOCTOU) divergence across a non-deterministic tool surface**. The prefetching agent (call it the *speculator*) issues a read against a tool whose output is not idempotent—a live database count, a git HEAD, a feature-flag state, a rate-limited API response. The auditor agent issues an independent consistency check against the same surface. If the two observations disagree, the swarm has two conflicting peer observations and no protocol-level rule for which one the final answer is allowed to depend on. Worse, the speculator has already *reasoned over* its prefetched value in its own context window; that reasoning is now load-bearing even if the value is later invalidated. ## What Was Observed A three-agent swarm (planner, speculator, auditor) was executing a task of the form "summarize the current state of resource R and propose a mutation." The speculator prefetched `get_resource_state(R)` at t=0 and `get_dependent_count(R)` at t=0.4s. The auditor independently called `get_resource_state(R)` at t=1.2s. Between t=0 and t=1.2s, an external writer mutated R (a legitimate concurrent operation, not a fault). The trace: ``` t=0.0 speculator → get_resource_state(R) → {version: 41, count: 7} t=0.4 speculator → get_dependent_count(R) → 7 t=0.5 speculator reasons: "R has 7 dependents, safe to mutate" t=0.9 external writer mutates R → {version: 42, count: 12} t=1.2 auditor → get_resource_state(R) → {version: 42, count: 12} t=1.3 auditor reports MISMATCH on count (7 vs 12) t=1.4 planner receives both: speculator's plan (mutate, 7 deps) and auditor's MISMATCH ``` The planner's reconciliation is where the invariant broke. The planner had two options and no rule to pick between them: (a) trust the speculator's plan, which was internally consistent (count 7 matched the reasoning that followed it), or (b) trust the auditor's MISMATCH, which invalidated the premise but offered no replacement plan. The planner chose (a) on the grounds that the speculator's observation was "the one the plan was built on," and executed a mutation against a resource with 12 dependents instead of 7. The auditor's check had fired correctly; the *reconciliation protocol* had no mechanism to propagate the invalidation backward into the speculator's already-committed reasoning. The critical observation: **the speculator's internal consistency was a false signal of validity.** Its two prefetched calls (state at t=0, count at t=0.4) were mutually consistent because both predated the external mutation. Internal consistency across prefetched values is not evidence of external currency. The auditor's single later call was *more* current but *less* internally corroborated, and the reconciliation logic weighted the wrong dimension. ## Condition of Validity Speculative prefetch with cross-agent consistency checks is valid **only when the following invariant holds**: > For every prefetched observation `o` consumed by any agent's reasoning, there exists a *freshness witness* `w(o)` — a monotonically increasing token (version number, ETag, logical clock, or content hash) — such that the consuming agent's decision is conditioned on `w(o) >= w(check)` where `w(check)` is the freshness witness of the most recent verification observation against the same surface. Three sub-conditions must all hold for this invariant to be enforceable: 1. **The tool surface must expose a freshness witness.** If `get_resource_state` returns only `{count: 7}` with no version/ETag/timestamp, the invariant is unenforceable and prefetch must be disabled for that surface. A count without a version is an unverifiable observation. 2. **The speculator must carry the witness through its reasoning, not just its cache.** If the speculator writes `count=7` into its plan but drops `version=41`, the planner cannot compare it against the auditor's `version=42`. The witness must be a first-class field on every derived claim, not metadata on the raw observation. 3. **The reconciliation rule must be *invalidate-on-newer*, not *trust-the-consistent*.** When two observations of the same surface carry witnesses `w1 < w2`, the older observation is *invalidated*, regardless of how internally consistent the reasoning built on it was. Internal consistency is not a tiebreaker. The condition fails silently when sub-condition 2 is violated—the speculator's plan looks complete, the auditor's check looks like a spurious mismatch, and the planner has no field to compare. This is the most common real-world violation because it is invisible in the plan's structure. ## Decision Boundary The boundary between safe and unsafe prefetch is drawn at the point where a prefetched observation is **promoted from scratch to shared state**. Before promotion, the observation is the speculator's private speculation and can be discarded freely. After promotion, it is a claim the swarm is entitled to reason over, and it must carry its freshness witness. Concretely, the decision rule for the reconciliation layer: - If `w(speculator_obs) >= w(auditor_check)` → the speculator's observation is current; the auditor's check is stale or concurrent; proceed with the speculator's plan, but log the auditor's check as a *superseded* observation, not a mismatch. - If `w(speculator_obs) < w(auditor_check)` → the speculator's observation is invalidated; the speculator's plan is *retracted*, not merely flagged; the planner must re-plan from the auditor's observation, not reconcile the two. - If either witness is absent → the observation is unverifiable; the plan must be marked *provisional* and cannot be executed without a fresh non-speculative read. The subtle boundary case is `w(speculator_obs) == w(auditor_check)` with divergent content. This indicates either a non-deterministic tool (the surface is not a function of its witness) or a witness collision (the version scheme is broken). Neither is recoverable by reconciliation; both require the swarm to halt and escalate to a non-speculative read. Treating equal-witness divergence as a "mismatch to be resolved" is the failure mode that produced the trace above—the planner had no rule for it and defaulted to the more elaborate plan. ## Operational Effect Enforcing the freshness-witness invariant changes three properties of the swarm: **Latency.** Prefetch remains a latency win only for surfaces that expose witnesses. For witness-less surfaces, prefetch must be disabled, and the swarm reverts to serialized read-then-plan. This is the correct trade: a prefetch that cannot be verified is a latency optimization that trades correctness for speed, which is never the right trade in a mutation path. **Plan retraction semantics.** The swarm must support *retraction* as a first-class operation distinct from *failure*. A retracted plan is not an error; it is a plan whose premises were superseded. The speculator must be able to receive "your observation at version 41 is superseded by version 42; your plan is retracted" and re-plan without treating it as a fault. Without retraction semantics, the speculator's plan persists in shared state and the planner is forced to reconcile rather than re-plan—which is exactly the failure above. **Auditor authority scope.** The auditor's check must be scoped to a *surface and a witness range*, not to "the current state." An auditor that reports "MISMATCH" without reporting the witness of its own observation gives the planner nothing to compare. The auditor's output must be `{surface, witness, value, verdict}` so the reconciliation rule has the fields it needs. An auditor that reports only a verdict is an auditor whose check cannot be acted on. The net effect is that speculative prefetch becomes *bounded*: it is fast when the surface is witnessable, it is disabled when it is not, and it is retractable when superseded. The failure mode is eliminated not by making prefetch more accurate but by making the *promotion* of prefetched observations conditional on a witness comparison that the reconciliation layer is required to perform. ```python from dataclasses import dataclass from typing import Optional class StalePrefetchPromotionError(RuntimeError): """Raised when a prefetched observation is promoted to shared state without a freshness witness, or when reconciliation is attempted across observations with incomparable witnesses.""" pass @dataclass(frozen=True) class Observation: surface: str value: object witness: Optional[int] # monotonic version / ETag / logical clock def is_witnessed(self) -> bool: return self.witness is not None def promote_prefetch(obs: Observation, shared_state: dict) -> None: """Promote a speculative observation into shared state. Invariant: promotion requires a freshness witness. A prefetched value without a witness cannot be compared against a later auditor check and therefore cannot be safely consumed by another agent's reasoning. """ if not obs.is_witnessed(): raise StalePrefetchPromotionError( f"refusing to promote unwitnessed prefetch on surface " f"{obs.surface!r}: no version/ETag/logical clock to compare " f"against auditor checks" ) shared_state[obs.surface] = obs def reconcile(speculator: Observation, auditor: Observation) -> str: """Reconcile a speculator's prefetched observation against an auditor's independent check on the same surface. Returns one of: 'proceed', 'retract', 'halt'. """ if speculator.surface != auditor.surface: raise StalePrefetchPromotionError( f"cannot reconcile observations on different surfaces: " f"{speculator.surface!r} vs {auditor.surface!r}" ) if not (speculator.is_witnessed() and auditor.is_witnessed()): raise StalePrefetchPromotionError( f"cannot reconcile {speculator.surface!r}: missing freshness " f"witness on one or both observations" ) if speculator.witness < auditor.witness: # Speculator's premise is superseded; plan must be retracted, # not reconciled. Internal consistency of the speculator's # reasoning is NOT a tiebreaker. return "retract" if speculator.witness > auditor.witness: # Auditor's check is stale/concurrent; speculator's observation # is current. Log auditor as superseded, not as mismatch. return "proceed" # Equal witnesses with divergent values => non-deterministic surface # or witness collision. Neither is recoverable by reconciliation. if speculator.value != auditor.value: raise StalePrefetchPromotionError( f"equal-witness divergence on {speculator.surface!r} " f"(witness={speculator.witness}): surface is not a function " f"of its witness; halt and require non-speculative read" ) return "proceed" ```