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): 补齐本地 profile 迁移与启动期 IPC

liugaoweib28e7491e0cd
diff --git a/apps/desktop/src/main/__tests__/modelVisibilityOwnerClaimOrdering.test.ts b/apps/desktop/src/main/__tests__/modelVisibilityOwnerClaimOrdering.test.ts
new file mode 100644
index 0000000000..d07bfdb4b3
--- /dev/null
+++ b/apps/desktop/src/main/__tests__/modelVisibilityOwnerClaimOrdering.test.ts
@@ -0,0 +1,16 @@
+import { readFileSync } from 'node:fs';
+import { resolve } from 'node:path';
+
+import { describe, expect, it } from 'vitest';
+
+const source = readFileSync(resolve(__dirname, '..', 'bootstrap-electron.ts'), 'utf8');
+
+describe('model visibility owner claim IPC ordering', () => {
+ it('registers the synchronous claim handler before the first BrowserWindow is created', () => {
+ const registration = source.indexOf('registerModelVisibilityOwnerClaimIpc();');
+ const createWindow = source.indexOf('startupWindowCreationAllowed = true;');
+
+ expect(registration).toBeGreaterThanOrEqual(0);
+ expect(createWindow).toBeGreaterThan(registration);
+ });
+});
diff --git a/apps/desktop/src/main/bootstrap-electron.ts b/apps/desktop/src/main/bootstrap-electron.ts
index 3961670edd..6c926398ab 100644
--- a/apps/desktop/src/main/bootstrap-electron.ts
+++ b/apps/desktop/src/main/bootstrap-electron.ts
@@ -399,6 +399,7 @@ import { reconcileSavepointRefsForDeletedSessions } from './git-snapshot/savepoi
import { registerGitContextIpc, disposeGitContext } from './git-context';
import { registerGitReviewDeviceOp, registerGitReviewIpc } from './git-review';
import { registerSidebarSettingsIpc } from './sidebarSettingsStore';
+import { registerModelVisibilityOwnerClaimIpc } from './maker-host/model-visibility-owner-claim.js';
import { registerRemotePrecreatedWorktreeLedgerIpc } from './remotePrecreatedWorktreeLedger';
import { registerTerminalHandlers } from './maker-ipc/terminal-handlers';
import { registerLocalThemesIpc } from './local-themes/register';
@@ -7040,6 +7041,7 @@ app.on('ready', async () => {
// device-link 远程 git 审查(只读):git-review:remote-op handler(被控端角色;
// invoke-registry 捕获后供控制端隧道调用,本机 renderer 不调用)。
registerGitReviewDeviceOp();
+ registerModelVisibilityOwnerClaimIpc();
registerSidebarSettingsIpc();
registerRemotePrecreatedWorktreeLedgerIpc();
// RSB terminal tab: PTY backend + 8 个 terminal:* IPC channels(create/write/resize/dispose/restart
diff --git a/apps/desktop/src/main/maker-host/__tests__/model-visibility-owner-claim.test.ts b/apps/desktop/src/main/maker-host/__tests__/model-visibility-owner-claim.test.ts
index 9c67a2df5b..19b6874537 100644
--- a/apps/desktop/src/main/maker-host/__tests__/model-visibility-owner-claim.test.ts
+++ b/apps/desktop/src/main/maker-host/__tests__/model-visibility-owner-claim.test.ts
@@ -11,10 +11,17 @@ const harness = vi.hoisted(() => ({
ownerGeneration: 1,
boundaryPending: false,
exclusive: true,
+ listeners: new Map<string, (event: { returnValue?: unknown }) => void>(),
+ assertTrusted: vi.fn(),
}));
vi.mock('electron', () => ({
app: { getPath: () => harness.root },
+ ipcMain: {
+ on: (channel: string, listener: (event: { returnValue?: unknown }) => void) => {
+ harness.listeners.set(channel, listener);
+ },
+ },
}));
vi.mock('../../appSessionState.js', () => ({
@@ -38,7 +45,14 @@ vi.mock('../../ownerNamespaceMigration.js', () => ({
hasExclusiveSharedLegacyUserDataAccess: () => harness.exclusive,
}));
-import { claimLegacyModelVisibilityOwner } from '../model-visibility-owner-claim.js';
+vi.mock('../../security/trustedAppRenderer.js', () => ({
+ assertTrustedAppRendererEvent: (...args: unknown[]) => harness.assertTrusted(...args),
+}));
+
+import {
+ claimLegacyModelVisibilityOwner,
+ registerModelVisibilityOwnerClaimIpc,
+} from '../model-visibility-owner-claim.js';
const markerName = 'model-visibility-renderer-legacy-owner.v1.json';
@@ -49,6 +63,8 @@ beforeEach(() => {
harness.ownerGeneration = 1;
harness.boundaryPending = false;
harness.exclusive = true;
+ harness.listeners.clear();
+ harness.assertTrusted.mockClear();
});
afterEach(() => {
@@ -56,7 +72,7 @@ afterEach(() => {
});
describe('model visibility legacy Renderer owner claim', () => {
- it('atomically binds the legacy key to the active verified cloud account only once', () => {
+ it('atomically binds the legacy key to the active stable owner only once', () => {
expect(claimLegacyModelVisibilityOwner()).toEqual({
dataOwnerId: 'owner-a',
ownerGeneration: 1,
@@ -80,10 +96,30 @@ describe('model visibility legacy Renderer owner claim', () => {
});
});
- it('does not claim from signed-out, local, boundary-pending, or shared access', () => {
+ it('lets a stable local profile claim the legacy key before a later cloud login', () => {
harness.mode = 'local';
+ harness.ownerId = 'local-v1';
+ expect(claimLegacyModelVisibilityOwner()).toMatchObject({
+ dataOwnerId: 'local-v1',
+ claimed: true,
+ claimedByOtherOwner: false,
+ canInitialize: true,
+ });
+ harness.mode = 'cloud';
+ harness.ownerId = 'owner-a';
+ expect(claimLegacyModelVisibilityOwner()).toMatchObject({
+ claimed: false,
+ claimedByOtherOwner: true,
+ canInitialize: false,
+ });
+ });
+
+ it('does not claim from signed-out, boundary-pending, or shared access', () => {
+ harness.mode = 'signed-out';
+ harness.ownerId = null;
expect(claimLegacyModelVisibilityOwner().claimed).toBe(false);
harness.mode = 'cloud';
+ harness.ownerId = 'owner-a';
harness.boundaryPending = true;
expect(claimLegacyModelVisibilityOwner().claimed).toBe(false);
harness.boundaryPending = false;
@@ -92,6 +128,16 @@ describe('model visibility legacy Renderer owner claim', () => {
expect(fs.existsSync(path.join(harness.root, markerName))).toBe(false);
});
+ it('registers the trusted synchronous claim handler in the eager bootstrap phase', () => {
+ registerModelVisibilityOwnerClaimIpc();
+ const listener = harness.listeners.get('maker:model-visibility:legacy-owner-claim-sync');
+ expect(listener).toBeDefined();
+ const event: { returnValue?: unknown } = {};
+ listener?.(event);
+ expect(harness.assertTrusted).toHaveBeenCalledWith(event);
+ expect(event.returnValue).toMatchObject({ dataOwnerId: 'owner-a', claimed: true });
+ });
+
it('keeps a claimed owner readable but blocks legacy initialization without exclusivity', () => {
expect(claimLegacyModelVisibilityOwner().canInitialize).toBe(true);
harness.exclusive = false;
diff --git a/apps/desktop/src/main/maker-host/model-visibility-owner-claim.ts b/apps/desktop/src/main/maker-host/model-visibility-owner-claim.ts
index a14f1a8afb..a1bf6fb54f 100644
--- a/apps/desktop/src/main/maker-host/model-visibility-owner-claim.ts
+++ b/apps/desktop/src/main/maker-host/model-visibility-owner-claim.ts
@@ -1,4 +1,4 @@
-import { app } from 'electron';
+import { app, ipcMain } from 'electron';
import { randomUUID } from 'node:crypto';
import fs from 'node:fs';
import path from 'node:path';
@@ -11,7 +11,9 @@ import {
} from '../appSessionState.js';
import { createLogger } from '../logger.js';
import { hasExclusiveSharedLegacyUserDataAccess } from '../ownerNamespaceMigration.js';
+import { assertTrustedAppRendererEvent } from '../security/trustedAppRenderer.js';
import { readBoundedFileNoFollowSync } from '../utils/readBoundedFile.js';
+import { MAKER_INVOKE } from '../maker-ipc/channels.js';
import type { ModelVisibilityLegacyOwnerClaim } from '../../shared/modelVisibility.js';
const MARKER_FILE = 'model-visibility-renderer-legacy-owner.v1.json';
@@ -52,7 +54,7 @@ function readOwnerMarker(markerPath: string): OwnerMarkerRead {
}
/**
- * Atomically binds the pre-account Renderer preference key to the cloud account that is active
+ * Atomically binds the pre-account Renderer preference key to the stable local/cloud owner active
* during this upgrade. The marker is model-visibility-specific: older general migrations may have
* been claimed by a historical account and therefore cannot establish who owns this localStorage.
*/
@@ -60,7 +62,7 @@ export function claimLegacyModelVisibilityOwner(): ModelVisibilityLegacyOwnerCla
const stamp = getActiveDataOwnerPushStamp();
const session = getActiveAppSession();
if (
- session.mode !== 'cloud'
+ session.mode === 'signed-out'
|| !stamp.dataOwnerId
|| session.dataOwnerId !== stamp.dataOwnerId
|| isAppSessionBoundaryPending()
@@ -114,3 +116,11 @@ export function claimLegacyModelVisibilityOwner(): ModelVisibilityLegacyOwnerCla
claimed && exclusiveAtStart && hasExclusiveSharedLegacyUserDataAccess(),
};
}
+
+/** Register before the first BrowserWindow: preload uses a synchronous claim read during auth boot. */
+export function registerModelVisibilityOwnerClaimIpc(): void {
+ ipcMain.on(MAKER_INVOKE.MODEL_VISIBILITY_LEGACY_OWNER_CLAIM_SYNC, (event) => {
+ assertTrustedAppRendererEvent(event);
+ event.returnValue = claimLegacyModelVisibilityOwner();
+ });
+}
diff --git a/apps/desktop/src/main/maker-ipc/channels.ts b/apps/desktop/src/main/maker-ipc/channels.ts
index 06198fafa5..6dc3c4d1dc 100644
--- a/apps/desktop/src/main/maker-ipc/channels.ts
+++ b/apps/desktop/src/main/maker-ipc/channels.ts
@@ -226,7 +226,7 @@ export const MAKER_INVOKE = {
* ownerGeneration,Main 拒绝账号切换期间的迟到快照。fire-and-forget,不落盘。
*/
MODEL_VISIBILITY_SYNC: 'maker:model-visibility:sync',
- /** Sync read: which verified cloud owner may import the pre-account Renderer preference key. */
+ /** Sync read: which stable local/cloud owner may import the pre-account Renderer preference key. */
MODEL_VISIBILITY_LEGACY_OWNER_CLAIM_SYNC: 'maker:model-visibility:legacy-owner-claim-sync',
/**
* 「模型 / 供应商停用」override 写入(model-disable-store,main 侧持久化真源)。
diff --git a/apps/desktop/src/main/maker-ipc/register.ts b/apps/desktop/src/main/maker-ipc/register.ts
index 55799957da..a14c088d07 100644
--- a/apps/desktop/src/main/maker-ipc/register.ts
+++ b/apps/desktop/src/main/maker-ipc/register.ts
@@ -732,7 +732,6 @@ import {
getModelVisibilityMirrorSnapshot,
syncModelVisibilityMirrorForOwner,
} from '../maker-host/model-visibility-mirror.js';
-import { claimLegacyModelVisibilityOwner } from '../maker-host/model-visibility-owner-claim.js';
import {
clearProviderDisableOverrides,
setModelsDisabled,
@@ -13137,11 +13136,6 @@ export function registerMakerIpc(maker: Maker, options: RegisterMakerIpcOptions)
// main 缓存供 IM /model 与 device-link provider:list 复用同一套可见性过滤。值实变时
// 复用 PROVIDER_CHANGED 目录失效事件,让已连接控制端驱逐缓存并重拉 override 快照。
// 容错存储(非对象 ⇒ 清空),无错误路径,故不需要 throwIpcError。
- ipcMain.on(MAKER_INVOKE.MODEL_VISIBILITY_LEGACY_OWNER_CLAIM_SYNC, (event) => {
- assertTrustedAppRendererEvent(event);
- event.returnValue = claimLegacyModelVisibilityOwner();
- });
-
ipcMain.handle(MAKER_INVOKE.MODEL_VISIBILITY_SYNC, async (
event,
dataOwnerId: unknown,
diff --git a/apps/desktop/src/renderer/__tests__/modelVisibilityPrefs.test.ts b/apps/desktop/src/renderer/__tests__/modelVisibilityPrefs.test.ts
index 76cffb930b..7ab0f7afab 100644
--- a/apps/desktop/src/renderer/__tests__/modelVisibilityPrefs.test.ts
+++ b/apps/desktop/src/renderer/__tests__/modelVisibilityPrefs.test.ts
@@ -253,14 +253,15 @@ describe('modelVisibilityPrefs store', () => {
expect(syncModelVisibility).toHaveBeenLastCalledWith(null, 2, {});
});
- it('本地 profile 使用自己的 namespace,但不会认领云账号的历史全局 key', async () => {
+ it('本地 profile 认领历史全局 key 并迁移到自己的 namespace', async () => {
memStorage.setItem(
'xdt:modelVisibilityPrefs:v1',
JSON.stringify({ 'codex:openai:gpt-5.6': false }),
);
+ setOwnerClaim('local-v1', 1);
const module = await loadModuleForOwner('local-v1', 1, 'local');
- expect(module.isModelEnabled('codex', 'openai', { id: 'gpt-5.6' })).toBe(true);
+ expect(module.isModelEnabled('codex', 'openai', { id: 'gpt-5.6' })).toBe(false);
module.setModelVisibility('codex', 'openai', 'gpt-5.5', false);
expect(module.isModelEnabled('codex', 'openai', { id: 'gpt-5.5' })).toBe(false);
expect(memStorage.getItem('xdt:modelVisibilityPrefs:v1')).not.toBeNull();
diff --git a/apps/desktop/src/renderer/state/modelVisibilityPrefs.ts b/apps/desktop/src/renderer/state/modelVisibilityPrefs.ts
index a07a474a2f..5e7c8687d9 100644
--- a/apps/desktop/src/renderer/state/modelVisibilityPrefs.ts
+++ b/apps/desktop/src/renderer/state/modelVisibilityPrefs.ts
@@ -106,7 +106,7 @@ function migrateLegacyVisibility(ownerId: string, ownerGeneration: number): Migr
return { readyForWrites: true, migrationPending: false };
}
- // Main 用模型可见性专属 marker 把旧 key 原子归属给升级时的当前已验证云账号;
+ // Main 用模型可见性专属 marker 把旧 key 原子归属给升级时的当前稳定 local/cloud owner;
// canInitialize 还保证此刻没有另一个共享 userData 的旧进程在并发改写迁移输入。
const claim = window.electronAPI?.maker?.claimLegacyModelVisibilityOwner?.();
if (claim?.dataOwnerId !== ownerId || claim.ownerGeneration !== ownerGeneration) {
@@ -145,12 +145,7 @@ function migrateLegacyVisibility(ownerId: string, ownerGeneration: number): Migr
function ensureActiveOwnerReadyForWrites(): boolean {
if (!activeOwnerId) return false;
if (activeOwnerReadyForWrites && !activeOwnerMigrationPending) return true;
- if (activeOwnerMode === 'local') {
- activeOwnerReadyForWrites = true;
- activeOwnerMigrationPending = false;
- return true;
- }
- if (activeOwnerMode !== 'cloud') return false;
+ if (activeOwnerMode === 'signed-out') return false;
const migration = migrateLegacyVisibility(activeOwnerId, activeOwnerGeneration);
activeOwnerReadyForWrites = migration.readyForWrites;
activeOwnerMigrationPending = migration.migrationPending;
@@ -242,10 +237,10 @@ export function setModelVisibilityOwner(
activeOwnerId = ownerId;
activeOwnerGeneration = ownerGeneration;
activeOwnerMode = mode;
- activeOwnerReadyForWrites = mode === 'local' && ownerId !== null;
+ activeOwnerReadyForWrites = false;
activeOwnerMigrationPending = false;
cache = null;
- if (ownerId && mode === 'cloud') {
+ if (ownerId && mode !== 'signed-out') {
const migration = migrateLegacyVisibility(ownerId, ownerGeneration);
activeOwnerReadyForWrites = migration.readyForWrites;
activeOwnerMigrationPending = migration.migrationPending;
diff --git a/apps/desktop/src/renderer/vite-env.d.ts b/apps/desktop/src/renderer/vite-env.d.ts
index e7bac06cda..0cb6bc3f36 100644
--- a/apps/desktop/src/renderer/vite-env.d.ts
+++ b/apps/desktop/src/renderer/vite-env.d.ts
@@ -4747,7 +4747,7 @@ interface ElectronAPI {
ownerGeneration: number,
map: Record<string, boolean>,
) => Promise<void>;
- /** Resolve the verified cloud owner allowed to import the pre-account preference key. */
+ /** Resolve the stable local/cloud owner allowed to import the pre-account preference key. */
claimLegacyModelVisibilityOwner: () => import('../shared/modelVisibility').ModelVisibilityLegacyOwnerClaim;
/**
* 「模型 / 供应商停用」override 写入(main 侧 model-disable-store);成功后 main 广播
diff --git a/apps/desktop/src/shared/modelVisibility.ts b/apps/desktop/src/shared/modelVisibility.ts
index 1b55cad3cc..a3dd114d22 100644
--- a/apps/desktop/src/shared/modelVisibility.ts
+++ b/apps/desktop/src/shared/modelVisibility.ts
@@ -2,7 +2,7 @@ import { isDataOwnerPushStamp, type DataOwnerPushStamp } from './dataOwnerPush.j
/** Main's decision for importing the one pre-account model visibility namespace. */
export interface ModelVisibilityLegacyOwnerClaim extends DataOwnerPushStamp {
- /** The model visibility legacy marker belongs to the active verified cloud owner. */
+ /** The model visibility legacy marker belongs to the active stable local/cloud owner. */
readonly claimed: boolean;
/** The model visibility legacy marker durably belongs to another account. */
readonly claimedByOtherOwner: boolean;