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(desktop): 允许已归属账号在非独占状态保存设置

liugaowei930908fb59c5
diff --git a/apps/desktop/src/renderer/__tests__/modelVisibilityPrefs.test.ts b/apps/desktop/src/renderer/__tests__/modelVisibilityPrefs.test.ts
index 6c223c5af0..76cffb930b 100644
--- a/apps/desktop/src/renderer/__tests__/modelVisibilityPrefs.test.ts
+++ b/apps/desktop/src/renderer/__tests__/modelVisibilityPrefs.test.ts
@@ -187,22 +187,44 @@ describe('modelVisibilityPrefs store', () => {
expect(module.isModelEnabled('codex', 'openai', { id: 'gpt-5.6' })).toBe(false);
});
- it('仲裁暂不可用时不允许 owner 写入覆盖迁移输入,恢复后重试并保留旧 override', async () => {
+ it('已归属但非独占时保存新 override,恢复独占后合并旧值且新值优先', async () => {
memStorage.setItem(
'xdt:modelVisibilityPrefs:v1',
- JSON.stringify({ 'codex:openai:gpt-5.6': false }),
+ JSON.stringify({
+ 'codex:openai:gpt-5.6': false,
+ 'codex:openai:gpt-5.5': true,
+ }),
);
setOwnerClaim('owner-a', 1, true, false);
const module = await loadModuleForOwner();
module.setModelVisibility('codex', 'openai', 'gpt-5.5', false);
- expect(memStorage.getItem('xdt:modelVisibilityPrefs:v1.owner.owner-a')).toBeNull();
+ expect(memStorage.getItem('xdt:modelVisibilityPrefs:v1.owner.owner-a')).toBe(
+ JSON.stringify({ 'codex:openai:gpt-5.5': false }),
+ );
setOwnerClaim('owner-a', 1, true, true);
module.setModelVisibility('codex', 'openai', 'gpt-5.5', false);
expect(module.isModelEnabled('codex', 'openai', { id: 'gpt-5.6' })).toBe(false);
expect(module.isModelEnabled('codex', 'openai', { id: 'gpt-5.5' })).toBe(false);
+ expect(memStorage.getItem(
+ 'xdt:modelVisibilityPrefs:v1.migration-complete.owner.owner-a',
+ )).toBe('1');
+ });
+
+ it('旧 key 尚未归属任何账号时继续阻止写入,避免抢占未知 owner 的迁移输入', async () => {
+ memStorage.setItem(
+ 'xdt:modelVisibilityPrefs:v1',
+ JSON.stringify({ 'codex:openai:gpt-5.6': false }),
+ );
+ setOwnerClaim('owner-a', 1, false, false);
+ const module = await loadModuleForOwner();
+
+ module.setModelVisibility('codex', 'openai', 'gpt-5.5', false);
+
+ expect(memStorage.getItem('xdt:modelVisibilityPrefs:v1.owner.owner-a')).toBeNull();
+ expect(module.isModelEnabled('codex', 'openai', { id: 'gpt-5.5' })).toBe(true);
});
it('已有 owner-scoped override 优先,迁移不会覆盖', async () => {
diff --git a/apps/desktop/src/renderer/state/modelVisibilityPrefs.ts b/apps/desktop/src/renderer/state/modelVisibilityPrefs.ts
index 79e96666f6..a07a474a2f 100644
--- a/apps/desktop/src/renderer/state/modelVisibilityPrefs.ts
+++ b/apps/desktop/src/renderer/state/modelVisibilityPrefs.ts
@@ -37,6 +37,7 @@ import type { AgentKind } from '@/hooks/useAgentCapabilities';
const LEGACY_STORAGE_KEY = 'xdt:modelVisibilityPrefs:v1';
const STORAGE_KEY_PREFIX = `${LEGACY_STORAGE_KEY}.owner`;
+const MIGRATION_COMPLETE_KEY_PREFIX = `${LEGACY_STORAGE_KEY}.migration-complete.owner`;
/** override 表:key=`${agent}:${providerId}:${modelId}` → 用户显式设定的可见性。 */
type VisibilityMap = Record<string, boolean>;
@@ -62,12 +63,17 @@ let cache: VisibilityMap | null = null;
let activeOwnerId: string | null = null;
let activeOwnerGeneration = 0;
let activeOwnerReadyForWrites = false;
+let activeOwnerMigrationPending = false;
let activeOwnerMode: 'signed-out' | 'local' | 'cloud' = 'signed-out';
function ownerStorageKey(ownerId: string): string {
return `${STORAGE_KEY_PREFIX}.${encodeURIComponent(ownerId)}`;
}
+function ownerMigrationCompleteKey(ownerId: string): string {
+ return `${MIGRATION_COMPLETE_KEY_PREFIX}.${encodeURIComponent(ownerId)}`;
+}
+
function readStoredMap(raw: string | null): VisibilityMap {
if (!raw) return {};
try {
@@ -81,42 +87,79 @@ function readStoredMap(raw: string | null): VisibilityMap {
* 把旧版唯一全局 key 的快照交给 Main 已原子认领的 owner。旧 key 故意保留给并发运行的
* 旧版本;新版本只认 owner-scoped key,所以其它账号不会再次导入这份数据。
*/
-function migrateLegacyVisibility(ownerId: string, ownerGeneration: number): boolean {
- if (typeof window === 'undefined') return false;
+interface MigrationState {
+ readyForWrites: boolean;
+ migrationPending: boolean;
+}
+
+const BLOCKED_MIGRATION: MigrationState = {
+ readyForWrites: false,
+ migrationPending: true,
+};
+
+function migrateLegacyVisibility(ownerId: string, ownerGeneration: number): MigrationState {
+ if (typeof window === 'undefined') return BLOCKED_MIGRATION;
try {
const scopedKey = ownerStorageKey(ownerId);
- if (window.localStorage.getItem(scopedKey) !== null) return true;
+ const migrationCompleteKey = ownerMigrationCompleteKey(ownerId);
+ if (window.localStorage.getItem(migrationCompleteKey) === '1') {
+ return { readyForWrites: true, migrationPending: false };
+ }
// Main 用模型可见性专属 marker 把旧 key 原子归属给升级时的当前已验证云账号;
// canInitialize 还保证此刻没有另一个共享 userData 的旧进程在并发改写迁移输入。
const claim = window.electronAPI?.maker?.claimLegacyModelVisibilityOwner?.();
- if (claim?.dataOwnerId !== ownerId || claim.ownerGeneration !== ownerGeneration) return false;
- if (claim.claimedByOtherOwner === true) return true;
- if (claim.claimed !== true || claim.canInitialize !== true) return false;
+ if (claim?.dataOwnerId !== ownerId || claim.ownerGeneration !== ownerGeneration) {
+ return BLOCKED_MIGRATION;
+ }
+ if (claim.claimedByOtherOwner === true) {
+ // 旧快照永久属于另一账号;写入本 owner 的完成标记,后续无需依赖全局 marker 继续读写。
+ window.localStorage.setItem(migrationCompleteKey, '1');
+ return {
+ readyForWrites: window.localStorage.getItem(migrationCompleteKey) === '1',
+ migrationPending: false,
+ };
+ }
+ if (claim.claimed !== true) return BLOCKED_MIGRATION;
+ if (claim.canInitialize !== true) {
+ // 归属已经明确时,新设置可以安全写进 owner namespace;只把旧全局快照的导入推迟到独占时。
+ return { readyForWrites: true, migrationPending: true };
+ }
const legacy = readStoredMap(window.localStorage.getItem(LEGACY_STORAGE_KEY));
- // 即使旧 key 不存在也写空表,作为该 owner 已完成一次性检查的持久标记。
- window.localStorage.setItem(scopedKey, JSON.stringify(legacy));
- return window.localStorage.getItem(scopedKey) !== null;
+ const scoped = readStoredMap(window.localStorage.getItem(scopedKey));
+ // 非独占期间可能已经有新设置;完成迁移时由新设置覆盖同槽旧值,其余历史值仍被保留。
+ window.localStorage.setItem(scopedKey, JSON.stringify({ ...legacy, ...scoped }));
+ // 快照先落盘再标完成;任一步失败都会在下次写入/登录时幂等重试。
+ window.localStorage.setItem(migrationCompleteKey, '1');
+ return {
+ readyForWrites: window.localStorage.getItem(migrationCompleteKey) === '1',
+ migrationPending: false,
+ };
} catch {
// localStorage / 同步 owner 仲裁不可用时 fail closed:不读取未归属的旧数据。
- return false;
+ return BLOCKED_MIGRATION;
}
}
function ensureActiveOwnerReadyForWrites(): boolean {
if (!activeOwnerId) return false;
- if (activeOwnerReadyForWrites) return true;
+ if (activeOwnerReadyForWrites && !activeOwnerMigrationPending) return true;
if (activeOwnerMode === 'local') {
activeOwnerReadyForWrites = true;
+ activeOwnerMigrationPending = false;
return true;
}
if (activeOwnerMode !== 'cloud') return false;
- activeOwnerReadyForWrites = migrateLegacyVisibility(activeOwnerId, activeOwnerGeneration);
+ const migration = migrateLegacyVisibility(activeOwnerId, activeOwnerGeneration);
+ activeOwnerReadyForWrites = migration.readyForWrites;
+ activeOwnerMigrationPending = migration.migrationPending;
if (!activeOwnerReadyForWrites) return false;
- // A deferred migration may have imported the legacy map after this owner was first loaded.
- cache = null;
- load();
+ if (!activeOwnerMigrationPending) {
+ // A deferred migration may have imported the legacy map after this owner was first loaded.
+ cache = null;
+ load();
+ }
return true;
}
@@ -200,9 +243,12 @@ export function setModelVisibilityOwner(
activeOwnerGeneration = ownerGeneration;
activeOwnerMode = mode;
activeOwnerReadyForWrites = mode === 'local' && ownerId !== null;
+ activeOwnerMigrationPending = false;
cache = null;
if (ownerId && mode === 'cloud') {
- activeOwnerReadyForWrites = migrateLegacyVisibility(ownerId, ownerGeneration);
+ const migration = migrateLegacyVisibility(ownerId, ownerGeneration);
+ activeOwnerReadyForWrites = migration.readyForWrites;
+ activeOwnerMigrationPending = migration.migrationPending;
}
load();
version += 1;
@@ -272,10 +318,12 @@ export function useModelVisibilityVersion(): number {
/** 测试用 —— 重置缓存 + 清 localStorage(其它代码不应调用)。 */
export function __resetForTest(): void {
const currentScopedKey = activeOwnerId ? ownerStorageKey(activeOwnerId) : null;
+ const currentMigrationKey = activeOwnerId ? ownerMigrationCompleteKey(activeOwnerId) : null;
cache = null;
activeOwnerId = null;
activeOwnerGeneration = 0;
activeOwnerReadyForWrites = false;
+ activeOwnerMigrationPending = false;
activeOwnerMode = 'signed-out';
version = 0;
listeners.clear();
@@ -283,6 +331,7 @@ export function __resetForTest(): void {
try {
window.localStorage.removeItem(LEGACY_STORAGE_KEY);
if (currentScopedKey) window.localStorage.removeItem(currentScopedKey);
+ if (currentMigrationKey) window.localStorage.removeItem(currentMigrationKey);
} catch {
// ignore
}