import type { VMInstance } from "./vm.js"; const activeVms = new Set(); export function registerVm(vm: VMInstance) { activeVms.add(vm); } export function unregisterVm(vm: VMInstance) { activeVms.delete(vm); } async function cleanupAll() { const vms = Array.from(activeVms); activeVms.clear(); await Promise.allSettled(vms.map((vm) => vm.destroy())); } let registered = false; export function installSignalHandlers() { if (registered) return; registered = true; const handler = async (signal: string) => { process.stderr.write(`\n[fireclaw] Caught ${signal}, cleaning up...\n`); await cleanupAll(); process.exit(signal === "SIGINT" ? 130 : 143); }; process.on("SIGINT", () => handler("SIGINT")); process.on("SIGTERM", () => handler("SIGTERM")); process.on("uncaughtException", async (err) => { process.stderr.write(`[fireclaw] Uncaught exception: ${err.message}\n`); await cleanupAll(); process.exit(1); }); process.on("unhandledRejection", async (reason) => { process.stderr.write( `[fireclaw] Unhandled rejection: ${reason}\n` ); await cleanupAll(); process.exit(1); }); }