Skip to content

Commit bef663a

Browse files
fix(device): recover cached agent daemons after exit (#23441)
Co-authored-by: maria-rcks <[email protected]>
1 parent c6ef3b4 commit bef663a

2 files changed

Lines changed: 54 additions & 4 deletions

File tree

‎apps/server/src/device/LocalDeviceHost.test.ts‎

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as Layer from "effect/Layer";
66
import * as Option from "effect/Option";
77
import * as Path from "effect/Path";
88
import * as PlatformError from "effect/PlatformError";
9+
import * as Ref from "effect/Ref";
910
import * as FileSystem from "effect/FileSystem";
1011
import * as Sink from "effect/Sink";
1112
import * as Stream from "effect/Stream";
@@ -45,6 +46,7 @@ const agentFixture = Effect.fn("agentFixture")(function* (
4546
const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-device-agent-start-" });
4647
const hub = { running: true, spawnFails: false };
4748
const stateDir = agentDeviceStateDir(path, path.join(baseDir, "userdata"));
49+
const daemonHealthy = yield* Ref.make(true);
4850
for (const [name, version, entry] of [
4951
["expo-device-hub", DEVICE_HUB_VERSION, "dist/server/cli.mjs"],
5052
["agent-device", AGENT_DEVICE_VERSION, "bin/agent-device.mjs"],
@@ -98,15 +100,56 @@ const agentFixture = Effect.fn("agentFixture")(function* (
98100
Effect.provideService(
99101
HttpClient.HttpClient,
100102
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+
),
102113
),
103114
),
104115
);
105116
yield* host.ensureReady(() => Effect.void);
106-
return { host, fs, stateDir, hub };
117+
return { host, fs, stateDir, hub, daemonHealthy };
107118
});
108119

109120
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+
110153
it.effect("reports an incompatible lock immediately and retains it for manual repair", () =>
111154
Effect.gen(function* () {
112155
const result = processOutput(

‎apps/server/src/device/LocalDeviceHost.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
522522
});
523523

524524
const startAgentDeviceDaemon = Effect.fn("LocalDeviceHost.startAgentDeviceDaemon")(function* (
525-
agentTool: DeviceToolPaths,
525+
agentTool: Pick<DeviceToolPaths, "entryPath">,
526526
nodePath: string,
527527
): Effect.fn.Return<
528528
DeviceHost.AgentDeviceEndpoint,
@@ -728,7 +728,14 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
728728
DeviceHost.DeviceHostAgentReady
729729
> {
730730
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+
}
732739
const installed = yield* isAgentDeviceInstalled(config.baseDir).pipe(
733740
Effect.provideService(FileSystem.FileSystem, fs),
734741
Effect.provideService(Path.Path, path),

0 commit comments

Comments
 (0)