Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/manager/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ...; <agent command>`, 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) => {
Expand Down Expand Up @@ -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]),
);
}),
Expand Down
11 changes: 10 additions & 1 deletion src/test/manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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");
Expand Down