@devries-cameron20 / t3code Mirror · read-only / commits / a75ea468b1
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 23, 04:15 UTC

fix(server): keep a remote server update running when the client disconnects (#18980)

Co-authored-by: macodev00 <273427913+macodev00@users.noreply.github.com> Co-authored-by: maco <macodev00@users.noreply.github.com>

maco committed Oct 18, 2026 at 21:31 UTC a75ea468b13dff836cf686fb38fac9846cbdfddc
3 files changed +74 -26
apps/server/src/cloud/selfUpdate.test.ts
+47 -2
@@ -8,6 +8,7 @@ import * as Effect from "effect/Effect";
8 import * as FileSystem from "effect/FileSystem";
9 import * as Fiber from "effect/Fiber";
10 import * as Path from "effect/Path";
11 +import * as Stream from "effect/Stream";
12 import { HttpClient, HttpClientResponse } from "effect/http";
13 import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
14
@@ -22,12 +23,13 @@ interface HarnessOptions {
23 readonly mode?: "web" | "desktop";
24 readonly managed?: boolean;
25 readonly preflight?: "ready" | "blocked";
26 + readonly beforeDownload?: Effect.Effect<void>;
27 readonly requestUpdate?: ServiceLauncherClient.ServiceLauncherClient["Service"]["requestUpdate"];
28 readonly desktopAppUpdate?: DesktopAppUpdate.DesktopAppUpdate["Service"];
29 }
30
31 const archiveBytes = new TextEncoder().encode("not really a tarball");
30 -const releaseHttpClient = (order: string[]) =>
32 +const releaseHttpClient = (order: string[], beforeDownload?: Effect.Effect<void>) =>
33 HttpClient.make((request) =>
34 Effect.gen(function* () {
35 if (request.url.endsWith("/SHA256SUMS")) {
@@ -40,6 +42,7 @@ const releaseHttpClient = (order: string[]) =>
42 new Response(`${hex} t3-1.1.0-linux-x64.tar.gz\n`),
43 );
44 }
45 + if (beforeDownload) yield* beforeDownload;
46 order.push("download");
47 return HttpClientResponse.fromWeb(request, new Response(archiveBytes));
48 }),
@@ -116,7 +119,7 @@ const makeHarness = Effect.fn("test.make_self_update_harness")(function* (
119 run: () => Effect.die("unexpected desktop app update run"),
120 },
121 ),
119 - Effect.provideService(HttpClient.HttpClient, releaseHttpClient(order)),
122 + Effect.provideService(HttpClient.HttpClient, releaseHttpClient(order, options.beforeDownload)),
123 Effect.provideService(HostProcess.Platform, "linux"),
124 Effect.provideService(HostProcess.Architecture, "x64"),
125 Effect.provide(ServerConfig.layer({ ...config, mode: options.mode ?? "web" })),
@@ -418,4 +421,46 @@ it.layer(NodeServices.layer)("server self update", (it) => {
421 expect((yield* Fiber.join(first)).updateId).toBe("launcher-id");
422 }),
423 );
424 +
425 + it.effect("keeps preparing after the progress stream closes", () =>
426 + Effect.gen(function* () {
427 + const releaseDownload = yield* Deferred.make<void>();
428 + const sawDownloading = yield* Deferred.make<void>();
429 + const accepted = yield* Deferred.make<void>();
430 + const { selfUpdate, order } = yield* makeHarness({
431 + beforeDownload: Deferred.await(releaseDownload),
432 + requestUpdate: () =>
433 + Effect.sync(() => {
434 + order.push("accept");
435 + }).pipe(Effect.andThen(Deferred.succeed(accepted, undefined)), Effect.as("launcher-id")),
436 + });
437 +
438 + const consumed = yield* ServerSelfUpdate.updateProgressStream(selfUpdate, {
439 + targetVersion: "1.1.0",
440 + }).pipe(
441 + Stream.tap((event) =>
442 + event.type === "progress" && event.stage === "downloading"
443 + ? Deferred.succeed(sawDownloading, undefined)
444 + : Effect.void,
445 + ),
446 + Stream.runDrain,
447 + Effect.forkChild,
448 + );
449 +
450 + yield* Deferred.await(sawDownloading);
451 + // The socket drop ends the stream. The download is still held.
452 + yield* Fiber.interrupt(consumed);
453 + expect((yield* selfUpdate.update({ targetVersion: "1.2.0" }).pipe(Effect.flip)).reason).toBe(
454 + "A server update is already in progress.",
455 + );
456 +
457 + yield* Deferred.succeed(releaseDownload, undefined);
458 + // `installing` is offered only after the stream queue has shut down.
459 + yield* Deferred.await(accepted);
460 + expect(order).toEqual(["download", "extract", "preflight", "accept"]);
461 + expect((yield* selfUpdate.update({ targetVersion: "1.1.0" }).pipe(Effect.flip)).reason).toBe(
462 + "A server update is already in progress.",
463 + );
464 + }),
465 + );
466 });
apps/server/src/cloud/selfUpdate.ts
+26
@@ -2,6 +2,7 @@ import {
2 ServerSelfUpdateError,
3 type ServerSelfUpdateCapability,
4 type ServerSelfUpdateInput,
5 + type ServerSelfUpdateProgressEvent,
6 type ServerSelfUpdateProgressStage,
7 type ServerSelfUpdateResult,
8 type ThreadId,
@@ -18,6 +19,8 @@ import * as Ref from "effect/Ref";
19 import * as FileSystem from "effect/FileSystem";
20 import * as Layer from "effect/Layer";
21 import * as Path from "effect/Path";
22 +import * as Queue from "effect/Queue";
23 +import * as Stream from "effect/Stream";
24 import { HttpClient } from "effect/http";
25
26 import { CLI_RELEASE_BASE_URL_ENV } from "@t3tools/shared/cliRelease";
@@ -339,3 +342,26 @@ export const make = Effect.fn("cloud.server_self_update.make")(function* () {
342 export const layer = Layer.effect(ServerSelfUpdate, make()).pipe(
343 Layer.provide(ProcessRunner.layer),
344 );
345 +
346 +// The RPC stream used to own this fiber, so a dropped client socket interrupted
347 +// the download. Offers after the queue shuts down are ignored; the update
348 +// still finishes, and the in-flight guard stays as it was.
349 +export const updateProgressStream = (
350 + selfUpdate: ServerSelfUpdate["Service"],
351 + input: ServerSelfUpdateInput,
352 +) =>
353 + Stream.callback<ServerSelfUpdateProgressEvent, ServerSelfUpdateError>((queue) => {
354 + const offer = (event: ServerSelfUpdateProgressEvent) =>
355 + Queue.offer(queue, event).pipe(Effect.ignore);
356 +
357 + return selfUpdate
358 + .update(input, (stage) => offer({ type: "progress", stage }))
359 + .pipe(
360 + Effect.flatMap((result) => offer({ type: "complete", result })),
361 + Effect.catchTags({
362 + ServerSelfUpdateError: (error) => Queue.fail(queue, error).pipe(Effect.ignore),
363 + }),
364 + Effect.andThen(Queue.end(queue).pipe(Effect.ignore)),
365 + Effect.forkDetach,
366 + );
367 + });
apps/server/src/ws.ts
+1 -24
@@ -77,8 +77,6 @@ import {
77 ProviderSetupError,
78 RelayClientInstallFailedError,
79 type RelayClientInstallProgressEvent,
80 - type ServerSelfUpdateError,
81 - type ServerSelfUpdateProgressEvent,
80 type ServerConfig as ClientServerConfig,
81 type ServerConfigStreamEvent,
82 type ServerLifecycleStreamEvent,
@@ -2335,28 +2333,7 @@ const layerWsRpc = (
2333 [WS_METHODS.providerInstallRemove]: (input) => providerInstallation.remove(input),
2334 [WS_METHODS.serverUpdateServer]: (input) => serverSelfUpdate.update(input),
2335 [WS_METHODS.serverUpdateServerWithProgress]: (input) =>
2338 - Stream.callback<ServerSelfUpdateProgressEvent, ServerSelfUpdateError>((queue) =>
2339 - serverSelfUpdate
2340 - .update(input, (stage) =>
2341 - Queue.offer(queue, {
2342 - type: "progress",
2343 - stage,
2344 - }).pipe(Effect.asVoid),
2345 - )
2346 - .pipe(
2347 - Effect.flatMap((result) =>
2348 - Queue.offer(queue, {
2349 - type: "complete",
2350 - result,
2351 - }),
2352 - ),
2353 - Effect.catchTags({
2354 - ServerSelfUpdateError: (error) => Queue.fail(queue, error),
2355 - }),
2356 - Effect.andThen(Queue.end(queue)),
2357 - Effect.forkScoped,
2358 - ),
2359 - ),
2336 + ServerSelfUpdate.updateProgressStream(serverSelfUpdate, input),
2337 [WS_METHODS.serverCommitDesktopUpdate]: (input) =>
2338 serverSelfUpdate.commitDesktopUpdate(input.requestId),
2339 [WS_METHODS.serverUpsertKeybinding]: (rule) =>