@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
138 lines • 6.93 kB
JavaScript
/**
* Which unit a new generation's supervisor runs in (senpi#7099).
*
* A supervisor is setsid'd and reparented, but on Linux it stays in its CALLER's cgroup: when that caller lives in
* a transient unit (a terminal's per-terminal service, an ssh scope, a CI job), stopping the unit SIGTERMs the shared
* host with it. Where a user systemd manager answers, the supervisor is therefore started through
* `systemd-run --user --scope`, which moves itself into a new scope named for the generation and then EXECS the
* supervisor argv in place: the spawned pid is the supervisor itself, every inherited descriptor (the owner pipe at
* 3, the presenter channel at 4, the startup pipe, private fds) survives, and `--collect` unloads the scope once the
* generation exits. No live process is ever moved between cgroups. The probe creates one throwaway scope first: a
* manager that answers but refuses transient scopes (no cgroup delegation, bus policy) keeps the spawn unchanged
* instead of failing the start. Without a reachable manager (containers, CI without systemd, macOS) the spawn is
* unchanged too, and `host ensure` / `host status` name the unit the host's lifetime is tied to (`readHostUnit`).
*/
import { spawn } from "node:child_process";
import { constants } from "node:fs";
import { access, readFile } from "node:fs/promises";
import { delimiter, join } from "node:path";
const HOST_UNIT_PREFIX = "senpi-host-";
const MANAGER_PROBE_TIMEOUT_MS = 2_000;
/** `is-system-running` states in which the user manager answers and accepts a transient unit. */
function managerAnswers(state) {
return ["running", "degraded", "starting", "initializing", "maintenance"].includes(state);
}
/** One generation, one scope: the instance id makes the name unique per generation. */
function hostUnitName(instanceId) {
return `${HOST_UNIT_PREFIX}${instanceId}.scope`;
}
export function decideHostUnitPlacement(instanceId, probe) {
if (probe.platform !== "linux")
return { kind: "caller_unit", reason: "not_linux" };
if (!probe.userManager)
return { kind: "caller_unit", reason: "no_user_manager" };
if (probe.systemdRun === undefined)
return { kind: "caller_unit", reason: "no_systemd_run" };
if (!probe.scopeAccepted)
return { kind: "caller_unit", reason: "scope_refused" };
return { kind: "own_unit", unit: hostUnitName(instanceId), systemdRun: probe.systemdRun };
}
export function placedLaunch(launch, placement) {
if (placement.kind === "caller_unit")
return launch;
return {
command: placement.systemdRun,
args: [
"--user",
"--scope",
`--unit=${placement.unit}`,
"--collect",
"--quiet",
"--",
launch.command,
...launch.args,
],
};
}
/** What the placement needs to know about `env`, the environment the supervisor is spawned with. */
export async function probeHostUnitPlacement(env, platform = process.platform) {
if (platform !== "linux" || !env.XDG_RUNTIME_DIR)
return { platform, userManager: false, systemdRun: undefined, scopeAccepted: false };
const [userManager, systemdRun] = await Promise.all([userManagerAnswers(env), onPath("systemd-run", env.PATH)]);
const scopeAccepted = userManager && systemdRun !== undefined && (await scopeStarts(systemdRun, env));
return { platform, userManager, systemdRun, scopeAccepted };
}
/** The launch a generation is spawned with: in its own scope where the user manager answers, else unchanged. */
export async function placeHostLaunch(launch, instanceId, env) {
return placedLaunch(launch, decideHostUnitPlacement(instanceId, await probeHostUnitPlacement(env)));
}
function probeEnv(env) {
return { PATH: env.PATH ?? "", XDG_RUNTIME_DIR: env.XDG_RUNTIME_DIR ?? "" };
}
/** The real placement's own command with `true` as the payload: a refused scope shows here, not at the spawn. */
function scopeStarts(systemdRun, env) {
return new Promise((resolve) => {
const child = spawn(systemdRun, ["--user", "--scope", "--collect", "--quiet", "--", "true"], {
env: probeEnv(env),
stdio: "ignore",
timeout: MANAGER_PROBE_TIMEOUT_MS,
});
child.once("error", () => resolve(false));
child.once("close", (code) => resolve(code === 0));
});
}
function userManagerAnswers(env) {
return new Promise((resolve) => {
const child = spawn("systemctl", ["--user", "is-system-running"], {
env: probeEnv(env),
stdio: ["ignore", "pipe", "ignore"],
timeout: MANAGER_PROBE_TIMEOUT_MS,
});
let stdout = "";
child.stdout.setEncoding("utf8").on("data", (chunk) => {
stdout += chunk;
});
child.once("error", () => resolve(false));
child.once("close", () => resolve(managerAnswers(stdout.trim())));
});
}
async function onPath(program, path) {
for (const dir of (path ?? "").split(delimiter)) {
if (!dir)
continue;
const candidate = join(dir, program);
if (await access(candidate, constants.X_OK).then(() => true, () => false))
return candidate;
}
return undefined;
}
/** Status rows with the unit each live generation's supervisor runs in; an ended one has none. */
export function withHostUnits(rows) {
return Promise.all(rows.map(async (row) => ({ ...row, unit: row.alive ? await readHostUnit(row.pid, row.instanceId) : null })));
}
/**
* The `host ensure` / `host handoff` answer: the unit, and `lifetime_tied_to` naming the unit whose stop would end
* the host when it is not the generation's own.
*/
export async function hostUnitPayload(pid, instanceId) {
const unit = await readHostUnit(pid, instanceId);
return unit !== null && !unit.own && unit.name !== null ? { unit, lifetime_tied_to: unit.name } : { unit };
}
/** `null` off Linux or when the process's cgroup cannot be read. */
async function readHostUnit(pid, instanceId) {
if (process.platform !== "linux")
return null;
const table = await readFile(`/proc/${pid}/cgroup`, "utf8").catch(() => undefined);
return table === undefined ? null : hostUnitFromCgroupTable(table, instanceId);
}
export function hostUnitFromCgroupTable(table, instanceId) {
const lines = table.split("\n");
const entry = lines.find((line) => line.startsWith("0::")) ?? lines.find((line) => line.includes(":name=systemd:"));
if (entry === undefined)
return null;
const cgroup = entry.slice(entry.indexOf(":", entry.indexOf(":") + 1) + 1);
const leaf = cgroup.slice(cgroup.lastIndexOf("/") + 1);
const name = /\.(service|scope)$/u.test(leaf) ? leaf : null;
return { cgroup, name, own: name !== null && instanceId !== undefined && name === hostUnitName(instanceId) };
}
//# sourceMappingURL=host-unit-placement.js.map