diff --git a/docs/DETAILS.md b/docs/DETAILS.md index a75509f8..0ff0d1d9 100644 --- a/docs/DETAILS.md +++ b/docs/DETAILS.md @@ -1043,8 +1043,10 @@ list, a compacted history) restores that prefix and leaves the parked conversation where it is, so its next turn still resumes in full; the engine log says "borrowed". A match on a parked conversation's live state or its newest checkpoint is that conversation going on and moves it into the session as -before. When the outgoing conversation fits only in the parked one's room, it -keeps its place and the parked one is moved in whole, as before. +before; so does a match that reaches a pinned shared prefix (`pin=N`), which +keeps one conversation holding the pin. When the outgoing conversation fits only +in the parked one's room, it keeps its place and the parked one is moved in +whole, as before. 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 tokens. A parked conversation costs one of the slots whatever its length, and parking is a switch-time decision, so a diff --git a/include/strata/core/conversation_cache.hpp b/include/strata/core/conversation_cache.hpp index 7d09e53b..560e1ed2 100644 --- a/include/strata/core/conversation_cache.hpp +++ b/include/strata/core/conversation_cache.hpp @@ -267,11 +267,16 @@ public: // take() would move the whole conversation into the session and the new request would overwrite everything after // that checkpoint, so the other conversation's next turn would read its history again. A match on the entry's // live state or on its deepest checkpoint is the same conversation going on (the deepest one: its client - // re-rendered the last reply), whose tail is stale: that is still taken. + // re-rendered the last reply), whose tail is stale: that is still taken. So is a match that reaches a pinned + // shared prefix (pin=N): a borrow copies the checkpoints up to the match, and taking it instead keeps one + // conversation holding the pin, as pin=N expects. bool borrows(const Match& m) const { if (m.tokens <= 0 || m.live || m.index >= entries_.size() || serials_[m.index] != m.serial) return false; size_t deepest = 0; - for (const auto& c : entries_[m.index].checkpoints) deepest = std::max(deepest, c.ids.size()); + for (const auto& c : entries_[m.index].checkpoints) { + if (c.pinned && (int64_t) c.ids.size() <= m.tokens) return false; + deepest = std::max(deepest, c.ids.size()); + } return (size_t) m.tokens < deepest; } // The entry with this identity: its index (SIZE_MAX: gone), or the entry itself (nullptr: gone; the pointer is @@ -285,12 +290,13 @@ public: const size_t i = index_of(serial); return i == SIZE_MAX ? nullptr : &entries_[i]; } - // While a borrowed entry is being restored, parking the outgoing conversation must not evict or drop it. When - // the outgoing image fits only without it, make_room refuses before evicting anything and pin_blocked() says so: - // the caller then takes the entry as before, so the conversation that was active a moment ago keeps its place. - void pin(uint64_t serial) { pinned_ = serial; pin_blocked_ = false; } - void unpin() { pinned_ = 0; } - bool pin_blocked() const { return pin_blocked_; } + // While a borrowed entry (the donor) is being restored, parking the outgoing conversation must not evict or drop + // it: guard() it. When the outgoing image fits only without it, make_room refuses before evicting anything and + // guard_blocked() says so: the caller then takes the entry as before, so the conversation that was active a + // moment ago keeps its place. + void guard(uint64_t serial) { guarded_ = serial; guard_blocked_ = false; } + void unguard() { guarded_ = 0; } + bool guard_blocked() const { return guard_blocked_; } size_t borrowed() const { return borrowed_; } void count_borrow() { ++borrowed_; } @@ -298,9 +304,14 @@ public: // with take() but still alive during the exchange; count it against RAM too. bool make_room(size_t incoming, size_t held = 0) { if (!enabled() || held > budget_ || incoming > budget_ - held) return false; - if (const SavedConversation* p = pinned_ != 0 ? find(pinned_) : nullptr) { - // with everything else gone, would it fit beside the pinned entry? If not, evict nothing - if (slots_ < 2 || p->bytes() > budget_ - held - incoming) { pin_blocked_ = true; return false; } + auto stays = [&](size_t i) { return entries_[i].pinned() || (guarded_ != 0 && serials_[i] == guarded_); }; + if (guarded_ != 0 && index_of(guarded_) != SIZE_MAX) { + // with everything else gone, would it fit beside the entries that stay (the guarded donor and any pinned + // shared prefix)? If not, evict nothing + size_t kept = 0, kept_bytes = 0; + for (size_t i = 0; i < entries_.size(); ++i) + if (stays(i)) { ++kept; kept_bytes += entries_[i].bytes(); } + if (kept >= slots_ || kept_bytes > budget_ - held - incoming) { guard_blocked_ = true; return false; } } if (bytes() > budget_ - held - incoming) reuse_ = {}; while (!entries_.empty() && (entries_.size() >= slots_ || bytes_ > budget_ - held - incoming)) { @@ -318,9 +329,9 @@ public: // 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() { - // the oldest entry that holds no pinned shared prefix and is not the borrowed (pinned) one + // the oldest entry that holds no pinned shared prefix and is not the guarded (borrowed) donor for (size_t i = 0; i < entries_.size(); ++i) { - if (entries_[i].pinned() || (pinned_ != 0 && serials_[i] == pinned_)) continue; + if (entries_[i].pinned() || (guarded_ != 0 && serials_[i] == guarded_)) continue; bytes_ -= entries_[i].bytes(); erase(i); ++evictions_; @@ -365,7 +376,7 @@ public: 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) && - !(pinned_ != 0 && serials_[i] == pinned_)) { + !(guarded_ != 0 && serials_[i] == guarded_)) { bytes_ -= e.bytes(); erase(i); ++dropped; @@ -418,8 +429,8 @@ private: int64_t min_tokens_ = 0; // --conversation-cache-min-tokens: 0 parks every conversation, whatever its size std::deque entries_; // least recently active first std::deque serials_; // entries_[i]'s identity, never reused - uint64_t next_serial_ = 1, pinned_ = 0; - bool pin_blocked_ = false; + uint64_t next_serial_ = 1, guarded_ = 0; + bool guard_blocked_ = false; ConversationKvReuse reuse_; }; diff --git a/src/core/conversation_cache_test.cpp b/src/core/conversation_cache_test.cpp index f49038fe..a21c0d7f 100644 --- a/src/core/conversation_cache_test.cpp +++ b/src/core/conversation_cache_test.cpp @@ -327,17 +327,17 @@ int main() { const auto cont = cache.best(std::vector{1, 2, 3, 4, 10, 11, 12, 13, 14}, {}, true); check(cont.live && !cache.borrows(cont), "A going on is taken"); check(!cache.borrows(cache.best(std::vector{5, 6, 7}, {}, true)), "no match, nothing to borrow"); - // while the outgoing conversations are parked, the pinned donor is neither evicted nor dropped - cache.pin(sib.serial); + // while the outgoing conversations are parked, the guarded donor is neither evicted nor dropped + cache.guard(sib.serial); check(cache.put(image({7, 7, 7})) && cache.put(image({8, 8, 8})), "two outgoing conversations park"); - check(cache.find(sib.serial) != nullptr && cache.size() == 2 && cache.evictions() == 1 && !cache.pin_blocked(), - "the slot limit evicts the other entry, not the pinned donor"); - check(cache.best(std::vector{7, 7, 7, 1}, {}, true).tokens == 0, "the unpinned one went"); + check(cache.find(sib.serial) != nullptr && cache.size() == 2 && cache.evictions() == 1 && !cache.guard_blocked(), + "the slot limit evicts the other entry, not the guarded donor"); + check(cache.best(std::vector{7, 7, 7, 1}, {}, true).tokens == 0, "the other one went"); SavedConversation rewritten = image({1, 2, 3, 4, 10, 11, 40}); // holds A's deepest checkpoint: supersedes A rewritten.checkpoints = {cp({1, 2, 3, 4}), cp({1, 2, 3, 4, 10, 11})}; check(cache.put(std::move(rewritten)) && cache.find(sib.serial) != nullptr && cache.superseded() == 0, - "a pinned donor is not dropped as superseded"); - cache.unpin(); + "a guarded donor is not dropped as superseded"); + cache.unguard(); // identity survives index shifts: take an older entry, the donor's serial still finds it const auto donor_index = cache.index_of(sib.serial); check(donor_index != SIZE_MAX, "donor indexed by its serial"); @@ -345,7 +345,7 @@ int main() { "matches report the entry's serial"); } { - // the outgoing conversation fits only in the pinned donor's room: make_room refuses before evicting anything + // the outgoing conversation fits only in the guarded donor's room: make_room refuses before evicting anything auto cp = [](std::vector ids) { ConversationCheckpoint c; c.ids = std::move(ids); return c; }; SavedConversation a = image({1, 2, 3, 4, 10, 11}); a.checkpoints = {cp({1, 2, 3, 4}), cp({1, 2, 3, 4, 10})}; @@ -355,10 +355,10 @@ int main() { check(tight.put(std::move(a)), "park the donor"); const auto m = tight.best(std::vector{1, 2, 3, 4, 50}, {}, true); check(tight.borrows(m), "a sibling would borrow"); - tight.pin(m.serial); - check(!tight.put(image({9, 9, 9})) && tight.pin_blocked(), "no room beside the pinned donor: refused"); + tight.guard(m.serial); + check(!tight.put(image({9, 9, 9})) && tight.guard_blocked(), "no room beside the guarded donor: refused"); check(tight.size() == 1 && tight.evictions() == 0, "nothing evicted by the refusal"); - tight.unpin(); + tight.unguard(); auto taken = tight.take(tight.index_of(m.serial)); // what the caller does then: take it, as before check(taken.live.ids.size() == 6 && taken.checkpoints.size() == 2 && tight.size() == 0, "the taken donor is whole"); @@ -367,10 +367,58 @@ int main() { b.checkpoints = {cp({1, 2, 3, 4}), cp({1, 2, 3, 4, 10})}; check(one_slot.put(std::move(b)), "park in a single slot"); const auto mb = one_slot.best(std::vector{1, 2, 3, 4, 50}, {}, true); - one_slot.pin(mb.serial); - check(!one_slot.put(image({9, 9, 9})) && one_slot.pin_blocked() && one_slot.size() == 1, - "one slot: the pinned donor blocks, nothing evicted"); - one_slot.unpin(); + one_slot.guard(mb.serial); + check(!one_slot.put(image({9, 9, 9})) && one_slot.guard_blocked() && one_slot.size() == 1, + "one slot: the guarded donor blocks, nothing evicted"); + one_slot.unguard(); + } + { + // pin=N beside a borrow: the guarded donor and a conversation holding a pinned shared prefix both stay; the + // oldest other entry is the one evicted (the case midhatn tested on #1164) + auto cp = [](std::vector ids, bool pinned = false) { + ConversationCheckpoint c; c.ids = std::move(ids); c.pinned = pinned; return c; + }; + SavedConversation doc = image({1, 2, 3, 4, 5, 6, 30, 31}); + doc.checkpoints = {cp({1, 2, 3, 4, 5, 6}, true), cp({1, 2, 3, 4, 5, 6, 30})}; + SavedConversation donor = image({1, 2, 10, 11, 12}); + donor.checkpoints = {cp({1, 2}), cp({1, 2, 10, 11})}; + ConversationCache cache(1 << 20, 3); + check(cache.put(std::move(doc)) && cache.put(std::move(donor)) && cache.put(image({7, 7, 7})), + "pinned document, donor and an ordinary conversation parked"); + const auto m = cache.best(std::vector{1, 2, 40, 41}, {}, true); + check(cache.borrows(m), "a sibling on the donor's root borrows"); + cache.guard(m.serial); + check(cache.put(image({8, 8, 8})) && !cache.guard_blocked() && cache.size() == 3 && cache.evictions() == 1, + "the outgoing conversation parks"); + check(cache.find(m.serial) != nullptr && + cache.best(std::vector{1, 2, 3, 4, 5, 6, 9}, {}, true).tokens == 6 && + cache.best(std::vector{7, 7, 7, 1}, {}, true).tokens == 0, + "only the ordinary entry was evicted: the donor and the pinned document stay"); + check(cache.put(image({9, 9, 9})) && cache.size() == 3 && cache.evictions() == 2 && + cache.find(m.serial) != nullptr && cache.best(std::vector{8, 8, 8, 1}, {}, true).tokens == 0, + "the next outgoing one evicts the previous outgoing one, still not the donor or the document"); + cache.unguard(); + ConversationCache two(1 << 20, 2); + SavedConversation doc2 = image({1, 2, 3, 4, 5, 6, 30}); + doc2.checkpoints = {cp({1, 2, 3, 4, 5, 6}, true)}; + SavedConversation donor2 = image({1, 2, 10, 11, 12}); + donor2.checkpoints = {cp({1, 2}), cp({1, 2, 10, 11})}; + check(two.put(std::move(doc2)) && two.put(std::move(donor2)), "pinned document and donor fill two slots"); + const auto m2 = two.best(std::vector{1, 2, 40}, {}, true); + two.guard(m2.serial); + check(!two.put(image({9, 9, 9})) && two.guard_blocked() && two.size() == 2 && two.evictions() == 0, + "no slot beside the donor and the pinned document: refused before evicting anything"); + two.unguard(); + // a match that reaches the pinned prefix is taken, not borrowed: one conversation keeps holding the pin + const auto on_pin = two.best(std::vector{1, 2, 3, 4, 5, 6, 77}, {}, true); + check(on_pin.tokens == 6 && !on_pin.live && !two.borrows(on_pin), "a pinned prefix is taken, not borrowed"); + ConversationCache three(1 << 20, 3); + SavedConversation doc3 = image({1, 2, 3, 4, 5, 6, 30, 31, 32}); + doc3.checkpoints = {cp({1, 2, 3, 4, 5, 6}, true), cp({1, 2, 3, 4, 5, 6, 30}), cp({1, 2, 3, 4, 5, 6, 30, 31})}; + check(three.put(std::move(doc3)), "a conversation past its pinned prefix is parked"); + const auto past_pin = three.best(std::vector{1, 2, 3, 4, 5, 6, 30, 99}, {}, true); + check(past_pin.tokens == 7 && !three.borrows(past_pin), + "a match past the pinned prefix (the copy would carry the pin) is taken, not borrowed"); } { ConversationCache disabled(0,4), no_slots(1024,0); diff --git a/src/program/generate.cpp b/src/program/generate.cpp index d6fe2b04..9f2b0b53 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -11070,10 +11070,10 @@ int main(int argc, char** argv) { const bool park_worth = o.conversation_cache_park_gain <= 0 || park_held == 0 || (int64_t) live.size() >= (int64_t) park_held + o.conversation_cache_park_gain; if (borrow && !pin_sibling && park_worth) { - conversations.pin(parked.serial); // parking the outgoing conversation must not evict the donor + conversations.guard(parked.serial); // parking the outgoing conversation must not evict the donor const bool parked_ok = park_current(0); - const bool blocked = conversations.pin_blocked(); - conversations.unpin(); + const bool blocked = conversations.guard_blocked(); + conversations.unguard(); if (!parked_ok) { std::printf("ERR %s\n", err.c_str()); return 1; @@ -11145,7 +11145,7 @@ int main(int argc, char** argv) { strata::core::conversation_snapshot_restore_prefix(*donor, parked.tokens, ss, g, use_mtp ? &mtp.kv_state() : nullptr, err) != strata::core::ConversationRestore::restored) { - // validated above and pinned while parking: a failure here is fatal, as for a whole restore + // validated above and guarded while parking: a failure here is fatal, as for a whole restore std::printf("ERR restoring a borrowed conversation prefix: %s\n", err.empty() ? "gone" : err.c_str()); return 1;