Skip to content

Commit 301bea2

Browse files
committed
fix(devices): reuse daemon after startup output failures
1 parent 3a59720 commit 301bea2

2 files changed

Lines changed: 44 additions & 7 deletions

File tree

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,40 @@ describe("AgentDevice startup", () => {
171171
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
172172
);
173173

174+
it.effect("uses a registered daemon when reading startup command output fails", () =>
175+
Effect.gen(function* () {
176+
const fs = yield* FileSystem.FileSystem;
177+
const cause = new ProcessRunner.ProcessReadError({
178+
command: process.execPath,
179+
argumentCount: 3,
180+
stream: "stdout",
181+
cause: new Error("output read failed after registration"),
182+
});
183+
let attempts = 0;
184+
const { host } = yield* agentFixture((stateDir) =>
185+
Effect.gen(function* () {
186+
attempts++;
187+
yield* fs
188+
.writeFileString(
189+
`${stateDir}/daemon.json`,
190+
JSON.stringify({ httpPort: 12345, token: "test-token" }),
191+
)
192+
.pipe(Effect.orDie);
193+
return yield* cause;
194+
}),
195+
);
196+
const ready = yield* host.ensureAgentReady(() => Effect.void);
197+
expect(ready.agentDevice).toMatchObject({
198+
baseUrl: "http://127.0.0.1:12345",
199+
token: "test-token",
200+
});
201+
expect((yield* host.ensureAgentReady(() => Effect.void)).agentDevice).toEqual(
202+
ready.agentDevice,
203+
);
204+
expect(attempts).toBe(1);
205+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
206+
);
207+
174208
it.effect("allows retry and uses a registered daemon even when discovery exits nonzero", () =>
175209
Effect.gen(function* () {
176210
const fs = yield* FileSystem.FileSystem;

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -554,24 +554,27 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
554554
}
555555
// There is no `daemon start`; the first command in a state dir spawns the
556556
// daemon and blocks until it answers. `devices` is the cheapest one.
557-
const result = yield* runner
557+
const runResult = yield* runner
558558
.run({
559559
command: nodePath,
560560
args: [agentTool.entryPath, "devices", "--json"],
561561
env: daemonEnvironment,
562562
timeout: Duration.millis(DAEMON_READY_TIMEOUT_MS),
563563
timeoutBehavior: "timedOutResult",
564564
})
565-
.pipe(
566-
Effect.mapError(
567-
(cause) =>
568-
new DeviceHost.DeviceHostError({ hostId, step: "starting agent tools", cause }),
569-
),
570-
);
565+
.pipe(Effect.result);
571566
// Discovery can fail after the daemon starts. A published endpoint still
572567
// lets agents connect; absent one, preserve the CLI failure instead of polling.
573568
const started = yield* readDaemonFile().pipe(Effect.option);
574569
if (started._tag === "Some") return toEndpoint(started.value);
570+
if (runResult._tag === "Failure") {
571+
return yield* new DeviceHost.DeviceHostError({
572+
hostId,
573+
step: "starting agent tools",
574+
cause: runResult.failure,
575+
});
576+
}
577+
const result = runResult.success;
575578
if (result.timedOut) {
576579
return yield* new DeviceHost.DeviceHostTimeoutError({
577580
hostId,

0 commit comments

Comments
 (0)