@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
68 lines • 2.91 kB
JavaScript
import { readOwnFootprint } from "../process-footprint.js";
import { jscHeapSizeOnBun, readCodemodeKernelListings } from "./kernel-registry-read.js";
import { RESERVED_MEMORY_REPORT_KEYS, tuiRenderCacheTotals, } from "./memory-report-registry.js";
export function buildMemoryReport(source, heapSnapshot) {
const { sections, errors } = reporterSections(source.reporters());
const tuiRenderCache = tuiRenderCacheTotals();
const frameLineBytes = readFrameLineBytes();
const core = {
sessionId: source.sessionId(),
takenAt: new Date().toISOString(),
pid: process.pid,
main: mainThreadMemory(),
kernels: readCodemodeKernelListings(),
residentStore: source.residentStore(),
...(tuiRenderCache === undefined ? {} : { tuiRenderCache }),
...(frameLineBytes === undefined ? {} : { tui: { previousLinesBytes: frameLineBytes } }),
...(heapSnapshot === undefined ? {} : { heapSnapshot }),
...(Object.keys(errors).length === 0 ? {} : { reporterErrors: errors }),
};
return { ...sections, ...core };
}
function mainThreadMemory() {
const usage = process.memoryUsage();
const jscHeapSize = jscHeapSizeOnBun();
return {
...(jscHeapSize === undefined ? {} : { jscHeapSize }),
heapUsed: usage.heapUsed,
external: usage.external,
footprint: readOwnFootprint(),
};
}
// The TUI publishes its frame-line byte total under this process-global key (tui.ts); read structurally
// so the report never imports the renderer into a host that runs headless.
const FRAME_LINE_BYTES_KEY = Symbol.for("senpi.tui.frame-line-bytes");
/** The TUI's frame-line byte total, or \`undefined\` in a process that has not rendered a frame yet. */
function readFrameLineBytes() {
const state = Reflect.get(globalThis, FRAME_LINE_BYTES_KEY);
if (typeof state !== "object" || state === null)
return undefined;
const bytes = Reflect.get(state, "bytes");
return typeof bytes === "number" && Number.isFinite(bytes) ? bytes : undefined;
}
function reporterSections(reporters) {
const sections = {};
const errors = {};
for (const { name, reporter } of reporters) {
if (RESERVED_MEMORY_REPORT_KEYS.has(name) || name in sections || name in errors)
continue;
try {
sections[name] = finiteFigures(reporter());
}
catch (error) {
errors[name] = error instanceof Error ? error.message : String(error);
}
}
return { sections, errors };
}
function finiteFigures(figures) {
if (typeof figures !== "object" || figures === null)
return {};
const kept = {};
for (const [key, value] of Object.entries(figures)) {
if (typeof value === "number" && Number.isFinite(value))
kept[key] = value;
}
return kept;
}
//# sourceMappingURL=memory-report-build.js.map