Repository navigation
Description
Part of epic #1148.
Problem / Background
For snapshot-only families (every model reporting supports_snapshot_reuse(): bailing_moe_linear, falcon_h1, gemma4, granitemoehybrid, jamba, lfm2, mamba, mamba2, muse_glimmer, nemotron_h, plamo2, qwen3_5, qwen3_next), the only prompt-cache reuse path is the exact-prefix snapshot lookup in PromptCacheStore::lookup_snapshot_prefix (src/server/prompt_cache/store.rs ~line 901). The entry donated at end of generation (donate_finished_sequence_cache, src/server/batch/scheduler.rs ~line 2195) stores the whole token vector: previous prompt plus generated tokens. That vector must be an exact prefix of the next request's tokens for a hit.
In practice this never happens, for three independent reasons verified live on 2026-08-14 (main at 9c154ff):
- (a) Generation-prompt-only tokens.
gemma-4-31b-it-4bit: the template appends an empty thought scaffold<|channel>thought\n<channel|>only to the generation prompt, never in history re-render. Token-level proof: stored entry 139 tokens (94 prompt + 45 generated), turn-2 prompt shares only 90, divergence tokens[100, 45518, 107, 101]detokenize to exactly the scaffold.qwen3.5-4b-4bitwithenable_thinking=falsehas the same shape via its empty<think>\n\n</think>\n\ninjection (turn 2cached_tokens = 0of 177). - (b) Thinking stripped from history.
qwen3.5-4b-4bitdefault mode primes<think>\nin the generation prompt, but the template renders assistant history before the last user query without the<think>block, so the vectors diverge right after<|im_start|>assistant\n(turn 2cached_tokens = 0of 123). Gemma 4 withenable_thinking=truehits the same class viastrip_thinking. - (c) Retokenization drift.
falcon-h1-tiny-90m-instruct-4bithas a plain ChatML template with no thinking scaffold and still misses (turn 2cached_tokens = 0of 250): the donated vector stores 120 sampled completion tokens while the same reply text re-tokenizes to 118 tokens. Sampled sequences are not canonical tokenizations.
The insight: all three divergence classes happen after the history boundary. The history portion of the prompt (everything rendered with add_generation_prompt=false) is prefix-stable across turns regardless of template quirks.
Proposed Solution
Capture a second snapshot at the history boundary during prefill, in addition to the existing end-of-generation donate:
- The route layer already renders the chat template. Render it a second time with
add_generation_prompt=false, tokenize, and carry that boundary token count inPromptCacheRequestContext(src/server/config.rs). - The scheduler aligns a prefill chunk boundary at that index (chunked prefill already exists via
--prefill-chunk-size) and captures an additional model-state snapshot there, keyed by the boundary token prefix. - Insert the boundary snapshot into the store alongside the existing end-of-generation donate.
Because history rendering is prefix-stable, the boundary snapshot is guaranteed to be a prefix of every follow-up turn's prompt. This neutralizes classes (a), (b), and (c) by construction: the donated vector is produced by tokenizing the re-rendered history form itself.
Acceptance Criteria
- Turn N+1 against
gemma-4-31b-it-4bitrestores the boundary snapshot and reportscached_tokensequal to the boundary length through the production/v1/chat/completionspath (deferred to the epic-level verification run: only small checkpoints were exercised on this branch) - Turn N+1 against
qwen3.5-4b-4bit(default thinking mode) does the same, covering the GatedDeltaNet side of the class where partial restore is impossible and this path is the only route to reuse - Verification is counter-based via
/v1/cache/stats(snapshot_hitsadvances), not wall-clock only - The existing end-of-generation snapshot donate stays intact
- Integrated end to end: route context field, scheduler capture, store insert, adopt path; not a standalone helper
- No regression for dense-KV families (llama control scenario stays at parity)
Technical Considerations
- Measured baseline (2026-08-14, Apple Silicon dev box, local main at 9c154ff,
cargo build --release --features metal,accelerate): gemma-4-31b two-turn chat, turn 2usage.prompt_tokens_details.cached_tokens = 0of 169; qwen3.5-4b, 0 of 123 (thinking on) and 0 of 177 (thinking off); falcon-h1-tiny, 0 of 250. Control on the same binary: llama-3.2-1b-instruct, identical scenario, turn 2cached_tokens = 256of 308,hits = 1. The store, key composition, and adopt paths work; the failure is specific to the snapshot-only path. - Gemma 4 uses sliding_window = 1024 with 60 layers in a 5:1 sliding:full pattern, which bounds where rotating-layer truncation is mechanically possible (relevant to the follow-up partial-restore work). Recurrent-state families (GatedDeltaNet, SSM) cannot truncate at all, so this boundary snapshot is their load-bearing fix.
Activity
fix(server): add a kill switch and record the boundary split's output…
fix(server): drop the preempted sequence's prompt-cache context
fix(server): add a kill switch and record the boundary split's output…
fix(server): drop the preempted sequence's prompt-cache context
fix(server): scope the snapshot supersede chain to one producer
3 remaining items
chore(release): v0.5.1
feat(server): donate a history-boundary snapshot during prefill (labl…
fix(kimi_k3): close the review findings on the XTML chat path
fix(kimi_k3): close the review findings on the XTML chat path
fix(kimi_k3): close the review findings on the XTML chat path
(#1743)
feat(core): share KV snapshot serialization across attention cache fa…
fix(gemma4): mirror the chat history-boundary prefill split in MTP
fix(gemma4): make MTP verify byte-identical to decode on CUDA (#2185)
feat(server): donate a history-boundary snapshot during prefill (labl…
feat(server): warm the next turn's history prefix in the background (l…
chore(release): v0.5.1
docs(batching): explain server vs generate divergence on snapshot fam…
feat(prompt-cache): add opt-in system-prompt root snapshot (#2950)