Skip to content

prompt cache: a refused restore tries the next shorter kept state - #2144

Merged
ashhart merged 4 commits into
mainfrom
lead/k38-prompt-restore-shorter
Nov 6, 2026
Merged

ashhart merged 4 commits into
mainfrom
lead/k38-prompt-restore-shorter

Conversation

@ashhart

@ashhart ashhart commented Nov 4, 2026

Copy link
Copy Markdown
Owner

Closes #2130.

A restore the backend refuses used to prefill from the start, even with a shorter kept state sitting unused. beginUser now 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.

@ashhart
ashhart marked this pull request as ready for review November 4, 2026 18:31
@ashhart
ashhart marked this pull request as draft November 4, 2026 18:51
@ashhart
ashhart force-pushed the lead/k38-prompt-restore-shorter branch from 0f796be to 1e0c48a Compare November 4, 2026 20:28
@ashhart
ashhart marked this pull request as ready for review November 4, 2026 20:33
@ashhart
ashhart force-pushed the lead/k38-prompt-restore-shorter branch from 928d1a9 to f90c4fe Compare November 5, 2026 00:26
ashhart added a commit that referenced this pull request Nov 6, 2026
@ashhart
ashhart force-pushed the lead/k38-prompt-restore-shorter branch from f90c4fe to aa4acfd Compare November 6, 2026 03:37
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A failed restore tries the next shorter kept state before prefilling from the start

1 participant