Skip to content

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

3.7 KB
Raw Blame
import { mkdtemp, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { delimiter, dirname, join } from "node:path";
import { execPath } from "node:process";
import { RpcClientError, rpcClientErrorFromResponse } from "@vetta/coding-agent/rpc";
import { describe, expect, it } from "vitest";
import { RpcClient, resolveRpcClientProcessLaunch } from "../src/rpc/rpc-client.js";

function nodeRuntimeEnv(): Record<string, string> {
	const pathKey = Object.keys(process.env).find((key) => key.toLowerCase() === "path") ?? "PATH";
	const currentPath = process.env[pathKey];
	return { [pathKey]: [dirname(execPath), currentPath].filter(Boolean).join(delimiter) };
}

describe("RpcClient process launch", () => {
	it("uses the canonical installed RPC executable by default", () => {
		expect(resolveRpcClientProcessLaunch(undefined, ["--mode", "rpc"])).toEqual({
			command: "vetta-agent-rpc",
			args: ["--mode", "rpc"],
		});
	});

	it("preserves explicit JavaScript entry paths for compatibility", () => {
		expect(resolveRpcClientProcessLaunch("custom-agent.js", ["--mode", "rpc"])).toEqual({
			command: "node",
			args: ["custom-agent.js", "--mode", "rpc"],
		});
	});

	it("preserves structured failure metadata when a command rejects", () => {
		const error = rpcClientErrorFromResponse({
			id: "prompt-1",
			type: "response",
			command: "prompt",
			success: false,
			error: "provider failed",
			errorCode: "provider_unavailable",
			phase: "turn",
			recoverability: "continue_session",
		});

		expect(error).toBeInstanceOf(RpcClientError);
		expect(error).toMatchObject({
			message: "provider failed",
			command: "prompt",
			errorCode: "provider_unavailable",
			phase: "turn",
			recoverability: "continue_session",
		});
	});

	it("rejects event waits immediately on process exit and permits an explicit restart", async () => {
		const fixtureDir = await mkdtemp(join(tmpdir(), "rpc-client-exit-"));
		const fixturePath = join(fixtureDir, "exit.mjs");
		await writeFile(fixturePath, "setTimeout(() => process.exit(9), 300);\n", "utf8");
		const client = new RpcClient({ cliPath: fixturePath, env: nodeRuntimeEnv() });

		try {
			await client.start();
			const startedAt = Date.now();
			await expect(client.collectEvents(5_000)).rejects.toMatchObject({
				errorCode: "process_exited",
				phase: "turn",
				recoverability: "restart_session",
			});
			expect(Date.now() - startedAt).toBeLessThan(2_000);

			await client.start();
			await client.stop();
		} finally {
			await client.stop();
			await rm(fixtureDir, { recursive: true, force: true });
		}
	});

	it("rejects failed responses for commands without response data", async () => {
		const fixtureDir = await mkdtemp(join(tmpdir(), "rpc-client-failure-"));
		const fixturePath = join(fixtureDir, "failure.mjs");
		await writeFile(
			fixturePath,
			`import { createInterface } from "node:readline";
const lines = createInterface({ input: process.stdin });
lines.on("line", (line) => {
  const command = JSON.parse(line);
  process.stdout.write(JSON.stringify({
    id: command.id,
    type: "response",
    command: command.type,
    success: false,
    error: "prompt was rejected",
    errorCode: "provider_unavailable",
    phase: "turn",
    recoverability: "continue_session"
  }) + "\\n");
});
`,
			"utf8",
		);
		const client = new RpcClient({ cliPath: fixturePath, env: nodeRuntimeEnv() });

		try {
			await client.start();
			await expect(client.prompt("hello")).rejects.toMatchObject({
				message: "prompt was rejected",
				command: "prompt",
				errorCode: "provider_unavailable",
				phase: "turn",
				recoverability: "continue_session",
			});
		} finally {
			await client.stop();
			await rm(fixtureDir, { recursive: true, force: true });
		}
	});
});