Repository navigation
Poison chat context on batch-add failure to prevent stale cached_prompt - #2228
Conversation
…pt (closes #2201) Every batch.add() failure in generate_blocking previously propagated via `?` without touching cached_prompt, even though the KV cache had already been trimmed or extended for the in-flight prompt. A later call could then compute a "reusable prefix" against cached_prompt while the KV cache actually held a different prompt's tokens, corrupting generation silently. The ctx.decode() failure paths a few lines below already call poison_context() for this exact reason — the batch-add paths were missed. Co-Authored-By: Claude <[email protected]>
Code Review SummaryThis is a small, well-targeted fix: three One real gap of the same bug class exists just outside the diff's boundary and is worth a follow-up (not a blocker, see below). FindingsCritical IssuesNone. Suggested Improvements
Nitpicks
Test CoverageNo new tests were added. Given the difficulty of deterministically triggering a real RecommendationAPPROVE — the fix is correct, minimal, atomic, and consistent with the existing error-handling pattern in the same function. The one adjacent gap found (grammar sampler / streaming-parser init failures after prefill, before |
malibio
left a comment
There was a problem hiding this comment.
Reviewed — see full findings in the comment above. Recommendation: APPROVE. Fix is correct, minimal, and consistent with the existing ctx.decode() poisoning pattern in the same function. One same-class gap found outside this diff's scope (build_grammar_sampler / streaming_state_oaicompat failures between prefill completion and the cached_prompt update at line 738) — recommend tracking as a fast follow-up issue rather than blocking here. Self-review posted as COMMENT since self-approval is rejected by GitHub for PR authors.
Address-Review SummaryReview verdict was APPROVE with no critical findings. Evaluated the three non-blocking notes:
No code changes made — nothing addressed required a diff. Re-Review DecisionDecision: NO RE-REVIEW NEEDED Rationale: No code changes were made to the PR; the review's only actionable item (the follow-up bug) was filed as a separate issue rather than folded into this diff. The approved diff is unchanged. |
Summary
generate_blockingmaintains thecached_prompt/KV-cache pairing only on loop completion, explicitly clearingcached_prompton context overflow. But everybatch.add()failure — during prefill, in the token-decode-error recovery branch, and in the main generation loop — propagated its error via?without touchingcached_prompt, even though the KV cache had already been trimmed and/or extended for the in-flight prompt at that point.The three
ctx.decode()failure sites a few lines below each of these already callllama.poison_context()for exactly this reason (it clears both the context andcached_prompt) — the batch-add sites were missed.Fix
Each of the three
batch.add(...).map_err(...)?call sites inpackages/nlp-engine/src/chat/mod.rsnow matches the pattern used by the adjacentctx.decode()failure handling: on error, callllama.poison_context()before returning, so a later call can't compute a "reusable prefix" against acached_promptthat no longer matches what the KV cache actually holds.Closes #2201.
Test plan
cargo test -p nodespace-nlp-engine --features chat-service --lib chat— 101 passedbun run test:all— 227 frontend test files (4794 tests) + 2 more (24 tests) + full Rust workspace, all passing, no new failures vs baselinebun run quality:fix— clean, no warnings🤖 Generated with Claude Code