@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
131 lines • 5.87 kB
JavaScript
import { execFile } from "node:child_process";
import { readFile } from "node:fs/promises";
import { sameBootAsOwn } from "./process-identity.js";
import { readProcessStartMs } from "./process-start-probe.js";
/** Start instants further apart than this are different processes, even on the same pid. */
export const OWNER_START_TOLERANCE_MS = 2_000;
export const DEFAULT_KILL_GRACE_MS = 1_000;
/** `ps -o lstart=` is always `Www Mmm dd hh:mm:ss yyyy`; the command follows. */
const LSTART_LENGTH = 24;
function errorCode(error) {
return typeof error === "object" && error !== null && "code" in error ? error.code : undefined;
}
export function isProcessAlive(pid) {
try {
process.kill(pid, 0);
return true;
}
catch (error) {
// EPERM: the pid exists but belongs to someone else - alive, and certainly not ours to kill.
return errorCode(error) !== "ESRCH";
}
}
function execText(command, args) {
return new Promise((resolve, reject) => {
const env = { ...process.env, LC_ALL: "C" };
execFile(command, [...args], { encoding: "utf8", timeout: 5_000, windowsHide: true, env }, (error, stdout) => {
if (error)
reject(error);
else
resolve(stdout);
});
});
}
async function readLinuxEvidence(pid) {
const [startedAtMs, environ, cmdline] = await Promise.all([
readProcessStartMs(pid),
readFile(`/proc/${pid}/environ`, "utf8"),
readFile(`/proc/${pid}/cmdline`, "utf8"),
]);
return { startedAtMs, environ, argv: cmdline.split("\0").filter(Boolean).join(" ") };
}
async function readDarwinEvidence(pid) {
const text = (await execText("ps", ["-o", "lstart=,command=", "-p", String(pid)])).trim();
if (text.length <= LSTART_LENGTH)
return undefined;
return { startedAtMs: new Date(text.slice(0, LSTART_LENGTH)).getTime(), argv: text.slice(LSTART_LENGTH).trim() };
}
async function readWindowsEvidence(pid) {
const script = `Get-CimInstance Win32_Process -Filter "ProcessId=${pid}" | ForEach-Object { $_.CreationDate.ToUniversalTime().ToString('o') + '|' + $_.CommandLine }`;
const text = (await execText("powershell", ["-NoProfile", "-NonInteractive", "-Command", script])).trim();
const separator = text.indexOf("|");
if (separator < 0)
return undefined;
return { startedAtMs: new Date(text.slice(0, separator)).getTime(), argv: text.slice(separator + 1).trim() };
}
function defaultEvidenceReader(platform) {
if (platform === "linux")
return readLinuxEvidence;
if (platform === "darwin")
return readDarwinEvidence;
if (platform === "win32")
return readWindowsEvidence;
return async () => undefined;
}
function argvMatches(observed, argv) {
if (observed === undefined || argv.length === 0)
return false;
const command = argv[argv.length - 1] ?? "";
return observed.trim() === argv.join(" ") || (command.length > 0 && observed.includes(command));
}
/**
* A pid alone is never trusted: after a crash or reboot the OS recycles it, so killing on a bare
* pid match can take down an unrelated process. Ownership needs the pid alive on the same boot,
* the same start instant, and a per-platform content marker (env on Linux, argv elsewhere). A
* background session carries no monitor id in its environment, so it is matched by argv everywhere.
*/
export async function confirmOwner(runtime, monitorId, probes = {}) {
const platform = probes.platform ?? process.platform;
try {
if (!(await sameBootAsOwn(runtime.bootAtMs, probes.bootAtMs)))
return "dead";
if (!(probes.isProcessAlive ?? isProcessAlive)(runtime.pid))
return "dead";
const evidence = await (probes.readProcessEvidence ?? defaultEvidenceReader(platform))(runtime.pid);
if (evidence?.startedAtMs === undefined || !Number.isFinite(evidence.startedAtMs))
return "unverifiable";
if (Math.abs(evidence.startedAtMs - runtime.startedAtMs) > OWNER_START_TOLERANCE_MS)
return "unverifiable";
const marked = platform === "linux" && monitorId !== undefined
? (evidence.environ ?? "").split("\0").includes(`SENPI_MONITOR_ID=${monitorId}`)
: argvMatches(evidence.argv, runtime.argv);
return marked ? "confirmed" : "unverifiable";
}
catch {
return "unverifiable";
}
}
export async function killTree(runtime, options = {}) {
const kill = options.kill ?? ((pid, signal) => process.kill(pid, signal));
const wait = options.wait ?? ((ms) => new Promise((resolve) => setTimeout(resolve, ms)));
const target = runtime.processGroupId !== undefined ? -runtime.processGroupId : runtime.pid;
try {
kill(target, "SIGTERM");
}
catch (error) {
if (errorCode(error) === "ESRCH")
return { action: "already-dead" };
throw error;
}
await wait(options.graceMs ?? DEFAULT_KILL_GRACE_MS);
if ((options.isProcessAlive ?? isProcessAlive)(runtime.pid)) {
try {
kill(target, "SIGKILL");
}
catch (error) {
if (errorCode(error) !== "ESRCH")
throw error;
}
}
return { action: "killed" };
}
export async function reapBeforeRespawn(runtime, monitorId, probes = {}) {
const verdict = await confirmOwner(runtime, monitorId, probes);
if (verdict === "dead")
return { action: "none" };
// win32 has no process groups to signal and no race-free way to act on a confirmation, so it only reports.
if (verdict === "confirmed" && (probes.platform ?? process.platform) !== "win32")
return killTree(runtime, probes);
return { action: "none", reason: `previous watcher pid ${runtime.pid} unverifiable` };
}
//# sourceMappingURL=orphan-reaper.js.map