@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
65 lines • 2.97 kB
JavaScript
import { spawnSync } from "node:child_process";
import { judgeArgv, judgeCommand, judgeSignal } from "./command.js";
import { createProcessView, spawnSyncFromNode } from "./process-view.js";
import { refuse } from "./verdict.js";
const isRecord = (value) => typeof value === "object" && value !== null;
/** Parses a bridge request; `undefined` when it is not one of the three shapes. */
function parseKillGuardRequest(value) {
if (!isRecord(value))
return undefined;
const vantagePid = typeof value.vantagePid === "number" && Number.isInteger(value.vantagePid) ? value.vantagePid : undefined;
const extra = vantagePid === undefined ? {} : { vantagePid };
if (value.kind === "command" && typeof value.command === "string")
return { kind: "command", command: value.command, ...extra };
if (value.kind === "argv" && Array.isArray(value.argv) && value.argv.every((word) => typeof word === "string")) {
return { kind: "argv", argv: value.argv, ...extra };
}
const signal = value.signal;
if (value.kind === "signal" &&
typeof value.target === "number" &&
typeof value.group === "boolean" &&
(typeof signal === "number" || typeof signal === "string" || signal === null)) {
return { kind: "signal", target: value.target, group: value.group, signal, ...extra };
}
return undefined;
}
function vantageOf(request, agentPid) {
if (request.vantagePid === undefined)
return { kind: "fresh-session" };
return request.vantagePid === agentPid ? { kind: "agent" } : { kind: "process", pid: request.vantagePid };
}
/** The host side of the kernel bridge's kill-guard check (E7, E8): decides one request from its kernel's vantage. */
export function decideKillGuardRequest(value, options) {
const request = parseKillGuardRequest(value);
if (request === undefined)
return refuse("R5", "the kill-guard check was malformed");
const platform = options.platform ?? process.platform;
let read = false;
let view;
const readView = options.view ??
(() => {
if (!read) {
read = true;
view = createProcessView(platform, spawnSyncFromNode(spawnSync));
}
return view;
});
const shell = request.kind === "command" ? "sh" : "argv";
const context = {
platform,
agentPid: options.agentPid,
...(options.agentPpid === undefined ? {} : { agentPpid: options.agentPpid }),
vantage: vantageOf(request, options.agentPid),
shell,
view: readView,
};
switch (request.kind) {
case "command":
return judgeCommand(request.command, context);
case "argv":
return judgeArgv(request.argv, context);
case "signal":
return judgeSignal({ target: request.target, group: request.group, signal: request.signal ?? undefined }, context);
}
}
//# sourceMappingURL=request.js.map