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 turn's parked copy supersedes the previous turn's, so a subagent no longer evicts its parent (#342)
Niko1221fc2eb78e0ab9
diff --git a/include/strata/core/conversation_cache.hpp b/include/strata/core/conversation_cache.hppindex 40490a58c..0a190c709 100644--- a/include/strata/core/conversation_cache.hpp+++ b/include/strata/core/conversation_cache.hpp@@ -164,8 +164,44 @@ public:return true;}+ // #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+ // checkpoints older than that one. A subagent's successive turns parked one such copy each, and make_room's+ // oldest-first eviction then pushed the parent conversation out after `slots` turns. An entry without+ // checkpoints, or whose deepest checkpoint the outgoing chain does not hold (another conversation that only+ // shares the system prompt's root with it), is kept. Returns how many were dropped.+ size_t drop_superseded(const std::vector<int32_t>& ids, const std::vector<ConversationImageKey>& images,+ const std::vector<ConversationCheckpoint>& checkpoints, bool cvec) {+ auto held = [&](const ConversationCheckpoint& c) {+ if (c.ids == ids && c.imgs == images) return true;+ for (const auto& k : checkpoints)+ if (k.ids == c.ids && k.imgs == c.imgs) return true;+ return false;+ };+ size_t dropped = 0;+ for (size_t i = 0; i < entries_.size();) {+ const auto& e = entries_[i];+ const ConversationCheckpoint* deepest = nullptr;+ for (const auto& c : e.checkpoints)+ if (!deepest || c.ids.size() > deepest->ids.size()) deepest = &c;+ if (e.cvec == cvec && deepest && !deepest->ids.empty() && held(*deepest)) {+ bytes_ -= e.bytes();+ entries_.erase(entries_.begin() + (std::ptrdiff_t) i);+ ++dropped;+ continue;+ }+ ++i;+ }+ superseded_ += dropped;+ return dropped;+ }+ size_t superseded() const { return superseded_; }+bool put(SavedConversation&& image, size_t held = 0) {const size_t n = image.bytes();+ if (!enabled() || held > budget_ || n > budget_ - held) return false; // make_room's refusal, first+ drop_superseded(image.live.ids, image.live.imgs, image.checkpoints, image.cvec);if (!make_room(n, held)) return false;entries_.push_back(std::move(image));bytes_ += n;@@ -173,7 +209,7 @@ public:}private:- size_t budget_ = 0, slots_ = 0, bytes_ = 0, evictions_ = 0;+ size_t budget_ = 0, slots_ = 0, bytes_ = 0, evictions_ = 0, superseded_ = 0;std::deque<SavedConversation> entries_; // least recently active firstConversationKvReuse reuse_;};diff --git a/src/core/conversation_cache_test.cpp b/src/core/conversation_cache_test.cppindex ecb71f19f..f10b729b5 100644--- a/src/core/conversation_cache_test.cpp+++ b/src/core/conversation_cache_test.cpp@@ -142,6 +142,67 @@ int main() {check(cache.size() == 1, "oversized put leaves cache unchanged");check(!cache.make_room(0, one*2+1), "held larger than budget cannot underflow");}+ {+ // #342: MAIN -> SUB (9 turns) -> MAIN with 4 slots. Every SUB turn parks the previous turn's live state+ // (its reply as generated, which the next request re-rendered) with a chain that holds that turn's+ // boundary checkpoint; the copy a turn back adds only that stale tail and is dropped, so MAIN survives.+ auto cp = [](std::vector<int32_t> ids) { ConversationCheckpoint c; c.ids = std::move(ids); return c; };+ auto with = [](std::vector<int32_t> prefix, std::initializer_list<int32_t> more) {+ prefix.insert(prefix.end(), more); return prefix;+ };+ const std::vector<int32_t> root = {1, 2, 3, 4};+ SavedConversation main = image({});+ main.live.ids = with(with(root, {10, 11, 12}), {13, 14});+ main.checkpoints = {cp(root), cp(with(root, {10, 11, 12}))};+ const size_t big = 1 << 20;+ ConversationCache cache(big, 4);+ check(cache.put(std::move(main)), "park MAIN");+ std::vector<int32_t> history = with(root, {20}); // SUB's conversation so far, re-rendered+ std::vector<ConversationCheckpoint> chain = {cp(root)};+ for (int turn = 1; turn <= 9; ++turn) {+ chain.push_back(cp(history)); // the turn boundary the next request resumes from+ SavedConversation sub = image({});+ sub.live.ids = with(history, {900, (int32_t) turn}); // + the reply with its thinking (stale tail)+ sub.checkpoints = chain;+ check(cache.put(std::move(sub)), "park SUB turn");+ check(cache.size() <= 2, "one parked copy of SUB at a time");+ history = with(history, {30, (int32_t) turn}); // the reply as the next request renders it+ }+ check(cache.evictions() == 0 && cache.superseded() == 8, "8 superseded copies dropped, nothing evicted");+ const auto m = cache.best(with(with(root, {10, 11, 12}), {13, 14, 15}), {}, true);+ check(m.tokens == 9 && m.live, "MAIN still restores in full");+ const auto s = cache.best(with(history, {40}), {}, true);+ check(s.tokens == (int64_t) history.size() - 2, "SUB resumes from its last turn boundary");+ }+ {+ // what is NOT superseded: another conversation sharing only the root, an entry without checkpoints, the+ // other steering mode, a branch whose deepest checkpoint the new chain does not hold+ auto cp = [](std::vector<int32_t> ids) { ConversationCheckpoint c; c.ids = std::move(ids); return c; };+ ConversationCache cache(1 << 20, 8);+ SavedConversation other = image({1, 2, 3, 4, 50, 51, 52});+ other.checkpoints = {cp({1, 2, 3, 4}), cp({1, 2, 3, 4, 50, 51})};+ cache.put(std::move(other));+ cache.put(image({1, 2, 3, 4, 7, 7})); // no checkpoints+ SavedConversation steered = image({1, 2, 3, 4, 60, 61}, false);+ steered.checkpoints = {cp({1, 2, 3, 4, 60})};+ cache.put(std::move(steered));+ SavedConversation branch = image({1, 2, 3, 4, 60, 70, 71});+ branch.checkpoints = {cp({1, 2, 3, 4}), cp({1, 2, 3, 4, 60, 70})};+ cache.put(std::move(branch));+ SavedConversation incoming = image({1, 2, 3, 4, 60, 80, 81});+ incoming.checkpoints = {cp({1, 2, 3, 4}), cp({1, 2, 3, 4, 60})};+ check(cache.put(std::move(incoming)), "park a conversation sharing roots with all of them");+ check(cache.size() == 5 && cache.superseded() == 0, "none of them is superseded");+ SavedConversation same_state = image({1, 2, 3, 4, 60, 70}); // live equal to the branch's deepest point+ same_state.checkpoints = {cp({1, 2, 3, 4})};+ check(cache.put(std::move(same_state)) && cache.superseded() == 1 && cache.size() == 5,+ "the live state equal to an entry's deepest checkpoint supersedes it");+ SavedConversation huge = image({1, 2, 3, 4, 60, 80});+ huge.checkpoints = {cp({1, 2, 3, 4}), cp({1, 2, 3, 4, 60})};+ huge.live.gdn.resize(2 << 20);+ check(!cache.put(std::move(huge)) && cache.size() == 5 && cache.superseded() == 1,+ "an oversized put drops nothing");+ }{ConversationCache disabled(0,4), no_slots(1024,0);check(!disabled.enabled() && !no_slots.enabled(), "both disable switches");diff --git a/src/program/generate.cpp b/src/program/generate.cppindex 30c1a13e3..41acd0329 100644--- a/src/program/generate.cpp+++ b/src/program/generate.cpp@@ -3986,6 +3986,11 @@ int main(int argc, char** argv) {reuse = {};estimate = fresh_estimate;}+ // #342: before make_room evicts oldest-first, the copies of this conversation a turn back go (they hold+ // nothing the outgoing chain does not, apart from the tail this conversation rewrote)+ if (const size_t dropped = conversations.drop_superseded(live, live_imgs, checks, cvec_cached))+ std::fprintf(stderr, "strata serve: conversation cache: dropped %zu superseded cop%s of this "+ "conversation; parked=%zu\n", dropped, dropped == 1 ? "y" : "ies", conversations.size());if (!conversations.make_room(estimate, held)) {std::fprintf(stderr, "strata serve: conversation cache: skip parking (snapshot %zu MiB exceeds available budget)\n",estimate >> 20);