Skip to content

AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.

fix(desktop): hold auto-restart until the instance catches up

tianma-if4b618964e2f4
diff --git a/apps/desktop/src/main/desktop-update-flow.test.mjs b/apps/desktop/src/main/desktop-update-flow.test.mjs
index 15052316..2bd26f65 100644
--- a/apps/desktop/src/main/desktop-update-flow.test.mjs
+++ b/apps/desktop/src/main/desktop-update-flow.test.mjs
@@ -77,4 +77,14 @@ describe("desktop update flow", () => {
expect(notebookPaneSource).toContain("<DesktopUpdateNotice />");
expect(notebookPaneSource).toContain('className="flex items-center gap-1"');
});
+
+ test("holds in-app restart installation while the GitHub release is newer than the instance", () => {
+ expect(mainSource).toContain("shouldHoldAutoRestartUpdate(downloadedUpdateVersion, instanceVersion)");
+ expect(mainSource).toContain("holdAutoRestartUpdate(downloadedUpdateVersion)");
+ expect(mainSource).toContain('writeDiagnostic("update.held-for-instance"');
+ expect(mainSource).toContain("autoUpdater.autoInstallOnAppQuit = false");
+ expect(mainSource).toContain("releaseHeldAutoRestartUpdate()");
+ expect(mainSource).toContain("${configuredApiBaseUrl}/api/release");
+ expect(mainSource).toContain("heldUpdateVersion ||");
+ });
});
diff --git a/apps/desktop/src/main/index.mjs b/apps/desktop/src/main/index.mjs
index 4856d074..4f0782d9 100644
--- a/apps/desktop/src/main/index.mjs
+++ b/apps/desktop/src/main/index.mjs
@@ -39,6 +39,7 @@ import {
fetchTrustedWindowsUpdate,
verifyDownloadedWindowsUpdate,
} from "./windows-update-trust.mjs";
+import { instanceReleaseVersionFromPayload, shouldHoldAutoRestartUpdate } from "./instance-update-gate.mjs";
import electronUpdater from "electron-updater";
import { createPluginPublicNetworkRuntime } from "./plugin-public-network.mjs";
import { shouldQuitAfterAllWindowsClosed } from "./window-lifecycle.mjs";
@@ -115,6 +116,7 @@ let updateDownloadInFlight = null;
let updateCheckTimer = null;
let lastUpdateCheckAt = 0;
let downloadedUpdateVersion = null;
+let heldUpdateVersion = null;
let promptedUpdateVersion = null;
let trustedWindowsUpdate = null;
let windowsDownloadedUpdateVerified = false;
@@ -806,9 +808,52 @@ const publishDesktopUpdateStatus = () => {
mainWindow.webContents.send("desktop:update-status-changed", desktopUpdateStatus());
};
+const readInstanceReleaseVersion = async () => {
+ if (!configuredApiBaseUrl) return null;
+ try {
+ const response = await net.fetch(`${configuredApiBaseUrl}/api/release`);
+ if (!response.ok) return null;
+ return instanceReleaseVersionFromPayload(await response.json());
+ } catch {
+ return null;
+ }
+};
+
+const holdAutoRestartUpdate = async (version) => {
+ heldUpdateVersion = version || "unknown";
+ autoUpdater.autoInstallOnAppQuit = false;
+ updateState = "available";
+ downloadedUpdateVersion = version || downloadedUpdateVersion;
+ refreshTrayMenu();
+ publishDesktopUpdateStatus();
+ await writeDiagnostic("update.held-for-instance", { version: heldUpdateVersion });
+};
+
+const releaseHeldAutoRestartUpdate = async () => {
+ if (!heldUpdateVersion || linuxUpdateTestMode) return false;
+ const instanceVersion = await readInstanceReleaseVersion();
+ if (shouldHoldAutoRestartUpdate(heldUpdateVersion, instanceVersion)) return false;
+ const version = heldUpdateVersion === "unknown" ? downloadedUpdateVersion : heldUpdateVersion;
+ heldUpdateVersion = null;
+ if (process.platform !== "win32" || windowsDownloadedUpdateVerified) {
+ autoUpdater.autoInstallOnAppQuit = true;
+ }
+ updateState = "downloaded";
+ downloadedUpdateVersion = version || downloadedUpdateVersion;
+ refreshTrayMenu();
+ publishDesktopUpdateStatus();
+ await writeDiagnostic("update.released-for-instance", { version: downloadedUpdateVersion });
+ await promptForDownloadedUpdate(downloadedUpdateVersion).catch((error) => {
+ promptedUpdateVersion = null;
+ void writeDiagnostic("update.prompt-failed", { message: error.message });
+ });
+ return true;
+};
+
const installDownloadedUpdate = () => {
if (
updateState !== "downloaded" ||
+ heldUpdateVersion ||
(process.platform === "win32" && !windowsDownloadedUpdateVerified)
) return { started: false };
// The normal window close handler hides the app. Mark this as a real quit
@@ -881,8 +926,11 @@ const checkForDesktopUpdate = (reason, { force = false, throwOnError = false } =
if (!force && now - lastUpdateCheckAt < updateCheckFocusThrottleMs) return Promise.resolve(null);
lastUpdateCheckAt = now;
void writeDiagnostic("update.check-started", { reason });
- updateCheckInFlight = autoUpdater.checkForUpdates()
- .then(async (result) => {
+ updateCheckInFlight = Promise.resolve()
+ .then(() => releaseHeldAutoRestartUpdate())
+ .then(async (released) => {
+ if (released || updateState === "downloaded") return null;
+ const result = await autoUpdater.checkForUpdates();
if (process.platform === "win32" && result?.isUpdateAvailable) {
trustedWindowsUpdate = await fetchTrustedWindowsUpdate({
version: result.updateInfo.version,
@@ -902,12 +950,14 @@ const checkForDesktopUpdate = (reason, { force = false, throwOnError = false } =
return result;
})
.catch(async (error) => {
- updateState = "idle";
- downloadedUpdateVersion = null;
- trustedWindowsUpdate = null;
- windowsDownloadedUpdateVerified = false;
- refreshTrayMenu();
- publishDesktopUpdateStatus();
+ if (!heldUpdateVersion) {
+ updateState = "idle";
+ downloadedUpdateVersion = null;
+ trustedWindowsUpdate = null;
+ windowsDownloadedUpdateVerified = false;
+ refreshTrayMenu();
+ publishDesktopUpdateStatus();
+ }
await writeDiagnostic("update.check-failed", { reason, message: error.message });
throw error;
})
@@ -945,6 +995,7 @@ const configureAutoUpdater = () => {
autoUpdater.on("update-not-available", () => {
updateState = "idle";
downloadedUpdateVersion = null;
+ heldUpdateVersion = null;
trustedWindowsUpdate = null;
windowsDownloadedUpdateVerified = false;
refreshTrayMenu();
@@ -968,8 +1019,16 @@ const configureAutoUpdater = () => {
windowsDownloadedUpdateVerified = true;
autoUpdater.autoInstallOnAppQuit = true;
}
- updateState = "downloaded";
downloadedUpdateVersion = info?.version || downloadedUpdateVersion;
+ if (!linuxUpdateTestMode) {
+ const instanceVersion = await readInstanceReleaseVersion();
+ if (shouldHoldAutoRestartUpdate(downloadedUpdateVersion, instanceVersion)) {
+ await holdAutoRestartUpdate(downloadedUpdateVersion);
+ return;
+ }
+ }
+ updateState = "downloaded";
+ heldUpdateVersion = null;
refreshTrayMenu();
publishDesktopUpdateStatus();
await writeDiagnostic("update.downloaded", { version: downloadedUpdateVersion });
diff --git a/apps/desktop/src/main/instance-update-gate.mjs b/apps/desktop/src/main/instance-update-gate.mjs
new file mode 100644
index 00000000..1413ad86
--- /dev/null
+++ b/apps/desktop/src/main/instance-update-gate.mjs
@@ -0,0 +1,42 @@
+const parseVersion = (value) => {
+ const match = String(value ?? "").match(/^v?(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z.-]+))?(?:\+[0-9A-Za-z.-]+)?$/i);
+ return match
+ ? { core: match.slice(1, 4).map(Number), prerelease: match[4]?.split(".") ?? null }
+ : null;
+};
+
+const isVersionOutdated = (currentVersion, latestVersion) => {
+ const current = parseVersion(currentVersion);
+ const latest = parseVersion(latestVersion);
+ if (!current || !latest) return false;
+ for (let index = 0; index < 3; index += 1) {
+ if (current.core[index] !== latest.core[index]) return current.core[index] < latest.core[index];
+ }
+ if (!current.prerelease) return false;
+ if (!latest.prerelease) return true;
+ const length = Math.max(current.prerelease.length, latest.prerelease.length);
+ for (let index = 0; index < length; index += 1) {
+ const currentPart = current.prerelease[index];
+ const latestPart = latest.prerelease[index];
+ if (currentPart === undefined) return true;
+ if (latestPart === undefined) return false;
+ if (currentPart === latestPart) continue;
+ const currentNumber = /^\d+$/.test(currentPart) ? Number(currentPart) : null;
+ const latestNumber = /^\d+$/.test(latestPart) ? Number(latestPart) : null;
+ if (currentNumber !== null && latestNumber !== null) return currentNumber < latestNumber;
+ if (currentNumber !== null) return true;
+ if (latestNumber !== null) return false;
+ return currentPart.localeCompare(latestPart) < 0;
+ }
+ return false;
+};
+
+export const instanceReleaseVersionFromPayload = (payload) => (
+ payload && typeof payload.version === "string" && payload.version.trim()
+ ? payload.version.trim()
+ : null
+);
+
+export const shouldHoldAutoRestartUpdate = (updateVersion, instanceVersion) => (
+ Boolean(updateVersion && instanceVersion && isVersionOutdated(instanceVersion, updateVersion))
+);
diff --git a/apps/desktop/src/main/instance-update-gate.test.mjs b/apps/desktop/src/main/instance-update-gate.test.mjs
new file mode 100644
index 00000000..b73b270e
--- /dev/null
+++ b/apps/desktop/src/main/instance-update-gate.test.mjs
@@ -0,0 +1,25 @@
+import { describe, expect, test } from "bun:test";
+import { instanceReleaseVersionFromPayload, shouldHoldAutoRestartUpdate } from "./instance-update-gate.mjs";
+
+describe("desktop auto-restart update gate", () => {
+ test("holds only when the GitHub release is newer than the connected instance", () => {
+ expect(shouldHoldAutoRestartUpdate("1.64.0", "1.63.2")).toBe(true);
+ expect(shouldHoldAutoRestartUpdate("v1.64.0", "v1.64.0")).toBe(false);
+ expect(shouldHoldAutoRestartUpdate("1.63.2", "1.64.0")).toBe(false);
+ expect(shouldHoldAutoRestartUpdate("1.64.0+12", "1.64.0")).toBe(false);
+ });
+
+ test("does not hold when the instance version is unknown", () => {
+ expect(shouldHoldAutoRestartUpdate("1.64.0", null)).toBe(false);
+ expect(shouldHoldAutoRestartUpdate("1.64.0", "")).toBe(false);
+ expect(shouldHoldAutoRestartUpdate("1.64.0", "local")).toBe(false);
+ expect(shouldHoldAutoRestartUpdate("", "1.63.2")).toBe(false);
+ });
+
+ test("reads the public instance release payload", () => {
+ expect(instanceReleaseVersionFromPayload({ version: "1.63.2+4" })).toBe("1.63.2+4");
+ expect(instanceReleaseVersionFromPayload({ version: " v1.64.0 " })).toBe("v1.64.0");
+ expect(instanceReleaseVersionFromPayload({})).toBeNull();
+ expect(instanceReleaseVersionFromPayload(null)).toBeNull();
+ });
+});
diff --git a/package.json b/package.json
index 77d3fecc..755671a6 100644
--- a/package.json
+++ b/package.json
@@ -63,7 +63,7 @@
"release": "bun scripts/release.mjs",
"publish:stores": "bun scripts/store-delivery.mjs",
"test:desktop:sidecar": "bun scripts/test-desktop-sidecar.mjs",
- "test:desktop": "bun test apps/desktop/src/main/account-scope.test.mjs apps/desktop/src/main/app-protocol.test.mjs apps/desktop/src/main/child-process-start.test.mjs apps/desktop/src/main/clipboard-write.test.mjs apps/desktop/src/main/desktop-diagnostics.test.mjs apps/desktop/src/main/desktop-menu.test.mjs apps/desktop/src/main/desktop-update-flow.test.mjs apps/desktop/src/main/diagnostic-log.test.mjs apps/desktop/src/main/file-association.test.mjs apps/desktop/src/main/file-permissions.test.mjs apps/desktop/src/main/installation-location.test.mjs apps/desktop/src/main/local-data-reset.test.mjs apps/desktop/src/main/plugin-public-network.test.mjs apps/desktop/src/main/renderer-recovery.test.mjs apps/desktop/src/main/renderer-startup-guard.test.mjs apps/desktop/src/main/renderer-storage-migration.test.mjs apps/desktop/src/main/resource-request.test.mjs apps/desktop/src/main/resource-url.test.mjs apps/desktop/src/main/scheduled-task-scheduler.test.mjs apps/desktop/src/main/staged-resource.test.mjs apps/desktop/src/main/tray-icon.test.mjs apps/desktop/src/main/user-data-directory.test.mjs apps/desktop/src/main/window-open-policy.test.mjs apps/desktop/src/main/window-visibility.test.mjs apps/desktop/src/main/windows-update-trust.test.mjs apps/web/src/lib/api.test.mjs apps/web/src/lib/app-page-path.test.mjs apps/web/src/lib/clipboard.test.mjs apps/web/src/lib/desktop-renderer-ready.test.mjs apps/web/src/lib/desktop-repository.test.mjs apps/web/src/lib/desktop-resources.test.mjs apps/web/src/lib/desktop-sync.test.mjs apps/web/src/lib/local-mirror.test.mjs apps/web/src/lib/repository.test.mjs apps/web/src/lib/runtime.test.mjs apps/web/src/lib/sync-queue.test.mjs && bun run test:desktop:sidecar",
+ "test:desktop": "bun test apps/desktop/src/main/account-scope.test.mjs apps/desktop/src/main/app-protocol.test.mjs apps/desktop/src/main/child-process-start.test.mjs apps/desktop/src/main/clipboard-write.test.mjs apps/desktop/src/main/desktop-diagnostics.test.mjs apps/desktop/src/main/desktop-menu.test.mjs apps/desktop/src/main/desktop-update-flow.test.mjs apps/desktop/src/main/diagnostic-log.test.mjs apps/desktop/src/main/file-association.test.mjs apps/desktop/src/main/file-permissions.test.mjs apps/desktop/src/main/instance-update-gate.test.mjs apps/desktop/src/main/installation-location.test.mjs apps/desktop/src/main/local-data-reset.test.mjs apps/desktop/src/main/plugin-public-network.test.mjs apps/desktop/src/main/renderer-recovery.test.mjs apps/desktop/src/main/renderer-startup-guard.test.mjs apps/desktop/src/main/renderer-storage-migration.test.mjs apps/desktop/src/main/resource-request.test.mjs apps/desktop/src/main/resource-url.test.mjs apps/desktop/src/main/scheduled-task-scheduler.test.mjs apps/desktop/src/main/staged-resource.test.mjs apps/desktop/src/main/tray-icon.test.mjs apps/desktop/src/main/user-data-directory.test.mjs apps/desktop/src/main/window-open-policy.test.mjs apps/desktop/src/main/window-visibility.test.mjs apps/desktop/src/main/windows-update-trust.test.mjs apps/web/src/lib/api.test.mjs apps/web/src/lib/app-page-path.test.mjs apps/web/src/lib/clipboard.test.mjs apps/web/src/lib/desktop-renderer-ready.test.mjs apps/web/src/lib/desktop-repository.test.mjs apps/web/src/lib/desktop-resources.test.mjs apps/web/src/lib/desktop-sync.test.mjs apps/web/src/lib/local-mirror.test.mjs apps/web/src/lib/repository.test.mjs apps/web/src/lib/runtime.test.mjs apps/web/src/lib/sync-queue.test.mjs && bun run test:desktop:sidecar",
"verify:desktop-renderer": "bun scripts/verify-desktop-renderer.mjs",
"verify:renderer-origin-migration": "bun run --cwd apps/desktop verify-origin-migration",
"verify:desktop-cross-version-startup": "bun scripts/verify-desktop-cross-version-startup.mjs",