UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

61 lines • 2.9 kB
import { rm, writeFile } from "node:fs/promises"; import { generationPaths, HOST_STATE_FILE_MODE } from "./host-daemon-paths.js"; import { releaseGeneration } from "./host-daemon-registration.js"; import { announceStop, recordEscalation, signalPid } from "./host-ensure-stop.js"; /** How long a refused handoff waits for the successor it killed to exit before releasing its record. */ const SUCCESSOR_EXIT_WAIT_MS = 5_000; /** * Undoes what a refused handoff left: kills the successor, releases its generation once it has exited * (a successor that never got a pid never ran, so its directory goes at once; one that outlives the wait * keeps its record until pruning finds it dead), and puts the boot settings back as they were. A * successor that was never spawned leaves only the boot settings to undo. A cleanup that fails is * answered, not thrown: the handoff still refused, and the caller is told why * the endpoint may not be as it was. */ export async function abandonSuccessor(context) { const { child, exited, paths, instanceId, bootSettings } = context; const target = { daemonDir: paths.dir, generation: generationPaths(paths, instanceId), instanceId, sender: { pid: process.pid, kind: "successor" }, reason: context.reason, }; // A successor that cannot be asked to stop is killed outright; it runs no exit handler, so the // record of it is this process's to write, BEFORE its generation is released below. const killed = child?.pid !== undefined && child.exitCode === null && child.signalCode === null ? await announceStop(target, child.pid) : undefined; if (killed !== undefined && child?.pid !== undefined) signalPid(child.pid, "SIGKILL"); try { if (bootSettings === undefined) await rm(paths.settingsFile, { force: true }); else await writeFile(paths.settingsFile, bootSettings, { mode: HOST_STATE_FILE_MODE }); if (child?.pid === undefined) await rm(generationPaths(paths, instanceId).dir, { recursive: true, force: true }); else if (exited !== undefined && (await exitedWithin(exited, SUCCESSOR_EXIT_WAIT_MS))) { if (killed !== undefined) await recordEscalation(target, killed); await releaseGeneration(paths, { instanceId, pid: child.pid }); } return undefined; } catch (cause) { return `successor cleanup failed: ${cause instanceof Error ? cause.message : String(cause)}`; } } async function exitedWithin(exited, ms) { let timer; const timedOut = new Promise((resolve) => { timer = setTimeout(() => resolve(false), ms); }); try { return await Promise.race([exited.then(() => true), timedOut]); } finally { clearTimeout(timer); } } //# sourceMappingURL=host-successor-abandon.js.map