prefix cache: the host tier cannot hold a streamed entry, so it is off

The prefix cache's host tier and --kv-resident are exclusive, and the engine
used to find that out at the first restore. An entry covers every cached
group; a streamed group's rows are addressed by the block id its sequence
used, in that group's own pinned mirror, so a slot cannot stand in for them
and their ids cannot be given up in exchange for one.

Three defects came from ignoring that:

  * the write-back gathered the resident sibling with the streamed group's
    block id, so the host slot held another group's row (TierExec indexes
    its block vector by gi_, the entry's ids are in groups_ order);
  * demotion released the streamed group's ids too (PrefixCache::
    release_blocks walks every group), handing the mirror rows the entry
    lives in back to the allocator;
  * a restore reserved one group's ids against an entry that has two, so
    rebind returned RAD_E_SHAPE and the record was dropped -- and
    engine.cpp's failure path then released those ids against the wrong
    pools.

Measured on 2x16GB tp2, three 44220-token conversations and a replay of the
first after the third displaced it, streaming on: 20247 entries demoted,
8588 restored, replay 0 cached tokens / 16.5s against streaming off's
44160 cached / 0.4s. The copies were pure waste.

Two rules now:

  * PrefixCache::demotable() is false when any cached group streams: the
    tiers are never told about such an entry (insert), and trim_sparing no
    longer skips it as though its copy were in flight, so it is evicted
    whole with every group's ids.
  * kvstream_prefix_host_off() turns --prefix-cache-mib off at bring-up,
    before anything is sized from it, when cfg.kv_streams is set -- the
    flag asked, the architecture streams and the ring is under the context.
    A flag with no streaming behind it (0, a ring that covers the context,
    a model with no kv_resolve) keeps the tier exactly as configured.

On the cards: --kv-resident 32768 reports "no host cache ... so it is off"
and 0 (off); --kv-resident 131072 keeps 2.00 GiB / 38836 entries. The tests
live in the mem test file's own commit; the docs in the docs commit.
This commit is contained in:
C_Tamas committed 2026-10-25 07:03:03 +07:00
1 parent 8a765db488
commit 2bf1096131
6 files changed
+82 -7

No files matched your search

+16 -2
View File
@@ -182,8 +182,22 @@ void kvstream_ring(const Config& cfg, int64_t ctx, int64_t block_size, int64_t s
}
}
int kvstream_cap_probe(KVManager& probe, Config& cfg, const Program& prog, int64_t ctx) {
cfg.kv_streams = false;
/* The prefix cache's host tier cannot hold an entry that contains a streamed group: its rows are
* addressed by the block id a sequence used, so they cannot be copied into a slot and their ids
* cannot be given up for one (PrefixCache::demotable, docs/KV-STREAMING.md §4.1). A host tier
* configured beside a streamed group would only be pinned and never used, so it is turned off
* before anything is sized. `cfg.kv_streams` is what kvstream_cap_probe sets when the flag asks,
* the architecture serves it and the ring is under the context -- a flag alone, a model with no
* streamed path and a ring that covers the context all leave it false, and the tier stays on.
* Returns the MiB taken off, for the caller's one line; 0 when nothing changed. */
int64_t kvstream_prefix_host_off(Config& cfg) {
if (!cfg.kv_streams || cfg.prefix_cache_mib <= 0) return 0;
const int64_t was = cfg.prefix_cache_mib;
cfg.prefix_cache_mib = 0;
return was;
}
int kvstream_cap_probe(KVManager& probe, Config& cfg, const Program& prog, int64_t ctx) { cfg.kv_streams = false;
if (cfg.kv_resident <= 0) return RAD_OK;
const int32_t g = kv_stream_group(probe, &prog);
if (g < 0) return RAD_OK;