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++
conversation cache: a conversation parked again keeps at most an eighth of each K/V buffer in reserve (was up to 16 MiB per buffer, 0.4-0.5 GB per conversation)
BlueKingMuch53f72571fb74
diff --git a/docs/DETAILS.md b/docs/DETAILS.mdindex 5bd5fbbbd..7339d2ad2 100644--- a/docs/DETAILS.md+++ b/docs/DETAILS.md@@ -1018,11 +1018,11 @@ Snapshots contain running state, checkpoints, used K/V pages, and draft-layer K/They add host RAM, not another model or VRAM allocation. The byte budget also countsan incoming snapshot during a switch. After a restore, unchanged K/V pages can beretained for the next parking operation; growth appends storage without copying-the existing pages. Rewinds refresh the affected pages, and running state and+the existing pages, with room for the next turns of at most an eighth of each+buffer (and at most 16 MiB), which the budget counts like the rest of the snapshot.+Rewinds refresh the affected pages, and running state andcheckpoints are captured again. Retained active K/V counts against the same bytebudget and is discarded before evicting parked entries under memory pressure.-If reserving space for growth would evict another conversation, parking uses a-full capture instead.Oldest parked entries are evicted first.Oversized snapshots or host allocation failures fall back to ordinary prompt processing.`--conversation-cache-min-tokens N` (default 0 = park every conversation) parks only conversations of at least Ndiff --git a/include/strata/core/conversation_buffer.hpp b/include/strata/core/conversation_buffer.hppindex 88a010b42..d8a451776 100644--- a/include/strata/core/conversation_buffer.hpp+++ b/include/strata/core/conversation_buffer.hpp@@ -54,7 +54,7 @@ public:const size_t full = extra / segment_bytes, tail = extra % segment_bytes;if (!add(total, full * segment_bytes)) return SIZE_MAX;if (tail && !add(total, segment_capacity(tail, full ? segment_bytes :- segments_.empty() ? 0 : segments_.back().capacity(), size_ != 0))) return SIZE_MAX;+ segments_.empty() ? 0 : segments_.back().capacity(), size_ != 0, size_))) return SIZE_MAX;size_t count = segments_.size();if (!add(count, full + (tail != 0))) return SIZE_MAX;if (count > segments_.capacity() &&@@ -63,7 +63,8 @@ public:}void resize(size_t n, uint8_t value = 0) {if (n > size_) {- const bool growing = size_ != 0;+ const size_t held = size_;+ const bool growing = held != 0;const size_t spare = segments_.empty() ? 0 : segments_.back().capacity() - segments_.back().size();const size_t extend = std::min(n - size_, spare);const size_t extra = n - size_ - extend;@@ -76,7 +77,7 @@ public:const size_t count = std::min(segment_bytes, n - size_);const size_t previous = segments_.empty() ? 0 : segments_.back().capacity();Segment segment;- segment.reserve(segment_capacity(count, previous, growing));+ segment.reserve(segment_capacity(count, previous, growing, held));segment.resize(count, value);segments_.push_back(std::move(segment));size_ += count;@@ -126,9 +127,14 @@ private:// A fresh capture allocates exactly its payload. Later appends reserve// geometrically growing segments, bounded by 16 MiB. Small chat turns then// extend the final allocation instead of adding one transfer per turn.- static size_t segment_capacity(size_t count, size_t previous, bool growing) {+ // The reserve is also bounded by an eighth of what the buffer held before+ // the append (at least 64 KiB): a parked conversation carries it in every+ // K/V buffer, and at up to 16 MiB each it came to 0.4-0.5 GB of a ~1 GB+ // conversation (Qwen3.8-Flash-Next), counted against the cache's budget.+ static size_t segment_capacity(size_t count, size_t previous, bool growing, size_t held) {if (!growing) return count;- return std::max(count, std::min(segment_bytes, std::max<size_t>(65536, std::min(previous, segment_bytes/2)*2)));+ const size_t geometric = std::min(segment_bytes, std::max<size_t>(65536, std::min(previous, segment_bytes/2)*2));+ return std::max(count, std::min(geometric, std::max<size_t>(65536, held / 8)));}static bool add(size_t& n, size_t extra) {if (extra > SIZE_MAX - n) return false;diff --git a/src/core/conversation_cache_test.cpp b/src/core/conversation_cache_test.cppindex 665513e37..fb9d51c9a 100644--- a/src/core/conversation_cache_test.cpp+++ b/src/core/conversation_cache_test.cpp@@ -58,6 +58,42 @@ int main() {bytes.visit(0,bytes.size(),[&](const uint8_t*,size_t,size_t){++segments;return true;});check(segments <= 4,"thousands of small turns do not create thousands of restore transfers");}+ {+ // a parked conversation's K/V buffer (16 MiB, as captured) grows by one turn: the reserve for later turns is+ // an eighth of the buffer at most, not another 16 MiB segment+ ConversationBuffer bytes;+ bytes.resize(ConversationBuffer::segment_bytes, 3);+ const size_t captured = bytes.bytes();+ check(captured - bytes.size() < 1024, "a fresh capture reserves nothing");+ const size_t turn = 1 << 20;+ const size_t peak = bytes.allocation_peak(bytes.size() + turn);+ bytes.resize(bytes.size() + turn, 4);+ check(bytes.bytes() <= peak, "a turn's growth stays within the predicted allocation peak");+ check(bytes.bytes() - captured <= turn + ConversationBuffer::segment_bytes / 8 + 1024,+ "the reserve for later turns is at most an eighth of the buffer");+ const size_t reserved = bytes.bytes();+ bytes.resize(bytes.size() + turn / 2, 5);+ check(bytes.bytes() == reserved, "the next small turn fills the reserve instead of allocating");+ size_t segments = 0;+ bytes.visit(0, bytes.size(), [&](const uint8_t*, size_t, size_t) { ++segments; return true; });+ check(segments == 2, "the reserve extends the turn's segment");+ }+ {+ // many turns of 1 MiB onto a 16 MiB buffer: the reserve grows with the buffer, so the segments do not+ // multiply one per turn, and the reserve never exceeds an eighth of the buffer+ ConversationBuffer bytes;+ bytes.resize(ConversationBuffer::segment_bytes, 1);+ for (int k = 0; k < 256; ++k) {+ const size_t before = bytes.size();+ const size_t peak = bytes.allocation_peak(before + (1 << 20));+ bytes.resize(before + (1 << 20), 2);+ check(bytes.bytes() <= peak, "each turn stays within the predicted allocation peak");+ check(bytes.bytes() - bytes.size() <= before / 8 + 4096, "the reserve stays within an eighth");+ }+ size_t segments = 0;+ bytes.visit(0, bytes.size(), [&](const uint8_t*, size_t, size_t) { ++segments; return true; });+ check(segments <= 64, "256 turns of 1 MiB take far fewer than 256 segments");+ }{ConversationKv layer;layer.k.resize(400);