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 org beta default off the user override

guyong1e20dee01d02
diff --git a/apps/desktop/src/main/__tests__/updateChannelStore.test.ts b/apps/desktop/src/main/__tests__/updateChannelStore.test.ts
index edc55eb7d4..3d230a8794 100644
--- a/apps/desktop/src/main/__tests__/updateChannelStore.test.ts
+++ b/apps/desktop/src/main/__tests__/updateChannelStore.test.ts
@@ -40,18 +40,20 @@ afterEach(() => {
});
describe('tryEnableUncustomizedBeta', () => {
- it('writes enableBeta=true when the device has never customized the switch', async () => {
+ it('turns beta on via org default without writing a user enableBeta override', async () => {
const store = await loadStore();
expect(store.readUpdateChannelSettingsState()).toMatchObject({
- value: { enableBeta: false },
- isCustomized: false,
+ value: { enableBeta: false, orgDefaultEnableBeta: false },
+ customizedKeys: [],
});
expect(store.tryEnableUncustomizedBeta()).toBe(true);
- expect(store.readUpdateChannelSettingsState()).toMatchObject({
- value: { enableBeta: true },
- isCustomized: true,
+ expect(store.readUpdateChannelSettings()).toEqual({
+ enableBeta: true,
+ orgDefaultEnableBeta: true,
});
+ expect(store.isEnableBetaUserCustomized()).toBe(false);
+ expect(store.readUpdateChannelSettingsState().customizedKeys).toEqual(['orgDefaultEnableBeta']);
});
it('does not reopen beta after the user turned it off', async () => {
@@ -59,23 +61,19 @@ describe('tryEnableUncustomizedBeta', () => {
store.writeEnableBeta(true);
store.writeEnableBeta(false);
- expect(store.readUpdateChannelSettingsState()).toMatchObject({
- value: { enableBeta: false },
- isCustomized: true,
- });
+ expect(store.readUpdateChannelSettings()).toMatchObject({ enableBeta: false });
+ expect(store.isEnableBetaUserCustomized()).toBe(true);
expect(store.tryEnableUncustomizedBeta()).toBe(false);
- expect(store.readUpdateChannelSettings()).toEqual({ enableBeta: false });
+ expect(store.readUpdateChannelSettings().enableBeta).toBe(false);
});
- it('keeps a never-enabled opt-out as customized', async () => {
+ it('keeps a never-enabled opt-out as a user choice', async () => {
const store = await loadStore();
store.writeEnableBeta(false);
- expect(store.readUpdateChannelSettingsState()).toMatchObject({
- value: { enableBeta: false },
- isCustomized: true,
- });
+ expect(store.isEnableBetaUserCustomized()).toBe(true);
expect(store.tryEnableUncustomizedBeta()).toBe(false);
+ expect(store.readUpdateChannelSettings().enableBeta).toBe(false);
});
it('is a no-op when beta is already on', async () => {
@@ -83,6 +81,7 @@ describe('tryEnableUncustomizedBeta', () => {
store.writeEnableBeta(true);
expect(store.tryEnableUncustomizedBeta()).toBe(false);
- expect(store.readUpdateChannelSettings()).toEqual({ enableBeta: true });
+ expect(store.readUpdateChannelSettings().enableBeta).toBe(true);
+ expect(store.isEnableBetaUserCustomized()).toBe(true);
});
});
diff --git a/apps/desktop/src/main/authManager.ts b/apps/desktop/src/main/authManager.ts
index 91124f9c37..b080c4ec48 100644
--- a/apps/desktop/src/main/authManager.ts
+++ b/apps/desktop/src/main/authManager.ts
@@ -43,7 +43,7 @@ import {
} from '@cindy/auth-client';
import { readReloginFlag, clearReloginFlag, enableUncustomizedBetaChannel } from './updateService';
import { probeBetaManifest } from './manifestService';
-import { readUpdateChannelSettingsState } from './updateChannelStore';
+import { isEnableBetaUserCustomized, readUpdateChannelSettings } from './updateChannelStore';
import * as canaryFlagStore from './canaryFlagStore';
import { decodeAccessTokenOrgSlug } from './authTokenClaims';
import { getProviderSecretStore } from './secrets/providerSecretStore.js';
@@ -1468,13 +1468,10 @@ function scheduleXdOrgBetaDefault(input: {
authEpoch: authStateEpoch,
userId: currentUser?.id ?? null,
}),
- readChannelState: () => {
- const state = readUpdateChannelSettingsState();
- return {
- enableBeta: state.value.enableBeta,
- isCustomized: state.isCustomized,
- };
- },
+ readChannelState: () => ({
+ enableBeta: readUpdateChannelSettings().enableBeta,
+ isCustomized: isEnableBetaUserCustomized(),
+ }),
probeBetaManifest,
enableBeta: () => {
enableUncustomizedBetaChannel();
diff --git a/apps/desktop/src/main/updateChannelStore.ts b/apps/desktop/src/main/updateChannelStore.ts
index f6ee7fdad9..d02bc79d18 100644
--- a/apps/desktop/src/main/updateChannelStore.ts
+++ b/apps/desktop/src/main/updateChannelStore.ts
@@ -10,10 +10,11 @@
* 所以这里用 createOverrideSettingsFile(与 auto-update-settings 同一套
* override 语义:默认值 + 用户 override、恢复默认只删 override),而不是仿
* canaryFlagStore 的裸 JSON。
- * - xd 组织登录后可由 authManager 在「尚未自定义」时补一次默认打开;用户
- * 手动关过(isCustomized)后重启 / 重登都不再打开。
+ * - xd 组织登录后可由 authManager 在「用户没拨过开关」时补一次默认打开;
+ * 用户手动关过(enableBeta override 存在)后重启 / 重登都不再打开。
*
- * 落盘:userData/update-channel-settings.json,字段 { enableBeta: boolean }。
+ * 落盘:userData/update-channel-settings.json。用户拨动写 enableBeta;
+ * 组织默认写 orgDefaultEnableBeta,两者分开。
* 默认关闭。manifestService.fetchManifest() 用 resolveUpdateChannel 把本开关与
* canaryFlagStore.read() 收敛成最终发布通道(优先级 canary > beta > release)。
*/
@@ -31,10 +32,16 @@ const log = desktopMakerLogger.child('update-channel-settings');
export interface UpdateChannelSettings {
enableBeta: boolean;
+ /**
+ * xd 组织默认打开留下的标记,不是用户拨动。
+ * 有效值:用户写过 enableBeta 用用户值,否则看这个标记。
+ */
+ orgDefaultEnableBeta: boolean;
}
const DEFAULTS: UpdateChannelSettings = {
enableBeta: false,
+ orgDefaultEnableBeta: false,
};
function settingsFilePath(): string {
@@ -45,8 +52,21 @@ function normalize(raw: unknown): UpdateChannelSettings {
if (!raw || typeof raw !== 'object') return { ...DEFAULTS };
const r = raw as Record<string, unknown>;
return {
- enableBeta:
- typeof r.enableBeta === 'boolean' ? r.enableBeta : DEFAULTS.enableBeta,
+ // 这里只还原落盘字段。override store 读盘时会把 defaults 和 overrides 先
+ // 合并再交给 normalize,enableBeta:false 分不清是默认还是用户关过,有效值
+ // 在 resolveEffectiveSettings 里按 customizedKeys 算。
+ enableBeta: typeof r.enableBeta === 'boolean' ? r.enableBeta : DEFAULTS.enableBeta,
+ orgDefaultEnableBeta: r.orgDefaultEnableBeta === true,
+ };
+}
+
+function resolveEffectiveSettings(
+ state: OverrideSettingsState<UpdateChannelSettings>,
+): UpdateChannelSettings {
+ const userCustomized = state.customizedKeys.includes('enableBeta');
+ return {
+ enableBeta: userCustomized ? state.value.enableBeta : state.value.orgDefaultEnableBeta,
+ orgDefaultEnableBeta: state.value.orgDefaultEnableBeta,
};
}
@@ -59,7 +79,7 @@ const store = createOverrideSettingsFile<UpdateChannelSettings>({
});
export function readUpdateChannelSettings(): UpdateChannelSettings {
- return store.read();
+ return resolveEffectiveSettings(store.readState());
}
export function readUpdateChannelSettingsState(): OverrideSettingsState<UpdateChannelSettings> {
@@ -68,22 +88,31 @@ export function readUpdateChannelSettingsState(): OverrideSettingsState<UpdateCh
export function writeEnableBeta(enableBeta: boolean): void {
// 关 beta 的值等于系统默认 false。若不 preserveDefaults,override 会被删掉,
- // isCustomized 变回 false,xd 组织下次登录又会把开关默认打开,盖掉用户的
- // 手动关闭。设置页每次拨动都算显式自定义。
+ // 用户键消失后会被当成未自定义,xd 组织下次登录又会默认打开。
+ // 设置页每次拨动都写 enableBeta,与组织默认标记分开。
store.writePatch({ enableBeta }, { preserveDefaults: true });
log.info('beta update channel setting written', { enableBeta });
}
+/** 用户是否显式拨过 beta 开关。组织默认写入不算。 */
+export function isEnableBetaUserCustomized(): boolean {
+ return store.readState().customizedKeys.includes('enableBeta');
+}
+
/**
- * 仅在用户从没改过这个开关时打开 beta。
- * 给 xd 组织默认打开用:用户手动关过(isCustomized)后必须保持关。
- * 返回是否实际写入了 true。
+ * 仅在用户从没拨过这个开关时打开 beta。
+ * 写入 orgDefaultEnableBeta,不写 enableBeta,避免把组织默认伪装成用户 override。
+ * 用户手动关过(enableBeta 键存在)后必须保持关。
+ * 返回是否实际把有效值从关写成开。
*/
export function tryEnableUncustomizedBeta(): boolean {
const state = store.readState();
- if (state.value.enableBeta || state.isCustomized) return false;
- store.writePatch({ enableBeta: true });
- log.info('beta update channel setting written', { enableBeta: true, source: 'xd-org-default' });
+ 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',
+ });
return true;
}
diff --git a/apps/desktop/src/main/updateService.ts b/apps/desktop/src/main/updateService.ts
index 20fbd817fe..68641ba29c 100644
--- a/apps/desktop/src/main/updateService.ts
+++ b/apps/desktop/src/main/updateService.ts
@@ -41,8 +41,8 @@ import {
writeAutoRelaunchOnIdle,
} from './auto-update-settings-store';
import {
+ isEnableBetaUserCustomized,
readUpdateChannelSettings,
- readUpdateChannelSettingsState,
resetUpdateChannelSettings,
tryEnableUncustomizedBeta,
writeEnableBeta,
@@ -559,12 +559,21 @@ function removePatchInfo(): void {
try { fs.unlinkSync(path.join(getUpdatesDir(), PATCH_INFO_FILE)); } catch { /* ignore */ }
}
+function isUpdateApplyInFlight(): boolean {
+ return isRelaunching || autoRelaunchInProgress;
+}
+
/**
* 清掉已 staged 的补丁(ready 态):删 zip + patch-info.json,状态回 idle。
- * 只在「用户关掉 beta 开关」时调用——opt-out 后不能仍让 staged 的 beta patch
- * 在下一次启动/后台轮询里被装上去(切渠道不等于切版本,必须把旧渠道的补丁作废)。
+ * 切渠道时调用——opt-out 后不能仍让 staged 的旧渠道 patch 在下次启动/后台
+ * 轮询里被装上去(切渠道不等于切版本,必须把旧渠道的补丁作废)。
+ * 自动重启已经过资格检查时不能清,否则 executeRelaunch 会走 no_ready_file。
*/
function clearStagedPatch(): void {
+ if (isUpdateApplyInFlight()) {
+ log.info('skipping staged patch clear — update apply already in flight');
+ return;
+ }
if (readyFilePath) {
try { fs.unlinkSync(readyFilePath); } catch { /* ignore */ }
}
@@ -1255,8 +1264,10 @@ export function initUpdateService(): void {
// 更新设置或重启应用(旧 update-auto-settings 没断言是历史债,不构成豁免)。
ipcMain.handle('update-channel-settings-get', (event) => {
assertTrustedAppRendererEvent(event);
- const state = readUpdateChannelSettingsState();
- return { enableBeta: state.value.enableBeta, isCustomized: state.isCustomized };
+ return {
+ enableBeta: readUpdateChannelSettings().enableBeta,
+ isCustomized: isEnableBetaUserCustomized(),
+ };
});
ipcMain.handle('update-channel-settings-set', (event, payload: unknown) => {
@@ -1278,8 +1289,10 @@ export function initUpdateService(): void {
if (wasBeta !== next) {
clearStagedPatch();
}
- const state = readUpdateChannelSettingsState();
- return { enableBeta: state.value.enableBeta, isCustomized: state.isCustomized };
+ return {
+ enableBeta: readUpdateChannelSettings().enableBeta,
+ isCustomized: isEnableBetaUserCustomized(),
+ };
});
ipcMain.handle('update-channel-settings-reset', (event) => {
@@ -1290,8 +1303,10 @@ export function initUpdateService(): void {
if (wasBeta) {
clearStagedPatch();
}
- const state = readUpdateChannelSettingsState();
- return { enableBeta: state.value.enableBeta, isCustomized: state.isCustomized };
+ return {
+ enableBeta: readUpdateChannelSettings().enableBeta,
+ isCustomized: isEnableBetaUserCustomized(),
+ };
});
// 打开 beta 前的预检:探测 manifest-{platform}-beta.json 是否可达。
@@ -1480,7 +1495,13 @@ export function enableUncustomizedBetaChannel(): boolean {
const wasBeta = readUpdateChannelSettings().enableBeta;
const wrote = tryEnableUncustomizedBeta();
if (wrote && !wasBeta) {
- clearStagedPatch();
+ // 自动重启已经过资格检查、即将 executeRelaunch 时不能清补丁,
+ // 否则 readyFilePath 被拆掉会走 no_ready_file / updater_spawn_failed。
+ if (isUpdateApplyInFlight()) {
+ log.info('xd org beta default enabled — leaving in-flight update patch in place');
+ } else {
+ clearStagedPatch();
+ }
}
return wrote;
}
diff --git a/apps/desktop/src/main/xdOrgBetaDefault.ts b/apps/desktop/src/main/xdOrgBetaDefault.ts
index 33386a92fe..df8c17a7f1 100644
--- a/apps/desktop/src/main/xdOrgBetaDefault.ts
+++ b/apps/desktop/src/main/xdOrgBetaDefault.ts
@@ -24,6 +24,7 @@ export interface XdOrgBetaUser {
export interface XdOrgBetaChannelState {
enableBeta: boolean;
+ /** 用户是否显式拨过开关。组织默认写入不算。 */
isCustomized: boolean;
}