Skip to content

Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.

C++

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

alanthinker940011dad986
diff --git a/include/strata/core/conversation_cache.hpp b/include/strata/core/conversation_cache.hpp
index 305d352b9..9938bb6f6 100644
--- a/include/strata/core/conversation_cache.hpp
+++ b/include/strata/core/conversation_cache.hpp
@@ -263,15 +263,29 @@ public:
while (!entries_.empty() && (entries_.size() >= slots_ || bytes_ > budget_ - held - incoming)) {
// the oldest entry that does not hold a pinned shared prefix leaves; with only pinned ones left the new
// image does not fit (the caller skips parking it - the pinned prefix is what the queries come back to)
- auto victim = std::find_if(entries_.begin(), entries_.end(), [](const SavedConversation& e) { return !e.pinned(); });
- if (victim == entries_.end()) return false;
- bytes_ -= victim->bytes();
- entries_.erase(victim);
- ++evictions_;
+ if (!evict_oldest()) return false;
}
return true;
}
+ // The parked conversation that has gone unused the longest. This is make_room()'s loop body, so
+ // the parking path can also free RAM one entry at a time on demand (see the physical-RAM admission
+ // gate in generate.cpp). A ConversationBuffer is a list of 16 MiB segments and each segment is its
+ // own allocation, far above glibc's mmap threshold, so dropping an entry returns the whole footprint
+ // to the kernel at once - the next admission check reads it back from /proc/meminfo.
+ // False when none can go (empty, or only entries that hold a pinned shared prefix are left).
+ bool evict_oldest() {
+ auto victim = std::find_if(entries_.begin(), entries_.end(), [](const SavedConversation& e) { return !e.pinned(); });
+ if (victim == entries_.end()) return false;
+ bytes_ -= victim->bytes();
+ entries_.erase(victim);
+ ++evictions_;
+ return true;
+ }
+
+ // The slot count, so a caller that evicts in a loop has a bound it did not invent.
+ size_t slots() const { return slots_; }
+
// #342: drop the parked entries an outgoing conversation (its live tokens and checkpoint chain) supersedes:
// the same conversation a turn back, whose DEEPEST checkpoint the outgoing chain still holds, so all it adds
// is the tail the client rewrote (the reply as it was generated, before the next request re-rendered it) and
diff --git a/src/core/conversation_cache_test.cpp b/src/core/conversation_cache_test.cpp
index c4883fe04..0ac2f2231 100644
--- a/src/core/conversation_cache_test.cpp
+++ b/src/core/conversation_cache_test.cpp
@@ -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;
diff --git a/src/program/generate.cpp b/src/program/generate.cpp
index eb51ac50c..f1984d054 100644
--- a/src/program/generate.cpp
+++ b/src/program/generate.cpp
@@ -7019,12 +7019,33 @@ int main(int argc, char** argv) {
const uint64_t floor = (uint64_t) o.conversation_cache_min_free_mib * 1024 * 1024;
const size_t retained = reuse.bytes() + stage_retained;
const size_t additional = estimate > retained ? estimate - retained : 0;
- if (!strata::core::conversation_memory_admit(strata::core::conversation_available_memory(),
- additional, floor)) {
- std::fprintf(stderr, "strata serve: conversation cache: skip parking (physical RAM admission; need %zu MiB plus %lld MiB floor, or telemetry unavailable)\n",
- additional >> 20, (long long) o.conversation_cache_min_free_mib);
+ // The physical-RAM gate: the incoming snapshot has to fit beside the floor. make_room()
+ // above only balanced the cache's own budget, so a full cache leaves this one short even
+ // though every parked conversation could give its RAM back - and refusing here throws
+ // away the whole prompt read that produced this snapshot (measured: 270k tokens, 96 s).
+ // Evict the least recently active parked conversations until it fits, or until none are
+ // left. Each ConversationBuffer is a list of 16 MiB segments, each segment its own
+ // allocation, so an evicted entry is back with the kernel before the next check reads
+ // /proc/meminfo; no waiting is needed. slots() bounds the loop, and the two lines
+ // below say what it did either way.
+ size_t evicted = 0;
+ auto admit = [&] {
+ return strata::core::conversation_memory_admit(
+ strata::core::conversation_available_memory(), additional, floor);
+ };
+ while (!admit() && conversations.size() > 0 && evicted < conversations.slots() &&
+ conversations.evict_oldest())
+ ++evicted;
+ if (!admit()) {
+ std::fprintf(stderr, "strata serve: conversation cache: skip parking (physical RAM admission; need %zu MiB plus %lld MiB floor; evicted %zu, %zu still parked, or telemetry unavailable)\n",
+ additional >> 20, (long long) o.conversation_cache_min_free_mib,
+ evicted, conversations.size());
return true;
}
+ if (evicted)
+ std::fprintf(stderr, "strata serve: conversation cache: evicted %zu parked conversation%s to admit this snapshot (%zu MiB plus %lld MiB floor)\n",
+ evicted, evicted == 1 ? "" : "s", additional >> 20,
+ (long long) o.conversation_cache_min_free_mib);
strata::core::SavedConversation image;
size_t reused_bytes = 0;
if (!strata::core::conversation_snapshot_save(image, view, ss, g, draft0, err,