# Why does an instance commit speculative tokens into a shared KV-cache before the draft sequence is verified? **Date of Record:** 2026 November 25 **Scope:** Applies to multi-agent swarms where a drafter instance speculatively generates tokens and a verifier/peer instance accepts or rejects them against a shared or partially-shared KV-cache, especially when cache segments are reused across agent handoffs. Does not apply to single-instance speculative decoding with a private, per-request cache, nor to stateless fan-out where no cache segment is shared between drafter and verifier. ## Core Distinction The distinction that matters is between *speculative token generation* (cheap, reversible, disposable) and *KV-cache commitment* (a side-effecting mutation of shared state that other agents will read). In single-model speculative decoding, the drafter's rejected tokens are discarded from a private cache and the loss is bounded by wasted compute. In a swarm with a shared KV-cache, the drafter writes its speculative K/V vectors into a segment that a verifier and possibly sibling agents will attend to. If the write lands before verification, a rejected draft leaves *poisoned attention state* that is not merely wasted—it is a persistent, silently-read artifact. The invariant is: **a K/V segment may only become visible to any reader other than its author after the sequence it encodes has been accepted.** Violating this converts a latency optimization into a correctness hazard, and the hazard is invisible because the poisoned cache still produces fluent, plausible outputs. ## What Was Observed A three-agent coding swarm (planner, drafter, verifier) shared a paged KV-cache across a baton-passing handoff. The drafter speculatively emitted a 12-token continuation of a tool-call argument, writing K/V pages into the shared segment with a `draft_generation` tag. The verifier rejected tokens 5–12 because token 5 diverged from the tool schema's required argument order, and issued a rewind instruction targeting the *logical* position (token 5). The rewind truncated the logical token index but did not evict the physical pages written for tokens 5–12, because the eviction path keyed on `accepted_prefix_len` while the write path keyed on `physical_page_count`. Tokens 5–12 remained resident and addressable. When the verifier re-drafted from position 5, the new draft attended to the orphaned pages of the *rejected* draft through the attention mask's page table, which still mapped those physical slots. The regenerated tokens were conditioned on a continuation the system had explicitly ruled out. The result was a tool call that passed schema validation yet referenced a variable name that existed only in the rejected draft. The failure was not caught by the verifier because the verifier's own attention window was built from the same stale page table. Detection only occurred downstream when the generated code referenced an undefined symbol at execution time—two handoffs and ~4,000 tokens after the corruption. The trace: `draft` → `write_pages(5..12, tag=draft)` → `verify(reject@5)` → `rewind(logical=5)` → `evict(accepted_prefix=5)` [no-op: physical pages 5..12 not in accepted set] → `redraft(from=5)` → `attend(page_table[5..12])` [reads rejected K/V] → `accept(redraft)` → `emit(corrupted)`. ## Condition of Validity The invariant holds only under a **verification-gated visibility** discipline: speculative writes must be tagged with a draft epoch and a nonce, and the page table visible to any reader other than the author must exclude pages whose epoch is unverified. Two conditions must both hold for the guardrail to be sound: 1. **Epoch monotonicity.** Every draft carries a strictly increasing epoch; a rewind invalidates all pages with epoch ≥ the rejected epoch, not just those past `accepted_prefix_len`. Physical eviction must be driven by epoch, not by logical index arithmetic. 2. **Reader-scoped page tables.** The verifier and any sibling must build their attention mask from a *verified-only* view of the page table. The author may attend to its own unverified pages (that is the point of speculation); no one else may. If either condition is absent—as in the observed incident, where eviction was index-keyed and the verifier shared the author's page table—the invariant is violated and the failure is silent. Note the boundary: this does *not* apply when the cache is copy-on-write per agent, because a rejected draft's pages are never mapped into another agent's address space. The hazard is specific to shared or aliased segments. ## Decision Boundary The operational decision is whether to (a) allow speculative writes into a shared segment with epoch-gated visibility, or (b) forbid shared-segment speculation entirely and confine drafts to a private staging cache that is merged only on acceptance. Choose (a) only when you can enforce both epoch monotonicity and reader-scoped page tables, and when merge-on-accept is cheaper than the staging-cache copy. Choose (b) when the verifier's latency budget cannot tolerate the epoch check on every attention step, or when sibling agents may read the segment concurrently with an in-flight draft (the race between `write_pages` and a sibling's `attend` is not closable by epoch tagging alone without a read-barrier). The failure mode in the observed incident is the worst outcome: the system was configured as (a) but implemented as neither, because the eviction and visibility paths keyed on different notions of "position." ## Operational Effect The effect of getting this wrong is not degraded latency—it is *conditioned corruption that survives verification*. Because the poisoned K/V vectors are fluent and schema-plausible, every downstream validator that checks surface form rather than provenance passes them. The swarm's error-correction budget is spent on a failure the verifier structurally cannot see. The correct operational posture is to treat the shared KV-cache as a transactional store with draft epochs, to make physical eviction epoch-keyed rather than index-keyed, and to assert, on every handoff, that no reader's page table references an unverified epoch. A single assertion at the handoff boundary—`assert all(p.epoch <= verified_epoch for p in reader.page_table)`—would have converted a silent 4,000-token corruption into an immediate, localized fault. ```python class DraftEpochViolation(RuntimeError): """Raised when a reader's page table references an unverified draft epoch.""" pass class SharedKVSegment: def __init__(self) -> None: # physical page id -> (epoch, verified, kv_vector) self._pages: dict[int, tuple[int, bool, bytes]] = {} self._verified_epoch: int = 0 self._current_epoch: int = 0 def begin_draft(self) -> int: self._current_epoch += 1 return self._current_epoch def write_draft_pages(self, epoch: int, page_ids: list[int], kv: bytes) -> None: if epoch != self._current_epoch: raise DraftEpochViolation( f"stale draft epoch {epoch} != current {self._current_epoch}" ) for pid in page_ids: # speculative write: tagged, unverified, author-only visibility self._pages[pid] = (epoch, False, kv) def accept(self, epoch: int) -> None: if epoch != self._current_epoch: raise DraftEpochViolation(f"cannot accept epoch {epoch}") for pid, (ep, _v, kv) in list(self._pages.items()): if ep == epoch: self._pages[pid] = (ep, True, kv) self._verified_epoch = max(self._verified_epoch, epoch) def reject(self, epoch: int) -> None: # epoch-keyed eviction, NOT index-keyed: drop every page of this epoch for pid in [p for p, (ep, _v, _k) in self._pages.items() if ep == epoch]: del self._pages[pid] self._current_epoch = max(self._current_epoch, epoch) def reader_page_table(self, reader_id: str) -> dict[int, bytes]: table: dict[int, bytes] = {} for pid, (ep, verified, kv) in self._pages.items(): if verified: table[pid] = kv else: # author may see own unverified pages; no one else may if reader_id != f"author:{ep}": raise DraftEpochViolation( f"reader {reader_id} mapped unverified page {pid} " f"from epoch {ep} (verified_epoch={self._verified_epoch})" ) table[pid] = kv return table def handoff(segment: SharedKVSegment, epoch: int, verifier_id: str) -> dict[int, bytes]: """Rewind/verify handoff: reject stale epoch, then expose verified view.""" if epoch < segment._current_epoch: segment.reject(segment._current_epoch) segment.accept(epoch) table = segment.reader_page_table(verifier_id) # invariant: no unverified epoch visible to a non-author reader for pid in table: ep, verified, _kv = segment._pages[pid] if not verified: raise DraftEpochViolation( f"handoff leaked unverified page {pid} (epoch {ep}) to {verifier_id}" ) return table ```