The best way to control Claude Code, Codex and more from any computer or phone
fix(device): recover cached agent daemons after exit (#23441)
Co-authored-by: maria-rcks <254055478+maria-rcks@users.noreply.github.com>
Aditya Garud committed
Oct 31, 2026 at 03:14 UTC
bef663a354cf01844d33a0a238108768e5c58d58
2 files changed
+54
-4
apps/server/src/device/LocalDeviceHost.test.ts
+45
-2
@@ -6,6 +6,7 @@ import * as Layer from "effect/Layer";
6
import * as Option from "effect/Option";
7
import * as Path from "effect/Path";
8
import * as PlatformError from "effect/PlatformError";
9
+import * as Ref from "effect/Ref";
10
import * as FileSystem from "effect/FileSystem";
11
import * as Sink from "effect/Sink";
12
import * as Stream from "effect/Stream";
@@ -45,6 +46,7 @@ const agentFixture = Effect.fn("agentFixture")(function* (
46
const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-device-agent-start-" });
47
const hub = { running: true, spawnFails: false };
48
const stateDir = agentDeviceStateDir(path, path.join(baseDir, "userdata"));
49
+ const daemonHealthy = yield* Ref.make(true);
50
for (const [name, version, entry] of [
51
["expo-device-hub", DEVICE_HUB_VERSION, "dist/server/cli.mjs"],
52
["agent-device", AGENT_DEVICE_VERSION, "bin/agent-device.mjs"],
@@ -98,15 +100,56 @@ const agentFixture = Effect.fn("agentFixture")(function* (
100
Effect.provideService(
101
HttpClient.HttpClient,
102
HttpClient.make((request) =>
101
- Effect.succeed(HttpClientResponse.fromWeb(request, new Response("ok"))),
103
+ Ref.get(daemonHealthy).pipe(
104
+ Effect.map((healthy) =>
105
+ HttpClientResponse.fromWeb(
106
+ request,
107
+ new Response("ok", {
108
+ status: request.url.endsWith("/health") && !healthy ? 503 : 200,
109
+ }),
110
+ ),
111
+ ),
112
+ ),
113
),
114
),
115
);
116
yield* host.ensureReady(() => Effect.void);
106
- return { host, fs, stateDir, hub };
117
+ return { host, fs, stateDir, hub, daemonHealthy };
118
});
119
120
describe("AgentDevice startup", () => {
121
+ it.effect("restarts a cached daemon that no longer answers its health check", () =>
122
+ Effect.gen(function* () {
123
+ const fs = yield* FileSystem.FileSystem;
124
+ let attempts = 0;
125
+ const { host, daemonHealthy } = yield* agentFixture((stateDir) =>
126
+ Effect.gen(function* () {
127
+ attempts++;
128
+ yield* fs
129
+ .writeFileString(
130
+ `${stateDir}/daemon.json`,
131
+ JSON.stringify({ httpPort: 12345 + attempts, token: `token-${attempts}` }),
132
+ )
133
+ .pipe(Effect.orDie);
134
+ return processOutput(0);
135
+ }),
136
+ );
137
+ const first = yield* host.ensureAgentReady(() => Effect.void);
138
+ expect((yield* host.ensureAgentReady(() => Effect.void)).agentDevice).toEqual(
139
+ first.agentDevice,
140
+ );
141
+ expect(attempts).toBe(1);
142
+
143
+ yield* Ref.set(daemonHealthy, false);
144
+ const restarted = yield* host.ensureAgentReady(() => Effect.void);
145
+ expect(attempts).toBe(2);
146
+ expect(restarted.agentDevice).toMatchObject({
147
+ baseUrl: "http://127.0.0.1:12347",
148
+ token: "token-2",
149
+ });
150
+ }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
151
+ );
152
+
153
it.effect("reports an incompatible lock immediately and retains it for manual repair", () =>
154
Effect.gen(function* () {
155
const result = processOutput(
apps/server/src/device/LocalDeviceHost.ts
+9
-2
@@ -522,7 +522,7 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
522
});
523
524
const startAgentDeviceDaemon = Effect.fn("LocalDeviceHost.startAgentDeviceDaemon")(function* (
525
- agentTool: DeviceToolPaths,
525
+ agentTool: Pick<DeviceToolPaths, "entryPath">,
526
nodePath: string,
527
): Effect.fn.Return<
528
DeviceHost.AgentDeviceEndpoint,
@@ -728,7 +728,14 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
728
DeviceHost.DeviceHostAgentReady
729
> {
730
const running = yield* ensureHubReady(onPhase);
731
- if (running.agentDevice) return { ...toReady(running), agentDevice: running.agentDevice };
731
+ if (running.agentDevice) {
732
+ const agentDevice = yield* startAgentDeviceDaemon(
733
+ running.agentDevice,
734
+ running.hub.nodePath,
735
+ );
736
+ yield* Ref.set(runningRef, { ...running, agentDevice });
737
+ return { ...toReady(running), agentDevice };
738
+ }
739
const installed = yield* isAgentDeviceInstalled(config.baseDir).pipe(
740
Effect.provideService(FileSystem.FileSystem, fs),
741
Effect.provideService(Path.Path, path),