@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
62 lines • 2.42 kB
JavaScript
import { envValue } from "../brand.js";
/** `SENPI_MEMORY_REPORT=1` installs the on-demand report; nothing is installed or kept without it. */
export const MEMORY_REPORT_ENV = "SENPI_MEMORY_REPORT";
/** `SENPI_MEMORY_REPORT_SNAPSHOT=1` adds a heap snapshot to each report (itself a large allocation). */
export const MEMORY_REPORT_SNAPSHOT_ENV = "SENPI_MEMORY_REPORT_SNAPSHOT";
export function memoryReportEnabled(env = process.env) {
return envValue("MEMORY_REPORT", env) === "1";
}
export function memoryReportSnapshotEnabled(env = process.env) {
return envValue("MEMORY_REPORT_SNAPSHOT", env) === "1";
}
/** Keys the report owns; an extension reporter cannot shadow them. */
export const RESERVED_MEMORY_REPORT_KEYS = new Set([
"sessionId",
"takenAt",
"pid",
"main",
"kernels",
"residentStore",
"tuiRenderCache",
"heapSnapshot",
"reporterErrors",
]);
// Process-wide, whichever bundle chunk evaluates this module: the RPC handler is loaded lazily and must
// see the sessions the eagerly loaded session runtime registered.
const STATE_KEY = Symbol.for("senpi.memory-report.state");
function state() {
const existing = Reflect.get(globalThis, STATE_KEY);
if (isState(existing))
return existing;
const created = { sessions: new Map(), tuiRenderCache: undefined };
Reflect.set(globalThis, STATE_KEY, created);
return created;
}
function isState(value) {
return typeof value === "object" && value !== null && Reflect.get(value, "sessions") instanceof Map;
}
/** Lists `owner`'s session for reports while the flag is on; the returned function removes it. */
export function registerMemoryReportSession(owner, source) {
if (!memoryReportEnabled())
return () => { };
const { sessions } = state();
sessions.set(owner, source);
return () => {
if (sessions.get(owner) === source)
sessions.delete(owner);
};
}
export function memoryReportSessionFor(owner) {
return state().sessions.get(owner);
}
export function memoryReportSessions() {
return [...state().sessions.values()];
}
/** Called by the terminal UI: only a process that renders tool cards reports their render cache. */
export function registerTuiRenderCacheSource(read) {
state().tuiRenderCache = read;
}
export function tuiRenderCacheTotals() {
return state().tuiRenderCache?.();
}
//# sourceMappingURL=memory-report-registry.js.map