From 90eef6d78699f0945d0f8338e9ba6ce743f8052e Mon Sep 17 00:00:00 2001 From: Will Craig <24501963+WillCraig@users.noreply.github.com> Date: Mon, 23 Nov 2026 19:51:23 +0000 Subject: [PATCH 1/4] fix(server): an upgraded agent-device no longer reuses an idle daemon from the previous pin LocalDeviceHost adopted any daemon whose /health returned 200 and paired it with the newly installed client. After a pin change the old daemon stays up, because its idle timeout is disabled, and the new client fails with an RPC protocol mismatch. A registration whose version is not the pinned client is left in place. That client replaces the daemon only when it holds no work. A registration it leaves behind is still adopted. Co-authored-by: William Craig --- .../server/src/device/LocalDeviceHost.test.ts | 95 +++++++++++++++++++ apps/server/src/device/LocalDeviceHost.ts | 10 +- 2 files changed, 103 insertions(+), 2 deletions(-) diff --git a/apps/server/src/device/LocalDeviceHost.test.ts b/apps/server/src/device/LocalDeviceHost.test.ts index c1bbcbc347ea0..bcd6fa7531deb 100644 --- a/apps/server/src/device/LocalDeviceHost.test.ts +++ b/apps/server/src/device/LocalDeviceHost.test.ts @@ -190,6 +190,101 @@ describe("AgentDevice startup", () => { }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), ); + // regression: #41190 a daemon left by the previous pin keeps answering /health + it.effect("replaces a healthy daemon recorded at a different agent-device version", () => + Effect.gen(function* () { + const path = yield* Path.Path; + const fs = yield* FileSystem.FileSystem; + const previous = { + httpPort: 12345, + token: "previous-token", + pid: process.pid, + version: "0.21.36", + }; + expect(previous.version).not.toBe(AGENT_DEVICE_VERSION); + const { host, baseDir, stateDir, runs } = yield* agentFixture( + (dir) => + fs + .writeFileString( + `${dir}/daemon.json`, + JSON.stringify({ + httpPort: 23456, + token: "next-token", + version: AGENT_DEVICE_VERSION, + }), + ) + .pipe(Effect.orDie, Effect.as(processOutput(0))), + processOutput(0), + previous, + ); + const entry = path.join( + baseDir, + "tools", + "agent-device", + AGENT_DEVICE_VERSION, + "node_modules", + "agent-device", + "bin", + "agent-device.mjs", + ); + const ready = yield* host.ensureAgentReady(() => Effect.void); + expect(runs.filter((run) => run.args.includes("devices"))).toEqual([ + expect.objectContaining({ + args: [entry, "devices", "--json"], + env: expect.objectContaining({ AGENT_DEVICE_STATE_DIR: stateDir }), + }), + ]); + expect(ready.agentDevice).toMatchObject({ + baseUrl: "http://127.0.0.1:23456", + token: "next-token", + entryPath: entry, + }); + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), + ); + + it.effect("reuses a healthy daemon recorded at the pinned agent-device version", () => + Effect.gen(function* () { + const { host, runs } = yield* agentFixture( + () => Effect.die("A daemon at the pinned version must be reused"), + processOutput(0), + { + httpPort: 12345, + token: "current-token", + pid: process.pid, + version: AGENT_DEVICE_VERSION, + }, + ); + const ready = yield* host.ensureAgentReady(() => Effect.void); + expect(ready.agentDevice).toMatchObject({ + baseUrl: "http://127.0.0.1:12345", + token: "current-token", + }); + expect(runs.filter((run) => run.args.includes("devices"))).toEqual([]); + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), + ); + + it.effect("keeps a mismatched daemon the pinned client leaves registered", () => + Effect.gen(function* () { + const previous = { + httpPort: 12345, + token: "previous-token", + pid: process.pid, + version: "0.21.36", + }; + const { host, runs } = yield* agentFixture( + () => Effect.succeed(processOutput(1)), + processOutput(0), + previous, + ); + const ready = yield* host.ensureAgentReady(() => Effect.void); + expect(runs.filter((run) => run.args.includes("devices"))).toHaveLength(1); + expect(ready.agentDevice).toMatchObject({ + baseUrl: `http://127.0.0.1:${previous.httpPort}`, + token: previous.token, + }); + }).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 d7443d1a4b88b..9be46c7619374 100644 --- a/apps/server/src/device/LocalDeviceHost.ts +++ b/apps/server/src/device/LocalDeviceHost.ts @@ -601,8 +601,14 @@ 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); + // A healthy daemon from another pin is not this client. Leave its registration so the + // pinned CLI replaces it only when the daemon holds no work. + const differentPin = + existing.value.version !== undefined && existing.value.version !== AGENT_DEVICE_VERSION; + if (alive && !differentPin) return toEndpoint(existing.value); + if (!alive) { + 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 From 608155560349ff620a62706e3a3a8a3e86f9174d Mon Sep 17 00:00:00 2001 From: Will Craig <24501963+WillCraig@users.noreply.github.com> Date: Mon, 23 Nov 2026 20:14:16 +0000 Subject: [PATCH 2/4] fix(server): a failed agent-device replacement no longer adopts a stopped daemon A non-zero or timed-out devices run can retire the previous daemon and leave its registration behind. Startup kept that file and reported the host ready. The registration is kept only when /health still returns 200. Otherwise the original failure is reported. A successful run still accepts the endpoint it published, including a busy daemon the client leaves in place. Co-authored-by: William Craig --- .../server/src/device/LocalDeviceHost.test.ts | 62 +++++++++++++++++++ apps/server/src/device/LocalDeviceHost.ts | 19 ++++-- 2 files changed, 76 insertions(+), 5 deletions(-) diff --git a/apps/server/src/device/LocalDeviceHost.test.ts b/apps/server/src/device/LocalDeviceHost.test.ts index bcd6fa7531deb..7108261a51383 100644 --- a/apps/server/src/device/LocalDeviceHost.test.ts +++ b/apps/server/src/device/LocalDeviceHost.test.ts @@ -285,6 +285,68 @@ describe("AgentDevice startup", () => { }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), ); + // regression: #41190 a failed replacement can stop the old daemon and leave its file + it.effect("reports a stopped daemon a failed replacement left registered", () => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + let health: Ref.Ref | undefined; + const previous = { + httpPort: 12345, + token: "previous-token", + pid: process.pid, + version: "0.21.36", + }; + const { host, stateDir, runs, daemonHealthy } = yield* agentFixture( + () => (health ? Ref.set(health, false) : Effect.void).pipe(Effect.as(processOutput(1))), + processOutput(0), + previous, + ); + health = daemonHealthy; + const error = yield* host.ensureAgentReady(() => Effect.void).pipe(Effect.flip); + expect(runs.filter((run) => run.args.includes("devices"))).toHaveLength(1); + expect(error).toMatchObject({ + _tag: "AgentDeviceStartError", + reason: "command_failed", + exitCode: 1, + }); + expect(yield* fs.readFileString(path.join(stateDir, "daemon.json"))).toContain( + previous.token, + ); + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), + ); + + it.effect("reports a timeout when a timed-out replacement leaves a stopped daemon", () => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + let health: Ref.Ref | undefined; + const previous = { + httpPort: 12345, + token: "previous-token", + pid: process.pid, + version: "0.21.36", + }; + const { host, stateDir, daemonHealthy } = yield* agentFixture( + () => + (health ? Ref.set(health, false) : Effect.void).pipe( + Effect.as(processOutput(0, "", "", true)), + ), + processOutput(0), + previous, + ); + health = daemonHealthy; + const error = yield* host.ensureAgentReady(() => Effect.void).pipe(Effect.flip); + expect(error).toMatchObject({ + _tag: "DeviceHostTimeoutError", + timeoutMs: 30_000, + }); + expect(yield* fs.readFileString(path.join(stateDir, "daemon.json"))).toContain( + previous.token, + ); + }).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 9be46c7619374..6ace18384d707 100644 --- a/apps/server/src/device/LocalDeviceHost.ts +++ b/apps/server/src/device/LocalDeviceHost.ts @@ -590,9 +590,9 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () { token: file.token, entryPath: agentTool.entryPath, }); - if (existing._tag === "Some") { - const alive = yield* HttpClient.withScope(httpClient) - .get(`http://127.0.0.1:${existing.value.httpPort}/health`) + const daemonAnswersHealth = (httpPort: number) => + HttpClient.withScope(httpClient) + .get(`http://127.0.0.1:${httpPort}/health`) .pipe( Effect.timeout(Duration.seconds(2)), Effect.flatMap((response) => @@ -601,6 +601,8 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () { Effect.scoped, Effect.orElseSucceed(() => false), ); + if (existing._tag === "Some") { + const alive = yield* daemonAnswersHealth(existing.value.httpPort); // A healthy daemon from another pin is not this client. Leave its registration so the // pinned CLI replaces it only when the daemon holds no work. const differentPin = @@ -620,9 +622,16 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () { timeoutBehavior: "timedOutResult", }) .pipe(Effect.result); - // Discovery may fail after startup; accept a published endpoint or preserve the CLI failure. + // A failed run can stop the previous daemon and leave its file. Keep that registration + // only while it still answers; a successful run has published the endpoint it reached. const started = yield* readDaemonFile().pipe(Effect.option); - if (started._tag === "Some") return toEndpoint(started.value); + if (started._tag === "Some") { + const runSucceeded = + runResult._tag === "Success" && !runResult.success.timedOut && runResult.success.code === 0; + if (runSucceeded || (yield* daemonAnswersHealth(started.value.httpPort))) { + return toEndpoint(started.value); + } + } if (runResult._tag === "Failure") { return yield* new DeviceHost.DeviceHostError({ hostId, From 82ae5708d24340f23f4abebae7d2d87b8245bc54 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 23 Nov 2026 20:20:44 +0000 Subject: [PATCH 3/4] test(server): trim the agent-device version handover tests to distinct cases Drop the pinned-version reuse case, which already passes on main, and the timeout variant of the failed-replacement case, which exercises the same branch. Tighten the startup comment. Co-Authored-By: Claude Opus 5.5 --- .../server/src/device/LocalDeviceHost.test.ts | 101 ++---------------- apps/server/src/device/LocalDeviceHost.ts | 14 ++- 2 files changed, 16 insertions(+), 99 deletions(-) diff --git a/apps/server/src/device/LocalDeviceHost.test.ts b/apps/server/src/device/LocalDeviceHost.test.ts index 7108261a51383..33dd8f6aa7ade 100644 --- a/apps/server/src/device/LocalDeviceHost.test.ts +++ b/apps/server/src/device/LocalDeviceHost.test.ts @@ -191,22 +191,15 @@ describe("AgentDevice startup", () => { ); // regression: #41190 a daemon left by the previous pin keeps answering /health - it.effect("replaces a healthy daemon recorded at a different agent-device version", () => + it.effect("hands a daemon recorded at another agent-device version to the pinned client", () => Effect.gen(function* () { - const path = yield* Path.Path; const fs = yield* FileSystem.FileSystem; - const previous = { - httpPort: 12345, - token: "previous-token", - pid: process.pid, - version: "0.21.36", - }; - expect(previous.version).not.toBe(AGENT_DEVICE_VERSION); - const { host, baseDir, stateDir, runs } = yield* agentFixture( - (dir) => + const previous = { httpPort: 12345, token: "previous-token", version: "0.21.36" }; + const { host, runs } = yield* agentFixture( + (stateDir) => fs .writeFileString( - `${dir}/daemon.json`, + `${stateDir}/daemon.json`, JSON.stringify({ httpPort: 23456, token: "next-token", @@ -217,60 +210,18 @@ describe("AgentDevice startup", () => { processOutput(0), previous, ); - const entry = path.join( - baseDir, - "tools", - "agent-device", - AGENT_DEVICE_VERSION, - "node_modules", - "agent-device", - "bin", - "agent-device.mjs", - ); const ready = yield* host.ensureAgentReady(() => Effect.void); - expect(runs.filter((run) => run.args.includes("devices"))).toEqual([ - expect.objectContaining({ - args: [entry, "devices", "--json"], - env: expect.objectContaining({ AGENT_DEVICE_STATE_DIR: stateDir }), - }), - ]); + expect(runs.filter((run) => run.args.includes("devices"))).toHaveLength(1); expect(ready.agentDevice).toMatchObject({ baseUrl: "http://127.0.0.1:23456", token: "next-token", - entryPath: entry, }); }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), ); - it.effect("reuses a healthy daemon recorded at the pinned agent-device version", () => + it.effect("keeps a busy daemon from another version that the pinned client leaves running", () => Effect.gen(function* () { - const { host, runs } = yield* agentFixture( - () => Effect.die("A daemon at the pinned version must be reused"), - processOutput(0), - { - httpPort: 12345, - token: "current-token", - pid: process.pid, - version: AGENT_DEVICE_VERSION, - }, - ); - const ready = yield* host.ensureAgentReady(() => Effect.void); - expect(ready.agentDevice).toMatchObject({ - baseUrl: "http://127.0.0.1:12345", - token: "current-token", - }); - expect(runs.filter((run) => run.args.includes("devices"))).toEqual([]); - }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), - ); - - it.effect("keeps a mismatched daemon the pinned client leaves registered", () => - Effect.gen(function* () { - const previous = { - httpPort: 12345, - token: "previous-token", - pid: process.pid, - version: "0.21.36", - }; + const previous = { httpPort: 12345, token: "previous-token", version: "0.21.36" }; const { host, runs } = yield* agentFixture( () => Effect.succeed(processOutput(1)), processOutput(0), @@ -279,13 +230,12 @@ describe("AgentDevice startup", () => { const ready = yield* host.ensureAgentReady(() => Effect.void); expect(runs.filter((run) => run.args.includes("devices"))).toHaveLength(1); expect(ready.agentDevice).toMatchObject({ - baseUrl: `http://127.0.0.1:${previous.httpPort}`, - token: previous.token, + baseUrl: "http://127.0.0.1:12345", + token: "previous-token", }); }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), ); - // regression: #41190 a failed replacement can stop the old daemon and leave its file it.effect("reports a stopped daemon a failed replacement left registered", () => Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; @@ -316,37 +266,6 @@ describe("AgentDevice startup", () => { }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), ); - it.effect("reports a timeout when a timed-out replacement leaves a stopped daemon", () => - Effect.gen(function* () { - const fs = yield* FileSystem.FileSystem; - const path = yield* Path.Path; - let health: Ref.Ref | undefined; - const previous = { - httpPort: 12345, - token: "previous-token", - pid: process.pid, - version: "0.21.36", - }; - const { host, stateDir, daemonHealthy } = yield* agentFixture( - () => - (health ? Ref.set(health, false) : Effect.void).pipe( - Effect.as(processOutput(0, "", "", true)), - ), - processOutput(0), - previous, - ); - health = daemonHealthy; - const error = yield* host.ensureAgentReady(() => Effect.void).pipe(Effect.flip); - expect(error).toMatchObject({ - _tag: "DeviceHostTimeoutError", - timeoutMs: 30_000, - }); - expect(yield* fs.readFileString(path.join(stateDir, "daemon.json"))).toContain( - previous.token, - ); - }).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 6ace18384d707..9b2ac46a60a04 100644 --- a/apps/server/src/device/LocalDeviceHost.ts +++ b/apps/server/src/device/LocalDeviceHost.ts @@ -603,14 +603,12 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () { ); if (existing._tag === "Some") { const alive = yield* daemonAnswersHealth(existing.value.httpPort); - // A healthy daemon from another pin is not this client. Leave its registration so the - // pinned CLI replaces it only when the daemon holds no work. - const differentPin = - existing.value.version !== undefined && existing.value.version !== AGENT_DEVICE_VERSION; - if (alive && !differentPin) return toEndpoint(existing.value); - if (!alive) { - yield* fs.remove(daemonFilePath(), { force: true }).pipe(Effect.ignore); - } + const samePin = + existing.value.version === undefined || existing.value.version === AGENT_DEVICE_VERSION; + if (alive && samePin) return toEndpoint(existing.value); + // A live daemon from another pin may speak another RPC protocol. Keep its registration so + // the pinned CLI retires it, which that CLI does only while the daemon holds no work. + if (!alive) 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 From 5cf6d9c99499d28cb238e1e8deb7e8adc752e8a6 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 23 Nov 2026 20:31:58 +0000 Subject: [PATCH 4/4] fix(server): bound the agent-device health probe's body read too Co-Authored-By: Claude Opus 5.5 --- apps/server/src/device/LocalDeviceHost.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/server/src/device/LocalDeviceHost.ts b/apps/server/src/device/LocalDeviceHost.ts index 9b2ac46a60a04..8c9cfd9deaa98 100644 --- a/apps/server/src/device/LocalDeviceHost.ts +++ b/apps/server/src/device/LocalDeviceHost.ts @@ -594,10 +594,10 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () { HttpClient.withScope(httpClient) .get(`http://127.0.0.1:${httpPort}/health`) .pipe( - Effect.timeout(Duration.seconds(2)), Effect.flatMap((response) => response.arrayBuffer.pipe(Effect.as(response.status === 200)), ), + Effect.timeout(Duration.seconds(2)), Effect.scoped, Effect.orElseSucceed(() => false), );