Skip to content

feat: use llama.cpp slot state save/restore instead of text-based compression to eliminate re-prefill #5760

Description

@hwangtab

Problem

When context compression is triggered (at contextPercentageThreshold), qwen-code sends a summarized conversation history to the llama-server. This summary is a different token sequence from the original, causing:

  1. Full cache miss — the server's LCP (longest common prefix) matching finds no reusable cached state
  2. Full re-prefill — the entire summarized context must be reprocessed from scratch
  3. Wasted compute — on long sessions (50K+ tokens), this is a significant re-prefill cost (often 100+ seconds on local hardware)

The server's prompt cache becomes useless at exactly the moment it would be most valuable.

Proposed Solution

llama.cpp server already exposes a slot state save/restore API:

POST /slots/{id_slot}/save    — saves full slot state (KV cache + recurrent state) to a file
POST /slots/{id_slot}/restore — restores from that file and continues

Instead of text-based compression, qwen-code could:

  1. Before context limit is reached → call /slots/0/save to snapshot the current model state
  2. When compression would normally trigger → call /slots/0/restore + send only the new tokens since the snapshot
  3. The server resumes from the exact model state — zero re-prefill of prior context

This would replace "summarize history → resend full text" with "snapshot state → append new tokens".

Why This Is Especially Valuable for Qwen3.6-series Models

Qwen3.6-35B-A3B (and similar hybrid recurrent-transformer models) maintain a fixed-size recurrent state that already encodes a compressed representation of all past context. This state is exactly what is saved/restored by the slot API.

  • The recurrent state IS a lossless (for the model's learned compression) summary of past context
  • Saving and restoring it is architecturally more faithful than text summarization
  • llama.cpp PR #24785 recently improved recurrent state correctness during prompt cache operations, making this approach more reliable now

Current Workaround

Users working with local llama.cpp backends currently maintain external markdown files (PROGRESS.md, QWEN.md) to manually persist context across compression boundaries — effectively a manual implementation of what MemGPT/Letta does with external memory. The slot API approach would make this unnecessary for within-session continuity.

Implementation Notes

  • llama.cpp slot save path is configurable (--slot-save-path)
  • The saved state includes both KV cache and recurrent state
  • State files are ~60-500 MiB depending on context length and model size
  • qwen-code would need to track: (a) which slot is active, (b) the token position at snapshot time, (c) the file path of the saved state
  • On reconnect or server restart, fall back to current text-based compression

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    category/performancePerformance and optimizationpriority/P2Medium - Moderately impactful, noticeable problemscope/cachingCaching mechanismsstatus/needs-triageIssue needs to be triaged and labeledtype/feature-requestNew feature or enhancement request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions