@@ -8,6 +8,20 @@ import { autoRestoreCodexShim, buildUnixCodexShim, buildWindowsCodexShim, buildW
88const SHIM_MARKER = "opencodex codex autostart shim" ;
99const skipStabilityWait = ( ) => { } ;
1010
11+ function processState ( pid : number ) : string {
12+ return spawnSync ( "/bin/ps" , [ "-o" , "stat=" , "-p" , String ( pid ) ] , { encoding : "utf8" } ) . stdout . trim ( ) ;
13+ }
14+
15+ function expectProcessGroupMissing ( groupId : number ) : void {
16+ let code : string | undefined ;
17+ try {
18+ process . kill ( - groupId , 0 ) ;
19+ } catch ( error ) {
20+ code = ( error as NodeJS . ErrnoException ) . code ;
21+ }
22+ expect ( code ) . toBe ( "ESRCH" ) ;
23+ }
24+
1125function withInstalledShim ( run : ( paths : {
1226 binDir : string ;
1327 home : string ;
@@ -23,7 +37,7 @@ function withInstalledShim(run: (paths: {
2337 ? [ join ( binDir , "codex.cmd" ) , join ( binDir , "codex.ps1" ) , join ( binDir , "codex" ) ]
2438 : [ join ( binDir , "codex" ) ] ;
2539 try {
26- process . env . PATH = binDir ;
40+ process . env . PATH = ` ${ binDir } : ${ oldPath ?? "" } ` ;
2741 process . env . OPENCODEX_HOME = home ;
2842 for ( const wrapper of wrappers ) {
2943 writeFileSync ( wrapper , process . platform === "win32" ? `real ${ wrapper } \n` : "#!/bin/sh\necho real\n" , "utf8" ) ;
@@ -307,20 +321,27 @@ exit 64
307321 const codexPath = join ( binDir , "codex" ) ;
308322 const launcherPath = join ( binDir , "dynamic-launcher" ) ;
309323 const childPidPath = join ( home , "child-reentry.pid" ) ;
324+ const grandchildPidPath = join ( home , "child-reentry-grandchild.pid" ) ;
325+ const groupIdPath = join ( home , "child-reentry-group.pid" ) ;
310326 const original = `#!/bin/sh\nexec "${ launcherPath } " "$@"\n` ;
311327 try {
312328 process . env . PATH = `${ binDir } :${ oldPath ?? "" } ` ;
313329 process . env . OPENCODEX_HOME = home ;
314330 writeFileSync ( launcherPath , `#!/bin/sh
315331if [ -n "$OCX_TEST_CHILD_REENTRY" ]; then
316- /bin/sleep 30
332+ /bin/sleep 30 &
333+ grandchild=$!
334+ printf '%s\\n' "$grandchild" > "${ grandchildPidPath } "
335+ wait "$grandchild"
317336 exec codex "$@"
318337fi
319338OCX_TEST_CHILD_REENTRY=1
320339export OCX_TEST_CHILD_REENTRY
340+ printf '%s\\n' "$$" > "${ groupIdPath } "
321341codex "$@" &
322342child=$!
323343printf '%s\\n' "$child" > "${ childPidPath } "
344+ while [ ! -f "${ grandchildPidPath } " ]; do /bin/sleep 0.01; done
324345exit 0
325346` , "utf8" ) ;
326347 writeFileSync ( codexPath , original , "utf8" ) ;
@@ -335,8 +356,13 @@ exit 0
335356 expect ( existsSync ( `${ codexPath } .opencodex-real` ) ) . toBe ( false ) ;
336357 expect ( existsSync ( join ( home , "codex-shim.json" ) ) ) . toBe ( false ) ;
337358 const childPid = Number . parseInt ( readFileSync ( childPidPath , "utf8" ) . trim ( ) , 10 ) ;
338- const childState = spawnSync ( "/bin/ps" , [ "-o" , "stat=" , "-p" , String ( childPid ) ] , { encoding : "utf8" } ) . stdout . trim ( ) ;
359+ const grandchildPid = Number . parseInt ( readFileSync ( grandchildPidPath , "utf8" ) . trim ( ) , 10 ) ;
360+ const groupId = Number . parseInt ( readFileSync ( groupIdPath , "utf8" ) . trim ( ) , 10 ) ;
361+ expectProcessGroupMissing ( groupId ) ;
362+ const childState = processState ( childPid ) ;
363+ const grandchildState = processState ( grandchildPid ) ;
339364 expect ( childState === "" || childState . startsWith ( "Z" ) ) . toBe ( true ) ;
365+ expect ( grandchildState === "" || grandchildState . startsWith ( "Z" ) ) . toBe ( true ) ;
340366 } finally {
341367 if ( oldPath === undefined ) delete process . env . PATH ;
342368 else process . env . PATH = oldPath ;
@@ -382,8 +408,8 @@ wait "$child"
382408 const groupId = Number . parseInt ( readFileSync ( groupIdPath , "utf8" ) . trim ( ) , 10 ) ;
383409 expect ( Number . isInteger ( childPid ) ) . toBe ( true ) ;
384410 expect ( Number . isInteger ( groupId ) ) . toBe ( true ) ;
385- expect ( ( ) => process . kill ( - groupId , 0 ) ) . toThrow ( ) ;
386- const childState = spawnSync ( "/bin/ps" , [ "-o" , "stat=" , "-p" , String ( childPid ) ] , { encoding : "utf8" } ) . stdout . trim ( ) ;
411+ expectProcessGroupMissing ( groupId ) ;
412+ const childState = processState ( childPid ) ;
387413 expect ( childState === "" || childState . startsWith ( "Z" ) ) . toBe ( true ) ;
388414 } finally {
389415 if ( oldPath === undefined ) delete process . env . PATH ;
@@ -701,6 +727,49 @@ printf '%s\\n' child-codex
701727 } ) ;
702728 } ) ;
703729
730+ test ( "guarded auto-restore rejects a recursive replacement and restores both launcher generations" , ( ) => {
731+ if ( process . platform === "win32" ) return ;
732+ withInstalledShim ( ( { binDir, wrappers, backups, statePath } ) => {
733+ const dynamicLauncher = join ( binDir , "dynamic-codex-launcher" ) ;
734+ const replacement = `#!/bin/sh\nexec "${ dynamicLauncher } " "$@"\n` ;
735+ const oldBackup = readFileSync ( backups [ 0 ] ) ;
736+ const oldState = readFileSync ( statePath ) ;
737+ writeFileSync ( dynamicLauncher , "#!/bin/sh\nexec codex \"$@\"\n" , "utf8" ) ;
738+ writeFileSync ( wrappers [ 0 ] , replacement , "utf8" ) ;
739+ chmodSync ( dynamicLauncher , 0o755 ) ;
740+ chmodSync ( wrappers [ 0 ] , 0o755 ) ;
741+
742+ const result = autoRestoreCodexShim ( { enabled : ( ) => true , stabilitySleep : skipStabilityWait } ) ;
743+
744+ expect ( result . status ) . toBe ( "deferred" ) ;
745+ expect ( readFileSync ( wrappers [ 0 ] , "utf8" ) ) . toBe ( replacement ) ;
746+ expect ( readFileSync ( backups [ 0 ] ) ) . toEqual ( oldBackup ) ;
747+ expect ( readFileSync ( statePath ) ) . toEqual ( oldState ) ;
748+ } ) ;
749+ } ) ;
750+
751+ test ( "direct refresh rejects a recursive replacement without replacing the owned backup" , ( ) => {
752+ if ( process . platform === "win32" ) return ;
753+ withInstalledShim ( ( { binDir, wrappers, backups, statePath } ) => {
754+ const dynamicLauncher = join ( binDir , "dynamic-codex-launcher" ) ;
755+ const replacement = `#!/bin/sh\nexec "${ dynamicLauncher } " "$@"\n` ;
756+ const oldBackup = readFileSync ( backups [ 0 ] ) ;
757+ const oldState = readFileSync ( statePath ) ;
758+ writeFileSync ( dynamicLauncher , "#!/bin/sh\nexec codex \"$@\"\n" , "utf8" ) ;
759+ writeFileSync ( wrappers [ 0 ] , replacement , "utf8" ) ;
760+ chmodSync ( dynamicLauncher , 0o755 ) ;
761+ chmodSync ( wrappers [ 0 ] , 0o755 ) ;
762+
763+ const result = installCodexShim ( ) ;
764+
765+ expect ( result . installed ) . toBe ( false ) ;
766+ expect ( result . message ) . toContain ( "Refusing to overwrite existing backup" ) ;
767+ expect ( readFileSync ( wrappers [ 0 ] , "utf8" ) ) . toBe ( replacement ) ;
768+ expect ( readFileSync ( backups [ 0 ] ) ) . toEqual ( oldBackup ) ;
769+ expect ( readFileSync ( statePath ) ) . toEqual ( oldState ) ;
770+ } ) ;
771+ } ) ;
772+
704773 test ( "an aged lock held by a live restore owner is never reclaimed" , async ( ) => {
705774 const binDir = mkdtempSync ( join ( tmpdir ( ) , "ocx-shim-concurrent-bin-" ) ) ;
706775 const home = mkdtempSync ( join ( tmpdir ( ) , "ocx-shim-concurrent-home-" ) ) ;
0 commit comments