import { appendFile, chmod, mkdir, rename, rm, writeFile } from 'node:fs/promises'; import { Session } from 'node:inspector/promises'; import { homedir } from 'node:os'; import { join } from 'node:path'; import { PerformanceObserver } from 'node:perf_hooks'; import type { PerformanceEntry } from 'node:perf_hooks'; import { arch, platform } from 'node:process'; import { getHeapSpaceStatistics, getHeapStatistics } from 'node:v8'; export const PROCESS_MEMORY_DIAGNOSTICS_DEFAULTS = { sampleIntervalMs: 10_000, captureIntervalMs: 300_000, allocationIntervalBytes: 524_288, } as const; export const PROCESS_MEMORY_DIAGNOSTICS_MINIMUMS = { sampleIntervalMs: 1_000, captureIntervalMs: 10_000, allocationIntervalBytes: 32_768, } as const; const MAX_TIMER_DELAY_MS = 2_147_483_647; const MAX_PENDING_GC_EVENTS = 1_000; export interface ProcessMemoryDiagnosticsConfig { parentDirectory: string; sampleIntervalMs: number; captureIntervalMs: number; allocationIntervalBytes: number; } export interface ProcessMemoryDiagnosticsEnvironment { MASTRACODE_PROFILE?: string; MASTRACODE_PROFILE_DIR?: string; MASTRACODE_PROFILE_SAMPLE_INTERVAL_MS?: string; MASTRACODE_PROFILE_CAPTURE_INTERVAL_MS?: string; MASTRACODE_PROFILE_ALLOCATION_INTERVAL_BYTES?: string; } export interface ProcessMemoryDiagnosticsMemorySample { timestamp: string; sequence: number; elapsedMs: number; memory: ReturnType; resourceUsage: ReturnType; heap: ReturnType; heapSpaces: ReturnType; } export interface ProcessMemoryDiagnosticsStatus { state: 'inactive' | 'starting' | 'active' | 'stopping' | 'error'; outputDirectory: string | null; config: ProcessMemoryDiagnosticsConfig; sampleCount: number; captureCount: number; gcEventCount: number; latestSample: ProcessMemoryDiagnosticsMemorySample | null; latestCapturePath: string | null; error: string | null; } export interface ProcessMemoryDiagnosticsCapture { path: string; sequence: number; timestamp: string; reason: 'manual' | 'periodic' | 'stop'; } interface InspectorSessionAdapter { connect(): void; disconnect(): void; post(method: string, params?: Record): Promise>; } interface PerformanceObserverAdapter { observe(options: Parameters[0]): void; disconnect(): void; } interface ProcessMemoryDiagnosticsDependencies { createInspectorSession?: () => InspectorSessionAdapter; createPerformanceObserver?: (callback: (entries: PerformanceEntry[]) => void) => PerformanceObserverAdapter; now?: () => Date; randomId?: () => string; } export interface ProcessMemoryDiagnosticsSetup { diagnostics: ProcessMemoryDiagnostics; enabled: boolean; error: string | null; } export class ProcessMemoryDiagnosticsConfigError extends Error { constructor(message: string) { super(message); this.name = 'ProcessMemoryDiagnosticsConfigError'; } } function isEnabled(value: string | undefined): boolean { return ['1', 'true', 'yes', 'on'].includes(value?.trim().toLowerCase() ?? ''); } function parseBoundedInteger( env: ProcessMemoryDiagnosticsEnvironment, name: keyof ProcessMemoryDiagnosticsEnvironment, fallback: number, minimum: number, maximum?: number, ): number { const raw = env[name]; if (raw === undefined || raw.trim() === '') return fallback; const value = Number(raw); if (!Number.isSafeInteger(value) || value < minimum || (maximum !== undefined && value > maximum)) { const range = maximum === undefined ? `greater than or equal to ${minimum}` : `between ${minimum} and ${maximum}`; throw new ProcessMemoryDiagnosticsConfigError( `${name} must be an integer ${range}; received ${JSON.stringify(raw)}.`, ); } return value; } function getDefaultProfileParentDirectory(): string { if (process.env.MASTRA_APP_DATA_DIR) return join(process.env.MASTRA_APP_DATA_DIR, 'profiles'); const baseDirectory = platform === 'darwin' ? join(homedir(), 'Library', 'Application Support') : platform === 'win32' ? process.env.APPDATA || join(homedir(), 'AppData', 'Roaming') : process.env.XDG_DATA_HOME || join(homedir(), '.local', 'share'); return join(baseDirectory, 'mastracode', 'profiles'); } export function parseProcessMemoryDiagnosticsEnvironment(env: ProcessMemoryDiagnosticsEnvironment = process.env): { enabled: boolean; config: ProcessMemoryDiagnosticsConfig; } { return { enabled: isEnabled(env.MASTRACODE_PROFILE), config: { parentDirectory: env.MASTRACODE_PROFILE_DIR?.trim() || getDefaultProfileParentDirectory(), sampleIntervalMs: parseBoundedInteger( env, 'MASTRACODE_PROFILE_SAMPLE_INTERVAL_MS', PROCESS_MEMORY_DIAGNOSTICS_DEFAULTS.sampleIntervalMs, PROCESS_MEMORY_DIAGNOSTICS_MINIMUMS.sampleIntervalMs, MAX_TIMER_DELAY_MS, ), captureIntervalMs: parseBoundedInteger( env, 'MASTRACODE_PROFILE_CAPTURE_INTERVAL_MS', PROCESS_MEMORY_DIAGNOSTICS_DEFAULTS.captureIntervalMs, PROCESS_MEMORY_DIAGNOSTICS_MINIMUMS.captureIntervalMs, MAX_TIMER_DELAY_MS, ), allocationIntervalBytes: parseBoundedInteger( env, 'MASTRACODE_PROFILE_ALLOCATION_INTERVAL_BYTES', PROCESS_MEMORY_DIAGNOSTICS_DEFAULTS.allocationIntervalBytes, PROCESS_MEMORY_DIAGNOSTICS_MINIMUMS.allocationIntervalBytes, ), }, }; } export function createProcessMemoryDiagnosticsFromEnvironment( env: ProcessMemoryDiagnosticsEnvironment = process.env, dependencies: ProcessMemoryDiagnosticsDependencies = {}, ): ProcessMemoryDiagnosticsSetup { try { const { enabled, config } = parseProcessMemoryDiagnosticsEnvironment(env); return { diagnostics: new ProcessMemoryDiagnostics(config, dependencies), enabled, error: null }; } catch (error) { const config: ProcessMemoryDiagnosticsConfig = { parentDirectory: env.MASTRACODE_PROFILE_DIR?.trim() || getDefaultProfileParentDirectory(), ...PROCESS_MEMORY_DIAGNOSTICS_DEFAULTS, }; const message = error instanceof Error ? error.message : String(error); return { diagnostics: new ProcessMemoryDiagnostics(config, dependencies, message), enabled: isEnabled(env.MASTRACODE_PROFILE), error: message, }; } } export async function startConfiguredProcessMemoryDiagnostics( setup: ProcessMemoryDiagnosticsSetup, warn: (message: string) => void, ): Promise { if (!setup.enabled) return setup.diagnostics; if (setup.error) { warn(`Process memory diagnostics were not started: ${setup.error}`); return setup.diagnostics; } const status = await setup.diagnostics.start(); if (status.state !== 'active') { warn(`Process memory diagnostics were not started: ${status.error ?? 'unknown inspector error'}`); } return setup.diagnostics; } export async function stopProcessMemoryDiagnosticsWithTimeout( diagnostics: ProcessMemoryDiagnostics, warn: (message: string) => void, timeoutMs = 5_000, ): Promise { let timeout: ReturnType | undefined; const timedOut = await Promise.race([ diagnostics.stop().then( () => false, error => { warn(`Process memory diagnostics did not stop cleanly: ${errorMessage(error)}`); return false; }, ), new Promise(resolve => { timeout = setTimeout(() => resolve(true), timeoutMs); timeout.unref(); }), ]); if (timeout) clearTimeout(timeout); if (timedOut) warn(`Process memory diagnostics did not stop within ${timeoutMs}ms; final artifacts may be incomplete.`); } function defaultInspectorSession(): InspectorSessionAdapter { const session = new Session(); return { connect: () => session.connect(), disconnect: () => session.disconnect(), post: async (method, params) => (await session.post( method as Parameters[0], params as Parameters[1], )) as unknown as Record, }; } function defaultPerformanceObserver(callback: (entries: PerformanceEntry[]) => void) { const observer = new PerformanceObserver(list => callback(list.getEntries())); return observer; } function errorMessage(error: unknown): string { return error instanceof Error ? error.message : String(error); } function safeTimestamp(date: Date): string { return date.toISOString().replaceAll(':', '-'); } function unrefTimer(timer: ReturnType): void { timer.unref?.(); } export class ProcessMemoryDiagnostics { readonly config: ProcessMemoryDiagnosticsConfig; private state: ProcessMemoryDiagnosticsStatus['state'] = 'inactive'; private outputDirectory: string | null = null; private inspector: InspectorSessionAdapter | null = null; private observer: PerformanceObserverAdapter | null = null; private sampleTimer: ReturnType | null = null; private captureTimer: ReturnType | null = null; private startedAt: Date | null = null; private sampleCount = 0; private captureCount = 0; private gcEventCount = 0; private latestSample: ProcessMemoryDiagnosticsMemorySample | null = null; private latestCapturePath: string | null = null; private latestError: string | null; private samplingActive = false; private stopRequested = false; private startingPromise: Promise | null = null; private stoppingPromise: Promise | null = null; private restartAfterStopPromise: Promise | null = null; private restartRequested = false; private pendingGcEvents: Array> = []; private gcEventBufferOverflowed = false; private artifactWriteFailed = false; private captureQueue: Promise = Promise.resolve(); private writeQueue: Promise = Promise.resolve(); private readonly configError: string | null; private readonly createInspectorSession: () => InspectorSessionAdapter; private readonly createPerformanceObserver: NonNullable< ProcessMemoryDiagnosticsDependencies['createPerformanceObserver'] >; private readonly now: () => Date; private readonly randomId: () => string; constructor( config: ProcessMemoryDiagnosticsConfig, dependencies: ProcessMemoryDiagnosticsDependencies = {}, initialError: string | null = null, ) { this.config = { ...config }; this.configError = initialError; this.latestError = initialError; this.createInspectorSession = dependencies.createInspectorSession ?? defaultInspectorSession; this.createPerformanceObserver = dependencies.createPerformanceObserver ?? defaultPerformanceObserver; this.now = dependencies.now ?? (() => new Date()); this.randomId = dependencies.randomId ?? (() => globalThis.crypto.randomUUID()); } getStatus(): ProcessMemoryDiagnosticsStatus { return { state: this.state, outputDirectory: this.outputDirectory, config: { ...this.config }, sampleCount: this.sampleCount, captureCount: this.captureCount, gcEventCount: this.gcEventCount, latestSample: this.latestSample, latestCapturePath: this.latestCapturePath, error: this.latestError, }; } start(): Promise { if (this.state === 'active') return Promise.resolve(this.getStatus()); if (this.state === 'starting' && this.startingPromise) return this.startingPromise; if (this.state === 'stopping' && this.stoppingPromise) { this.restartRequested = true; this.restartAfterStopPromise ??= this.stoppingPromise.then(() => { this.restartAfterStopPromise = null; if (!this.restartRequested) return this.getStatus(); this.restartRequested = false; return this.start(); }); return this.restartAfterStopPromise; } if (this.configError) { this.latestError = this.configError; this.state = 'error'; return Promise.resolve(this.getStatus()); } this.state = 'starting'; this.stoppingPromise = null; this.restartRequested = false; this.stopRequested = false; this.outputDirectory = null; this.sampleCount = 0; this.captureCount = 0; this.gcEventCount = 0; this.pendingGcEvents = []; this.gcEventBufferOverflowed = false; this.artifactWriteFailed = false; this.latestSample = null; this.latestCapturePath = null; this.latestError = null; this.startedAt = this.now(); const startingPromise = this.startRun(); this.startingPromise = startingPromise; void startingPromise.finally(() => { if (this.startingPromise === startingPromise) this.startingPromise = null; }); return startingPromise; } private async startRun(): Promise { try { await this.createArtifacts(); if (await this.abortStartIfRequested()) return this.getStatus(); this.observeGc(); this.inspector = this.createInspectorSession(); this.inspector.connect(); await this.startSampling(); if (await this.abortStartIfRequested()) return this.getStatus(); await this.takeSample(); if (await this.abortStartIfRequested()) return this.getStatus(); this.sampleTimer = setInterval(() => { void this.takeSample().catch(error => this.recordError('Process sample failed', error)); }, this.config.sampleIntervalMs); unrefTimer(this.sampleTimer); this.captureTimer = setInterval(() => { void this.capture('periodic').catch(error => this.recordError('Periodic allocation capture failed', error)); }, this.config.captureIntervalMs); unrefTimer(this.captureTimer); this.state = 'active'; return this.getStatus(); } catch (error) { this.latestError = `Unable to start process memory diagnostics: ${errorMessage(error)}`; await this.cleanupAfterStartFailure(); this.state = 'error'; return this.getStatus(); } } private async abortStartIfRequested(): Promise { if (!this.stopRequested) return false; await this.cleanupAfterStartFailure(); this.state = 'inactive'; return true; } capture(reason: 'manual' | 'periodic' = 'manual'): Promise { if (this.state !== 'active') { return Promise.reject(new Error(this.latestError ?? 'Process memory diagnostics are not active.')); } return this.enqueueCapture(() => this.captureEpoch(reason, false)); } async stop(): Promise { if (this.stoppingPromise) { this.restartRequested = false; return this.stoppingPromise; } if (this.state === 'inactive') return this.getStatus(); if (this.state === 'error' && !this.inspector && !this.outputDirectory) return this.getStatus(); const startingPromise = this.state === 'starting' ? this.startingPromise : null; this.stopRequested = true; this.state = 'stopping'; this.clearTimersAndObserver(); if (startingPromise) { this.stoppingPromise = startingPromise.then(() => this.getStatus()); return this.stoppingPromise; } this.stoppingPromise = (async () => { let stopError: string | null = null; try { if (this.outputDirectory) await this.takeSample(); await this.enqueueCapture(async () => { if (this.inspector && this.samplingActive) await this.captureEpoch('stop', true); }); await this.writeQueue; if (this.gcEventBufferOverflowed) { throw new Error(`GC event buffer exceeded ${MAX_PENDING_GC_EVENTS} records before the next process sample.`); } if (this.artifactWriteFailed) throw new Error('One or more process memory diagnostic artifact writes failed.'); this.latestError = null; } catch (error) { stopError = `Unable to stop process memory diagnostics cleanly: ${errorMessage(error)}`; this.latestError = stopError; } finally { this.disconnectInspector(); this.state = stopError ? 'error' : 'inactive'; } return this.getStatus(); })(); return this.stoppingPromise; } private async createArtifacts(): Promise { await mkdir(this.config.parentDirectory, { recursive: true, mode: 0o700 }); await chmod(this.config.parentDirectory, 0o700); const runName = `run-${safeTimestamp(this.startedAt!)}-${process.pid}-${this.randomId() .replaceAll(/[^a-zA-Z0-9-]/g, '') .slice(0, 12)}`; this.outputDirectory = join(this.config.parentDirectory, runName); await mkdir(this.outputDirectory, { mode: 0o700 }); await chmod(this.outputDirectory, 0o700); const metadata = { schemaVersion: 1, runId: runName, startedAt: this.startedAt!.toISOString(), pid: process.pid, nodeVersion: process.version, platform, arch, config: this.config, }; await writeFile(join(this.outputDirectory, 'metadata.json'), `${JSON.stringify(metadata, null, 2)}\n`, { flag: 'wx', mode: 0o600, }); await writeFile(join(this.outputDirectory, 'process-samples.jsonl'), '', { flag: 'wx', mode: 0o600 }); await writeFile(join(this.outputDirectory, 'gc-events.jsonl'), '', { flag: 'wx', mode: 0o600 }); } private observeGc(): void { const observer = this.createPerformanceObserver(entries => { const before = this.latestSample?.memory ?? null; const after = process.memoryUsage(); const timestamp = this.now().toISOString(); for (const entry of entries) { const gcEntry = entry as PerformanceEntry & { kind?: number; flags?: number; detail?: { kind?: number; flags?: number }; }; this.gcEventCount += 1; if (this.pendingGcEvents.length >= MAX_PENDING_GC_EVENTS) { this.gcEventBufferOverflowed = true; this.latestError = `GC event buffer exceeded ${MAX_PENDING_GC_EVENTS} records before the next process sample.`; continue; } this.pendingGcEvents.push({ timestamp, sequence: this.gcEventCount, name: entry.name, startTime: entry.startTime, duration: entry.duration, kind: gcEntry.detail?.kind ?? gcEntry.kind ?? null, flags: gcEntry.detail?.flags ?? gcEntry.flags ?? null, before, after, latestSampleSequence: this.latestSample?.sequence ?? null, }); } }); observer.observe({ entryTypes: ['gc'] }); this.observer = observer; } private async startSampling(): Promise { if (!this.inspector) throw new Error('Inspector session is unavailable.'); await this.inspector.post('HeapProfiler.startSampling', { samplingInterval: this.config.allocationIntervalBytes, includeObjectsCollectedByMajorGC: true, includeObjectsCollectedByMinorGC: true, }); this.samplingActive = true; } private async takeSample(): Promise { if (!this.outputDirectory || !this.startedAt) throw new Error('Diagnostics output directory is unavailable.'); const sample: ProcessMemoryDiagnosticsMemorySample = { timestamp: this.now().toISOString(), sequence: this.sampleCount + 1, elapsedMs: Math.max(0, this.now().getTime() - this.startedAt.getTime()), memory: process.memoryUsage(), resourceUsage: process.resourceUsage(), heap: getHeapStatistics(), heapSpaces: getHeapSpaceStatistics(), }; this.sampleCount = sample.sequence; this.latestSample = sample; await this.enqueueWrite('process-samples.jsonl', sample); const gcEvents = this.pendingGcEvents.splice(0); if (gcEvents.length > 0) await this.enqueueWriteBatch('gc-events.jsonl', gcEvents); return sample; } private enqueueWrite(fileName: string, value: unknown): Promise { return this.enqueueWriteBatch(fileName, [value]); } private enqueueWriteBatch(fileName: string, values: unknown[]): Promise { const operation = this.writeQueue.then(async () => { if (!this.outputDirectory || values.length === 0) return; const lines = values.map(value => JSON.stringify(value)).join('\n'); try { await appendFile(join(this.outputDirectory, fileName), `${lines}\n`, { mode: 0o600 }); } catch (error) { this.artifactWriteFailed = true; throw error; } }); this.writeQueue = operation.catch(() => undefined); return operation; } private enqueueCapture(operation: () => Promise): Promise { const result = this.captureQueue.then(operation); this.captureQueue = result.catch(() => undefined); return result; } private async captureEpoch( reason: ProcessMemoryDiagnosticsCapture['reason'], final: boolean, ): Promise { if (!this.inspector || !this.samplingActive || !this.outputDirectory) { throw new Error(this.latestError ?? 'Allocation sampling is not active.'); } let response: Record; try { response = await this.inspector.post('HeapProfiler.stopSampling'); this.samplingActive = false; } catch (error) { this.latestError = `Allocation capture failed: ${errorMessage(error)}`; if (!final && !this.stopRequested) await this.restartSamplingAfterFailure(); throw new Error(this.latestError, { cause: error }); } const profile = response.profile; if (!profile || typeof profile !== 'object') { this.latestError = 'Allocation capture failed: inspector returned no profile.'; if (!final && !this.stopRequested) await this.restartSamplingAfterFailure(); throw new Error(this.latestError); } const sequence = this.captureCount + 1; const timestamp = this.now().toISOString(); const filename = `allocation-${String(sequence).padStart(6, '0')}-${safeTimestamp(new Date(timestamp))}.heapprofile`; const finalPath = join(this.outputDirectory, filename); const temporaryPath = join(this.outputDirectory, `.${filename}.${this.randomId()}.tmp`); try { await writeFile(temporaryPath, `${JSON.stringify(profile)}\n`, { flag: 'wx', mode: 0o600 }); await chmod(temporaryPath, 0o600); await rename(temporaryPath, finalPath); await chmod(finalPath, 0o600); this.captureCount = sequence; this.latestCapturePath = finalPath; this.latestError = null; } catch (error) { this.artifactWriteFailed = true; this.latestError = `Unable to persist allocation capture: ${errorMessage(error)}`; throw new Error(this.latestError, { cause: error }); } finally { if (!final && !this.stopRequested && this.state === 'active') { try { await this.startSampling(); } catch (error) { this.latestError = `Unable to restart allocation sampling: ${errorMessage(error)}`; this.state = 'error'; } } } if (!final && !this.stopRequested && !this.samplingActive) { throw new Error(this.latestError ?? 'Allocation sampling did not restart.'); } return { path: finalPath, sequence, timestamp, reason }; } private async restartSamplingAfterFailure(): Promise { try { await this.startSampling(); } catch (error) { this.latestError = `Unable to restart allocation sampling: ${errorMessage(error)}`; this.state = 'error'; } } private recordError(prefix: string, error: unknown): void { this.latestError = `${prefix}: ${errorMessage(error)}`; } private clearTimersAndObserver(): void { if (this.sampleTimer) clearInterval(this.sampleTimer); if (this.captureTimer) clearInterval(this.captureTimer); this.sampleTimer = null; this.captureTimer = null; this.observer?.disconnect(); this.observer = null; } private disconnectInspector(): void { this.samplingActive = false; if (!this.inspector) return; try { this.inspector.disconnect(); } catch { // Best-effort cleanup after an inspector failure. } this.inspector = null; } private async cleanupAfterStartFailure(): Promise { this.clearTimersAndObserver(); this.disconnectInspector(); await this.writeQueue; try { if (this.outputDirectory) await rm(this.outputDirectory, { recursive: true, force: true }); } catch (error) { this.latestError = `${this.latestError} Failed to remove partial artifacts: ${errorMessage(error)}`; } finally { this.outputDirectory = null; } } }