UNPKG

@code-yeongyu/senpi

Version:

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

239 lines • 11.2 kB
/** * The kill guard's runtime layer (senpi#5349, DESIGN-12096 §4' L2): a shim directory * `<agent dir>/kill-guard/<version>/` written once per version, and the environment that puts it first on PATH, points * BASH_ENV at its wrapper and exports what the shims need. The host activates it at session start (the `kill-guard` * builtin) and `getShellEnv()` plus the eval kernels' session environment carry it to every agent-spawned process. * POSIX only: on Windows nothing is installed. */ import { existsSync, mkdirSync, mkdtempSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs"; import { basename, delimiter, dirname, join } from "node:path"; import { fileURLToPath } from "node:url"; import { hasFunctions, realmRegistry } from "@earendil-works/pi-realm-registry"; import { isBunBinary, VERSION } from "../../config.js"; import { KILL_GUARD_BASH_ENV, KILL_GUARD_JUDGE_DEADLINE_SECONDS, KILL_GUARD_PROMPT_HOOK_PREFIX, KILL_GUARD_SHIM_MARKER, killGuardPromptCommand, renderKillGuardShims, } from "./shim-script.js"; export { KILL_GUARD_SHIM_MARKER, renderKillGuardShims } from "./shim-script.js"; /** * Writes the shim directory for `version` under `root` when it is missing or stale: staged in a temp directory and * renamed into place, so a reader sees either no directory or a complete one. Returns the directory. */ export function installKillGuardShims(root, version, options = {}) { const dir = join(root, version.replace(/[^A-Za-z0-9._-]/g, "_")); const files = renderKillGuardShims(dir, options.deadlineSeconds ?? KILL_GUARD_JUDGE_DEADLINE_SECONDS); if (matches(dir, files)) return dir; mkdirSync(root, { recursive: true }); const staged = mkdtempSync(join(root, ".staging-")); try { for (const [name, content] of Object.entries(files)) { writeFileSync(join(staged, name), content, { mode: name.endsWith(".sh") ? 0o644 : 0o755 }); } writeFileSync(join(staged, KILL_GUARD_SHIM_MARKER), ""); place(staged, dir, files); } finally { rmSync(staged, { recursive: true, force: true }); } return dir; } function matches(dir, files) { if (!existsSync(join(dir, KILL_GUARD_SHIM_MARKER))) return false; return Object.entries(files).every(([name, content]) => { try { return readFileSync(join(dir, name), "utf8") === content; } catch { return false; } }); } /** Renames the staged directory into place; a same-version directory with stale content is swapped out first. */ function place(staged, dir, files) { try { renameSync(staged, dir); return; } catch (error) { if (matches(dir, files)) return; if (!existsSync(dir)) throw error; } const stale = mkdtempSync(join(dirname(dir), ".stale-")); rmSync(stale, { recursive: true, force: true }); renameSync(dir, stale); try { renameSync(staged, dir); } finally { rmSync(stale, { recursive: true, force: true }); } } /** * Whether `base` carries an outer agent's kill-guard BASH_ENV: a `bash_env.sh` written together with the private * `SENPI_KILL_GUARD_USER_BASH_ENV` that `killGuardEnvironment` always sets beside it. Read from the environment alone, * like the PROMPT_COMMAND hook's prefix, so building a spawn environment touches no file (no-sync-in-session-path). */ function isKillGuardBashEnv(base) { return (base.BASH_ENV !== undefined && basename(base.BASH_ENV) === KILL_GUARD_BASH_ENV && base.SENPI_KILL_GUARD_USER_BASH_ENV !== undefined); } /** * Whether bash started with this inherited SHLVL counts as nested (level >= 2 after its own increment). A top-level * non-interactive bash skips BASH_ENV when its stdin is a socket (bash's rshd rule) or sshd started it, and SHLVL >= 999 * makes bash reset the level to 1. */ function nestedShellLevel(level) { return level !== undefined && /^[1-9]\d{0,2}$/.test(level) && Number(level) < 999; } /** * The variables that carry `runtime` into a process started with `base`: the shim directory first on PATH (once), * BASH_ENV on the wrapper with the user's own BASH_ENV kept in `SENPI_KILL_GUARD_USER_BASH_ENV` (also when `base` * already carries a kill-guard BASH_ENV from an outer agent), PROMPT_COMMAND on the hook that carries the same `kill` * wrapper into interactive bash with the user's own PROMPT_COMMAND kept the same way in * `SENPI_KILL_GUARD_USER_PROMPT_COMMAND` (an outer agent's hook is never captured as the user's: it would eval itself * forever), the judge exports, and `SHLVL=1` when `base` has no usable level, so bash reads BASH_ENV whatever its * stdin is. */ export function killGuardEnvironment(base, runtime) { const pathKey = Object.keys(base).find((key) => key.toLowerCase() === "path") ?? "PATH"; const rest = (base[pathKey] ?? "").split(delimiter).filter((entry) => entry !== "" && entry !== runtime.dir); const userBashEnv = isKillGuardBashEnv(base) ? base.SENPI_KILL_GUARD_USER_BASH_ENV : base.BASH_ENV; const userPromptCommand = base.PROMPT_COMMAND?.startsWith(KILL_GUARD_PROMPT_HOOK_PREFIX) ? base.SENPI_KILL_GUARD_USER_PROMPT_COMMAND : base.PROMPT_COMMAND; return { [pathKey]: [runtime.dir, ...rest].join(delimiter), BASH_ENV: runtime.bashEnv, SENPI_KILL_GUARD_USER_BASH_ENV: userBashEnv ?? "", PROMPT_COMMAND: killGuardPromptCommand(runtime.dir), SENPI_KILL_GUARD_USER_PROMPT_COMMAND: userPromptCommand ?? "", SENPI_KILL_GUARD_PROTECTED: runtime.protectedPids.join(","), SENPI_KILL_GUARD_EXEC: runtime.exec, SENPI_KILL_GUARD_EXEC_ENTRY: runtime.entry ?? "", SENPI_KILL_GUARD_AGENT_PID: String(runtime.agentPid), ...(nestedShellLevel(base.SHLVL) ? {} : { SHLVL: "1" }), }; } /** * The judge entry for this layout: none in the compiled binary (the shims call its hidden `__kill-guard` subcommand); * `check-entry` beside this file in the source tree and the unbundled `dist`; `kill-guard-check.js` beside the chunk * this module lands in for the npm bundle (the bundle build checks that they share a directory). */ function judgeEntry() { if (isBunBinary) return undefined; const here = import.meta.url; if (/\/core\/kill-guard\/runtime\.(ts|js)$/.test(here)) { return fileURLToPath(new URL(here.endsWith(".ts") ? "./check-entry.ts" : "./check-entry.js", here)); } return fileURLToPath(new URL("./kill-guard-check.js", here)); } const killGuardRuntimes = realmRegistry("@code-yeongyu/senpi/kill-guard/v1", () => { const installed = new Map(); const holders = new Map(); return { activate: (holder, agentDir, make) => { let runtime = installed.get(agentDir); if (runtime === undefined) { runtime = make([...installed.values()][0]); installed.set(agentDir, runtime); } holders.delete(holder); holders.set(holder, runtime); return runtime; }, release: (holder) => { holders.delete(holder); }, active: () => { let active; for (const runtime of holders.values()) active = runtime; return active; }, }; }, (value) => hasFunctions(value, ["activate", "release", "active"])); /** * The record each of this copy's holders uses, in activation order: the shared record, or this copy's own when the * shared one is unreadable. An unreadable active record falls back to the newest of these, so the guard stays on. */ const ownRecords = new Map(); /** This copy's own record per agent dir, built only when the shared record for that dir is unreadable. */ const localRuntimes = new Map(); const isNumberArray = (value) => Array.isArray(value) && value.every((entry) => typeof entry === "number"); /** A runtime record another copy may have built: every field present with its type. */ function isKillGuardRuntime(value) { if (typeof value !== "object" || value === null) return false; const field = (name) => Reflect.get(value, name); const entry = field("entry"); return (typeof field("dir") === "string" && typeof field("bashEnv") === "string" && typeof field("exec") === "string" && (entry === undefined || typeof entry === "string") && typeof field("agentPid") === "number" && isNumberArray(field("protectedPids"))); } function buildRuntime(agentDir, protectedPids) { const dir = installKillGuardShims(join(agentDir, "kill-guard"), VERSION); return Object.freeze({ dir, bashEnv: join(dir, KILL_GUARD_BASH_ENV), exec: process.execPath, entry: judgeEntry(), agentPid: process.pid, protectedPids, }); } /** Installs (once per agent dir) and activates the runtime layer for this process; `undefined` on Windows. */ export function activateKillGuardRuntime(options) { if ((options.platform ?? process.platform) === "win32") return undefined; const shared = killGuardRuntimes.activate(options.holder, options.agentDir, (first) => { const firstPids = typeof first === "object" && first !== null ? Reflect.get(first, "protectedPids") : undefined; return buildRuntime(options.agentDir, isNumberArray(firstPids) ? firstPids : options.protectedPids()); }); ownRecords.delete(options.holder); if (isKillGuardRuntime(shared)) { ownRecords.set(options.holder, shared); return shared; } // The guard stays on: an unreadable shared record never reads as "no runtime". let local = localRuntimes.get(options.agentDir); if (local === undefined) { local = buildRuntime(options.agentDir, options.protectedPids()); localRuntimes.set(options.agentDir, local); } ownRecords.set(options.holder, local); options.warn?.(`[senpi:kill-guard] shared record unreadable; local record in use for ${options.agentDir}`); return local; } /** * Drops one holder (its session ended, or its user switched the guard off); the other holders keep theirs, and the * runtime layer stops reaching new processes once none is left. */ export function releaseKillGuardRuntime(holder) { ownRecords.delete(holder); killGuardRuntimes.release(holder); } function activeKillGuardRuntime() { const active = killGuardRuntimes.active(); if (active === undefined || isKillGuardRuntime(active)) return active; let own; for (const record of ownRecords.values()) own = record; return own; } export function withKillGuardRuntime(env) { const active = activeKillGuardRuntime(); return active === undefined ? env : { ...env, ...killGuardEnvironment(env, active) }; } /** The runtime layer's variables for a kernel session environment built on `base`; empty when none is active. */ export function activeKillGuardEnvironment(base) { const active = activeKillGuardRuntime(); return active === undefined ? {} : killGuardEnvironment(base, active); } //# sourceMappingURL=runtime.js.map