Skip to content

Commit 3b4a189

Browse files
authored
fix(local-review): use selected Codex CLI (#541)
* fix(local-review): use selected Codex CLI * chore: retrigger PR checks * fix(local-review): preserve Windows Codex fallback
1 parent 7cc5ef4 commit 3b4a189

9 files changed

Lines changed: 126 additions & 17 deletions

File tree

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,9 @@ unset OPENAI_API_KEY
6868
```
6969

7070
If the wrong Codex binary is used, set `CODEX_BIN` in the local shell for that
71-
run. On Windows, the runner prefers the Codex app binary under
72-
`%LOCALAPPDATA%\OpenAI\Codex\bin\codex.exe` when `--local-only` is used.
71+
run. Otherwise, `--local-only` uses the first spawnable `codex` command on
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

‎scripts/check-local-codex.mjs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { spawnSync } from "node:child_process";
33

44
const model = argValue("--model") ?? process.env.CLAWSWEEPER_LOCAL_CODEX_MODEL ?? "gpt-5.6-sol";
55
const { codexSpawnInvocation } = await loadCodexLauncher();
6-
const codexEnv = { ...process.env, CLAWSWEEPER_PREFER_WINDOWS_CODEX_APP: "1" };
6+
const codexEnv = { ...process.env };
77
const codex = codexInvocation([]);
88

99
console.log(`Codex binary: ${codex.command}${codex.args.length ? ` ${codex.args.join(" ")}` : ""}`);

‎src/clawsweeper.ts‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9403,7 +9403,6 @@ function runCodex(options: {
94039403
serviceTier: string;
94049404
forcedLoginMethod?: string;
94059405
preserveCodexAuth?: boolean;
9406-
preferWindowsAppBinary?: boolean;
94079406
timeoutMs: number;
94089407
workDir: string;
94099408
additionalPrompt?: string;
@@ -9488,7 +9487,6 @@ function runCodex(options: {
94889487
preserveCodexAuth: options.preserveCodexAuth,
94899488
}),
94909489
CLAWSWEEPER_PROOF_SCRATCH_DIR: proofScratchDir,
9491-
...(options.preferWindowsAppBinary ? { CLAWSWEEPER_PREFER_WINDOWS_CODEX_APP: "1" } : {}),
94929490
},
94939491
input: prompt,
94949492
stderrPath: join(options.workDir, `${options.item.number}.${attempt}.codex.stderr.log`),
@@ -21257,7 +21255,7 @@ function finishReviewActionLedger(options: {
2125721255
function reviewCommand(args: Args): void {
2125821256
const profile = repoFromArgs(args);
2125921257
// `--local-range` is inherently a local, offline operation, so it implies `--local-only`
21260-
// (no GitHub writes, and the local Codex auth / Windows-launcher path in runCodex below).
21258+
// (no GitHub writes, and the local Codex auth path in runCodex below).
2126121259
const localRange = boolArg(args.local_range);
2126221260
const localOnly = boolArg(args.local_only) || localRange;
2126321261
const verbose = boolArg(args.verbose);
@@ -22319,7 +22317,6 @@ function reviewCommand(args: Args): void {
2231922317
serviceTier,
2232022318
forcedLoginMethod,
2232122319
preserveCodexAuth: localOnly,
22322-
preferWindowsAppBinary: localOnly,
2232322320
timeoutMs,
2232422321
workDir: codexWorkDir,
2232522322
additionalPrompt,

‎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/clawsweeper.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2278,6 +2278,7 @@ test("Codex workflows install pinned CLI releases and keep the model secret", ()
22782278
assert.doesNotMatch(action, /@latest/);
22792279
assert.match(localCheck, /CLAWSWEEPER_LOCAL_CODEX_MODEL \?\? "gpt-5\.6-sol"/);
22802280
assert.match(localCheck, /model_reasoning_effort="high"/);
2281+
assert.doesNotMatch(localCheck, /CLAWSWEEPER_PREFER_WINDOWS_CODEX_APP/);
22812282
assert.doesNotMatch(localCheck, /gpt-5\.5/);
22822283
assert.match(action, /env -u OPENAI_API_KEY[\s\S]*-u CLAWSWEEPER_INTERNAL_MODEL/);
22832284
assert.equal(action.match(/--ignore-scripts/g)?.length, 2);

‎test/codex-process.test.ts‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ const tmpPrefix = join(tmpdir(), "clawsweeper-codex-process-test-");
2424
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");
27+
assert.equal(
28+
codexProcessCommand(
29+
{
30+
CODEX_BIN: "custom-codex",
31+
CLAWSWEEPER_PREFER_WINDOWS_CODEX_APP: "1",
32+
},
33+
"win32",
34+
),
35+
"custom-codex",
36+
);
2737
assert.deepEqual(codexSpawnInvocation(["exec", "-"], { CODEX_BIN: "codex" }, "linux"), {
2838
command: "codex",
2939
args: ["exec", "-"],
@@ -45,6 +55,7 @@ test("Codex process resolves command overrides and escaped Windows launchers", (
4555
const root = mkdtempSync(tmpPrefix);
4656
const binDir = join(root, "bin");
4757
mkdirSync(binDir);
58+
writeFileSync(join(binDir, "codex"), "#!/bin/sh\r\n");
4859
writeFileSync(join(binDir, "codex.cmd"), "@echo off\r\n");
4960
try {
5061
const invocation = codexSpawnInvocation(
@@ -74,6 +85,74 @@ test("Codex process resolves command overrides and escaped Windows launchers", (
7485
);
7586
});
7687

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+
77156
test("Codex process resolves extensionless Windows node shebang shims", () => {
78157
const root = mkdtempSync(tmpPrefix);
79158
const binDir = join(root, "bin");

‎test/command.test.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,14 @@ process.exit(1);
240240
}
241241
});
242242

243-
test("local exact review labels Codex failures without a stack trace", () => {
243+
test("local exact review selects PATH Codex instead of the Desktop app binary", () => {
244244
const root = mkdtempSync(join(tmpdir(), "cmd-"));
245245
const origin = join(root, "origin.git");
246246
const targetDir = join(root, "target");
247247
const artifactDir = join(root, "artifacts");
248248
const binDir = join(root, "bin");
249+
const localAppData = join(root, "local-app-data");
250+
const codexMarker = join(root, "path-codex-ran.txt");
249251
try {
250252
execFileSync("git", ["init", "--bare", origin], { stdio: "ignore" });
251253
execFileSync("git", ["init", targetDir], { stdio: "ignore" });
@@ -324,9 +326,13 @@ process.exit(1);
324326
chmodSync(ghPath, 0o755);
325327

326328
const codexPath = join(binDir, "codex");
329+
const desktopCodexDir = join(localAppData, "OpenAI", "Codex", "bin");
330+
mkdirSync(desktopCodexDir, { recursive: true });
331+
writeFileSync(join(desktopCodexDir, "codex.exe"), "");
327332
writeFileSync(
328333
codexPath,
329334
`#!/usr/bin/env node
335+
require("node:fs").writeFileSync(${JSON.stringify(codexMarker)}, "path\\n");
330336
process.stdin.resume();
331337
process.stdin.on("end", () => process.exit(1));
332338
`,
@@ -350,7 +356,7 @@ process.stdin.on("end", () => process.exit(1));
350356
encoding: "utf8",
351357
env: {
352358
...process.env,
353-
CODEX_BIN: codexPath,
359+
LOCALAPPDATA: localAppData,
354360
...mockGhBinEnv(ghPath),
355361
PATH: `${binDir}${delimiter}${process.env.PATH ?? ""}`,
356362
},
@@ -369,6 +375,7 @@ process.stdin.on("end", () => process.exit(1));
369375
assert.doesNotMatch(result.stderr, /Review complete/);
370376
assert.doesNotMatch(result.stderr, /\n\s+at /);
371377
assert.match(readFileSync(join(artifactDir, "96221.md"), "utf8"), /review_status: failed/);
378+
assert.equal(readFileSync(codexMarker, "utf8"), "path\n");
372379
} finally {
373380
rmSync(root, { recursive: true, force: true });
374381
}

0 commit comments

Comments
 (0)