The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 03:37 UTC
| 1 | /** |
| 2 | * Cloud simulators from Expo Simulators. A device exists only while a session |
| 3 | * runs, and every session bills the Expo account until it stops, so this host |
| 4 | * bounds each one, stops all of them when it stops, and stops sessions a |
| 5 | * previous server left behind. It never touches sessions it did not start. |
| 6 | */ |
| 7 | import { |
| 8 | DeviceBootError, |
| 9 | type DeviceHostSummary, |
| 10 | DeviceHostUnavailableError, |
| 11 | DeviceOperationError, |
| 12 | type DevicePlatform, |
| 13 | type EasDeviceHostConfig, |
| 14 | type SettingsSecretValue, |
| 15 | } from "@t3tools/contracts"; |
| 16 | import * as Cause from "effect/Cause"; |
| 17 | import * as Clock from "effect/Clock"; |
| 18 | import * as Duration from "effect/Duration"; |
| 19 | import * as Effect from "effect/Effect"; |
| 20 | import * as Exit from "effect/Exit"; |
| 21 | import * as Fiber from "effect/Fiber"; |
| 22 | import * as FiberSet from "effect/FiberSet"; |
| 23 | import * as FileSystem from "effect/FileSystem"; |
| 24 | import * as Option from "effect/Option"; |
| 25 | import * as Path from "effect/Path"; |
| 26 | import * as Random from "effect/Random"; |
| 27 | import * as Redacted from "effect/Redacted"; |
| 28 | import * as Schedule from "effect/Schedule"; |
| 29 | import * as Schema from "effect/Schema"; |
| 30 | import * as Semaphore from "effect/Semaphore"; |
| 31 | import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/http"; |
| 32 | import { writeFileStringAtomically } from "@t3tools/shared/atomicWrite"; |
| 33 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 34 | |
| 35 | import * as ServerConfig from "../config.ts"; |
| 36 | import * as SecretResolver from "@t3tools/secret-provider-core/server/SecretResolver"; |
| 37 | import { requireSettingsSecret } from "../secrets/settingsSecretValue.ts"; |
| 38 | import { releaseAgentDeviceSession } from "./AgentDeviceTarget.ts"; |
| 39 | import * as DeviceHost from "./DeviceHost.ts"; |
| 40 | import { findEasProjectId } from "./easProjectId.ts"; |
| 41 | import { type ExpoApi, makeExpoApi, resolveExpoCredential } from "./ExpoApi.ts"; |
| 42 | |
| 43 | const POLL_INTERVAL = Duration.seconds(5); |
| 44 | // How stale a running session's status may get before listing rechecks it. |
| 45 | const STATUS_TTL_MS = 15_000; |
| 46 | // Expo's own CLI waits this long once a worker picks the session up. |
| 47 | const START_TIMEOUT = Duration.minutes(15); |
| 48 | const PREVIEW_TIMEOUT = Duration.seconds(20); |
| 49 | const decodeServeSimApi = Schema.decodeUnknownEffect( |
| 50 | Schema.Struct({ device: Schema.NonEmptyString }), |
| 51 | ); |
| 52 | const decodeServeEmuApi = Schema.decodeUnknownEffect( |
| 53 | Schema.Struct({ serial: Schema.NonEmptyString }), |
| 54 | ); |
| 55 | /** |
| 56 | * Each session's preview host serves serve-sim at its root and serve-emu under |
| 57 | * its vendor prefix, and names its device by an id of its own that agent-device |
| 58 | * also selects it by. |
| 59 | */ |
| 60 | const PLATFORMS = { |
| 61 | ios: { |
| 62 | name: "iPhone (Expo Simulator)", |
| 63 | hubPrefix: "/vendor/serve-sim", |
| 64 | previewPrefix: "", |
| 65 | protocol: "serve-sim", |
| 66 | remoteDeviceId: (api: unknown) => decodeServeSimApi(api).pipe(Effect.map((api) => api.device)), |
| 67 | }, |
| 68 | android: { |
| 69 | name: "Android (Expo Simulator)", |
| 70 | hubPrefix: "/vendor/serve-emu", |
| 71 | previewPrefix: "/vendor/serve-emu", |
| 72 | protocol: "serve-emu", |
| 73 | remoteDeviceId: (api: unknown) => decodeServeEmuApi(api).pipe(Effect.map((api) => api.serial)), |
| 74 | }, |
| 75 | } as const; |
| 76 | |
| 77 | /** Expo picks the simulator model; one entry starts a session. */ |
| 78 | const CATALOG: ReadonlyArray<DeviceHost.HostDevice> = [ |
| 79 | { |
| 80 | id: "eas-ios", |
| 81 | platform: "ios", |
| 82 | name: PLATFORMS.ios.name, |
| 83 | version: "Cloud", |
| 84 | booted: false, |
| 85 | physical: false, |
| 86 | }, |
| 87 | { |
| 88 | id: "eas-android", |
| 89 | platform: "android", |
| 90 | name: PLATFORMS.android.name, |
| 91 | version: "Cloud", |
| 92 | booted: false, |
| 93 | physical: false, |
| 94 | }, |
| 95 | ]; |
| 96 | |
| 97 | interface RunningSession { |
| 98 | readonly sessionId: string; |
| 99 | readonly name: string; |
| 100 | readonly platform: DevicePlatform; |
| 101 | readonly remoteDeviceId: string; |
| 102 | readonly preview: { readonly origin: string; readonly token: Redacted.Redacted<string> }; |
| 103 | readonly agent: DeviceHost.AgentDaemonEndpoint; |
| 104 | /** Set once a stop has been asked for but Expo has not confirmed it. */ |
| 105 | stopping: boolean; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Every session this host may have started, recorded by its unique name before |
| 110 | * Expo is asked to create it, so a server that crashes at any point can still |
| 111 | * find and stop the session on its next start. |
| 112 | */ |
| 113 | const LedgerEntry = Schema.Struct({ |
| 114 | name: Schema.String, |
| 115 | appId: Schema.String, |
| 116 | recordedAt: Schema.Number, |
| 117 | /** Expo refused the create: nothing should run, but the reaper still looks. */ |
| 118 | refused: Schema.optional(Schema.Boolean), |
| 119 | }); |
| 120 | type LedgerEntry = typeof LedgerEntry.Type; |
| 121 | const Ledger = Schema.fromJsonString(Schema.Array(LedgerEntry)); |
| 122 | const decodeLedger = Schema.decodeUnknownEffect(Ledger); |
| 123 | const encodeLedger = Schema.encodeSync(Ledger); |
| 124 | // How often listing looks for recorded sessions this host no longer tracks. |
| 125 | const REAP_INTERVAL_MS = 60_000; |
| 126 | // Expo's ready queue is unbounded despite its 115-minute session limit and five-minute cleanup. |
| 127 | // Unlisted sessions stop counting after this lifetime; forget their records only once |
| 128 | // confirmed not running or after a day unconfirmed. |
| 129 | const RECORD_LIFETIME_MS = 180 * 60_000; |
| 130 | const RECORD_MAX_AGE_MS = 24 * 60 * 60_000; |
| 131 | // Expo lists a session it made within seconds; a create with an unknown |
| 132 | // outcome stops counting against the limit once it has had this long. |
| 133 | const UNCERTAIN_CREATE_MS = 5 * 60_000; |
| 134 | // One failed request to Expo during a minutes-long start does not end the start. |
| 135 | const TRANSIENT_RETRY = { times: 3, schedule: Schedule.spaced("2 seconds") } as const; |
| 136 | |
| 137 | /** A malformed escape names no device, so the proxy answers 404 instead of failing. */ |
| 138 | const decodePathSegment = (segment: string): string | null => { |
| 139 | try { |
| 140 | return decodeURIComponent(segment); |
| 141 | } catch { |
| 142 | return null; |
| 143 | } |
| 144 | }; |
| 145 | |
| 146 | /** |
| 147 | * The Expo credential a host's setting holds or names. Every session bills the account behind |
| 148 | * it, so a named secret that cannot be read fails rather than falling back to another login. |
| 149 | */ |
| 150 | export const resolveSavedExpoCredential = ( |
| 151 | secrets: SecretResolver.SecretResolver["Service"], |
| 152 | saved: SettingsSecretValue, |
| 153 | ) => requireSettingsSecret(secrets, saved).pipe(Effect.flatMap(resolveExpoCredential)); |
| 154 | |
| 155 | export const make = Effect.fn("EasDeviceHost.make")(function* (config: EasDeviceHostConfig) { |
| 156 | const http = yield* HttpClient.HttpClient; |
| 157 | const fs = yield* FileSystem.FileSystem; |
| 158 | const path = yield* Path.Path; |
| 159 | const server = yield* ServerConfig.ServerConfig; |
| 160 | const secrets = yield* SecretResolver.SecretResolver; |
| 161 | const services = yield* Effect.context< |
| 162 | FileSystem.FileSystem | Path.Path | Effect.Services<ReturnType<typeof findEasProjectId>> |
| 163 | >(); |
| 164 | const hostId = config.id; |
| 165 | const ledgerPath = path.join(server.stateDir, "device", "eas", `${hostId}.json`); |
| 166 | // Keyed by Expo's session id, which is also the device id: every Expo |
| 167 | // simulator reports the same remote device id, so that id cannot tell sessions apart. |
| 168 | const sessions = new Map<string, RunningSession>(); |
| 169 | const checkedAt = new Map<string, number>(); |
| 170 | const startLock = yield* Semaphore.make(1); |
| 171 | const ledgerLock = yield* Semaphore.make(1); |
| 172 | // One sweep at a time: two would stop the same session twice. |
| 173 | const reapLock = yield* Semaphore.make(1); |
| 174 | // Starts run in the host rather than the caller: a client that disconnects |
| 175 | // while a session boots leaves it running and tracked, and stopping or |
| 176 | // closing the host interrupts the start, which then stops its session. |
| 177 | const starts = yield* FiberSet.make<string, unknown>(); |
| 178 | // The startup sweep for recorded leftovers. |
| 179 | const background = yield* FiberSet.make<void, never>(); |
| 180 | // Sessions this host owns that `sessions` does not hold: starts in progress, |
| 181 | // and sessions whose stop after a failed start failed too. The reaper leaves |
| 182 | // them alone and they count against the limit. |
| 183 | const starting = new Set<string>(); |
| 184 | const unresolved = new Map<string, { readonly sessionId: string }>(); |
| 185 | // Creates whose outcome is unknown (no answer, or a server error), and |
| 186 | // fresh records a previous server left: Expo may have made the session. |
| 187 | // Each listing looks for it, and it counts against the limit until found |
| 188 | // or until Expo would have listed it by now. The reaper handles these. |
| 189 | const uncertain = new Map<string, { readonly entry: LedgerEntry; readonly until: number }>(); |
| 190 | const ownedNames = () => |
| 191 | new Set([ |
| 192 | ...[...sessions.values()].map((session) => session.name), |
| 193 | ...starting, |
| 194 | ...unresolved.keys(), |
| 195 | ]); |
| 196 | // Bumped whenever the host stops its sessions, so a start that finishes |
| 197 | // afterwards stops its own session instead of leaving it untracked. |
| 198 | let epoch = 0; |
| 199 | // Set once the host's scope closes. A listing still running on this |
| 200 | // instance then neither sweeps nor writes the ledger: a replacement with |
| 201 | // the same id owns the sessions and the record now. |
| 202 | let closed = false; |
| 203 | let reapedAt = 0; |
| 204 | let ready: DeviceHost.DeviceHostReady | null = null; |
| 205 | |
| 206 | const unavailable = (reason: string, cause?: unknown) => |
| 207 | new DeviceHostUnavailableError({ |
| 208 | hostId, |
| 209 | hostLabel: config.label, |
| 210 | reason, |
| 211 | ...(cause === undefined ? {} : { cause }), |
| 212 | }); |
| 213 | |
| 214 | let lastApi: ExpoApi | undefined; |
| 215 | const api: Effect.Effect<ExpoApi, DeviceHostUnavailableError> = resolveSavedExpoCredential( |
| 216 | secrets, |
| 217 | config.accessToken, |
| 218 | ).pipe( |
| 219 | Effect.map((credential) => makeExpoApi(http, credential)), |
| 220 | Effect.tap((expo) => |
| 221 | Effect.sync(() => { |
| 222 | lastApi = expo; |
| 223 | }), |
| 224 | ), |
| 225 | Effect.provide(services), |
| 226 | Effect.mapError((error) => unavailable(error.message, error)), |
| 227 | ); |
| 228 | /** Stopping a billed session must not depend on the secret still being readable; starting one must. */ |
| 229 | const apiForStop = api.pipe( |
| 230 | Effect.catch((error) => (lastApi === undefined ? Effect.fail(error) : Effect.succeed(lastApi))), |
| 231 | ); |
| 232 | |
| 233 | /** A missing ledger is empty; an unreadable one is an error, never an empty list. */ |
| 234 | const readLedger = fs.readFileString(ledgerPath).pipe( |
| 235 | Effect.catchIf( |
| 236 | (error) => error.reason._tag === "NotFound", |
| 237 | () => Effect.succeed("[]"), |
| 238 | ), |
| 239 | Effect.flatMap(decodeLedger), |
| 240 | Effect.mapError((cause) => |
| 241 | unavailable( |
| 242 | `The record of running Expo Simulators sessions at ${ledgerPath} could not be read. Fix or remove it.`, |
| 243 | cause, |
| 244 | ), |
| 245 | ), |
| 246 | ); |
| 247 | const writeLedger = Effect.fn("EasDeviceHost.writeLedger")(function* ( |
| 248 | update: (entries: ReadonlyArray<LedgerEntry>) => ReadonlyArray<LedgerEntry>, |
| 249 | ) { |
| 250 | if (closed) return yield* unavailable(`${config.label} was replaced or removed.`); |
| 251 | const entries = update(yield* readLedger); |
| 252 | yield* writeFileStringAtomically({ |
| 253 | filePath: ledgerPath, |
| 254 | contents: encodeLedger(entries), |
| 255 | }).pipe( |
| 256 | Effect.provide(services), |
| 257 | Effect.mapError((cause) => |
| 258 | unavailable( |
| 259 | `The record of running Expo Simulators sessions at ${ledgerPath} could not be written.`, |
| 260 | cause, |
| 261 | ), |
| 262 | ), |
| 263 | ); |
| 264 | }, ledgerLock.withPermit); |
| 265 | const markRefused = (name: string) => |
| 266 | writeLedger((entries) => |
| 267 | entries.map((entry) => (entry.name === name ? { ...entry, refused: true } : entry)), |
| 268 | ).pipe( |
| 269 | Effect.catch((cause) => |
| 270 | Effect.logWarning("Could not update the Expo Simulators session record", { name, cause }), |
| 271 | ), |
| 272 | ); |
| 273 | const forgetEntry = (name: string) => |
| 274 | writeLedger((entries) => entries.filter((entry) => entry.name !== name)).pipe( |
| 275 | Effect.catch((cause) => |
| 276 | Effect.logWarning("Could not update the Expo Simulators session record", { name, cause }), |
| 277 | ), |
| 278 | ); |
| 279 | |
| 280 | /** Stops a session and, once Expo confirms it, drops it from the ledger. */ |
| 281 | const stopSession = (expo: ExpoApi, sessionId: string, name: string) => |
| 282 | expo.stop(sessionId).pipe( |
| 283 | Effect.andThen(forgetEntry(name)), |
| 284 | Effect.tapError((cause) => |
| 285 | Effect.logWarning("Could not stop Expo Simulators session", { sessionId, cause }), |
| 286 | ), |
| 287 | ); |
| 288 | |
| 289 | /** Finds recorded sessions by name, so a session whose id never arrived is found too. */ |
| 290 | const stopRecorded = (expo: ExpoApi, entries: ReadonlyArray<LedgerEntry>) => { |
| 291 | const found = new Set<string>(); |
| 292 | const listed = new Set<string>(); |
| 293 | return Effect.forEach( |
| 294 | [...new Set(entries.map((entry) => entry.appId))], |
| 295 | (appId) => |
| 296 | expo.activeSessions(appId).pipe( |
| 297 | Effect.flatMap((active) => |
| 298 | Effect.forEach( |
| 299 | entries.filter((entry) => entry.appId === appId), |
| 300 | (entry) => |
| 301 | Effect.gen(function* () { |
| 302 | const running = active.filter((session) => session.name === entry.name); |
| 303 | if (running.length > 0) { |
| 304 | found.add(entry.name); |
| 305 | return yield* Effect.forEach( |
| 306 | running, |
| 307 | (session) => |
| 308 | stopSession(expo, session.id, entry.name).pipe( |
| 309 | // Still running and billed: owned, counted, and retried by listing. |
| 310 | Effect.catch(() => |
| 311 | Effect.sync( |
| 312 | () => void unresolved.set(entry.name, { sessionId: session.id }), |
| 313 | ), |
| 314 | ), |
| 315 | ), |
| 316 | { discard: true }, |
| 317 | ); |
| 318 | } |
| 319 | }), |
| 320 | { discard: true }, |
| 321 | ), |
| 322 | ), |
| 323 | Effect.tap(() => Effect.sync(() => void listed.add(appId))), |
| 324 | Effect.ignore, |
| 325 | ), |
| 326 | { discard: true, concurrency: 4 }, |
| 327 | ).pipe( |
| 328 | Effect.as({ found, listed } as { |
| 329 | readonly found: ReadonlySet<string>; |
| 330 | /** The apps whose listing succeeded; a failed one rules nothing out. */ |
| 331 | readonly listed: ReadonlySet<string>; |
| 332 | }), |
| 333 | ); |
| 334 | }; |
| 335 | |
| 336 | /** |
| 337 | * Retries the stop of a session whose stop failed. A session Expo already |
| 338 | * ended is let go even if stopping it keeps failing. |
| 339 | */ |
| 340 | const retryUnresolved = (expo: ExpoApi, name: string, sessionId: string) => |
| 341 | stopSession(expo, sessionId, name).pipe( |
| 342 | Effect.catch(() => |
| 343 | expo |
| 344 | .session(sessionId) |
| 345 | .pipe( |
| 346 | Effect.flatMap((current) => |
| 347 | current.status === "STOPPED" || current.status === "ERRORED" |
| 348 | ? forgetEntry(name) |
| 349 | : Effect.fail(current.status), |
| 350 | ), |
| 351 | ), |
| 352 | ), |
| 353 | Effect.andThen(Effect.sync(() => void unresolved.delete(name))), |
| 354 | Effect.ignore, |
| 355 | ); |
| 356 | |
| 357 | /** |
| 358 | * Records a previous server left count against the limit until a sweep |
| 359 | * finds them (a fresh one until Expo would have listed it by now), so a |
| 360 | * restart cannot start past it. |
| 361 | */ |
| 362 | const adoptRecorded = Effect.gen(function* () { |
| 363 | const now = yield* Clock.currentTimeMillis; |
| 364 | const owned = ownedNames(); |
| 365 | for (const entry of yield* readLedger) { |
| 366 | const until = Math.max(entry.recordedAt + UNCERTAIN_CREATE_MS, now); |
| 367 | // A refused create made nothing: its record is for the reaper only. |
| 368 | if (!owned.has(entry.name) && !entry.refused) uncertain.set(entry.name, { entry, until }); |
| 369 | } |
| 370 | }).pipe(Effect.ignore); |
| 371 | |
| 372 | /** |
| 373 | * Stops recorded sessions this host does not own: a previous server's, or |
| 374 | * a failed or uncertain start's. An uncertain one found (stopped, or |
| 375 | * unresolved if that failed) or long enough unlisted frees its slot; its |
| 376 | * record stays until it expires. |
| 377 | */ |
| 378 | const reapLeftovers = Effect.fn("EasDeviceHost.reapLeftovers")( |
| 379 | function* (expo: ExpoApi) { |
| 380 | if (closed) return; |
| 381 | reapedAt = yield* Clock.currentTimeMillis; |
| 382 | const entries = yield* readLedger; |
| 383 | const owned = ownedNames(); |
| 384 | const leftovers = entries.filter((entry) => !owned.has(entry.name)); |
| 385 | const { found, listed } = yield* stopRecorded(expo, leftovers); |
| 386 | for (const [name, { entry, until }] of uncertain) { |
| 387 | if (found.has(name) || (reapedAt >= until && listed.has(entry.appId))) { |
| 388 | uncertain.delete(name); |
| 389 | } |
| 390 | } |
| 391 | // Expo has ended these by now unless they waited in its queue: the slot |
| 392 | // goes whatever the listing says (the app may be gone, or belong to |
| 393 | // another account); the record goes once a listing confirms Expo does |
| 394 | // not run it, or once no queue could have held it. |
| 395 | for (const entry of leftovers) { |
| 396 | const age = reapedAt - entry.recordedAt; |
| 397 | if (age > RECORD_LIFETIME_MS) uncertain.delete(entry.name); |
| 398 | if (found.has(entry.name)) continue; |
| 399 | if ((age > RECORD_LIFETIME_MS && listed.has(entry.appId)) || age > RECORD_MAX_AGE_MS) { |
| 400 | yield* forgetEntry(entry.name); |
| 401 | } |
| 402 | } |
| 403 | }, |
| 404 | reapLock.withPermit, |
| 405 | Effect.catch((cause) => |
| 406 | Effect.logWarning("Could not stop recorded Expo Simulators sessions", { cause }), |
| 407 | ), |
| 408 | ); |
| 409 | |
| 410 | const previewDevice = ( |
| 411 | origin: string, |
| 412 | token: Redacted.Redacted<string>, |
| 413 | platform: DevicePlatform, |
| 414 | ) => |
| 415 | http |
| 416 | .execute( |
| 417 | HttpClientRequest.get(`${origin}${PLATFORMS[platform].previewPrefix}/api`).pipe( |
| 418 | HttpClientRequest.bearerToken(Redacted.value(token)), |
| 419 | ), |
| 420 | ) |
| 421 | .pipe( |
| 422 | Effect.flatMap(HttpClientResponse.filterStatusOk), |
| 423 | Effect.flatMap((response) => response.json), |
| 424 | Effect.flatMap(PLATFORMS[platform].remoteDeviceId), |
| 425 | Effect.scoped, |
| 426 | Effect.timeout(PREVIEW_TIMEOUT), |
| 427 | Effect.retry(TRANSIENT_RETRY), |
| 428 | ); |
| 429 | |
| 430 | const awaitRunning = Effect.fn("EasDeviceHost.awaitRunning")( |
| 431 | function* (expo: ExpoApi, sessionId: string) { |
| 432 | for (;;) { |
| 433 | const session = yield* expo.session(sessionId).pipe( |
| 434 | Effect.retry(TRANSIENT_RETRY), |
| 435 | Effect.mapError((error) => unavailable(error.message, error)), |
| 436 | ); |
| 437 | if (session.status === "STOPPED" || session.status === "ERRORED") { |
| 438 | return yield* new DeviceBootError({ |
| 439 | hostId, |
| 440 | deviceId: sessionId, |
| 441 | reason: "launch_failed", |
| 442 | cause: new Error( |
| 443 | `Expo Simulators session ended (${session.status}) before it was ready`, |
| 444 | ), |
| 445 | }); |
| 446 | } |
| 447 | if (session.status === "IN_PROGRESS" && session.endpoints) return session.endpoints; |
| 448 | yield* Effect.sleep(POLL_INTERVAL); |
| 449 | } |
| 450 | }, |
| 451 | (poll, _expo, sessionId) => |
| 452 | poll.pipe( |
| 453 | Effect.timeoutOrElse({ |
| 454 | duration: START_TIMEOUT, |
| 455 | orElse: () => |
| 456 | Effect.fail( |
| 457 | new DeviceBootError({ |
| 458 | hostId, |
| 459 | deviceId: sessionId, |
| 460 | reason: "timeout", |
| 461 | cause: new Error("Expo Simulators session did not start in time"), |
| 462 | }), |
| 463 | ), |
| 464 | }), |
| 465 | ), |
| 466 | ); |
| 467 | |
| 468 | const reserveSlot = (name: string) => |
| 469 | startLock.withPermit( |
| 470 | Effect.suspend(() => { |
| 471 | const pending = unresolved.size + uncertain.size; |
| 472 | if (sessions.size + starting.size + pending >= config.maxConcurrentSessions) { |
| 473 | const limit = `${config.label} is limited to ${config.maxConcurrentSessions} running ${config.maxConcurrentSessions === 1 ? "session" : "sessions"}.`; |
| 474 | return Effect.fail( |
| 475 | unavailable( |
| 476 | pending > 0 |
| 477 | ? `${limit} ${pending === 1 ? "A session" : `${pending} sessions`} that failed to start or stop ${pending === 1 ? "is" : "are"} still being cleaned up; try again shortly.` |
| 478 | : `${limit} Close one in the Device panel first.`, |
| 479 | ), |
| 480 | ); |
| 481 | } |
| 482 | starting.add(name); |
| 483 | return Effect.void; |
| 484 | }), |
| 485 | ); |
| 486 | |
| 487 | const startSession = Effect.fn("EasDeviceHost.startSession")(function* ( |
| 488 | device: DeviceHost.HostDevice, |
| 489 | context: DeviceHost.DeviceOpenContext | undefined, |
| 490 | startedIn: number, |
| 491 | ) { |
| 492 | const name = `T3 Code · ${config.label} · ${(yield* Random.nextIntBetween(0, 0xffffff)).toString(16).padStart(6, "0")}`; |
| 493 | // The credential comes first: without one, the answer is how to sign in, |
| 494 | // not that the limit is reached. |
| 495 | const expo = yield* api; |
| 496 | return yield* Effect.acquireUseRelease( |
| 497 | reserveSlot(name), |
| 498 | () => |
| 499 | Effect.gen(function* () { |
| 500 | const appId = |
| 501 | config.appId ?? |
| 502 | Option.getOrUndefined( |
| 503 | context?.workspaceRoot |
| 504 | ? yield* findEasProjectId(context.workspaceRoot).pipe(Effect.provide(services)) |
| 505 | : Option.none<string>(), |
| 506 | ); |
| 507 | if (!appId) { |
| 508 | return yield* unavailable( |
| 509 | "No Expo project to run the session under. Choose one for this host in Settings, or open the device from a thread whose project names extra.eas.projectId in app.json or its app config.", |
| 510 | ); |
| 511 | } |
| 512 | const entry = { name, appId, recordedAt: yield* Clock.currentTimeMillis }; |
| 513 | // Recorded first: a session the ledger cannot hold is never started. |
| 514 | yield* writeLedger((entries) => [...entries, entry]); |
| 515 | if (epoch !== startedIn) { |
| 516 | yield* forgetEntry(name); |
| 517 | return yield* unavailable(`${config.label} was stopped while the session started.`); |
| 518 | } |
| 519 | // Create runs uninterrupted (Expo's API answers within 30 seconds) and |
| 520 | // the stop below is in place before anything can interrupt the start, |
| 521 | // so a session Expo made is always either tracked or stopped. |
| 522 | return yield* Effect.uninterruptibleMask((restore) => |
| 523 | Effect.gen(function* () { |
| 524 | const sessionId = yield* expo |
| 525 | .create({ |
| 526 | appId, |
| 527 | platform: device.platform, |
| 528 | name, |
| 529 | maxRunTimeMinutes: config.maxSessionMinutes, |
| 530 | maxIdleTimeMinutes: config.idleMinutes, |
| 531 | }) |
| 532 | .pipe( |
| 533 | // Unless Expo refused, it may have made the session before |
| 534 | // failing to answer. Unlisted, it stays counted for a while. |
| 535 | // Refused, it made nothing: the slot frees, but the record |
| 536 | // stays for the reaper in case the refusal was misread. |
| 537 | Effect.tapError((error) => |
| 538 | error.rejected |
| 539 | ? stopRecorded(expo, [entry]).pipe( |
| 540 | Effect.flatMap(({ found }) => |
| 541 | found.has(name) ? Effect.void : markRefused(name), |
| 542 | ), |
| 543 | ) |
| 544 | : stopRecorded(expo, [entry]).pipe( |
| 545 | Effect.flatMap(({ found }) => |
| 546 | found.has(name) |
| 547 | ? Effect.void |
| 548 | : Effect.map(Clock.currentTimeMillis, (now) => { |
| 549 | uncertain.set(name, { |
| 550 | entry, |
| 551 | until: now + UNCERTAIN_CREATE_MS, |
| 552 | }); |
| 553 | }), |
| 554 | ), |
| 555 | ), |
| 556 | ), |
| 557 | Effect.mapError((error) => unavailable(error.message, error)), |
| 558 | ); |
| 559 | return yield* restore( |
| 560 | trackSession(expo, sessionId, name, device.platform, startedIn), |
| 561 | ).pipe( |
| 562 | Effect.onError(() => |
| 563 | stopSession(expo, sessionId, name).pipe( |
| 564 | // Still running and billed: owned, counted, and retried by listing. |
| 565 | Effect.catch(() => Effect.sync(() => void unresolved.set(name, { sessionId }))), |
| 566 | ), |
| 567 | ), |
| 568 | ); |
| 569 | }), |
| 570 | ); |
| 571 | }), |
| 572 | () => Effect.sync(() => void starting.delete(name)), |
| 573 | ); |
| 574 | }); |
| 575 | |
| 576 | /** Waits for a created session and starts tracking it. */ |
| 577 | const trackSession = Effect.fn("EasDeviceHost.trackSession")(function* ( |
| 578 | expo: ExpoApi, |
| 579 | sessionId: string, |
| 580 | name: string, |
| 581 | platform: DevicePlatform, |
| 582 | startedIn: number, |
| 583 | ) { |
| 584 | const endpoints = yield* awaitRunning(expo, sessionId); |
| 585 | if (!endpoints.preview) { |
| 586 | return yield* new DeviceBootError({ |
| 587 | hostId, |
| 588 | deviceId: sessionId, |
| 589 | reason: "launch_failed", |
| 590 | cause: new Error("Expo Simulators returned no preview endpoint"), |
| 591 | }); |
| 592 | } |
| 593 | const origin = new URL(endpoints.preview.url).origin; |
| 594 | const remoteDeviceId = yield* previewDevice(origin, endpoints.preview.token, platform).pipe( |
| 595 | Effect.mapError( |
| 596 | (cause) => |
| 597 | new DeviceBootError({ |
| 598 | hostId, |
| 599 | deviceId: sessionId, |
| 600 | reason: "launch_failed", |
| 601 | cause, |
| 602 | }), |
| 603 | ), |
| 604 | ); |
| 605 | if (epoch !== startedIn) { |
| 606 | return yield* unavailable(`${config.label} was stopped while the session started.`); |
| 607 | } |
| 608 | sessions.set(sessionId, { |
| 609 | sessionId, |
| 610 | name, |
| 611 | platform, |
| 612 | remoteDeviceId, |
| 613 | preview: { origin, token: endpoints.preview.token }, |
| 614 | agent: { |
| 615 | baseUrl: endpoints.agentDevice.url, |
| 616 | token: Redacted.value(endpoints.agentDevice.token), |
| 617 | }, |
| 618 | stopping: false, |
| 619 | }); |
| 620 | return sessionId; |
| 621 | }); |
| 622 | |
| 623 | const sessionFor = (deviceId: string) => Option.fromNullishOr(sessions.get(deviceId)); |
| 624 | |
| 625 | const forget = (session: RunningSession) => |
| 626 | Effect.sync(() => { |
| 627 | sessions.delete(session.sessionId); |
| 628 | checkedAt.delete(session.sessionId); |
| 629 | }); |
| 630 | |
| 631 | /** |
| 632 | * Expo ends sessions on its own at their run or idle limit; stop listing |
| 633 | * those. A stop that failed earlier is retried, and recorded sessions nothing |
| 634 | * tracks are reaped now and then. |
| 635 | */ |
| 636 | const dropEnded = Effect.gen(function* () { |
| 637 | if (closed) return; |
| 638 | const now = yield* Clock.currentTimeMillis; |
| 639 | const stale = [...sessions.values()].filter( |
| 640 | (session) => |
| 641 | session.stopping || now - (checkedAt.get(session.sessionId) ?? 0) > STATUS_TTL_MS, |
| 642 | ); |
| 643 | const reapDue = now - reapedAt > REAP_INTERVAL_MS; |
| 644 | if (stale.length === 0 && unresolved.size === 0 && uncertain.size === 0 && !reapDue) return; |
| 645 | const reap = reapDue || uncertain.size > 0; |
| 646 | if (reap) reapedAt = now; |
| 647 | const expo = yield* apiForStop.pipe( |
| 648 | // Without a credential no sweep can look; a record Expo would have |
| 649 | // listed by now still stops counting, so the limit is not held forever. |
| 650 | Effect.tapError(() => |
| 651 | Effect.sync(() => { |
| 652 | for (const [name, { until }] of uncertain) if (now >= until) uncertain.delete(name); |
| 653 | }), |
| 654 | ), |
| 655 | ); |
| 656 | yield* Effect.forEach( |
| 657 | [...unresolved], |
| 658 | ([name, { sessionId }]) => retryUnresolved(expo, name, sessionId), |
| 659 | { discard: true, concurrency: 4 }, |
| 660 | ); |
| 661 | yield* Effect.forEach( |
| 662 | stale, |
| 663 | (session) => |
| 664 | (session.stopping |
| 665 | ? stopSession(expo, session.sessionId, session.name).pipe( |
| 666 | Effect.andThen(forget(session)), |
| 667 | // A session Expo already ended is let go even if stopping it keeps failing. |
| 668 | Effect.catch(() => |
| 669 | expo |
| 670 | .session(session.sessionId) |
| 671 | .pipe( |
| 672 | Effect.flatMap((current) => |
| 673 | current.status === "STOPPED" || current.status === "ERRORED" |
| 674 | ? forget(session).pipe(Effect.andThen(forgetEntry(session.name))) |
| 675 | : Effect.fail(current.status), |
| 676 | ), |
| 677 | ), |
| 678 | ), |
| 679 | ) |
| 680 | : expo |
| 681 | .session(session.sessionId) |
| 682 | .pipe( |
| 683 | Effect.flatMap((current) => |
| 684 | current.status === "STOPPED" || current.status === "ERRORED" |
| 685 | ? forget(session).pipe(Effect.andThen(forgetEntry(session.name))) |
| 686 | : Effect.sync(() => void checkedAt.set(session.sessionId, now)), |
| 687 | ), |
| 688 | ) |
| 689 | ).pipe(Effect.ignore), |
| 690 | { discard: true, concurrency: 4 }, |
| 691 | ); |
| 692 | if (reap) yield* reapLeftovers(expo); |
| 693 | }).pipe(Effect.ignore); |
| 694 | |
| 695 | /** |
| 696 | * Stops a tracked session. Until Expo confirms, it stays tracked: listed, |
| 697 | * counted against the limit, and retried by the next listing. |
| 698 | */ |
| 699 | const stopTracked = (expo: ExpoApi, session: RunningSession) => |
| 700 | Effect.suspend(() => { |
| 701 | session.stopping = true; |
| 702 | return stopSession(expo, session.sessionId, session.name); |
| 703 | }).pipe(Effect.andThen(forget(session))); |
| 704 | |
| 705 | const devices: DeviceHost.DeviceOperations = { |
| 706 | list: dropEnded.pipe( |
| 707 | Effect.andThen( |
| 708 | Effect.sync(() => ({ |
| 709 | devices: [ |
| 710 | ...CATALOG, |
| 711 | ...[...sessions.values()].map((session): DeviceHost.HostDevice => ({ |
| 712 | id: session.sessionId, |
| 713 | platform: session.platform, |
| 714 | name: PLATFORMS[session.platform].name, |
| 715 | version: "Cloud", |
| 716 | booted: true, |
| 717 | physical: false, |
| 718 | })), |
| 719 | ], |
| 720 | detail: undefined, |
| 721 | })), |
| 722 | ), |
| 723 | ), |
| 724 | boot: (device, context) => { |
| 725 | if (sessions.has(device.id)) return Effect.succeed(device.id); |
| 726 | // Read before anything yields, so a stop that lands while the start |
| 727 | // reads settings or files is seen. |
| 728 | const startedIn = epoch; |
| 729 | return startSession(device, context, startedIn).pipe( |
| 730 | FiberSet.run(starts), |
| 731 | Effect.flatMap(Fiber.await), |
| 732 | Effect.flatMap((exit) => |
| 733 | Exit.isFailure(exit) && Cause.hasInterruptsOnly(exit.cause) |
| 734 | ? Effect.fail(unavailable(`${config.label} was stopped while the session started.`)) |
| 735 | : exit, |
| 736 | ), |
| 737 | ); |
| 738 | }, |
| 739 | attach: () => Effect.void, |
| 740 | shutdown: (device) => |
| 741 | Option.match(sessionFor(device.id), { |
| 742 | onNone: () => Effect.void, |
| 743 | onSome: (session) => |
| 744 | apiForStop.pipe( |
| 745 | Effect.flatMap((expo) => stopTracked(expo, session)), |
| 746 | Effect.asVoid, |
| 747 | Effect.mapError( |
| 748 | (cause) => |
| 749 | new DeviceOperationError({ |
| 750 | operation: "shutdown", |
| 751 | reason: "request_failed", |
| 752 | cause, |
| 753 | }), |
| 754 | ), |
| 755 | ), |
| 756 | }), |
| 757 | screenshot: (device) => |
| 758 | Option.match(sessionFor(device.id), { |
| 759 | onNone: () => |
| 760 | Effect.fail( |
| 761 | new DeviceOperationError({ |
| 762 | operation: "screenshot", |
| 763 | reason: "request_failed", |
| 764 | cause: new Error("The Expo Simulator is not running"), |
| 765 | }), |
| 766 | ), |
| 767 | onSome: (session) => |
| 768 | http |
| 769 | .execute( |
| 770 | HttpClientRequest.post( |
| 771 | `${session.preview.origin}${PLATFORMS[session.platform].previewPrefix}/api/screenshot?device=${encodeURIComponent(session.remoteDeviceId)}`, |
| 772 | ).pipe(HttpClientRequest.bearerToken(Redacted.value(session.preview.token))), |
| 773 | ) |
| 774 | .pipe( |
| 775 | Effect.flatMap(HttpClientResponse.filterStatusOk), |
| 776 | Effect.flatMap((response) => response.arrayBuffer), |
| 777 | Effect.map((buffer) => new Uint8Array(buffer)), |
| 778 | Effect.scoped, |
| 779 | Effect.timeout(PREVIEW_TIMEOUT), |
| 780 | Effect.mapError( |
| 781 | (cause) => |
| 782 | new DeviceOperationError({ |
| 783 | operation: "screenshot", |
| 784 | reason: "request_failed", |
| 785 | cause, |
| 786 | }), |
| 787 | ), |
| 788 | ), |
| 789 | }), |
| 790 | agentEndpoint: (deviceId) => |
| 791 | Option.match(sessionFor(deviceId), { |
| 792 | onNone: () => Effect.fail(unavailable("This Expo Simulator is no longer running.")), |
| 793 | onSome: (session) => |
| 794 | Effect.succeed({ ...session.agent, agentDeviceId: session.remoteDeviceId }), |
| 795 | }), |
| 796 | endsDevices: true, |
| 797 | }; |
| 798 | |
| 799 | /** |
| 800 | * Clients name the device by its session id; the preview host knows it by its |
| 801 | * own id, so the session id is swapped for that in the path and in the |
| 802 | * `device` parameter. A session answers only on its own platform's server. |
| 803 | */ |
| 804 | const upstream: NonNullable<DeviceHost.DeviceHostReady["upstream"]> = (hubPath, search) => |
| 805 | Effect.sync(() => { |
| 806 | const platform = (["ios", "android"] as const).find((candidate) => |
| 807 | hubPath.startsWith(`${PLATFORMS[candidate].hubPrefix}/`), |
| 808 | ); |
| 809 | if (!platform) return null; |
| 810 | const helper = /^\/vendor\/serve-sim\/helper\/([^/]+)\//.exec(hubPath); |
| 811 | const deviceId = helper ? decodePathSegment(helper[1]!) : search.get("device"); |
| 812 | if (helper && deviceId === null) return null; |
| 813 | const session = |
| 814 | deviceId !== null |
| 815 | ? sessions.get(deviceId) |
| 816 | : sessions.size === 1 |
| 817 | ? [...sessions.values()][0] |
| 818 | : undefined; |
| 819 | if (session?.platform !== platform) return null; |
| 820 | const { hubPrefix, previewPrefix, protocol } = PLATFORMS[platform]; |
| 821 | const path = helper |
| 822 | ? `/helper/${encodeURIComponent(session.remoteDeviceId)}${hubPath.slice(helper[0].length - 1)}` |
| 823 | : `${previewPrefix}${hubPath.slice(hubPrefix.length)}`; |
| 824 | const params = new URLSearchParams(search); |
| 825 | if (params.has("device")) params.set("device", session.remoteDeviceId); |
| 826 | const query = params.size > 0 ? `?${params.toString()}` : ""; |
| 827 | const token = Redacted.value(session.preview.token); |
| 828 | return { |
| 829 | url: `${session.preview.origin}${path}${query}`, |
| 830 | headers: { authorization: `Bearer ${token}` }, |
| 831 | protocols: [`${protocol}.token.${token}`], |
| 832 | }; |
| 833 | }); |
| 834 | |
| 835 | const nodePath = yield* HostProcess.ExecutablePath; |
| 836 | const ensureReady: DeviceHost.DeviceHost["Service"]["ensureReady"] = Effect.fn( |
| 837 | "EasDeviceHost.ensureReady", |
| 838 | )(function* () { |
| 839 | if (ready) return ready; |
| 840 | yield* adoptRecorded; |
| 841 | // Without a credential the host is still ready: opening a device then |
| 842 | // says how to sign in, and listing reaps once one is there. |
| 843 | yield* FiberSet.run(background)(api.pipe(Effect.flatMap(reapLeftovers), Effect.ignore)); |
| 844 | ready = { |
| 845 | nodePath, |
| 846 | hub: { origin: "https://api.expo.dev" }, |
| 847 | devices, |
| 848 | upstream, |
| 849 | // Each session runs its own daemon, so one that has ended holds no agent sessions. |
| 850 | releaseAgentSession: (session, deviceId) => |
| 851 | Option.match(sessionFor(deviceId), { |
| 852 | onNone: () => Effect.void, |
| 853 | onSome: (running) => releaseAgentDeviceSession(http, running.agent)(session), |
| 854 | }), |
| 855 | // Device settings run simctl on the simulator's own machine, which Expo does not expose. |
| 856 | run: () => |
| 857 | Effect.succeed({ |
| 858 | stdout: "", |
| 859 | stderr: "Devices from Expo Simulators cannot run host commands.", |
| 860 | code: 127, |
| 861 | }), |
| 862 | helpers: { serveSimAxSettings: null, serveSimCli: null }, |
| 863 | }; |
| 864 | return ready; |
| 865 | }); |
| 866 | |
| 867 | // A session whose stop fails stays tracked for the next listing to retry, |
| 868 | // and stays in the ledger for the next server if this one closes first. |
| 869 | const stopAll = Effect.gen(function* () { |
| 870 | epoch++; |
| 871 | // Interrupted starts stop the sessions they created before this returns. |
| 872 | yield* FiberSet.clear(starts); |
| 873 | yield* FiberSet.clear(background); |
| 874 | const expo = yield* apiForStop; |
| 875 | yield* Effect.forEach( |
| 876 | [...sessions.values()], |
| 877 | (session) => stopTracked(expo, session).pipe(Effect.ignore), |
| 878 | { discard: true, concurrency: 4 }, |
| 879 | ); |
| 880 | yield* Effect.forEach( |
| 881 | [...unresolved], |
| 882 | ([name, { sessionId }]) => retryUnresolved(expo, name, sessionId), |
| 883 | { discard: true, concurrency: 4 }, |
| 884 | ); |
| 885 | // Also stop a recorded session a create with an unknown outcome made after all. |
| 886 | yield* reapLeftovers(expo); |
| 887 | }).pipe(Effect.ignore); |
| 888 | yield* Effect.addFinalizer(() => |
| 889 | stopAll.pipe( |
| 890 | // Under the ledger lock, so a write still in flight lands before a |
| 891 | // replacement writes its own, which that write would overwrite. |
| 892 | Effect.andThen( |
| 893 | ledgerLock.withPermit( |
| 894 | Effect.sync(() => { |
| 895 | closed = true; |
| 896 | }), |
| 897 | ), |
| 898 | ), |
| 899 | ), |
| 900 | ); |
| 901 | |
| 902 | // Each server signs in on its own, so one with no Expo credential answers for itself. Reading |
| 903 | // the credential is local; whether Expo accepts it is only known once a session starts. |
| 904 | const platformAvailability = (platform: DevicePlatform) => |
| 905 | resolveSavedExpoCredential(secrets, config.accessToken).pipe( |
| 906 | Effect.provide(services), |
| 907 | Effect.as({ platform, available: true }), |
| 908 | Effect.catch((error) => |
| 909 | Effect.succeed({ platform, available: false, reason: error.message }), |
| 910 | ), |
| 911 | ); |
| 912 | |
| 913 | const summary: Effect.Effect<DeviceHostSummary> = Effect.all([ |
| 914 | platformAvailability("ios"), |
| 915 | platformAvailability("android"), |
| 916 | ]).pipe( |
| 917 | Effect.map((platforms) => ({ |
| 918 | id: hostId, |
| 919 | label: config.label, |
| 920 | kind: "eas" as const, |
| 921 | platforms, |
| 922 | // Nothing to install: the hub and agent-device daemon run in each session. |
| 923 | hubInstalled: true, |
| 924 | agentDeviceInstalled: true, |
| 925 | })), |
| 926 | ); |
| 927 | |
| 928 | return DeviceHost.DeviceHost.of({ |
| 929 | id: hostId, |
| 930 | label: config.label, |
| 931 | summary, |
| 932 | platformAvailability, |
| 933 | ensureReady, |
| 934 | ensureAgentReady: Effect.fn("DeviceHost.ensureAgentReady")(function (onPhase) { |
| 935 | return ensureReady(onPhase).pipe(Effect.map((value) => ({ ...value, agentDevice: null }))); |
| 936 | }), |
| 937 | current: Effect.sync(() => ready), |
| 938 | stopAgent: Effect.void, |
| 939 | // Unlike machines the user owns, every running session here is billed. |
| 940 | stop: stopAll, |
| 941 | }); |
| 942 | }); |