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): 同 owner 换代后重推默认模型草稿镜像,伙伴不再报“请先选择已开启的伙伴模型”(#4469) (#4784)

FicoWang30689b101ec4
diff --git a/apps/desktop/src/renderer/App.tsx b/apps/desktop/src/renderer/App.tsx
index d1e0bf2947..ed10600de8 100644
--- a/apps/desktop/src/renderer/App.tsx
+++ b/apps/desktop/src/renderer/App.tsx
@@ -6,6 +6,7 @@ import { useEffect } from 'react';
import { RemoteDesktopHost } from '@/features/remote-desktop/RemoteDesktopHost';
import { useCloseWindowFallbackShortcut } from '@/hooks/useCloseWindowShortcut';
+import { useNewMakerPrefsOwnerResync } from '@/hooks/useNewMakerPrefsOwnerResync';
import { useDisableContextMenu } from '@/hooks/useDisableContextMenu';
import { ThemeProvider } from '@/hooks/useTheme';
import { FontSettingsProvider } from '@/hooks/useFontSettings';
@@ -193,11 +194,12 @@ function MakerBootstrap() {
// Auth 广播的多个 listener 没有顺序契约;等 AuthContext 提交新 owner 后再预热一次,
// 保证 provider 快照与 capabilities 不会沿用或提交前一个 owner 的在途结果。
useEffect(() => {
- // Early fire-and-forget sends can precede maker IPC registration. Repeat only
- // after the ready/owner boundary, using the same persisted preference snapshot.
- syncNewMakerPrefs();
void preloadLocalCatalogSnapshot();
}, [dataOwnerId, dataOwnerRecoveryEpoch]);
+ // Early fire-and-forget sends can precede maker IPC registration. Repeat after
+ // the ready/owner boundary and after every owner generation change (same-owner
+ // repairs included, #4469), using the same persisted preference snapshot.
+ useNewMakerPrefsOwnerResync(syncNewMakerPrefs);
return null;
}
diff --git a/apps/desktop/src/renderer/__tests__/authContextSessionBoundary.test.tsx b/apps/desktop/src/renderer/__tests__/authContextSessionBoundary.test.tsx
index e12966a19d..e706488cad 100644
--- a/apps/desktop/src/renderer/__tests__/authContextSessionBoundary.test.tsx
+++ b/apps/desktop/src/renderer/__tests__/authContextSessionBoundary.test.tsx
@@ -345,6 +345,26 @@ describe('AuthContext session cache boundaries', () => {
expect(mocks.preloadLocalCatalogSnapshot).toHaveBeenCalledOnce();
});
+ it('exposes the pushed owner generation so same-owner repairs are observable (#4469)', async () => {
+ const { result } = renderHook(() => useAuth(), { wrapper });
+ act(() => {
+ mocks.emitAuth({ ...authState('a'), ownerGeneration: 1 });
+ });
+ await waitFor(() => expect(result.current.dataOwnerGeneration).toBe(1));
+ const epochBefore = result.current.dataOwnerRecoveryEpoch;
+ mocks.reset.mockClear();
+
+ // Same-owner projection repair: generation advances, owner and epoch do not.
+ act(() => {
+ mocks.emitAuth({ ...authState('a'), ownerGeneration: 2 });
+ });
+ await waitFor(() => expect(result.current.dataOwnerGeneration).toBe(2));
+ expect(result.current.dataOwnerId).toBe('a');
+ expect(result.current.dataOwnerRecoveryEpoch).toBe(epochBefore);
+ expect(getDataOwnerGeneration()).toEqual({ dataOwnerId: 'a', generation: 2 });
+ expect(mocks.reset).not.toHaveBeenCalled();
+ });
+
it('updates Canary state without treating it as an account switch', async () => {
const view = renderHook(() => useAuth(), { wrapper });
await waitFor(() => expect(view.result.current.user?.id).toBe('account-a'));
diff --git a/apps/desktop/src/renderer/contexts/AuthContext.tsx b/apps/desktop/src/renderer/contexts/AuthContext.tsx
index 3069c2707d..d50de4f8ee 100644
--- a/apps/desktop/src/renderer/contexts/AuthContext.tsx
+++ b/apps/desktop/src/renderer/contexts/AuthContext.tsx
@@ -73,6 +73,13 @@ export interface AuthContextValue {
dataOwnerId: string | null;
/** Failed auth boundaries remount owner-scoped routes so stale generations can rehydrate. */
dataOwnerRecoveryEpoch: number;
+ /**
+ * Main-owned owner generation as last pushed. It advances on every owner commit,
+ * including same-owner repairs that keep `dataOwnerId` and never touch
+ * `dataOwnerRecoveryEpoch`; owner-stamped mirrors in main are fenced on it, so a
+ * consumer that pushes such a mirror must re-push when this changes (#4469).
+ */
+ dataOwnerGeneration: number;
canEnterApp: boolean;
isAuthenticated: boolean;
/** 当前账号是否加入 Canary 发布通道。 */
@@ -138,6 +145,7 @@ export function AuthProvider({
const [mode, setMode] = useState<'signed-out' | 'local' | 'cloud'>('signed-out');
const [dataOwnerId, setDataOwnerId] = useState<string | null>(null);
const [dataOwnerRecoveryEpoch, setDataOwnerRecoveryEpoch] = useState(0);
+ const [dataOwnerGeneration, setDataOwnerGenerationState] = useState(0);
const [canEnterApp, setCanEnterApp] = useState(false);
const [isAuthenticated, setIsAuthenticated] = useState(false);
const [isCanary, setIsCanary] = useState(false);
@@ -175,6 +183,7 @@ export function AuthProvider({
activeDataOwnerIdRef.current,
activeDataOwnerGenerationRef.current,
);
+ setDataOwnerGenerationState(activeDataOwnerGenerationRef.current);
setDataOwnerRecoveryEpoch((epoch) => epoch + 1);
void preloadLocalCatalogSnapshot();
throw error;
@@ -203,6 +212,7 @@ export function AuthProvider({
}
activeDataOwnerIdRef.current = state.dataOwnerId;
activeDataOwnerGenerationRef.current = state.ownerGeneration;
+ setDataOwnerGenerationState(state.ownerGeneration);
setNewMakerDraftOwner(state.dataOwnerId);
setProviderModelMemoryOwner(state.dataOwnerId);
// 模型选择器的持久记忆与 newMakerDraft 同待遇:同一处、同一个 dataOwnerId、
@@ -507,6 +517,7 @@ export function AuthProvider({
mode,
dataOwnerId,
dataOwnerRecoveryEpoch,
+ dataOwnerGeneration,
canEnterApp,
isAuthenticated,
isCanary,
@@ -538,6 +549,7 @@ export function AuthProvider({
mode,
dataOwnerId,
dataOwnerRecoveryEpoch,
+ dataOwnerGeneration,
canEnterApp,
isAuthenticated,
isCanary,
diff --git a/apps/desktop/src/renderer/hooks/__tests__/useNewMakerPrefsOwnerResync.test.ts b/apps/desktop/src/renderer/hooks/__tests__/useNewMakerPrefsOwnerResync.test.ts
new file mode 100644
index 0000000000..90293b9e6e
--- /dev/null
+++ b/apps/desktop/src/renderer/hooks/__tests__/useNewMakerPrefsOwnerResync.test.ts
@@ -0,0 +1,59 @@
+// @vitest-environment jsdom
+
+import { renderHook } from '@testing-library/react';
+import { afterEach, describe, expect, it, vi } from 'vitest';
+
+const auth = vi.hoisted(() => ({
+ current: { dataOwnerId: 'owner-a' as string | null, dataOwnerRecoveryEpoch: 0, dataOwnerGeneration: 1 },
+}));
+vi.mock('@/contexts/AuthContext', () => ({
+ useAuth: () => auth.current,
+}));
+
+import { useNewMakerPrefsOwnerResync } from '@/hooks/useNewMakerPrefsOwnerResync';
+
+describe('useNewMakerPrefsOwnerResync (#4469)', () => {
+ afterEach(() => {
+ auth.current = { dataOwnerId: 'owner-a', dataOwnerRecoveryEpoch: 0, dataOwnerGeneration: 1 };
+ });
+
+ it('re-pushes when the same owner advances its generation, and only then', () => {
+ const sync = vi.fn();
+ const { rerender } = renderHook(() => useNewMakerPrefsOwnerResync(sync));
+ expect(sync).toHaveBeenCalledTimes(1);
+
+ // Unrelated re-render: same owner, same epoch, same generation → no push.
+ rerender();
+ expect(sync).toHaveBeenCalledTimes(1);
+
+ // Same-owner projection repair: only the generation moves.
+ auth.current = { ...auth.current, dataOwnerGeneration: 2 };
+ rerender();
+ expect(sync).toHaveBeenCalledTimes(2);
+
+ // A rejected boundary restores the generation and bumps the recovery epoch.
+ auth.current = { ...auth.current, dataOwnerRecoveryEpoch: 1 };
+ rerender();
+ expect(sync).toHaveBeenCalledTimes(3);
+
+ // Owner switch still re-pushes as before.
+ auth.current = { dataOwnerId: 'owner-b', dataOwnerRecoveryEpoch: 1, dataOwnerGeneration: 5 };
+ rerender();
+ expect(sync).toHaveBeenCalledTimes(4);
+ });
+
+ it('follows the latest sync callback identity without duplicating pushes', () => {
+ const first = vi.fn();
+ const second = vi.fn();
+ const { rerender } = renderHook(({ sync }) => useNewMakerPrefsOwnerResync(sync), {
+ initialProps: { sync: first },
+ });
+ expect(first).toHaveBeenCalledTimes(1);
+ rerender({ sync: second });
+ expect(second).toHaveBeenCalledTimes(1);
+ auth.current = { ...auth.current, dataOwnerGeneration: 3 };
+ rerender({ sync: second });
+ expect(first).toHaveBeenCalledTimes(1);
+ expect(second).toHaveBeenCalledTimes(2);
+ });
+});
diff --git a/apps/desktop/src/renderer/hooks/useNewMakerPrefsOwnerResync.ts b/apps/desktop/src/renderer/hooks/useNewMakerPrefsOwnerResync.ts
new file mode 100644
index 0000000000..9c88b6edf9
--- /dev/null
+++ b/apps/desktop/src/renderer/hooks/useNewMakerPrefsOwnerResync.ts
@@ -0,0 +1,24 @@
+import { useEffect } from 'react';
+
+import { useAuth } from '@/contexts/AuthContext';
+
+/**
+ * Re-push the persisted new-task / Bot default snapshot whenever the owner scope
+ * main fences it on changes.
+ *
+ * Main keeps that mirror keyed by `mode:dataOwnerId:generation` and refuses a
+ * push stamped with any other generation. A same-owner repair (Ghost projection
+ * recovery) bumps the generation without changing `dataOwnerId` or the recovery
+ * epoch, so nothing used to re-push: the mirror stayed fenced on the old scope,
+ * Bots that follow the app default read no preferred route, and every send hit
+ * "请先选择已开启的伙伴模型" until the user touched the model picker (#4469).
+ *
+ * `sync` must early-return while the persisted draft / memory snapshot is not
+ * loaded, so a re-push never overwrites a configured mirror with an empty one.
+ */
+export function useNewMakerPrefsOwnerResync(sync: () => void): void {
+ const { dataOwnerId, dataOwnerRecoveryEpoch, dataOwnerGeneration } = useAuth();
+ useEffect(() => {
+ sync();
+ }, [sync, dataOwnerId, dataOwnerRecoveryEpoch, dataOwnerGeneration]);
+}