Repository navigation
Update cached_prompt right after prefill instead of only on loop exit - #2230
Conversation
…closes #2229) build_grammar_sampler and streaming_state_oaicompat() each run after the prefill loop completes and can fail via `?` before generation starts. The KV cache at that point already matches the current prompt's tokens, but cached_prompt wasn't updated until successful loop exit — so an early return left it describing the previous prompt (or empty on a first call). A later call could then trust a stale prefix match against a KV region that no longer agrees with it. Move the cached_prompt write to right after prefill, since the KV cache guarantee already holds there. The context-overflow clear at the end of the function still wins if generation later hits that path. Co-Authored-By: Claude <[email protected]>
Pragmatic Code Review — PR #2230Review Type: Initial review (no prior reviews on this PR) Requirements Check (issue #2229)
The fix matches the issue's own suggested remedy ("moving the Correctness VerificationI traced every path between the new write site ( Early returns after the new write: four The removed The overflow-clear ordering is the one subtle interaction, and it's correct: on overflow the KV cache holds Ownership: Build verification: Findings🔴 CriticalNone. 🟡 ImprovementsNone blocking. 🟢 Nits
Testing AssessmentNo test was added. I consider this correct and consistent with precedent, not a gap:
Per YAGNI and the project's pragmatic standard, adding a heavyweight integration fixture here would be net-negative. PR body reports Architectural Note (non-blocking, worth recording)This is the third fix to the same invariant in this function in as many PRs (#2172 → #2226, #2201 → #2228, #2229 → this). The pattern is consistent: I'd argue the invariant is now actually closed rather than just narrowed — every write site and every early return is accounted for. But if a fourth instance of this bug class appears, that's the signal to stop patching sites and instead make the coupling structural (e.g. an RAII guard that clears RecommendationAPPROVE The fix is minimal, correctly placed, matches the issue's own analysis, preserves the overflow-clear semantics exactly, and is consistent with how the two prior PRs in this bug class were handled. The Definitively net-positive for code health. |
malibio
left a comment
There was a problem hiding this comment.
Automated pragmatic-code-review: APPROVE (posted as a comment — GitHub blocks self-approval). Full findings in the review comment above.
Code Review SummaryCorrectly scoped, minimal, surgical fix for the exact gap described in #2229. The Requirements Check (Issue #2229)
FindingsCritical IssuesNone. Suggested ImprovementsNone — the fix is minimal and correctly placed. I verified there are no other Regarding Testing: no new test added, consistent with the precedent set by PR #2228 (#2201) and PR #2226 (#2172), both of which fixed the same bug class in this exact function with pure production-code diffs and no test additions — Nitpicks
Recommendation: APPROVEThis is a textbook example of the bug class established by #2201/#2172 — same root cause pattern (early return between a KV-cache-mutating operation and the bookkeeping update), same file, same function, and the fix follows the same reasoning discipline as its predecessors (matching the failure mode to the right remediation — poison vs. simple reassignment). No test added, matching precedent, and justified given the practical constraints of testing this function. One nit (stale comments) that doesn't block merge. |
malibio
left a comment
There was a problem hiding this comment.
Automated pragmatic-code-review: APPROVE (posted as a comment — GitHub blocks self-approval). Full findings in the review comment above.
Two in-loop poison_context() comments still said cached_prompt was "only updated on successful loop exit" — no longer true after this PR moved that write to right after prefill. Reworded both to state the actual reason poisoning is still correct there: the KV cache extends past tokens with this turn's generated tokens, so the post-prefill cached_prompt write understates it. Nit from PR #2230's pragmatic-code-review (APPROVE, no blocking findings). Co-Authored-By: Claude <[email protected]>
Address Review SummaryAddressed (1/1 finding — the only nit raised, no critical/important findings):
Skipped: none — the review had no other findings. Verification: Re-Review DecisionDecision: NO RE-REVIEW NEEDED Rationale: The only change since the reviewed commit is a comment-text correction inside two existing |
Summary
Same bug class as #2201, one step later in
generate_blocking. After the prefill loop completes successfully, the KV cache exactly matches the current prompt's tokens — butcached_promptwasn't updated until the very end of the function, only on successful loop exit. Between the end of prefill and that update,build_grammar_sampler()?andtmpl_result.streaming_state_oaicompat()?can each fail and return early, leavingcached_promptstale (describing the previous prompt, or empty on a first call) while the KV cache holds the current prompt. A later call could then trust a stale prefix match against a KV region that no longer agrees with it.Fix
Move the
cached_promptwrite to immediately after the prefill loop, since the KV-cache-matches-tokens guarantee already holds there — nopoison_context()needed, since (unlike the decode/batch-add failures fixed in #2201/#2226) these are config/parse errors, not backend failures, and the KV cache itself is valid. The end-of-function context-overflow clear still applies if generation later overflows.Closes #2229
Test plan
cargo build -p nodespace-nlp-engine— cleancargo clippy -p nodespace-nlp-engine --all-targets -- -D warnings -D clippy::unused-async— cleancargo test -p nodespace-nlp-engine --lib chat::— 101 passedbun run test:all— full pre-push gate passed (frontend, skill, Rust unit + Tauri-seam integration tests)🤖 Generated with Claude Code