@@ -39,6 +39,16 @@ export function verifyInstallTree(packageDir, expectedVersion) {
3939 } catch {
4040 failures . push ( "bin/ocx.mjs absent" ) ;
4141 }
42+ // The bundled Bun binary is the load-bearing artifact: without it the launcher exits
43+ // before serving anything, and a boot probe that called this tree healthy would reap
44+ // the only backup (review High 3). Size-gate the real binary, not just its package.json.
45+ const bunPkgDir = join ( packageDir , "node_modules" , "bun" ) ;
46+ if ( existsSync ( bunPkgDir ) ) {
47+ const bunBinary = findLargestFile ( bunPkgDir ) ;
48+ if ( ! bunBinary || bunBinary . size < 10 * 1024 * 1024 ) {
49+ failures . push ( "bundled Bun binary missing or truncated (< 10MB)" ) ;
50+ }
51+ }
4252 // Sentinel direct deps: each must have an intact package.json. The bundled Bun dep is
4353 // the load-bearing one — without it the launcher cannot start the proxy at all.
4454 const deps = Object . keys ( pkg . dependencies ?? { } ) ;
@@ -56,6 +66,42 @@ function stampedName(prefix) {
5666 return prefix + "-" + new Date ( ) . toISOString ( ) . replace ( / [: .] / g, "-" ) ;
5767}
5868
69+ /** Largest regular file under a directory tree (bounded depth) — locates the Bun binary. */
70+ function findLargestFile ( root , depth = 3 ) {
71+ let best ;
72+ let names = [ ] ;
73+ try { names = readdirSync ( root ) ; } catch { return undefined ; }
74+ for ( const name of names ) {
75+ const full = join ( root , name ) ;
76+ let st ;
77+ try { st = statSync ( full ) ; } catch { continue ; }
78+ if ( st . isFile ( ) ) {
79+ if ( ! best || st . size > best . size ) best = { path : full , size : st . size } ;
80+ } else if ( st . isDirectory ( ) && depth > 0 ) {
81+ const sub = findLargestFile ( full , depth - 1 ) ;
82+ if ( sub && ( ! best || sub . size > best . size ) ) best = sub ;
83+ }
84+ }
85+ return best ;
86+ }
87+
88+ /** Bounded Windows-class rename retry: EPERM/EBUSY/EACCES from AV/indexers clears in ms. */
89+ function renameWithRetry ( rename , from , to , attempts = 5 ) {
90+ for ( let attempt = 0 ; ; attempt += 1 ) {
91+ try {
92+ rename ( from , to ) ;
93+ return ;
94+ } catch ( error ) {
95+ const code = error ?. code ;
96+ const retryable = code === "EPERM" || code === "EBUSY" || code === "EACCES" ;
97+ if ( ! retryable || attempt >= attempts - 1 ) throw error ;
98+ // Synchronous bounded backoff (launcher context has no async loop here).
99+ const until = Date . now ( ) + 100 * ( attempt + 1 ) ;
100+ while ( Date . now ( ) < until ) { /* spin briefly; total worst case ~1.5s */ }
101+ }
102+ }
103+ }
104+
59105function recoveryMarkerPath ( scopeDir ) {
60106 return join ( scopeDir , ".ocx-recovery.json" ) ;
61107}
@@ -111,8 +157,14 @@ export function transactionalNpmUpdate({
111157 const stagedPackage = join ( stageRoot , "node_modules" , ...pkgName . split ( "/" ) ) ;
112158
113159 // D1: stage to the side. --prefix keeps npm entirely inside stageRoot; the live tree
114- // and the npm bin shims are untouched until the swap.
115- mkdirSync ( stageRoot , { recursive : true } ) ;
160+ // and the npm bin shims are untouched until the swap. A failure HERE (mkdir EACCES,
161+ // ENOSPC) must NOT fall back to the destructive legacy install (review High 4): the
162+ // caller sees a normal phase failure with live untouched.
163+ try {
164+ mkdirSync ( stageRoot , { recursive : true } ) ;
165+ } catch ( error ) {
166+ return { ok : false , phase : "stage" , error : "could not create staging directory: " + ( error ?. message ?? String ( error ) ) } ;
167+ }
116168 const spec = pkgName + "@" + ( targetVersion || tag ) ;
117169 log ( "Staging " + spec + " into " + stageRoot ) ;
118170 const install = runNpm ( [ "install" , "--prefix" , stageRoot , "--no-audit" , "--no-fund" , spec ] ) ;
@@ -131,20 +183,25 @@ export function transactionalNpmUpdate({
131183 // D3: swap. live -> backup, stage -> live, re-verify live, rollback on failure.
132184 const backupRoot = join ( scopeDir , stampedName ( ".ocx-backup" ) ) ;
133185 const backupPackage = join ( backupRoot , "opencodex" ) ;
134- mkdirSync ( backupRoot , { recursive : true } ) ;
135186 try {
136- rename ( packageDir , backupPackage ) ;
187+ mkdirSync ( backupRoot , { recursive : true } ) ;
188+ } catch ( error ) {
189+ try { rmSync ( stageRoot , { recursive : true , force : true } ) ; } catch { /* best effort */ }
190+ return { ok : false , phase : "swap-backup" , error : "could not create backup directory: " + ( error ?. message ?? String ( error ) ) } ;
191+ }
192+ try {
193+ renameWithRetry ( rename , packageDir , backupPackage ) ;
137194 } catch ( error ) {
138195 try { rmSync ( stageRoot , { recursive : true , force : true } ) ; } catch { /* best effort */ }
139196 try { rmSync ( backupRoot , { recursive : true , force : true } ) ; } catch { /* best effort */ }
140197 return { ok : false , phase : "swap-backup" , error : "could not move live tree aside: " + ( error ?. message ?? String ( error ) ) } ;
141198 }
142199 try {
143- rename ( stagedPackage , packageDir ) ;
200+ renameWithRetry ( rename , stagedPackage , packageDir ) ;
144201 } catch ( error ) {
145202 // Rollback: reverse the first rename. Double fault leaves the recovery marker.
146203 try {
147- rename ( backupPackage , packageDir ) ;
204+ renameWithRetry ( rename , backupPackage , packageDir ) ;
148205 try { rmSync ( stageRoot , { recursive : true , force : true } ) ; } catch { /* best effort */ }
149206 try { rmSync ( backupRoot , { recursive : true , force : true } ) ; } catch { /* best effort */ }
150207 return { ok : false , phase : "swap-live" , rolledBack : true , error : "could not place staged tree: " + ( error ?. message ?? String ( error ) ) } ;
@@ -186,4 +243,3 @@ export function transactionalNpmUpdate({
186243 try { rmSync ( stageRoot , { recursive : true , force : true } ) ; } catch { /* best effort */ }
187244 return { ok : true , phase : "done" , backup : backupPackage } ;
188245}
189-
0 commit comments