// Benchmark-only preload inherited by the MCP subprocess. Each process records its own OS peak, // including every thread, in a private directory. Neither stdout nor the tool's IPC channel is used. import { writeFileSync } from 'node:fs'; import path from 'node:path'; import { isMainThread } from 'node:worker_threads'; if (isMainThread) { const file = path.join(process.env.PIXELBOLT_BENCH_MEMORY_DIR, process.pid + '.json'); const record = { pid: process.pid, ppid: process.ppid }; // An abnormal exit must leave an incomplete record instead of silently omitting a process. writeFileSync(file, JSON.stringify(record), { flag: 'wx' }); const samples = []; const sample = () => samples.push([performance.timeOrigin + performance.now(), process.memoryUsage.rss()]); sample(); const timer = setInterval(sample, 10).unref(); process.once('exit', () => { clearInterval(timer); sample(); const peakKB = process.resourceUsage().maxRSS; writeFileSync(file, JSON.stringify({ ...record, peakKB, samples })); }); }