UNPKG

@code-yeongyu/senpi

Version:

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

206 lines • 7.94 kB
import { execFile } from "node:child_process"; import { randomBytes } from "node:crypto"; import { readFileSync } from "node:fs"; import { mkdir, readdir, readFile, rm, writeFile } from "node:fs/promises"; import { join } from "node:path"; /** * Environment variable every exec/spawn child of an instance with a scope registry (a `stateDir`) carries; the sweep * finds members by it. An instance without a registry (senpi's own tool path) adds no variable. */ export const SCOPE_ENV = "SENPI_MACHINE_SCOPE"; /** Set on the runtime's own processes, which a sweep never signals. */ export const RUNTIME_ENV = "OMO_MACHINE_RUNTIME"; /** HUP, then TERM, then KILL, each followed by its wait (herdr sweep ladder). */ export const SWEEP_LADDER = [ { signal: "SIGHUP", waitMs: 500 }, { signal: "SIGTERM", waitMs: 500 }, { signal: "SIGKILL", waitMs: 2000 }, ]; const SWEEP_POLL_MS = 25; export function newScopeToken() { return randomBytes(16).toString("base64url"); } function alive(target) { try { process.kill(target, 0); return true; } catch (error) { // EPERM means the target exists but is not ours to signal; only ESRCH proves it is gone. return error.code !== "ESRCH"; } } /** * The start time the OS reports for `pid`, as an opaque string, or undefined when it cannot be read. Linux: field 22 * of `/proc/<pid>/stat` (clock ticks since boot), read synchronously so a just-spawned child cannot be reaped first; * macOS/BSD: `ps -o lstart=`; Windows: undefined (recorded groups are never swept there). */ function readStartTime(pid) { if (process.platform === "linux") { try { const stat = readFileSync(`/proc/${pid}/stat`, "utf8"); // Field 2 (comm) may contain spaces and parentheses; the fields after its closing ")" start at field 3. return Promise.resolve(stat.slice(stat.lastIndexOf(")") + 2).split(" ")[22 - 3]); } catch { return Promise.resolve(undefined); } } if (process.platform === "win32") return Promise.resolve(undefined); return new Promise((resolve) => { execFile("ps", ["-o", "lstart=", "-p", String(pid)], (error, stdout) => { const value = stdout.trim(); resolve(error || value === "" ? undefined : value); }); }); } function signal(target, sig) { try { process.kill(target, sig); } catch { // Already gone. } } /** Linux: every process whose environment carries `<SCOPE_ENV>=<token>` and not `<RUNTIME_ENV>=1`. */ async function scanEnvironments(tokens) { if (process.platform !== "linux") return []; let names; try { names = await readdir("/proc"); } catch { return []; } const members = []; await Promise.all(names.map(async (name) => { const pid = Number(name); if (!Number.isInteger(pid) || pid === process.pid) return; let environ; try { environ = await readFile(`/proc/${pid}/environ`, "utf8"); } catch { return; } const vars = environ.split("\0"); if (vars.includes(`${RUNTIME_ENV}=1`)) return; for (const entry of vars) { if (entry.startsWith(`${SCOPE_ENV}=`) && tokens.has(entry.slice(SCOPE_ENV.length + 1))) { members.push(pid); return; } } })); return members; } function groupTarget(pid) { return process.platform === "win32" ? pid : -pid; } /** * Scope tokens: the token file is written under `<stateDir>/machine/scopes/` before the child spawns, so a crash * between spawn and bookkeeping still leaves a record; `sweep` ends every member of a token. */ export class ScopeRegistry { constructor(stateDir, killTree, options) { /** token -> process-group leader -> the leader's start time read when it was attached. */ this.groups = new Map(); this.dir = join(stateDir, "machine", "scopes"); this.killTree = killTree; this.startTimeOf = options?.startTimeOf ?? readStartTime; } tokenPath(token) { return join(this.dir, `${token}.json`); } async record(token, meta) { // Created on every record, never cached: a scopes dir removed mid-process is recreated, not fatal forever. await mkdir(this.dir, { recursive: true }); await writeFile(this.tokenPath(token), `${JSON.stringify({ token, createdAtMs: Date.now(), ...meta })}\n`); } attach(token, pid) { let leaders = this.groups.get(token); if (leaders === undefined) { leaders = new Map(); this.groups.set(token, leaders); } leaders.set(pid, this.startTimeOf(pid)); } /** Drop the token file once its exec settled; the group stays sweepable by token. */ async release(token) { await rm(this.tokenPath(token), { force: true }); const leaders = this.groups.get(token); if (leaders === undefined) return; for (const pid of leaders.keys()) if (!alive(groupTarget(pid))) leaders.delete(pid); if (leaders.size === 0) this.groups.delete(token); } /** * Whether the live group `pid` is still the one this registry started. A live process with the leader's pid must * have the start time recorded at attach. A gone leader with a live group is ours: POSIX does not reuse a process * group ID while the group exists, so the pid cannot have been handed to a new group leader meanwhile. */ async sameGroup(pid, recorded) { if (!alive(pid)) return true; const then = await recorded; return then !== undefined && (await this.startTimeOf(pid)) === then; } async targets(tokens) { const groups = []; for (const token of tokens) { const leaders = this.groups.get(token); if (leaders === undefined) continue; for (const [pid, recorded] of leaders) { if (!alive(groupTarget(pid))) continue; // A reused pid leads a group this registry did not create: drop the record without signalling. if (await this.sameGroup(pid, recorded)) groups.push(pid); else leaders.delete(pid); } } const pids = (await scanEnvironments(tokens)).filter(alive); return { groups, pids }; } async sweep(tokenList) { const tokens = new Set(tokenList); for (const step of SWEEP_LADDER) { const { groups, pids } = await this.targets(tokens); if (groups.length === 0 && pids.length === 0) break; if (process.platform === "win32") { if (step.signal === "SIGKILL") for (const pid of [...groups, ...pids]) this.killTree(pid); } else { for (const pgid of groups) signal(-pgid, step.signal); for (const pid of pids) signal(pid, step.signal); } const deadline = Date.now() + step.waitMs; for (;;) { const live = [...groups.map(groupTarget), ...pids].filter(alive); if (live.length === 0 || Date.now() >= deadline) break; // OS boundary: process death has no event to await, so the wait is a bounded poll. await new Promise((resolve) => setTimeout(resolve, SWEEP_POLL_MS)); } } for (const token of tokens) { this.groups.delete(token); await rm(this.tokenPath(token), { force: true }); } } } //# sourceMappingURL=scopes.js.map