@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
129 lines • 5.24 kB
JavaScript
import { posixSignalTable } from "./posix-signals.js";
const MAX_TIMEOUT_MS = 2_147_483_647;
const MAX_TIMEOUT_SECONDS = MAX_TIMEOUT_MS / 1000;
function resolveTimeoutMs(timeout) {
if (timeout === undefined)
return undefined;
if (!Number.isFinite(timeout) || timeout <= 0) {
throw new Error("Invalid timeout: must be a finite number of seconds");
}
const timeoutMs = timeout * 1000;
if (timeoutMs > MAX_TIMEOUT_MS) {
throw new Error(`Invalid timeout: maximum is ${MAX_TIMEOUT_SECONDS} seconds`);
}
return timeoutMs;
}
/**
* A host that knows only the tool machine: the machine's shell and environment, the cwd checked there, and the signal
* numbers of the machine's own OS.
*/
export function machineShellExecHost(machine, shell) {
return {
resolveShell: () => shell,
checkCwd: async (cwd, signal) => {
await machine.ops.files.stat({ path: cwd }, machine.context(signal));
},
envMode: "overlay",
defaultEnv: () => undefined,
signalNumber: (signal) => posixSignalTable(machine.ops.info.host().os)[signal],
};
}
/**
* Shell execution used by the built-in shell tools: one `process.exec` of the operation layer (own process group,
* abort/timeout kill the group). The tool keeps its own output handling.
*/
export function createShellOperations(shellName, machine, host) {
return {
exec: async (command, cwd, { onData, signal, timeout, env, sessionId }) => {
const timeoutMs = resolveTimeoutMs(timeout);
if (signal?.aborted) {
throw new Error("aborted");
}
const shell = host.resolveShell();
try {
await host.checkCwd(cwd, signal);
}
catch {
throw new Error(`Working directory does not exist: ${cwd}\nCannot execute ${shellName} commands.`);
}
let streamCallbackError;
let hasStreamCallbackError = false;
// A failing output callback stops the command the same way an abort does.
const callbackAbort = new AbortController();
const failStream = (error) => {
if (hasStreamCallbackError)
return;
streamCallbackError = error;
hasStreamCallbackError = true;
callbackAbort.abort();
};
const handleData = (data, stream) => {
if (hasStreamCallbackError)
return;
try {
const callbackResult = onData(data, stream);
if (callbackResult && typeof callbackResult.then === "function") {
void Promise.resolve(callbackResult).catch(failStream);
}
}
catch (error) {
failStream(error);
}
};
const execSignal = signal ? AbortSignal.any([signal, callbackAbort.signal]) : callbackAbort.signal;
const attest = host.attestSessionSpawn;
let attestation;
const onSpawn = attest === undefined || sessionId === undefined
? host.onSpawn
: (pid) => {
attestation = attest(sessionId, pid);
host.onSpawn?.(pid);
};
const onExit = attest === undefined || sessionId === undefined
? host.onExit
: (pid) => {
void attestation?.release();
host.onExit?.(pid);
};
let result;
try {
result = await machine.ops.process.exec({
command,
cwd,
env: env ?? host.defaultEnv(),
envMode: host.envMode,
...(shell !== undefined ? { shell } : {}),
timeoutMs: timeoutMs ?? Number.POSITIVE_INFINITY,
onOutput: handleData,
...(onSpawn !== undefined ? { onSpawn } : {}),
...(onExit !== undefined ? { onExit } : {}),
}, machine.context(execSignal));
}
catch (error) {
if (hasStreamCallbackError)
throw streamCallbackError;
if (signal?.aborted)
throw new Error("aborted");
throw error;
}
finally {
host.afterExec?.();
}
if (hasStreamCallbackError) {
throw streamCallbackError;
}
if (signal?.aborted) {
throw new Error("aborted");
}
if (result.timedOut) {
throw new Error(`timeout:${timeout}`);
}
// A signal-killed shell has no exit code. Use the standard shell convention so
// callers do not mistake the termination for a successful command.
return {
exitCode: result.exitCode ?? (result.signal ? 128 + (host.signalNumber(result.signal) ?? 0) : 1),
};
},
};
}
//# sourceMappingURL=shell-operations.js.map