serve: a full conversation cache evicts to pass the physical-RAM gate instead of dropping the snapshot

The parking path has two gates in series: make_room() balances the cache's own budget, then conversation_memory_admit() checks the host's free RAM against the snapshot plus --conversation-cache-min-free-mib. The second was final: a failure dropped the snapshot even though the cache held conversations whose RAM it could have, and the next request read its whole prompt again (270k tokens, 96 s, measured by the reporter in #1347).

Evict the least recently active parked conversations until the gate admits the snapshot, bounded by the slot count; entries that hold a pinned shared prefix are not evicted. The failure line says how many were evicted and how many remain.

Reported with the fix by alanthinker in #1347.
This commit is contained in:
alanthinker authored and Niko1221 committed 2026-10-07 20:37:05 +02:00
1 parent d7932123e1
commit 940011dad9
3 files changed
+63 -9

No files matched your search

+19
View File
@@ -98,6 +98,25 @@ int main() {
check(cache.best(b, {}, true).tokens == 0 && cache.best(a, {}, true).tokens == 3, "B evicted before A");
check(cache.evictions() == 1, "eviction counter");
}
{
// evict_oldest(): the physical-RAM admission gate in generate.cpp frees parked conversations
// one at a time and re-checks the host's free memory, so this has to be exactly the oldest-first
// step make_room() takes - same accounting, same counters - and it has to stop at empty.
ConversationCache cache(4096, 4);
cache.put(image({1, 2, 3}));
cache.put(image({9, 8, 7}));
check(cache.size() == 2 && cache.slots() == 4, "slots() reports the configured limit");
const size_t both = cache.bytes();
cache.evict_oldest();
check(cache.size() == 1 && cache.evictions() == 1, "evict_oldest drops exactly one, oldest first");
check(cache.best(a, {}, true).tokens == 0 && cache.best(b, {}, true).tokens == 3,
"the oldest conversation went, the newest stayed");
check(cache.bytes() == both - image({1, 2, 3}).bytes(), "evict_oldest releases the entry's bytes");
cache.evict_oldest();
check(cache.size() == 0 && cache.bytes() == 0, "evicting the last parked conversation empties the cache");
cache.evict_oldest(); // the gate's loop can reach this once the cache is empty
check(cache.size() == 0 && cache.evictions() == 2, "evict_oldest on an empty cache is a no-op");
}
{
auto s = image({1, 2, 3});
ConversationCheckpoint cp;