@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
106 lines • 4.77 kB
JavaScript
import { readCodemodeKernelRows, readMainThreadHeapBytes } from "../../core/memory-report/kernel-registry-read.js";
import { readOwnFootprint } from "../../core/process-footprint.js";
/** Environment override for the memory warning threshold (compared with the footprint), in megabytes. */
export const HOST_RSS_WARN_MB_ENV = "SENPI_RPC_HOST_RSS_WARN_MB";
export const DEFAULT_HOST_RSS_WARN_MB = 4096;
/** Sampling interval. Memory moves slowly; this is bookkeeping, not a hot loop. */
export const HOST_MEMORY_SAMPLE_MS = 30_000;
/** One stderr line per window, however many samples stay above the threshold. */
export const HOST_MEMORY_STDERR_INTERVAL_MS = 5 * 60_000;
const BYTES_PER_MEGABYTE = 1024 * 1024;
function parsePositiveInteger(value) {
if (value === undefined || !/^\d+$/.test(value.trim()))
return undefined;
const parsed = Number(value.trim());
return Number.isFinite(parsed) && parsed > 0 ? parsed : undefined;
}
/**
* Memory-footprint reporter for the shared host.
*
* The daemon's capacity is memory, never an occupancy cap: it never counts sessions and
* never kills one. What it does is SAY how much memory it holds - as a lifecycle record to
* every connection and one stderr line per five minutes - and, while it is above the
* warning threshold, tell the router to park idle sessions at half the usual window so
* their memory returns to the process sooner. It never refuses a session: a host with
* no resource caps admits every open whatever it holds (#2207).
*/
export class HostMemorySampler {
constructor(options) {
this.pressure = false;
this.idleReported = false;
const env = options.env ?? process.env;
this.emit = options.emit;
this.sessions = options.sessions;
this.onPressure = options.onPressure;
if (options.onIdlePressure)
this.onIdlePressure = options.onIdlePressure;
this.log = options.log ?? ((message) => void process.stderr.write(message));
this.now = options.now ?? Date.now;
this.readFootprint = options.readFootprint ?? readOwnFootprint;
this.readRssBytes = options.readRssBytes ?? (() => process.memoryUsage.rss());
this.readKernels = options.readKernels ?? readCodemodeKernelRows;
this.readMainHeap = options.readMainHeap ?? readMainThreadHeapBytes;
this.warnMb = parsePositiveInteger(env[HOST_RSS_WARN_MB_ENV]) ?? DEFAULT_HOST_RSS_WARN_MB;
}
start() {
if (this.timer !== undefined)
return;
// Unref'd: memory bookkeeping must never be the reason the host stays alive.
this.timer = setInterval(() => this.sample(), HOST_MEMORY_SAMPLE_MS);
this.timer.unref?.();
}
stop() {
if (this.timer === undefined)
return;
clearInterval(this.timer);
this.timer = undefined;
}
/** One sample. Public so tests drive it on an injected clock and memory readings. */
sample() {
const footprint = this.readFootprint();
const reading = {
footprintMb: Math.round(footprint.bytes / BYTES_PER_MEGABYTE),
measure: footprint.measure,
rssMb: Math.round(this.readRssBytes() / BYTES_PER_MEGABYTE),
main: { heapBytes: this.readMainHeap() },
kernels: this.readKernels(),
};
if (reading.footprintMb <= this.warnMb) {
this.idleReported = false;
if (!this.pressure)
return;
this.pressure = false;
this.onPressure(false);
return;
}
if (!this.pressure) {
this.pressure = true;
this.onPressure(true);
}
const sessions = this.sessions();
// A session arriving ends the episode: the next empty sample above the threshold is a new
// observation, not a repetition of this one.
if (sessions > 0)
this.idleReported = false;
else if (!this.idleReported) {
this.idleReported = true;
this.onIdlePressure?.(reading);
}
const { footprintMb, measure, rssMb } = reading;
this.emit({
type: "host_memory_pressure",
rssMb,
footprintMb,
measure,
sessions,
main: reading.main,
kernels: reading.kernels,
});
const now = this.now();
if (this.lastLoggedAt !== undefined && now - this.lastLoggedAt < HOST_MEMORY_STDERR_INTERVAL_MS)
return;
this.lastLoggedAt = now;
this.log(`senpi rpc host memory pressure: footprintMb=${footprintMb} (${measure}) rssMb=${rssMb} sessions=${sessions} (idle parking halved)\n`);
}
}
//# sourceMappingURL=host-memory-sampler.js.map