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

+16
View File
@@ -173,6 +173,16 @@ struct IPrefixCache {
/* Restores the pool could not hold whole (PrefixCache::Stats::restore_cuts). */
virtual int64_t restore_cuts() const { return 0; }
virtual int64_t ckpt_expired() const { return 0; }
/* Entries and snapshots a client released on purpose (release_session). */
virtual int64_t released() const { return 0; }
virtual int64_t ckpt_released() const { return 0; }
/* Forget one stored conversation now, by the client's label or the engine's session id
* (PrefixCache::release_session). RAD_E_UNSUPPORTED from a cache without a session table. */
virtual int release_session(const std::string& /*label*/, uint64_t /*id*/,
int64_t /*keep_tokens*/, SessionRelease* /*out*/) {
return RAD_E_UNSUPPORTED;
}
/* Tokens of context held only by the cache right now. Blocks a running sequence also reads
* are excluded. Separates a working cache from a leak in the pool figures. */
@@ -424,6 +434,12 @@ struct PCBinding final : IPrefixCache {
int64_t expired() const override { return m->stats().expired; }
int64_t restore_cuts() const override { return m->stats().restore_cuts; }
int64_t ckpt_expired() const override { return m->stats().ckpt_expired; }
int64_t released() const override { return m->stats().released; }
int64_t ckpt_released() const override { return m->stats().ckpt_released; }
int release_session(const std::string& label, uint64_t id, int64_t keep_tokens,
SessionRelease* out) override {
return m->release_session(label, id, keep_tokens, out);
}
int64_t held_tokens() const override { return m->unshared_tokens(); }
int64_t next_checkpoint_after(int64_t p) const override { return m->next_checkpoint_after(p); }
+1
View File
@@ -92,6 +92,7 @@ struct SchedMetrics {
* and reservations that went without a snapshot. */
int64_t prefix_ckpt_retired = 0, prefix_ckpt_lru = 0, prefix_ckpt_refused = 0;
int64_t prefix_expired = 0, prefix_ckpt_expired = 0; /* --prefix-cache-ttl */
int64_t prefix_released = 0, prefix_ckpt_released = 0; /* DELETE /sessions */
/* Admissions whose attention hit was cut to nothing: no snapshot at or below it (recurrent
* state or a window snapshot), a windowed trunk group no window snapshot covers, or blocks
* that could not be adopted. `hit_cut_resumed_below` counts those that resumed at a
+36
View File
@@ -385,6 +385,40 @@ int64_t Scheduler::reset_prefix_cache() {
return dropped;
}
int Scheduler::release_session(const std::string& label, uint64_t id, int64_t keep_tokens,
SessionRelease* out) {
std::lock_guard<std::mutex> lk(mu_);
if (!pc_ || !cfg_.prefix_cache) return RAD_E_UNSUPPORTED;
if (label.empty() && id == 0) return RAD_E_INVAL;
for (const auto& p : reqs_) {
if (!p || !p->req) continue;
const Request& r = *p->req;
if (r.state == ReqState::Finished || r.state == ReqState::Failed ||
r.state == ReqState::Cancelled)
continue;
if ((id != 0 && r.id == id) || (!label.empty() && r.session_label == label))
return RAD_E_STATE;
}
const int rc = pc_->release_session(label, id, keep_tokens, out);
if (rc == RAD_OK && out) {
if (label.empty())
RAD_INFO("prefix cache: session %llu released: %lld entries (%lld tokens), %lld "
"snapshots; kept %lld tokens as asked, %lld blocks shared, %lld live",
(unsigned long long)id, (long long)out->entries, (long long)out->tokens,
(long long)out->snapshots, (long long)out->kept_tokens,
(long long)out->kept_shared, (long long)out->kept_live);
else
RAD_INFO("prefix cache: session '%s' released (%lld rows): %lld entries (%lld "
"tokens), %lld snapshots; kept %lld tokens as asked, %lld blocks shared, "
"%lld live",
label.c_str(), (long long)out->sessions, (long long)out->entries,
(long long)out->tokens, (long long)out->snapshots,
(long long)out->kept_tokens, (long long)out->kept_shared,
(long long)out->kept_live);
}
return rc;
}
void Scheduler::trim_prefix_cache() {
if (!pc_ || !cfg_.prefix_cache) return;
const std::vector<int32_t>& cg = pc_->cached_groups();
@@ -2632,6 +2666,8 @@ SchedMetrics Scheduler::metrics_locked() const {
m.prefix_expired = pc_->expired();
m.prefix_ckpt_expired = pc_->ckpt_expired();
m.prefix_restore_cuts = pc_->restore_cuts();
m.prefix_released = pc_->released();
m.prefix_ckpt_released = pc_->ckpt_released();
m.prefix_held_tokens = pc_->held_tokens(); }
m.attn_hit_rate = m.prompt_tokens
? (float)((double)m.attn_cached_tokens / (double)m.prompt_tokens) : 0.0f;
+7
View File
@@ -254,6 +254,13 @@ public:
* cache. Takes the step lock because the step path inserts into the map (live sequences hold
* their own block references). See IScheduler::reset_prefix_cache. */
int64_t reset_prefix_cache();
/* Forget one stored conversation, by label or engine id, keeping its first `keep_tokens`
* (IPrefixCache::release_session). Refused with RAD_E_STATE while a request of that
* conversation is queued or running: its blocks would be skipped as live and its close
* would publish the chain again, so the release would be half done and undone at once.
* Takes the step lock like reset_prefix_cache. */
int release_session(const std::string& label, uint64_t id, int64_t keep_tokens,
SessionRelease* out);
/* ---------------------------------------------------------- the step path */
/* One decision, one batch. Null when there is nothing to run, which is not an error. */