@@ -7,6 +7,29 @@ import { autoRestoreCodexShim, buildUnixCodexShim, buildWindowsCodexShim, buildW
77
88const SHIM_MARKER = "opencodex codex autostart shim" ;
99const 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+ }
1033const skipStabilityWait = ( ) => { } ;
1134const 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