Skip to content
This repository was archived by the owner on Oct 10, 2026. It is now read-only.

Commit 197d348

Browse files
authored
fix(web): show server update progress through reconnect (pingdotgg#4903)
1 parent 5c9358a commit 197d348

21 files changed

Lines changed: 1072 additions & 364 deletions

File tree

‎apps/server/src/auth/RpcAuthorization.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export const RPC_REQUIRED_SCOPES = {
3232
[WS_METHODS.serverRefreshProviders]: AuthOrchestrationOperateScope,
3333
[WS_METHODS.serverUpdateProvider]: AuthOrchestrationOperateScope,
3434
[WS_METHODS.serverUpdateServer]: AuthOrchestrationOperateScope,
35+
[WS_METHODS.serverUpdateServerWithProgress]: AuthOrchestrationOperateScope,
3536
[WS_METHODS.serverUpsertKeybinding]: AuthOrchestrationOperateScope,
3637
[WS_METHODS.serverRemoveKeybinding]: AuthOrchestrationOperateScope,
3738
[WS_METHODS.serverGetSettings]: AuthOrchestrationReadScope,

‎apps/server/src/cloud/selfUpdate.test.ts‎

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,33 @@ import * as SelfUpdate from "./selfUpdate.ts";
2626

2727
const NODE_PATH = "/usr/local/bin/node";
2828

29+
const eventuallyFileString = Effect.fn("test.eventuallyFileString")(function* (
30+
filePath: string,
31+
expected: string,
32+
) {
33+
const fs = yield* FileSystem.FileSystem;
34+
for (let iteration = 0; iteration < 1_000; iteration += 1) {
35+
const contents = yield* fs.readFileString(filePath);
36+
if (contents === expected) {
37+
return;
38+
}
39+
// The rollback performs real filesystem I/O on a detached fiber, which
40+
// advancing TestClock does not await.
41+
yield* Effect.yieldNow;
42+
}
43+
return yield* Effect.die(new Error(`Expected file contents were not observed at ${filePath}.`));
44+
});
45+
46+
const eventuallyTrue = Effect.fn("test.eventuallyTrue")(function* (predicate: () => boolean) {
47+
for (let iteration = 0; iteration < 1_000; iteration += 1) {
48+
if (predicate()) {
49+
return;
50+
}
51+
yield* Effect.yieldNow;
52+
}
53+
return yield* Effect.die(new Error("Expected condition was not observed."));
54+
});
55+
2956
interface RecordedCommand {
3057
readonly command: string;
3158
readonly args: ReadonlyArray<string>;
@@ -457,8 +484,12 @@ it.layer(NodeServices.layer)("ServerSelfUpdate.update", (it) => {
457484
it.effect("installs, preflights, and respawns a foreground server", () =>
458485
Effect.gen(function* () {
459486
const context = yield* makeContext();
460-
const result = yield* context.service.update({ targetVersion: "0.0.29" });
487+
const progress: Array<string> = [];
488+
const result = yield* context.service.update({ targetVersion: "0.0.29" }, (stage) =>
489+
Effect.sync(() => progress.push(stage)),
490+
);
461491
assert.deepEqual(result, { targetVersion: "0.0.29", method: "respawn" });
492+
assert.deepEqual(progress, ["downloading", "installing"]);
462493
assert.lengthOf(context.spawns, 1);
463494

464495
const concurrentError = yield* context.service
@@ -506,10 +537,12 @@ it.layer(NodeServices.layer)("ServerSelfUpdate.update", (it) => {
506537
assert.include(unit, `ExecStart=${NODE_PATH} ${pinnedEntry} serve`);
507538
assert.deepEqual(
508539
context.commands.map((entry) => entry.command),
509-
["npm", NODE_PATH, "systemctl", "systemctl"],
540+
["npm", NODE_PATH, "systemctl"],
510541
);
511542
assert.deepEqual(context.commands[2]?.args, ["--user", "daemon-reload"]);
512543

544+
// Restart waits until after the update acknowledgement can flush.
545+
yield* TestClock.adjust(Duration.seconds(10));
513546
assert.deepEqual(context.commands[3], {
514547
command: "systemctl",
515548
args: ["--user", "restart", "t3code.service"],
@@ -542,9 +575,11 @@ it.layer(NodeServices.layer)("ServerSelfUpdate.update", (it) => {
542575
);
543576
const previousUnit = yield* context.fs.readFileString(unitPath);
544577

545-
const first = yield* context.service.update({ targetVersion: "0.0.29" }).pipe(Effect.flip);
546-
assert.include(first.reason, "Restarting the systemd boot service failed");
547-
assert.equal(yield* context.fs.readFileString(unitPath), previousUnit);
578+
const first = yield* context.service.update({ targetVersion: "0.0.29" });
579+
assert.deepEqual(first, { targetVersion: "0.0.29", method: "boot-service" });
580+
yield* TestClock.adjust(Duration.seconds(10));
581+
yield* eventuallyFileString(unitPath, previousUnit);
582+
yield* eventuallyTrue(() => context.commands.at(-1)?.args[1] === "daemon-reload");
548583
assert.deepEqual(
549584
context.commands.slice(-2).map((entry) => entry.args),
550585
[

‎apps/server/src/cloud/selfUpdate.ts‎

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
ServerSelfUpdateError,
77
type ServerSelfUpdateCapability,
88
type ServerSelfUpdateInput,
9+
type ServerSelfUpdateProgressStage,
910
type ServerSelfUpdateResult,
1011
} from "@t3tools/contracts";
1112
import {
@@ -147,6 +148,7 @@ export class ServerSelfUpdate extends Context.Service<
147148
{
148149
readonly update: (
149150
input: ServerSelfUpdateInput,
151+
reportProgress?: (stage: ServerSelfUpdateProgressStage) => Effect.Effect<void, never, never>,
150152
) => Effect.Effect<ServerSelfUpdateResult, ServerSelfUpdateError>;
151153
}
152154
>()("t3/cloud/selfUpdate/ServerSelfUpdate") {}
@@ -227,7 +229,7 @@ export const make = Effect.fn("cloud.server_self_update.make")(function* (option
227229

228230
const update: ServerSelfUpdate["Service"]["update"] = Effect.fn(
229231
"cloud.server_self_update.update",
230-
)(function* (input) {
232+
)(function* (input, reportProgress = () => Effect.void) {
231233
if (capability === "desktop-managed") {
232234
return yield* failWith(
233235
"This server is managed by the T3 Code desktop app on its machine; update the desktop app to update it.",
@@ -250,6 +252,7 @@ export const make = Effect.fn("cloud.server_self_update.make")(function* (option
250252
}
251253

252254
return yield* Effect.gen(function* () {
255+
yield* reportProgress("downloading");
253256
const runtimePaths = yield* ensurePinnedRuntimeInstalled({
254257
baseDir: serverConfig.baseDir,
255258
version: targetVersion,
@@ -260,6 +263,7 @@ export const make = Effect.fn("cloud.server_self_update.make")(function* (option
260263
Effect.mapError((error) => failWith("Could not install the requested t3 version.", error)),
261264
);
262265

266+
yield* reportProgress("installing");
263267
// A broken artifact (failed native build, incompatible node) must be
264268
// caught while the current server is still alive to report it.
265269
const preflight = yield* runner
@@ -349,38 +353,45 @@ export const make = Effect.fn("cloud.server_self_update.make")(function* (option
349353
yield* Effect.logInfo("Server self-update installed; restarting boot service.", {
350354
targetVersion,
351355
});
352-
// A successful systemd restart stops this process, so the RPC is
353-
// interrupted and the reconnecting client observes the new version.
354-
// A rejected restart returns while the old process is still alive;
355-
// restore the previous unit and report that failure through the RPC.
356-
yield* Effect.gen(function* () {
357-
const restart = yield* runner
358-
.run({
359-
command: "systemctl",
360-
args: ["--user", "restart", BOOT_SERVICE_UNIT_FILE],
361-
})
362-
.pipe(
363-
Effect.mapError((cause) =>
364-
failWith("Could not restart the systemd boot service.", cause),
356+
// Restart after the acknowledgement has had time to cross any relay
357+
// hop. If systemd rejects the handoff, restore the previous unit while
358+
// this process is still alive and log the failure for diagnostics.
359+
yield* scheduleRestart(
360+
Effect.gen(function* () {
361+
const restart = yield* runner
362+
.run({
363+
command: "systemctl",
364+
args: ["--user", "restart", BOOT_SERVICE_UNIT_FILE],
365+
})
366+
.pipe(
367+
Effect.mapError((cause) =>
368+
failWith("Could not restart the systemd boot service.", cause),
369+
),
370+
);
371+
if (restart.code !== 0) {
372+
return yield* failWith(
373+
`Restarting the systemd boot service failed (exit code ${String(restart.code)}).`,
374+
);
375+
}
376+
}).pipe(
377+
Effect.catch((restartError) =>
378+
writeUnitAtomically(unitPath, previousUnit).pipe(
379+
Effect.andThen(reloadSystemd()),
380+
Effect.mapError((rollbackError) =>
381+
failWith("Could not restore the previous systemd unit.", {
382+
restartError,
383+
rollbackError,
384+
}),
385+
),
386+
Effect.andThen(Effect.fail(restartError)),
365387
),
366-
);
367-
if (restart.code !== 0) {
368-
return yield* failWith(
369-
`Restarting the systemd boot service failed (exit code ${String(restart.code)}).`,
370-
);
371-
}
372-
}).pipe(
373-
Effect.catch((restartError) =>
374-
writeUnitAtomically(unitPath, previousUnit).pipe(
375-
Effect.andThen(reloadSystemd()),
376-
Effect.mapError((rollbackError) =>
377-
failWith("Could not restore the previous systemd unit.", {
378-
restartError,
379-
rollbackError,
380-
}),
388+
),
389+
Effect.catch((error) =>
390+
Effect.logError("Server self-update could not restart the boot service.").pipe(
391+
Effect.annotateLogs({ targetVersion, error: error.reason }),
381392
),
382-
Effect.andThen(Effect.fail(restartError)),
383393
),
394+
Effect.ensuring(Ref.set(inFlight, false)),
384395
),
385396
);
386397
} else {

‎apps/server/src/environment/ServerEnvironment.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ export const make = Effect.gen(function* () {
144144
threadSnooze: true,
145145
threadTitleRegeneration: true,
146146
...(serverSelfUpdate === null ? {} : { serverSelfUpdate }),
147+
...(serverSelfUpdate === "boot-service" || serverSelfUpdate === "respawn"
148+
? { serverSelfUpdateProgress: true }
149+
: {}),
147150
},
148151
};
149152

‎apps/server/src/ws.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ import {
4040
ProjectWriteFileError,
4141
RelayClientInstallFailedError,
4242
type RelayClientInstallProgressEvent,
43+
type ServerSelfUpdateError,
44+
type ServerSelfUpdateProgressEvent,
4345
type FilesystemBrowseFailure,
4446
FilesystemBrowseError,
4547
AssetWorkspaceContextNotFoundError,
@@ -1364,6 +1366,33 @@ const makeWsRpcLayer = (
13641366
observeRpcEffect(WS_METHODS.serverUpdateServer, serverSelfUpdate.update(input), {
13651367
"rpc.aggregate": "server",
13661368
}),
1369+
[WS_METHODS.serverUpdateServerWithProgress]: (input) =>
1370+
observeRpcStream(
1371+
WS_METHODS.serverUpdateServerWithProgress,
1372+
Stream.callback<ServerSelfUpdateProgressEvent, ServerSelfUpdateError>((queue) =>
1373+
serverSelfUpdate
1374+
.update(input, (stage) =>
1375+
Queue.offer(queue, {
1376+
type: "progress",
1377+
stage,
1378+
}).pipe(Effect.asVoid),
1379+
)
1380+
.pipe(
1381+
Effect.flatMap((result) =>
1382+
Queue.offer(queue, {
1383+
type: "complete",
1384+
result,
1385+
}),
1386+
),
1387+
Effect.catchTags({
1388+
ServerSelfUpdateError: (error) => Queue.fail(queue, error),
1389+
}),
1390+
Effect.andThen(Queue.end(queue)),
1391+
Effect.forkScoped,
1392+
),
1393+
),
1394+
{ "rpc.aggregate": "server" },
1395+
),
13671396
[WS_METHODS.serverUpsertKeybinding]: (rule) =>
13681397
observeRpcEffect(
13691398
WS_METHODS.serverUpsertKeybinding,

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 48 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ import {
296296
AlertDialogTitle,
297297
} from "./ui/alert-dialog";
298298
import { Tooltip, TooltipPopup, TooltipTrigger } from "./ui/tooltip";
299-
import { ServerUpdateAction } from "./ServerUpdateAction";
299+
import { ServerUpdateAction, ServerUpdateProgress } from "./ServerUpdateAction";
300300
import {
301301
buildVersionMismatchDismissalKey,
302302
dismissVersionMismatch,
@@ -1876,12 +1876,16 @@ function ChatViewContent(props: ChatViewProps) {
18761876
hasMultipleRegisteredEnvironments && activeThread
18771877
? `${environmentById.get(activeThread.environmentId)?.label ?? serverConfig?.environment.label ?? activeThread.environmentId} server`
18781878
: "server";
1879-
const versionMismatchEnvironmentId =
1880-
versionMismatch && activeThread ? activeThread.environmentId : null;
1879+
const serverUpdateEnvironmentId = activeThread?.environmentId ?? null;
18811880
const versionMismatchSelfUpdate = resolveServerSelfUpdateCapability(serverConfig);
1881+
const serverUpdateState = useAtomValue(
1882+
serverEnvironment.updateStateAtom(serverUpdateEnvironmentId),
1883+
);
18821884
const systemComposerBannerItems = useMemo<ComposerBannerStackItem[]>(() => {
18831885
const items: ComposerBannerStackItem[] = [];
1884-
if (activeEnvironmentUnavailableState) {
1886+
const resumingServerUpdate =
1887+
serverUpdateState.status === "running" && serverUpdateState.stage === "resuming";
1888+
if (activeEnvironmentUnavailableState && !resumingServerUpdate) {
18851889
const connection = activeEnvironmentUnavailableState.connection;
18861890
const isReconnecting =
18871891
connection.phase === "connecting" || connection.phase === "reconnecting";
@@ -1918,39 +1922,57 @@ function ChatViewContent(props: ChatViewProps) {
19181922
});
19191923
}
19201924
if (
1921-
showVersionMismatchBanner &&
1922-
versionMismatch &&
1923-
versionMismatchDismissKey &&
1924-
versionMismatchEnvironmentId
1925+
serverUpdateEnvironmentId &&
1926+
(serverUpdateState.status !== "idle" ||
1927+
(showVersionMismatchBanner && versionMismatch && versionMismatchDismissKey))
19251928
) {
1929+
const updateInProgress = serverUpdateState.status === "running";
1930+
const updateFailed = serverUpdateState.status === "failed";
19261931
items.push({
1927-
id: `version-mismatch:${versionMismatchDismissKey}`,
1928-
variant: "warning",
1932+
id: `server-version:${serverUpdateEnvironmentId}`,
1933+
variant: updateFailed ? "error" : "warning",
19291934
icon: <TriangleAlertIcon />,
1930-
title: "Client and server versions differ",
1931-
description: (
1932-
<>
1933-
Client {versionMismatch.clientVersion} is connected to {versionMismatchServerLabel}{" "}
1934-
{versionMismatch.serverVersion}.{" "}
1935-
{serverUpdateGuidance(versionMismatchSelfUpdate, versionMismatchServerLabel)}
1936-
</>
1937-
),
1935+
title:
1936+
updateInProgress || updateFailed
1937+
? `${updateFailed ? "Could not update" : "Updating"} ${versionMismatchServerLabel}`
1938+
: "Client and server versions differ",
1939+
description:
1940+
updateInProgress || updateFailed ? (
1941+
<ServerUpdateProgress
1942+
fromVersion={serverUpdateState.fromVersion}
1943+
serverLabel={versionMismatchServerLabel}
1944+
state={serverUpdateState}
1945+
/>
1946+
) : versionMismatch ? (
1947+
<>
1948+
Client {versionMismatch.clientVersion} is connected to {versionMismatchServerLabel}{" "}
1949+
{versionMismatch.serverVersion}.{" "}
1950+
{serverUpdateGuidance(versionMismatchSelfUpdate, versionMismatchServerLabel)}
1951+
</>
1952+
) : null,
19381953
// The desktop-managed guidance is already the description; the action
19391954
// slot would only repeat it.
19401955
actions:
1956+
updateInProgress ||
1957+
!versionMismatch ||
19411958
versionMismatchSelfUpdate === "desktop-managed" ? undefined : (
19421959
<ServerUpdateAction
1943-
environmentId={versionMismatchEnvironmentId}
1960+
environmentId={serverUpdateEnvironmentId}
19441961
serverLabel={versionMismatchServerLabel}
19451962
selfUpdate={versionMismatchSelfUpdate}
19461963
targetVersion={versionMismatch.clientVersion}
1964+
{...(updateFailed ? { label: "Retry update" } : {})}
19471965
/>
19481966
),
1949-
dismissLabel: "Dismiss version mismatch warning",
1950-
onDismiss: () => {
1951-
dismissVersionMismatch(versionMismatchDismissKey);
1952-
setDismissedVersionMismatchKey(versionMismatchDismissKey);
1953-
},
1967+
...(updateInProgress || updateFailed || !versionMismatchDismissKey
1968+
? {}
1969+
: {
1970+
dismissLabel: "Dismiss version mismatch warning",
1971+
onDismiss: () => {
1972+
dismissVersionMismatch(versionMismatchDismissKey);
1973+
setDismissedVersionMismatchKey(versionMismatchDismissKey);
1974+
},
1975+
}),
19541976
});
19551977
}
19561978
return items;
@@ -1960,9 +1982,10 @@ function ChatViewContent(props: ChatViewProps) {
19601982
navigate,
19611983
setDismissedVersionMismatchKey,
19621984
showVersionMismatchBanner,
1985+
serverUpdateState,
19631986
versionMismatch,
19641987
versionMismatchDismissKey,
1965-
versionMismatchEnvironmentId,
1988+
serverUpdateEnvironmentId,
19661989
versionMismatchSelfUpdate,
19671990
versionMismatchServerLabel,
19681991
]);

0 commit comments

Comments
 (0)