ds4 icon indicating copy to clipboard operation
ds4 copied to clipboard

Shutdown checkpoint is never found on restart when the conversation began after another live conversation in the same process

Open pswai opened this issue 2 months ago • 4 comments

Build: main 9139e2a, Metal, M4 Max 64 GB, Qwen3.8-Flash-Next-Q2.gguf (self-contained), --mtp -c 163840 --prefill-chunk 1024 --kv-disk-dir DIR --kv-disk-space-mb 16384. Same result on ivanfioravanti/ds4-metal 6c1e836 (identical ds4_kvstore.c), so it predates the Qwen merge and is not specific to this model's checkpoint payload.

Repro (10 minutes, two small OpenAI-compatible clients)

  1. Conversation A: 4 chat turns, ~35–40K tokens.
  2. Conversation B: 8 turns with tools, ~80K tokens. B's first request logs live kv cache miss live=38403 prompt=11129 common=3 vision=match reason=token-mismatch then kv cache stored tokens=38403 ... reason=evict (A goes to disk), and B is prefilled.
  3. SIGTERM the server. Log: persisting resident KV cache before shutdown slot=0 tokens=79188 → kv cache stored tokens=79188 ... reason=shutdown key=token-text. The file is on disk; its header SHA matches its text; the text starts with B's system prompt.
  4. Restart with the same flags and --trace; resend B's exact history plus one user turn (~90K tokens).

Result: full re-prefill. Trace for that request:

memory_miss_reason: no-live-checkpoint
cache_source: none
cached_tokens: 0
disk_cached_tokens: 0

No kv cache hit and no kv cache skipped line; the lookup found nothing, not even B's smaller continued checkpoints.

What does and does not matter (all tested)

Variant Restores after restart?
B alone in the process (no A), empty cache dir yes (cache_source: disk-text, 80,091 tokens, ~0.3 s)
B alone, cache dir at budget (--kv-disk-space-mb 3072) yes
A (with tools) then B (with tools) no
A without tools, then B with tools no
A with tools, then B without tools no

So tool calls, cache pressure and the eviction order of #444 are ruled out. The one condition that flips the result is whether B started cold (fresh process, live position 0) or warm (another conversation was live; B's first request matched a few common tokens of it and the rest was re-prefilled). In every warm case B's first continued checkpoint lands at 8,192 tokens instead of the cold case's 10,240, so B's session bookkeeping differs from the first turn on.

