Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(update): drop patch-info before channel relaunch

guyong46bd47ad7340
diff --git a/apps/desktop/src/main/__tests__/updateService.test.ts b/apps/desktop/src/main/__tests__/updateService.test.ts
index 27a7db5fb9..f1f3545626 100644
--- a/apps/desktop/src/main/__tests__/updateService.test.ts
+++ b/apps/desktop/src/main/__tests__/updateService.test.ts
@@ -19,6 +19,8 @@ const powerMonitorGetSystemIdleTime = vi.fn(() => 600);
const powerMonitorOn = vi.fn();
const powerMonitorRemoveListener = vi.fn();
const appGetVersion = vi.fn(() => '0.0.64');
+const appRelaunch = vi.fn();
+const appQuit = vi.fn();
const appIsInApplicationsFolder = vi.fn(() => true);
const appGetPath = vi.fn((name: string) => {
if (name === 'userData') return TEST_USER_DATA;
@@ -40,6 +42,8 @@ vi.mock('electron', () => ({
app: {
getVersion: appGetVersion,
getPath: appGetPath,
+ relaunch: appRelaunch,
+ quit: appQuit,
isPackaged: true,
isInApplicationsFolder: appIsInApplicationsFolder,
moveToApplicationsFolder: vi.fn(),
@@ -116,6 +120,11 @@ vi.mock('../cindy-brain/index', () => ({
getGhostNodeRuntimeBroker: () => ({ destroyAll: vi.fn() }),
}));
+vi.mock('../security/trustedAppRenderer', () => ({
+ assertTrustedAppRendererEvent: vi.fn(),
+}));
+
+
vi.mock('../logger', () => ({
createLogger: () => ({
info: logInfo,
@@ -166,6 +175,8 @@ beforeEach(() => {
powerMonitorRemoveListener.mockReset();
appGetVersion.mockReset();
appGetVersion.mockReturnValue('0.0.64');
+ appRelaunch.mockReset();
+ appQuit.mockReset();
appIsInApplicationsFolder.mockReset();
appIsInApplicationsFolder.mockReturnValue(true);
appGetPath.mockReset();
@@ -626,6 +637,89 @@ describe('startup update relaunch safety', () => {
}
});
+ it('drops patch-info immediately so a channel relaunch cannot revive the old zip', async () => {
+ const service = await bootWithStagedPatch({ enabled: true });
+ let releaseProbe: ((busy: boolean) => void) | undefined;
+ const probeStarted = new Promise<void>((resolveStarted) => {
+ service.setUpdateAutoRelaunchBusyProbe(
+ () =>
+ new Promise<boolean>((resolveProbe) => {
+ resolveStarted();
+ releaseProbe = resolveProbe;
+ }),
+ );
+ });
+ try {
+ await probeStarted;
+ await expect(service.enableUncustomizedBetaChannel()).resolves.toBe(true);
+ expect(service.getUpdateStatus()).toBe('ready');
+ expect(fs.existsSync(path.join(TEST_USER_DATA, 'updates', 'patch-info.json'))).toBe(false);
+
+ const relaunch = ipcHandlers.get('update-channel-relaunch');
+ expect(relaunch).toBeTypeOf('function');
+ relaunch?.({ sender: { id: 1 } });
+ expect(appRelaunch).toHaveBeenCalled();
+ expect(appQuit).toHaveBeenCalled();
+ expect(fs.existsSync(path.join(TEST_USER_DATA, 'updates', 'patch-info.json'))).toBe(false);
+ } finally {
+ releaseProbe?.(true);
+ service.stopUpdateService();
+ }
+ });
+
+ it('does not bump the channel epoch again when flushing during a same-path download', async () => {
+ const sharedHotfix = 'app/darwin-arm64/xdt-maker-hotfix.zip';
+ const service = await bootWithStagedPatch({
+ enabled: true,
+ manifest: updateManifest('0.0.65', sharedHotfix),
+ });
+ let releaseProbe: ((busy: boolean) => void) | undefined;
+ const probeStarted = new Promise<void>((resolveStarted) => {
+ service.setUpdateAutoRelaunchBusyProbe(
+ () =>
+ new Promise<boolean>((resolveProbe) => {
+ resolveStarted();
+ releaseProbe = resolveProbe;
+ }),
+ );
+ });
+ let finishDownload: (() => void) | undefined;
+ download.mockImplementation(async ({ targetPath }: { targetPath: string }) => {
+ await new Promise<void>((resolve) => {
+ finishDownload = resolve;
+ });
+ fs.mkdirSync(path.dirname(targetPath), { recursive: true });
+ fs.writeFileSync(targetPath, 'newer-update');
+ return { path: targetPath, size: 123 };
+ });
+ try {
+ await probeStarted;
+ await expect(service.enableUncustomizedBetaChannel()).resolves.toBe(true);
+ service.setUpdateAutoRelaunchBusyProbe(() => true);
+ readUpdateChannelSettings.mockReturnValue({
+ enableBeta: true,
+ orgDefaultEnableBeta: true,
+ });
+ fetchManifest.mockResolvedValue(updateManifest('0.0.66', sharedHotfix));
+ const checkPromise = service.checkForUpdate();
+ await vi.waitFor(() => {
+ expect(finishDownload).toBeTypeOf('function');
+ });
+ releaseProbe?.(true);
+ await vi.waitFor(() => {
+ expect(service.getUpdateStatus()).toBe('superseding');
+ });
+ finishDownload?.();
+ await expect(checkPromise).resolves.toBe('ready');
+ expect(service.getUpdateStatus()).toBe('ready');
+ expect(
+ fs.readFileSync(path.join(TEST_USER_DATA, 'updates', 'patch-info.json'), 'utf-8'),
+ ).toContain('0.0.66');
+ } finally {
+ service.stopUpdateService();
+ }
+ });
+
it('invalidates an in-flight check when another instance changes the shared channel', async () => {
const service = await bootWithStagedPatch({ enabled: true });
try {
diff --git a/apps/desktop/src/main/updateService.ts b/apps/desktop/src/main/updateService.ts
index e7ac5e2d21..7c87b248d3 100644
--- a/apps/desktop/src/main/updateService.ts
+++ b/apps/desktop/src/main/updateService.ts
@@ -647,6 +647,23 @@ function isCurrentPatchNewerThanDeferred(
return Boolean(readyFilePath && readyFilePath !== stale.path);
}
+function discardStagedPatchFiles(): void {
+ if (readyFilePath) {
+ try { fs.unlinkSync(readyFilePath); } catch { /* ignore */ }
+ }
+ readyVersion = undefined;
+ readyFilePath = undefined;
+ readyChannelEpoch = undefined;
+ removePatchInfo();
+ if (
+ currentStatus === 'ready' ||
+ currentStatus === 'superseding' ||
+ currentStatus === 'downloading'
+ ) {
+ setStatus('idle');
+ }
+}
+
function flushDeferredStagedPatchClear(): void {
if (!deferredStagedPatch) return;
if (isUpdateApplyCommitted() || autoRelaunchDecisionDepth > 0) return;
@@ -659,7 +676,26 @@ function flushDeferredStagedPatchClear(): void {
}
return;
}
- clearStagedPatch();
+ if (currentStatus === 'downloading' || currentStatus === 'superseding') {
+ // 新渠道下载还在飞:不要再推进代际,也不要删同路径 dest。
+ if (readyChannelEpoch === stale.epoch) {
+ readyVersion = undefined;
+ readyFilePath = undefined;
+ readyChannelEpoch = undefined;
+ }
+ return;
+ }
+ discardStagedPatchFiles();
+}
+
+/**
+ * 渠道切换重启前把未应用的旧补丁从盘上拿掉。
+ * 延迟清理只活在内存里,这里不补清的话,下次启动仍可能把旧渠道 zip 当匹配补丁装上。
+ */
+function discardUnappliedStagedPatchForChannelRelaunch(): void {
+ if (isUpdateApplyCommitted()) return;
+ deferredStagedPatch = undefined;
+ discardStagedPatchFiles();
}
/**
@@ -667,7 +703,8 @@ function flushDeferredStagedPatchClear(): void {
* 切渠道时调用——opt-out 后不能仍让 staged 的旧渠道 patch 在下次启动/后台
* 轮询里被装上去(切渠道不等于切版本,必须把旧渠道的补丁作废)。
* 已经开始应用时不能清,否则 executeRelaunch 会走 no_ready_file。
- * 资格检查还在等 busyProbe 时先记下,检查结束后若没真正开始应用再补清。
+ * 资格检查还在等 busyProbe 时先记下 zip,但立刻丢掉 patch-info,
+ * 避免用户马上切渠道重启后旧补丁跨进程复活。
*/
function clearStagedPatch(): void {
if (isUpdateApplyCommitted()) {
@@ -678,27 +715,13 @@ function clearStagedPatch(): void {
rememberDeferredStagedPatch();
// 立刻作废旧渠道 in-flight 下载,避免下载完成后又把旧补丁写成 ready。
invalidateInFlightChannelDownloads();
+ // patch-info 立刻拿掉:渠道重启走 app.quit(),等不到 probe/finally。
+ removePatchInfo();
log.info('deferring staged patch clear until auto-relaunch eligibility settles');
return;
}
- if (readyFilePath) {
- try { fs.unlinkSync(readyFilePath); } catch { /* ignore */ }
- }
- readyVersion = undefined;
- readyFilePath = undefined;
- readyChannelEpoch = undefined;
- removePatchInfo();
invalidateInFlightChannelDownloads();
- // downloading 也要重置:opt-out 若发生在「首次 beta 下载进行中」,status 仍是
- // downloading;不 reset 的话,in-flight 下载完成后 discard 分支只是 `return 'idle'`
- // 而不 setStatus,update-get-status / update-check-now 会永远卡在 downloading。
- if (
- currentStatus === 'ready' ||
- currentStatus === 'superseding' ||
- currentStatus === 'downloading'
- ) {
- setStatus('idle');
- }
+ discardStagedPatchFiles();
}
function incrementApplyAttempts(): void {
@@ -861,6 +884,7 @@ async function doCheckForUpdate(manifestOverride?: Manifest | null): Promise<Che
const wasReady = currentStatus === 'ready';
const previousReadyVersion = wasReady ? readyVersion : undefined;
const previousReadyFilePath = wasReady ? readyFilePath : undefined;
+ const previousReadyChannelEpoch = wasReady ? readyChannelEpoch : undefined;
// 只有非 ready 路径才广播 'checking' — wasReady 路径下广播 checking 会让 banner
// 的可见条件(status === 'ready' || 'superseding')瞬间不满足,banner 抖一下。
@@ -1062,7 +1086,7 @@ async function doCheckForUpdate(manifestOverride?: Manifest | null): Promise<Che
log.info('Superseding download failed — rolling back to ready v%s', previousReadyVersion);
readyVersion = previousReadyVersion;
readyFilePath = previousReadyFilePath;
- readyChannelEpoch = channelEpochAtStart;
+ readyChannelEpoch = previousReadyChannelEpoch;
setStatus('ready', { version: previousReadyVersion });
return 'ready';
}
@@ -1436,6 +1460,7 @@ export function initUpdateService(): void {
ipcMain.handle('update-channel-relaunch', (event) => {
assertTrustedAppRendererEvent(event);
log.info('relaunch requested for update channel change');
+ discardUnappliedStagedPatchForChannelRelaunch();
app.relaunch();
app.quit();
});