@@ -18,6 +18,7 @@ import {
1818} from "@t3tools/contracts" ;
1919import { waitForHttpReady } from "@t3tools/shared/httpReadiness" ;
2020import { HostProcessEnvironment , HostProcessPlatform } from "@t3tools/shared/hostProcess" ;
21+ import { resolveNodeExecutable } from "@t3tools/shared/nodeRuntime" ;
2122import * as NetService from "@t3tools/shared/Net" ;
2223import { isCommandAvailable } from "@t3tools/shared/shell" ;
2324import * as Clock from "effect/Clock" ;
@@ -76,6 +77,7 @@ const AgentDeviceDaemonFile = Schema.Struct({
7677const decodeDaemonFile = Schema . decodeUnknownEffect ( Schema . fromJsonString ( AgentDeviceDaemonFile ) ) ;
7778
7879interface HubProcess {
80+ readonly nodePath : string ;
7981 readonly child : ChildProcessSpawner . ChildProcessHandle ;
8082 readonly scope : Scope . Closeable ;
8183 readonly origin : string ;
@@ -310,6 +312,7 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
310312
311313 const spawnHub = Effect . fn ( "LocalDeviceHost.spawnHub" ) ( function * (
312314 hubTool : DeviceToolPaths ,
315+ nodePath : string ,
313316 ) : Effect . fn . Return < HubProcess , DeviceHost . DeviceHostError > {
314317 yield * reapStaleHub ;
315318 yield * fs
@@ -330,7 +333,7 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
330333 const child = yield * spawner
331334 . spawn (
332335 ChildProcess . make (
333- process . execPath ,
336+ nodePath ,
334337 [
335338 hubTool . entryPath ,
336339 "--port" ,
@@ -361,7 +364,7 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
361364 ) ,
362365 ) ;
363366 const startedAtMillis = yield * Clock . currentTimeMillis ;
364- const hub : HubProcess = { child, scope, origin, startedAtMillis } ;
367+ const hub : HubProcess = { child, scope, origin, startedAtMillis, nodePath } ;
365368 yield * Effect . forkIn ( observeHubOutput ( hub ) , scope ) ;
366369 yield * waitForHttpReady ( {
367370 baseUrl : origin ,
@@ -418,7 +421,7 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
418421 Effect . gen ( function * ( ) {
419422 const current = yield * Ref . get ( runningRef ) ;
420423 if ( current ?. hub . child . pid !== hub . child . pid ) return ;
421- const replacement = yield * spawnHub ( hubTool ) ;
424+ const replacement = yield * spawnHub ( hubTool , hub . nodePath ) ;
422425 yield * Ref . set ( runningRef , { ...current , hub : replacement } ) ;
423426 yield * Effect . forkDetach ( superviseHub ( replacement , hubTool ) ) ;
424427 } ) ,
@@ -441,6 +444,7 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
441444 */
442445 const startAgentDeviceDaemon = Effect . fn ( "LocalDeviceHost.startAgentDeviceDaemon" ) ( function * (
443446 agentTool : DeviceToolPaths ,
447+ nodePath : string ,
444448 ) : Effect . fn . Return < DeviceHost . AgentDeviceEndpoint , DeviceHost . DeviceHostTimeoutError > {
445449 const stateDir = agentDeviceStateDir ( path , config . stateDir ) ;
446450 yield * fs . makeDirectory ( stateDir , { recursive : true } ) . pipe ( Effect . ignore ) ;
@@ -481,7 +485,7 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
481485 // daemon and blocks until it answers. `devices` is the cheapest one.
482486 yield * runner
483487 . run ( {
484- command : process . execPath ,
488+ command : nodePath ,
485489 args : [ agentTool . entryPath , "devices" , "--json" ] ,
486490 env : daemonEnvironment ,
487491 timeout : Duration . millis ( DAEMON_READY_TIMEOUT_MS ) ,
@@ -502,11 +506,13 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
502506 }
503507 } ) ;
504508
505- const stopAgentDeviceDaemon = ( agentTool : DeviceToolPaths | null ) =>
509+ const stopAgentDeviceDaemon = (
510+ agentTool : { readonly entryPath : string ; readonly nodePath : string } | null ,
511+ ) =>
506512 agentTool
507513 ? runner
508514 . run ( {
509- command : process . execPath ,
515+ command : agentTool . nodePath ,
510516 args : [
511517 agentTool . entryPath ,
512518 "daemon" ,
@@ -521,7 +527,7 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
521527 . pipe ( Effect . ignore )
522528 : Effect . void ;
523529
524- let agentToolRef : DeviceToolPaths | null = null ;
530+ let agentToolRef : { readonly entryPath : string ; readonly nodePath : string } | null = null ;
525531
526532 const ensureHubReady = Effect . fn ( "LocalDeviceHost.ensureHubReady" ) ( function * (
527533 onPhase : ( phase : "installing" | "starting" ) => Effect . Effect < void > ,
@@ -532,6 +538,20 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
532538 if ( alive ) return running ;
533539 yield * Ref . set ( runningRef , null ) ;
534540 }
541+ const nodePath = yield * resolveNodeExecutable ( "Local device support" , hostEnvironment ) . pipe (
542+ Effect . provideService ( FileSystem . FileSystem , fs ) ,
543+ Effect . provideService ( Path . Path , path ) ,
544+ Effect . provideService ( HostProcessPlatform , hostPlatform ) ,
545+ Effect . mapError (
546+ ( cause ) =>
547+ new DeviceHost . DeviceHostError ( {
548+ hostId,
549+ step : "finding Node.js" ,
550+ detail : cause . message ,
551+ cause,
552+ } ) ,
553+ ) ,
554+ ) ;
535555 const installed = yield * isDeviceHubInstalled ( config . baseDir ) . pipe (
536556 Effect . provideService ( FileSystem . FileSystem , fs ) ,
537557 Effect . provideService ( Path . Path , path ) ,
@@ -551,7 +571,7 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
551571 ) ,
552572 ) ;
553573 yield * onPhase ( "starting" ) ;
554- const hub = yield * spawnHub ( hubTool ) ;
574+ const hub = yield * spawnHub ( hubTool , nodePath ) ;
555575 const candidate = helperPaths ( hubTool ) ;
556576 const [ axExists , cliExists ] = yield * Effect . all ( [
557577 fs . exists ( candidate . serveSimAxSettings ) . pipe ( Effect . orElseSucceed ( ( ) => false ) ) ,
@@ -600,9 +620,9 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
600620 } ) ,
601621 ) ,
602622 ) ;
603- agentToolRef = agentTool ;
623+ agentToolRef = { entryPath : agentTool . entryPath , nodePath : running . hub . nodePath } ;
604624 yield * onPhase ( "starting" ) ;
605- const agentDevice = yield * startAgentDeviceDaemon ( agentTool ) ;
625+ const agentDevice = yield * startAgentDeviceDaemon ( agentTool , running . hub . nodePath ) ;
606626 const next = { ...running , agentDevice } ;
607627 yield * Ref . set ( runningRef , next ) ;
608628 return { ...toReady ( next ) , agentDevice } ;
@@ -652,7 +672,7 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
652672
653673 const toReady = ( running : RunningHost ) : DeviceHost . DeviceHostReady => ( {
654674 hub : { origin : running . hub . origin } satisfies DeviceHost . DeviceHubEndpoint ,
655- nodePath : process . execPath ,
675+ nodePath : running . hub . nodePath ,
656676 run,
657677 helpers : running . helpers ,
658678 } ) ;
0 commit comments