Skip to content

feat(prompt-cache): keep the previous turn's restore point - #3037

Merged
inureyes merged 2 commits into
mainfrom
feat/prompt-cache-keep-previous-turn
Nov 14, 2026
Merged

inureyes merged 2 commits into
mainfrom
feat/prompt-cache-keep-previous-turn

Conversation

@inureyes

Copy link
Copy Markdown
Member

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.
  • With the switch on, the model-aware default snapshot budget targets 9 representative entries instead of 6, still capped at a quarter of free memory. An operator-set --prompt-cache-snapshot-capacity-bytes is unchanged.
  • scripts/bench_prompt_cache_replay.py --live-replies feeds 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)

Edit Switch off Switch on
Last user message (turn 8 of a 28.5K prompt) 4,744 cached, 22.47 s 25,804 cached, 2.90 s
Three turns back (turn 6) 4,744 cached, 22.18 s 4,744 cached, 22.06 s

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 --verify runs 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, and dead_doc_pointers pass locally. Not measured: small-Mac memory behavior (#2883).

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.
@inureyes inureyes added type:enhancement New features, capabilities, or significant additions priority:medium Medium priority status:review Under review labels Nov 12, 2026
@inureyes
inureyes merged commit e4ed09e into main Nov 14, 2026
31 checks passed
@inureyes
inureyes deleted the feat/prompt-cache-keep-previous-turn branch November 14, 2026 04:19
@inureyes inureyes added status:done Completed and removed status:review Under review labels Nov 14, 2026
@inureyes inureyes added this to the 0.9 milestone Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

priority:medium Medium priority status:done Completed type:enhancement New features, capabilities, or significant additions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(prompt-cache): keep the previous turn's restore point for last-turn edits

1 participant