From 53f72571fb74a21312a939ee3aee44b146feeb05 Mon Sep 17 00:00:00 2001 From: BlueKingMuch <53972523+BlueKingMuch@users.noreply.github.com> Date: Fri, 2 Oct 2026 09:14:41 +0000 Subject: [PATCH] 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) A conversation that was restored and is parked again keeps its unchanged K/V pages and appends the new ones (92a3d5f). The appended segment reserved room for later turns, growing geometrically up to 16 MiB - per buffer, and a parked image has one per K/V layer and part (k, v, their scales, the indexer rows, the draft layer). With Qwen3.8-Flash-Next (IQ3_S, int8 K/V) a 32K-token main agent parked again took 1.49 GB instead of 0.97 GB, a 17K-token subagent 1.16 GB instead of 0.74 GB. The cache counts that capacity against its budget, so with 2816 MiB the reserve decides which conversations are evicted. The reserve is now at most an eighth of what the buffer held before the append (at least 64 KiB, and at most 16 MiB as before). Small turns still fill the reserve instead of adding a transfer each: the 4096-append test keeps its segment bound, and 256 turns of 1 MiB onto a 16 MiB buffer take 28 segments (17 before). The docs said parking falls back to a full capture when the reserve would evict another conversation; that check went in 6cd29d9. With the reserve bounded the two captures differ by at most an eighth of the K/V, and the docs now say what the reserve is. --- docs/DETAILS.md | 6 ++-- include/strata/core/conversation_buffer.hpp | 16 ++++++--- src/core/conversation_cache_test.cpp | 36 +++++++++++++++++++++ 3 files changed, 50 insertions(+), 8 deletions(-) diff --git a/docs/DETAILS.md b/docs/DETAILS.md index 5bd5fbbb..7339d2ad 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 counts an incoming snapshot during a switch. After a restore, unchanged K/V pages can be retained 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 and checkpoints are captured again. Retained active K/V counts against the same byte budget 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 N diff --git a/include/strata/core/conversation_buffer.hpp b/include/strata/core/conversation_buffer.hpp index 88a010b4..d8a45177 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(65536, std::min(previous, segment_bytes/2)*2))); + const size_t geometric = std::min(segment_bytes, std::max(65536, std::min(previous, segment_bytes/2)*2)); + return std::max(count, std::min(geometric, std::max(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.cpp index 665513e3..fb9d51c9 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);