File size: 10,985 Bytes
fc93158 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 | import { spawn } from "node:child_process";
import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import path from "node:path";
import { afterEach, describe, expect, it, vi } from "vitest";
import { createWindowsCmdShimFixture } from "../../../shared/windows-cmd-shim-test-fixtures.js";
import {
resolveSpawnCommand,
spawnAndCollect,
type SpawnCommandCache,
waitForExit,
} from "./process.js";
const tempDirs: string[] = [];
function winRuntime(env: NodeJS.ProcessEnv) {
return {
platform: "win32" as const,
env,
execPath: "C:\\node\\node.exe",
};
}
async function createTempDir(): Promise<string> {
const dir = await mkdtemp(path.join(tmpdir(), "openclaw-acpx-process-test-"));
tempDirs.push(dir);
return dir;
}
afterEach(async () => {
vi.unstubAllEnvs();
while (tempDirs.length > 0) {
const dir = tempDirs.pop();
if (!dir) {
continue;
}
await rm(dir, {
recursive: true,
force: true,
maxRetries: 8,
retryDelay: 8,
});
}
});
describe("resolveSpawnCommand", () => {
it("keeps non-windows spawns unchanged", () => {
const resolved = resolveSpawnCommand(
{
command: "acpx",
args: ["--help"],
},
undefined,
{
platform: "darwin",
env: {},
execPath: "/usr/bin/node",
},
);
expect(resolved).toEqual({
command: "acpx",
args: ["--help"],
});
});
it("routes .js command execution through node on windows", () => {
const resolved = resolveSpawnCommand(
{
command: "C:/tools/acpx/cli.js",
args: ["--help"],
},
undefined,
winRuntime({}),
);
expect(resolved.command).toBe("C:\\node\\node.exe");
expect(resolved.args).toEqual(["C:/tools/acpx/cli.js", "--help"]);
expect(resolved.shell).toBeUndefined();
expect(resolved.windowsHide).toBe(true);
});
it("resolves a .cmd wrapper from PATH and unwraps shim entrypoint", async () => {
const dir = await createTempDir();
const binDir = path.join(dir, "bin");
const scriptPath = path.join(dir, "acpx", "dist", "index.js");
const shimPath = path.join(binDir, "acpx.cmd");
await createWindowsCmdShimFixture({
shimPath,
scriptPath,
shimLine: '"%~dp0\\..\\acpx\\dist\\index.js" %*',
});
const resolved = resolveSpawnCommand(
{
command: "acpx",
args: ["--format", "json", "agent", "status"],
},
undefined,
winRuntime({
PATH: binDir,
PATHEXT: ".CMD;.EXE;.BAT",
}),
);
expect(resolved.command).toBe("C:\\node\\node.exe");
expect(resolved.args[0]).toBe(scriptPath);
expect(resolved.args.slice(1)).toEqual(["--format", "json", "agent", "status"]);
expect(resolved.shell).toBeUndefined();
expect(resolved.windowsHide).toBe(true);
});
it("prefers executable shim targets without shell", async () => {
const dir = await createTempDir();
const wrapperPath = path.join(dir, "acpx.cmd");
const exePath = path.join(dir, "acpx.exe");
await writeFile(exePath, "", "utf8");
await writeFile(wrapperPath, ["@ECHO off", '"%~dp0\\acpx.exe" %*', ""].join("\r\n"), "utf8");
const resolved = resolveSpawnCommand(
{
command: wrapperPath,
args: ["--help"],
},
undefined,
winRuntime({}),
);
expect(resolved).toEqual({
command: exePath,
args: ["--help"],
windowsHide: true,
});
});
it("falls back to shell mode when wrapper cannot be safely unwrapped", async () => {
const dir = await createTempDir();
const wrapperPath = path.join(dir, "custom-wrapper.cmd");
await writeFile(wrapperPath, "@ECHO off\r\necho wrapper\r\n", "utf8");
const resolved = resolveSpawnCommand(
{
command: wrapperPath,
args: ["--arg", "value"],
},
undefined,
winRuntime({}),
);
expect(resolved).toEqual({
command: wrapperPath,
args: ["--arg", "value"],
shell: true,
});
});
it("fails closed in strict mode when wrapper cannot be safely unwrapped", async () => {
const dir = await createTempDir();
const wrapperPath = path.join(dir, "strict-wrapper.cmd");
await writeFile(wrapperPath, "@ECHO off\r\necho wrapper\r\n", "utf8");
expect(() =>
resolveSpawnCommand(
{
command: wrapperPath,
args: ["--arg", "value"],
},
{ strictWindowsCmdWrapper: true },
winRuntime({}),
),
).toThrow(/without shell execution/);
});
it("fails closed for wrapper fallback when args include a malicious cwd payload", async () => {
const dir = await createTempDir();
const wrapperPath = path.join(dir, "strict-wrapper.cmd");
await writeFile(wrapperPath, "@ECHO off\r\necho wrapper\r\n", "utf8");
const payload = "C:\\safe & calc.exe";
const events: Array<{ resolution: string }> = [];
expect(() =>
resolveSpawnCommand(
{
command: wrapperPath,
args: ["--cwd", payload, "agent", "status"],
},
{
strictWindowsCmdWrapper: true,
onResolved: (event) => {
events.push({ resolution: event.resolution });
},
},
winRuntime({}),
),
).toThrow(/without shell execution/);
expect(events).toEqual([{ resolution: "unresolved-wrapper" }]);
});
it("reuses resolved command when cache is provided", async () => {
const dir = await createTempDir();
const wrapperPath = path.join(dir, "acpx.cmd");
const scriptPath = path.join(dir, "acpx", "dist", "index.js");
await createWindowsCmdShimFixture({
shimPath: wrapperPath,
scriptPath,
shimLine: '"%~dp0\\acpx\\dist\\index.js" %*',
});
const cache: SpawnCommandCache = {};
const first = resolveSpawnCommand(
{
command: wrapperPath,
args: ["--help"],
},
{ cache },
winRuntime({}),
);
await rm(scriptPath, { force: true });
const second = resolveSpawnCommand(
{
command: wrapperPath,
args: ["--version"],
},
{ cache },
winRuntime({}),
);
expect(first.command).toBe("C:\\node\\node.exe");
expect(second.command).toBe("C:\\node\\node.exe");
expect(first.args[0]).toBe(scriptPath);
expect(second.args[0]).toBe(scriptPath);
});
});
describe("waitForExit", () => {
it("resolves when the child already exited before waiting starts", async () => {
const child = spawn(process.execPath, ["-e", "process.exit(0)"], {
stdio: ["pipe", "pipe", "pipe"],
});
await new Promise<void>((resolve, reject) => {
child.once("close", () => {
resolve();
});
child.once("error", reject);
});
const exit = await waitForExit(child);
expect(exit.code).toBe(0);
expect(exit.signal).toBeNull();
expect(exit.error).toBeNull();
});
});
describe("spawnAndCollect", () => {
type SpawnedEnvSnapshot = {
openai?: string;
github?: string;
hf?: string;
openclaw?: string;
shell?: string;
};
function stubProviderAuthEnv(env: Record<string, string>) {
for (const [key, value] of Object.entries(env)) {
vi.stubEnv(key, value);
}
}
async function collectSpawnedEnvSnapshot(options?: {
stripProviderAuthEnvVars?: boolean;
openAiEnvKey?: string;
githubEnvKey?: string;
hfEnvKey?: string;
}): Promise<SpawnedEnvSnapshot> {
const openAiEnvKey = options?.openAiEnvKey ?? "OPENAI_API_KEY";
const githubEnvKey = options?.githubEnvKey ?? "GITHUB_TOKEN";
const hfEnvKey = options?.hfEnvKey ?? "HF_TOKEN";
const result = await spawnAndCollect({
command: process.execPath,
args: [
"-e",
`process.stdout.write(JSON.stringify({openai:process.env.${openAiEnvKey},github:process.env.${githubEnvKey},hf:process.env.${hfEnvKey},openclaw:process.env.OPENCLAW_API_KEY,shell:process.env.OPENCLAW_SHELL}))`,
],
cwd: process.cwd(),
stripProviderAuthEnvVars: options?.stripProviderAuthEnvVars,
});
expect(result.code).toBe(0);
expect(result.error).toBeNull();
return JSON.parse(result.stdout) as SpawnedEnvSnapshot;
}
it("returns abort error immediately when signal is already aborted", async () => {
const controller = new AbortController();
controller.abort();
const result = await spawnAndCollect(
{
command: process.execPath,
args: ["-e", "process.exit(0)"],
cwd: process.cwd(),
},
undefined,
{ signal: controller.signal },
);
expect(result.code).toBeNull();
expect(result.error?.name).toBe("AbortError");
});
it("terminates a running process when signal aborts", async () => {
const controller = new AbortController();
const resultPromise = spawnAndCollect(
{
command: process.execPath,
args: ["-e", "setTimeout(() => process.stdout.write('done'), 10_000)"],
cwd: process.cwd(),
},
undefined,
{ signal: controller.signal },
);
setTimeout(() => {
controller.abort();
}, 10);
const result = await resultPromise;
expect(result.error?.name).toBe("AbortError");
});
it("strips shared provider auth env vars from spawned acpx children", async () => {
stubProviderAuthEnv({
OPENAI_API_KEY: "openai-secret",
GITHUB_TOKEN: "gh-secret",
HF_TOKEN: "hf-secret",
OPENCLAW_API_KEY: "keep-me",
});
const parsed = await collectSpawnedEnvSnapshot({
stripProviderAuthEnvVars: true,
});
expect(parsed.openai).toBeUndefined();
expect(parsed.github).toBeUndefined();
expect(parsed.hf).toBeUndefined();
expect(parsed.openclaw).toBe("keep-me");
expect(parsed.shell).toBe("acp");
});
it("strips provider auth env vars case-insensitively", async () => {
stubProviderAuthEnv({
OpenAI_Api_Key: "openai-secret",
Github_Token: "gh-secret",
OPENCLAW_API_KEY: "keep-me",
});
const parsed = await collectSpawnedEnvSnapshot({
stripProviderAuthEnvVars: true,
openAiEnvKey: "OpenAI_Api_Key",
githubEnvKey: "Github_Token",
});
expect(parsed.openai).toBeUndefined();
expect(parsed.github).toBeUndefined();
expect(parsed.openclaw).toBe("keep-me");
expect(parsed.shell).toBe("acp");
});
it("preserves provider auth env vars for explicit custom commands by default", async () => {
stubProviderAuthEnv({
OPENAI_API_KEY: "openai-secret",
GITHUB_TOKEN: "gh-secret",
HF_TOKEN: "hf-secret",
OPENCLAW_API_KEY: "keep-me",
});
const parsed = await collectSpawnedEnvSnapshot();
expect(parsed.openai).toBe("openai-secret");
expect(parsed.github).toBe("gh-secret");
expect(parsed.hf).toBe("hf-secret");
expect(parsed.openclaw).toBe("keep-me");
expect(parsed.shell).toBe("acp");
});
});
|