Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix(desktop): check for core OTA immediately after startup (#20163)

Innei9e6b1bab60d9
diff --git a/apps/desktop/src/main/core/infrastructure/coreOta/CoreUpdateManager.ts b/apps/desktop/src/main/core/infrastructure/coreOta/CoreUpdateManager.ts
index ba1f5d9ee79..2fdcf83042f 100644
--- a/apps/desktop/src/main/core/infrastructure/coreOta/CoreUpdateManager.ts
+++ b/apps/desktop/src/main/core/infrastructure/coreOta/CoreUpdateManager.ts
@@ -40,7 +40,7 @@ const DOWNLOAD_TIMEOUT = 15 * 60 * 1000;
const LOAD_PING_TIMEOUT = 3000;
const MAX_BOOT_CRASHES = 2;
const CHECK_INTERVAL = 60 * 60 * 1000;
-const FIRST_CHECK_DELAY = Number(process.env['RENDERER_OTA_CHECK_DELAY']) || 90 * 1000;
+const FIRST_CHECK_DELAY = Number(process.env['RENDERER_OTA_CHECK_DELAY']) || 0;
const IDLE_APPLY_DELAY = 5 * 60 * 1000;
const RENDERER_ROOT = 'dist/renderer';
const FEED_BASE_URL =
@@ -83,6 +83,7 @@ export class CoreUpdateManager {
private rollbackRendererDir: string | null = null;
private pendingBootCheck = false;
private coldBootCheck = false;
+ private deferredColdBootCheck = false;
private mountedSeen = false;
private bootCrashCount = 0;
private bootCheckTimer: NodeJS.Timeout | null = null;
@@ -225,10 +226,13 @@ export class CoreUpdateManager {
}
logger.info(`Core ${this.pointer.current} boot check passed`);
this.clearBootTimers();
+ const shouldRunDeferredCheck = this.deferredColdBootCheck;
this.pendingBootCheck = false;
+ this.deferredColdBootCheck = false;
this.bootCrashCount = 0;
this.rollbackRendererDir = null;
this.gc();
+ if (shouldRunDeferredCheck) this.checkForUpdates();
};
handleRendererCrash = () => {
@@ -302,6 +306,11 @@ export class CoreUpdateManager {
});
return;
}
+ if (this.pendingBootCheck && this.coldBootCheck) {
+ this.deferredColdBootCheck = true;
+ logger.info('Core OTA check deferred', { reason: 'cold-boot-check' });
+ return;
+ }
const generation = this.checkGeneration;
const feedUrl = this.feedUrl();
this.busy = true;
@@ -458,6 +467,7 @@ export class CoreUpdateManager {
const coldBoot = this.coldBootCheck;
logger.warn('Core OTA rolled back', { coldBoot, failedVersion: bad, reason });
this.pendingBootCheck = false;
+ this.deferredColdBootCheck = false;
this.savePointer({
blacklist: bad ? [...new Set([...this.pointer.blacklist, bad])] : this.pointer.blacklist,
current: this.pointer.previous,
@@ -476,6 +486,7 @@ export class CoreUpdateManager {
private armBootCheck({ cold = false } = {}) {
this.pendingBootCheck = true;
this.coldBootCheck = cold;
+ this.deferredColdBootCheck = false;
this.bootCrashCount = 0;
this.clearBootTimers();
if (!cold) {
diff --git a/apps/desktop/src/main/core/infrastructure/coreOta/__tests__/CoreUpdateManager.int.test.ts b/apps/desktop/src/main/core/infrastructure/coreOta/__tests__/CoreUpdateManager.int.test.ts
index 8f4e25a73ac..2db026262bf 100644
--- a/apps/desktop/src/main/core/infrastructure/coreOta/__tests__/CoreUpdateManager.int.test.ts
+++ b/apps/desktop/src/main/core/infrastructure/coreOta/__tests__/CoreUpdateManager.int.test.ts
@@ -216,6 +216,26 @@ const mainChanged = (version: string, seq: number) =>
buildManifest(version, seq, { ...BASE_FILES, 'dist/main/index.js': `main-${version}` });
describe('CoreUpdateManager initialize', () => {
+ it('checks for updates in the background immediately after startup and hourly thereafter', async () => {
+ vi.useFakeTimers();
+ try {
+ const { manager } = await loadManager();
+ manager.startScheduledChecks();
+
+ expect(fetchImpl).not.toHaveBeenCalled();
+ await vi.advanceTimersByTimeAsync(1);
+ expect(fetchImpl).toHaveBeenCalledExactlyOnceWith(
+ `${SERVER}/stable/core/${PLATFORM}/latest.json`,
+ { cache: 'no-store' },
+ );
+
+ await vi.advanceTimersByTimeAsync(60 * 60 * 1000 - 1);
+ expect(fetchImpl).toHaveBeenCalledTimes(2);
+ } finally {
+ vi.useRealTimers();
+ }
+ });
+
it('reports disabled reasons when shell is missing', async () => {
const { manager } = await loadManager(makeApp(), null);
expect(manager.enabled).toBe(false);
@@ -350,6 +370,57 @@ describe('CoreUpdateManager initialize', () => {
}
});
+ it('defers the startup update check until the cold boot check passes', async () => {
+ vi.useFakeTimers();
+ try {
+ serveLatest(mainChanged('1.0.2', 2));
+ const { manager } = await bootExternal({ failures: 1, version: '1.0.1' });
+ manager.startScheduledChecks();
+
+ await vi.advanceTimersByTimeAsync(1);
+ expect(fetchImpl).not.toHaveBeenCalled();
+ expect(readPointer(otaRoot(), ABI)).toMatchObject({ current: '1.0.1', previous: null });
+
+ manager.handleBootPing('mounted');
+ await vi.advanceTimersByTimeAsync(1);
+
+ expect(fetchImpl).toHaveBeenCalledExactlyOnceWith(
+ `${SERVER}/stable/core/${PLATFORM}/latest.json`,
+ { cache: 'no-store' },
+ );
+ await vi.waitFor(() => {
+ expect(readPointer(otaRoot(), ABI)).toMatchObject({
+ current: '1.0.2',
+ previous: '1.0.1',
+ });
+ });
+ } finally {
+ vi.useRealTimers();
+ }
+ });
+
+ it('does not stage a newer core before a cold boot timeout rollback', async () => {
+ vi.useFakeTimers();
+ try {
+ serveLatest(mainChanged('1.0.2', 2));
+ const { manager } = await bootExternal({ failures: 1, version: '1.0.1' });
+ manager.startScheduledChecks();
+
+ await vi.advanceTimersByTimeAsync(1);
+ await vi.advanceTimersByTimeAsync(61_000);
+
+ expect(fetchImpl).not.toHaveBeenCalled();
+ expect(readPointer(otaRoot(), ABI)).toMatchObject({
+ blacklist: ['1.0.1'],
+ current: null,
+ previous: null,
+ });
+ expect(electronMock.app.relaunch).toHaveBeenCalled();
+ } finally {
+ vi.useRealTimers();
+ }
+ });
+
it('does not arm when the renderer already mounted before scheduling', async () => {
vi.useFakeTimers();
try {