Skip to content

cached_prompt left stale on decode-error paths, enabling silent KV-cache prefix corruption on later turns #2201

Closed

Description

@mstomar125

Summary

generate_blocking maintains the cached_prompt/KV-cache pairing only on loop completion (lines 681-688, clearing it explicitly on context overflow), but every Err return after the KV trim — a prefill ctx.decode failure (line 504-506), a batch-add failure, or a mid-generation decode failure (lines 672-676) — leaves the KV cache holding tokens of the NEW prompt (plus generated tokens) while cached_prompt still describes the OLD prompt, so a later call can compute a reusable prefix that keeps KV entries which do not match its own tokens.

Failure scenario

One engine serves interleaved divergent prompts by design (Stage-1 routing, Stage-2 ReAct, resolve_query, routing probe — chat/mod.rs:383-386). Prompt A (chat 1) completes; cached_prompt=A. Prompt B (chat 2) shares only the system-prompt prefix d with A; during B's generation a transient Metal decode error occurs (the decode-failure class core#2172 shows is real) → Err returned, KV now holds B's tokens beyond position d, cached_prompt still = A. The user returns to chat 1: prompt C extends A, common(C, A)=len(A) > d, so the trim keeps KV [0, len(A)) — but positions [d, len(A)) contain B's tokens, not C's. RoPE/attention run over a silently corrupted prefix: chat 1's answers degrade to subtly or grossly wrong output with no error, and the corruption persists across subsequent turns of that conversation because each successful turn re-blesses the poisoned prefix by setting cached_prompt to its own tokens. The overflow path already clears cached_prompt for exactly this invariant; the Err paths were missed.

Location

packages/nlp-engine/src/chat/mod.rs:687

Severity

minor


Found via a full-repo adversarial bug sweep, independently verified by two separate skeptical reviewers reading the actual current code before filing.

Activity

  1. self-assigned this
    on Aug 21, 2026
  2. malibio commented on Aug 21, 2026

    @malibio
    Collaborator

    Frontend baseline: 227 test files passed, 4794 tests passed

  3. self-assigned this
    on Aug 21, 2026
  4. mstomar125 commented on Sep 3, 2026

    @mstomar125
    ContributorAuthor

    Independently re-verified before starting today's drain: this was split into #2228/#2229/#2230, all closed+merged, plus the same #2226 backend-recovery fix that closed the upstream #2172. Confirmed live in packages/nlp-engine/src/chat/mod.rs. Board status was stuck on Todo despite the real GitHub closure — fixing that now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions