@@ -19,7 +19,7 @@ import {
1919 writeFileSync ,
2020} from "node:fs" ;
2121import { getConfigDir } from "../config" ;
22- import { durableBunPath } from "../lib/bun-runtime" ;
22+ import { BUN_RUNTIME_SOURCE_ENV , durableBunRuntime , type BunRuntimeSource } from "../lib/bun-runtime" ;
2323import { isProcessAlive } from "../lib/process-control" ;
2424import { serviceApiTokenFilePath } from "../lib/service-secrets" ;
2525import { recordOwnedConfigPath } from "../lib/config-ownership" ;
@@ -120,11 +120,16 @@ export type CodexShimAutoRestoreResult =
120120 | { status : "ineligible" | "deferred" ; message ?: string }
121121 | { status : "restored" ; message : string } ;
122122
123- function cliEntry ( ) : { bun : string ; cli : string } {
124- // Bundled Bun path (survives `ocx update`) ; all three shim builders
125- // (Unix / Windows cmd / Windows PowerShell) receive it via this entry .
123+ function cliEntry ( ) : { bun : string ; bunRuntimeSource : BunRuntimeSource ; cli : string } {
124+ // Resolve the durable path and its diagnostic provenance together ; all three shim
125+ // builders (Unix / Windows cmd / Windows PowerShell) receive the same pair .
126126 // This module lives in src/codex/, the CLI entry in src/cli/index.ts.
127- return { bun : durableBunPath ( ) , cli : join ( import . meta. dir , ".." , "cli" , "index.ts" ) } ;
127+ const bunRuntime = durableBunRuntime ( ) ;
128+ return {
129+ bun : bunRuntime . path ,
130+ bunRuntimeSource : bunRuntime . source ,
131+ cli : join ( import . meta. dir , ".." , "cli" , "index.ts" ) ,
132+ } ;
128133}
129134
130135function commandNames ( name : string ) : string [ ] {
@@ -366,11 +371,19 @@ function shQuote(value: string): string {
366371 return `'${ value . replace ( / ' / g, "'\\''" ) } '` ;
367372}
368373
369- export function buildUnixCodexShim ( realCodexPath : string , bunPath : string , cliPath : string , tokenFile = serviceApiTokenFilePath ( ) ) : string {
374+ export function buildUnixCodexShim (
375+ realCodexPath : string ,
376+ bunPath : string ,
377+ cliPath : string ,
378+ tokenFile = serviceApiTokenFilePath ( ) ,
379+ bunRuntimeSource : BunRuntimeSource = "process" ,
380+ ) : string {
370381 const internalCommands = CODEX_INTERNAL_COMMANDS . join ( "|" ) ;
371382 const valueOptions = CODEX_GLOBAL_OPTIONS_WITH_VALUE . join ( "|" ) ;
372383 return `#!/usr/bin/env sh
373384# ${ SHIM_MARKER }
385+ ${ BUN_RUNTIME_SOURCE_ENV } =${ shQuote ( bunRuntimeSource ) }
386+ export ${ BUN_RUNTIME_SOURCE_ENV }
374387if [ -z "$OPENCODEX_API_AUTH_TOKEN" ] && [ -f ${ shQuote ( tokenFile ) } ]; then
375388 OPENCODEX_API_AUTH_TOKEN="$(cat ${ shQuote ( tokenFile ) } )"
376389 export OPENCODEX_API_AUTH_TOKEN
@@ -431,14 +444,20 @@ function windowsBatchSet(name: string, value: string): string {
431444 return `set "${ name } =${ windowsEnvIndirectBatchValue ( value , windowsBatchValue ) } "` ;
432445}
433446
434- export function buildWindowsCodexShim ( realCodexPath : string , bunPath : string , cliPath : string ) : string {
447+ export function buildWindowsCodexShim (
448+ realCodexPath : string ,
449+ bunPath : string ,
450+ cliPath : string ,
451+ bunRuntimeSource : BunRuntimeSource = "process" ,
452+ ) : string {
435453 const internalCommandChecks = CODEX_INTERNAL_COMMANDS . map ( command => `if /I "%~1"=="${ command } " goto run_codex` ) . join ( "\r\n" ) ;
436454 const valueOptionChecks = CODEX_GLOBAL_OPTIONS_WITH_VALUE . map ( option => `if /I "%~1"=="${ option } " goto skip_option_value` ) . join ( "\r\n" ) ;
437455 return `@echo off\r
438456rem ${ SHIM_MARKER } \r
439457${ windowsBatchSet ( "OCX_REAL_CODEX" , realCodexPath ) } \r
440458${ windowsBatchSet ( "OCX_BUN" , bunPath ) } \r
441459${ windowsBatchSet ( "OCX_CLI" , cliPath ) } \r
460+ ${ windowsBatchSet ( BUN_RUNTIME_SOURCE_ENV , bunRuntimeSource ) } \r
442461${ windowsBatchSet ( "OCX_API_TOKEN_FILE" , serviceApiTokenFilePath ( ) ) } \r
443462if "%OPENCODEX_API_AUTH_TOKEN%"=="" if exist "%OCX_API_TOKEN_FILE%" set /p OPENCODEX_API_AUTH_TOKEN=<"%OCX_API_TOKEN_FILE%"\r
444463if not "%OCX_SHIM_BYPASS%"=="" goto run_codex\r
@@ -472,12 +491,18 @@ function psString(value: string): string {
472491 return `'${ value . replace ( / ' / g, "''" ) } '` ;
473492}
474493
475- export function buildWindowsPowerShellCodexShim ( realCodexPath : string , bunPath : string , cliPath : string ) : string {
494+ export function buildWindowsPowerShellCodexShim (
495+ realCodexPath : string ,
496+ bunPath : string ,
497+ cliPath : string ,
498+ bunRuntimeSource : BunRuntimeSource = "process" ,
499+ ) : string {
476500 const internalCommands = CODEX_INTERNAL_COMMANDS . map ( command => psString ( command ) ) . join ( ", " ) ;
477501 const valueOptions = CODEX_GLOBAL_OPTIONS_WITH_VALUE . map ( option => psString ( option ) ) . join ( ", " ) ;
478502 const tokenFile = serviceApiTokenFilePath ( ) ;
479503 return `#!/usr/bin/env pwsh
480504# ${ SHIM_MARKER }
505+ $env:${ BUN_RUNTIME_SOURCE_ENV } = ${ psString ( bunRuntimeSource ) }
481506if (-not $env:OPENCODEX_API_AUTH_TOKEN -and (Test-Path -LiteralPath ${ psString ( tokenFile ) } )) {
482507 $env:OPENCODEX_API_AUTH_TOKEN = (Get-Content -Raw -LiteralPath ${ psString ( tokenFile ) } ).Trim()
483508}
@@ -601,25 +626,35 @@ function gitBashPath(path: string): string {
601626}
602627
603628function writeShim ( wrapperPath : string , realCodexPath : string ) : void {
604- const { bun, cli } = cliEntry ( ) ;
629+ const { bun, bunRuntimeSource , cli } = cliEntry ( ) ;
605630 if ( process . platform === "win32" ) {
606631 const lower = wrapperPath . toLowerCase ( ) ;
607632 if ( lower . endsWith ( ".ps1" ) ) {
608633 // UTF-8 BOM: Windows PowerShell 5.1 decodes BOM-less .ps1 files in the ANSI
609634 // codepage, which mangles non-ASCII paths embedded in the shim.
610- writeFileSync ( wrapperPath , `\uFEFF${ buildWindowsPowerShellCodexShim ( realCodexPath , bun , cli ) } ` , "utf8" ) ;
635+ writeFileSync ( wrapperPath , `\uFEFF${ buildWindowsPowerShellCodexShim ( realCodexPath , bun , cli , bunRuntimeSource ) } ` , "utf8" ) ;
611636 } else if ( lower . endsWith ( ".cmd" ) || lower . endsWith ( ".bat" ) ) {
612- writeFileSync ( wrapperPath , buildWindowsCodexShim ( realCodexPath , bun , cli ) , "utf8" ) ;
637+ writeFileSync ( wrapperPath , buildWindowsCodexShim ( realCodexPath , bun , cli , bunRuntimeSource ) , "utf8" ) ;
613638 } else {
614639 // Extensionless Git-Bash sh launcher: sh shim with forward-slash paths.
615640 writeFileSync (
616641 wrapperPath ,
617- buildUnixCodexShim ( gitBashPath ( realCodexPath ) , gitBashPath ( bun ) , gitBashPath ( cli ) , gitBashPath ( serviceApiTokenFilePath ( ) ) ) ,
642+ buildUnixCodexShim (
643+ gitBashPath ( realCodexPath ) ,
644+ gitBashPath ( bun ) ,
645+ gitBashPath ( cli ) ,
646+ gitBashPath ( serviceApiTokenFilePath ( ) ) ,
647+ bunRuntimeSource ,
648+ ) ,
618649 "utf8" ,
619650 ) ;
620651 }
621652 } else {
622- writeFileSync ( wrapperPath , buildUnixCodexShim ( realCodexPath , bun , cli ) , "utf8" ) ;
653+ writeFileSync (
654+ wrapperPath ,
655+ buildUnixCodexShim ( realCodexPath , bun , cli , serviceApiTokenFilePath ( ) , bunRuntimeSource ) ,
656+ "utf8" ,
657+ ) ;
623658 chmodSync ( wrapperPath , 0o755 ) ;
624659 }
625660}
0 commit comments