Skip to content

Open-source, local-first AI agent for real work.

8.6 KB
Raw Blame
import { spawn } from "node:child_process";
import { randomBytes } from "node:crypto";
import { appendFileSync, createWriteStream, existsSync, readFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { setImmediate } from "node:timers";
import type { ForegroundCommandOperations } from "@vetta/runtime-tools";
import type {
	BackgroundCommandHost,
	BackgroundCommandOutputStore,
	BackgroundCommandProcessOperations,
	DetachedProcessRegistry,
} from "../shared/background-command-host.js";
import type { ForegroundCommandExecutorOptions } from "../shared/foreground-command-executor.js";
import { isProcessGroupAlive, isSameProcessGroup, watchDetachedProcessGroup } from "./detached-process-group.js";
import { killNodeProcessTree } from "./process-tree.js";

export interface NodeShellCommand {
	readonly executable: string;
	readonly args: readonly string[];
	readonly commandPrefix?: string;
}

export interface NodeForegroundCommandHostOptions {
	readonly resolveShell: () => NodeShellCommand;
	readonly environment?: () => NodeJS.ProcessEnv;
	readonly protectedDirectories?: readonly string[];
}

export interface NodeBackgroundCommandHostOptions {
	readonly resolveShell: () => NodeShellCommand;
	readonly normalizeOutput?: (value: string) => string;
	/**
	 * Where adopted process groups are remembered so a later host process can show them again.
	 * Without it they are still adopted, but forgotten when the session ends.
	 */
	readonly detachedProcessRegistry?: DetachedProcessRegistry;
	/** How long leftover processes must outlive the shell before they are adopted. */
	readonly detachedGraceMs?: number;
	readonly detachedPollMs?: number;
}

const DEFAULT_DETACHED_GRACE_MS = 500;

export function createNodeForegroundCommandHost(
	options: NodeForegroundCommandHostOptions,
): ForegroundCommandExecutorOptions {
	return {
		operations: createForegroundOperations(options.resolveShell, options.environment),
		environment: options.environment,
		protectedDirectories: options.protectedDirectories,
		commandPrefix: options.resolveShell().commandPrefix,
	};
}

export function createNodeBackgroundCommandHost(options: NodeBackgroundCommandHostOptions): BackgroundCommandHost {
	return {
		processOperations: createBackgroundProcessOperations(options),
		outputStore: localBackgroundCommandOutputStore,
		...(options.detachedProcessRegistry ? { detachedProcessRegistry: options.detachedProcessRegistry } : {}),
	};
}

function createForegroundOperations(
	resolveShell: () => NodeShellCommand,
	readEnvironment: (() => NodeJS.ProcessEnv) | undefined,
): ForegroundCommandOperations {
	return {
		exec(command, cwd, { onData, signal, timeout, env }) {
			return new Promise((resolveExecution, rejectExecution) => {
				if (!existsSync(cwd)) {
					rejectExecution(new Error(`Working directory does not exist: ${cwd}\nCannot execute bash commands.`));
					return;
				}
				const shell = resolveShell();
				const child = spawn(shell.executable, [...shell.args, command], {
					cwd,
					detached: process.platform !== "win32",
					env: { ...(env ?? readEnvironment?.() ?? process.env) },
					stdio: ["ignore", "pipe", "pipe"],
					windowsHide: true,
				});
				let timedOut = false;
				let settled = false;
				let timeoutHandle: NodeJS.Timeout | undefined;
				const cleanup = () => {
					if (timeoutHandle) clearTimeout(timeoutHandle);
					signal?.removeEventListener("abort", onAbort);
					child.stdout?.destroy();
					child.stderr?.destroy();
				};
				const resolveOnce = (exitCode: number | null) => {
					if (settled) return;
					settled = true;
					cleanup();
					resolveExecution({ exitCode });
				};
				const rejectOnce = (error: Error) => {
					if (settled) return;
					settled = true;
					cleanup();
					rejectExecution(error);
				};
				const stop = () => {
					if (child.pid) killNodeProcessTree(child.pid);
					else child.kill();
				};
				const onAbort = () => {
					stop();
					rejectOnce(new Error("aborted"));
				};

				if (timeout !== undefined && timeout > 0) {
					timeoutHandle = setTimeout(() => {
						timedOut = true;
						stop();
						rejectOnce(new Error(`timeout:${timeout}`));
					}, timeout * 1_000);
				}
				child.stdout?.on("data", onData);
				child.stderr?.on("data", onData);
				child.once("error", rejectOnce);
				if (signal?.aborted) onAbort();
				else signal?.addEventListener("abort", onAbort, { once: true });
				child.once("exit", (exitCode) => {
					if (signal?.aborted || timedOut) return;
					// Daemonizing commands can leave descendants holding the inherited
					// output pipes after the command shell itself exits. Flush queued data
					// for one turn, then settle from `exit` instead of waiting forever for
					// `close`; `close` remains the fast path for ordinary commands.
					setImmediate(() => resolveOnce(exitCode));
				});
				child.once("close", (exitCode) => {
					if (signal?.aborted) rejectOnce(new Error("aborted"));
					else if (timedOut) rejectOnce(new Error(`timeout:${timeout}`));
					else resolveOnce(exitCode);
				});
			});
		},
	};
}

function createBackgroundProcessOperations(
	options: NodeBackgroundCommandHostOptions,
): BackgroundCommandProcessOperations {
	return {
		spawn(request) {
			const shell = options.resolveShell();
			const child = spawn(shell.executable, [...shell.args, request.command], {
				cwd: request.cwd,
				detached: process.platform !== "win32",
				env: request.env,
				stdio: ["ignore", "pipe", "pipe"],
				windowsHide: true,
			});
			const decoder = new TextDecoder();
			// Output goes to the command task, then to the adopted group once the shell is gone.
			let deliverOutput = request.onOutput;
			const onData = (data: Buffer): void => {
				const decoded = decoder.decode(data, { stream: true });
				const text = (options.normalizeOutput?.(decoded) ?? decoded).replaceAll("\r", "");
				if (text) deliverOutput(text);
			};
			let settled = false;
			let stopping = false;
			const cleanup = (): void => {
				child.stdout?.destroy();
				child.stderr?.destroy();
			};
			const completeExit = (exitCode: number | null): void => {
				const processGroupId = child.pid;
				if (
					stopping ||
					!request.onDetached ||
					processGroupId === undefined ||
					!isProcessGroupAlive(processGroupId)
				) {
					cleanup();
					request.onExit(exitCode ?? undefined);
					return;
				}
				// Stragglers that exit right after the shell are not worth a task of their own.
				const grace = setTimeout(() => {
					if (stopping || !isProcessGroupAlive(processGroupId)) {
						cleanup();
						request.onExit(exitCode ?? undefined);
						return;
					}
					const group = watchDetachedProcessGroup(processGroupId, Date.now(), {
						pollMs: options.detachedPollMs,
						// Keep draining inherited pipes: closing them would SIGPIPE a group that still prints.
						onRelease: () => {
							deliverOutput = () => {};
						},
					});
					deliverOutput = (text) => group.push(text);
					request.onDetached?.(group);
					request.onExit(exitCode ?? undefined);
				}, options.detachedGraceMs ?? DEFAULT_DETACHED_GRACE_MS);
				grace.unref?.();
			};
			const settleExit = (exitCode: number | null): void => {
				if (settled) return;
				settled = true;
				completeExit(exitCode);
			};
			const settleError = (error: Error): void => {
				if (settled) return;
				settled = true;
				cleanup();
				request.onError(error);
			};
			child.stdout?.on("data", onData);
			child.stderr?.on("data", onData);
			child.once("exit", (exitCode) => setImmediate(() => settleExit(exitCode)));
			child.once("close", settleExit);
			child.once("error", settleError);
			return {
				stop() {
					stopping = true;
					if (child.pid) killNodeProcessTree(child.pid);
					else child.kill();
				},
			};
		},
		async reattach(record) {
			if (process.platform === "win32") return undefined;
			if (!(await isSameProcessGroup(record.processGroupId, record.adoptedAt))) return undefined;
			return watchDetachedProcessGroup(record.processGroupId, record.adoptedAt, { pollMs: options.detachedPollMs });
		},
	};
}

const localBackgroundCommandOutputStore: BackgroundCommandOutputStore = {
	create(taskId) {
		const path = join(tmpdir(), `vetta-task-${taskId}-${randomBytes(4).toString("hex")}.log`);
		const stream = createWriteStream(path);
		return {
			path,
			append: (text) => stream.write(text),
			read: (offset) => readFileSync(path).subarray(offset).toString("utf-8"),
			close: () => stream.end(),
		};
	},
	open(path) {
		return {
			path,
			append: (text) => {
				try {
					appendFileSync(path, text);
				} catch {
					// The earlier output may have been cleaned from the temp directory.
				}
			},
			read: (offset) => readFileSync(path).subarray(offset).toString("utf-8"),
			close: () => {},
		};
	},
};