serve: a new conversation that starts with a checkpoint of a parked one borrows it, the parked conversation stays (#1164)

Sibling subagents with the same system prompt and tools no longer cost each other their history: only the K/V up to the checkpoint and the checkpoint itself are copied into the session (conversation_snapshot_restore_prefix); the parked conversation stays where it is and is pinned while the outgoing one parks. A match on a parked conversation's live state or newest checkpoint is that conversation going on and is taken as before.

Rebased onto 0.1.44.1 by hand: the pinned shared prefix (#879) and evict_oldest() now also skip the borrowed donor, serials follow every erase, and park-gain (#2265) and the pin= sibling rule gate the outgoing park.

Co-authored-by: BlueKingMuch <53972523+BlueKingMuch@users.noreply.github.com>
This commit is contained in:
Niko1221andBlueKingMuch committed 2026-10-15 07:02:42 +02:00
1 parent 04e3481b99
commit c9e571e057
8 files changed
+294 -18

No files matched your search

+73 -4
View File
@@ -11005,8 +11005,16 @@ int main(int argc, char** argv) {
}
const auto parked = conversations.best(ids, req_imgs, want_cvec);
std::optional<strata::core::SavedConversation> incoming;
if (parked.tokens > std::max(resume, slot_tokens)) incoming.emplace(conversations.take(parked.index));
if (incoming) slot_source = -1;
// A checkpoint inside ANOTHER parked conversation (a sibling subagent's system prompt and tools, say) is
// borrowed: its K/V up to there and the checkpoint come over, and that conversation stays parked for its
// own next turn (ConversationCache::borrows). Everything else is taken whole, as before.
// (a layer split restores whole stage images only, so it never borrows; a batch slot that holds a longer
// start of this prompt wins as before)
bool borrow = stages.empty() && parked.tokens > std::max(resume, slot_tokens) &&
conversations.borrows(parked);
if (parked.tokens > std::max(resume, slot_tokens) && !borrow)
incoming.emplace(conversations.take(parked.index));
if (incoming || borrow) slot_source = -1;
// Reject the entire image before parking/overwriting the outgoing
// state. Invalid entries can safely fall back to its existing prefix.
if (incoming && incoming->stage_images.size() != stages.size()) {
@@ -11030,6 +11038,17 @@ int main(int argc, char** argv) {
incoming.reset();
err.clear();
}
if (borrow) {
const strata::core::SavedConversation* donor = conversations.find(parked.serial);
if (donor == nullptr ||
!strata::core::conversation_snapshot_validate(*donor, ss, g, use_mtp ? &mtp.kv_state() : nullptr,
err)) {
std::fprintf(stderr, "strata serve: conversation cache: cannot borrow from a parked conversation "
"(%s)\n", err.empty() ? "gone" : err.c_str());
borrow = false;
err.clear();
}
}
// Preserve the outgoing branch before any checkpoint rewind, reset,
// or incoming restore overwrites the positional state it requires.
// pin=N: this request resumes exactly at the pinned shared prefix of the conversation the session holds
@@ -11050,8 +11069,30 @@ int main(int argc, char** argv) {
: 0;
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 ((!from_live || incoming || slot_source >= 0) && !pin_sibling && park_worth &&
!park_current(incoming ? incoming->bytes() : 0)) {
if (borrow && !pin_sibling && park_worth) {
conversations.pin(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();
if (!parked_ok) {
std::printf("ERR %s\n", err.c_str());
return 1;
}
if (blocked) {
// the outgoing conversation fits only in the donor's room: it keeps its place, the donor is
// taken whole as before (it was validated above)
std::fprintf(stderr, "strata serve: conversation cache: no room beside the parked conversation "
"this request starts with; taking it instead of borrowing\n");
borrow = false;
if (const size_t at = conversations.index_of(parked.serial); at != SIZE_MAX)
incoming.emplace(conversations.take(at));
if (!park_current(incoming ? incoming->bytes() : 0)) {
std::printf("ERR %s\n", err.c_str());
return 1;
}
}
} else if ((!from_live || incoming || slot_source >= 0) && !pin_sibling && park_worth &&
!park_current(incoming ? incoming->bytes() : 0)) {
std::printf("ERR %s\n", err.c_str());
return 1;
}
@@ -11097,6 +11138,34 @@ int main(int argc, char** argv) {
std::printf("ERR the K/V cannot grow to the parked conversation: no VRAM is left\n");
return 1;
}
if (borrow) {
const auto t0 = Clock::now();
const strata::core::SavedConversation* donor = conversations.find(parked.serial); // still parked
if (donor == nullptr ||
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
std::printf("ERR restoring a borrowed conversation prefix: %s\n",
err.empty() ? "gone" : err.c_str());
return 1;
}
// the donor's checkpoints on this prompt's path, copied: the donor keeps its own (the one at
// parked.tokens is mounted below, as for any checkpoint resume)
checks.clear();
for (const ConvCheckpoint& c : donor->checkpoints)
if ((int64_t) c.ids.size() <= parked.tokens && starts_with(c.ids, c.imgs)) checks.push_back(c);
for (const ConvCheckpoint& c : checks)
if ((int64_t) c.ids.size() == parked.tokens) { live = c.ids; live_imgs = c.imgs; }
cvec_cached = donor->cvec;
resume = parked.tokens;
from_live = false;
conversations.count_borrow();
std::fprintf(stderr, "strata serve: conversation cache: borrowed %lld tokens from a parked "
"conversation, which stays parked, in %.1f ms; parked=%zu bytes=%zu borrowed=%zu\n",
(long long) resume, std::chrono::duration<double, std::milli>(Clock::now() - t0).count(),
conversations.size(), conversations.bytes(), conversations.borrowed());
}
if (incoming) {
const auto t0 = Clock::now();
if (strata::core::conversation_snapshot_restore(*incoming, ss, g,