You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Preserve native prompt cache across last-turn rewinds - #571
Editing the latest user message previously lost the native prompt cache's later history checkpoint and forced a prefill from the start. This change derives a safe token boundary from two renderings of the latest real user message and keeps a private rewind checkpoint there. The normal history endpoint remains first in the byte budget; Qwen's background warm pass carries the same boundary. Planned families align both checkpoints to the request's chunk starts or their default grid. The Python engine is unchanged. This replaces the approach in #569.
Tests cover an edited prompt resuming at the rewind point with the same complete fake host state and output as a fresh prompt, both native lookahead modes, one-state and two-state budgets, warm-turn retention, and grid-aligned rewinds. The focused cache suite passes 27/27.
Validation with pinned Zig 0.17.0: zig build native -Dcpu=apple_m1 -Dversion=1.0.0 -j2 passed. zig build test -Dcpu=apple_m1 -j2 reported 939 passed, 14 skipped, and one ANE hardware test failure (NoDevice); the same specific test fails with NoDevice on clean upstream main. zig build test-golden -Dcpu=apple_m1 -j2 reported 309/319 equal, 10 known differences, and zero unexpected differences. lean_check.py reports 509 findings on both current upstream main and this branch. Actual GLM, FlashNext, and Qwen GPU inference was not run on model hardware.
Thank you, @otarkhan, and thank you for bringing #569's idea over to the native engine so quickly. A prompt state
kept just before the latest user message is now in main, so editing, deleting or regenerating the last turn
resumes there instead of reading the conversation again. Your commit, rebased onto today's main, is 71dff68
under your name. It came in with a personal email address, so we re-authored it to your GitHub noreply address,
which is why GitHub shows this pull request as closed rather than merged.
The design is yours: the boundary found by rendering the latest user message differently, the history's state
first in the byte budget, the rewind state evicted last while its turn lasts, and the warm pass carrying the same
boundary. Our follow-ups are cca0fb7, f655372, dfe32b7 and e936c98:
Nemotron on CUDA (Nemotron CUDA prompt reuse #467, landed earlier tonight) keeps and resumes states only on its own 2,048-row grid. The
rewind and history positions now floor to that grid too; before, a grid family never kept a rewind state.
With the rewind state protected, victimBut could return null when it was the only state left, and keep()
unwraps that. It can happen when the 27B keeps a reply's state after decoding. The rewind state now goes last
instead of never.
On a first turn the rewind position is the system prompt's cut. It is noted as a shared cut again, so --learn
still writes it to disk.
Marks over the budget are counted as refused again, and a request whose rewind state doesn't fit plans exactly
as before.
The boundary takes one probe render instead of two, with the prompt itself as the second rendering, and only on
engines that keep prompt states. A render of a 9,000-token conversation takes about 5 ms on a GB10.
When a state the prompt can resume is already kept less than min_gap before the boundary, that state is the
rewind state, and the turn, its reply's keep and its warm pass all protect it. The 27B resumes most turns four
tokens before the boundary, and copying a second state there added about 60 ms to every turn.
The rewind tests moved to their own file, because the prompt-cache tests had passed our 600-line limit.
We checked it on a GB10 and an M3 Ultra against the same requests with the cache off. Every reply of a three-turn
conversation, and of its last turn deleted, edited, regenerated and edited again, equalled the cache-off reply, also
with two conversations at once and with drafts off. Resumes stayed on the CUDA grid, fresh prompts cost the same
within 1.5%, the cache never held more than its budget, and ordinary turns cost what they did before. On the M3
Ultra the Qwen3.8-27B's deleted, edited, regenerated and re-edited last turns of a 9,000-token chat took 1.7 seconds
of prompt processing in all, against 10.9 on main; with room for only two states, Nemotron's second edit of a long
last message resumed at the previous reply in 1.15 seconds where main read all 10,809 tokens again in 4.2. On the
GB10, replacing a pasted last message resumed at 8,192 tokens where main resumes at 6,144, so its prompt took 0.49
seconds against 0.79, and 1.65 with the cache off.
Flash Next and GLM had no test machine tonight; their host tests pass. It ships in the next release. Thanks again!
ashhart
added
the
landed
Landed on main by the maintainers; the commits keep the contributor's authorship
label
Oct 28, 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
landedLanded on main by the maintainers; the commits keep the contributor's authorship
2 participants
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.
Editing the latest user message previously lost the native prompt cache's later history checkpoint and forced a prefill from the start. This change derives a safe token boundary from two renderings of the latest real user message and keeps a private rewind checkpoint there. The normal history endpoint remains first in the byte budget; Qwen's background warm pass carries the same boundary. Planned families align both checkpoints to the request's chunk starts or their default grid. The Python engine is unchanged. This replaces the approach in #569.
Tests cover an edited prompt resuming at the rewind point with the same complete fake host state and output as a fresh prompt, both native lookahead modes, one-state and two-state budgets, warm-turn retention, and grid-aligned rewinds. The focused cache suite passes 27/27.
Validation with pinned Zig 0.17.0:
zig build native -Dcpu=apple_m1 -Dversion=1.0.0 -j2passed.zig build test -Dcpu=apple_m1 -j2reported 939 passed, 14 skipped, and one ANE hardware test failure (NoDevice); the same specific test fails withNoDeviceon clean upstream main.zig build test-golden -Dcpu=apple_m1 -j2reported 309/319 equal, 10 known differences, and zero unexpected differences.lean_check.pyreports 509 findings on both current upstream main and this branch. Actual GLM, FlashNext, and Qwen GPU inference was not run on model hardware.