UNPKG

@code-yeongyu/senpi

Version:

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

96 lines • 4.4 kB
/** * HOW A GENERATION'S END IS READ: the exit verdict, and the terminal record of it (senpi#2566). * * Split out of `host-lifecycle.ts` because that file is far past the per-file ceiling and this is * one cohesive unit - deciding whether the host stopped, was stopped, or died, and recording it. */ import { crashingRuntimeVersions } from "../../core/process-crash-record.js"; import { recordTerminalHostRecord } from "./host-crash-record.js"; import { recentStall, STOP_STALL_EVIDENCE_MAX_AGE_MS } from "./host-stalled-evidence.js"; import { consumeStopIntent } from "./host-stop-intent.js"; /** * The RPC host exits 0 only through its own clean shutdown path - including its idle/empty-host * policy - so that is an intentional stop, not a crash: the supervisor mirrors its own idle exit * instead of reporting failure. Any non-zero code or signal stays a crash. */ export function classifyChildExit(code, signal) { if (code === 0 && signal === null) return { reason: "rpc host exited on its own idle policy", exitCode: 0 }; return { reason: `rpc host process exited unexpectedly (${code ?? signal})`, exitCode: 1 }; } /** * Record how the host child ended. A fresh stop intent for THIS generation makes it an `engine_stop` * naming the intent's sender; a supervisor shutdown without one is the supervisor's own stop; a signal * nobody announced is `external`; any other non-zero exit is the supervisor's observation of a crash. * A clean exit the host chose itself writes nothing. Never throws. */ export async function noteChildExit(context) { const now = context.now ?? Date.now(); const intent = await consumeStopIntent(context.generation, now).catch(() => undefined); const outcome = describeExit(context, intent); if (outcome === undefined) return; const stall = await recentStall(context.generation, now, STOP_STALL_EVIDENCE_MAX_AGE_MS).catch(() => undefined); await recordTerminalHostRecord(context.daemonDir, { at: new Date(now).toISOString(), ...(context.signal === null ? { code: context.code ?? undefined } : { signal: context.signal }), uptimeMs: Math.max(0, now - context.childStartedAt), kind: "rpc-host", generation: context.instanceId, ...outcome, ...(stall ? { stall: { driftMs: stall.driftMs, at: stall.at, attributedSessionId: stall.sessionId } } : {}), ...crashingRuntimeVersions(), }).catch(() => undefined); } function describeExit(context, intent) { if (intent !== undefined) { return { detection: "engine_stop", sender: intent.sender, ...(intent.chain ? { chain: intent.chain } : {}), reason: intent.reason, stopIntent: true, }; } if (context.shutdown !== undefined) { return { detection: "engine_stop", sender: context.shutdown.supervisor, reason: context.shutdown.reason, stopIntent: false, }; } if (classifyChildExit(context.code, context.signal).exitCode === 0) return undefined; const selfInflicted = context.signal === null || SELF_INFLICTED_SIGNALS.has(context.signal); return { detection: selfInflicted ? "supervisor" : "external", stopIntent: false }; } /** Signals a process raises against itself when it crashes; anything else was sent from outside. */ const SELF_INFLICTED_SIGNALS = new Set([ "SIGSEGV", "SIGBUS", "SIGABRT", "SIGILL", "SIGFPE", "SIGTRAP", "SIGSYS", ]); /** * The terminal record a CALLER writes after it had to SIGKILL a supervisor: a SIGKILLed supervisor * runs no exit handler, so the caller is the only process guaranteed to survive this path. It writes * BEFORE it releases or clears the registration, whose recursive removal takes the intent with it. */ export async function noteEscalatedStop(daemonDir, instanceId, intent, now = Date.now()) { await recordTerminalHostRecord(daemonDir, { at: new Date(now).toISOString(), signal: "SIGKILL", kind: "rpc-host", generation: instanceId, detection: "engine_stop", sender: intent.sender, ...(intent.chain ? { chain: intent.chain } : {}), reason: `${intent.reason}; supervisor_escalated`, stopIntent: true, }).catch(() => undefined); } //# sourceMappingURL=host-child-exit.js.map