diff --git a/src/manager/actions.ts b/src/manager/actions.ts index ef8ff46..d03398b 100644 --- a/src/manager/actions.ts +++ b/src/manager/actions.ts @@ -80,6 +80,18 @@ const alive = (pid: number) => { } }; +/** + * Arguments for `wt.exe` that open one new tab running `command` (the argv of the shell and its command). + * + * Windows Terminal reads `;` in its own command line as the border between two of its subcommands, wherever it stands, + * even inside one argument. Since 0.47 the command carries `$env:GH_TOKEN = ...; `, and wt opened it as two + * tabs: one that only set the token and one that failed to start `" node ..."` (alexey, 6251). `\;` is wt's escape for + * a literal semicolon; the fallback `cmd /c start` does not split on it and gets the arguments as they are. + */ +export function windowsTerminalArgs(dir: string, title: string, command: string[]): string[] { + return ["-w", "0", "nt", "-d", dir, "--title", title, ...command].map((a) => a.replace(/;/g, "\\;")); +} + export const systemRunner: Runner = { openTerminal: (dir, title, command) => new Promise((resolve, reject) => { @@ -120,7 +132,7 @@ export const systemRunner: Runner = { }; return next(0); } - tryStart("wt.exe", ["-w", "0", "nt", "-d", dir, "--title", title, ...ps], () => + tryStart("wt.exe", windowsTerminalArgs(dir, title, ps), () => tryStart("cmd.exe", ["/c", "start", JSON.stringify(title), "/D", dir, ...ps]), ); }), diff --git a/src/test/manager.test.ts b/src/test/manager.test.ts index 18c1496..4c78c03 100644 --- a/src/test/manager.test.ts +++ b/src/test/manager.test.ts @@ -5,7 +5,7 @@ import { createServer, request, type Server } from "node:http"; import { tmpdir } from "node:os"; import { basename, join } from "node:path"; import test from "node:test"; -import { Actions, TMUX_PAUSE, atLeast, attachTerminals, readSettings, startCommand, terminalCommand, terminalNote, tmuxSession, type Runner } from "../manager/actions.js"; +import { Actions, TMUX_PAUSE, atLeast, attachTerminals, readSettings, startCommand, terminalCommand, windowsTerminalArgs, terminalNote, tmuxSession, type Runner } from "../manager/actions.js"; import { CLAUDE_START_PS1, CLAUDE_START_SH, startScriptCommand, startScriptName, writeStartScript } from "../manager/claude-start.js"; import { migrateAgent, reportText, type AdapterRun } from "../manager/migrate.js"; import { adapterVersion, brokerOrigin, discover, tail, type LocalAgent } from "../manager/agents.js"; @@ -1805,6 +1805,15 @@ test("manager actions: an agent's ghTokenFile reaches its background process, an assert.ok(!opened.includes("opencode-token"), "the token itself is not in the command"); }); +test("manager actions: Windows Terminal gets the semicolons of the command escaped, one tab not two (alexey 6251)", () => { + const ps = ["powershell", "-NoExit", "-Command", "$env:GH_TOKEN = (Get-Content -Raw -LiteralPath 'D:/t;x.token').Trim(); node ./codex-channel.mjs attach"]; + const args = windowsTerminalArgs("D:/agents/codex", "codex; attach", ps); + assert.deepEqual(args.slice(0, 5), ["-w", "0", "nt", "-d", "D:/agents/codex"]); + assert.equal(args[6], "codex\\; attach", "the title too"); + assert.equal(args.at(-1), "$env:GH_TOKEN = (Get-Content -Raw -LiteralPath 'D:/t\\;x.token').Trim()\\; node ./codex-channel.mjs attach"); + assert.ok(args.every((a) => !/(^|[^\\]);/.test(a)), "no bare semicolon is left for wt to split on"); +}); + test("manager actions: a terminal hands over the path to the token file, quoting it for its own shell (claude-alexey 6198)", () => { assert.equal(terminalCommand("opencode", null), "opencode", "no file: the command is untouched"); assert.equal(terminalCommand("opencode", join(tmpdir(), "no-such-aichat-token-file"), "win32"), "opencode", "a missing file adds nothing to the tab");