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): keep newer patches and uncommitted holds

guyongdf90dc0a76e3
diff --git a/apps/desktop/src/main/__tests__/updateService.test.ts b/apps/desktop/src/main/__tests__/updateService.test.ts
index 4465c5ad1d..eae669b5b9 100644
--- a/apps/desktop/src/main/__tests__/updateService.test.ts
+++ b/apps/desktop/src/main/__tests__/updateService.test.ts
@@ -21,6 +21,7 @@ const powerMonitorRemoveListener = vi.fn();
const appGetVersion = vi.fn(() => '0.0.64');
const appRelaunch = vi.fn();
const appQuit = vi.fn();
+const appGetAppPath = vi.fn(() => path.join(TEST_ROOT, 'Cindy.app', 'Contents', 'Resources', 'app.asar'));
const appIsInApplicationsFolder = vi.fn(() => true);
const appGetPath = vi.fn((name: string) => {
if (name === 'userData') return TEST_USER_DATA;
@@ -44,6 +45,7 @@ vi.mock('electron', () => ({
getPath: appGetPath,
relaunch: appRelaunch,
quit: appQuit,
+ getAppPath: appGetAppPath,
isPackaged: true,
isInApplicationsFolder: appIsInApplicationsFolder,
moveToApplicationsFolder: vi.fn(),
@@ -911,6 +913,93 @@ describe('startup update relaunch safety', () => {
}
});
+ it('applies a newer same-path patch after a deferred channel change', async () => {
+ const sharedHotfix = 'app/darwin-arm64/xdt-maker-hotfix.zip';
+ const service = await bootWithStagedPatch({
+ enabled: true,
+ manifest: updateManifest('0.0.65', sharedHotfix),
+ });
+ const exitSpy = vi.spyOn(process, 'exit').mockImplementation((() => undefined) as never);
+ 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);
+ service.setUpdateAutoRelaunchBusyProbe(() => true);
+ readAutoUpdateSettings.mockReturnValue({ autoRelaunchOnIdle: false });
+ readUpdateChannelSettings.mockReturnValue({
+ enableBeta: true,
+ orgDefaultEnableBeta: true,
+ });
+ fetchManifest.mockResolvedValue(updateManifest('0.0.66', sharedHotfix));
+ await expect(service.checkForUpdate()).resolves.toBe('ready');
+ expect(service.getUpdateStatus()).toBe('ready');
+ logInfo.mockClear();
+ ipcListeners.get('update-relaunch')?.({}, 'dark');
+ expect(logInfo.mock.calls.map((call) => String(call[0])).join('\n')).not.toContain(
+ 'executeRelaunch() aborted',
+ );
+ expect(logInfo.mock.calls.map((call) => String(call[0])).join('\n')).toContain(
+ 'executeRelaunch() called',
+ );
+ expect(
+ fs.readFileSync(path.join(TEST_USER_DATA, 'updates', 'patch-info.json'), 'utf-8'),
+ ).toContain('0.0.66');
+ } finally {
+ exitSpy.mockRestore();
+ releaseProbe?.(true);
+ service.stopUpdateService();
+ }
+ });
+
+ it('does not treat a failed settings write as a committed channel change', 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;
+ }),
+ );
+ });
+ let rejectWrite: ((error: Error) => void) | undefined;
+ writeEnableBeta.mockImplementation(
+ () =>
+ new Promise<undefined>((_resolve, reject) => {
+ rejectWrite = reject;
+ }),
+ );
+ try {
+ await probeStarted;
+ const setHandler = ipcHandlers.get('update-channel-settings-set');
+ expect(setHandler).toBeTypeOf('function');
+ const writePromise = setHandler?.({ sender: { id: 1 } }, { enableBeta: true });
+ await vi.waitFor(() => {
+ expect(rejectWrite).toBeTypeOf('function');
+ });
+ rejectWrite?.(new Error('lock timeout'));
+ await expect(writePromise).rejects.toThrow();
+ expect(fs.existsSync(path.join(TEST_USER_DATA, 'updates', 'patch-info.json'))).toBe(true);
+ releaseProbe?.(true);
+ await vi.waitFor(() => {
+ expect(service.getUpdateStatus()).toBe('ready');
+ });
+ expect(fs.existsSync(path.join(TEST_USER_DATA, 'updates', 'patch-info.json'))).toBe(true);
+ } finally {
+ service.stopUpdateService();
+ }
+ });
+
it('releases a pending hold when the org-default write throws', async () => {
const service = await bootWithStagedPatch({ enabled: true });
let releaseProbe: ((busy: boolean) => void) | undefined;
diff --git a/apps/desktop/src/main/updateService.ts b/apps/desktop/src/main/updateService.ts
index 5c48e77771..d6f820f306 100644
--- a/apps/desktop/src/main/updateService.ts
+++ b/apps/desktop/src/main/updateService.ts
@@ -372,7 +372,7 @@ async function requestAutoRelaunch(
return { accepted: false, blockReason: 'not-ready' };
}
- if (deferredStagedPatch) {
+ if (shouldAbortStagedPatchApply()) {
log.info('auto relaunch aborted — update channel changed during eligibility check');
return { accepted: false, blockReason: 'not-ready' };
}
@@ -647,6 +647,7 @@ function invalidateInFlightChannelDownloads(): void {
}
function syncObservedUpdateChannel(): boolean {
+ if (pendingChannelChangeHolds > 0) return false;
const enableBeta = readUpdateChannelSettings().enableBeta;
if (enableBeta === observedEnableBeta) return false;
observedEnableBeta = enableBeta;
@@ -654,6 +655,12 @@ function syncObservedUpdateChannel(): boolean {
return true;
}
+function shouldAbortStagedPatchApply(): boolean {
+ if (pendingChannelChangeHolds > 0) return true;
+ if (!deferredStagedPatch) return false;
+ return !isCurrentPatchNewerThanDeferred(deferredStagedPatch);
+}
+
function rememberDeferredStagedPatch(): void {
deferredStagedPatch = {
path: readyFilePath ?? deferredStagedPatch?.path,
@@ -721,6 +728,7 @@ function discardStagedPatchFiles(): void {
function flushDeferredStagedPatchClear(): void {
if (!deferredStagedPatch) return;
if (isUpdateApplyCommitted() || autoRelaunchDecisionDepth > 0) return;
+ if (pendingChannelChangeHolds > 0) return;
const stale = deferredStagedPatch;
if (currentStatus === 'downloading' || currentStatus === 'superseding') {
// 新渠道下载还在飞:不要再推进代际,也不要删同路径 dest。
@@ -1362,14 +1370,22 @@ function executeRelaunch(theme: 'light' | 'dark'): void {
return;
}
- if (syncObservedUpdateChannel() || deferredStagedPatch) {
+ if (syncObservedUpdateChannel() || shouldAbortStagedPatchApply()) {
+ const pendingHold = pendingChannelChangeHolds > 0;
log.info(
- deferredStagedPatch
+ pendingHold || shouldAbortStagedPatchApply()
? 'executeRelaunch() aborted — update channel changed during eligibility check'
: 'executeRelaunch() aborted — shared update channel changed',
);
isRelaunching = false;
autoRelaunchInProgress = false;
+ // 写入还没落盘,或当前已经是新渠道补丁:只拦住 apply,别把 zip 清掉。
+ if (
+ pendingHold
+ || (deferredStagedPatch && isCurrentPatchNewerThanDeferred(deferredStagedPatch))
+ ) {
+ return;
+ }
// 标志先放下,否则 clearStagedPatch 会当成 apply 已提交而跳过。
clearStagedPatch();
return;