TypeScript
fix(device): recover cached agent daemons after exit (#23441)
Aditya Garudbef663a354cf
diff --git a/apps/server/src/device/LocalDeviceHost.test.ts b/apps/server/src/device/LocalDeviceHost.test.tsindex 57468c98878..6b86a4d0326 100644--- a/apps/server/src/device/LocalDeviceHost.test.ts+++ b/apps/server/src/device/LocalDeviceHost.test.ts@@ -6,6 +6,7 @@ import * as Layer from "effect/Layer";import * as Option from "effect/Option";import * as Path from "effect/Path";import * as PlatformError from "effect/PlatformError";+import * as Ref from "effect/Ref";import * as FileSystem from "effect/FileSystem";import * as Sink from "effect/Sink";import * as Stream from "effect/Stream";@@ -45,6 +46,7 @@ const agentFixture = Effect.fn("agentFixture")(function* (const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-device-agent-start-" });const hub = { running: true, spawnFails: false };const stateDir = agentDeviceStateDir(path, path.join(baseDir, "userdata"));+ const daemonHealthy = yield* Ref.make(true);for (const [name, version, entry] of [["expo-device-hub", DEVICE_HUB_VERSION, "dist/server/cli.mjs"],["agent-device", AGENT_DEVICE_VERSION, "bin/agent-device.mjs"],@@ -98,15 +100,56 @@ const agentFixture = Effect.fn("agentFixture")(function* (Effect.provideService(HttpClient.HttpClient,HttpClient.make((request) =>- Effect.succeed(HttpClientResponse.fromWeb(request, new Response("ok"))),+ Ref.get(daemonHealthy).pipe(+ Effect.map((healthy) =>+ HttpClientResponse.fromWeb(+ request,+ new Response("ok", {+ status: request.url.endsWith("/health") && !healthy ? 503 : 200,+ }),+ ),+ ),+ ),),),);yield* host.ensureReady(() => Effect.void);- return { host, fs, stateDir, hub };+ return { host, fs, stateDir, hub, daemonHealthy };});describe("AgentDevice startup", () => {+ it.effect("restarts a cached daemon that no longer answers its health check", () =>+ Effect.gen(function* () {+ const fs = yield* FileSystem.FileSystem;+ let attempts = 0;+ const { host, daemonHealthy } = yield* agentFixture((stateDir) =>+ Effect.gen(function* () {+ attempts++;+ yield* fs+ .writeFileString(+ `${stateDir}/daemon.json`,+ JSON.stringify({ httpPort: 12345 + attempts, token: `token-${attempts}` }),+ )+ .pipe(Effect.orDie);+ return processOutput(0);+ }),+ );+ const first = yield* host.ensureAgentReady(() => Effect.void);+ expect((yield* host.ensureAgentReady(() => Effect.void)).agentDevice).toEqual(+ first.agentDevice,+ );+ expect(attempts).toBe(1);++ yield* Ref.set(daemonHealthy, false);+ const restarted = yield* host.ensureAgentReady(() => Effect.void);+ expect(attempts).toBe(2);+ expect(restarted.agentDevice).toMatchObject({+ baseUrl: "http://127.0.0.1:12347",+ token: "token-2",+ });+ }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),+ );+it.effect("reports an incompatible lock immediately and retains it for manual repair", () =>Effect.gen(function* () {const result = processOutput(diff --git a/apps/server/src/device/LocalDeviceHost.ts b/apps/server/src/device/LocalDeviceHost.tsindex 34250bc199f..13ad97fcec6 100644--- a/apps/server/src/device/LocalDeviceHost.ts+++ b/apps/server/src/device/LocalDeviceHost.ts@@ -522,7 +522,7 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {});const startAgentDeviceDaemon = Effect.fn("LocalDeviceHost.startAgentDeviceDaemon")(function* (- agentTool: DeviceToolPaths,+ agentTool: Pick<DeviceToolPaths, "entryPath">,nodePath: string,): Effect.fn.Return<DeviceHost.AgentDeviceEndpoint,@@ -728,7 +728,14 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {DeviceHost.DeviceHostAgentReady> {const running = yield* ensureHubReady(onPhase);- if (running.agentDevice) return { ...toReady(running), agentDevice: running.agentDevice };+ if (running.agentDevice) {+ const agentDevice = yield* startAgentDeviceDaemon(+ running.agentDevice,+ running.hub.nodePath,+ );+ yield* Ref.set(runningRef, { ...running, agentDevice });+ return { ...toReady(running), agentDevice };+ }const installed = yield* isAgentDeviceInstalled(config.baseDir).pipe(Effect.provideService(FileSystem.FileSystem, fs),Effect.provideService(Path.Path, path),