import { ChildProcess } from "node:child_process"; /** * Tiered exit strategy for child processes: * 1. Send SIGTERM and wait for a grace period * 2. Send SIGKILL if still running after the grace period * * NOTE: This is forward-looking scaffolding for when the codebase * manages async child processes directly. Currently process lifecycle * is managed through pi-coding-agent internals. */ export async function terminateWithGrace( child: ChildProcess, graceMs: number = 1000 ): Promise { if (child.killed || child.exitCode !== null) return; // 1. Send SIGTERM child.kill("SIGTERM"); // 2. Wait for grace period const exitPromise = new Promise((resolve) => { child.on("exit", () => resolve()); }); const timeoutPromise = new Promise((resolve) => { setTimeout(resolve, graceMs); }); await Promise.race([exitPromise, timeoutPromise]); // 3. If timeout won, send SIGKILL if (child.exitCode === null && !child.killed) { child.kill("SIGKILL"); } }