Skip to content

Commit a6163dd

Browse files
committed
fix(local-review): preserve Windows Codex fallback
1 parent c8899b5 commit a6163dd

5 files changed

Lines changed: 110 additions & 13 deletions

File tree

‎.agents/skills/local-clawsweeper-review/SKILL.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ unset OPENAI_API_KEY
6969

7070
If the wrong Codex binary is used, set `CODEX_BIN` in the local shell for that
7171
run. Otherwise, `--local-only` uses the first spawnable `codex` command on
72-
`PATH`, including on Windows.
72+
`PATH`, including on Windows. If no Windows PATH command is spawnable, it falls
73+
back to the installed Codex Desktop binary.
7374

7475
## Target Checkout Modes
7576

‎src/codex-process.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export function runCodexProcess(options: {
9090
optionsPath,
9191
JSON.stringify({
9292
args: [...options.args],
93-
command: codexProcessCommand(options.env),
93+
command: codexProcessCommand(options.env, process.platform, options.cwd),
9494
timeoutMs: options.timeoutMs,
9595
resultPath,
9696
stdoutPath,

‎src/codex-spawn.ts‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,25 @@ import {
1515

1616
export type CodexSpawnInvocation = CommandInvocation;
1717

18-
export function codexProcessCommand(env: NodeJS.ProcessEnv = process.env): string {
18+
export function codexProcessCommand(
19+
env: NodeJS.ProcessEnv = process.env,
20+
platform: NodeJS.Platform = process.platform,
21+
cwd = process.cwd(),
22+
): string {
1923
const configured = env.CODEX_BIN?.trim();
2024
if (configured) return configured;
21-
if (process.platform === "win32" && env.CLAWSWEEPER_PREFER_WINDOWS_CODEX_APP === "1") {
25+
if (platform === "win32") {
26+
try {
27+
const pathInvocation = resolveSpawnCommand("codex", [], {
28+
cwd,
29+
env,
30+
missingCommandMessage: "Unable to resolve Windows Codex command: codex",
31+
platform,
32+
});
33+
if (windowsInvocationIsSpawnable(pathInvocation)) return "codex";
34+
} catch {
35+
// Fall through to the Desktop app fallback below.
36+
}
2237
const appBinary = windowsCodexAppBinary(env);
2338
if (appBinary) return appBinary;
2439
}
@@ -31,7 +46,7 @@ export function codexSpawnInvocation(
3146
platform: NodeJS.Platform = process.platform,
3247
cwd = process.cwd(),
3348
): CodexSpawnInvocation {
34-
const configuredCommand = codexProcessCommand(env);
49+
const configuredCommand = codexProcessCommand(env, platform, cwd);
3550
return resolveSpawnCommand(configuredCommand, args, {
3651
cwd,
3752
env,
@@ -114,3 +129,7 @@ function windowsCodexAppBinary(env: NodeJS.ProcessEnv): string | null {
114129
const candidate = join(localAppData, "OpenAI", "Codex", "bin", "codex.exe");
115130
return existsSync(candidate) ? candidate : null;
116131
}
132+
133+
function windowsInvocationIsSpawnable(invocation: CommandInvocation): boolean {
134+
return invocation.command === process.execPath || /\.(?:com|exe)$/i.test(invocation.command);
135+
}

‎src/command.ts‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,18 +163,23 @@ function resolveWindowsCommand(
163163
const extensions = (windowsEnvironmentValue(env, "PATHEXT") || ".COM;.EXE;.BAT;.CMD")
164164
.split(";")
165165
.filter(Boolean);
166-
const candidates = [command, ...extensions.map((extension) => `${command}${extension}`)];
166+
let unsupportedExtensionlessCommand: string | undefined;
167167
for (const directory of (windowsEnvironmentValue(env, "PATH") || "")
168168
.split(delimiter)
169169
.filter(Boolean)) {
170-
for (const candidate of candidates) {
171-
const parent = resolve(cwd, directory);
170+
const parent = resolve(cwd, directory);
171+
for (const candidate of extensions.map((extension) => `${command}${extension}`)) {
172172
const filePath = resolve(parent, candidate);
173173
const actualPath = actualCasePath(parent, candidate);
174174
if (actualPath || existsSync(filePath)) return actualPath ?? filePath;
175175
}
176+
const extensionlessCommand = actualCasePath(parent, command) ?? resolve(parent, command);
177+
if (existsSync(extensionlessCommand)) {
178+
if (nodeShebangScript(extensionlessCommand)) return extensionlessCommand;
179+
unsupportedExtensionlessCommand ??= extensionlessCommand;
180+
}
176181
}
177-
return undefined;
182+
return unsupportedExtensionlessCommand;
178183
}
179184

180185
function actualCasePath(parent: string, candidate: string): string | undefined {

‎test/codex-process.test.ts‎

Lines changed: 76 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,13 @@ test("Codex process resolves command overrides and escaped Windows launchers", (
2525
assert.equal(codexProcessCommand({}), "codex");
2626
assert.equal(codexProcessCommand({ CODEX_BIN: " custom-codex " }), "custom-codex");
2727
assert.equal(
28-
codexProcessCommand({
29-
CODEX_BIN: "custom-codex",
30-
CLAWSWEEPER_PREFER_WINDOWS_CODEX_APP: "1",
31-
}),
28+
codexProcessCommand(
29+
{
30+
CODEX_BIN: "custom-codex",
31+
CLAWSWEEPER_PREFER_WINDOWS_CODEX_APP: "1",
32+
},
33+
"win32",
34+
),
3235
"custom-codex",
3336
);
3437
assert.deepEqual(codexSpawnInvocation(["exec", "-"], { CODEX_BIN: "codex" }, "linux"), {
@@ -52,6 +55,7 @@ test("Codex process resolves command overrides and escaped Windows launchers", (
5255
const root = mkdtempSync(tmpPrefix);
5356
const binDir = join(root, "bin");
5457
mkdirSync(binDir);
58+
writeFileSync(join(binDir, "codex"), "#!/bin/sh\r\n");
5559
writeFileSync(join(binDir, "codex.cmd"), "@echo off\r\n");
5660
try {
5761
const invocation = codexSpawnInvocation(
@@ -81,6 +85,74 @@ test("Codex process resolves command overrides and escaped Windows launchers", (
8185
);
8286
});
8387

88+
test("Windows Codex selection prefers PATH and falls back to the Desktop app", () => {
89+
const root = mkdtempSync(tmpPrefix);
90+
const pathBin = join(root, "path-bin");
91+
const unsupportedBin = join(root, "unsupported-bin");
92+
const fallbackPathBin = join(root, "fallback-path-bin");
93+
const localAppData = join(root, "local-app-data");
94+
const desktopCodex = join(localAppData, "OpenAI", "Codex", "bin", "codex.exe");
95+
mkdirSync(pathBin);
96+
mkdirSync(unsupportedBin);
97+
mkdirSync(fallbackPathBin);
98+
mkdirSync(join(localAppData, "OpenAI", "Codex", "bin"), { recursive: true });
99+
writeFileSync(join(pathBin, "codex.exe"), "");
100+
writeFileSync(join(unsupportedBin, "codex"), "#!/bin/sh\r\n");
101+
writeFileSync(join(fallbackPathBin, "codex.exe"), "");
102+
writeFileSync(desktopCodex, "");
103+
try {
104+
const pathEnv = {
105+
LOCALAPPDATA: localAppData,
106+
Path: pathBin,
107+
PATHEXT: ".EXE",
108+
SystemRoot: String.raw`C:\Windows`,
109+
};
110+
const pathInvocation = codexSpawnInvocation(["exec"], pathEnv, "win32", root);
111+
assert.equal(pathInvocation.command, join(pathBin, "codex.exe"));
112+
113+
const desktopEnv = { ...pathEnv, Path: "" };
114+
assert.equal(codexProcessCommand(desktopEnv, "win32", root), desktopCodex);
115+
const desktopInvocation = codexSpawnInvocation(["exec"], desktopEnv, "win32", root);
116+
assert.equal(desktopInvocation.command, desktopCodex);
117+
118+
const unsupportedEnv = { ...pathEnv, Path: unsupportedBin };
119+
assert.equal(codexProcessCommand(unsupportedEnv, "win32", root), desktopCodex);
120+
const unsupportedInvocation = codexSpawnInvocation(["exec"], unsupportedEnv, "win32", root);
121+
assert.equal(unsupportedInvocation.command, desktopCodex);
122+
123+
const laterPathEnv = { ...pathEnv, Path: `${unsupportedBin}${delimiter}${fallbackPathBin}` };
124+
const laterPathInvocation = codexSpawnInvocation(["exec"], laterPathEnv, "win32", root);
125+
assert.equal(laterPathInvocation.command, join(fallbackPathBin, "codex.exe"));
126+
} finally {
127+
rmSync(root, { recursive: true, force: true });
128+
}
129+
});
130+
131+
test("Windows Codex selection resolves relative PATH entries from the launch cwd", () => {
132+
const root = mkdtempSync(tmpPrefix);
133+
const launcherDir = join(root, "launcher");
134+
const targetDir = join(root, "target");
135+
const localAppData = join(root, "local-app-data");
136+
const desktopCodex = join(localAppData, "OpenAI", "Codex", "bin", "codex.exe");
137+
mkdirSync(launcherDir);
138+
mkdirSync(join(targetDir, "bin"), { recursive: true });
139+
mkdirSync(join(localAppData, "OpenAI", "Codex", "bin"), { recursive: true });
140+
writeFileSync(join(targetDir, "bin", "codex.exe"), "");
141+
writeFileSync(desktopCodex, "");
142+
try {
143+
const env = {
144+
LOCALAPPDATA: localAppData,
145+
Path: "bin",
146+
PATHEXT: ".EXE",
147+
SystemRoot: String.raw`C:\Windows`,
148+
};
149+
assert.equal(codexProcessCommand(env, "win32", launcherDir), desktopCodex);
150+
assert.equal(codexProcessCommand(env, "win32", targetDir), "codex");
151+
} finally {
152+
rmSync(root, { recursive: true, force: true });
153+
}
154+
});
155+
84156
test("Codex process resolves extensionless Windows node shebang shims", () => {
85157
const root = mkdtempSync(tmpPrefix);
86158
const binDir = join(root, "bin");

0 commit comments

Comments
 (0)