import { describe, expect, it } from "vitest"; import { chmodSync, closeSync, lstatSync, mkdtempSync, mkdirSync, openSync, rmSync, writeFileSync, } from "node:fs"; import { spawn, spawnSync } from "node:child_process"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { TRUSTED_OPENCODE_EXECUTABLE_ARG, trustedOpenCodeLaunchBinding, withoutAmbientOpenCodeCommand, } from "./opencode-proxy-command.js"; import { openCodeProxyTaskEnvelope } from "./opencode-proxy-task-envelope.js"; describe("OpenCode runnerd proxy executable", () => { it("uses the runner-owned inherited executable descriptor", () => { if (process.platform !== "linux") return; expect(trustedOpenCodeLaunchBinding([ TRUSTED_OPENCODE_EXECUTABLE_ARG, "/proc/self/fd/7", ])).toEqual({ command: "/proc/self/fd/7", commandFd: 7 }); }); it("executes the inherited artifact through the nested child fd mapping", async () => { if (process.platform === "win32") return; if (process.platform === "darwin") { const root = mkdtempSync(join(tmpdir(), "paperclip-opencode-nested-")); const directory = join( root, ".paperclip-verified-executable-0123456789abcdef0123456789abcdef", ); const command = join(directory, "launch"); try { mkdirSync(directory, { mode: 0o700 }); writeFileSync(command, "#!/bin/sh\nprintf verified-nested-spawn\n"); chmodSync(command, 0o500); const binding = trustedOpenCodeLaunchBinding([ TRUSTED_OPENCODE_EXECUTABLE_ARG, command, ]); for (let attempt = 0; attempt < 2; attempt += 1) { binding.commandLifecycle!.beforeSpawn(); const child = spawn(binding.command, [], { stdio: ["ignore", "pipe", "pipe"] }); let stdout = ""; child.stdout!.on("data", chunk => { stdout += chunk; }); const exited = new Promise((resolve, reject) => { child.once("error", reject); child.once("close", resolve); }); binding.commandLifecycle!.afterSpawn(); // Exercise the production async boundary, not spawnSync followed by // cleanup after the executable has already finished. expect(lstatSync(command).isFile()).toBe(true); expect(() => binding.commandLifecycle!.beforeSpawn()).toThrow("still in use"); expect(await exited).toBe(0); expect(stdout).toBe("verified-nested-spawn"); binding.commandLifecycle!.afterExit(); binding.commandLifecycle!.afterExit(); expect(() => lstatSync(command)).toThrow(); expect(() => lstatSync(directory)).toThrow(); } } finally { rmSync(root, { recursive: true, force: true }); } return; } const parentFd = openSync(process.execPath, "r"); try { const binding = trustedOpenCodeLaunchBinding([ TRUSTED_OPENCODE_EXECUTABLE_ARG, `/proc/self/fd/${parentFd}`, ]); const stdio: Array<"ignore" | "pipe" | number> = [ "ignore", "pipe", "pipe", ]; while (stdio.length <= binding.commandFd!) stdio.push("ignore"); stdio[binding.commandFd!] = binding.commandFd!; const child = spawnSync( binding.command, ["-e", "process.stdout.write('verified-nested-spawn')"], { encoding: "utf8", stdio, }, ); expect(child.error).toBeUndefined(); expect(child.status).toBe(0); expect(child.stdout).toBe("verified-nested-spawn"); } finally { closeSync(parentFd); } }); it("fails closed instead of accepting an ambient command fallback", () => { expect(() => trustedOpenCodeLaunchBinding([])) .toThrow("refusing ambient PATH or PAPERCLIP_OPENCODE_COMMAND fallback"); expect(() => trustedOpenCodeLaunchBinding([ TRUSTED_OPENCODE_EXECUTABLE_ARG, "/tmp/unqualified-opencode", ])).toThrow("runner-owned executable binding is unavailable"); expect(() => trustedOpenCodeLaunchBinding([ TRUSTED_OPENCODE_EXECUTABLE_ARG, "/proc/self/fd/7", "unexpected", ])).toThrow("runner-owned executable binding is unavailable"); }); it("removes the ambient override from the launched provider environment", () => { const original = { OPENROUTER_API_KEY: "secret", PAPERCLIP_OPENCODE_COMMAND: "/tmp/unqualified-opencode", }; expect(withoutAmbientOpenCodeCommand(original)).toEqual({ OPENROUTER_API_KEY: "secret", }); expect(original.PAPERCLIP_OPENCODE_COMMAND).toBe("/tmp/unqualified-opencode"); }); }); describe("OpenCode runnerd proxy task envelope", () => { it("uses the durable completion-contract binding instead of a demo revision", () => { expect(openCodeProxyTaskEnvelope({ baseInstructions: "Complete only this task.", completionContract: { revision: "17", criterionIds: ["objective", "verification"], }, })).toMatchObject({ constraints: ["Use Paperclip MCP tools for semantic operations."], completionContract: { revision: "17", criteria: [ { id: "objective" }, { id: "verification" }, ], }, }); }); });