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): lock user beta writes and recheck identity

guyongde9eb286c750
diff --git a/apps/desktop/src/main/__tests__/authLoginFlowReset.test.ts b/apps/desktop/src/main/__tests__/authLoginFlowReset.test.ts
index fcb222c73c..8008a9f3c7 100644
--- a/apps/desktop/src/main/__tests__/authLoginFlowReset.test.ts
+++ b/apps/desktop/src/main/__tests__/authLoginFlowReset.test.ts
@@ -316,6 +316,7 @@ describe('auth login-flow reset', () => {
expect(betaBody).toContain('if (isPassiveSharedUserDataInstance()) return;');
expect(betaBody).toContain('decodeAccessTokenOrgSlug(accessToken)');
expect(betaBody).toContain('enableUncustomizedBetaChannel');
+ expect(betaBody).toContain('authStateEpoch === input.expectedAuthEpoch');
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 c80f6b311b..d55ab37187 100644
--- a/apps/desktop/src/main/__tests__/updateChannelStore.test.ts
+++ b/apps/desktop/src/main/__tests__/updateChannelStore.test.ts
@@ -58,8 +58,8 @@ describe('tryEnableUncustomizedBetaAtomic', () => {
it('does not reopen beta after the user turned it off', async () => {
const store = await loadStore();
- store.writeEnableBeta(true);
- store.writeEnableBeta(false);
+ await store.writeEnableBeta(true);
+ await store.writeEnableBeta(false);
expect(store.readUpdateChannelSettings()).toMatchObject({ enableBeta: false });
expect(store.isEnableBetaUserCustomized()).toBe(true);
@@ -69,7 +69,7 @@ describe('tryEnableUncustomizedBetaAtomic', () => {
it('keeps a never-enabled opt-out as a user choice', async () => {
const store = await loadStore();
- store.writeEnableBeta(false);
+ await store.writeEnableBeta(false);
expect(store.isEnableBetaUserCustomized()).toBe(true);
expect(await store.tryEnableUncustomizedBetaAtomic()).toBe(false);
@@ -78,10 +78,19 @@ describe('tryEnableUncustomizedBetaAtomic', () => {
it('is a no-op when beta is already on', async () => {
const store = await loadStore();
- store.writeEnableBeta(true);
+ await store.writeEnableBeta(true);
expect(await store.tryEnableUncustomizedBetaAtomic()).toBe(false);
expect(store.readUpdateChannelSettings().enableBeta).toBe(true);
expect(store.isEnableBetaUserCustomized()).toBe(true);
});
+
+ it('does not write when the lock-time identity guard rejects', async () => {
+ const store = await loadStore();
+ expect(await store.tryEnableUncustomizedBetaAtomic(() => false)).toBe(false);
+ expect(store.readUpdateChannelSettings()).toEqual({
+ enableBeta: false,
+ orgDefaultEnableBeta: false,
+ });
+ });
});
diff --git a/apps/desktop/src/main/authManager.ts b/apps/desktop/src/main/authManager.ts
index 8846475d19..8e1851d89d 100644
--- a/apps/desktop/src/main/authManager.ts
+++ b/apps/desktop/src/main/authManager.ts
@@ -1473,7 +1473,11 @@ function scheduleXdOrgBetaDefault(input: {
isCustomized: isEnableBetaUserCustomized(),
}),
probeBetaManifest,
- enableBeta: () => enableUncustomizedBetaChannel(),
+ enableBeta: () =>
+ enableUncustomizedBetaChannel(
+ () =>
+ authStateEpoch === input.expectedAuthEpoch && currentUser?.id === input.expectedUserId,
+ ),
},
)
.then((outcome) => {
diff --git a/apps/desktop/src/main/updateChannelStore.ts b/apps/desktop/src/main/updateChannelStore.ts
index ca440b9a6e..add6008ba6 100644
--- a/apps/desktop/src/main/updateChannelStore.ts
+++ b/apps/desktop/src/main/updateChannelStore.ts
@@ -86,11 +86,12 @@ export function readUpdateChannelSettingsState(): OverrideSettingsState<UpdateCh
return store.readState();
}
-export function writeEnableBeta(enableBeta: boolean): void {
+export async function writeEnableBeta(enableBeta: boolean): Promise<void> {
// 关 beta 的值等于系统默认 false。若不 preserveDefaults,override 会被删掉,
// 用户键消失后会被当成未自定义,xd 组织下次登录又会默认打开。
// 设置页每次拨动都写 enableBeta,与组织默认标记分开。
- store.writePatch({ enableBeta }, { preserveDefaults: true });
+ // 和 tryEnableUncustomizedBetaAtomic 共用同一把跨进程锁,避免默认写入盖掉用户关闭。
+ await store.writePatchAtomic({ enableBeta }, { preserveDefaults: true });
log.info('beta update channel setting written', { enableBeta });
}
@@ -109,10 +110,16 @@ export function isEnableBetaUserCustomized(): boolean {
* 跨进程锁内现读再决定是否打开组织默认。
* probe 前后另一实例可能已经写下用户关闭,不能用过期的 isCustomized 缓存覆盖。
*/
-export async function tryEnableUncustomizedBetaAtomic(): Promise<boolean> {
+export async function tryEnableUncustomizedBetaAtomic(
+ shouldWrite: () => boolean = () => true,
+): Promise<boolean> {
let wrote = false;
await store.updateAtomic((current) => {
- if (current.customizedKeys.includes('enableBeta') || current.value.enableBeta) {
+ if (
+ !shouldWrite() ||
+ current.customizedKeys.includes('enableBeta') ||
+ current.value.enableBeta
+ ) {
return {};
}
wrote = true;
@@ -127,8 +134,8 @@ export async function tryEnableUncustomizedBetaAtomic(): Promise<boolean> {
return wrote;
}
-export function resetUpdateChannelSettings(): UpdateChannelSettings {
- return store.reset();
+export async function resetUpdateChannelSettings(): Promise<UpdateChannelSettings> {
+ return store.resetAtomic();
}
/** manifestService 消费的单一读取入口:返回是否启用 beta(设备级)。 */
diff --git a/apps/desktop/src/main/updateService.ts b/apps/desktop/src/main/updateService.ts
index f84b93118a..abeaea8e85 100644
--- a/apps/desktop/src/main/updateService.ts
+++ b/apps/desktop/src/main/updateService.ts
@@ -1331,7 +1331,7 @@ export function initUpdateService(): void {
return channelSettingsWire();
});
- ipcMain.handle('update-channel-settings-set', (event, payload: unknown) => {
+ ipcMain.handle('update-channel-settings-set', async (event, payload: unknown) => {
assertTrustedAppRendererEvent(event);
if (!payload || typeof payload !== 'object') {
throwIpcError('INVALID_PARAMS', 'update channel settings payload required');
@@ -1341,7 +1341,7 @@ export function initUpdateService(): void {
throwIpcError('INVALID_PARAMS', 'enableBeta required (boolean)');
}
const wasBeta = readUpdateChannelSettings().enableBeta;
- writeEnableBeta(next);
+ await writeEnableBeta(next);
// 渠道一变(无论 opt-in 还是 opt-out)就作废已 staged 的旧渠道补丁:
// - opt-out(beta→release):否则用户关掉 beta 后仍被装到 beta 版本;
// - opt-in(release→beta):否则旧 release 补丁仍 staged,重启后 beta manifest
@@ -1353,10 +1353,10 @@ export function initUpdateService(): void {
return channelSettingsWire();
});
- ipcMain.handle('update-channel-settings-reset', (event) => {
+ ipcMain.handle('update-channel-settings-reset', async (event) => {
assertTrustedAppRendererEvent(event);
const wasBeta = readUpdateChannelSettings().enableBeta;
- resetUpdateChannelSettings();
+ await resetUpdateChannelSettings();
// 恢复默认 = 关闭 beta;同 set(false),渠道变化即作废已 staged 的补丁。
if (wasBeta) {
clearStagedPatch();
@@ -1546,9 +1546,11 @@ export function initUpdateService(): void {
* 渠道从关变开时作废已 staged 的旧渠道补丁,与设置页手动打开同一口径。
* 不 relaunch:本次进程继续走当前通道,下次冷启动 / 用户自行重启再生效。
*/
-export async function enableUncustomizedBetaChannel(): Promise<boolean> {
+export async function enableUncustomizedBetaChannel(
+ shouldWrite: () => boolean = () => true,
+): Promise<boolean> {
const wasBeta = readUpdateChannelSettings().enableBeta;
- const wrote = await tryEnableUncustomizedBetaAtomic();
+ const wrote = await tryEnableUncustomizedBetaAtomic(shouldWrite);
if (wrote && !wasBeta) {
// 真正开始应用时 clearStagedPatch 会跳过;资格检查中会记下,结束后再补清。
clearStagedPatch();