Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

fix(cli): quote spawn args on Windows so paths with spaces survive cmd.exe

Gus689a657fa6a5
diff --git a/src/cli/daemon/agent/__tests__/win-quote.test.ts b/src/cli/daemon/agent/__tests__/win-quote.test.ts
new file mode 100644
index 000000000..d91cce059
--- /dev/null
+++ b/src/cli/daemon/agent/__tests__/win-quote.test.ts
@@ -0,0 +1,69 @@
+import { describe, it, expect } from "vitest";
+import { quoteWinArg, quoteWinArgs } from "../win-quote.js";
+
+describe("quoteWinArg", () => {
+ it("leaves simple tokens untouched", () => {
+ expect(quoteWinArg("simple")).toBe("simple");
+ expect(quoteWinArg("--flag")).toBe("--flag");
+ expect(quoteWinArg("value123")).toBe("value123");
+ });
+
+ it("wraps paths containing spaces", () => {
+ expect(quoteWinArg("C:\\Users\\John Doe\\.alook")).toBe(
+ "\"C:\\Users\\John Doe\\.alook\"",
+ );
+ });
+
+ it("escapes embedded double quotes", () => {
+ expect(quoteWinArg('say "hi"')).toBe('"say \\"hi\\""');
+ });
+
+ it("doubles trailing backslashes before the closing quote when quoting", () => {
+ // Only relevant when we DO quote — a bare `trailing\` needs no quoting so
+ // MSVCRT parses it as-is. But `path with space\` must be quoted, and the
+ // trailing backslash must be doubled so MSVCRT doesn't read the closing
+ // quote as escaped.
+ expect(quoteWinArg("path with\\")).toBe("\"path with\\\\\"");
+ expect(quoteWinArg("has space\\\\")).toBe("\"has space\\\\\\\\\"");
+ expect(quoteWinArg("trailing\\")).toBe("trailing\\");
+ });
+
+ it("preserves backslashes that don't precede a quote", () => {
+ expect(quoteWinArg("a\\b c")).toBe("\"a\\b c\"");
+ });
+
+ it("doubles a backslash run that precedes an embedded quote", () => {
+ // MSVCRT rule: N backslashes before a `"` become 2N+1 backslashes + `"`.
+ // Two backslashes + `"` → four backslashes + escaped quote.
+ expect(quoteWinArg('a\\\\"b c')).toBe('"a\\\\\\\\\\"b c"');
+ // One backslash + `"` → two backslashes + escaped quote.
+ expect(quoteWinArg('a\\"b c')).toBe('"a\\\\\\"b c"');
+ });
+
+ it("quotes args containing cmd metachars", () => {
+ expect(quoteWinArg("a&b")).toBe("\"a&b\"");
+ expect(quoteWinArg("a|b")).toBe("\"a|b\"");
+ expect(quoteWinArg("a>b")).toBe("\"a>b\"");
+ expect(quoteWinArg("100%")).toBe("\"100%\"");
+ });
+
+ it("quotes empty string", () => {
+ expect(quoteWinArg("")).toBe("\"\"");
+ });
+
+ it("maps arrays through quoteWinArgs", () => {
+ expect(
+ quoteWinArgs([
+ "run",
+ "--dir",
+ "C:\\Users\\John Doe\\.alook\\workspaces",
+ "hello world",
+ ]),
+ ).toEqual([
+ "run",
+ "--dir",
+ "\"C:\\Users\\John Doe\\.alook\\workspaces\"",
+ "\"hello world\"",
+ ]);
+ });
+});
diff --git a/src/cli/daemon/agent/claude.ts b/src/cli/daemon/agent/claude.ts
index dcfad2f8c..bcf2ede09 100644
--- a/src/cli/daemon/agent/claude.ts
+++ b/src/cli/daemon/agent/claude.ts
@@ -12,6 +12,7 @@ import type {
EncodeOpts,
} from "../types.js";
import { killProcessTree } from "../kill-tree.js";
+import { quoteWinArg, quoteWinArgs } from "./win-quote.js";
export class ClaudeBackend implements AgentBackend {
name = "claude";
@@ -176,11 +177,15 @@ export class ClaudeBackend implements AgentBackend {
args.push("--resume", options.resumeSessionId);
}
- const proc = spawn(this.cliPath, args, {
+ const isWin = process.platform === "win32";
+ const spawnCmd = isWin ? quoteWinArg(this.cliPath) : this.cliPath;
+ const spawnArgs = isWin ? quoteWinArgs(args) : args;
+
+ const proc = spawn(spawnCmd, spawnArgs, {
cwd: options.cwd,
stdio: ["pipe", "pipe", "pipe"],
env: { ...process.env, ...options.env },
- shell: process.platform === "win32",
+ shell: isWin,
windowsHide: true,
// POSIX: own process group (pgid === pid) so the session-runner can reap
// the CLI *and* its tool/MCP subprocesses via a group kill. No unref() —
diff --git a/src/cli/daemon/agent/codex.ts b/src/cli/daemon/agent/codex.ts
index 5bdf3fc14..2fff239cf 100644
--- a/src/cli/daemon/agent/codex.ts
+++ b/src/cli/daemon/agent/codex.ts
@@ -12,6 +12,7 @@ import type {
EncodeOpts,
} from "../types.js";
import { killProcessTree } from "../kill-tree.js";
+import { quoteWinArg, quoteWinArgs } from "./win-quote.js";
interface JsonRpcMessage {
jsonrpc: "2.0";
@@ -258,11 +259,16 @@ export class CodexBackend implements AgentBackend {
}
execute(prompt: string, options: ExecOptions): AgentSession {
- const proc = spawn(this.cliPath, ["app-server", "--listen", "stdio://", "--config", "sandbox_mode=danger-full-access"], {
+ const isWin = process.platform === "win32";
+ const rawArgs = ["app-server", "--listen", "stdio://", "--config", "sandbox_mode=danger-full-access"];
+ const spawnCmd = isWin ? quoteWinArg(this.cliPath) : this.cliPath;
+ const spawnArgs = isWin ? quoteWinArgs(rawArgs) : rawArgs;
+
+ const proc = spawn(spawnCmd, spawnArgs, {
cwd: options.cwd,
stdio: ["pipe", "pipe", "pipe"],
env: { ...process.env, ...options.env },
- shell: process.platform === "win32",
+ shell: isWin,
windowsHide: true,
// POSIX: own process group (pgid === pid) so the session-runner can reap
// the CLI *and* its spawned workers via a group kill. No unref() — we keep
diff --git a/src/cli/daemon/agent/opencode.ts b/src/cli/daemon/agent/opencode.ts
index 4c253c973..f714bbc77 100644
--- a/src/cli/daemon/agent/opencode.ts
+++ b/src/cli/daemon/agent/opencode.ts
@@ -10,6 +10,7 @@ import type {
BusyDeliveryMode,
} from "../types.js";
import { killProcessTree } from "../kill-tree.js";
+import { quoteWinArg, quoteWinArgs } from "./win-quote.js";
export class OpenCodeBackend implements AgentBackend {
name = "opencode";
@@ -137,11 +138,18 @@ export class OpenCodeBackend implements AgentBackend {
// User prompt as positional argument (no flag)
args.push(prompt);
- const proc = spawn(this.cliPath, args, {
+ // spawn({ shell: true }) on Windows joins args with spaces without quoting
+ // — see win-quote.ts. Pre-quote so args with spaces (workspace paths under
+ // C:\Users\<name with space>\.alook\...) survive cmd.exe parsing.
+ const isWin = process.platform === "win32";
+ const spawnCmd = isWin ? quoteWinArg(this.cliPath) : this.cliPath;
+ const spawnArgs = isWin ? quoteWinArgs(args) : args;
+
+ const proc = spawn(spawnCmd, spawnArgs, {
cwd: options.cwd,
stdio: ["ignore", "pipe", "pipe"],
env: { ...process.env, ...options.env, OPENCODE_PERMISSION: '{"*":"allow"}' },
- shell: process.platform === "win32",
+ shell: isWin,
windowsHide: true,
// POSIX: own process group (pgid === pid) so the session-runner can reap
// the CLI *and* its tool subprocesses via a group kill. No unref() — we
diff --git a/src/cli/daemon/agent/win-quote.ts b/src/cli/daemon/agent/win-quote.ts
new file mode 100644
index 000000000..5796fe36d
--- /dev/null
+++ b/src/cli/daemon/agent/win-quote.ts
@@ -0,0 +1,53 @@
+// Windows argument quoting for spawn({ shell: true }).
+//
+// Node's child_process.spawn does NOT quote individual args when `shell: true`
+// is set on Windows — it joins them with spaces and hands the raw string to
+// cmd.exe. Any arg containing whitespace (e.g. --dir C:\Users\John Doe\...) is
+// then split by cmd, causing "Failed to change dir to C:\Users\John".
+//
+// We can't drop `shell: true` because Node has blocked spawning .cmd/.bat
+// shims without a shell since CVE-2024-27980, and every npm-installed CLI we
+// launch (opencode, claude, codex) lands as a .cmd shim on Windows. So we
+// pre-quote each argument here.
+//
+// Strategy: wrap in double quotes whenever the arg contains whitespace or any
+// cmd.exe metachar (& | < > ^ ( ) " %), then apply the MSVCRT rules for
+// escaping backslashes and inner quotes. Inside "..." cmd itself does not
+// interpret & | < > ^ ( ) — so no ^-escaping is needed once quoted.
+//
+// Known cmd.exe residuals (out of scope for this helper — they are limitations
+// of ALL shell:true spawns on Windows, not regressions of this fix):
+// - CR/LF inside an arg terminates the cmd command line. Multi-line prompts
+// have to be delivered via stdin or a temp file, not argv, on Windows.
+// - %VAR% still expands inside "...". There is no reliable command-line
+// escape for % on Windows (%% only works in batch files). If arbitrary
+// user text with literal % must reach the child, use stdin.
+// Both were broken before this fix and remain broken; the space-in-path bug
+// (the reported one) is fully addressed.
+
+const NEEDS_QUOTING = /[\s"&|<>^()%]/;
+
+export function quoteWinArg(arg: string): string {
+ if (arg.length > 0 && !NEEDS_QUOTING.test(arg)) return arg;
+
+ let result = "\"";
+ let backslashes = 0;
+ for (const ch of arg) {
+ if (ch === "\\") {
+ backslashes++;
+ continue;
+ }
+ if (ch === "\"") {
+ result += "\\".repeat(backslashes * 2 + 1) + "\"";
+ } else {
+ result += "\\".repeat(backslashes) + ch;
+ }
+ backslashes = 0;
+ }
+ result += "\\".repeat(backslashes * 2) + "\"";
+ return result;
+}
+
+export function quoteWinArgs(args: string[]): string[] {
+ return args.map(quoteWinArg);
+}