@@ -902,7 +902,7 @@ export class NodeExecutionDriver implements RuntimeDriver {
902902 }
903903
904904 private async waitForManagedResources ( ) : Promise < void > {
905- const graceDeadline = Date . now ( ) + 100 ;
905+ const graceDeadline = Date . now ( ) + 5000 ;
906906
907907 // Give async bridge callbacks a moment to register their host-side handles.
908908 while ( ! this . disposed && ! this . hasManagedResources ( ) && Date . now ( ) < graceDeadline ) {
@@ -1048,8 +1048,12 @@ export class NodeExecutionDriver implements RuntimeDriver {
10481048 // large dependency trees (e.g., PI has hundreds of @sinclair/typebox
10491049 // sub-modules). CJS mode uses the in-process require-setup transform
10501050 // which is orders of magnitude faster.
1051- const sessionMode = options . mode === "run" || entryIsEsm ? "run" : "exec" ;
1052- const userCode = entryIsEsm
1051+ // If the code was already CJS-transformed by _resolveEntry (has the
1052+ // require-esm marker), use exec mode so require() and __dirname work.
1053+ const REQUIRE_ESM_MARKER = "/*__secure_exec_require_esm__*/" ;
1054+ const alreadyTransformed = options . code . startsWith ( REQUIRE_ESM_MARKER ) ;
1055+ const sessionMode = alreadyTransformed ? "exec" : ( options . mode === "run" || entryIsEsm ? "run" : "exec" ) ;
1056+ const userCode = entryIsEsm && ! alreadyTransformed
10531057 ? options . code
10541058 : ( ( ) => {
10551059 const transformed = transformSourceForRequireSync (
@@ -1276,7 +1280,7 @@ export class NodeExecutionDriver implements RuntimeDriver {
12761280 } ,
12771281 timingMitigation ,
12781282 frozenTimeMs ,
1279- options . mode ,
1283+ sessionMode ,
12801284 options . filePath ,
12811285 bindingKeys ,
12821286 ) ;
@@ -1285,6 +1289,7 @@ export class NodeExecutionDriver implements RuntimeDriver {
12851289 this . _currentSession = session ;
12861290
12871291 // Execute in V8 session
1292+ console . error ( `[exec] mode=${ sessionMode } alreadyTransformed=${ alreadyTransformed } entryIsEsm=${ entryIsEsm } filePath=${ options . filePath } codeLen=${ userCode . length } ` ) ;
12881293 const result = await session . execute ( {
12891294 bridgeCode,
12901295 postRestoreScript,
@@ -1325,7 +1330,7 @@ export class NodeExecutionDriver implements RuntimeDriver {
13251330 } ,
13261331 } ) ;
13271332
1328- if ( options . mode === "exec" && ! result . error ) {
1333+ if ( ! result . error ) {
13291334 await this . waitForManagedResources ( ) ;
13301335 }
13311336
@@ -1537,7 +1542,9 @@ function buildPostRestoreScript(
15371542 parts . push ( getIsolateRuntimeSource ( "applyTimingMitigationOff" ) ) ;
15381543 }
15391544
1540- // Apply env/cwd overrides for all modes (needed for ESM process.env access)
1545+ // Apply env, cwd, and stdin overrides for all modes.
1546+ // These must run even in "run" (ESM) mode so that process.env and
1547+ // process.cwd() reflect the spawn-time configuration.
15411548 if ( processConfig . env ) {
15421549 parts . push ( `globalThis.__runtimeProcessEnvOverride = ${ JSON . stringify ( processConfig . env ) } ;` ) ;
15431550 parts . push ( getIsolateRuntimeSource ( "overrideProcessEnv" ) ) ;
@@ -1546,7 +1553,8 @@ function buildPostRestoreScript(
15461553 parts . push ( `globalThis.__runtimeProcessCwdOverride = ${ JSON . stringify ( processConfig . cwd ) } ;` ) ;
15471554 parts . push ( getIsolateRuntimeSource ( "overrideProcessCwd" ) ) ;
15481555 }
1549- // CJS file globals and stdin only for exec mode
1556+
1557+ // CJS file globals (__filename, __dirname, module) only for exec mode.
15501558 if ( mode === "exec" ) {
15511559 const commonJsFileConfig = ( ( ) => {
15521560 if ( filePath ) {
0 commit comments