@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
52 lines • 1.74 kB
JavaScript
import { TOOL_RUNTIME_UNSUPPORTED_HOST } from "../../../cli/tool-runtime.js";
import { loadKernel32Job, WindowsJob, WindowsJobError } from "./windows-job.js";
const DEFAULT_HOST = { platform: process.platform, loadKernel: loadKernel32Job };
export class ChildContainmentError extends Error {
constructor(code, message) {
super(`${code}: ${message}`);
this.name = "ChildContainmentError";
this.code = code;
}
}
export function containChild(child, host = DEFAULT_HOST) {
const pid = child.pid;
if (pid === undefined)
throw new ChildContainmentError("tool_child_bootstrap_failed", "the tool child has no pid");
if (host.platform !== "win32")
return processGroup(pid);
const kernel = host.loadKernel();
if (kernel === undefined)
throw new ChildContainmentError(TOOL_RUNTIME_UNSUPPORTED_HOST, "Windows tool children need bun:ffi for their Job Object");
const job = WindowsJob.create(kernel);
try {
job.assign(pid);
}
catch (error) {
job.terminate();
job.close();
if (error instanceof WindowsJobError)
throw new ChildContainmentError(error.code, error.message);
throw error;
}
return {
kind: "job",
kill: () => job.terminate(),
close: () => job.close(),
isMember: (pid) => job.memberPids().includes(pid),
};
}
function processGroup(pid) {
return {
kind: "process-group",
kill: () => {
try {
process.kill(-pid, "SIGKILL");
}
catch {
// The group is already gone.
}
},
close: () => { },
};
}
//# sourceMappingURL=child-containment.js.map