From 1b37750b0a65d7da64e12a5331632a9e18ae6cf9 Mon Sep 17 00:00:00 2001 From: bessim boujebli Date: Wed, 18 Nov 2026 13:27:56 +0100 Subject: [PATCH 1/5] fix(server): retire inherited device daemons before reuse Fixes #38588. A new local host has no session records for a surviving daemon. Retire that daemon before exposing automation, preserve retained registrations, and keep healthy reuse within the current server. --- .../server/src/device/LocalDeviceHost.test.ts | 78 +++++++++++++++---- apps/server/src/device/LocalDeviceHost.ts | 26 ++++++- 2 files changed, 88 insertions(+), 16 deletions(-) diff --git a/apps/server/src/device/LocalDeviceHost.test.ts b/apps/server/src/device/LocalDeviceHost.test.ts index b29b80a647837..498de74f2f4a9 100644 --- a/apps/server/src/device/LocalDeviceHost.test.ts +++ b/apps/server/src/device/LocalDeviceHost.test.ts @@ -52,6 +52,7 @@ const agentFixture = Effect.fn("agentFixture")(function* ( const daemonStopRequested = yield* Deferred.make>(); const stateDir = agentDeviceStateDir(path, path.join(baseDir, "userdata")); const daemonHealthy = yield* Ref.make(true); + const daemonStop = { output: processOutput(0), removeRecord: true }; const runs: Array = []; for (const [name, version, entry] of [ ["expo-device-hub", DEVICE_HUB_VERSION, "dist/server/cli.mjs"], @@ -109,7 +110,18 @@ const agentFixture = Effect.fn("agentFixture")(function* ( return input.args.includes("devices") ? startup(stateDir) : input.args.includes("daemon") - ? Deferred.succeed(daemonStopRequested, input.args).pipe(Effect.as(processOutput(0))) + ? Deferred.succeed(daemonStopRequested, input.args).pipe( + Effect.andThen(() => + daemonStop.removeRecord && + daemonStop.output.code === 0 && + !daemonStop.output.timedOut + ? fs + .remove(path.join(stateDir, "daemon.json"), { force: true }) + .pipe(Effect.orDie) + : Effect.void, + ), + Effect.as(daemonStop.output), + ) : Effect.succeed(input.args.includes("-list-avds") ? avdOutput : processOutput(0)); }, }), @@ -130,27 +142,36 @@ const agentFixture = Effect.fn("agentFixture")(function* ( ), ); yield* host.ensureReady(() => Effect.void); - return { host, fs, baseDir, stateDir, hub, daemonHealthy, daemonStopRequested, runs }; + return { host, fs, baseDir, stateDir, hub, daemonHealthy, daemonStop, daemonStopRequested, runs }; }); describe("AgentDevice startup", () => { - it.effect("restarts a cached daemon that no longer answers its health check", () => + it.effect("replaces an inherited daemon and reuses only its own healthy daemon", () => 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 { host, daemonHealthy, runs } = 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); + }), + processOutput(0), + { + httpPort: 12345, + token: "previous-token", + pid: process.pid, + version: AGENT_DEVICE_VERSION, + }, ); const first = yield* host.ensureAgentReady(() => Effect.void); + expect(first.agentDevice?.token).toBe("token-1"); expect((yield* host.ensureAgentReady(() => Effect.void)).agentDevice).toEqual( first.agentDevice, ); @@ -159,6 +180,7 @@ describe("AgentDevice startup", () => { yield* Ref.set(daemonHealthy, false); const restarted = yield* host.ensureAgentReady(() => Effect.void); expect(attempts).toBe(2); + expect(runs.filter((run) => run.args.includes("daemon"))).toHaveLength(1); expect(restarted.agentDevice).toMatchObject({ baseUrl: "http://127.0.0.1:12347", token: "token-2", @@ -166,6 +188,34 @@ describe("AgentDevice startup", () => { }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), ); + it.effect.each([ + { scenario: "failed stop", output: processOutput(1) }, + { scenario: "timed-out stop", output: processOutput(0, "", "", true) }, + { scenario: "retained registration", output: processOutput(0) }, + ])("does not adopt the previous daemon after a $scenario", ({ output }) => + Effect.gen(function* () { + const previous = { + httpPort: 12345, + token: "previous-token", + pid: process.pid, + version: AGENT_DEVICE_VERSION, + }; + const { host, fs, stateDir, daemonStop } = yield* agentFixture( + () => Effect.die("Do not start a replacement until the old daemon has retired"), + processOutput(0), + previous, + ); + daemonStop.output = output; + daemonStop.removeRecord = false; + const error = yield* host.ensureAgentReady(() => Effect.void).pipe(Effect.flip); + expect(error).toMatchObject({ + _tag: "AgentDeviceStartError", + reason: "daemon_retirement_unconfirmed", + }); + expect(JSON.parse(yield* fs.readFileString(`${stateDir}/daemon.json`))).toEqual(previous); + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), + ); + it.effect("uses the SDK a later shell environment selects", () => Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; diff --git a/apps/server/src/device/LocalDeviceHost.ts b/apps/server/src/device/LocalDeviceHost.ts index 50089af431ff8..f968384a5935a 100644 --- a/apps/server/src/device/LocalDeviceHost.ts +++ b/apps/server/src/device/LocalDeviceHost.ts @@ -530,6 +530,7 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () { const startAgentDeviceDaemon = Effect.fn("LocalDeviceHost.startAgentDeviceDaemon")(function* ( agentTool: Pick, nodePath: string, + currentEndpoint?: DeviceHost.AgentDaemonEndpoint, ): Effect.fn.Return< DeviceHost.AgentDeviceEndpoint, | DeviceHost.DeviceHostError @@ -567,8 +568,28 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () { Effect.scoped, Effect.orElseSucceed(() => false), ); - if (alive) return toEndpoint(existing.value); - yield* fs.remove(daemonFilePath(), { force: true }).pipe(Effect.ignore); + if (alive) { + const endpoint = toEndpoint(existing.value); + if ( + currentEndpoint?.baseUrl === endpoint.baseUrl && + currentEndpoint.token === endpoint.token + ) { + return endpoint; + } + // A previous server's in-memory device sessions cannot be recovered by this host. + yield* stopAgentDeviceDaemon; + if (yield* fs.exists(daemonFilePath()).pipe(Effect.orElseSucceed(() => true))) { + return yield* new DeviceHost.AgentDeviceStartError({ + hostId, + stateDir, + exitCode: null, + reason: "daemon_retirement_unconfirmed", + cause: new Error("The previous agent-device daemon retained its registration."), + }); + } + } else { + yield* fs.remove(daemonFilePath(), { force: true }).pipe(Effect.ignore); + } } // No `daemon start` exists; `devices` is the cheapest command that starts and awaits it. const runResult = yield* runner @@ -758,6 +779,7 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () { const agentDevice = yield* startAgentDeviceDaemon( running.agentDevice, running.hub.nodePath, + running.agentDevice, ); yield* Ref.set(runningRef, { ...running, agentDevice }); return { ...toReady(running), agentDevice }; From 954bdf824de004cc472e24ed4abb8bd5f2c48e7a Mon Sep 17 00:00:00 2001 From: bessim boujebli Date: Wed, 18 Nov 2026 13:55:14 +0100 Subject: [PATCH 2/5] fix(server): harden device daemon recovery after review --- .../server/src/device/LocalDeviceHost.test.ts | 200 ++++++++++++++++-- apps/server/src/device/LocalDeviceHost.ts | 86 +++++--- 2 files changed, 244 insertions(+), 42 deletions(-) diff --git a/apps/server/src/device/LocalDeviceHost.test.ts b/apps/server/src/device/LocalDeviceHost.test.ts index 498de74f2f4a9..2affe444e54a6 100644 --- a/apps/server/src/device/LocalDeviceHost.test.ts +++ b/apps/server/src/device/LocalDeviceHost.test.ts @@ -52,7 +52,13 @@ const agentFixture = Effect.fn("agentFixture")(function* ( const daemonStopRequested = yield* Deferred.make>(); const stateDir = agentDeviceStateDir(path, path.join(baseDir, "userdata")); const daemonHealthy = yield* Ref.make(true); - const daemonStop = { output: processOutput(0), removeRecord: true }; + const daemonStop = { + output: processOutput(0), + removeRecord: true, + failure: Option.none(), + }; + const pruning = { effect: Effect.void }; + const requests: Array = []; const runs: Array = []; for (const [name, version, entry] of [ ["expo-device-hub", DEVICE_HUB_VERSION, "dist/server/cli.mjs"], @@ -107,6 +113,9 @@ const agentFixture = Effect.fn("agentFixture")(function* ( Effect.provideService(ProcessRunner.ProcessRunner, { run: (input) => { runs.push(input); + if (input.args.includes("daemon") && Option.isSome(daemonStop.failure)) { + return Effect.fail(daemonStop.failure.value); + } return input.args.includes("devices") ? startup(stateDir) : input.args.includes("daemon") @@ -122,27 +131,42 @@ const agentFixture = Effect.fn("agentFixture")(function* ( ), Effect.as(daemonStop.output), ) - : Effect.succeed(input.args.includes("-list-avds") ? avdOutput : processOutput(0)); + : input.args.some((arg) => arg.includes(`[["agent-device","${AGENT_DEVICE_VERSION}"]]`)) + ? pruning.effect.pipe(Effect.as(processOutput(0))) + : Effect.succeed(input.args.includes("-list-avds") ? avdOutput : processOutput(0)); }, }), Effect.provideService( HttpClient.HttpClient, HttpClient.make((request) => Ref.get(daemonHealthy).pipe( - Effect.map((healthy) => - HttpClientResponse.fromWeb( + Effect.map((healthy) => { + requests.push(request.url); + return HttpClientResponse.fromWeb( request, - new Response("ok", { + new Response(request.url.endsWith("/rpc") ? '{"result":{"ok":true}}' : "ok", { status: request.url.endsWith("/health") && !healthy ? 503 : 200, }), - ), - ), + ); + }), ), ), ), ); yield* host.ensureReady(() => Effect.void); - return { host, fs, baseDir, stateDir, hub, daemonHealthy, daemonStop, daemonStopRequested, runs }; + return { + host, + fs, + baseDir, + stateDir, + hub, + daemonHealthy, + daemonStop, + daemonStopRequested, + pruning, + requests, + runs, + }; }); describe("AgentDevice startup", () => { @@ -150,7 +174,7 @@ describe("AgentDevice startup", () => { Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; let attempts = 0; - const { host, daemonHealthy, runs } = yield* agentFixture( + const { host, daemonHealthy, runs, baseDir } = yield* agentFixture( (stateDir) => Effect.gen(function* () { attempts++; @@ -167,9 +191,14 @@ describe("AgentDevice startup", () => { httpPort: 12345, token: "previous-token", pid: process.pid, - version: AGENT_DEVICE_VERSION, + version: "0.0.1", }, ); + const olderInstall = `${baseDir}/tools/agent-device/0.0.1`; + const olderEntry = `${olderInstall}/node_modules/agent-device/bin/agent-device.mjs`; + yield* fs.makeDirectory(`${olderInstall}/node_modules/agent-device/bin`, { recursive: true }); + yield* fs.writeFileString(olderEntry, ""); + yield* fs.writeFileString(`${olderInstall}/.install-complete`, "0.0.1"); const first = yield* host.ensureAgentReady(() => Effect.void); expect(first.agentDevice?.token).toBe("token-1"); expect((yield* host.ensureAgentReady(() => Effect.void)).agentDevice).toEqual( @@ -180,7 +209,10 @@ describe("AgentDevice startup", () => { yield* Ref.set(daemonHealthy, false); const restarted = yield* host.ensureAgentReady(() => Effect.void); expect(attempts).toBe(2); - expect(runs.filter((run) => run.args.includes("daemon"))).toHaveLength(1); + expect(runs.filter((run) => run.args.includes("daemon")).map((run) => run.args[0])).toEqual([ + olderEntry, + `${baseDir}/tools/agent-device/${AGENT_DEVICE_VERSION}/node_modules/agent-device/bin/agent-device.mjs`, + ]); expect(restarted.agentDevice).toMatchObject({ baseUrl: "http://127.0.0.1:12347", token: "token-2", @@ -189,33 +221,167 @@ describe("AgentDevice startup", () => { ); it.effect.each([ - { scenario: "failed stop", output: processOutput(1) }, - { scenario: "timed-out stop", output: processOutput(0, "", "", true) }, - { scenario: "retained registration", output: processOutput(0) }, - ])("does not adopt the previous daemon after a $scenario", ({ output }) => + { scenario: "failed stop", output: processOutput(1), healthy: true, pid: process.pid }, + { + scenario: "timed-out stop", + output: processOutput(0, "", "", true), + healthy: false, + pid: process.pid, + }, + { + scenario: "retained registration", + output: processOutput(0), + healthy: true, + pid: process.pid, + }, + { scenario: "failed health probe", output: processOutput(0), healthy: false, pid: process.pid }, + { scenario: "missing owner PID", output: processOutput(0), healthy: false, pid: undefined }, + ])("does not adopt the previous daemon after a $scenario", ({ output, healthy, pid }) => Effect.gen(function* () { const previous = { httpPort: 12345, token: "previous-token", - pid: process.pid, + ...(pid === undefined ? {} : { pid }), version: AGENT_DEVICE_VERSION, }; - const { host, fs, stateDir, daemonStop } = yield* agentFixture( + const { host, fs, stateDir, daemonStop, daemonHealthy } = yield* agentFixture( () => Effect.die("Do not start a replacement until the old daemon has retired"), processOutput(0), previous, ); daemonStop.output = output; daemonStop.removeRecord = false; + yield* Ref.set(daemonHealthy, healthy); const error = yield* host.ensureAgentReady(() => Effect.void).pipe(Effect.flip); expect(error).toMatchObject({ _tag: "AgentDeviceStartError", reason: "daemon_retirement_unconfirmed", + exitCode: output.code, + cause: output, }); expect(JSON.parse(yield* fs.readFileString(`${stateDir}/daemon.json`))).toEqual(previous); }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), ); + it.effect("preserves an inherited daemon's stop execution failure", () => + Effect.gen(function* () { + const cause = new ProcessRunner.ProcessSpawnError({ + command: "/missing/node", + argumentCount: 5, + cause: new Error("missing executable"), + }); + const { host, daemonStop } = yield* agentFixture( + () => Effect.die("Do not replace a daemon whose stop could not run"), + processOutput(0), + { httpPort: 12345, token: "previous-token", pid: process.pid }, + ); + daemonStop.failure = Option.some(cause); + const error = yield* host.ensureAgentReady(() => Effect.void).pipe(Effect.flip); + expect(error).toMatchObject({ + _tag: "AgentDeviceStartError", + reason: "daemon_retirement_unconfirmed", + exitCode: null, + cause, + }); + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), + ); + + it.effect.each(["startup", "pruning"] as const)( + "retains its daemon ownership when interrupted during %s", + (boundary) => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const entered = yield* Deferred.make(); + const resume = yield* Deferred.make(); + let attempts = 0; + const { host, pruning, runs } = yield* agentFixture((stateDir) => + Effect.gen(function* () { + attempts++; + yield* fs + .writeFileString( + `${stateDir}/daemon.json`, + JSON.stringify({ + httpPort: 12345, + token: "owned-token", + pid: process.pid, + }), + ) + .pipe(Effect.orDie); + if (boundary === "startup") { + yield* Deferred.succeed(entered, undefined); + yield* Deferred.await(resume); + } + return processOutput(0); + }), + ); + if (boundary === "pruning") { + pruning.effect = Deferred.succeed(entered, undefined).pipe(Effect.andThen(Effect.never)); + } + const opening = yield* Effect.forkChild(host.ensureAgentReady(() => Effect.void)); + yield* Deferred.await(entered); + const interrupting = yield* Fiber.interrupt(opening).pipe( + Effect.forkChild({ startImmediately: true }), + ); + yield* Deferred.succeed(resume, undefined); + yield* Fiber.join(interrupting); + expect((yield* host.current)?.releaseAgentSession).toBeTypeOf("function"); + expect((yield* host.ensureAgentReady(() => Effect.void)).agentDevice?.token).toBe( + "owned-token", + ); + expect(attempts).toBe(1); + expect(runs.filter((run) => run.args.includes("daemon"))).toEqual([]); + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), + ); + + it.effect("invalidates obsolete cleanup endpoints before failed replacement", () => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + let attempts = 0; + let startupFails = false; + const { host, stateDir, daemonStop, requests } = yield* agentFixture((stateDir) => + Effect.gen(function* () { + attempts++; + if (startupFails) return processOutput(1); + yield* fs + .writeFileString( + `${stateDir}/daemon.json`, + JSON.stringify({ + httpPort: 12345 + attempts, + token: `token-${attempts}`, + pid: process.pid, + }), + ) + .pipe(Effect.orDie); + return processOutput(0); + }), + ); + yield* host.ensureAgentReady(() => Effect.void); + yield* fs.writeFileString( + `${stateDir}/daemon.json`, + JSON.stringify({ + httpPort: 12350, + token: "replacement-token", + pid: process.pid, + }), + ); + daemonStop.output = processOutput(1); + daemonStop.removeRecord = false; + yield* host.ensureAgentReady(() => Effect.void).pipe(Effect.flip); + expect((yield* host.current)?.releaseAgentSession).toBeUndefined(); + daemonStop.output = processOutput(0); + daemonStop.removeRecord = true; + startupFails = true; + yield* host.ensureAgentReady(() => Effect.void).pipe(Effect.flip); + expect((yield* host.current)?.releaseAgentSession).toBeUndefined(); + startupFails = false; + const ready = yield* host.ensureAgentReady(() => Effect.void); + expect(ready.agentDevice?.token).toBe("token-3"); + expect(ready.releaseAgentSession).toBeTypeOf("function"); + if (ready.releaseAgentSession) yield* ready.releaseAgentSession("test-session", "phone"); + expect(requests.at(-1)).toBe("http://127.0.0.1:12348/rpc"); + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), + ); + it.effect("uses the SDK a later shell environment selects", () => Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; diff --git a/apps/server/src/device/LocalDeviceHost.ts b/apps/server/src/device/LocalDeviceHost.ts index f968384a5935a..31b934aef52ba 100644 --- a/apps/server/src/device/LocalDeviceHost.ts +++ b/apps/server/src/device/LocalDeviceHost.ts @@ -576,19 +576,54 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () { ) { return endpoint; } - // A previous server's in-memory device sessions cannot be recovered by this host. - yield* stopAgentDeviceDaemon; - if (yield* fs.exists(daemonFilePath()).pipe(Effect.orElseSucceed(() => true))) { - return yield* new DeviceHost.AgentDeviceStartError({ - hostId, - stateDir, - exitCode: null, - reason: "daemon_retirement_unconfirmed", - cause: new Error("The previous agent-device daemon retained its registration."), - }); - } - } else { - yield* fs.remove(daemonFilePath(), { force: true }).pipe(Effect.ignore); + } + } + yield* Ref.update(runningRef, (running) => + running ? { ...running, agentDevice: null } : running, + ); + if (existing._tag === "Some") { + // A failed health probe does not prove that the recorded owner exited. + const stopped = yield* stopAgentDeviceDaemon.pipe(Effect.result); + if (stopped._tag === "Failure") { + return yield* new DeviceHost.AgentDeviceStartError({ + hostId, + stateDir, + exitCode: null, + reason: "daemon_retirement_unconfirmed", + cause: stopped.failure, + }); + } + const result = stopped.success; + if (result && (result.timedOut || result.code !== 0)) { + return yield* new DeviceHost.AgentDeviceStartError({ + hostId, + stateDir, + exitCode: result.code, + reason: "daemon_retirement_unconfirmed", + cause: result, + }); + } + const registered = yield* fs.exists(daemonFilePath()).pipe( + Effect.mapError( + (cause) => + new DeviceHost.AgentDeviceStartError({ + hostId, + stateDir, + exitCode: result?.code ?? null, + reason: "daemon_retirement_unconfirmed", + cause, + }), + ), + ); + if (registered) { + return yield* new DeviceHost.AgentDeviceStartError({ + hostId, + stateDir, + exitCode: result?.code ?? null, + reason: "daemon_retirement_unconfirmed", + cause: + result ?? new Error("No installed agent-device CLI could retire the registration."), + }); } } // No `daemon start` exists; `devices` is the cheapest command that starts and awaits it. @@ -672,7 +707,7 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () { const nodePath = yield* resolveNodeExecutable("Local device support", hostEnvironment).pipe( Effect.provideService(HostProcess.Platform, hostPlatform), ); - yield* runner.run({ + return yield* runner.run({ command: nodePath, args: [ entryPath, @@ -685,11 +720,7 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () { timeout: Duration.seconds(10), timeoutBehavior: "timedOutResult", }); - }).pipe( - Effect.provideService(FileSystem.FileSystem, fs), - Effect.provideService(Path.Path, path), - Effect.ignore, - ); + }).pipe(Effect.provideService(FileSystem.FileSystem, fs), Effect.provideService(Path.Path, path)); const ensureHubReady = Effect.fn("LocalDeviceHost.ensureHubReady")(function* ( onPhase: (phase: "installing" | "starting", detail?: string) => Effect.Effect, @@ -780,9 +811,11 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () { running.agentDevice, running.hub.nodePath, running.agentDevice, + ).pipe( + Effect.tap((agentDevice) => Ref.set(runningRef, { ...running, agentDevice })), + Effect.uninterruptible, ); - yield* Ref.set(runningRef, { ...running, agentDevice }); - return { ...toReady(running), agentDevice }; + return { ...toReady({ ...running, agentDevice }), agentDevice }; } const installed = yield* isAgentDeviceInstalled(config.baseDir).pipe( Effect.provideService(FileSystem.FileSystem, fs), @@ -809,14 +842,17 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () { ), ); yield* onPhase("starting"); - const agentDevice = yield* startAgentDeviceDaemon(agentTool, running.hub.nodePath); + // A daemon can publish its registration before its startup command returns. + const agentDevice = yield* startAgentDeviceDaemon(agentTool, running.hub.nodePath).pipe( + Effect.tap((agentDevice) => Ref.set(runningRef, { ...running, agentDevice })), + Effect.uninterruptible, + ); yield* pruneLocalDeviceTools(config.baseDir, running.hub.nodePath, "agent").pipe( Effect.provideService(Path.Path, path), Effect.provideService(ProcessRunner.ProcessRunner, runner), Effect.ignore, ); const next = { ...running, agentDevice }; - yield* Ref.set(runningRef, next); return { ...toReady(next), agentDevice }; }), ); @@ -891,7 +927,7 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () { const stopAgent: DeviceHost.DeviceHost["Service"]["stopAgent"] = startLock.withPermits(1)( Effect.gen(function* () { - yield* stopAgentDeviceDaemon; + yield* stopAgentDeviceDaemon.pipe(Effect.ignore); yield* Ref.update(runningRef, (running) => running ? { ...running, agentDevice: null } : running, ); @@ -907,7 +943,7 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () { stopHub(running?.hub).pipe( Effect.andThen(fs.remove(hubStatePath(), { force: true }).pipe(Effect.ignore)), ), - stopAgentDeviceDaemon, + stopAgentDeviceDaemon.pipe(Effect.ignore), ], { concurrency: "unbounded", discard: true }, ); From bc05e0e0234b946d586bbfe4408af9212c248fc0 Mon Sep 17 00:00:00 2001 From: bessim boujebli Date: Wed, 18 Nov 2026 14:17:10 +0100 Subject: [PATCH 3/5] fix(server): preserve cleanup during unresolved daemon recovery --- .../server/src/device/LocalDeviceHost.test.ts | 140 +++++++++++++++++- apps/server/src/device/LocalDeviceHost.ts | 57 ++++--- 2 files changed, 177 insertions(+), 20 deletions(-) diff --git a/apps/server/src/device/LocalDeviceHost.test.ts b/apps/server/src/device/LocalDeviceHost.test.ts index 2affe444e54a6..3b05c4b68193c 100644 --- a/apps/server/src/device/LocalDeviceHost.test.ts +++ b/apps/server/src/device/LocalDeviceHost.test.ts @@ -12,6 +12,7 @@ import * as Ref from "effect/Ref"; import * as FileSystem from "effect/FileSystem"; import * as Sink from "effect/Sink"; import * as Stream from "effect/Stream"; +import * as TestClock from "effect/testing/TestClock"; import * as LocalDeviceHost from "./LocalDeviceHost.ts"; import * as NodeServices from "@effect/platform-node/NodeServices"; @@ -58,6 +59,9 @@ const agentFixture = Effect.fn("agentFixture")(function* ( failure: Option.none(), }; const pruning = { effect: Effect.void }; + const daemonReads = { failure: Option.none() }; + const healthProbe = { body: Option.none>() }; + const healthHeaders = yield* Deferred.make(); const requests: Array = []; const runs: Array = []; for (const [name, version, entry] of [ @@ -79,6 +83,18 @@ const agentFixture = Effect.fn("agentFixture")(function* ( Effect.provideService(HostProcess.Environment, environment ?? { HOME: baseDir, PATH: "" }), Effect.provideService(HostProcess.IsExecutable, false), Effect.provideService(HostProcess.Platform, "darwin"), + Effect.provideService(FileSystem.FileSystem, { + ...fs, + readFileString: (file) => + Effect.suspend(() => { + if (file === path.join(stateDir, "daemon.json") && Option.isSome(daemonReads.failure)) { + const failure = daemonReads.failure.value; + daemonReads.failure = Option.none(); + return Effect.fail(failure); + } + return fs.readFileString(file); + }), + }), Effect.provideService( ChildProcessSpawner.ChildProcessSpawner, ChildProcessSpawner.make(() => @@ -140,14 +156,23 @@ const agentFixture = Effect.fn("agentFixture")(function* ( HttpClient.HttpClient, HttpClient.make((request) => Ref.get(daemonHealthy).pipe( + Effect.tap(() => + request.url.endsWith("/health") + ? Deferred.succeed(healthHeaders, undefined) + : Effect.void, + ), Effect.map((healthy) => { requests.push(request.url); - return HttpClientResponse.fromWeb( + const response = HttpClientResponse.fromWeb( request, new Response(request.url.endsWith("/rpc") ? '{"result":{"ok":true}}' : "ok", { status: request.url.endsWith("/health") && !healthy ? 503 : 200, }), ); + if (request.url.endsWith("/health") && Option.isSome(healthProbe.body)) { + Object.defineProperty(response, "arrayBuffer", { value: healthProbe.body.value }); + } + return response; }), ), ), @@ -164,12 +189,125 @@ const agentFixture = Effect.fn("agentFixture")(function* ( daemonStop, daemonStopRequested, pruning, + daemonReads, + healthProbe, + healthHeaders, requests, runs, }; }); describe("AgentDevice startup", () => { + it.effect("keeps matching cleanup available until retirement is confirmed", () => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + let startupFails = false; + const { host, daemonHealthy, daemonStop, requests } = yield* agentFixture((stateDir) => + startupFails + ? Effect.succeed(processOutput(1)) + : fs + .writeFileString( + `${stateDir}/daemon.json`, + JSON.stringify({ httpPort: 12345, token: "owned-token", pid: process.pid }), + ) + .pipe(Effect.orDie, Effect.as(processOutput(0))), + ); + yield* host.ensureAgentReady(() => Effect.void); + const cause = new ProcessRunner.ProcessSpawnError({ + command: "/missing/node", + argumentCount: 5, + cause: new Error("missing executable"), + }); + daemonStop.failure = Option.some(cause); + yield* Ref.set(daemonHealthy, false); + const error = yield* host.ensureAgentReady(() => Effect.void).pipe(Effect.flip); + expect(error).toMatchObject({ reason: "daemon_retirement_unconfirmed", cause }); + const current = yield* host.current; + expect(current?.releaseAgentSession).toBeTypeOf("function"); + if (current?.releaseAgentSession) + yield* current.releaseAgentSession("owned-session", "phone"); + expect(requests.at(-1)).toBe("http://127.0.0.1:12345/rpc"); + daemonStop.failure = Option.none(); + startupFails = true; + yield* host.ensureAgentReady(() => Effect.void).pipe(Effect.flip); + expect((yield* host.current)?.releaseAgentSession).toBeUndefined(); + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), + ); + + it.effect.each(["transient read failure", "invalid JSON"] as const)( + "preserves an unverified registration when %s prevents verification", + (scenario) => + Effect.gen(function* () { + const previous = { httpPort: 12345, token: "previous-token", pid: process.pid }; + const { host, fs, stateDir, daemonReads, runs } = yield* agentFixture( + () => Effect.die("Do not launch before establishing registration ownership"), + processOutput(0), + previous, + ); + const cause = PlatformError.systemError({ + _tag: "PermissionDenied", + module: "FileSystem", + method: "readFileString", + description: "read denied", + }); + if (scenario === "transient read failure") daemonReads.failure = Option.some(cause); + const registration = + scenario === "invalid JSON" ? "invalid-json" : JSON.stringify(previous); + yield* fs.writeFileString(`${stateDir}/daemon.json`, registration); + const error = yield* host.ensureAgentReady(() => Effect.void).pipe(Effect.flip); + expect(error).toMatchObject({ + _tag: "AgentDeviceStartError", + reason: "daemon_retirement_unconfirmed", + }); + if (scenario === "transient read failure") expect(error).toMatchObject({ cause }); + expect(yield* fs.readFileString(`${stateDir}/daemon.json`)).toBe(registration); + expect( + runs.filter((run) => run.args.includes("devices") || run.args.includes("daemon")), + ).toEqual([]); + expect((yield* host.current)?.releaseAgentSession).toBeUndefined(); + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), + ); + + it.effect.each(["timeout", "cancellation"] as const)( + "releases the startup lock after a stalled health body and %s", + (mode) => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const { host, healthProbe, healthHeaders, daemonStop } = yield* agentFixture((stateDir) => + fs + .writeFileString( + `${stateDir}/daemon.json`, + JSON.stringify({ httpPort: 12345, token: "owned-token", pid: process.pid }), + ) + .pipe(Effect.orDie, Effect.as(processOutput(0))), + ); + yield* host.ensureAgentReady(() => Effect.void); + const bodyReleased = yield* Deferred.make(); + const bodyInterrupted = yield* Deferred.make(); + healthProbe.body = Option.some( + Deferred.await(bodyReleased).pipe( + Effect.onInterrupt(() => Deferred.succeed(bodyInterrupted, undefined)), + ), + ); + daemonStop.output = processOutput(1); + yield* Effect.gen(function* () { + const opening = yield* Effect.forkChild( + host.ensureAgentReady(() => Effect.void).pipe(Effect.result), + ); + yield* Deferred.await(healthHeaders); + if (mode === "timeout") { + yield* TestClock.adjust("2 seconds"); + } else { + yield* Fiber.interrupt(opening).pipe(Effect.forkChild({ startImmediately: true })); + yield* TestClock.adjust("0 seconds"); + } + expect(yield* Deferred.isDone(bodyInterrupted)).toBe(true); + yield* Fiber.await(opening); + yield* host.ensureReady(() => Effect.void); + }).pipe(Effect.ensuring(Deferred.succeed(bodyReleased, new ArrayBuffer(0)))); + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), + ); + it.effect("replaces an inherited daemon and reuses only its own healthy daemon", () => Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; diff --git a/apps/server/src/device/LocalDeviceHost.ts b/apps/server/src/device/LocalDeviceHost.ts index 31b934aef52ba..78c2eab9362ad 100644 --- a/apps/server/src/device/LocalDeviceHost.ts +++ b/apps/server/src/device/LocalDeviceHost.ts @@ -24,6 +24,7 @@ import * as FileSystem from "effect/FileSystem"; 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 Schema from "effect/Schema"; import * as Scope from "effect/Scope"; @@ -526,6 +527,13 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () { const raw = yield* fs.readFileString(daemonFilePath()); return yield* decodeDaemonFile(raw); }); + const readOptionalDaemonFile = readDaemonFile().pipe( + Effect.asSome, + Effect.catchIf( + (cause) => PlatformError.isPlatformError(cause) && cause.reason._tag === "NotFound", + () => Effect.succeed(Option.none()), + ), + ); const startAgentDeviceDaemon = Effect.fn("LocalDeviceHost.startAgentDeviceDaemon")(function* ( agentTool: Pick, @@ -539,7 +547,19 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () { > { const stateDir = agentDeviceStateDir(path, config.stateDir); yield* fs.makeDirectory(stateDir, { recursive: true }).pipe(Effect.ignore); - const existing = yield* readDaemonFile().pipe(Effect.option); + const readRegistration = readOptionalDaemonFile.pipe( + Effect.mapError( + (cause) => + new DeviceHost.AgentDeviceStartError({ + hostId, + stateDir, + exitCode: null, + reason: "daemon_retirement_unconfirmed", + cause, + }), + ), + ); + const existing = yield* readRegistration; const daemonEnvironment: NodeJS.ProcessEnv = { ...(yield* currentHostEnvironment), AGENT_DEVICE_STATE_DIR: stateDir, @@ -558,30 +578,26 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () { entryPath: agentTool.entryPath, }); if (existing._tag === "Some") { + const endpoint = toEndpoint(existing.value); + const matchesCurrent = + currentEndpoint?.baseUrl === endpoint.baseUrl && currentEndpoint.token === endpoint.token; + if (!matchesCurrent) { + yield* Ref.update(runningRef, (running) => + running ? { ...running, agentDevice: null } : running, + ); + } const alive = yield* HttpClient.withScope(httpClient) .get(`http://127.0.0.1:${existing.value.httpPort}/health`) .pipe( - Effect.timeout(Duration.seconds(2)), Effect.flatMap((response) => response.arrayBuffer.pipe(Effect.as(response.status === 200)), ), Effect.scoped, + Effect.timeout(Duration.seconds(2)), + Effect.interruptible, Effect.orElseSucceed(() => false), ); - if (alive) { - const endpoint = toEndpoint(existing.value); - if ( - currentEndpoint?.baseUrl === endpoint.baseUrl && - currentEndpoint.token === endpoint.token - ) { - return endpoint; - } - } - } - yield* Ref.update(runningRef, (running) => - running ? { ...running, agentDevice: null } : running, - ); - if (existing._tag === "Some") { + if (alive && matchesCurrent) return endpoint; // A failed health probe does not prove that the recorded owner exited. const stopped = yield* stopAgentDeviceDaemon.pipe(Effect.result); if (stopped._tag === "Failure") { @@ -625,6 +641,9 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () { result ?? new Error("No installed agent-device CLI could retire the registration."), }); } + yield* Ref.update(runningRef, (running) => + running ? { ...running, agentDevice: null } : running, + ); } // No `daemon start` exists; `devices` is the cheapest command that starts and awaits it. const runResult = yield* runner @@ -637,7 +656,7 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () { }) .pipe(Effect.result); // Discovery may fail after startup; accept a published endpoint or preserve the CLI failure. - const started = yield* readDaemonFile().pipe(Effect.option); + const started = yield* readRegistration; if (started._tag === "Some") return toEndpoint(started.value); if (runResult._tag === "Failure") { return yield* new DeviceHost.DeviceHostError({ @@ -674,7 +693,7 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () { } const deadline = (yield* Clock.currentTimeMillis) + DAEMON_READY_TIMEOUT_MS; while (true) { - const file = yield* readDaemonFile().pipe(Effect.option); + const file = yield* readRegistration; if (file._tag === "Some") return toEndpoint(file.value); if ((yield* Clock.currentTimeMillis) > deadline) { return yield* new DeviceHost.DeviceHostTimeoutError({ @@ -690,7 +709,7 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () { // older pin), so stop it with the CLI of its recorded version when that is installed. const stopAgentDeviceDaemon = Effect.gen(function* () { if (!(yield* fs.exists(daemonFilePath()))) return; - const recorded = yield* readDaemonFile().pipe(Effect.option); + const recorded = yield* readOptionalDaemonFile; if ( Option.isSome(recorded) && recorded.value.pid && From 3c905e28195e60fa2db6a44ff26de6577cf27935 Mon Sep 17 00:00:00 2001 From: bessim boujebli Date: Wed, 18 Nov 2026 14:34:00 +0100 Subject: [PATCH 4/5] fix(server): recover daemon registration reads after launch --- .../server/src/device/LocalDeviceHost.test.ts | 197 +++++++++++++----- apps/server/src/device/LocalDeviceHost.ts | 28 ++- 2 files changed, 161 insertions(+), 64 deletions(-) diff --git a/apps/server/src/device/LocalDeviceHost.test.ts b/apps/server/src/device/LocalDeviceHost.test.ts index 3b05c4b68193c..4e67a9ecf1922 100644 --- a/apps/server/src/device/LocalDeviceHost.test.ts +++ b/apps/server/src/device/LocalDeviceHost.test.ts @@ -1,8 +1,10 @@ import { describe, expect, it } from "@effect/vitest"; import * as NodePath from "@effect/platform-node/NodePath"; import * as HostProcess from "@t3tools/shared/HostProcess"; +import * as Cause from "effect/Cause"; import * as Deferred from "effect/Deferred"; import * as Effect from "effect/Effect"; +import * as Exit from "effect/Exit"; import * as Fiber from "effect/Fiber"; import * as Layer from "effect/Layer"; import * as Option from "effect/Option"; @@ -59,7 +61,11 @@ const agentFixture = Effect.fn("agentFixture")(function* ( failure: Option.none(), }; const pruning = { effect: Effect.void }; - const daemonReads = { failure: Option.none() }; + const daemonReads = { + failure: Option.none(), + repeatFailure: false, + onFailure: Effect.void, + }; const healthProbe = { body: Option.none>() }; const healthHeaders = yield* Deferred.make(); const requests: Array = []; @@ -89,8 +95,8 @@ const agentFixture = Effect.fn("agentFixture")(function* ( Effect.suspend(() => { if (file === path.join(stateDir, "daemon.json") && Option.isSome(daemonReads.failure)) { const failure = daemonReads.failure.value; - daemonReads.failure = Option.none(); - return Effect.fail(failure); + if (!daemonReads.repeatFailure) daemonReads.failure = Option.none(); + return daemonReads.onFailure.pipe(Effect.andThen(Effect.fail(failure))); } return fs.readFileString(file); }), @@ -268,18 +274,85 @@ describe("AgentDevice startup", () => { }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), ); - it.effect.each(["timeout", "cancellation"] as const)( - "releases the startup lock after a stalled health body and %s", - (mode) => + it.effect.each(["transient", "persistent"] as const)( + "retries a %s post-launch registration read failure within the readiness deadline", + (scenario) => Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; - const { host, healthProbe, healthHeaders, daemonStop } = yield* agentFixture((stateDir) => + const readFailed = yield* Deferred.make(); + const completed = yield* Deferred.make(); + let failRegistrationRead = Effect.void; + const { host, daemonReads, runs } = yield* agentFixture((stateDir) => fs .writeFileString( `${stateDir}/daemon.json`, JSON.stringify({ httpPort: 12345, token: "owned-token", pid: process.pid }), ) - .pipe(Effect.orDie, Effect.as(processOutput(0))), + .pipe( + Effect.orDie, + Effect.andThen(() => failRegistrationRead), + Effect.as(processOutput(0)), + ), + ); + const cause = PlatformError.systemError({ + _tag: "PermissionDenied", + module: "FileSystem", + method: "readFileString", + description: "post-launch read denied", + }); + failRegistrationRead = Effect.sync(() => { + daemonReads.failure = Option.some(cause); + }); + daemonReads.repeatFailure = scenario === "persistent"; + daemonReads.onFailure = Deferred.succeed(readFailed, undefined).pipe(Effect.asVoid); + const opening = yield* Effect.forkChild( + host + .ensureAgentReady(() => Effect.void) + .pipe( + Effect.result, + Effect.tap(() => Deferred.succeed(completed, undefined)), + ), + ); + yield* Deferred.await(readFailed); + if (scenario === "persistent") { + yield* TestClock.adjust("29 seconds"); + expect(yield* Deferred.isDone(completed)).toBe(false); + yield* TestClock.adjust("1 second"); + } else { + yield* TestClock.adjust("100 millis"); + } + const result = yield* Fiber.join(opening); + if (scenario === "transient") { + expect(result).toMatchObject({ + _tag: "Success", + success: { agentDevice: { token: "owned-token" } }, + }); + expect((yield* host.current)?.releaseAgentSession).toBeTypeOf("function"); + yield* host.ensureAgentReady(() => Effect.void); + } else { + expect(result).toMatchObject({ + _tag: "Failure", + failure: { reason: "daemon_retirement_unconfirmed", cause }, + }); + } + expect(runs.filter((run) => run.args.includes("devices"))).toHaveLength(1); + expect(runs.filter((run) => run.args.includes("daemon"))).toEqual([]); + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), + ); + + it.effect.each(["timeout", "cancellation"] as const)( + "releases the startup lock after a stalled health body and %s", + (mode) => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const { host, healthProbe, healthHeaders, daemonStop, runs } = yield* agentFixture( + (stateDir) => + fs + .writeFileString( + `${stateDir}/daemon.json`, + JSON.stringify({ httpPort: 12345, token: "owned-token", pid: process.pid }), + ) + .pipe(Effect.orDie, Effect.as(processOutput(0))), ); yield* host.ensureAgentReady(() => Effect.void); const bodyReleased = yield* Deferred.make(); @@ -302,7 +375,13 @@ describe("AgentDevice startup", () => { yield* TestClock.adjust("0 seconds"); } expect(yield* Deferred.isDone(bodyInterrupted)).toBe(true); - yield* Fiber.await(opening); + const exit = yield* Fiber.await(opening); + if (mode === "cancellation") { + expect(Exit.isFailure(exit) && Cause.hasInterruptsOnly(exit.cause)).toBe(true); + } + expect(runs.filter((run) => run.args.includes("daemon"))).toHaveLength( + mode === "timeout" ? 1 : 0, + ); yield* host.ensureReady(() => Effect.void); }).pipe(Effect.ensuring(Deferred.succeed(bodyReleased, new ArrayBuffer(0)))); }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), @@ -471,53 +550,59 @@ describe("AgentDevice startup", () => { }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), ); - it.effect("invalidates obsolete cleanup endpoints before failed replacement", () => - Effect.gen(function* () { - const fs = yield* FileSystem.FileSystem; - let attempts = 0; - let startupFails = false; - const { host, stateDir, daemonStop, requests } = yield* agentFixture((stateDir) => - Effect.gen(function* () { - attempts++; - if (startupFails) return processOutput(1); - yield* fs - .writeFileString( - `${stateDir}/daemon.json`, - JSON.stringify({ - httpPort: 12345 + attempts, - token: `token-${attempts}`, - pid: process.pid, - }), - ) - .pipe(Effect.orDie); - return processOutput(0); - }), - ); - yield* host.ensureAgentReady(() => Effect.void); - yield* fs.writeFileString( - `${stateDir}/daemon.json`, - JSON.stringify({ - httpPort: 12350, - token: "replacement-token", - pid: process.pid, - }), - ); - daemonStop.output = processOutput(1); - daemonStop.removeRecord = false; - yield* host.ensureAgentReady(() => Effect.void).pipe(Effect.flip); - expect((yield* host.current)?.releaseAgentSession).toBeUndefined(); - daemonStop.output = processOutput(0); - daemonStop.removeRecord = true; - startupFails = true; - yield* host.ensureAgentReady(() => Effect.void).pipe(Effect.flip); - expect((yield* host.current)?.releaseAgentSession).toBeUndefined(); - startupFails = false; - const ready = yield* host.ensureAgentReady(() => Effect.void); - expect(ready.agentDevice?.token).toBe("token-3"); - expect(ready.releaseAgentSession).toBeTypeOf("function"); - if (ready.releaseAgentSession) yield* ready.releaseAgentSession("test-session", "phone"); - expect(requests.at(-1)).toBe("http://127.0.0.1:12348/rpc"); - }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), + it.effect.each(["superseded", "removed"] as const)( + "invalidates cleanup after registration is %s before failed replacement", + (registration) => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + let attempts = 0; + let startupFails = false; + const { host, stateDir, daemonStop, requests } = yield* agentFixture((stateDir) => + Effect.gen(function* () { + attempts++; + if (startupFails) return processOutput(1); + yield* fs + .writeFileString( + `${stateDir}/daemon.json`, + JSON.stringify({ + httpPort: 12345 + attempts, + token: `token-${attempts}`, + pid: process.pid, + }), + ) + .pipe(Effect.orDie); + return processOutput(0); + }), + ); + yield* host.ensureAgentReady(() => Effect.void); + if (registration === "superseded") { + yield* fs.writeFileString( + `${stateDir}/daemon.json`, + JSON.stringify({ + httpPort: 12350, + token: "replacement-token", + pid: process.pid, + }), + ); + daemonStop.output = processOutput(1); + daemonStop.removeRecord = false; + yield* host.ensureAgentReady(() => Effect.void).pipe(Effect.flip); + expect((yield* host.current)?.releaseAgentSession).toBeUndefined(); + daemonStop.output = processOutput(0); + daemonStop.removeRecord = true; + } else { + yield* fs.remove(`${stateDir}/daemon.json`); + } + startupFails = true; + yield* host.ensureAgentReady(() => Effect.void).pipe(Effect.flip); + expect((yield* host.current)?.releaseAgentSession).toBeUndefined(); + startupFails = false; + const ready = yield* host.ensureAgentReady(() => Effect.void); + expect(ready.agentDevice?.token).toBe("token-3"); + expect(ready.releaseAgentSession).toBeTypeOf("function"); + if (ready.releaseAgentSession) yield* ready.releaseAgentSession("test-session", "phone"); + expect(requests.at(-1)).toBe("http://127.0.0.1:12348/rpc"); + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), ); it.effect("uses the SDK a later shell environment selects", () => diff --git a/apps/server/src/device/LocalDeviceHost.ts b/apps/server/src/device/LocalDeviceHost.ts index 78c2eab9362ad..07fe0c4270f07 100644 --- a/apps/server/src/device/LocalDeviceHost.ts +++ b/apps/server/src/device/LocalDeviceHost.ts @@ -641,10 +641,10 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () { result ?? new Error("No installed agent-device CLI could retire the registration."), }); } - yield* Ref.update(runningRef, (running) => - running ? { ...running, agentDevice: null } : running, - ); } + yield* Ref.update(runningRef, (running) => + running ? { ...running, agentDevice: null } : running, + ); // No `daemon start` exists; `devices` is the cheapest command that starts and awaits it. const runResult = yield* runner .run({ @@ -656,8 +656,16 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () { }) .pipe(Effect.result); // Discovery may fail after startup; accept a published endpoint or preserve the CLI failure. - const started = yield* readRegistration; - if (started._tag === "Some") return toEndpoint(started.value); + const started = yield* readRegistration.pipe(Effect.result); + if (started._tag === "Success" && started.success._tag === "Some") { + return toEndpoint(started.success.value); + } + if ( + started._tag === "Failure" && + (runResult._tag === "Failure" || runResult.success.timedOut || runResult.success.code !== 0) + ) { + return yield* started.failure; + } if (runResult._tag === "Failure") { return yield* new DeviceHost.DeviceHostError({ hostId, @@ -692,16 +700,20 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () { }); } const deadline = (yield* Clock.currentTimeMillis) + DAEMON_READY_TIMEOUT_MS; + let registration = started; while (true) { - const file = yield* readRegistration; - if (file._tag === "Some") return toEndpoint(file.value); - if ((yield* Clock.currentTimeMillis) > deadline) { + if (registration._tag === "Success" && registration.success._tag === "Some") { + return toEndpoint(registration.success.value); + } + if ((yield* Clock.currentTimeMillis) >= deadline) { + if (registration._tag === "Failure") return yield* registration.failure; return yield* new DeviceHost.DeviceHostTimeoutError({ hostId, timeoutMs: DAEMON_READY_TIMEOUT_MS, }); } yield* Effect.sleep(Duration.millis(DAEMON_POLL_MS)); + registration = yield* readRegistration.pipe(Effect.result); } }); From df7da2687ac5d86f5e56bc5a66a1bbe2f9840239 Mon Sep 17 00:00:00 2001 From: bessim boujebli Date: Wed, 18 Nov 2026 14:41:21 +0100 Subject: [PATCH 5/5] fix(server): preserve daemon registrations on inconclusive probes --- .../server/src/device/LocalDeviceHost.test.ts | 43 +++++++++++++------ apps/server/src/device/LocalDeviceHost.ts | 9 +++- 2 files changed, 36 insertions(+), 16 deletions(-) diff --git a/apps/server/src/device/LocalDeviceHost.test.ts b/apps/server/src/device/LocalDeviceHost.test.ts index 4e67a9ecf1922..6958123f81144 100644 --- a/apps/server/src/device/LocalDeviceHost.test.ts +++ b/apps/server/src/device/LocalDeviceHost.test.ts @@ -1,4 +1,5 @@ import { describe, expect, it } from "@effect/vitest"; +import { vi } from "vite-plus/test"; import * as NodePath from "@effect/platform-node/NodePath"; import * as HostProcess from "@t3tools/shared/HostProcess"; import * as Cause from "effect/Cause"; @@ -835,20 +836,34 @@ describe("AgentDevice startup", () => { }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), ); - it.effect("drops a dead daemon's record without running agent-device when the host stops", () => - Effect.gen(function* () { - // Above the Linux and macOS PID limits, so never a live process. - const deadPid = 4_194_305; - const path = yield* Path.Path; - const { host, fs, stateDir, runs } = yield* agentFixture( - () => Effect.die("A session that never readies agent tools must not start the daemon"), - processOutput(0), - { httpPort: 12345, token: "previous-token", pid: deadPid, version: AGENT_DEVICE_VERSION }, - ); - yield* host.stop; - expect(runs.filter((run) => run.args.includes("daemon"))).toEqual([]); - expect(yield* fs.exists(path.join(stateDir, "daemon.json"))).toBe(false); - }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), + it.effect.each(["ESRCH", "EPERM", "unexpected"] as const)( + "confirms daemon death only for ESRCH when its process probe reports %s", + (probeError) => + Effect.gen(function* () { + // Above the Linux and macOS PID limits, so never a live process. + const deadPid = 4_194_305; + const kill = process.kill; + using _probe = vi.spyOn(process, "kill").mockImplementation((pid, signal) => { + if (pid === deadPid && signal === 0 && probeError !== "ESRCH") { + const cause = new Error("Process probe failed"); + throw probeError === "EPERM" ? Object.assign(cause, { code: "EPERM" }) : cause; + } + return kill(pid, signal); + }); + const path = yield* Path.Path; + const { host, fs, stateDir, runs, daemonStop } = yield* agentFixture( + () => Effect.die("A session that never readies agent tools must not start the daemon"), + processOutput(0), + { httpPort: 12345, token: "previous-token", pid: deadPid, version: AGENT_DEVICE_VERSION }, + ); + daemonStop.output = processOutput(1); + daemonStop.removeRecord = false; + yield* host.stop; + expect(runs.filter((run) => run.args.includes("daemon"))).toHaveLength( + probeError === "ESRCH" ? 0 : 1, + ); + expect(yield* fs.exists(path.join(stateDir, "daemon.json"))).toBe(probeError !== "ESRCH"); + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), ); it.effect("stops the agent daemon while the hub is still shutting down", () => diff --git a/apps/server/src/device/LocalDeviceHost.ts b/apps/server/src/device/LocalDeviceHost.ts index 07fe0c4270f07..1fb17d67445f2 100644 --- a/apps/server/src/device/LocalDeviceHost.ts +++ b/apps/server/src/device/LocalDeviceHost.ts @@ -343,8 +343,13 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () { try { process.kill(pid, 0); return true; - } catch { - return false; + } catch (cause) { + return !( + typeof cause === "object" && + cause !== null && + "code" in cause && + cause.code === "ESRCH" + ); } });