The best way to control Claude Code, Codex and more from any computer or phone
fix(server): quitting stops an agent-device daemon left by an earlier server (#33587)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Nov 10, 2026 at 14:09 UTC
1a2a4a5e9684585c31eb55c1974fb32f8b1db2b7
3 files changed
+136
-29
apps/server/src/device/DeviceToolchain.ts
+13
@@ -209,6 +209,19 @@ export const isDeviceHubInstalled = (baseDir: string) =>
209
export const isAgentDeviceInstalled = (baseDir: string) =>
210
isToolInstalled(baseDir, AGENT_DEVICE_SPEC, (paths) => paths.agentDevice);
211
212
+/** Entry of the first installed agent-device among `versions`; never installs. */
213
+export const installedAgentDeviceEntry = Effect.fn("DeviceToolchain.installedAgentDeviceEntry")(
214
+ function* (baseDir: string, versions: ReadonlyArray<string>) {
215
+ const fs = yield* FileSystem.FileSystem;
216
+ const path = yield* Path.Path;
217
+ for (const version of versions) {
218
+ const paths = toolPaths(path, baseDir, { ...AGENT_DEVICE_SPEC, version });
219
+ if (yield* isInstalled(fs, paths, version)) return paths.entryPath;
220
+ }
221
+ return null;
222
+ },
223
+);
224
+
225
export const deviceToolVersions = Effect.fn("DeviceToolchain.versions")(function* (
226
baseDir: string,
227
running: { hub?: string; agent?: string } = {},
apps/server/src/device/LocalDeviceHost.test.ts
+81
-4
@@ -40,6 +40,7 @@ const agentFixture = Effect.fn("agentFixture")(function* (
40
stateDir: string,
41
) => Effect.Effect<ProcessRunner.ProcessRunOutput, ProcessRunner.ProcessRunError>,
42
avdOutput: ProcessRunner.ProcessRunOutput = processOutput(0),
43
+ previousDaemon?: typeof LocalDeviceHost.__testing.AgentDeviceDaemonFile.Type,
44
) {
45
const fs = yield* FileSystem.FileSystem;
46
const path = yield* Path.Path;
@@ -47,6 +48,7 @@ const agentFixture = Effect.fn("agentFixture")(function* (
48
const hub = { running: true, spawnFails: false };
49
const stateDir = agentDeviceStateDir(path, path.join(baseDir, "userdata"));
50
const daemonHealthy = yield* Ref.make(true);
51
+ const runs: Array<ProcessRunner.ProcessRunInput> = [];
52
for (const [name, version, entry] of [
53
["expo-device-hub", DEVICE_HUB_VERSION, "dist/server/cli.mjs"],
54
["agent-device", AGENT_DEVICE_VERSION, "bin/agent-device.mjs"],
@@ -57,6 +59,10 @@ const agentFixture = Effect.fn("agentFixture")(function* (
59
yield* fs.writeFileString(entryPath, "");
60
yield* fs.writeFileString(path.join(installDir, ".install-complete"), version!);
61
}
62
+ if (previousDaemon) {
63
+ yield* fs.makeDirectory(stateDir, { recursive: true });
64
+ yield* fs.writeFileString(path.join(stateDir, "daemon.json"), JSON.stringify(previousDaemon));
65
+ }
66
const host = yield* LocalDeviceHost.make().pipe(
67
Effect.provide(Layer.mergeAll(ServerConfig.layerTest(baseDir, baseDir), NetService.layer)),
68
Effect.provideService(HostProcess.Environment, { HOME: baseDir, PATH: "" }),
@@ -92,10 +98,12 @@ const agentFixture = Effect.fn("agentFixture")(function* (
98
),
99
),
100
Effect.provideService(ProcessRunner.ProcessRunner, {
95
- run: (input) =>
96
- input.args.includes("devices")
101
+ run: (input) => {
102
+ runs.push(input);
103
+ return input.args.includes("devices")
104
? startup(stateDir)
98
- : Effect.succeed(input.args.includes("-list-avds") ? avdOutput : processOutput(0)),
105
+ : Effect.succeed(input.args.includes("-list-avds") ? avdOutput : processOutput(0));
106
+ },
107
}),
108
Effect.provideService(
109
HttpClient.HttpClient,
@@ -114,7 +122,7 @@ const agentFixture = Effect.fn("agentFixture")(function* (
122
),
123
);
124
yield* host.ensureReady(() => Effect.void);
117
- return { host, fs, stateDir, hub, daemonHealthy };
125
+ return { host, fs, baseDir, stateDir, hub, daemonHealthy, runs };
126
});
127
128
describe("AgentDevice startup", () => {
@@ -276,6 +284,75 @@ describe("AgentDevice startup", () => {
284
expect((yield* host.current)?.releaseAgentSession).toBeTypeOf("function");
285
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
286
);
287
+
288
+ it.effect("stops a previous server's daemon with the agent-device version it recorded", () =>
289
+ Effect.gen(function* () {
290
+ const path = yield* Path.Path;
291
+ const { host, fs, baseDir, runs } = yield* agentFixture(
292
+ () => Effect.die("A session that never readies agent tools must not start the daemon"),
293
+ processOutput(0),
294
+ { httpPort: 12345, token: "previous-token", pid: process.pid, version: "0.0.1" },
295
+ );
296
+ const olderInstall = path.join(baseDir, "tools", "agent-device", "0.0.1");
297
+ const olderEntry = path.join(
298
+ olderInstall,
299
+ "node_modules",
300
+ "agent-device",
301
+ "bin",
302
+ "agent-device.mjs",
303
+ );
304
+ yield* fs.makeDirectory(path.dirname(olderEntry), { recursive: true });
305
+ yield* fs.writeFileString(olderEntry, "");
306
+ yield* fs.writeFileString(path.join(olderInstall, ".install-complete"), "0.0.1");
307
+ yield* host.stop;
308
+ expect(runs.find((run) => run.args.includes("daemon"))?.args[0]).toBe(olderEntry);
309
+ }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
310
+ );
311
+
312
+ // regression: #33552 a daemon left by a previous server survived every later graceful quit
313
+ it.effect("stops a previous server's daemon with the current pin when its version is gone", () =>
314
+ Effect.gen(function* () {
315
+ const path = yield* Path.Path;
316
+ const { host, baseDir, stateDir, runs } = yield* agentFixture(
317
+ () => Effect.die("A session that never readies agent tools must not start the daemon"),
318
+ processOutput(0),
319
+ { httpPort: 12345, token: "previous-token", pid: process.pid, version: "0.0.1" },
320
+ );
321
+ yield* host.stop;
322
+ expect(runs.find((run) => run.args.includes("daemon"))?.args).toEqual([
323
+ path.join(
324
+ baseDir,
325
+ "tools",
326
+ "agent-device",
327
+ AGENT_DEVICE_VERSION,
328
+ "node_modules",
329
+ "agent-device",
330
+ "bin",
331
+ "agent-device.mjs",
332
+ ),
333
+ "daemon",
334
+ "stop",
335
+ "--state-dir",
336
+ stateDir,
337
+ ]);
338
+ }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
339
+ );
340
+
341
+ it.effect("drops a dead daemon's record without running agent-device when the host stops", () =>
342
+ Effect.gen(function* () {
343
+ // Above the Linux and macOS PID limits, so never a live process.
344
+ const deadPid = 4_194_305;
345
+ const path = yield* Path.Path;
346
+ const { host, fs, stateDir, runs } = yield* agentFixture(
347
+ () => Effect.die("A session that never readies agent tools must not start the daemon"),
348
+ processOutput(0),
349
+ { httpPort: 12345, token: "previous-token", pid: deadPid, version: AGENT_DEVICE_VERSION },
350
+ );
351
+ yield* host.stop;
352
+ expect(runs.filter((run) => run.args.includes("daemon"))).toEqual([]);
353
+ expect(yield* fs.exists(path.join(stateDir, "daemon.json"))).toBe(false);
354
+ }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
355
+ );
356
});
357
358
const diagnose = (
apps/server/src/device/LocalDeviceHost.ts
+42
-25
@@ -47,7 +47,9 @@ import {
47
isAgentDeviceInstalled,
48
isDeviceHubInstalled,
49
deviceToolVersions,
50
+ AGENT_DEVICE_VERSION,
51
DEVICE_HUB_VERSION,
52
+ installedAgentDeviceEntry,
53
} from "./DeviceToolchain.ts";
54
55
const HUB_READY_TIMEOUT_MS = 30_000;
@@ -624,28 +626,44 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
626
}
627
});
628
627
- const stopAgentDeviceDaemon = (
628
- agentTool: { readonly entryPath: string; readonly nodePath: string } | null,
629
- ) =>
630
- agentTool
631
- ? runner
632
- .run({
633
- command: agentTool.nodePath,
634
- args: [
635
- agentTool.entryPath,
636
- "daemon",
637
- "stop",
638
- "--state-dir",
639
- agentDeviceStateDir(path, config.stateDir),
640
- ],
641
- env: { ...hostEnvironment, AGENT_DEVICE_NO_UPDATE_NOTIFIER: "1" },
642
- timeout: Duration.seconds(10),
643
- timeoutBehavior: "timedOutResult",
644
- })
645
- .pipe(Effect.ignore)
646
- : Effect.void;
647
-
648
- let agentToolRef: { readonly entryPath: string; readonly nodePath: string } | null = null;
629
+ // The daemon may predate this process (an earlier server was killed, possibly on an
630
+ // older pin), so stop it with the CLI of its recorded version when that is installed.
631
+ const stopAgentDeviceDaemon = Effect.gen(function* () {
632
+ if (!(yield* fs.exists(daemonFilePath()))) return;
633
+ const recorded = yield* readDaemonFile().pipe(Effect.option);
634
+ if (
635
+ Option.isSome(recorded) &&
636
+ recorded.value.pid &&
637
+ !(yield* isProcessAlive(recorded.value.pid))
638
+ ) {
639
+ return yield* fs.remove(daemonFilePath(), { force: true });
640
+ }
641
+ const entryPath = yield* installedAgentDeviceEntry(config.baseDir, [
642
+ ...(Option.isSome(recorded) && recorded.value.version ? [recorded.value.version] : []),
643
+ AGENT_DEVICE_VERSION,
644
+ ]);
645
+ if (!entryPath) return;
646
+ const nodePath = yield* resolveNodeExecutable("Local device support", hostEnvironment).pipe(
647
+ Effect.provideService(HostProcess.Platform, hostPlatform),
648
+ );
649
+ yield* runner.run({
650
+ command: nodePath,
651
+ args: [
652
+ entryPath,
653
+ "daemon",
654
+ "stop",
655
+ "--state-dir",
656
+ agentDeviceStateDir(path, config.stateDir),
657
+ ],
658
+ env: { ...hostEnvironment, AGENT_DEVICE_NO_UPDATE_NOTIFIER: "1" },
659
+ timeout: Duration.seconds(10),
660
+ timeoutBehavior: "timedOutResult",
661
+ });
662
+ }).pipe(
663
+ Effect.provideService(FileSystem.FileSystem, fs),
664
+ Effect.provideService(Path.Path, path),
665
+ Effect.ignore,
666
+ );
667
668
const ensureHubReady = Effect.fn("LocalDeviceHost.ensureHubReady")(function* (
669
onPhase: (phase: "installing" | "starting", detail?: string) => Effect.Effect<void>,
@@ -760,7 +778,6 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
778
}),
779
),
780
);
763
- agentToolRef = { entryPath: agentTool.entryPath, nodePath: running.hub.nodePath };
781
yield* onPhase("starting");
782
const agentDevice = yield* startAgentDeviceDaemon(agentTool, running.hub.nodePath);
783
yield* pruneLocalDeviceTools(config.baseDir, running.hub.nodePath, "agent").pipe(
@@ -843,7 +860,7 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
860
861
const stopAgent: DeviceHost.DeviceHost["Service"]["stopAgent"] = startLock.withPermits(1)(
862
Effect.gen(function* () {
846
- yield* stopAgentDeviceDaemon(agentToolRef);
863
+ yield* stopAgentDeviceDaemon;
864
yield* Ref.update(runningRef, (running) =>
865
running ? { ...running, agentDevice: null } : running,
866
);
@@ -855,7 +872,7 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
872
const running = yield* Ref.getAndSet(runningRef, null);
873
yield* stopHub(running?.hub);
874
yield* fs.remove(hubStatePath(), { force: true }).pipe(Effect.ignore);
858
- yield* stopAgentDeviceDaemon(agentToolRef);
875
+ yield* stopAgentDeviceDaemon;
876
}),
877
);
878