Skip to content

Commit b0f1739

Browse files
committed
test(codex): centralize shim recursion-guard sanitization
The previous fix deleted both guard variables at one spawn site. A reviewer pointed out the class of bug survives: the nearby re-entry tests assert status 126, so an inherited depth offset leaves them passing for entirely the wrong reason, and the next test to spawn a shim starts the cycle again. shimChildEnv strips PID, DEPTH and PROBE_ACTIVE in one place, mirroring what probeUnixShimInstall already does before spawning its probe. The second spawn site at line 291 had the same gap - it deleted only the pid - so it was quietly depth-sensitive too. What this buys is that a green run means what it says. Before, these tests measured whatever shim ancestry the developer's shell happened to carry, and were green in CI only because CI has no shimmed ancestor.
1 parent 75a1b31 commit b0f1739

1 file changed

Lines changed: 25 additions & 11 deletions

File tree

‎tests/codex-shim.test.ts‎

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,29 @@ import { autoRestoreCodexShim, buildUnixCodexShim, buildWindowsCodexShim, buildW
77

88
const SHIM_MARKER = "opencodex codex autostart shim";
99
const UNIX_SHIM_REVISION_MARKER = "opencodex unix codex shim revision 2";
10+
11+
/**
12+
* A child environment with the shim's recursion-guard state stripped.
13+
*
14+
* Every one of these tests reasons about a shim invocation starting from depth 0, but a developer
15+
* whose shell was itself launched through an installed Codex shim exports
16+
* `OCX_SHIM_ACTIVE_DEPTH=1` — so the guard fires a level early and the test measures whatever
17+
* ancestry the machine happened to have. CI has no shimmed ancestor, which is what let that bleed
18+
* hide: green there, red on a real developer's machine.
19+
*
20+
* The re-entry tests are the subtle case. They assert `status === 126`, so an inherited +1 offset
21+
* leaves them passing for entirely the wrong reason. Sanitizing centrally is what makes their
22+
* green mean what it says.
23+
*
24+
* Mirrors `probeUnixShimInstall`, which already clears the same three before spawning its probe.
25+
*/
26+
function shimChildEnv(overrides: Record<string, string> = {}): NodeJS.ProcessEnv {
27+
const env: NodeJS.ProcessEnv = { ...process.env, ...overrides };
28+
delete env.OCX_SHIM_ACTIVE_PID;
29+
delete env.OCX_SHIM_ACTIVE_DEPTH;
30+
delete env.OCX_SHIM_PROBE_ACTIVE;
31+
return env;
32+
}
1033
const skipStabilityWait = () => {};
1134
const python3Path = process.platform === "win32"
1235
? ""
@@ -288,8 +311,7 @@ exit 64
288311
chmodSync(misePath, 0o755);
289312
chmodSync(realCodexPath, 0o755);
290313
chmodSync(shimPath, 0o755);
291-
const env = { ...process.env, PATH: prependPath(dir, process.env.PATH), OCX_SHIM_BYPASS: "1" };
292-
delete env.OCX_SHIM_ACTIVE_PID;
314+
const env = shimChildEnv({ PATH: prependPath(dir, process.env.PATH) ?? "", OCX_SHIM_BYPASS: "1" });
293315

294316
const result = spawnSync(shimPath, ["--help"], {
295317
encoding: "utf8",
@@ -1042,15 +1064,7 @@ printf '%s\\n' child-codex
10421064
chmodSync(bunPath, 0o755);
10431065
chmodSync(realCodexPath, 0o755);
10441066
chmodSync(shimPath, 0o755);
1045-
const env = { ...process.env, OCX_SHIM_BYPASS: "1" };
1046-
// Both recursion-guard variables, not just the pid. A developer running this suite from a
1047-
// shell that was itself launched through an installed shim inherits
1048-
// OCX_SHIM_ACTIVE_DEPTH=1, so the outer shim starts at depth 1, the child re-entry lands on
1049-
// depth 2, and the guard exits 126 with the launcher-loop message — the shim behaving
1050-
// exactly as designed, on a test that meant to start from a clean slate. CI never sees it
1051-
// because CI has no shimmed ancestor, which is what made this look environmental.
1052-
delete env.OCX_SHIM_ACTIVE_PID;
1053-
delete env.OCX_SHIM_ACTIVE_DEPTH;
1067+
const env = shimChildEnv({ OCX_SHIM_BYPASS: "1" });
10541068

10551069
const result = spawnSync(shimPath, ["--help"], {
10561070
encoding: "utf8",

0 commit comments

Comments
 (0)