diff --git a/apps/server/src/device/LocalDeviceHost.test.ts b/apps/server/src/device/LocalDeviceHost.test.ts index c1bbcbc347ea0..33dd8f6aa7ade 100644 --- a/apps/server/src/device/LocalDeviceHost.test.ts +++ b/apps/server/src/device/LocalDeviceHost.test.ts @@ -190,6 +190,82 @@ describe("AgentDevice startup", () => { }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), ); + // regression: #41190 a daemon left by the previous pin keeps answering /health + it.effect("hands a daemon recorded at another agent-device version to the pinned client", () => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const previous = { httpPort: 12345, token: "previous-token", version: "0.21.36" }; + const { host, runs } = yield* agentFixture( + (stateDir) => + fs + .writeFileString( + `${stateDir}/daemon.json`, + JSON.stringify({ + httpPort: 23456, + token: "next-token", + version: AGENT_DEVICE_VERSION, + }), + ) + .pipe(Effect.orDie, Effect.as(processOutput(0))), + 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:23456", + token: "next-token", + }); + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), + ); + + it.effect("keeps a busy daemon from another version that the pinned client leaves running", () => + Effect.gen(function* () { + const previous = { httpPort: 12345, token: "previous-token", 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:12345", + token: "previous-token", + }); + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), + ); + + 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("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..8c9cfd9deaa98 100644 --- a/apps/server/src/device/LocalDeviceHost.ts +++ b/apps/server/src/device/LocalDeviceHost.ts @@ -590,19 +590,25 @@ 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) => response.arrayBuffer.pipe(Effect.as(response.status === 200)), ), + Effect.timeout(Duration.seconds(2)), Effect.scoped, Effect.orElseSucceed(() => false), ); - if (alive) return toEndpoint(existing.value); - yield* fs.remove(daemonFilePath(), { force: true }).pipe(Effect.ignore); + if (existing._tag === "Some") { + const alive = yield* daemonAnswersHealth(existing.value.httpPort); + 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 @@ -614,9 +620,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,