ds4 icon indicating copy to clipboard operation
ds4 copied to clipboard

kvstore: don't let a shared fixed payload cost bias eviction toward big files

Open Flor1an-B opened this issue 3 months ago • 1 comments

Fixes #444.

The reported symptom

Under disk-KV pressure, small "continued" checkpoints (the early, still-reusable prefixes of a growing conversation) get evicted before large ones. Eventually only the largest (full-length) snapshot survives, and once the incoming prompt diverges anywhere before its stored length, it no longer matches — forcing a full re-prefill that a smaller, earlier checkpoint would have avoided. The issue's own math pointed at "token density increases slightly with file size due to fixed header overhead."

Where the fixed cost actually comes from

Not the 48-byte file header (negligible) — session_payload_live_tensor_bytes() caps the persisted raw SWA cache at raw_window rows regardless of checkpoint length (session_raw_live_rows()). So every checkpoint beyond raw_window tokens — the common case for anything past the disk cache's default min_tokens floor — pays roughly the same fixed number of raw-cache bytes no matter how many tokens it actually holds. That shared fixed cost is a much bigger fraction of a short checkpoint's file_size than a long one's, so tokens/file_size scores short checkpoints lower than long ones at equal hit counts — not because they're less useful, but purely because they're short.

Fix

ds4_kvstore now carries an optional fixed_overhead_bytes estimate (defaults to 0, so ds4-agent and any other caller that doesn't set it are completely unaffected — this is opt-in). ds4_kvstore_entry_eviction_score() subtracts it from file_size before computing density, so the score reflects each checkpoint's own marginal bytes rather than a cost every checkpoint pays once regardless of length.

New ds4_engine_checkpoint_fixed_overhead_bytes() computes the estimate from the engine's own raw_window/layer-count/head-dim shape (returns 0 for GLM, whose dense KV cache scales with checkpoint length and has no equivalent fixed cost — I didn't want to apply an unverified correction there). ds4-server wires it in once at kv_cache_open() time, right next to the existing budget_bytes setup — no new config surface.

Testing

  • Full existing ds4_test suite passes unchanged (fixed_overhead_bytes defaults to 0, so behavior is bit-for-bit identical when unset).
  • New regression test test_kv_cache_eviction_ignores_shared_fixed_overhead: two entries with the exact same marginal (per-token) cost but different sizes — scores differ without the fix (reproducing the reported bias) and become equal with it — plus an edge-case check that an overestimated overhead can't divide by zero or go negative.

I considered a couple of alternatives before landing here — worth saying why I didn't:

  • Estimating the overhead by fitting it from the existing on-disk entry population (no cross-module engine knowledge needed) — works but adds real complexity/fragility to a hot eviction path for what the engine already knows exactly.
  • Actually reworking "value" to account for short prefixes being more robust to future divergence than long ones (not just equally dense) — a real effect, but a deeper design change than this specific bug needs; happy to discuss separately if useful.

🤖 Generated with Claude Code

Flor1an-B avatar Aug 05 '26 17:08 Flor1an-B

Self-review caught an incompleteness: the fixed-overhead estimate only accounted for the raw SWA cache's contribution, missing `layer_attn_state_bytes()`/`layer_index_state_bytes()` — per-compressed-layer terms that `session_payload_live_tensor_bytes()` also adds and that depend only on each layer's fixed compress ratio, not checkpoint length. Pushed a fix that mirrors the full per-layer loop (excluding only the genuinely token-scaling terms). Full test suite still passes.

Flor1an-B avatar Aug 05 '26 19:08 Flor1an-B