UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

127 lines • 6.25 kB
import { spawn } from "node:child_process"; import { randomUUID } from "node:crypto"; import { base64Length, DeviceFileError, decodeBase64, encodeBase64, FILE_CHANNEL_MAX_BYTES, } from "@earendil-works/pi-agent-core"; import { holdActivityLock } from "./hand-activity-lock.js"; import { planRun, renderCommand } from "./hand-run-allowlist.js"; import { confinePaths, confinedIndexFile, gitIsSupported, hasUrlRewrites, runDirectory } from "./hand-run-checks.js"; import { transportFor } from "./hand-run-transport.js"; const RUN_TIMEOUT_CAP_MS = 300_000; const DEFAULT_TIMEOUT_MS = 30_000; const KILL_GRACE_MS = 10_000; async function judged(guards, command, nested) { const input = { command }; let decision; try { decision = await guards.beforeToolCall("bash", `run-${randomUUID()}`, input, nested); } catch (error) { throw new DeviceFileError("refused_by_policy", error instanceof Error ? error.message : String(error)); } if (decision?.block === true) throw new DeviceFileError("refused_by_policy", decision.reason ?? "blocked"); if (input.command !== command) throw new DeviceFileError("refused_by_policy", "a tool_call handler rewrote the command"); } /** * One `run` on a hand (S40 PR 2, design v2.2-v2.4): the allowlist's exact argv and environment, the scope's handlers * judging the rendered command as `bash`, the policy checking `cwd` as a write, `GIT_INDEX_FILE` confined, git's version * floor, the activity lock held as fd 3 until the child exits, and no shell. Streams and time are bounded. */ export async function runOnHand(args, deps, signal) { const plan = planRun({ exe: args.exe, argv: args.argv, env: args.env }, deps.context); if (!plan.ok) throw new DeviceFileError("refused_by_policy", plan.reason); if (args.stdin !== undefined && base64Length(args.stdin) > FILE_CHANNEL_MAX_BYTES) throw new DeviceFileError("too_large", "stdin"); const cwd = await runDirectory(deps.root, args.cwd); const verdict = await deps.guards.filesystemPolicy()?.({ operation: "write", canonicalPath: cwd, toolName: "bash" }); if (verdict !== undefined && !verdict.allow) throw new DeviceFileError("refused_by_policy", verdict.reason); const transport = plan.exe === "git" ? transportFor(plan.argv, plan.env, plan.paths) : { argv: plan.argv, env: plan.env, local: false }; const env = { ...transport.env }; if (plan.exe === "git") { if (!(await gitIsSupported(env))) throw new DeviceFileError("unsupported", "git 2.29 or newer is required"); if (transport.local && (await hasUrlRewrites(cwd, env))) throw new DeviceFileError("refused_by_policy", "local_url_rewritten: a url.* rewrite is configured"); } await judged(deps.guards, renderCommand(plan.exe, transport.argv), deps.nested); if (plan.exe === "git") { if (env.GIT_INDEX_FILE !== undefined) env.GIT_INDEX_FILE = await confinedIndexFile(env.GIT_INDEX_FILE, cwd, env); for (const { operation, canonicalPath } of await confinePaths(plan.paths, cwd, env)) { const decision = await deps.guards.filesystemPolicy()?.({ operation, canonicalPath, toolName: "bash" }); if (decision !== undefined && !decision.allow) throw new DeviceFileError("refused_by_policy", decision.reason); } } const hold = args.activityLock === true && plan.exe === "git" ? await holdActivityLock(cwd, deps.flock) : undefined; try { return await spawnBounded(plan.exe, transport.argv, cwd, env, args, hold?.fd, signal); } finally { await hold?.release(); } } function spawnBounded(exe, argv, cwd, env, args, lockFd, signal) { return new Promise((done, fail) => { const child = spawn(exe, [...argv], { cwd, env, shell: false, windowsHide: true, stdio: ["pipe", "pipe", "pipe", lockFd ?? "ignore"], }); const out = []; const err = []; let size = { out: 0, err: 0 }; let overflow = false; let killTimer; const stop = () => { if (child.exitCode === null && child.signalCode === null) child.kill("SIGTERM"); // A grandchild (ssh) may hold the pipes after git exits; destroying them lets the run end at its deadline. killTimer ??= setTimeout(() => { child.kill("SIGKILL"); child.stdout?.destroy(); child.stderr?.destroy(); }, KILL_GRACE_MS); }; const timeout = setTimeout(stop, Math.min(args.timeoutMs ?? DEFAULT_TIMEOUT_MS, RUN_TIMEOUT_CAP_MS)); const onAbort = () => stop(); signal.addEventListener("abort", onAbort, { once: true }); const collect = (into, key) => (chunk) => { size = { ...size, [key]: size[key] + chunk.length }; if (size[key] > FILE_CHANNEL_MAX_BYTES) { overflow = true; stop(); return; } into.push(chunk); }; child.stdout?.on("data", collect(out, "out")); child.stderr?.on("data", collect(err, "err")); child.on("error", (error) => fail(new DeviceFileError("io", error.message))); child.on("close", (code, exitSignal) => { clearTimeout(timeout); if (killTimer !== undefined) clearTimeout(killTimer); signal.removeEventListener("abort", onAbort); if (signal.aborted) return fail(new DeviceFileError("aborted", "the run was aborted")); if (overflow) return fail(new DeviceFileError("too_large", "the run's output passed the limit")); done({ code, signal: exitSignal, stdout: encodeBase64(new Uint8Array(Buffer.concat(out))), stderr: Buffer.concat(err).toString("utf8"), }); }); child.stdin?.on("error", () => undefined); child.stdin?.end(args.stdin === undefined ? undefined : Buffer.from(decodeBase64(args.stdin))); }); } //# sourceMappingURL=hand-run-ops.js.map