server: DELETE /sessions forgets one stored conversation

A client that deleted a conversation, or compacted it past a prefix, knows the
rest is dead before --prefix-cache-ttl does. DELETE /sessions/<label> (or
?label=, ?id=<engine id>) walks the session's chain from the tip inwards like
expiry: VRAM blocks to the pool, the host copy with them, the session's
snapshots to the store. ?keep_tokens=N keeps the chain's start in whole blocks.
Blocks another stored session shares (the system prompt) and blocks a live
request reads stay and are counted apart; the row is cut at the deepest block
that stayed, so a later release reaches it. A request of the same conversation
in flight is 409. IScheduler::release_session beside reset_prefix_cache,
PrefixCache::release_session over the tiers' session table (find_sessions,
shared_prefix_blocks, cut_session). Counters released{entries,snapshots} on
/sessions and /stats, prefix_cache_released_total{what} on /metrics.
This commit is contained in:
Nunze committed 2026-10-22 09:58:08 +00:00
1 parent 123c439684
commit 3aded1bbf8
20 files changed
+722 -2

No files matched your search

+182
View File
@@ -3234,6 +3234,188 @@ TEST(the_tiers_are_told_the_cache_s_block_ids_and_not_the_requests) {
}
}
/* ================================================================== release on request
*
* A client that deleted a conversation, or kept a prefix of it and replaced the rest, forgets it
* through the session table: by label, from the tip inwards, host copy and snapshots with it.
* What another conversation also walks stays, as does what a live sequence holds.
*/
/* Publishes `tokens` under `id` with a session label, as the scheduler does at a turn's close,
* and frees the sequence, as it does right after. */
static void publish_turn(Fixture& f, uint64_t id, const std::vector<int32_t>& tokens,
const std::string& label) {
CHECK_OK(f.kv.add_sequence(id));
CHECK_OK(f.kv.ensure(id, (int64_t)tokens.size()));
std::vector<std::vector<int32_t>> bt;
for (int32_t g : f.pc.cached_groups()) bt.push_back(f.kv.block_table(id, g));
CHECK_OK(f.pc.insert(id, tokens, {}, bt, (int64_t)tokens.size(), label));
f.kv.free_sequence(id);
}
/* The label's rows in the session table. */
static std::vector<IdleTiers::SessionInfo> rows_of(IdleTiers& t, const std::string& label) {
std::vector<IdleTiers::SessionInfo> out;
for (const IdleTiers::SessionInfo& s : t.sessions())
if (s.label == label) out.push_back(s);
return out;
}
TEST(a_released_conversation_goes_from_the_tip_down_to_what_another_one_shares) {
Fixture f;
CHECK_OK(f.build(/*with_linear=*/false, /*ckpt_slots=*/0));
IdleTiers t;
CHECK_OK(t.configure(tier_config(1 << 20, 1024)));
f.pc.set_tiers(&t);
/* Conversation A over three turns under three request ids: each turn extends the last. The
* first two blocks are a system prompt conversation B opens with too. */
const auto a1 = tok_run(100, 64); /* 4 blocks */
auto a2 = a1; for (int i = 0; i < 32; ++i) a2.push_back(700 + i); /* 6 */
auto a3 = a2; for (int i = 0; i < 32; ++i) a3.push_back(800 + i); /* 8 */
publish_turn(f, 1, a1, "chat-a");
publish_turn(f, 2, a2, "chat-a");
publish_turn(f, 3, a3, "chat-a");
auto b = tok_run(100, 32); for (int i = 0; i < 64; ++i) b.push_back(900 + i); /* 6 */
publish_turn(f, 4, b, "chat-b");
CHECK_EQ(f.pc.size(), 12ll); /* 8 + 4 own blocks of B */
REQUIRE_EQ(rows_of(t, "chat-a").size(), 1u); /* one row per conversation */
CHECK_EQ(rows_of(t, "chat-a")[0].n_tokens, 128ll);
/* Every entry has its host copy. */
copy_now(t);
CHECK_EQ(t.arena().used_slots(), 12);
const int64_t pool_before = f.kv.free_blocks(G_FULL);
SessionRelease r;
CHECK_EQ(f.pc.release_session("chat-c", 0, 0, &r), RAD_E_NOTFOUND);
CHECK_EQ(f.pc.release_session("", 0, 0, &r), RAD_E_INVAL);
CHECK_OK(f.pc.release_session("chat-a", 0, 0, &r));
CHECK_EQ(r.sessions, 1ll);
CHECK_EQ(r.entries, 6ll); /* 8 less the 2 B walks too */
CHECK_EQ(r.tokens, 96ll);
CHECK_EQ(r.kept_shared, 2ll);
CHECK_EQ(r.kept_live, 0ll);
CHECK_EQ(r.kept_tokens, 0ll);
CHECK(r.device_bytes > 0);
CHECK_EQ(r.host_bytes, 6 * 1024ll);
/* Pool and arena got the six back; the index holds B's chain and the shared start. */
CHECK_EQ(f.kv.free_blocks(G_FULL), pool_before + 6);
CHECK_EQ(t.arena().used_slots(), 6);
CHECK_EQ(f.pc.size(), 6ll);
CHECK_EQ(f.pc.stats().released, 6ll);
/* A's next turn hits the shared system prompt and nothing more; B is whole. */
CHECK_EQ(f.pc.lookup(a3, {}).n_attn_tokens, 32);
CHECK_EQ(f.pc.lookup(b, {}).n_attn_tokens, 80);
CHECK_EQ(rows_of(t, "chat-a").size(), 0u);
REQUIRE_EQ(rows_of(t, "chat-b").size(), 1u);
CHECK_EQ(rows_of(t, "chat-b")[0].n_tokens, 96ll);
/* Released twice is not found the second time. */
CHECK_EQ(f.pc.release_session("chat-a", 0, 0, &r), RAD_E_NOTFOUND);
}
TEST(keep_tokens_leaves_the_chains_start_standing_in_whole_blocks) {
Fixture f;
CHECK_OK(f.build(/*with_linear=*/false, /*ckpt_slots=*/0));
IdleTiers t;
CHECK_OK(t.configure(tier_config(1 << 20, 1024)));
f.pc.set_tiers(&t);
auto b = tok_run(100, 32); for (int i = 0; i < 64; ++i) b.push_back(900 + i); /* 6 blocks */
publish_turn(f, 4, b, "chat-b");
/* 40 tokens keep three blocks: the one holding token 40 goes with them. */
SessionRelease r;
CHECK_OK(f.pc.release_session("chat-b", 0, 40, &r));
CHECK_EQ(r.entries, 3ll);
CHECK_EQ(r.kept_tokens, 48ll);
CHECK_EQ(r.kept_shared, 0ll);
CHECK_EQ(f.pc.lookup(b, {}).n_attn_tokens, 48);
/* The row stays, cut to the kept prefix: the client extends it again. */
REQUIRE_EQ(rows_of(t, "chat-b").size(), 1u);
CHECK_EQ(rows_of(t, "chat-b")[0].n_tokens, 48ll);
const uint64_t id = rows_of(t, "chat-b")[0].id;
/* The same by engine id, keeping more than is left: nothing goes, nothing breaks. */
CHECK_OK(f.pc.release_session("", id, 200, &r));
CHECK_EQ(r.entries, 0ll);
CHECK_EQ(r.kept_tokens, 48ll);
CHECK_EQ(f.pc.lookup(b, {}).n_attn_tokens, 48);
/* A later turn extends the stub into a full row again. */
publish_turn(f, 5, b, "chat-b");
REQUIRE_EQ(rows_of(t, "chat-b").size(), 1u);
CHECK_EQ(rows_of(t, "chat-b")[0].n_tokens, 96ll);
CHECK_EQ(f.pc.lookup(b, {}).n_attn_tokens, 80);
}
TEST(a_release_takes_the_sessions_snapshots_past_the_kept_prefix_and_leaves_live_blocks) {
Fixture f;
CHECK_OK(f.build(/*with_linear=*/true, /*ckpt_slots=*/4));
IdleTiers t;
IdleTiers::Config tc = tier_config(1 << 20, 1024);
tc.ckpt_bytes = f.kv.checkpoint_bytes();
tc.ckpt_slots = 4;
CHECK_OK(t.configure(tc));
f.pc.set_tiers(&t);
const auto a = tok_run(100, 128); /* 8 blocks, checkpoints at 64, 128 */
const std::vector<BlockHash> hs = chain_of(a);
CHECK_OK(f.kv.add_sequence(3));
CHECK_OK(f.kv.ensure(3, 128));
ckpt_at(f, 3, hs, 64);
ckpt_at(f, 3, hs, 128);
f.pc.begin_step();
{
std::vector<std::vector<int32_t>> bt;
for (int32_t g : f.pc.cached_groups()) bt.push_back(f.kv.block_table(3, g));
CHECK_OK(f.pc.insert(3, a, {}, bt, 128, "chat-a"));
}
f.kv.free_sequence(3);
CHECK_EQ(f.kv.free_checkpoint_slots(), 2ll);
CHECK_EQ(rows_of(t, "chat-a")[0].snapshots, 2ll);
/* A waiting request restored the chain and holds it; a running one adopted the first
* five blocks. Both keep their blocks through a release. */
std::vector<std::vector<int32_t>> found;
const CacheHit h = f.pc.lookup(a, {}, &found);
CHECK_EQ(h.n_attn_tokens, 112);
CHECK_OK(f.kv.add_sequence(9));
std::vector<int32_t> five(found[0].begin(), found[0].begin() + 5);
CHECK_OK(f.kv.adopt(9, f.pc.cached_groups()[0], five, 80));
f.pc.begin_step();
SessionRelease r;
CHECK_OK(f.pc.release_session("chat-a", 0, 64, &r));
/* Blocks 4..7 are past the kept 64 tokens; block 4 is live, 5..7 go. */
CHECK_EQ(r.entries, 3ll);
CHECK_EQ(r.kept_live, 1ll);
CHECK_EQ(r.kept_tokens, 64ll);
/* The snapshot at 128 sat on block 7 and went with it; the one at 64 is on the kept prefix. */
CHECK_EQ(r.snapshots, 1ll);
CHECK_EQ(f.kv.free_checkpoint_slots(), 3ll);
const std::vector<int64_t> left = f.pc.checkpoints(3);
REQUIRE_EQ(left.size(), 1u);
CHECK_EQ(left[0], 64ll);
CHECK_EQ(f.pc.stats().ckpt_released, 1ll);
/* The live block is still indexed and the sequence still reads it; the row reaches it. */
CHECK_EQ(f.kv.block_refcount(G_FULL, five[4]), 2);
REQUIRE_EQ(rows_of(t, "chat-a").size(), 1u);
CHECK_EQ(rows_of(t, "chat-a")[0].n_tokens, 80ll);
CHECK_EQ(f.pc.lookup(a, {}).n_attn_tokens, 80);
CHECK_EQ(f.pc.lookup(a, {}).n_linear_tokens, 64);
/* The sequence is gone: the block it held goes on the next release. */
f.kv.free_sequence(9);
CHECK_OK(f.pc.release_session("chat-a", 0, 64, &r));
CHECK_EQ(r.entries, 1ll);
CHECK_EQ(r.kept_live, 0ll);
CHECK_EQ(f.pc.lookup(a, {}).n_attn_tokens, 64);
CHECK_EQ(f.pc.lookup(a, {}).n_linear_tokens, 64);
CHECK_EQ(rows_of(t, "chat-a")[0].n_tokens, 64ll);
}
/* ================================================================== the pool in bytes
*
* Blocks and tokens cannot be summed across KV groups: a recurrent group has no token axis and