Repository navigation
prompt cache: a refused restore tries the next shorter kept state - #2144
Merged
Merged
Conversation
ashhart
marked this pull request as ready for review
November 4, 2026 18:31
ashhart
marked this pull request as draft
November 4, 2026 18:51
ashhart
force-pushed
the
lead/k38-prompt-restore-shorter
branch
from
November 4, 2026 20:28
0f796be to
1e0c48a
Compare
ashhart
marked this pull request as ready for review
November 4, 2026 20:33
ashhart
force-pushed
the
lead/k38-prompt-restore-shorter
branch
from
November 5, 2026 00:26
928d1a9 to
f90c4fe
Compare
ashhart
added a commit
that referenced
this pull request
Nov 6, 2026
ashhart
force-pushed
the
lead/k38-prompt-restore-shorter
branch
from
November 6, 2026 03:37
f90c4fe to
aa4acfd
Compare
Measured first on main-now with the store's fake family: with a kept state at 7 and another at 4, a 9-token prompt whose longest state the backend refuses prefilled from 0 (8 tokens), while the 4-token state sat unused. The same holds in prompt_cache_index_test: a 12-token prompt prefilled 12 instead of 8. beginUser now drops the refused state (as before) and looks up the prompt again, so the next shorter kept state is restored in the same request; the pass starts from its position, and only when no kept state resumes does it prefill from the start with the first lookup's marks, exactly as main did. Refused states stay expired, so a later request cannot retry them. No replies change: the restored prefix is the prompt's own (tokens and canonical spans), and the fake family's pass sum equals a fresh pass's. Tests: a fake backend that refuses only the longest state resumes the shorter one, with equal tokens, one counted failure, no outstanding borrows and the live state count matching the store's; the index test now refuses the longest state and checks the shorter one resumes from the same begin, then that refusing every state drops both and prefills from 0. test-learn-recovery passes 504.
A cold review found the K38 loop unbounded: each refusal called lookupUser, which recalls the imprint and the spill tier, so a disk state this request just refused came back every time and the engine thread never left beginUser. beginUser's retries now call lookupUserAt with disk=false: only states already in RAM are tried after the first refusal. The first lookup still recalls every tier, so a learned or spilled state is tried once; a refused one is expired and removed, and the retry cannot bring it back. Marks on the fallback pass stay the first lookup's, as main had them. Test: a fake family with a learned state at 5 and an imprint whose restore refuses that state every time ends with from = 0, one counted failure, no entry left and a pass equal to a fresh one (prompt_cache_learn_test.zig). Unfixed, the loop recalls the same state forever. test-learn-recovery passes 504.
The bound from this PR covered the imprint route. The rows tier behind --learn-conversations is the other recall path: a disk state whose restore the backend refuses must not be re-read. This adds the same case with a real rows store: one state, every restore refused, the pass from the start, one counted failure and no outstanding borrow, and the retry reads no file.
ashhart
force-pushed
the
lead/k38-prompt-restore-shorter
branch
from
November 6, 2026 04:07
aa4acfd to
70643ba
Compare
8 tasks done
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 #2130.
A restore the backend refuses used to prefill from the start, even with a shorter kept state sitting unused.
beginUsernow drops the refused state, as before, and looks the prompt up again, so the next shorter kept state resumes in the same request. Only when no state resumes does it prefill from the start, as main does. Refused states stay expired, so a later request doesn't retry them.Measured on main's fake family: with kept states at 7 and 4 tokens, a 9-token prompt whose longest state is refused prefilled 8 tokens with the 4-token state unused. The index's 12-token case prefilled 12 instead of 8.
Replies don't change: the restored prefix is the prompt's own tokens and canonical spans. The fake backend's test refuses only the longest state; the shorter one resumes with equal tokens, one counted failure, no outstanding borrows, and the live count matching the store's.
Host tests only so far. No device run yet, and a backend that really refuses a restore hasn't been exercised.