ds4 icon indicating copy to clipboard operation
ds4 copied to clipboard

--kv-cache-cold-max-tokens also deletes checkpoints on load - including continued frontiers - which makes every deep restore single-use

Open kandaman opened this issue 4 months ago • 0 comments

TL;DR

--kv-cache-cold-max-tokens is documented as a store-side gate, and on the store side it behaves exactly as documented. But the same constant is reused on the load side, where a successful hit larger than the threshold is unlink()ed immediately after it is read. The deletion is not limited to checkpoints written by the cold path: continued frontiers, which are governed by a different flag (--kv-cache-continued-interval-tokens), are deleted by the cold threshold too.

Net effect: any checkpoint deeper than 30,000 tokens (default) can be reused exactly once, ever. Nothing in the help text says so.

Code (upstream main, b7e9f0091139999b6c070a57590c447c5741da5c)

Help text — store side only:

ds4_help.c:356
opt(fp, c, "--kv-cache-cold-max-tokens N", "Save cold first prompts up to N tokens. 0 disables. Default: 30000");

For contrast, the sibling flag two lines up documents both directions explicitly:

ds4_help.c:355
opt(fp, c, "--kv-cache-min-tokens N", "Do not save/load checkpoints shorter than N. Default: 512");

Store side — matches the documentation:

/* ds4_server.c:11165-11177 */
int cold_store_len = 0;
if (cached == 0 &&
    s->kv.enabled &&
    prompt_for_sync->len >= s->kv.opt.min_tokens &&
    s->kv.opt.cold_max_tokens > 0 &&
    prompt_for_sync->len <= s->kv.opt.cold_max_tokens)
{
    ...
}

Load side — not documented anywhere:

/* ds4_kvstore.c:1317-1339, in ds4_kvstore_try_load_text() */
if (loaded > 0) {
    ...
    bool consumed = false;
    if (kc->opt.cold_max_tokens > 0 && loaded > kc->opt.cold_max_tokens) {
        unlink(path);
        consumed = true;
        kv_logf(..., "kv cache hit text ... load=%.1f ms consumed file=%s", ...);
    } else {
        ds4_kvstore_touch_file(path, hdr.hits + 1);
        kv_logf(..., "kv cache hit text ... load=%.1f ms file=%s", ...);
    }

Default:

/* ds4_kvstore.c:34 */
#define KV_CACHE_DEFAULT_COLD_MAX_TOKENS 30000

Why this is surprising

  1. The flag is documented as save-only. A reader who wants deeper cold anchors raises the value; a reader who wants to bound disk usage lowers it. Neither expects it to control how long an already-written checkpoint survives.

  2. The deletion is not scoped to cold checkpoints. ds4_kvstore_try_load_text() does not look at how the file was produced. Continued frontiers written by kv_cache_maybe_store_continued() — whose cadence is set by --kv-cache-continued-interval-tokens (ds4_kvstore.c:730-752) — are deleted by the cold threshold. Two independent knobs, one shared lifetime rule.

  3. The name consumed suggests intent, but the consequence is invisible. If one-shot semantics for large checkpoints are deliberate (large files are expensive to keep around; a hit usually means the session moved past that prefix anyway), that is a defensible design — but it is not stated in the help text, not mentioned in the flag name, and the only signal is the word consumed in a log line.

Observed effect

Measured across four nodes in our deployment (Apple Silicon / Metal, DeepSeek V4 Flash), using kv cache hit text lines from the server log as ground truth for disk restores:

  • 65–83% of all successful disk restores are consumed (deleted on the spot). Per node: 68.0% / 83.3% / 73.4% / 64.9%.
  • The smallest consumed depth observed is 30,103 tokens. There are zero consumed events at or below 30,000 — i.e. the threshold is exactly what selects them, not eviction or LRU.
  • Because deep checkpoints do not survive their first use, restore depth collapses to whatever is still ≤ 30,000 as soon as the chain of "previous turn wrote a new checkpoint" breaks — typically the cold anchor (~21K, the shared system+tools prefix) or the continued frontier at 20,480. Externally this looks like a hard ceiling on reuse depth that is actually a floor of survivors.
  • This happens with disk budget to spare. One node was using ~35% of its configured --kv-disk-space-mb with zero evictions and still fell back to 20,480, which rules out capacity pressure as the cause.
  • Cost side: one cold prefill of a ~92K-token session writes 9 generations (10,240 → 92,160), ≈6.6 GiB. The ≈5.9 GiB above 30,720 is written, read at most once, and then removed.

Relation to #392

#392 (closed) covered the store side of the same constant: first prompts above the threshold silently skip the cold store. This report is the mirror image — the load side of the same constant, affecting checkpoints that were successfully written.

Suggested resolution

Any one of these would resolve it; they are listed in increasing order of change:

  1. Document it. Amend the help text to state both directions, e.g. "Save cold first prompts up to N tokens; checkpoints larger than N are deleted after their first successful load. 0 disables. Default: 30000". This alone would have saved the investigation.
  2. Do not consume continued frontiers. Scope the unlink() to checkpoints written by the cold path, so that the flag that governs a checkpoint's creation also governs its lifetime.
  3. Split the knob. A separate --kv-cache-consume-above-tokens (defaulting to cold_max_tokens for compatibility, 0 to disable) would let deployments with disk headroom keep deep checkpoints reusable without also widening what gets written.

Happy to send a PR for (1), and for (2) or (3) if you have a preference on the shape.

kandaman avatar Aug 03 '26 20:08 kandaman