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 honor late opt-out

guyonge6bcd1a1db07
diff --git a/apps/desktop/src/main/__tests__/authLoginFlowReset.test.ts b/apps/desktop/src/main/__tests__/authLoginFlowReset.test.ts
index 241cddd45a..fcb222c73c 100644
--- a/apps/desktop/src/main/__tests__/authLoginFlowReset.test.ts
+++ b/apps/desktop/src/main/__tests__/authLoginFlowReset.test.ts
@@ -315,7 +315,7 @@ describe('auth login-flow reset', () => {
const betaBody = source.slice(betaStart, betaEnd);
expect(betaBody).toContain('if (isPassiveSharedUserDataInstance()) return;');
expect(betaBody).toContain('decodeAccessTokenOrgSlug(accessToken)');
- expect(betaBody).toContain('enableUncustomizedBetaChannel()');
+ expect(betaBody).toContain('enableUncustomizedBetaChannel');
expect(source).not.toContain('relaunchForChannelChange');
const clearIntegrationsStart = source.indexOf('async function clearPerAccountIntegrations(');
diff --git a/apps/desktop/src/main/__tests__/updateChannelStore.test.ts b/apps/desktop/src/main/__tests__/updateChannelStore.test.ts
index 3d230a8794..c80f6b311b 100644
--- a/apps/desktop/src/main/__tests__/updateChannelStore.test.ts
+++ b/apps/desktop/src/main/__tests__/updateChannelStore.test.ts
@@ -39,7 +39,7 @@ afterEach(() => {
fs.rmSync(tempDir, { recursive: true, force: true });
});
-describe('tryEnableUncustomizedBeta', () => {
+describe('tryEnableUncustomizedBetaAtomic', () => {
it('turns beta on via org default without writing a user enableBeta override', async () => {
const store = await loadStore();
@@ -47,7 +47,7 @@ describe('tryEnableUncustomizedBeta', () => {
value: { enableBeta: false, orgDefaultEnableBeta: false },
customizedKeys: [],
});
- expect(store.tryEnableUncustomizedBeta()).toBe(true);
+ expect(await store.tryEnableUncustomizedBetaAtomic()).toBe(true);
expect(store.readUpdateChannelSettings()).toEqual({
enableBeta: true,
orgDefaultEnableBeta: true,
@@ -63,7 +63,7 @@ describe('tryEnableUncustomizedBeta', () => {
expect(store.readUpdateChannelSettings()).toMatchObject({ enableBeta: false });
expect(store.isEnableBetaUserCustomized()).toBe(true);
- expect(store.tryEnableUncustomizedBeta()).toBe(false);
+ expect(await store.tryEnableUncustomizedBetaAtomic()).toBe(false);
expect(store.readUpdateChannelSettings().enableBeta).toBe(false);
});
@@ -72,7 +72,7 @@ describe('tryEnableUncustomizedBeta', () => {
store.writeEnableBeta(false);
expect(store.isEnableBetaUserCustomized()).toBe(true);
- expect(store.tryEnableUncustomizedBeta()).toBe(false);
+ expect(await store.tryEnableUncustomizedBetaAtomic()).toBe(false);
expect(store.readUpdateChannelSettings().enableBeta).toBe(false);
});
@@ -80,7 +80,7 @@ describe('tryEnableUncustomizedBeta', () => {
const store = await loadStore();
store.writeEnableBeta(true);
- expect(store.tryEnableUncustomizedBeta()).toBe(false);
+ expect(await store.tryEnableUncustomizedBetaAtomic()).toBe(false);
expect(store.readUpdateChannelSettings().enableBeta).toBe(true);
expect(store.isEnableBetaUserCustomized()).toBe(true);
});
diff --git a/apps/desktop/src/main/__tests__/updateService.test.ts b/apps/desktop/src/main/__tests__/updateService.test.ts
index 766e50cfa0..33ad309006 100644
--- a/apps/desktop/src/main/__tests__/updateService.test.ts
+++ b/apps/desktop/src/main/__tests__/updateService.test.ts
@@ -70,7 +70,7 @@ vi.mock('../auto-update-settings-store', () => ({
writeAutoRelaunchOnIdle: vi.fn(),
}));
-const tryEnableUncustomizedBeta = vi.fn(() => true);
+const tryEnableUncustomizedBetaAtomic = vi.fn(async () => true);
const readUpdateChannelSettings = vi.fn(() => ({
enableBeta: false,
orgDefaultEnableBeta: false,
@@ -93,7 +93,7 @@ vi.mock('../updateChannelStore', () => ({
}),
resetUpdateChannelSettings: () => ({ enableBeta: false, orgDefaultEnableBeta: false }),
writeEnableBeta: vi.fn(),
- tryEnableUncustomizedBeta,
+ tryEnableUncustomizedBetaAtomic,
isEnableBetaUserCustomized: () => false,
isBetaChannelEnabled: () => readUpdateChannelSettings().enableBeta === true,
}));
@@ -180,8 +180,8 @@ beforeEach(() => {
isDev.mockReset();
isDev.mockReturnValue(false);
download.mockReset();
- tryEnableUncustomizedBeta.mockReset();
- tryEnableUncustomizedBeta.mockReturnValue(true);
+ tryEnableUncustomizedBetaAtomic.mockReset();
+ tryEnableUncustomizedBetaAtomic.mockResolvedValue(true);
readUpdateChannelSettings.mockReset();
readUpdateChannelSettings.mockReturnValue({
enableBeta: false,
@@ -514,7 +514,7 @@ describe('startup update relaunch safety', () => {
try {
await probeStarted;
expect(service.getUpdateStatus()).toBe('ready');
- expect(service.enableUncustomizedBetaChannel()).toBe(true);
+ await expect(service.enableUncustomizedBetaChannel()).resolves.toBe(true);
expect(service.getUpdateStatus()).toBe('ready');
} finally {
releaseProbe?.(true);
@@ -536,7 +536,7 @@ describe('startup update relaunch safety', () => {
});
try {
await probeStarted;
- expect(service.enableUncustomizedBetaChannel()).toBe(true);
+ await expect(service.enableUncustomizedBetaChannel()).resolves.toBe(true);
expect(service.getUpdateStatus()).toBe('ready');
releaseProbe?.(true);
await vi.waitFor(() => {
@@ -561,7 +561,7 @@ describe('startup update relaunch safety', () => {
});
try {
await probeStarted;
- expect(service.enableUncustomizedBetaChannel()).toBe(true);
+ await expect(service.enableUncustomizedBetaChannel()).resolves.toBe(true);
expect(service.getUpdateStatus()).toBe('ready');
releaseProbe?.(false);
await vi.waitFor(() => {
diff --git a/apps/desktop/src/main/__tests__/xdOrgBetaDefault.test.ts b/apps/desktop/src/main/__tests__/xdOrgBetaDefault.test.ts
index 7e8d8c9b3d..cd916efa2d 100644
--- a/apps/desktop/src/main/__tests__/xdOrgBetaDefault.test.ts
+++ b/apps/desktop/src/main/__tests__/xdOrgBetaDefault.test.ts
@@ -49,7 +49,7 @@ function makeDeps(
probeBetaManifest: overrides.probeThrows
? vi.fn().mockRejectedValue(new Error('offline'))
: vi.fn().mockResolvedValue(overrides.available !== false),
- enableBeta: vi.fn(),
+ enableBeta: vi.fn().mockResolvedValue(true),
};
}
diff --git a/apps/desktop/src/main/authManager.ts b/apps/desktop/src/main/authManager.ts
index b080c4ec48..8846475d19 100644
--- a/apps/desktop/src/main/authManager.ts
+++ b/apps/desktop/src/main/authManager.ts
@@ -1473,9 +1473,7 @@ function scheduleXdOrgBetaDefault(input: {
isCustomized: isEnableBetaUserCustomized(),
}),
probeBetaManifest,
- enableBeta: () => {
- enableUncustomizedBetaChannel();
- },
+ enableBeta: () => enableUncustomizedBetaChannel(),
},
)
.then((outcome) => {
diff --git a/apps/desktop/src/main/updateChannelStore.ts b/apps/desktop/src/main/updateChannelStore.ts
index d02bc79d18..ca440b9a6e 100644
--- a/apps/desktop/src/main/updateChannelStore.ts
+++ b/apps/desktop/src/main/updateChannelStore.ts
@@ -105,15 +105,26 @@ export function isEnableBetaUserCustomized(): boolean {
* 用户手动关过(enableBeta 键存在)后必须保持关。
* 返回是否实际把有效值从关写成开。
*/
-export function tryEnableUncustomizedBeta(): boolean {
- const state = store.readState();
- if (state.customizedKeys.includes('enableBeta') || state.value.enableBeta) return false;
- store.writePatch({ orgDefaultEnableBeta: true });
- log.info('beta update channel setting written', {
- enableBeta: true,
- source: 'xd-org-default',
+/**
+ * 跨进程锁内现读再决定是否打开组织默认。
+ * probe 前后另一实例可能已经写下用户关闭,不能用过期的 isCustomized 缓存覆盖。
+ */
+export async function tryEnableUncustomizedBetaAtomic(): Promise<boolean> {
+ let wrote = false;
+ await store.updateAtomic((current) => {
+ if (current.customizedKeys.includes('enableBeta') || current.value.enableBeta) {
+ return {};
+ }
+ wrote = true;
+ return { orgDefaultEnableBeta: true };
});
- return true;
+ if (wrote) {
+ log.info('beta update channel setting written', {
+ enableBeta: true,
+ source: 'xd-org-default',
+ });
+ }
+ return wrote;
}
export function resetUpdateChannelSettings(): UpdateChannelSettings {
diff --git a/apps/desktop/src/main/updateService.ts b/apps/desktop/src/main/updateService.ts
index dbcfa67348..f84b93118a 100644
--- a/apps/desktop/src/main/updateService.ts
+++ b/apps/desktop/src/main/updateService.ts
@@ -44,7 +44,7 @@ import {
isEnableBetaUserCustomized,
readUpdateChannelSettings,
resetUpdateChannelSettings,
- tryEnableUncustomizedBeta,
+ tryEnableUncustomizedBetaAtomic,
writeEnableBeta,
} from './updateChannelStore';
import { assertTrustedAppRendererEvent } from './security/trustedAppRenderer';
@@ -155,8 +155,11 @@ let lastErrorCode: string | undefined;
let autoRelaunchInProgress = false;
/** 资格检查(含异步 busyProbe)进行中的次数。这段窗口暂缓清补丁。 */
let autoRelaunchDecisionDepth = 0;
-/** 资格检查期间有人要求作废旧渠道补丁,检查结束后若没真正开始应用再补清。 */
-let stagedPatchClearDeferred = false;
+/**
+ * 资格检查期间要求作废的那份旧补丁路径。
+ * 只清这份文件,避免误删检查窗口里新渠道刚写下的 readyFilePath。
+ */
+let deferredStagedPatchPath: string | undefined;
let lastAutoRelaunchBlockReason: AutoRelaunchBlockReason | null = null;
let lastBusyAtMs: number | null = null;
let lastResumeAtMs: number | null = null;
@@ -348,7 +351,7 @@ async function requestAutoRelaunch(
return { accepted: false, blockReason };
}
- if (stagedPatchClearDeferred) {
+ if (deferredStagedPatchPath) {
log.info('auto relaunch aborted — update channel changed during eligibility check');
return { accepted: false, blockReason: 'not-ready' };
}
@@ -609,9 +612,15 @@ function invalidateInFlightChannelDownloads(): void {
}
function flushDeferredStagedPatchClear(): void {
- if (!stagedPatchClearDeferred) return;
+ if (!deferredStagedPatchPath) return;
if (isUpdateApplyCommitted() || autoRelaunchDecisionDepth > 0) return;
- stagedPatchClearDeferred = false;
+ const stalePath = deferredStagedPatchPath;
+ deferredStagedPatchPath = undefined;
+ if (readyFilePath && readyFilePath !== stalePath) {
+ log.info('keeping newer staged patch after deferred channel-change clear');
+ try { fs.unlinkSync(stalePath); } catch { /* ignore */ }
+ return;
+ }
clearStagedPatch();
}
@@ -628,7 +637,7 @@ function clearStagedPatch(): void {
return;
}
if (autoRelaunchDecisionDepth > 0) {
- stagedPatchClearDeferred = true;
+ deferredStagedPatchPath = readyFilePath ?? deferredStagedPatchPath;
// 立刻作废旧渠道 in-flight 下载,避免下载完成后又把旧补丁写成 ready。
invalidateInFlightChannelDownloads();
log.info('deferring staged patch clear until auto-relaunch eligibility settles');
@@ -1537,9 +1546,9 @@ export function initUpdateService(): void {
* 渠道从关变开时作废已 staged 的旧渠道补丁,与设置页手动打开同一口径。
* 不 relaunch:本次进程继续走当前通道,下次冷启动 / 用户自行重启再生效。
*/
-export function enableUncustomizedBetaChannel(): boolean {
+export async function enableUncustomizedBetaChannel(): Promise<boolean> {
const wasBeta = readUpdateChannelSettings().enableBeta;
- const wrote = tryEnableUncustomizedBeta();
+ const wrote = await tryEnableUncustomizedBetaAtomic();
if (wrote && !wasBeta) {
// 真正开始应用时 clearStagedPatch 会跳过;资格检查中会记下,结束后再补清。
clearStagedPatch();
diff --git a/apps/desktop/src/main/xdOrgBetaDefault.ts b/apps/desktop/src/main/xdOrgBetaDefault.ts
index df8c17a7f1..4e7d082133 100644
--- a/apps/desktop/src/main/xdOrgBetaDefault.ts
+++ b/apps/desktop/src/main/xdOrgBetaDefault.ts
@@ -38,7 +38,7 @@ export interface XdOrgBetaDefaultDeps {
readCurrentAuthIdentity(): { authEpoch: number; userId: string | null };
readChannelState(): XdOrgBetaChannelState;
probeBetaManifest(): Promise<boolean>;
- enableBeta(): void;
+ enableBeta(): boolean | Promise<boolean>;
}
export type XdOrgBetaDefaultOutcome =
@@ -102,6 +102,11 @@ export async function maybeEnableXdOrgBetaDefault(
return { kind: 'skipped', reason: 'stale-auth' };
}
- deps.enableBeta();
+ const wrote = await deps.enableBeta();
+ if (!wrote) {
+ const latest = deps.readChannelState();
+ if (latest.enableBeta) return { kind: 'skipped', reason: 'already-enabled' };
+ return { kind: 'skipped', reason: 'user-customized' };
+ }
return { kind: 'enabled' };
}