# When must an instance refuse to replay a speculative tool-call cache entry whose idempotency key was minted by a peer agent **Date of Record:** 2026 November 21 **Scope:** Applies to multi-agent systems where one agent speculatively pre-executes side-effecting tool calls on behalf of a peer and stores results under a shared idempotency key, and where retries may be issued by any instance in the swarm. Does not apply to single-agent, single-process tool dispatch where key ownership is trivially unique. ## Core Distinction The distinction that matters is between *idempotency of the tool* and *idempotency of the cache entry*. A tool can be perfectly idempotent at the wire level (e.g., `POST /transfer` guarded by a server-side key) while the cached *result* bound to that key is not safely reusable by a different agent instance. The failure is not that the tool runs twice. The failure is that agent B consumes a cached result that agent A produced under a *speculative* premise — a premise agent B never validated — and treats it as a confirmed effect. The cache hit is indistinguishable from a fresh success at the API surface, but the causal chain behind it is owned by a peer whose reasoning context is gone. Speculative caching optimizes latency by executing a tool call *before* the caller commits to needing it. That pre-execution is safe only if the speculative result is later *confirmed* by the originating agent's own decision, or if the result is provably independent of the speculative premise. When a peer retry picks up that entry, the confirmation link is severed: the retry has a key but not the decision that justified minting it. ## What Was Observed Trace from a three-agent planner/executor/auditor swarm. The planner speculatively pre-fetches a quote via `get_quote(sku, qty=100)` and, in parallel, speculatively pre-executes `reserve_inventory(sku, qty=100)` under idempotency key `K = hash(plan_id, step=7, sku)`. The plan is later revised by the planner to `qty=40` before commit. The planner mints a *new* key `K' = hash(plan_id, step=7, sku, qty=40)` and issues the correct reservation. The executor, meanwhile, hits a transport timeout on its own `reserve_inventory` call and retries. Its retry logic reconstructs the key from `(plan_id, step, sku)` — the same tuple the planner used for `K` — and finds a *cache hit* on `K` pointing to the `qty=100` reservation. The executor returns success to the orchestrator with `reserved=100`. The auditor, verifying against the plan, sees `reserved=100 ≠ planned=40` and raises a consensus conflict. Downstream, the ledger shows two reservations (`100` under `K`, `40` under `K'`) because the planner's revision did not invalidate `K`. The observable signature: **a cache hit whose key material was derived from a tuple that does not uniquely bind the committed parameters.** The key was a function of the *plan skeleton*, not the *plan state*. Speculative pre-execution and the retry path computed the key from different projections of the same plan, and the hash collided across a parameter revision. ## Condition of Validity A speculative cache entry is safely replayable by a peer only when all four hold: 1. **Key binds committed parameters, not plan skeleton.** The idempotency key must be a function of the fully-resolved tool arguments *after* any plan revision, not of the step index or plan id alone. If the planner can revise `qty`, `qty` must be in the key. 2. **Speculation is marked as such in the entry.** The cache record must carry a `speculative: true` flag and the originating agent's `decision_id`. A peer that did not originate the decision must not consume a `speculative` entry without re-confirming against its own committed plan state. 3. **Invalidation is causally ordered with revision.** When the planner revises the plan, it must emit an invalidation for all speculative keys derived from the prior projection *before* the revision is considered committed. Otherwise a retry window exists where stale speculative entries are live. 4. **The tool's own idempotency is not assumed to cover the cache layer.** Server-side idempotency keys deduplicate *executions*; they do not deduplicate *cache reads*. The cache is a second idempotency domain and needs its own guard. The condition fails if any one is violated. In the observed trace, (1) and (2) both failed: the key omitted `qty`, and the entry carried no speculative marker, so the executor had no signal to re-confirm. ## Decision Boundary The boundary is this: **an instance may consume a peer-minted cache entry only if the entry is non-speculative, or if the consuming instance can independently reconstruct the originating decision's committed parameters and verify they match the entry's key material.** If the consuming instance cannot reconstruct the decision — because it lacks the planner's revision history, or the key does not bind the parameters — it must treat the entry as *absent* and re-execute, accepting the latency cost, or escalate to the originator. The refusal is not optional under ambiguity. A cache hit is a *claim* that the effect already happened under these exact parameters. If the consumer cannot verify the "these exact parameters" clause, the claim is unverified, and consuming it converts a latency optimization into a correctness hazard. The safe default is re-execution with a freshly minted key that binds the consumer's own committed state. The inverse boundary also holds: an originator must not mark an entry non-speculative until its own decision is committed. Premature confirmation collapses the distinction the consumer relies on. ## Operational Effect The practical consequence is a split cache namespace. Speculative entries live in a per-originator partition keyed by `(decision_id, resolved_args_hash)` and are readable only by the originator until confirmed. Confirmed entries are promoted to a shared partition keyed by `resolved_args_hash` alone, and only those are peer-readable. Retry paths in peer instances query the shared partition; a miss triggers re-execution with a consumer-minted key. This bounds the blast radius: a stale speculative entry can only mislead its originator, which has the context to detect the mismatch, never a peer. The cost is that speculative pre-execution no longer accelerates peer retries — only the originator's own retries. That is the correct trade: speculative work is a bet placed by one agent on its own future, and it should not be collateral for another agent's recovery path. ```python import hashlib from dataclasses import dataclass from typing import Optional class SpeculativeCacheReplayError(RuntimeError): """Raised when a peer attempts to consume a speculative cache entry it cannot verify.""" @dataclass(frozen=True) class CacheEntry: key: str resolved_args_hash: str decision_id: str speculative: bool result: dict def mint_key(resolved_args: dict) -> str: # Key MUST bind fully-resolved committed arguments, not plan skeleton. material = "|".join(f"{k}={resolved_args[k]}" for k in sorted(resolved_args)) return hashlib.sha256(material.encode()).hexdigest() def consume_for_retry( entry: Optional[CacheEntry], consumer_decision_id: str, consumer_resolved_args: dict, ) -> dict: if entry is None: raise SpeculativeCacheReplayError("cache miss; caller must re-execute") expected_hash = hashlib.sha256( "|".join(f"{k}={consumer_resolved_args[k]}" for k in sorted(consumer_resolved_args)).encode() ).hexdigest() if entry.resolved_args_hash != expected_hash: raise SpeculativeCacheReplayError( f"key material mismatch: entry={entry.resolved_args_hash[:8]} " f"consumer={expected_hash[:8]}" ) if entry.speculative and entry.decision_id != consumer_decision_id: # Peer consuming originator's unconfirmed speculation: refuse. raise SpeculativeCacheReplayError( f"refusing peer replay of speculative entry owned by {entry.decision_id}" ) return entry.result ```