@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 860 lines 33.4 KB
Raw
1 import {
2 SERVER_EXIT_CODE_MANAGED_POLICY,
3 SERVER_EXIT_CODE_STATE_DIR_OWNED,
4 DesktopBackendBootstrap,
5 type DesktopBackendBootstrap as DesktopBackendBootstrapValue,
6 DesktopTelemetryControlMessage,
7 } from "@t3tools/contracts";
8 import {
9 DATABASE_MIGRATION_PROGRESS_MESSAGE,
10 DATABASE_MIGRATIONS_FINISHED_MESSAGE,
11 DATABASE_MIGRATIONS_STARTED_MESSAGE,
12 } from "@t3tools/shared/databaseMigrationLog";
13 import { assert, describe, it } from "@effect/vitest";
14 import * as Clock from "effect/Clock";
15 import * as Deferred from "effect/Deferred";
16 import * as Duration from "effect/Duration";
17 import * as Effect from "effect/Effect";
18 import * as FileSystem from "effect/FileSystem";
19 import * as Fiber from "effect/Fiber";
20 import * as Layer from "effect/Layer";
21 import * as Option from "effect/Option";
22 import * as PlatformError from "effect/PlatformError";
23 import * as Queue from "effect/Queue";
24 import * as Ref from "effect/Ref";
25 import * as Schema from "effect/Schema";
26 import * as Sink from "effect/Sink";
27 import * as Scope from "effect/Scope";
28 import * as Stream from "effect/Stream";
29 import * as TestClock from "effect/testing/TestClock";
30 import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/http";
31 import { ChildProcess, ChildProcessSpawner } from "effect/process";
32
33 import * as DesktopBackendManager from "./DesktopBackendManager.ts";
34 import * as DesktopObservability from "../app/DesktopObservability.ts";
35 import * as DesktopTelemetryPublisher from "../telemetry/DesktopTelemetryPublisher.ts";
36 import * as DesktopBrowserHost from "../preview/DesktopBrowserHost.ts";
37 import * as DesktopWslEnvironment from "../wsl/DesktopWslEnvironment.ts";
38
39 const decodeDesktopBackendBootstrap = Schema.decodeEffect(
40 Schema.fromJsonString(DesktopBackendBootstrap),
41 );
42 const encodeDesktopTelemetryControl = Schema.encodeSync(
43 Schema.fromJsonString(DesktopTelemetryControlMessage),
44 );
45
46 const baseConfig: DesktopBackendManager.DesktopBackendStartConfig = {
47 executablePath: "/electron",
48 args: ["/server/bin.mjs", "--bootstrap-fd", "3"],
49 entryPath: "/server/bin.mjs",
50 cwd: "/server",
51 env: { ELECTRON_RUN_AS_NODE: "1" },
52 bootstrap: {
53 mode: "desktop",
54 noBrowser: true,
55 port: 3773,
56 t3Home: "/tmp/t3",
57 host: "127.0.0.1",
58 desktopBootstrapToken: "token",
59 tailscaleServeEnabled: false,
60 tailscaleServePort: 443,
61 desktopTelemetryFd: 4,
62 desktopTelemetryControlFd: 5,
63 },
64 bootstrapDelivery: "fd3",
65 extendEnv: true,
66 httpBaseUrl: new URL("http://127.0.0.1:3773"),
67 captureOutput: true,
68 preflightFailure: Option.none(),
69 };
70
71 const configWithObservability: DesktopBackendBootstrapValue = {
72 ...baseConfig.bootstrap,
73 tailscaleServeEnabled: true,
74 desktopTelemetryFd: 4,
75 otlpTracesUrl: "http://127.0.0.1:4318/v1/traces",
76 };
77
78 function makeProcess(options?: {
79 readonly stdout?: Stream.Stream<Uint8Array, PlatformError.PlatformError>;
80 readonly stderr?: Stream.Stream<Uint8Array, PlatformError.PlatformError>;
81 readonly exitCode?: Effect.Effect<ChildProcessSpawner.ExitCode, PlatformError.PlatformError>;
82 readonly kill?: ChildProcessSpawner.ChildProcessHandle["kill"];
83 readonly getOutputFd?: ChildProcessSpawner.ChildProcessHandle["getOutputFd"];
84 }): ChildProcessSpawner.ChildProcessHandle {
85 return ChildProcessSpawner.makeHandle({
86 pid: ChildProcessSpawner.ProcessId(123),
87 stdout: options?.stdout ?? Stream.empty,
88 stderr: options?.stderr ?? Stream.empty,
89 all: Stream.merge(options?.stdout ?? Stream.empty, options?.stderr ?? Stream.empty),
90 exitCode: options?.exitCode ?? Effect.succeed(ChildProcessSpawner.ExitCode(0)),
91 isRunning: Effect.succeed(false),
92 kill: options?.kill ?? (() => Effect.void),
93 stdin: Sink.drain,
94 getInputFd: () => Sink.drain,
95 getOutputFd: options?.getOutputFd ?? (() => Stream.empty),
96 unref: Effect.succeed(Effect.void),
97 });
98 }
99
100 function responseForRequest(
101 request: HttpClientRequest.HttpClientRequest,
102 status: number,
103 ): HttpClientResponse.HttpClientResponse {
104 return HttpClientResponse.fromWeb(request, new Response(null, { status }));
105 }
106
107 function layerHttpClient(
108 handler: (
109 request: HttpClientRequest.HttpClientRequest,
110 ) => Effect.Effect<HttpClientResponse.HttpClientResponse>,
111 ) {
112 return Layer.succeed(
113 HttpClient.HttpClient,
114 HttpClient.make((request) => handler(request)),
115 );
116 }
117
118 const layerHealthyHttpClient = layerHttpClient((request) =>
119 Effect.succeed(responseForRequest(request, 200)),
120 );
121
122 function decodeBootstrap(raw: string) {
123 return decodeDesktopBackendBootstrap(raw);
124 }
125
126 interface MakeInstanceInput {
127 readonly spawnerLayer: Layer.Layer<ChildProcessSpawner.ChildProcessSpawner>;
128 readonly httpClientLayer?: Layer.Layer<HttpClient.HttpClient>;
129 readonly backendOutputLog?: Partial<DesktopObservability.DesktopBackendOutputLogShape>;
130 readonly onReady?: Effect.Effect<void>;
131 readonly onShutdown?: Effect.Effect<void>;
132 readonly onStateDirOwned?: Effect.Effect<void>;
133 readonly onStartupFailure?: (
134 failure: DesktopBackendManager.BackendStartupFailure,
135 ) => Effect.Effect<void>;
136 readonly onPreflightFailed?: (
137 failure: DesktopBackendManager.PreflightFailure,
138 ) => Effect.Effect<boolean>;
139 readonly config?: DesktopBackendManager.DesktopBackendStartConfig;
140 readonly configResolve?: Effect.Effect<
141 DesktopBackendManager.DesktopBackendStartConfig,
142 PlatformError.PlatformError
143 >;
144 readonly desktopTelemetryStream?: Stream.Stream<Uint8Array>;
145 readonly desktopTelemetryPublisher?: Partial<
146 DesktopTelemetryPublisher.DesktopTelemetryPublisher["Service"]
147 >;
148 readonly pruneRuntimes?: (distro: string | null, runtimeId: string) => Effect.Effect<void>;
149 }
150
151 function makeTestInstance(input: MakeInstanceInput) {
152 const stubLog: DesktopObservability.DesktopBackendOutputLogShape = {
153 beginSession: () => Effect.void,
154 writeOutputChunk: () => Effect.void,
155 persistFailureSnapshot: () => Effect.void,
156 persistFailure: () => Effect.void,
157 discardSession: Effect.void,
158 ...input.backendOutputLog,
159 };
160 const layerServices = Layer.mergeAll(
161 FileSystem.layerNoop({
162 exists: () => Effect.succeed(true),
163 }),
164 input.spawnerLayer,
165 input.httpClientLayer ?? layerHealthyHttpClient,
166 Layer.succeed(DesktopObservability.DesktopBackendOutputLogFactory, {
167 forInstance: () => Effect.succeed(stubLog),
168 } satisfies DesktopObservability.DesktopBackendOutputLogFactory["Service"]),
169 Layer.succeed(DesktopTelemetryPublisher.DesktopTelemetryPublisher, {
170 latest: Effect.succeedNone,
171 changes: Stream.empty,
172 encoded: input.desktopTelemetryStream ?? Stream.empty,
173 handleControlForSource: () => Effect.void,
174 removeControlSource: () => Effect.void,
175 publishUpdateReport: () => Effect.void,
176 updateRequests: Stream.empty,
177 updateCommits: Stream.empty,
178 updateCancellations: Stream.empty,
179 ...input.desktopTelemetryPublisher,
180 }),
181 DesktopBrowserHost.layer,
182 DesktopWslEnvironment.layerTest(
183 input.pruneRuntimes === undefined ? {} : { pruneRuntimes: input.pruneRuntimes },
184 ),
185 );
186
187 const instance = DesktopBackendManager.makeBackendInstance({
188 id: DesktopBackendManager.PRIMARY_INSTANCE_ID,
189 label: Effect.succeed("Windows"),
190 configResolve: input.configResolve ?? Effect.succeed(input.config ?? baseConfig),
191 ...(input.onReady ? { onReady: () => input.onReady! } : {}),
192 ...(input.onShutdown ? { onShutdown: () => input.onShutdown! } : {}),
193 ...(input.onStateDirOwned ? { onStateDirOwned: () => input.onStateDirOwned! } : {}),
194 ...(input.onPreflightFailed ? { onPreflightFailed: input.onPreflightFailed } : {}),
195 ...(input.onStartupFailure ? { onStartupFailure: input.onStartupFailure } : {}),
196 });
197
198 return instance.pipe(Effect.provide(layerServices));
199 }
200
201 describe("DesktopBackendManager", () => {
202 it.effect("notifies readiness waiters on HTTP readiness and stop without clock advancement", () =>
203 Effect.gen(function* () {
204 const probe = yield* Deferred.make<void>();
205 const ready = yield* Deferred.make<void>();
206 const exit = yield* Deferred.make<ChildProcessSpawner.ExitCode>();
207 const instance = yield* makeTestInstance({
208 spawnerLayer: Layer.succeed(
209 ChildProcessSpawner.ChildProcessSpawner,
210 ChildProcessSpawner.make(() =>
211 Effect.acquireRelease(
212 Effect.succeed(
213 makeProcess({
214 exitCode: Deferred.await(exit),
215 kill: () =>
216 Deferred.succeed(exit, ChildProcessSpawner.ExitCode(0)).pipe(Effect.asVoid),
217 }),
218 ),
219 () => Deferred.succeed(exit, ChildProcessSpawner.ExitCode(0)),
220 ),
221 ),
222 ),
223 httpClientLayer: Layer.succeed(
224 HttpClient.HttpClient,
225 HttpClient.make((request) =>
226 Deferred.succeed(probe, undefined).pipe(
227 Effect.andThen(Deferred.await(ready)),
228 Effect.as(HttpClientResponse.fromWeb(request, new Response("{}"))),
229 ),
230 ),
231 ),
232 });
233 yield* instance.start;
234 yield* Deferred.await(probe);
235 const waiter = yield* instance.waitForReady(Duration.seconds(15)).pipe(Effect.forkChild);
236 yield* Deferred.succeed(ready, undefined);
237 assert.isTrue(yield* Fiber.join(waiter));
238 yield* instance.stop();
239 assert.isFalse(yield* instance.waitForReady(Duration.seconds(15)));
240 }),
241 );
242
243 it.effect("notices a WSL backend soon after it listens despite slow refused connects", () =>
244 Effect.gen(function* () {
245 const ready = yield* Deferred.make<void>();
246 const exit = yield* Deferred.make<ChildProcessSpawner.ExitCode>();
247 const instance = yield* makeTestInstance({
248 config: { ...baseConfig, httpBaseUrl: new URL("http://172.20.84.98:3773") },
249 onReady: Deferred.succeed(ready, undefined).pipe(Effect.asVoid),
250 spawnerLayer: Layer.succeed(
251 ChildProcessSpawner.ChildProcessSpawner,
252 ChildProcessSpawner.make(() =>
253 Effect.acquireRelease(
254 Effect.succeed(makeProcess({ exitCode: Deferred.await(exit) })),
255 () => Deferred.succeed(exit, ChildProcessSpawner.ExitCode(0)),
256 ),
257 ),
258 ),
259 // Before the server listens at 300 ms, each refused connect takes a second to fail.
260 httpClientLayer: layerHttpClient((request) =>
261 Clock.currentTimeMillis.pipe(
262 Effect.flatMap((now) =>
263 now < 300
264 ? Effect.sleep(Duration.seconds(1)).pipe(
265 Effect.as(responseForRequest(request, 503)),
266 )
267 : Effect.succeed(responseForRequest(request, 200)),
268 ),
269 ),
270 ),
271 });
272 yield* instance.start;
273 yield* TestClock.adjust(Duration.millis(600));
274
275 assert.isTrue(yield* Deferred.isDone(ready));
276 yield* instance.stop();
277 }),
278 );
279
280 it.effect("spawns the backend with fd3 bootstrap and fd4 telemetry", () =>
281 Effect.gen(function* () {
282 let spawnedCommand: ChildProcess.Command | undefined;
283 let bootstrapJson = "";
284 let telemetryJson = "";
285 let readyCount = 0;
286 const ready = yield* Deferred.make<void>();
287 const exited = yield* Queue.unbounded<void>();
288
289 const layerSpawner = Layer.succeed(
290 ChildProcessSpawner.ChildProcessSpawner,
291 ChildProcessSpawner.make((command) =>
292 Effect.gen(function* () {
293 spawnedCommand = command;
294 if (command._tag === "StandardCommand") {
295 const fd3 = command.options.additionalFds?.fd3;
296 if (fd3?.type === "input" && fd3.stream) {
297 bootstrapJson = yield* fd3.stream.pipe(Stream.decodeText(), Stream.mkString);
298 }
299 const fd4 = command.options.additionalFds?.fd4;
300 if (fd4?.type === "input" && fd4.stream) {
301 telemetryJson = yield* fd4.stream.pipe(Stream.decodeText(), Stream.mkString);
302 }
303 }
304
305 return makeProcess({
306 exitCode: Deferred.await(ready).pipe(Effect.as(ChildProcessSpawner.ExitCode(0))),
307 });
308 }),
309 ),
310 );
311
312 const instance = yield* makeTestInstance({
313 config: {
314 ...baseConfig,
315 bootstrap: configWithObservability,
316 },
317 spawnerLayer: layerSpawner,
318 desktopTelemetryStream: Stream.encodeText(
319 Stream.make('{"version":1,"type":"desktopTelemetryHello","electronPid":123}\n'),
320 ),
321 onReady: Effect.sync(() => {
322 readyCount += 1;
323 }).pipe(Effect.andThen(Deferred.succeed(ready, void 0)), Effect.asVoid),
324 backendOutputLog: {
325 persistFailure: () => Queue.offer(exited, void 0).pipe(Effect.asVoid),
326 },
327 });
328
329 yield* instance.start;
330 yield* Queue.take(exited);
331
332 assert.equal(readyCount, 1);
333 assert.isDefined(spawnedCommand);
334 if (spawnedCommand._tag !== "StandardCommand") {
335 throw new Error("Expected backend to spawn a standard command.");
336 }
337
338 assert.equal(spawnedCommand.command, "/electron");
339 assert.deepEqual(spawnedCommand.args, ["/server/bin.mjs", "--bootstrap-fd", "3"]);
340 assert.equal(spawnedCommand.options.cwd, "/server");
341 assert.equal(spawnedCommand.options.extendEnv, true);
342 assert.equal(spawnedCommand.options.stdout, "pipe");
343 assert.equal(spawnedCommand.options.stderr, "pipe");
344 assert.equal(spawnedCommand.options.killSignal, "SIGTERM");
345 assert.isDefined(spawnedCommand.options.forceKillAfter);
346 assert.equal(spawnedCommand.options.additionalFds?.fd4?.type, "input");
347 assert.equal(spawnedCommand.options.additionalFds?.fd5?.type, "output");
348 assert.equal(
349 Duration.toMillis(Duration.fromInputUnsafe(spawnedCommand.options.forceKillAfter)),
350 5_000,
351 );
352
353 assert.deepEqual(yield* decodeBootstrap(bootstrapJson), configWithObservability);
354 assert.equal(
355 telemetryJson,
356 '{"version":1,"type":"desktopTelemetryHello","electronPid":123}\n',
357 );
358 }),
359 );
360
361 it.effect("reports a SIGKILL exit without an exit code", () =>
362 Effect.gen(function* () {
363 const exitCause = PlatformError.systemError({
364 _tag: "Unknown",
365 module: "ChildProcess",
366 method: "exitCode",
367 cause: new Error("Process interrupted due to receipt of signal: 'SIGKILL'"),
368 });
369 const layerSpawner = Layer.succeed(
370 ChildProcessSpawner.ChildProcessSpawner,
371 ChildProcessSpawner.make(() =>
372 Effect.succeed(makeProcess({ exitCode: Effect.fail(exitCause) })),
373 ),
374 );
375
376 const exit = yield* DesktopBackendManager.runBackendProcess({
377 ...baseConfig,
378 desktopTelemetryStream: Stream.empty,
379 }).pipe(Effect.scoped, Effect.provide(Layer.merge(layerSpawner, layerHealthyHttpClient)));
380
381 assert.deepEqual(exit, { code: Option.none(), reason: "killed by SIGKILL" });
382 }),
383 );
384
385 it.effect("reports child exit before waiting for trailing output to drain", () =>
386 Effect.gen(function* () {
387 const exitObserved = yield* Deferred.make<void>();
388 const finishOutputDrain = yield* Deferred.make<void>();
389 const layerSpawner = Layer.succeed(
390 ChildProcessSpawner.ChildProcessSpawner,
391 ChildProcessSpawner.make(() =>
392 Effect.succeed(
393 makeProcess({
394 stdout: Stream.fromEffect(
395 Deferred.await(finishOutputDrain).pipe(
396 Effect.as(new TextEncoder().encode("trailing output\n")),
397 ),
398 ),
399 exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(1)),
400 }),
401 ),
402 ),
403 );
404
405 const runFiber = yield* DesktopBackendManager.runBackendProcess({
406 ...baseConfig,
407 desktopTelemetryStream: Stream.empty,
408 onExitObserved: () => Deferred.succeed(exitObserved, void 0).pipe(Effect.asVoid),
409 }).pipe(Effect.provide(Layer.merge(layerSpawner, layerHealthyHttpClient)), Effect.forkChild);
410
411 yield* Deferred.await(exitObserved);
412 assert.isUndefined(runFiber.pollUnsafe());
413
414 yield* Deferred.succeed(finishOutputDrain, void 0);
415 assert.equal((yield* Fiber.join(runFiber)).code.pipe(Option.getOrUndefined), 1);
416 }),
417 );
418
419 it.effect("becomes ready from the server ready line while HTTP probes stall", () =>
420 Effect.scoped(
421 Effect.gen(function* () {
422 const probeStarted = yield* Deferred.make<void>();
423 const exit = yield* Deferred.make<ChildProcessSpawner.ExitCode>();
424 const readyLine = encodeDesktopTelemetryControl({ version: 1, type: "serverReady" });
425 const instance = yield* makeTestInstance({
426 spawnerLayer: Layer.succeed(
427 ChildProcessSpawner.ChildProcessSpawner,
428 ChildProcessSpawner.make(() =>
429 Effect.succeed(
430 makeProcess({
431 getOutputFd: (fd) =>
432 fd === 5
433 ? Stream.fromEffect(Deferred.await(probeStarted)).pipe(
434 Stream.flatMap(() => Stream.encodeText(Stream.make(`${readyLine}\n`))),
435 )
436 : Stream.empty,
437 exitCode: Deferred.await(exit),
438 kill: () =>
439 Deferred.succeed(exit, ChildProcessSpawner.ExitCode(0)).pipe(Effect.asVoid),
440 }),
441 ),
442 ),
443 ),
444 httpClientLayer: layerHttpClient(() =>
445 Deferred.succeed(probeStarted, undefined).pipe(Effect.andThen(Effect.never)),
446 ),
447 });
448
449 yield* instance.start;
450 assert.isTrue(yield* instance.waitForReady(Duration.seconds(15)));
451 }),
452 ),
453 );
454
455 it.effect("continues routing desktop telemetry control messages after an invalid line", () =>
456 Effect.gen(function* () {
457 const handled = yield* Deferred.make<boolean>();
458 const controlMessage = encodeDesktopTelemetryControl({
459 version: 1,
460 type: "setDiagnosticsDemand",
461 enabled: true,
462 });
463 const layerSpawner = Layer.succeed(
464 ChildProcessSpawner.ChildProcessSpawner,
465 ChildProcessSpawner.make(() =>
466 Effect.succeed(
467 makeProcess({
468 getOutputFd: (fd) =>
469 fd === 5
470 ? Stream.encodeText(Stream.make(`not-json\n${controlMessage}\n`))
471 : Stream.empty,
472 exitCode: Deferred.await(handled).pipe(Effect.as(ChildProcessSpawner.ExitCode(0))),
473 }),
474 ),
475 ),
476 );
477 const instance = yield* makeTestInstance({
478 spawnerLayer: layerSpawner,
479 desktopTelemetryPublisher: {
480 handleControlForSource: (_sourceId, message) =>
481 message.type === "setDiagnosticsDemand"
482 ? Deferred.succeed(handled, message.enabled).pipe(Effect.asVoid)
483 : Effect.void,
484 },
485 });
486
487 yield* instance.start;
488 assert.isTrue(yield* Deferred.await(handled));
489 }),
490 );
491
492 it.effect("stops the scoped process with output still pending", () =>
493 Effect.gen(function* () {
494 let startCount = 0;
495 let closedCount = 0;
496 const closed = yield* Deferred.make<void>();
497 const teardownStarted = yield* Deferred.make<void>();
498 const finishTeardown = yield* Deferred.make<void>();
499 const startedPids = yield* Queue.unbounded<number>();
500 const ready = yield* Deferred.make<void>();
501 const backendReadyFlag = yield* Ref.make(false);
502 let shutdownCount = 0;
503 let persistedFailureCount = 0;
504 let discardedSessionCount = 0;
505 let removedTelemetrySources = 0;
506
507 const layerSpawner = Layer.succeed(
508 ChildProcessSpawner.ChildProcessSpawner,
509 ChildProcessSpawner.make(() =>
510 Effect.gen(function* () {
511 const scope = yield* Scope.Scope;
512 startCount += 1;
513 yield* Queue.offer(startedPids, 123);
514 const close = Deferred.succeed(teardownStarted, undefined).pipe(
515 Effect.andThen(Deferred.await(finishTeardown)),
516 Effect.andThen(
517 Effect.sync(() => {
518 closedCount += 1;
519 }),
520 ),
521 Effect.andThen(Deferred.succeed(closed, void 0)),
522 Effect.asVoid,
523 );
524
525 yield* Scope.addFinalizer(scope, close);
526
527 return makeProcess({
528 stdout: Stream.never,
529 exitCode: Deferred.await(closed).pipe(Effect.as(ChildProcessSpawner.ExitCode(0))),
530 kill: () => close,
531 });
532 }),
533 ),
534 );
535
536 const instance = yield* makeTestInstance({
537 spawnerLayer: layerSpawner,
538 onReady: Ref.set(backendReadyFlag, true).pipe(
539 Effect.andThen(Deferred.succeed(ready, void 0)),
540 Effect.asVoid,
541 ),
542 onShutdown: Ref.set(backendReadyFlag, false).pipe(
543 Effect.andThen(
544 Effect.sync(() => {
545 shutdownCount += 1;
546 }),
547 ),
548 ),
549 backendOutputLog: {
550 persistFailure: () =>
551 Effect.sync(() => {
552 persistedFailureCount += 1;
553 }),
554 discardSession: Effect.sync(() => {
555 discardedSessionCount += 1;
556 }),
557 },
558 desktopTelemetryPublisher: {
559 removeControlSource: () =>
560 Effect.sync(() => {
561 removedTelemetrySources += 1;
562 }),
563 },
564 });
565 assert.isTrue(Option.isNone(yield* instance.currentConfig));
566
567 yield* instance.start;
568 assert.equal(yield* Queue.take(startedPids), 123);
569 yield* Deferred.await(ready);
570 assert.isTrue(yield* Ref.get(backendReadyFlag));
571 assert.deepEqual(yield* instance.currentConfig, Option.some(baseConfig));
572
573 const runningSnapshot = yield* instance.snapshot;
574 assert.equal(runningSnapshot.ready, true);
575 assert.deepEqual(runningSnapshot.activePid, Option.some(123));
576
577 const stopFiber = yield* instance.stop().pipe(Effect.forkChild);
578 yield* Deferred.await(teardownStarted).pipe(Effect.timeout("1 second"));
579 assert.isFalse(yield* Ref.get(backendReadyFlag));
580 assert.equal(shutdownCount, 1);
581 yield* Deferred.succeed(finishTeardown, undefined);
582 yield* Fiber.join(stopFiber).pipe(Effect.timeout("1 second"));
583 assert.equal(startCount, 1);
584 assert.equal(closedCount, 1);
585 assert.equal(persistedFailureCount, 0);
586 assert.equal(discardedSessionCount, 1);
587 assert.equal(removedTelemetrySources, 1);
588
589 const stoppedSnapshot = yield* instance.snapshot;
590 assert.isFalse(yield* Ref.get(backendReadyFlag));
591 assert.equal(shutdownCount, 1);
592 assert.equal(stoppedSnapshot.desiredRunning, false);
593 assert.equal(stoppedSnapshot.ready, false);
594 assert.equal(Option.isNone(stoppedSnapshot.activePid), true);
595 }),
596 );
597
598 it.effect("restarts when start is requested during stop teardown", () =>
599 Effect.gen(function* () {
600 const starts = yield* Queue.unbounded<number>();
601 const teardownStarted = yield* Deferred.make<void>();
602 const finishTeardown = yield* Deferred.make<void>();
603 let startCount = 0;
604
605 const layerSpawner = Layer.succeed(
606 ChildProcessSpawner.ChildProcessSpawner,
607 ChildProcessSpawner.make(() =>
608 Effect.gen(function* () {
609 const scope = yield* Scope.Scope;
610 const closed = yield* Deferred.make<void>();
611 startCount += 1;
612 yield* Queue.offer(starts, startCount);
613 if (startCount === 1) {
614 yield* Scope.addFinalizer(
615 scope,
616 Deferred.succeed(teardownStarted, undefined).pipe(
617 Effect.andThen(Deferred.await(finishTeardown)),
618 Effect.andThen(Deferred.succeed(closed, undefined)),
619 Effect.asVoid,
620 ),
621 );
622 } else {
623 yield* Scope.addFinalizer(
624 scope,
625 Deferred.succeed(closed, undefined).pipe(Effect.asVoid),
626 );
627 }
628 return makeProcess({
629 exitCode: Deferred.await(closed).pipe(Effect.as(ChildProcessSpawner.ExitCode(0))),
630 });
631 }),
632 ),
633 );
634
635 const instance = yield* makeTestInstance({
636 spawnerLayer: layerSpawner,
637 httpClientLayer: layerHttpClient(() => Effect.never),
638 });
639
640 yield* instance.start;
641 assert.equal(yield* Queue.take(starts), 1);
642
643 const stopFiber = yield* instance.stop().pipe(Effect.forkChild);
644 yield* Deferred.await(teardownStarted).pipe(Effect.timeout("1 second"));
645 yield* instance.start;
646 assert.equal((yield* instance.snapshot).desiredRunning, true);
647
648 yield* Deferred.succeed(finishTeardown, undefined);
649 yield* Fiber.join(stopFiber).pipe(Effect.timeout("1 second"));
650 yield* TestClock.adjust(Duration.millis(500));
651
652 assert.equal(yield* Queue.take(starts).pipe(Effect.timeout("1 second")), 2);
653 const restarted = yield* instance.snapshot;
654 assert.equal(restarted.desiredRunning, true);
655 assert.deepEqual(restarted.activePid, Option.some(123));
656 }).pipe(Effect.provide(TestClock.layer())),
657 );
658
659 it.effect("reports state-directory ownership once without scheduling another backend", () =>
660 Effect.gen(function* () {
661 let startCount = 0;
662 const refused = yield* Deferred.make<void>();
663 const instance = yield* makeTestInstance({
664 spawnerLayer: Layer.succeed(
665 ChildProcessSpawner.ChildProcessSpawner,
666 ChildProcessSpawner.make(() =>
667 Effect.sync(() => {
668 startCount += 1;
669 return makeProcess({
670 exitCode: Effect.succeed(
671 ChildProcessSpawner.ExitCode(SERVER_EXIT_CODE_STATE_DIR_OWNED),
672 ),
673 });
674 }),
675 ),
676 ),
677 httpClientLayer: layerHttpClient(() => Effect.never),
678 onStateDirOwned: Deferred.succeed(refused, undefined).pipe(Effect.asVoid),
679 });
680 yield* instance.start;
681 yield* Deferred.await(refused);
682 const snapshot = yield* instance.snapshot;
683 assert.isFalse(snapshot.desiredRunning);
684 assert.isFalse(snapshot.restartScheduled);
685 yield* TestClock.adjust(Duration.minutes(1));
686 assert.equal(startCount, 1);
687 }).pipe(Effect.provide(TestClock.layer())),
688 );
689
690 // A policy refusal is reported on the first exit, since retrying cannot fix it.
691 it.effect.each([
692 { code: 1, exits: 3, managedPolicy: false },
693 { code: SERVER_EXIT_CODE_MANAGED_POLICY, exits: 1, managedPolicy: true },
694 ])(
695 "reports exit $code with the server output after $exits exits",
696 ({ code, exits, managedPolicy }) =>
697 Effect.gen(function* () {
698 const message = "Error: Cannot find module 'zod/v4' in /Users/René";
699 let exitCount = 0;
700 const firstExit = yield* Deferred.make<void>();
701 const reports = yield* Queue.unbounded<{
702 readonly exitCount: number;
703 readonly failure: DesktopBackendManager.BackendStartupFailure;
704 }>();
705 const instance = yield* makeTestInstance({
706 spawnerLayer: Layer.succeed(
707 ChildProcessSpawner.ChildProcessSpawner,
708 ChildProcessSpawner.make(() =>
709 Effect.succeed(
710 makeProcess({
711 // "é" split across chunks must not decode as U+FFFD, and the 4,000-char cut
712 // lands inside the emoji's surrogate pair.
713 stderr: Stream.make(
714 new TextEncoder().encode(
715 `😀${"x".repeat(3_999 - message.length)}${message.slice(0, -1)}`,
716 ),
717 new Uint8Array([0xc3]),
718 new Uint8Array([0xa9]),
719 ),
720 exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(code)),
721 }),
722 ),
723 ),
724 ),
725 httpClientLayer: layerHttpClient(() => Effect.never),
726 backendOutputLog: {
727 persistFailure: () => {
728 exitCount += 1;
729 return Deferred.succeed(firstExit, undefined).pipe(Effect.asVoid);
730 },
731 },
732 onStartupFailure: (failure) =>
733 Queue.offer(reports, { exitCount, failure }).pipe(Effect.asVoid),
734 });
735 yield* instance.start;
736 yield* Deferred.await(firstExit);
737 yield* TestClock.adjust(Duration.minutes(1));
738 const { exitCount: exitsWhenReported, failure } = yield* Queue.take(reports);
739 assert.equal(exitsWhenReported, exits);
740 assert.equal(failure.reason, `The server could not start (code=${code}).`);
741 assert.equal(failure.managedPolicy, managedPolicy);
742 assert.isTrue(failure.output.endsWith(message));
743 assert.isTrue(failure.output.isWellFormed());
744 }).pipe(Effect.provide(TestClock.layer())),
745 );
746
747 it.effect("waits out a slow startup and reports one that never becomes ready", () =>
748 Effect.gen(function* () {
749 const reports = yield* Queue.unbounded<DesktopBackendManager.BackendStartupFailure>();
750 const startInstance = (status: Ref.Ref<number>, onReady?: Effect.Effect<void>, stdout = "") =>
751 makeTestInstance({
752 spawnerLayer: Layer.succeed(
753 ChildProcessSpawner.ChildProcessSpawner,
754 ChildProcessSpawner.make(() =>
755 Effect.succeed(
756 makeProcess({
757 exitCode: Effect.never,
758 stdout: Stream.make(new TextEncoder().encode(stdout)),
759 }),
760 ),
761 ),
762 ),
763 httpClientLayer: layerHttpClient((request) =>
764 Ref.get(status).pipe(Effect.map((code) => responseForRequest(request, code))),
765 ),
766 onStartupFailure: (failure) => Queue.offer(reports, failure).pipe(Effect.asVoid),
767 ...(onReady ? { onReady } : {}),
768 }).pipe(Effect.tap((instance) => instance.start));
769
770 const slowStatus = yield* Ref.make(503);
771 const ready = yield* Deferred.make<void>();
772 yield* startInstance(slowStatus, Deferred.succeed(ready, undefined).pipe(Effect.asVoid));
773 yield* TestClock.adjust(Duration.seconds(66));
774 yield* Ref.set(slowStatus, 200);
775 yield* TestClock.adjust(Duration.millis(100));
776 yield* Deferred.await(ready);
777 assert.isTrue(Option.isNone(yield* Queue.poll(reports)));
778
779 const migratingStatus = yield* Ref.make(503);
780 const migrated = yield* Deferred.make<void>();
781 yield* startInstance(
782 migratingStatus,
783 Deferred.succeed(migrated, undefined).pipe(Effect.asVoid),
784 // Enough progress lines that the start line scrolls out of the output tail.
785 `INFO ${DATABASE_MIGRATIONS_STARTED_MESSAGE}\n` +
786 Array.from(
787 { length: 80 },
788 (_, index) =>
789 `INFO ${DATABASE_MIGRATION_PROGRESS_MESSAGE} migration=${index}_Migration progress="${index + 1} of 80"\n`,
790 ).join(""),
791 );
792 yield* TestClock.adjust(Duration.minutes(10));
793 assert.isTrue(Option.isNone(yield* Queue.poll(reports)));
794 yield* Ref.set(migratingStatus, 200);
795 yield* TestClock.adjust(Duration.millis(100));
796 yield* Deferred.await(migrated);
797
798 yield* startInstance(
799 yield* Ref.make(503),
800 undefined,
801 `${DATABASE_MIGRATIONS_STARTED_MESSAGE}\n${DATABASE_MIGRATIONS_FINISHED_MESSAGE}\n`,
802 );
803 yield* TestClock.adjust(Duration.seconds(179));
804 assert.isTrue(Option.isNone(yield* Queue.poll(reports)));
805 yield* TestClock.adjust(Duration.seconds(1));
806 const failure = yield* Queue.take(reports);
807 assert.equal(failure.reason, "The server started but did not become ready in time.");
808 }).pipe(Effect.provide(TestClock.layer())),
809 );
810
811 it.effect("the quit stop keeps waiting for a backend that outlasts its deadline", () =>
812 Effect.gen(function* () {
813 const spawned = yield* Deferred.make<void>();
814 const teardownStarted = yield* Deferred.make<void>();
815 const teardownFinished = yield* Deferred.make<void>();
816 const instance = yield* makeTestInstance({
817 spawnerLayer: Layer.succeed(
818 ChildProcessSpawner.ChildProcessSpawner,
819 ChildProcessSpawner.make(() =>
820 Effect.gen(function* () {
821 const scope = yield* Scope.Scope;
822 yield* Deferred.succeed(spawned, undefined);
823 yield* Scope.addFinalizer(
824 scope,
825 Deferred.succeed(teardownStarted, undefined).pipe(
826 Effect.andThen(Effect.sleep(Duration.seconds(10))),
827 Effect.andThen(Deferred.succeed(teardownFinished, undefined)),
828 Effect.asVoid,
829 ),
830 );
831 return makeProcess({
832 exitCode: Deferred.await(teardownFinished).pipe(
833 Effect.as(ChildProcessSpawner.ExitCode(0)),
834 ),
835 });
836 }),
837 ),
838 ),
839 httpClientLayer: layerHttpClient(() => Effect.never),
840 });
841 yield* instance.start;
842 yield* Deferred.await(spawned);
843
844 // App quit and the updater stop every backend with this deadline.
845 const quitDone = yield* Deferred.make<void>();
846 const quitFiber = yield* instance
847 .stop({ timeout: DesktopBackendManager.BACKEND_QUIT_STOP_TIMEOUT })
848 .pipe(Effect.andThen(Deferred.succeed(quitDone, undefined)), Effect.forkChild);
849
850 yield* Deferred.await(teardownStarted);
851 // Past the quit deadline, quit must still wait for the backend to exit.
852 yield* TestClock.adjust(Duration.seconds(9));
853 assert.isFalse(yield* Deferred.isDone(quitDone));
854
855 yield* TestClock.adjust(Duration.seconds(1));
856 yield* Fiber.join(quitFiber);
857 assert.isTrue(yield* Deferred.isDone(teardownFinished));
858 }).pipe(Effect.provide(TestClock.layer())),
859 );
860 });