@@ -22,6 +22,7 @@ import {
2222 type DeviceEasAccountInput ,
2323 type DeviceHostConfig ,
2424 type EasDeviceHostConfig ,
25+ type SimlockDeviceHostConfig ,
2526 type SshDeviceHostConfig ,
2627 type DeviceHostSummary ,
2728 LOCAL_DEVICE_HOST_ID ,
@@ -59,6 +60,7 @@ import * as ProcessRunner from "../processRunner.ts";
5960import * as DeviceHost from "./DeviceHost.ts" ;
6061import * as SshDeviceHost from "./SshDeviceHost.ts" ;
6162import * as EasDeviceHost from "./EasDeviceHost.ts" ;
63+ import * as SimlockDeviceHost from "./SimlockDeviceHost.ts" ;
6264import { makeExpoApi , resolveExpoCredential } from "./ExpoApi.ts" ;
6365import * as Exit from "effect/Exit" ;
6466import * as LocalDeviceHost from "./LocalDeviceHost.ts" ;
@@ -121,7 +123,11 @@ export class DeviceService extends Context.Service<
121123 hostId : DeviceHostId ;
122124 deviceId : DeviceId ;
123125 } ) => Effect . Effect <
124- { readonly agentDeviceId : string ; readonly args : ReadonlyArray < string > } ,
126+ {
127+ readonly agentDeviceId : string | null ;
128+ readonly args : ReadonlyArray < string > ;
129+ readonly installDirectory ?: string | undefined ;
130+ } ,
125131 DeviceError
126132 > ;
127133 readonly state : Effect . Effect < DeviceServiceState > ;
@@ -173,7 +179,7 @@ export const makeWithHosts = Effect.fn("DeviceService.makeWithHosts")(function*
173179 ) ,
174180 configureAgent : (
175181 hostId : DeviceHostId ,
176- endpoint : DeviceHost . AgentDaemonEndpoint ,
182+ endpoint : DeviceHost . AgentDaemonEndpoint & { readonly stateDir ?: string | undefined } ,
177183 deviceId ?: DeviceId ,
178184 ) => Effect . Effect < string , DeviceError > = ( hostId ) =>
179185 Effect . fail (
@@ -209,6 +215,7 @@ export const makeWithHosts = Effect.fn("DeviceService.makeWithHosts")(function*
209215 supportsToolUpdate : installTool !== undefined ,
210216 supportsToolInspection : true ,
211217 supportsEasHosts : true ,
218+ supportsSimlockHosts : true ,
212219 hosts : initialHosts ,
213220 hostStatus : initialSettings . enabled ? "idle" : "disabled" ,
214221 hostStatuses : { } ,
@@ -841,39 +848,48 @@ export const makeWithHosts = Effect.fn("DeviceService.makeWithHosts")(function*
841848 reason :
842849 "Agent device access requires enabled device support, agent access, and an available simulator platform on this host." ,
843850 } ) ;
844- const { configPath, agentDeviceId } = yield * lifecycleLock . withPermit (
845- Effect . gen ( function * ( ) {
846- if ( hosts . get ( host . id ) !== host )
847- return yield * new DeviceHostUnavailableError ( {
848- hostId : host . id ,
849- hostLabel : host . label ,
850- reason : "Host configuration changed. Retry the operation." ,
851- } ) ;
852- if ( ready . devices . agentEndpoint ) {
853- const endpoint = yield * ready . devices . agentEndpoint ( input . deviceId ) ;
851+ const { configPath, agentDeviceId, boundSession, installDirectory } =
852+ yield * lifecycleLock . withPermit (
853+ Effect . gen ( function * ( ) {
854+ if ( hosts . get ( host . id ) !== host )
855+ return yield * new DeviceHostUnavailableError ( {
856+ hostId : host . id ,
857+ hostLabel : host . label ,
858+ reason : "Host configuration changed. Retry the operation." ,
859+ } ) ;
860+ if ( ready . devices . agentEndpoint ) {
861+ const endpoint = yield * ready . devices . agentEndpoint ( input . deviceId ) ;
862+ return {
863+ configPath : yield * configureAgent ( input . hostId , endpoint , input . deviceId ) ,
864+ agentDeviceId : endpoint . agentDeviceId ,
865+ boundSession : endpoint . session ,
866+ installDirectory : endpoint . installDirectory ,
867+ } ;
868+ }
869+ if ( ! ready . agentDevice )
870+ return yield * new DeviceHostUnavailableError ( {
871+ hostId : host . id ,
872+ hostLabel : host . label ,
873+ reason : "This host has no agent-device daemon." ,
874+ } ) ;
854875 return {
855- configPath : yield * configureAgent ( input . hostId , endpoint , input . deviceId ) ,
856- agentDeviceId : endpoint . agentDeviceId ,
876+ configPath : yield * configureAgent ( input . hostId , ready . agentDevice ) ,
877+ agentDeviceId : input . deviceId ,
878+ boundSession : undefined ,
879+ installDirectory : undefined ,
857880 } ;
858- }
859- if ( ! ready . agentDevice )
860- return yield * new DeviceHostUnavailableError ( {
861- hostId : host . id ,
862- hostLabel : host . label ,
863- reason : "This host has no agent-device daemon." ,
864- } ) ;
865- return {
866- configPath : yield * configureAgent ( input . hostId , ready . agentDevice ) ,
867- agentDeviceId : input . deviceId ,
868- } ;
869- } ) ,
870- ) ;
871- const session = yield * agentDeviceSession (
872- input . threadId ,
873- input . hostId ,
874- input . deviceId ,
875- ) . pipe ( Effect . provideService ( Crypto . Crypto , crypto ) ) ;
876- return { agentDeviceId, args : [ "--config" , configPath , "--session" , session ] } ;
881+ } ) ,
882+ ) ;
883+ const session =
884+ boundSession ??
885+ ( yield * agentDeviceSession ( input . threadId , input . hostId , input . deviceId ) . pipe (
886+ Effect . provideService ( Crypto . Crypto , crypto ) ,
887+ ) ) ;
888+ return {
889+ agentDeviceId,
890+ args : [ "--config" , configPath , "--session" , session ] ,
891+ installDirectory,
892+ } ;
877893 } ) ,
878894 state : SynchronizedRef . get ( stateRef ) . pipe ( Effect . map ( ( { state } ) => state ) ) ,
879895 subscribe : PubSub . subscribe ( statePubSub ) ,
@@ -932,7 +948,7 @@ export const make = Effect.gen(function* () {
932948 const configureAgent = Effect . fn ( "DeviceService.configureAgent" ) (
933949 function * (
934950 hostId : DeviceHostId ,
935- endpoint : DeviceHost . AgentDaemonEndpoint ,
951+ endpoint : DeviceHost . AgentDaemonEndpoint & { readonly stateDir ?: string | undefined } ,
936952 deviceId ?: DeviceId ,
937953 ) {
938954 const file = yield * configPath ( hostId , deviceId ) ;
@@ -994,6 +1010,23 @@ export const make = Effect.gen(function* () {
9941010 yield * Effect . context < Effect . Services < ReturnType < typeof SshDeviceHost . make > > > ( ) ;
9951011 const easContext =
9961012 yield * Effect . context < Effect . Services < ReturnType < typeof EasDeviceHost . make > > > ( ) ;
1013+ const simlockContext =
1014+ yield * Effect . context < Effect . Services < ReturnType < typeof SimlockDeviceHost . make > > > ( ) ;
1015+ const simlockAgentCli = Effect . fn ( "DeviceService.simlockAgentCli" ) (
1016+ function * ( hostId : string ) {
1017+ const nodePath = yield * resolveNodeExecutable ( "Device automation" ) ;
1018+ const tool = yield * ensureAgentDevice ( config . baseDir ) . pipe (
1019+ Effect . mapError (
1020+ ( cause ) =>
1021+ new DeviceHost . DeviceHostError ( { hostId, step : "installing agent tools" , cause } ) ,
1022+ ) ,
1023+ ) ;
1024+ return { nodePath, entryPath : tool . entryPath } ;
1025+ } ,
1026+ Effect . provideService ( FileSystem . FileSystem , fs ) ,
1027+ Effect . provideService ( Path . Path , path ) ,
1028+ Effect . provideService ( ProcessRunner . ProcessRunner , runner ) ,
1029+ ) ;
9971030 const configured = new Map < string , { config : DeviceHostConfig ; scope : Scope . Closeable } > ( ) ;
9981031 const unchanged = ( current : DeviceHostConfig , previous : DeviceHostConfig ) =>
9991032 current . kind === "ssh" && previous . kind === "ssh"
@@ -1038,6 +1071,16 @@ export const make = Effect.gen(function* () {
10381071 if ( configured . has ( host . id ) ) continue ;
10391072 added = true ;
10401073 const hostScope = yield * Scope . fork ( scope ) ;
1074+ if ( host . kind === "simlock" ) {
1075+ // Closing this scope releases the host's leases back to the pool.
1076+ const instance = yield * SimlockDeviceHost . make ( host , simlockAgentCli ( host . id ) ) . pipe (
1077+ Effect . provideService ( Scope . Scope , hostScope ) ,
1078+ Effect . provide ( simlockContext ) ,
1079+ ) ;
1080+ hosts . set ( host . id , instance ) ;
1081+ configured . set ( host . id , { config : host , scope : hostScope } ) ;
1082+ continue ;
1083+ }
10411084 if ( host . kind === "eas" ) {
10421085 // Closing this scope stops the host's running sessions, so a changed
10431086 // limit or token never leaves a billed session behind.
@@ -1079,14 +1122,22 @@ export const make = Effect.gen(function* () {
10791122 ) ;
10801123 } ) ;
10811124 const changes = yield * settings . subscribeChanges ;
1082- // Settings keep EAS hosts apart from SSH hosts ; an id both lists use stays the SSH host 's.
1125+ // Each kind has its own settings list ; an id two lists share stays the first one 's.
10831126 const configuredHosts = ( value : {
10841127 readonly deviceHosts : ReadonlyArray < SshDeviceHostConfig > ;
10851128 readonly easDeviceHosts : ReadonlyArray < EasDeviceHostConfig > ;
1086- } ) : ReadonlyArray < DeviceHostConfig > => [
1087- ...value . deviceHosts ,
1088- ...value . easDeviceHosts . filter ( ( host ) => ! value . deviceHosts . some ( ( ssh ) => ssh . id === host . id ) ) ,
1089- ] ;
1129+ readonly simlockDeviceHosts : ReadonlyArray < SimlockDeviceHostConfig > ;
1130+ readonly simlockDeviceHostsEnabled : boolean ;
1131+ } ) : ReadonlyArray < DeviceHostConfig > => {
1132+ const hosts : Array < DeviceHostConfig > = [ ...value . deviceHosts ] ;
1133+ for ( const host of [
1134+ ...value . easDeviceHosts ,
1135+ ...( value . simlockDeviceHostsEnabled ? value . simlockDeviceHosts : [ ] ) ,
1136+ ] ) {
1137+ if ( ! hosts . some ( ( existing ) => existing . id === host . id ) ) hosts . push ( host ) ;
1138+ }
1139+ return hosts ;
1140+ } ;
10901141 yield * reconcile ( configuredHosts ( yield * settings . getSettings ) ) ;
10911142 yield * changes . pipe (
10921143 Stream . runForEach ( ( value ) => reconcile ( configuredHosts ( value ) ) ) ,
0 commit comments