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): invalidate patches before channel writes settle

guyong4aa0ed882c2d
diff --git a/apps/desktop/src/main/__tests__/updateService.test.ts b/apps/desktop/src/main/__tests__/updateService.test.ts
index d9df9c9aef..b35f6dd5d1 100644
--- a/apps/desktop/src/main/__tests__/updateService.test.ts
+++ b/apps/desktop/src/main/__tests__/updateService.test.ts
@@ -75,6 +75,7 @@ vi.mock('../auto-update-settings-store', () => ({
}));
const tryEnableUncustomizedBetaAtomic = vi.fn(async () => true);
+const writeEnableBeta = vi.fn(async () => undefined);
const readUpdateChannelSettings = vi.fn(() => ({
enableBeta: false,
orgDefaultEnableBeta: false,
@@ -96,7 +97,7 @@ vi.mock('../updateChannelStore', () => ({
defaults: { enableBeta: false, orgDefaultEnableBeta: false },
}),
resetUpdateChannelSettings: () => ({ enableBeta: false, orgDefaultEnableBeta: false }),
- writeEnableBeta: vi.fn(),
+ writeEnableBeta,
tryEnableUncustomizedBetaAtomic,
isEnableBetaUserCustomized: () => false,
isBetaChannelEnabled: () => readUpdateChannelSettings().enableBeta === true,
@@ -193,6 +194,8 @@ beforeEach(() => {
download.mockReset();
tryEnableUncustomizedBetaAtomic.mockReset();
tryEnableUncustomizedBetaAtomic.mockResolvedValue(true);
+ writeEnableBeta.mockReset();
+ writeEnableBeta.mockResolvedValue(undefined);
readUpdateChannelSettings.mockReset();
readUpdateChannelSettings.mockReturnValue({
enableBeta: false,
@@ -777,6 +780,68 @@ describe('startup update relaunch safety', () => {
service.stopUpdateService();
}
});
+
+ it('aborts a manual relaunch when a channel change is still deferred', 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;
+ const setHandler = ipcHandlers.get('update-channel-settings-set');
+ expect(setHandler).toBeTypeOf('function');
+ await setHandler?.({ sender: { id: 1 } }, { enableBeta: true });
+ expect(service.getUpdateStatus()).toBe('ready');
+ const relaunch = ipcListeners.get('update-relaunch');
+ expect(relaunch).toBeTypeOf('function');
+ logInfo.mockClear();
+ relaunch?.({}, 'dark');
+ expect(logInfo.mock.calls.map((call) => String(call[0]))).toEqual(
+ expect.arrayContaining([
+ expect.stringContaining('executeRelaunch() aborted'),
+ ]),
+ );
+ releaseProbe?.(false);
+ await vi.waitFor(() => {
+ expect(service.getUpdateStatus()).toBe('idle');
+ });
+ expect(service.isUpdateRelaunchImminent()).toBe(false);
+ } finally {
+ service.stopUpdateService();
+ }
+ });
+
+ it('invalidates the staged patch before the settings write settles', async () => {
+ const service = await bootWithStagedPatch({ enabled: false });
+ let releaseWrite: (() => void) | undefined;
+ writeEnableBeta.mockImplementation(
+ () =>
+ new Promise<undefined>((resolve) => {
+ releaseWrite = () => resolve(undefined);
+ }),
+ );
+ try {
+ const setHandler = ipcHandlers.get('update-channel-settings-set');
+ expect(setHandler).toBeTypeOf('function');
+ expect(fs.existsSync(path.join(TEST_USER_DATA, 'updates', 'patch-info.json'))).toBe(true);
+ const writePromise = setHandler?.({ sender: { id: 1 } }, { enableBeta: true });
+ await vi.waitFor(() => {
+ expect(releaseWrite).toBeTypeOf('function');
+ });
+ expect(fs.existsSync(path.join(TEST_USER_DATA, 'updates', 'patch-info.json'))).toBe(false);
+ releaseWrite?.();
+ await writePromise;
+ } finally {
+ service.stopUpdateService();
+ }
+ });
});
describe('splash 启动下载 0% 显式广播', () => {
diff --git a/apps/desktop/src/main/updateService.ts b/apps/desktop/src/main/updateService.ts
index 9e0fb7a36d..541cb40dc2 100644
--- a/apps/desktop/src/main/updateService.ts
+++ b/apps/desktop/src/main/updateService.ts
@@ -1306,10 +1306,15 @@ function executeRelaunch(theme: 'light' | 'dark'): void {
return;
}
- if (syncObservedUpdateChannel()) {
- log.info('executeRelaunch() aborted — shared update channel changed');
+ if (syncObservedUpdateChannel() || deferredStagedPatch) {
+ log.info(
+ deferredStagedPatch
+ ? 'executeRelaunch() aborted — update channel changed during eligibility check'
+ : 'executeRelaunch() aborted — shared update channel changed',
+ );
isRelaunching = false;
autoRelaunchInProgress = false;
+ // 标志先放下,否则 clearStagedPatch 会当成 apply 已提交而跳过。
clearStagedPatch();
return;
}
@@ -1433,28 +1438,34 @@ export function initUpdateService(): void {
throwIpcError('INVALID_PARAMS', 'enableBeta required (boolean)');
}
const wasBeta = readUpdateChannelSettings().enableBeta;
- await writeEnableBeta(next);
- // 渠道一变(无论 opt-in 还是 opt-out)就作废已 staged 的旧渠道补丁:
- // - opt-out(beta→release):否则用户关掉 beta 后仍被装到 beta 版本;
- // - opt-in(release→beta):否则旧 release 补丁仍 staged,重启后 beta manifest
- // 拉取失败时 checkExistingPatch() 会兜底把用户装到 release 版本。
- // 切渠道不等于切版本,两个方向都要清。
+ // 先作废旧补丁再等落盘:writeEnableBeta 可能卡住跨进程锁,这段空窗里
+ // busyProbe 若返回,资格检查仍会看到旧渠道+旧 zip 并提交 apply。
if (wasBeta !== next) {
observedEnableBeta = next;
clearStagedPatch();
}
+ try {
+ await writeEnableBeta(next);
+ } catch (err) {
+ log.error('writeEnableBeta failed:', err);
+ throwIpcError('INTERNAL', 'failed to write update channel settings');
+ }
return channelSettingsWire();
});
ipcMain.handle('update-channel-settings-reset', async (event) => {
assertTrustedAppRendererEvent(event);
const wasBeta = readUpdateChannelSettings().enableBeta;
- await resetUpdateChannelSettings();
- // 恢复默认 = 关闭 beta;同 set(false),渠道变化即作废已 staged 的补丁。
if (wasBeta) {
observedEnableBeta = false;
clearStagedPatch();
}
+ try {
+ await resetUpdateChannelSettings();
+ } catch (err) {
+ log.error('resetUpdateChannelSettings failed:', err);
+ throwIpcError('INTERNAL', 'failed to reset update channel settings');
+ }
return channelSettingsWire();
});
@@ -1647,11 +1658,14 @@ export async function enableUncustomizedBetaChannel(
shouldWrite: () => boolean = () => true,
): Promise<boolean> {
const wasBeta = readUpdateChannelSettings().enableBeta;
+ // 先作废旧补丁再等落盘,避免原子写等待期间自动重启把旧渠道包装上去。
+ // 没真正写成开之前,不要把 observedEnableBeta 提前改成 true。
+ if (!wasBeta) {
+ clearStagedPatch();
+ }
const wrote = await tryEnableUncustomizedBetaAtomic(shouldWrite);
if (wrote && !wasBeta) {
observedEnableBeta = true;
- // 真正开始应用时 clearStagedPatch 会跳过;资格检查中会记下,结束后再补清。
- clearStagedPatch();
broadcastChannelSettings();
}
return wrote;