UNPKG

@code-yeongyu/senpi

Version:

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

131 lines • 6.03 kB
/** * WHAT the daemon's boot settings say, and the primitives every state file goes through. * * `settings.json` is written twice on purpose: the daemon directory holds what the SUPERVISOR reads * at boot, and the generation's own directory keeps what THAT generation was started with, which * survives the next generation overwriting the boot copy. * * The three primitives below are shared with `host-daemon-registration.ts`: a write that carries the * 0600 mode and names the path it failed on, a read that treats a missing file as "no state" rather * than an error, and the JSON narrowing both sides parse records with. WHERE any of these files live * is `host-daemon-paths.ts`; nothing here builds a path of its own. */ import { readFile, writeFile } from "node:fs/promises"; import { join } from "node:path"; import { processStartTimeMs, readProcessIdentity, sameProcessStartMs } from "../../utils/process-identity.js"; import { createGenerationDirectory, generationPaths, HOST_STATE_FILE_MODE, HostDaemonStateError, } from "./host-daemon-paths.js"; import { readJsonObject, writeJsonAtomic } from "./host-state-json.js"; /** Null explicitly means unowned; undefined is an unreadable/old generation, never permission to take over. */ export async function readHostOwner(generationDir) { const record = await readJsonObject(join(generationDir, "owner.json")); if (record?.owner === null) return null; if (!isRecord(record?.owner)) return undefined; const { pid, startTime } = record.owner; return typeof pid === "number" && Number.isSafeInteger(pid) && pid > 0 && typeof startTime === "string" && startTime.length > 0 ? { pid, startTime } : undefined; } /** * The one mutable ownership record: ensure writes it under the endpoint lock while holding an * attachment. Supervisors only read it. Generation teardown removes it before the pointer (#2243). */ export function writeHostOwner(generationDir, owner) { return writeJsonAtomic(join(generationDir, "owner.json"), { owner }); } export function sameHostOwner(a, b) { if (a == null || b == null || a.pid !== b.pid) return false; // A pipe binds one incarnation: liveness tolerance must not merge two replacement owners. const started = processStartTimeMs(a.startTime); return started !== undefined && started === processStartTimeMs(b.startTime); } export async function callerHostOwner() { const observed = await readProcessIdentity(process.pid, process.platform, undefined, undefined, "UTC"); if (observed.kind !== "present" || processStartTimeMs(observed.identity) === undefined) throw new Error("cannot establish RPC host owner OS start identity"); return { pid: process.pid, startTime: observed.identity }; } export async function hostOwnerGone(owner) { const observed = await readProcessIdentity(owner.pid, process.platform, 1_000, undefined, "UTC"); return (observed.kind === "absent" || (observed.kind === "present" && processStartTimeMs(observed.identity) !== undefined && processStartTimeMs(owner.startTime) !== undefined && !sameProcessStartMs(processStartTimeMs(observed.identity), processStartTimeMs(owner.startTime)))); } /** A live or unknown different owner is never silently replaced. Called inside the ensure lock. */ export async function claimHostOwner(generationDir) { const previous = await readHostOwner(generationDir); if (previous === undefined) throw new Error("RPC host does not support owner lifetime registration"); const next = await callerHostOwner(); if (sameHostOwner(previous, next)) return; if (previous !== null && !(await hostOwnerGone(previous))) { throw new Error("RPC host lifetime owner is still alive or its identity is unknown"); } await writeHostOwner(generationDir, next); } /** * Publishes the settings a generation is started with, in both places they are read: the daemon * directory (what the supervisor loads at boot) and the generation's own directory (what that * generation was started with, which survives the next generation overwriting the boot copy). */ export async function writeHostSettings(paths, settings) { const generation = generationPaths(paths, settings.instanceId); await createGenerationDirectory(generation); await writeStateFile(paths.settingsFile, settings); await writeStateFile(generation.settingsFile, settings); } /** The policy the running generation was started with, for a successor that states none of its own. */ export async function readHostSettings(paths) { const parsed = parseJson(await readFileOrUndefined(paths.settingsFile)); if (!parsed) return undefined; return { ...(typeof parsed.coldStart === "string" && { coldStart: parsed.coldStart }), ...(typeof parsed.idleExitMs === "number" && { idleExitMs: parsed.idleExitMs }), }; } export async function writeStateFile(path, content) { try { await writeFile(path, `${JSON.stringify(content)}\n`, { mode: HOST_STATE_FILE_MODE }); } catch (cause) { throw new HostDaemonStateError(path, cause); } } export async function readFileOrUndefined(path) { try { return await readFile(path, "utf8"); } catch (error) { if (isNodeErrorCode(error, "ENOENT") || isNodeErrorCode(error, "ENOTDIR")) return undefined; throw error; } } export function parseJson(text) { if (text === undefined) return undefined; try { const parsed = JSON.parse(text); return isRecord(parsed) ? parsed : undefined; } catch { return undefined; } } export function isRecord(value) { return typeof value === "object" && value !== null && !Array.isArray(value); } function isNodeErrorCode(error, code) { return error instanceof Error && "code" in error && error.code === code; } //# sourceMappingURL=host-daemon-state.js.map