UNPKG

@code-yeongyu/senpi

Version:

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

117 lines • 5.38 kB
/** * Durable evidence of how a supervised RPC host generation ENDED. * * The supervisor already knows the difference - `classifyChildExit` in `host-lifecycle.ts` * separates a clean idle exit from a crash - but it only ever reported the crash to stderr, and * the daemon's stderr log is truncated by the very restart that replaces the dead host. A host * dying every fifty minutes was therefore indistinguishable, from inside senpi, from one that had * never died at all. * * This file is append-only and lives beside the other per-endpoint daemon state, so a restart adds * to it instead of replacing it. Two record identities share it (senpi#2566): the TERMINAL record * (`kind: "rpc-host"`) says how a generation's host child ended and who asked for it - a death * (`supervisor`, `external`) or an engine-initiated stop (`engine_stop`) - and the WATCHDOG record * (`kind: "rpc-host-watchdog"`) is the host child's own line when its supervisor vanished. At most * one terminal record is kept per generation; a clean exit the host chose itself writes nothing. */ import { appendFileSync, mkdirSync, readFileSync, writeFileSync } from "node:fs"; import { join } from "node:path"; import { acquireOwnershipSafeLock } from "./ownership-safe-lock.js"; /** Crash records kept per endpoint. A crash loop rewrites the file down to this many. */ export const HOST_CRASH_RECORD_LIMIT = 50; /** Written 0600 like every other daemon state file: it names local paths and process detail. */ const CRASH_RECORD_FILE_MODE = 0o600; /** * How long a terminal writer waits for the record lock before appending anyway: a duplicate terminal * line is tolerable, a missing one is not. Never the ensure lock - an ensure holds that one while it * waits for the very supervisor whose record this is. */ const TERMINAL_RECORD_LOCK_WAIT_MS = 500; /** `<daemonDir>/crashes.jsonl` - one JSON object per line, oldest first. */ export function hostCrashRecordFile(daemonDir) { return join(daemonDir, "crashes.jsonl"); } /** A record that counts as a death: a terminal record no engine process asked for. */ export function isHostCrash(record) { return (record.kind === undefined || record.kind === "rpc-host") && record.detection !== "engine_stop"; } /** * Append one record, then bound the file. * * NEVER throws: this runs on exit paths, where a filesystem failure must not be able to delay or * prevent the shutdown it is only annotating. */ export function recordHostCrash(daemonDir, record) { try { mkdirSync(daemonDir, { recursive: true, mode: 0o700 }); appendFileSync(hostCrashRecordFile(daemonDir), `${JSON.stringify(record)}\n`, { mode: CRASH_RECORD_FILE_MODE }); pruneHostCrashRecords(daemonDir); } catch { // Evidence is best-effort; the shutdown it annotates is not. } } /** * Append a TERMINAL record unless one for the same generation already exists: the supervisor's exit * handler and a caller that had to SIGKILL that supervisor can both reach this for one generation. * The check-then-append runs under `crashes.lock`; a lock that cannot be had in time appends anyway. */ export async function recordTerminalHostRecord(daemonDir, record) { const release = await acquireRecordLock(daemonDir); try { const duplicate = record.generation !== undefined && readHostCrashRecords(daemonDir).some((existing) => existing.kind === "rpc-host" && existing.generation === record.generation); if (!duplicate || release === undefined) recordHostCrash(daemonDir, { ...record, kind: "rpc-host" }); } finally { await release?.().catch(() => undefined); } } async function acquireRecordLock(daemonDir) { try { mkdirSync(daemonDir, { recursive: true, mode: 0o700 }); return await acquireOwnershipSafeLock(join(daemonDir, "crashes.lock"), { retries: { retries: Math.ceil(TERMINAL_RECORD_LOCK_WAIT_MS / 50), minTimeout: 20, maxTimeout: 50 }, }); } catch { return undefined; } } /** Every record currently held, oldest first. Unparseable lines are skipped, never thrown on. */ export function readHostCrashRecords(daemonDir) { let raw; try { raw = readFileSync(hostCrashRecordFile(daemonDir), "utf8"); } catch { return []; } const records = []; for (const line of raw.split("\n")) { if (line.trim() === "") continue; try { const value = JSON.parse(line); if (typeof value === "object" && value !== null && typeof Reflect.get(value, "at") === "string") { records.push(value); } } catch { // A torn final line from a host killed mid-append is not a reason to lose the rest. } } return records; } /** Keep the newest `HOST_CRASH_RECORD_LIMIT` records so a crash loop cannot grow the file forever. */ function pruneHostCrashRecords(daemonDir) { const records = readHostCrashRecords(daemonDir); if (records.length <= HOST_CRASH_RECORD_LIMIT) return; const kept = records.slice(records.length - HOST_CRASH_RECORD_LIMIT); writeFileSync(hostCrashRecordFile(daemonDir), `${kept.map((r) => JSON.stringify(r)).join("\n")}\n`, { mode: CRASH_RECORD_FILE_MODE, }); } //# sourceMappingURL=host-crash-record.js.map