akm-cli
Version:
akm (Agent Knowledge Manager) — a portable, local-first capability library for AI agents. Discover, load, share, and improve reusable skills, scripts, workflows, and knowledge across any shell-capable coding agent, including Claude Code, OpenCode, and Cur
166 lines (165 loc) • 6.81 kB
JavaScript
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
/**
* The record of the child processes akm starts asynchronously, so that a host running akm in its own process
* (akm-web) can wait for them on shutdown and stop what is left. Every asynchronous start in `src/` goes through
* {@link spawnChild}, {@link execFileChild} or, for `Bun.spawn`, {@link recordChild};
* `tests/integration/architecture/child-registry.test.ts` fails on one that does not. A synchronous start blocks this
* process until the child exits, so it is never running when a host stops, and is not recorded.
*
* The caller decides the process group (`detached`, see `childGroup` in `./subprocess`) unless a host called
* {@link startChildrenInOwnGroups}; the record notes it.
*/
import { spawn as nodeSpawn } from "node:child_process";
import { readdirSync, readFileSync } from "node:fs";
import path from "node:path";
const started = new Set();
const listeners = new Set();
let ownGroups = false;
/**
* From now on every child starts in a process group of its own on POSIX, whatever its caller asked. A host that stops
* its children by group (akm-web) needs each child's descendants in that child's group. The command line leaves the
* caller's choice alone: a child on a terminal shares akm's group so that Ctrl-C reaches it.
*/
export function startChildrenInOwnGroups() {
ownGroups = true;
}
/** The `detached` option a child starts with. */
export function childDetached(requested) {
return ownGroups && process.platform !== "win32" ? true : requested;
}
/** Records a started child. `exit` settles when the child itself exits. A child that failed to start has no pid. */
export function recordChild(pid, command, detached, exit) {
if (!pid)
return;
const child = {
pid,
name: [path.basename(command[0] ?? ""), ...command.slice(1, 2)].join(" "),
grouped: process.platform !== "win32" && detached === true,
exited: false,
};
started.add(child);
void exit.then(() => ended(child), () => ended(child));
for (const listener of listeners)
listener(child);
}
function ended(child) {
child.exited = true;
if (!running(child))
started.delete(child);
}
/** `child_process.spawn`, recorded. */
export function spawnChild(file, args, options) {
const detached = childDetached(options.detached);
const child = nodeSpawn(file, args, { ...options, detached });
recordChild(child.pid, [file, ...args], detached, exitOf(child));
return child;
}
/**
* `child_process.execFile` with UTF-8 output, recorded. Built on {@link spawnChild}, since `execFile` does not pass
* `detached` on and a host's own group would not apply. The callback gets what `execFile`'s does: `code` is the exit
* code or the error code of a failed start (`ENOENT`), `killed` is set when the timeout or the output limit ended it.
*/
export function execFileChild(file, args, options, callback) {
const child = spawnChild(file, args, { cwd: options.cwd, env: options.env });
const maxBuffer = options.maxBuffer ?? 1024 * 1024;
const output = { stdout: "", stderr: "" };
let failure;
let killed = false;
let settled = false;
const stop = () => {
killed = true;
child.kill();
};
const timer = options.timeout ? setTimeout(stop, options.timeout) : undefined;
const settle = (error) => {
if (settled)
return;
settled = true;
clearTimeout(timer);
callback(error, output.stdout, output.stderr);
};
for (const stream of ["stdout", "stderr"]) {
child[stream]?.setEncoding("utf8");
child[stream]?.on("data", (chunk) => {
output[stream] += chunk;
if (output[stream].length <= maxBuffer || failure)
return;
failure = Object.assign(new RangeError(`${stream} maxBuffer length exceeded`), {
code: "ERR_CHILD_PROCESS_STDIO_MAXBUFFER",
});
stop();
});
}
child.once("error", (error) => settle(Object.assign(error, { killed })));
child.once("close", (code, signal) => {
if (code === 0 && !failure)
return settle(null);
const command = [file, ...args].join(" ");
const error = failure ?? Object.assign(new Error(`Command failed: ${command}\n${output.stderr}`), { code: code ?? undefined });
settle(Object.assign(error, { killed, signal: signal ?? undefined, cmd: command }));
});
return child;
}
const exitOf = (child) => new Promise((resolve) => child.once("exit", () => resolve()));
/**
* The recorded children that still run: the child itself, or a member of the group it led. The others are dropped.
*/
export function runningChildren() {
for (const child of started)
if (!running(child))
started.delete(child);
return [...started];
}
/** Signals a recorded child: its whole process group when it leads one, otherwise the child alone. */
export function signalChild(child, signal) {
try {
process.kill(child.grouped ? -child.pid : child.pid, signal);
}
catch {
/* already gone */
}
}
/** Calls `listener` with every child recorded from now on. Returns the function that stops it. */
export function onChildStarted(listener) {
listeners.add(listener);
return () => listeners.delete(listener);
}
const running = (child) => !child.exited || (child.grouped && groupRunning(child.pid));
/**
* Whether a process group has a running member. A process that exited but that nobody waited for still belongs to its
* group; Linux can tell those apart, and has to where the container's first process never waits for them.
*/
function groupRunning(group) {
try {
process.kill(-group, 0);
}
catch {
return false;
}
if (process.platform !== "linux")
return true;
try {
// asset-io: host process table
for (const entry of readdirSync("/proc")) {
if (!/^\d+$/.test(entry))
continue;
try {
// "pid (name) state parent group …": the name may hold anything, so read after its closing bracket.
// asset-io: host process table
const stat = readFileSync(`/proc/${entry}/stat`, "utf8");
const [state, , pgrp] = stat.slice(stat.lastIndexOf(")") + 2).split(" ");
if (Number(pgrp) === group && state !== "Z")
return true;
}
catch {
/* gone */
}
}
return false;
}
catch {
return true;
}
}