Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

DesktopRemoteUpdates.ts

517 lines19.7 KB

1import type {
2 DesktopTelemetryRequestDesktopUpdate,
3 DesktopUpdateRemoteOutcome,
4 DesktopUpdateState,
5} from "@t3tools/contracts";
6import * as Duration from "effect/Duration";
7import * as Clock from "effect/Clock";
8import * as Deferred from "effect/Deferred";
9import * as Effect from "effect/Effect";
10import * as Option from "effect/Option";
11import * as Queue from "effect/Queue";
12import * as Ref from "effect/Ref";
13import { desktopUpdateChannelOf } from "@t3tools/shared/cliRelease";
14import { compareSemverVersions } from "@t3tools/shared/semver";
15import type * as Scope from "effect/Scope";
16import * as Stream from "effect/Stream";
17​
18import * as DesktopObservability from "../app/DesktopObservability.ts";
19import * as DesktopBackendPool from "../backend/DesktopBackendPool.ts";
20import * as DesktopTelemetryPublisher from "../telemetry/DesktopTelemetryPublisher.ts";
21import * as DesktopUpdates from "./DesktopUpdates.ts";
22import {
23 nextRemoteDesktopUpdateStep,
24 normalizeRemoteUpdateReason,
25 type RemoteDesktopUpdateAttempts,
26} from "./remoteUpdateFlow.ts";
27​
28const { logInfo, logError } = DesktopObservability.makeComponentLogger("desktop-remote-updates");
29​
30const ACTION_RETRY_DELAY = Duration.millis(250);
31const PREPARED_UPDATE_TTL = Duration.minutes(5);
32​
33interface PreparedUpdate {
34 readonly requestId: string;
35 readonly downloadedVersion: string;
36 readonly status: "prepared" | "committing" | "failed";
37 readonly failureReason?: string;
38 readonly expiresAt: number;
40​
41type CommitClaim =
42 | { readonly _tag: "invalid" }
43 | { readonly _tag: "failed"; readonly prepared: PreparedUpdate }
44 | { readonly _tag: "committing" }
45 | { readonly _tag: "claimed"; readonly prepared: PreparedUpdate };
46​
47export const listen: Effect.Effect<
48 void,
49 never,
50 | DesktopUpdates.DesktopUpdates
51 | DesktopTelemetryPublisher.DesktopTelemetryPublisher
52 | DesktopBackendPool.DesktopBackendPool
53 | Scope.Scope
54> = Effect.gen(function* () {
55 const updates = yield* DesktopUpdates.DesktopUpdates;
56 const backends = yield* DesktopBackendPool.DesktopBackendPool;
57 const publisher = yield* DesktopTelemetryPublisher.DesktopTelemetryPublisher;
58 const activeRequestIdRef = yield* Ref.make(Option.none<string>());
59 const requestControlRef = yield* Ref.make<{
60 readonly active: Option.Option<{
61 readonly requestId: string;
62 readonly signal: Deferred.Deferred<void>;
63 }>;
64 readonly cancelledBeforeStart: ReadonlyArray<string>;
65 }>({ active: Option.none(), cancelledBeforeStart: [] });
66 const preparedUpdateRef = yield* Ref.make(Option.none<PreparedUpdate>());
67​
68 const publishReport = (
69 state: DesktopUpdateState,
70 terminal?: { readonly outcome: DesktopUpdateRemoteOutcome; readonly reason?: string },
71 explicitRequestId?: string,
72 ): Effect.Effect<void> => {
73 const reason = normalizeRemoteUpdateReason(terminal?.reason);
74 return Ref.get(activeRequestIdRef).pipe(
75 Effect.flatMap((requestId) =>
76 publisher.publishUpdateReport({
77 version: 1,
78 type: "desktopUpdateStatus",
79 ...(explicitRequestId !== undefined
80 ? { requestId: explicitRequestId }
81 : Option.isSome(requestId)
82 ? { requestId: requestId.value }
83 : {}),
84 ...(terminal === undefined ? {} : { outcome: terminal.outcome }),
85 ...(reason ? { reason } : {}),
86 state,
87 }),
88 ),
89 );
90 };
91​
92 const recordPreparedFailure = (requestId: string, reason: string) =>
93 Ref.modify(preparedUpdateRef, (prepared) => {
94 if (
95 Option.isNone(prepared) ||
96 prepared.value.requestId !== requestId ||
97 prepared.value.status === "failed"
98 ) {
99 return [false, prepared] as const;
100 }
101 return [
102 true,
103 Option.some({ ...prepared.value, status: "failed" as const, failureReason: reason }),
104 ] as const;
105 });
106​
107 const clearActiveRequest = (requestId: string) =>
108 Ref.update(activeRequestIdRef, (active) =>
109 Option.isSome(active) && active.value === requestId ? Option.none() : active,
110 );
111​
112 yield* Effect.scoped(
113 Effect.gen(function* () {
114 const { latest, changes } = yield* updates.subscribe;
115 yield* publishReport(latest);
116 yield* Stream.runForEach(changes, (state) =>
117 Effect.gen(function* () {
118 yield* publishReport(state);
119 const prepared = yield* Ref.get(preparedUpdateRef);
120 if (
121 Option.isSome(prepared) &&
122 prepared.value.status === "committing" &&
123 state.errorContext === "install"
124 ) {
125 const reason = state.message ?? "The desktop app failed to install the update.";
126 if (yield* recordPreparedFailure(prepared.value.requestId, reason)) {
127 yield* publishReport(state, { outcome: "failed", reason }, prepared.value.requestId);
128 }
129 }
130 }),
131 );
132 }),
133 ).pipe(Effect.forkScoped);
134​
135 const handleRequest = (request: DesktopTelemetryRequestDesktopUpdate): Effect.Effect<void> =>
136 Effect.scoped(
137 Effect.gen(function* () {
138 const cancellation = yield* Deferred.make<void>();
139 const shouldStart = yield* Ref.modify(requestControlRef, (control) => {
140 if (control.cancelledBeforeStart.includes(request.requestId)) {
141 return [
142 false,
143 {
144 ...control,
145 cancelledBeforeStart: control.cancelledBeforeStart.filter(
146 (requestId) => requestId !== request.requestId,
147 ),
148 },
149 ] as const;
150 }
151 return [
152 true,
153 {
154 ...control,
155 active: Option.some({ requestId: request.requestId, signal: cancellation }),
156 },
157 ] as const;
158 });
159 if (!shouldStart) return;
160​
161 const now = yield* Clock.currentTimeMillis;
162 const prepared = yield* Ref.modify(preparedUpdateRef, (current) =>
163 Option.isSome(current) &&
164 current.value.status === "prepared" &&
165 current.value.expiresAt <= now
166 ? ([Option.none<typeof current.value>(), Option.none()] as const)
167 : ([current, current] as const),
168 );
169 if (Option.isSome(prepared) && prepared.value.status !== "failed") {
170 yield* publishReport(
171 yield* updates.getState,
172 {
173 outcome: "failed",
174 reason: "A prepared desktop update is already in progress.",
175 },
176 request.requestId,
177 );
178 return;
179 }
180 yield* Ref.set(activeRequestIdRef, Option.some(request.requestId));
181 yield* logInfo("remote update requested", {
182 requestId: request.requestId,
183 channel: request.channel,
184 });
185 // A switch saves its track first, so the step machine never sees the old track's offer.
186 // The saved track is the user's intent and stays even if this request fails: a track saved
187 // toward Stable never offers an older build outside a guarded switch.
188 if (request.channel !== undefined) {
189 // A reinstall restarts every backend, but the requesting server checked only its own
190 // database. A WSL backend keeps its own, so cross tracks only where all can be checked.
191 const current = yield* updates.getState;
192 if (
193 desktopUpdateChannelOf(current.currentVersion) !== request.channel &&
194 (yield* backends.list).length > 1
195 ) {
196 yield* publishReport(
197 current,
198 {
199 outcome: "failed",
200 reason:
201 "This desktop app also runs WSL backends, whose databases a remote switch cannot check. Switch it on that machine.",
202 },
203 request.requestId,
204 );
205 return;
206 }
207 const switched = yield* Effect.raceFirst(
208 updates.setChannel(request.channel, { minimumVersion: request.targetVersion }).pipe(
209 Effect.as(true),
210 Effect.catch((error) =>
211 updates.getState.pipe(
212 Effect.flatMap((state) =>
213 publishReport(
214 state,
215 { outcome: "failed", reason: error.message },
216 request.requestId,
217 ),
218 ),
219 Effect.as(false),
220 ),
221 ),
222 ),
223 Deferred.await(cancellation).pipe(Effect.as(false)),
224 );
225 if (!switched) return;
226 // A host already running the target only needed its track saved; installing a newer
227 // build here would restart it without the dialog having said so.
228 const state = yield* updates.getState;
229 if (request.targetVersion === state.currentVersion) {
230 yield* publishReport(state, { outcome: "up-to-date" }, request.requestId);
231 return;
232 }
233 }
234 const { latest, changes } = yield* updates.subscribe;
235 const disabledReason = Option.getOrNull(yield* updates.disabledReason);
236 let attempts: RemoteDesktopUpdateAttempts = { checks: 0, downloads: 0 };
237 // State events can precede reservation release; re-enqueue refused actions so they retry after release.
238 const retries = yield* Queue.unbounded<DesktopUpdateState>();
239 const retryLater = (state: DesktopUpdateState) =>
240 Effect.sleep(ACTION_RETRY_DELAY).pipe(
241 Effect.andThen(Queue.offer(retries, state)),
242 Effect.asVoid,
243 Effect.forkScoped,
244 );
245​
246 // A host leaving its build's track may need an older build, down to the checked release;
247 // the updater offers nothing below it.
248 const crossing = (state: DesktopUpdateState) =>
249 request.channel !== undefined &&
250 request.targetVersion !== undefined &&
251 desktopUpdateChannelOf(state.currentVersion) !== request.channel;
252 const step = Effect.fn("DesktopRemoteUpdates.step")(function* (
253 state: DesktopUpdateState,
254 ): Effect.fn.Return<boolean, never, Scope.Scope> {
255 const next = nextRemoteDesktopUpdateStep(state, attempts, disabledReason);
256 switch (next.action) {
257 case "wait":
258 return false;
259 case "check":
260 attempts = { ...attempts, checks: attempts.checks + 1 };
261 yield* updates.check("remote-update", { allowDowngrade: crossing(state) }).pipe(
262 Effect.flatMap((result) => {
263 if (result.checked) return Effect.void;
264 attempts = { ...attempts, checks: attempts.checks - 1 };
265 return retryLater(state);
266 }),
267 Effect.forkScoped,
268 );
269 return false;
270 case "download":
271 attempts = { ...attempts, downloads: attempts.downloads + 1 };
272 yield* updates.download.pipe(
273 Effect.flatMap((result) => {
274 if (result.accepted) return Effect.void;
275 attempts = { ...attempts, downloads: attempts.downloads - 1 };
276 return retryLater(state);
277 }),
278 Effect.forkScoped,
279 );
280 return false;
281 case "install": {
282 // The download event fires before its action releases the
283 // updater reservation. Wait until the prepared install can
284 // be committed by the client in a separate RPC.
285 if (yield* updates.isActionActive) {
286 yield* retryLater(state);
287 return false;
288 }
289 if (state.downloadedVersion === null) {
290 yield* publishReport(
291 state,
292 {
293 outcome: "failed",
294 reason: "The desktop app lost the downloaded update.",
295 },
296 request.requestId,
297 );
298 return true;
299 }
300 yield* Ref.set(
301 preparedUpdateRef,
302 Option.some({
303 requestId: request.requestId,
304 downloadedVersion: state.downloadedVersion,
305 status: "prepared",
306 expiresAt:
307 (yield* Clock.currentTimeMillis) + Duration.toMillis(PREPARED_UPDATE_TTL),
308 }),
309 );
310 yield* publishReport(state, { outcome: "ready-to-install" }, request.requestId);
311 yield* logInfo("remote update prepared", { requestId: request.requestId });
312 return true;
313 }
314 case "done": {
315 // A switch that leaves the host on its old track, or below its checked release, did
316 // not reach it; a track-only host already at the release is done.
317 const done =
318 request.targetVersion !== undefined &&
319 next.outcome === "up-to-date" &&
320 (crossing(state) ||
321 compareSemverVersions(state.currentVersion, request.targetVersion) < 0)
322 ? {
323 outcome: "failed" as const,
324 reason: `The desktop app found no build of ${request.targetVersion}.`,
325 }
326 : next;
327 yield* publishReport(
328 state,
329 {
330 outcome: done.outcome,
331 ...(done.reason === undefined ? {} : { reason: done.reason }),
332 },
333 request.requestId,
334 );
335 yield* logInfo("remote update finished", {
336 requestId: request.requestId,
337 outcome: done.outcome,
338 reason: done.reason ?? null,
339 });
340 return true;
341 }
342 }
343 });
344​
345 yield* Effect.raceFirst(
346 Effect.gen(function* () {
347 if (yield* step(latest)) return;
348 yield* Stream.merge(changes, Stream.fromQueue(retries)).pipe(
349 Stream.mapEffect(step),
350 Stream.takeUntil((done) => done),
351 Stream.runDrain,
352 );
353 }),
354 Deferred.await(cancellation),
355 );
356 }),
357 ).pipe(
358 Effect.ensuring(
359 Effect.all(
360 [
361 clearActiveRequest(request.requestId),
362 Ref.update(requestControlRef, (control) => ({
363 ...control,
364 active:
365 Option.isSome(control.active) &&
366 control.active.value.requestId === request.requestId
367 ? Option.none()
368 : control.active,
369 })),
370 ],
371 { discard: true },
372 ),
373 ),
374 Effect.catchCause((cause) =>
375 logError("remote update request failed unexpectedly", {
376 requestId: request.requestId,
377 cause: String(cause),
378 }),
379 ),
380 );
381​
382 yield* Stream.runForEach(publisher.updateRequests, handleRequest).pipe(Effect.forkScoped);
383​
384 yield* Stream.runForEach(publisher.updateCancellations, (cancellation) =>
385 Effect.gen(function* () {
386 const activeSignal = yield* Ref.modify(requestControlRef, (control) => {
387 if (
388 Option.isSome(control.active) &&
389 control.active.value.requestId === cancellation.requestId
390 ) {
391 return [Option.some(control.active.value.signal), control] as const;
392 }
393 return [
394 Option.none<Deferred.Deferred<void>>(),
395 {
396 ...control,
397 cancelledBeforeStart: [
398 ...control.cancelledBeforeStart.filter(
399 (requestId) => requestId !== cancellation.requestId,
400 ),
401 cancellation.requestId,
402 ],
403 },
404 ] as const;
405 });
406 if (Option.isSome(activeSignal)) {
407 yield* Deferred.succeed(activeSignal.value, undefined);
408 }
409 const matchedPrepared = yield* Ref.modify(preparedUpdateRef, (prepared) => {
410 if (Option.isNone(prepared) || prepared.value.requestId !== cancellation.requestId) {
411 return [false, prepared] as const;
412 }
413 return [true, prepared.value.status === "prepared" ? Option.none() : prepared] as const;
414 });
415 if (matchedPrepared) {
416 yield* Ref.update(requestControlRef, (control) => {
417 return {
418 ...control,
419 cancelledBeforeStart: control.cancelledBeforeStart.filter(
420 (requestId) => requestId !== cancellation.requestId,
421 ),
422 };
423 });
424 }
425 }),
426 ).pipe(Effect.forkScoped);
427​
428 yield* Stream.runForEach(publisher.updateCommits, (commit) =>
429 Effect.gen(function* () {
430 const current = yield* updates.getState;
431 const now = yield* Clock.currentTimeMillis;
432 const claim = yield* Ref.modify(
433 preparedUpdateRef,
434 (prepared): readonly [CommitClaim, Option.Option<PreparedUpdate>] => {
435 if (
436 Option.isNone(prepared) ||
437 prepared.value.requestId !== commit.requestId ||
438 (prepared.value.status === "prepared" && prepared.value.expiresAt <= now)
439 ) {
440 return [{ _tag: "invalid" as const }, prepared] as const;
441 }
442 if (prepared.value.status === "failed") {
443 return [{ _tag: "failed" as const, prepared: prepared.value }, prepared] as const;
444 }
445 if (prepared.value.status === "committing") {
446 return [{ _tag: "committing" as const }, prepared] as const;
447 }
448 return [
449 { _tag: "claimed" as const, prepared: prepared.value },
450 Option.some({ ...prepared.value, status: "committing" as const }),
451 ] as const;
452 },
453 );
454 if (claim._tag === "invalid") {
455 yield* publishReport(
456 yield* updates.getState,
457 {
458 outcome: "failed",
459 reason: "This desktop update is no longer prepared.",
460 },
461 commit.requestId,
462 );
463 return;
464 }
465 if (claim._tag === "failed") {
466 yield* publishReport(
467 current,
468 {
469 outcome: "failed",
470 reason: claim.prepared.failureReason ?? "The desktop app failed to install the update.",
471 },
472 commit.requestId,
473 );
474 return;
475 }
476 if (claim._tag === "committing") {
477 return;
478 }
479 yield* Ref.set(activeRequestIdRef, Option.some(commit.requestId));
480 if (current.downloadedVersion !== claim.prepared.downloadedVersion) {
481 const reason = "This desktop update is no longer prepared.";
482 if (yield* recordPreparedFailure(commit.requestId, reason)) {
483 yield* publishReport(current, { outcome: "failed", reason }, commit.requestId);
484 }
485 return;
486 }
487 const result = yield* updates.installPrepared(claim.prepared.downloadedVersion);
488 if (
489 !result.accepted &&
490 result.state.downloadedVersion === claim.prepared.downloadedVersion &&
491 (yield* updates.isInstallActive)
492 ) {
493 // Join the existing install; reconnect and target-version checks prove its handoff.
494 yield* logInfo("remote update commit joining an in-progress install", {
495 requestId: commit.requestId,
496 });
497 return;
498 }
499 if (!result.accepted || result.failed) {
500 const reason = result.state.message ?? "The desktop app could not start the install.";
501 if (yield* recordPreparedFailure(commit.requestId, reason)) {
502 yield* publishReport(result.state, { outcome: "failed", reason }, commit.requestId);
503 }
504 return;
505 }
506 // Installation kills this backend; only transport loss followed by the target version proves handoff.
507 }).pipe(
508 Effect.ensuring(clearActiveRequest(commit.requestId)),
509 Effect.catchCause((cause) =>
510 logError("remote update commit failed unexpectedly", {
511 requestId: commit.requestId,
512 cause: String(cause),
513 }),
514 ),
515 ),
516 ).pipe(Effect.forkScoped);
517});