@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
72 lines • 2.64 kB
JavaScript
/**
* Least-recently-used store of session summaries bounded by BOTH entry count and
* retained transcript bytes.
*
* An entry ceiling alone does not bound memory: a summary's footprint is
* dominated by `allMessagesText`, which grows with the transcript, so the same
* 4096 entries can hold kilobytes or gigabytes depending on whose sessions they
* are. `Map` preserves insertion order, so re-inserting on every hit makes the
* first key the least recently used one.
*/
export class SessionSummaryLru {
constructor(budget) {
this.entries = new Map();
this.retainedTextBytes = 0;
this.budget = budget;
}
get size() {
return this.entries.size;
}
/** Retained transcript bytes across every cached summary. */
get textBytes() {
return this.retainedTextBytes;
}
/** The cached entry for this key, promoted to most-recently-used. */
get(key) {
const retained = this.entries.get(key);
if (!retained)
return undefined;
this.entries.delete(key);
this.entries.set(key, retained);
return retained.entry;
}
drop(key) {
const retained = this.entries.get(key);
if (!retained)
return;
this.entries.delete(key);
this.retainedTextBytes -= retained.textBytes;
}
/**
* Retain one summary, evicting least-recently-used entries until both ceilings
* hold.
*
* A summary whose own transcript exceeds the byte budget is never retained:
* caching it could only be paid for by evicting every other entry, and it
* would still have to be evicted on the next insert. The caller keeps the
* summary it asked for either way. Any previous entry under this key is
* dropped first, so an oversized re-read can never leave stale bytes behind.
*/
retain(key, entry) {
this.drop(key);
const textBytes = Buffer.byteLength(entry.summary.allMessagesText, "utf8");
if (textBytes > this.budget.maxTextBytes)
return;
this.entries.set(key, { entry, textBytes });
this.retainedTextBytes += textBytes;
this.evictUntilWithinBudget();
}
clear() {
this.entries.clear();
this.retainedTextBytes = 0;
}
evictUntilWithinBudget() {
while (this.entries.size > this.budget.maxEntries || this.retainedTextBytes > this.budget.maxTextBytes) {
const oldest = this.entries.keys().next();
if (oldest.done)
break;
this.drop(oldest.value);
}
}
}
//# sourceMappingURL=session-summary-lru.js.map