@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
223 lines • 10.3 kB
JavaScript
import { Socket } from "node:net";
import { promisify } from "node:util";
import { TOOL_CHILD_ROLE_ENV } from "./channel.js";
import { controlFd, enoentError, goShimScript, shimTarget, withControlPipe } from "./go-shim.js";
import { LAUNCH_SHIM_COMMAND_LINE_ENV, TOOL_CHILD_REENTRY_ENV, toolChildReentry, withoutInternalEnv, } from "./launch-env-names.js";
import { quoteWindowsArgument, windowsCommandLine } from "./windows-command-line.js";
function isArgList(value) {
return Array.isArray(value);
}
/** Reports the launch done exactly once: on exit, or on a spawn error (which may precede the shim's own exit). */
function trackExit(client, launchId, child, reportStart = false) {
if (reportStart && child.pid !== undefined)
client.started(launchId, child.pid);
let reported = false;
const report = (outcome) => {
if (reported)
return;
reported = true;
client.done(launchId, outcome);
};
child.once("exit", (exitCode, signal) => report({ exitCode, signal }));
child.once("error", () => report({ exitCode: 1 }));
return child;
}
function runInherited(client, api, call, statusOf, containment, pidOf = () => undefined) {
const launchId = client.acquire({ origin: "kernel", api, containment, lifetime: "owned" });
client.invoke(launchId);
try {
const value = call();
const pid = containment === "job" ? pidOf(value) : undefined;
if (pid !== undefined)
client.started(launchId, pid);
client.done(launchId, { exitCode: statusOf(value) });
return value;
}
catch (error) {
const status = error instanceof Error && "status" in error && typeof error.status === "number" ? error.status : 1;
client.done(launchId, { exitCode: status });
throw error;
}
}
function ownGroupSpawn(real, client, command, args, options, lifetime) {
const callerStdio = Array.isArray(options.stdio)
? [...options.stdio]
: [options.stdio ?? "pipe", options.stdio ?? "pipe", options.stdio ?? "pipe"];
const fd = controlFd(callerStdio);
const target = shimTarget(command, args, options.shell);
const shim = real.spawn("/bin/sh", ["-c", goShimScript(fd), target.program, ...target.args], {
...options,
shell: false,
detached: true,
stdio: withControlPipe(callerStdio, fd, "ignore", "pipe"),
});
if (shim.pid === undefined)
return shim;
const launchId = client.acquire({
origin: "kernel",
api: "cp.spawn",
containment: "own-group",
lifetime,
pid: shim.pid,
pgid: shim.pid,
});
trackExit(client, launchId, shim);
client.invoke(launchId);
const control = shim.stdio[fd];
if (control instanceof Socket) {
let reply = "";
control.on("data", (chunk) => {
reply += chunk.toString("utf8");
if (reply.startsWith("ENOENT"))
shim.emit("error", enoentError(command, args));
});
// The command closed its copy at exec; the parent's end is only for the go line and the ENOENT reply.
control.on("error", () => { });
shim.once("exit", () => control.destroy());
control.unref();
control.write("go\n");
}
return shim;
}
/**
* Windows `detached: true`: the command must outlive the tool child, so it starts outside the child's Job Object
* through the `launch-shim` role (re-entering this engine exactly as the child was started). The shim reports the
* command's pid on fd 3 and exits with its exit code; the caller gets the shim's handle.
*/
function breakawaySpawn(real, client, command, args, options, reentryRaw) {
const reentry = parseReentry(reentryRaw);
const launchId = client.acquire({
origin: "kernel",
api: "cp.spawn",
containment: "breakaway",
lifetime: "detached",
});
client.invoke(launchId);
const stdio = Array.isArray(options.stdio)
? options.stdio
: [options.stdio ?? "ignore", options.stdio ?? "ignore", options.stdio ?? "ignore"];
const shim = real.spawn(reentry.command, [...reentry.args], {
...options,
shell: false,
detached: false,
env: {
...withoutInternalEnv(options.env ?? process.env),
[TOOL_CHILD_ROLE_ENV]: "launch-shim",
[LAUNCH_SHIM_COMMAND_LINE_ENV]: breakawayCommandLine(command, args, options.shell, options.env ?? process.env),
},
stdio: [stdio[0] ?? "ignore", stdio[1] ?? "ignore", stdio[2] ?? "ignore", "pipe"],
windowsHide: true,
});
const pidPipe = shim.stdio[3];
if (pidPipe !== null && pidPipe !== undefined && "on" in pidPipe) {
let buffered = "";
pidPipe.on("data", (chunk) => {
buffered += chunk.toString("utf8");
const line = buffered.split("\n")[0];
if (line === undefined || !buffered.includes("\n"))
return;
const pid = Number(JSON.parse(line).pid);
if (Number.isSafeInteger(pid))
client.started(launchId, pid, true);
});
}
return trackExit(client, launchId, shim);
}
/**
* The command line the shim starts, by Node's own rules for `shell` on Windows: a shell named `cmd` or `cmd.exe` gets
* `/d /s /c "<command>"` with the command passed through verbatim (cmd.exe does not understand the C runtime's `\"`
* escaping); any other shell gets `-c <command>` with normal quoting. `shell: true` means `%ComSpec%`, else cmd.exe.
*/
export function breakawayCommandLine(command, args, shell, env = process.env) {
if (!shell)
return windowsCommandLine([command, ...args]);
const program = typeof shell === "string" ? shell : (env.ComSpec ?? "cmd.exe");
const line = [command, ...args].join(" ");
if (/^(?:.*\\)?cmd(?:\.exe)?$/i.test(program))
return `${quoteWindowsArgument(program)} /d /s /c "${line}"`;
return windowsCommandLine([program, "-c", line]);
}
function parseReentry(raw) {
const parsed = raw === undefined ? undefined : JSON.parse(raw);
if (typeof parsed !== "object" ||
parsed === null ||
typeof Reflect.get(parsed, "command") !== "string" ||
!Array.isArray(Reflect.get(parsed, "args")))
throw new Error(`tool_child_breakaway_failed: ${TOOL_CHILD_REENTRY_ENV} is missing or malformed`);
const args = Reflect.get(parsed, "args");
return {
command: String(Reflect.get(parsed, "command")),
args: Array.isArray(args) ? Array.from(args, String) : [],
};
}
/**
* Node's `exec` / `execFile` carry a `util.promisify.custom` that resolves `{ stdout, stderr }`; a gated replacement
* must too, or `promisify(exec)` silently resolves the stdout string alone. It calls the gated function, so the promised
* form passes the barrier like the callback form, and like Node it rejects with `stdout`/`stderr` on the error and
* exposes the process as `.child`.
*/
function withPromisified(gated) {
const promised = (...args) => {
let child;
const result = new Promise((resolve, reject) => {
child = Reflect.apply(gated, undefined, [
...args,
(error, stdout, stderr) => error ? reject(Object.assign(error, { stdout, stderr })) : resolve({ stdout, stderr }),
]);
});
return Object.assign(result, { child });
};
Object.defineProperty(gated, promisify.custom, { value: promised });
return gated;
}
/** A `node:child_process` module object whose launch APIs pass the execution barrier. Non-launch exports pass through. */
export function createGatedChildProcess(module, client, platform = process.platform, reentry = toolChildReentry()) {
// Captured before anyone patches `module` in place: the gated functions must call the originals, never themselves.
const real = {
...module,
spawn: module.spawn,
spawnSync: module.spawnSync,
exec: module.exec,
execSync: module.execSync,
execFile: module.execFile,
execFileSync: module.execFileSync,
fork: module.fork,
};
const posix = platform !== "win32";
// Windows: every command the child starts inherits its Job Object, so the containment is the job.
const inherited = posix ? "inherited" : "job";
const spawn = (command, argsOrOptions, maybeOptions) => {
const args = isArgList(argsOrOptions) ? argsOrOptions : [];
const options = (isArgList(argsOrOptions) ? maybeOptions : argsOrOptions) ?? {};
if (!posix && options.detached === true)
return breakawaySpawn(real, client, command, args, options, reentry);
if (posix && options.detached !== false)
return ownGroupSpawn(real, client, command, args, options, options.detached === true ? "detached" : "owned");
const launchId = client.acquire({
origin: "kernel",
api: "cp.spawn",
containment: inherited,
lifetime: "owned",
});
client.invoke(launchId);
return trackExit(client, launchId, real.spawn(command, [...args], options), !posix);
};
const asyncInherited = (api, original) => (...args) => {
const launchId = client.acquire({ origin: "kernel", api, containment: inherited, lifetime: "owned" });
client.invoke(launchId);
return trackExit(client, launchId, original(...args), !posix);
};
const gated = {
...real,
spawn,
exec: withPromisified(asyncInherited("cp.spawn", real.exec)),
execFile: withPromisified(asyncInherited("cp.spawn", real.execFile)),
fork: asyncInherited("cp.spawn", real.fork),
spawnSync: (...args) => runInherited(client, "cp.spawnSync", () => real.spawnSync(...args), (result) => result.status, inherited, (result) => result.pid),
execSync: (...args) => runInherited(client, "cp.execFileSync", () => real.execSync(...args), () => 0, inherited),
execFileSync: (...args) => runInherited(client, "cp.execFileSync", () => real.execFileSync(...args), () => 0, inherited),
};
gated.default = gated;
return gated;
}
//# sourceMappingURL=gated-child-process.js.map