Repository navigation
feat(prompt-cache): keep the previous turn's restore point - #3037
Merged
Merged
Conversation
The most common agent history edit replaces or compacts the last user message. Serving it needs the snapshot the previous turn restored from, but the session-chain supersede rule removed it as soon as that turn stored its own snapshot, and the previous turn's history boundary ends after the edited message. On Qwen3.8-Flash-Next at the model-aware default budget such an edit fell back to the system-prompt root (4,744 of 28,553 tokens cached, 22.5 s TTFT). MLXCEL_PROMPT_CACHE_KEEP_PREVIOUS_TURN=1 makes the store remember, per session, the snapshot the latest turn restored from and exempt it from the supersede rule until a later turn restores from something else, so a conversation holds one extra snapshot. With the switch on, the model-aware default snapshot budget is sized for 9 representative entries instead of 6; operator caps are unchanged. scripts/bench_prompt_cache_replay.py gains --live-replies, which feeds the model's own replies back as a client does. With it and the switch on, the last-turn edit reuses 25,804 tokens and takes 2.9 s; an edit three turns back is unchanged (root, 22 s). Unedited outputs are byte-identical across arms. Closes #3035. Refs #2885.
This was referenced Nov 14, 2026
This was referenced Nov 22, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #3035. Refs #2885.
Replacing or compacting the last user message is the most common agent history edit, and the prompt cache did not serve it: the snapshot the previous turn restored from was superseded when that turn stored its own, and the previous turn's history boundary ends after the edited message. On Qwen3.8-Flash-Next at the model-aware default budget the edited turn fell back to the system-prompt root.
Change
MLXCEL_PROMPT_CACHE_KEEP_PREVIOUS_TURN=1(opt-in, read once): the store records, per session, the snapshot each lookup adopted and exempts it from the session-chain supersede rule until a later lookup adopts another. A conversation holds one extra snapshot (boundary, completion, previous restore source). The record is dropped when its entry is evicted or the store is cleared.--prompt-cache-snapshot-capacity-bytesis unchanged.scripts/bench_prompt_cache_replay.py --live-repliesfeeds the model's own replies back, as a client does. The scripted replies never matched the completion snapshots, which overstated snapshot pressure in earlier replays.Measurement (M3 Ultra, Qwen3.8-Flash-Next-OptiQ-2bit, default budget, live replies)
Unedited turns take 2.5 to 3.4 s in every arm and their outputs are byte-identical across arms. Details, memory, and output identity:
docs/benchmark_results/qwen4-exp-keep-previous-turn-replay-m3ultra-2026-11-12.md. The--verifyruns also show that any request adopting a completion snapshot differs from its cold run with or without the switch; that predates this PR and is noted in the doc.Validation
New store tests (restore source survives its successor, the exemption moves with the next restore, off keeps one entry per chain, clear forgets the record) and a sizing test pass; prompt-cache supersede tests, clippy (
--lib --tests -D warnings), the env-var docs check, anddead_doc_pointerspass locally. Not measured: small-Mac memory behavior (#2883).