@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
75 lines • 2.6 kB
JavaScript
/** Lines are written out in chunks of this many, so a burst never sits whole in memory. */
const SPILL_CHUNK_LINES = 256;
export class OverflowBook {
#spill;
#entries = new Map();
constructor(spill) {
this.#spill = spill;
}
get(id) {
return this.#entries.get(id);
}
count(id) {
return this.#entries.get(id)?.count ?? 0;
}
ids() {
return [...this.#entries.keys()];
}
record(event, body) {
const overflow = this.#entries.get(event.id) ?? {
id: event.id,
description: event.description,
count: 0,
kinds: new Set(),
humanInput: false,
pending: [],
spilled: 0,
path: undefined,
rotated: false,
};
overflow.count += 1;
overflow.kinds.add(event.type);
if (event.type === "line" && event.humanInput === true)
overflow.humanInput = true;
this.#entries.set(event.id, overflow);
if (this.#spill === undefined)
return;
overflow.pending.push(body);
if (overflow.pending.length >= SPILL_CHUNK_LINES)
this.flush(event.id);
}
flush(id) {
const overflow = this.#entries.get(id);
if (overflow === undefined || overflow.pending.length === 0 || this.#spill === undefined)
return;
const result = this.#spill(id, overflow.pending);
if (result !== undefined) {
overflow.spilled += overflow.pending.length;
overflow.path = result.path;
overflow.rotated ||= result.rotated;
}
overflow.pending = [];
}
/** The notice a wake carries for this monitor's overflow; "" when nothing overflowed. */
notice(id) {
const overflow = this.#entries.get(id);
if (overflow === undefined || overflow.count === 0)
return "";
const notices = [];
if (overflow.spilled > 0 && overflow.path !== undefined) {
const rotated = overflow.rotated ? `; earlier ones in ${overflow.path}.1` : "";
notices.push(`[${overflow.spilled} more lines in ${overflow.path}${rotated}]`);
}
const unspilled = overflow.count - overflow.spilled;
if (unspilled > 0)
notices.push(`[${unspilled} additional event lines omitted; peek bash_output for full history.]`);
return notices.join("\n");
}
delete(id) {
this.#entries.delete(id);
}
clear() {
this.#entries.clear();
}
}
//# sourceMappingURL=monitor-overflow.js.map