Open History to see the last commit
1import type { DesktopUpdateRemoteOutcome, DesktopUpdateState } from "@t3tools/contracts";
2
3/**
4 * What a server-triggered update run should do next, given the updater's
5 * current state. "wait" means an action (possibly started locally) is in
6 * flight and the run should ride along until the next state change.
7 */
8export type RemoteDesktopUpdateStep =
9 | { readonly action: "check" }
10 | { readonly action: "download" }
11 | { readonly action: "install" }
12 | { readonly action: "wait" }
13 | {
14 readonly action: "done";
15 readonly outcome: DesktopUpdateRemoteOutcome;
16 readonly reason?: string;
17 };
18
19/**
20 * How many times this run already issued each action. The caps are what stop
21 * a check -> up-to-date -> check loop and endless download retries; they are
22 * counts rather than booleans because a state event raced by the local
23 * 4-minute poller can re-show an already-handled status once.
24 */
25export interface RemoteDesktopUpdateAttempts {
26 readonly checks: number;
27 readonly downloads: number;
28}
29
30export const MAX_REMOTE_UPDATE_CHECKS = 2;
31export const MAX_REMOTE_UPDATE_DOWNLOADS = 3;
32
33/** Same predicate DesktopUpdates.installDownloadedUpdate uses for admission. */
34function isInstallableDesktopUpdateState(state: DesktopUpdateState): boolean {
35 return (
36 state.downloadedVersion !== null &&
37 (state.status === "downloaded" ||
38 (state.status === "error" &&
39 (state.errorContext === null || state.errorContext === "install")))
40 );
41}
42
43export function nextRemoteDesktopUpdateStep(
44 state: DesktopUpdateState,
45 attempts: RemoteDesktopUpdateAttempts,
46 disabledReason: string | null,
47): RemoteDesktopUpdateStep {
48 if (!state.enabled || state.status === "disabled") {
49 return {
50 action: "done",
51 outcome: "failed",
52 reason: disabledReason ?? "Automatic updates are disabled on this machine.",
53 };
54 }
55 // Mirror installDownloadedUpdate's own admission rule exactly, so the run
56 // never reports an install that the updater then refuses. A download
57 // survives an unrelated background updater error (errorContext null) and
58 // a previous failed install (errorContext "install"); it does not survive
59 // a check or download error, which fall through to the error branch.
60 if (isInstallableDesktopUpdateState(state)) {
61 return { action: "install" };
62 }
63 if (state.status === "downloading" || state.status === "checking") {
64 return { action: "wait" };
65 }
66 if (state.status === "available") {
67 if (attempts.downloads >= MAX_REMOTE_UPDATE_DOWNLOADS) {
68 return {
69 action: "done",
70 outcome: "failed",
71 reason: state.message ?? "The desktop app failed to download the update.",
72 };
73 }
74 return { action: "download" };
75 }
76 // "up-to-date" and "error" are retained from earlier/background checks,
77 // so before this run has issued its own check they are stale, not
78 // terminal: the whole point of a remote request is to look again.
79 if (state.status === "up-to-date") {
80 if (attempts.checks === 0) {
81 return { action: "check" };
82 }
83 return { action: "done", outcome: "up-to-date" };
84 }
85 if (state.status === "error") {
86 if (attempts.checks === 0) {
87 return { action: "check" };
88 }
89 return {
90 action: "done",
91 outcome: "failed",
92 reason: state.message ?? "The desktop app update failed.",
93 };
94 }
95 // status === "idle"
96 if (attempts.checks >= MAX_REMOTE_UPDATE_CHECKS) {
97 return {
98 action: "done",
99 outcome: "failed",
100 reason: "The desktop app did not report an update result.",
101 };
102 }
103 return { action: "check" };
104}
105
106/**
107 * Normalizes an updater message for the `reason` wire field, which is a
108 * TrimmedNonEmptyString. Updater messages are plain strings and may be blank;
109 * a blank reason would fail encoding and silently drop the terminal report,
110 * leaving the server to wait for its timeout.
111 */
112export function normalizeRemoteUpdateReason(reason: string | undefined): string | undefined {
113 const trimmed = reason?.trim();
114 return trimmed ? trimmed : undefined;
115}