Skip to content

Commit 1d3a4ee

Browse files
authored
Merge pull request pingdotgg#192 from Yarden-zamir/feat/desktop-update-newer-check
feat(desktop): check for a newer update before install
2 parents ac76147 + fb84325 commit 1d3a4ee

22 files changed

Lines changed: 360 additions & 4 deletions

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ This is a personal fork of [pingdotgg/t3code](https://github.com/pingdotgg/t3cod
3838
- **Fork installers:** one command installs the fork from the fork releases on macOS, Windows or Linux and joins the machine map. The installer has no source build.
3939
- **Install when idle:** while agents run, the update offers "Install when idle" next to "Install now".
4040
- **Server auto-update:** a headless server that runs as the background service installs each newer fork release when no agent turn runs (`forkAutoUpdate`).
41+
- **Newer update before install:** before the desktop app installs a downloaded update, it checks for a newer release. If one exists, it asks "Download newer" or "Install this one". Install when idle and the machine map take the newer release (`checkNewerBeforeUpdateInstall`, off by default).
4142
- **Local deploy:** `scripts/fork-deploy-local.sh <ssh-host>` builds this checkout for a headless Linux server and switches its service to it.
4243
- **Changelog:** a timeline of fork releases and commits, also in the update popover for the offered update, the pending upstream commits, a "Start Fork Sync" button and an "Ask agent" menu (`forkChangelogEnabled`, on by default).
4344
- **Fork Sync:** a daily workflow merges upstream main into fork main. It never rewrites main.

‎apps/desktop/src/ipc/DesktopIpcHandlers.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import {
3939
installUpdate,
4040
setUpdateChannel,
4141
} from "./methods/updates.ts";
42+
import { checkBeforeUpdateInstall } from "./methods/forkUpdates.ts";
4243
import {
4344
getAppBranding,
4445
getLocalEnvironmentBootstraps,
@@ -160,6 +161,7 @@ export const installDesktopIpcHandlers = Effect.fn("desktop.ipc.installHandlers"
160161
yield* ipc.handle(downloadUpdate);
161162
yield* ipc.handle(installUpdate);
162163
yield* ipc.handle(checkForUpdate);
164+
yield* ipc.handle(checkBeforeUpdateInstall);
163165
yield* ipc.handle(getCliCommandState);
164166
yield* ipc.handle(installCliCommand);
165167
yield* ipc.handle(uninstallCliCommand);

‎apps/desktop/src/ipc/channels.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export const UPDATE_SET_CHANNEL_CHANNEL = "desktop:update-set-channel";
2525
export const UPDATE_DOWNLOAD_CHANNEL = "desktop:update-download";
2626
export const UPDATE_INSTALL_CHANNEL = "desktop:update-install";
2727
export const UPDATE_CHECK_CHANNEL = "desktop:update-check";
28+
export const UPDATE_CHECK_BEFORE_INSTALL_CHANNEL = "desktop:update-check-before-install";
2829
export const CLI_COMMAND_GET_STATE_CHANNEL = "desktop:cli-command-get-state";
2930
export const CLI_COMMAND_INSTALL_CHANNEL = "desktop:cli-command-install";
3031
export const CLI_COMMAND_UNINSTALL_CHANNEL = "desktop:cli-command-uninstall";
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { DesktopUpdateInstallCheckSchema } from "@t3tools/contracts";
2+
import * as Effect from "effect/Effect";
3+
import * as Schema from "effect/Schema";
4+
5+
import * as DesktopUpdates from "../../updates/DesktopUpdates.ts";
6+
import * as IpcChannels from "../channels.ts";
7+
import * as DesktopIpc from "../DesktopIpc.ts";
8+
9+
/** Fork: see apps/desktop/src/updates/forkUpdateInstall.ts. */
10+
export const checkBeforeUpdateInstall = DesktopIpc.makeIpcMethod({
11+
channel: IpcChannels.UPDATE_CHECK_BEFORE_INSTALL_CHANNEL,
12+
payload: Schema.Void,
13+
result: DesktopUpdateInstallCheckSchema,
14+
handler: Effect.fn("desktop.ipc.updates.checkBeforeInstall")(function* () {
15+
const updates = yield* DesktopUpdates.DesktopUpdates;
16+
return yield* updates.checkBeforeInstall;
17+
}),
18+
});

‎apps/desktop/src/preload.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,8 @@ contextBridge.exposeInMainWorld("desktopBridge", {
270270
checkForUpdate: () => ipcRenderer.invoke(IpcChannels.UPDATE_CHECK_CHANNEL),
271271
downloadUpdate: () => ipcRenderer.invoke(IpcChannels.UPDATE_DOWNLOAD_CHANNEL),
272272
installUpdate: () => ipcRenderer.invoke(IpcChannels.UPDATE_INSTALL_CHANNEL),
273+
checkBeforeUpdateInstall: () =>
274+
ipcRenderer.invoke(IpcChannels.UPDATE_CHECK_BEFORE_INSTALL_CHANNEL),
273275
cliCommand: {
274276
getState: () => ipcRenderer.invoke(IpcChannels.CLI_COMMAND_GET_STATE_CHANNEL),
275277
install: () => ipcRenderer.invoke(IpcChannels.CLI_COMMAND_INSTALL_CHANNEL),

‎apps/desktop/src/updates/DesktopUpdates.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
type DesktopUpdateActionResult,
66
type DesktopUpdateChannel,
77
type DesktopUpdateCheckResult,
8+
type DesktopUpdateInstallCheck,
89
type DesktopUpdateState,
910
} from "@t3tools/contracts";
1011
import { errorTag } from "@t3tools/shared/observability";
@@ -34,6 +35,7 @@ import * as ElectronWindow from "../electron/ElectronWindow.ts";
3435
import * as IpcChannels from "../ipc/channels.ts";
3536
import * as DesktopAppSettings from "../settings/DesktopAppSettings.ts";
3637
import { normalizeDesktopUpdateReleaseNotes } from "./releaseNotes.ts";
38+
import * as ForkUpdateInstall from "./forkUpdateInstall.ts";
3739
import { resolveDefaultDesktopUpdateChannel } from "./updateChannels.ts";
3840
import {
3941
createInitialDesktopUpdateState,
@@ -193,6 +195,8 @@ export class DesktopUpdates extends Context.Service<
193195
readonly installPrepared: (
194196
expectedVersion: string,
195197
) => Effect.Effect<DesktopPreparedUpdateInstallResult>;
198+
// Fork: see forkUpdateInstall.ts.
199+
readonly checkBeforeInstall: Effect.Effect<DesktopUpdateInstallCheck>;
196200
}
197201
>()("@t3tools/desktop/updates/DesktopUpdates") {}
198202

@@ -1170,6 +1174,13 @@ export const make = Effect.gen(function* () {
11701174
Effect.map(({ accepted, completed, state }) => ({ accepted, completed, state })),
11711175
),
11721176
installPrepared: (expectedVersion) => installWithExpectedVersion(expectedVersion),
1177+
checkBeforeInstall: ForkUpdateInstall.makeCheckBeforeInstall({
1178+
getState: Ref.get(updateStateRef),
1179+
updateState,
1180+
check: Effect.suspend(() => checkForUpdates("install")),
1181+
stateChanges,
1182+
checkWait: PREPARED_INSTALL_CHECK_WAIT,
1183+
}),
11731184
});
11741185
});
11751186

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { describe, expect, it } from "vite-plus/test";
2+
3+
import { resolveNewerUpdateVersion } from "./forkUpdateInstall.ts";
4+
import {
5+
createInitialDesktopUpdateState,
6+
reduceDesktopUpdateStateOnUpdateAvailable,
7+
} from "./updateMachine.ts";
8+
9+
const initial = createInitialDesktopUpdateState(
10+
"0.0.46-nightly.20261015.170000",
11+
{ hostArch: "arm64", appArch: "arm64", runningUnderArm64Translation: false },
12+
"nightly",
13+
);
14+
const downloaded = "0.0.46-nightly.20261016.90000";
15+
const checkFound = (version: string) =>
16+
reduceDesktopUpdateStateOnUpdateAvailable(initial, version, "2026-10-16T10:00:00.000Z");
17+
18+
describe("resolveNewerUpdateVersion", () => {
19+
it("asks only for a release newer than the download", () => {
20+
// Build numbers compare as numbers: 180000 is newer than 90000.
21+
expect(
22+
resolveNewerUpdateVersion(downloaded, checkFound("0.0.46-nightly.20261016.180000")),
23+
).toBe("0.0.46-nightly.20261016.180000");
24+
expect(resolveNewerUpdateVersion(downloaded, checkFound(downloaded))).toBeNull();
25+
expect(
26+
resolveNewerUpdateVersion(downloaded, checkFound("0.0.46-nightly.20261015.990000")),
27+
).toBeNull();
28+
});
29+
});
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import type { DesktopUpdateInstallCheck, DesktopUpdateState } from "@t3tools/contracts";
2+
import { compareSemverVersions } from "@t3tools/shared/semver";
3+
import * as Clock from "effect/Clock";
4+
import * as Duration from "effect/Duration";
5+
import * as Effect from "effect/Effect";
6+
import * as PubSub from "effect/PubSub";
7+
8+
import { reduceDesktopUpdateStateOnDownloadComplete } from "./updateMachine.ts";
9+
10+
/** A check that finished this recently is fresh enough for an install. */
11+
const RECENT_CHECK_AGE = Duration.minutes(1);
12+
13+
/**
14+
* Fork: the version that replaces the downloaded one when a check finds a newer release, or
15+
* null. The fork releases on every main merge, so a download can be old by the time of the click.
16+
*/
17+
export function resolveNewerUpdateVersion(
18+
downloadedVersion: string,
19+
state: DesktopUpdateState,
20+
): string | null {
21+
const available = state.availableVersion;
22+
return state.status === "available" &&
23+
available !== null &&
24+
compareSemverVersions(available, downloadedVersion) > 0
25+
? available
26+
: null;
27+
}
28+
29+
/**
30+
* Fork: checks for a newer release before the user installs a downloaded update. A check drops
31+
* the download from the state, but electron-updater keeps the downloaded file until the next
32+
* download. So the state gets the download back, and "Install this one" still installs it.
33+
*/
34+
export const makeCheckBeforeInstall = (updater: {
35+
readonly getState: Effect.Effect<DesktopUpdateState>;
36+
readonly updateState: (
37+
f: (state: DesktopUpdateState) => DesktopUpdateState,
38+
) => Effect.Effect<DesktopUpdateState>;
39+
readonly check: Effect.Effect<boolean>;
40+
readonly stateChanges: PubSub.PubSub<DesktopUpdateState>;
41+
readonly checkWait: Duration.Duration;
42+
}): Effect.Effect<DesktopUpdateInstallCheck> =>
43+
Effect.scoped(
44+
Effect.gen(function* () {
45+
// Subscribe first, so the end of a check in flight cannot slip between the read and the wait.
46+
const changes = yield* PubSub.subscribe(updater.stateChanges);
47+
const before = yield* updater.getState;
48+
const downloadedVersion = before.downloadedVersion;
49+
if (downloadedVersion === null) return { newerVersion: null };
50+
51+
if (before.status !== "checking") {
52+
const now = yield* Clock.currentTimeMillis;
53+
// An unreadable `checkedAt` parses to NaN, which is never recent, so the check runs.
54+
const checkedAgo = before.checkedAt === null ? NaN : now - Date.parse(before.checkedAt);
55+
if (!(checkedAgo < Duration.toMillis(RECENT_CHECK_AGE))) yield* updater.check;
56+
}
57+
// The updater events that end a check reach the state on their own fiber, after the check
58+
// returns. So wait until the state leaves "checking", also for a check that ran already.
59+
if ((yield* updater.getState).status === "checking") {
60+
yield* PubSub.take(changes).pipe(
61+
Effect.repeat({ until: (state) => state.status !== "checking" }),
62+
Effect.timeoutOption(updater.checkWait),
63+
);
64+
}
65+
66+
const after = yield* updater.getState;
67+
if (after.status === "available" && after.downloadedVersion === null) {
68+
// No `checkedAt`: the last check found a newer release, so the next install checks again.
69+
yield* updater.updateState((current) =>
70+
current.status === "available" && current.downloadedVersion === null
71+
? {
72+
...reduceDesktopUpdateStateOnDownloadComplete(current, downloadedVersion),
73+
releaseNotes: before.releaseNotes,
74+
omittedReleaseCount: before.omittedReleaseCount,
75+
checkedAt: null,
76+
}
77+
: current,
78+
);
79+
}
80+
return { newerVersion: resolveNewerUpdateVersion(downloadedVersion, after) };
81+
}),
82+
).pipe(Effect.withSpan("desktop.updates.fork.checkBeforeInstall"));

‎apps/desktop/src/window/DesktopApplicationMenu.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ const layerDesktopUpdates = Layer.succeed(DesktopUpdates.DesktopUpdates, {
6969
download: Effect.die("unexpected download"),
7070
install: Effect.die("unexpected install"),
7171
installPrepared: () => Effect.die("unexpected installPrepared"),
72+
checkBeforeInstall: Effect.die("unexpected checkBeforeInstall"),
7273
} satisfies DesktopUpdates.DesktopUpdates["Service"]);
7374

7475
const layerDesktopWindow = (selectedAction: Deferred.Deferred<string>) =>

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ import {
147147
} from "./desktopUpdate.logic";
148148
import { showDesktopUpdateDownloadedToast } from "./desktopUpdate.toast";
149149
import { openIdleInstallPromptIfAgentsRun } from "./desktop/DesktopUpdateIdleInstall";
150+
import { prepareNewestUpdateInstall } from "./desktop/DesktopUpdateNewerCheck";
150151
import { Alert, AlertAction, AlertDescription, AlertTitle } from "./ui/alert";
151152
import { Button } from "./ui/button";
152153
import {
@@ -3762,7 +3763,7 @@ export default function LegacySidebar() {
37623763
);
37633764
return;
37643765
}
3765-
if (!confirmed) {
3766+
if (!confirmed || !(await prepareNewestUpdateInstall(bridge))) {
37663767
setDesktopUpdateActionPending(false);
37673768
return;
37683769
}

0 commit comments

Comments
 (0)