Where I got to in the code (pointers, not a diagnosis)

  • ds4_kvstore_find_text_prefix hashes the prompt prefix against every index entry (longest SHA1 match), so a miss means no stored text is a byte prefix of the replayed prompt.
  • The stored text comes from slot->thinking_live.visible_text / responses_live.visible_text when that record is valid and live_tokens == tokens->len (kv_cache_store_current, ds4_server.c ~11275), otherwise from ds4_kvstore_render_tokens_text (detokenised). token_text_disk_key is set from prompt_preserves_reasoning && !visible_continuation.
  • My guess is that after a warm start the stored text for B is produced on a different path than after a cold start (detokenised vs the client's rendered text, or with/without the thinking-visible handling), so its SHA1 can never equal the hash of the client's bytes. I did not verify which branch was taken; a log line naming the text source at store time would settle it in one run.

Why it matters

A daily server always has other conversations behind it (cron jobs, side requests), so in practice the live conversation is never restored after a graceful restart; at 160K that is an ~8-minute first turn every time the service restarts.

Clients used: two ~150-line Python scripts (streaming, tools); happy to attach them or run a variant with extra logging.

pswai avatar Sep 14 '26 23:09 pswai

I could reproduce this even with DeepSeek Flash, so it's not Qwen specific.

emilianbold avatar Sep 15 '26 15:09 emilianbold

I am working on a fix for this in https://github.com/antirez/ds4/pull/1058 I think it's almost done but I need to look some more tomorrow.

emilianbold avatar Sep 15 '26 16:09 emilianbold

Additional data point in the same family, but with no restart involved — a single long-lived process where the conversation never left memory, and where the Qwen tool-turn visible checkpoint is present and working most of the time.

Setup: M4 Max 128 GB, macOS 27.0, main-based build 7a8ca68, Qwen3.8 Flash Next Q4 (self-contained n-grams), -c 393216 (YaRN 1.5), tools enabled, one client conversation with 30 tool calls. Result: 34 requests, 133,718 generated tokens over 64.6 minutes.

What worked: 28 thinking live continuation match=visible-prefix lines — incremental prefills of 79–3,513 tokens. So the Qwen path is not broken in general.

Where it broke: 6 misses, and the three expensive ones fell back to a coarse store instead of the live prefix that had just matched:

13:19:52  live kv cache miss live=134274 prompt=108281 common=22872   -> prefilled  91897 tokens in 148.7 s
13:29:58  live kv cache miss live=135132 prompt=107173 common=106037  -> prefilled  25253 tokens in  41.0 s (disk hit at 81920)
13:48:36  live kv cache miss live=161562 prompt=124461 common=107172  -> prefilled  42541 tokens in  70.6 s (disk hit at 81920)

Two things that may be useful for #1058:

  1. In the 13:48:36 case the live stream matched the replayed prompt for 107,172 tokens, yet the server resumed from a disk checkpoint at 81,920 and re-prefilled 42,541 tokens — roughly 42 s that the matched live prefix could have covered.

  2. In all three cases the checkpoint stored just before/at the miss was keyed on the full live position including the hidden reasoning, and the replayed prompt was much shorter:

    13:19:54  kv cache stored tokens=134274 ... reason=evict key=thinking-visible size=4484.61 MiB
    13:30:00  kv cache stored tokens=135132 ... reason=evict key=thinking-visible size=4512.56 MiB
    13:48:37  kv cache stored tokens=161562 ... reason=evict key=thinking-visible size=5372.93 MiB
    

    The following request was 4,447 / 6,439 tokens longer than the previous one, while the turn itself had generated 30,440 / 43,540 tokens — i.e. the client replays its visible transcript without reasoning_content, so a store anchored on those tokens can never byte-prefix-match.

The turns that trigger this are the ones with very long hidden reasoning (30,440 / 43,540 / 34,967 tokens), which may be why "up to 10K tokens behind" undershoots in this case.

Happy to re-run the same workload (same model, flags and client) against a build of #1058 and report the miss/prefill numbers, if that is useful.

cropduster avatar Sep 16 '26 13:09 cropduster

Another in-process datapoint (no restart), on main 9139e2a, Qwen3.8-Flash-Next Q2, M4 Max 64 GB, --kv-disk-dir with a 64 GB budget, one resident session.

Three chat conversations alternated for three rounds, each turn appending a 10K-token tool result: A (120K→151K) and B (130K→162K) whose assistant replies contain tool calls, and C (100K→131K) whose replies are plain text (the history still contains earlier tool calls and tool results, and every request sends tools).

  • A and B: every switch logs live kv cache miss … reason=token-mismatch, then kv cache stored … reason=evict key=thinking-visible, and the next request for that conversation logs kv cache hit text tokens=… key=thinking-visible load=850–1800 ms. Six restores, all hits.
  • C: every switch stores kv cache stored tokens=100845 reason=evict key=token-text (then 110792, then 120459). None of those files was ever evicted (the 67 disk-cache-full evictions in the window are all 10240-multiple continued prefixes plus a few small unrelated files). Yet C's next request reports cached_tokens: 0 and re-prefills the whole prompt: 231 s, 251 s, 274 s.

So the checkpoint exists on disk but is looked up under a different key than it was stored under, and the only difference between C and A/B is that C's last assistant message has no tool_calls. That looks like the same should_remember_thinking_checkpoint path #1058 touches, hit here on a live slot switch rather than a restart. Happy to run the PR's smoke test or share the harness.

pswai avatar Sep 18 '26 01:09 pswai