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): gate mirror cache refresh by account auth (#3325)

DavidShenXDcde34ffe869a
diff --git a/apps/desktop/src/renderer/__tests__/deviceLinkRemoteProjectsAuth.test.tsx b/apps/desktop/src/renderer/__tests__/deviceLinkRemoteProjectsAuth.test.tsx
new file mode 100644
index 0000000000..8cb4796408
--- /dev/null
+++ b/apps/desktop/src/renderer/__tests__/deviceLinkRemoteProjectsAuth.test.tsx
@@ -0,0 +1,55 @@
+// @vitest-environment jsdom
+
+import { act, cleanup, renderHook } from '@testing-library/react';
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+
+const authState = vi.hoisted(() => ({
+ isAuthenticated: false,
+ deviceId: 'self-device',
+ dataOwnerId: 'local-v1' as string | null,
+}));
+
+vi.mock('@/contexts/AuthContext', () => ({
+ useAuth: () => authState,
+}));
+
+import { useDeviceLinkRemoteProjects } from '@/features/device-link/useDeviceLinkRemoteProjects';
+
+const getSessionList = vi.fn(async () => {
+ throw new Error('[PERMISSION_DENIED] Device Link requires a Cindy account.');
+});
+
+beforeEach(() => {
+ vi.useFakeTimers();
+ authState.isAuthenticated = false;
+ authState.deviceId = 'self-device';
+ authState.dataOwnerId = 'local-v1';
+ getSessionList.mockClear();
+ Object.defineProperty(window, 'electronAPI', {
+ configurable: true,
+ value: {
+ deviceLink: {
+ mirrorCache: { getSessionList },
+ },
+ } as unknown as Window['electronAPI'],
+ });
+});
+
+afterEach(() => {
+ cleanup();
+ Reflect.deleteProperty(window, 'electronAPI');
+ vi.useRealTimers();
+});
+
+describe('Device Link remote projects auth gate', () => {
+ it('跳过登录进入 local mode 时不启动 session-list owner token 补读', async () => {
+ const { unmount } = renderHook(() => useDeviceLinkRemoteProjects());
+
+ await act(async () => {
+ await vi.advanceTimersByTimeAsync(60_000);
+ });
+
+ expect(getSessionList).not.toHaveBeenCalled();
+ unmount();
+ });
+});
diff --git a/apps/desktop/src/renderer/features/device-link/useDeviceLinkRemoteProjects.ts b/apps/desktop/src/renderer/features/device-link/useDeviceLinkRemoteProjects.ts
index 3558b40c23..298eef6b1a 100644
--- a/apps/desktop/src/renderer/features/device-link/useDeviceLinkRemoteProjects.ts
+++ b/apps/desktop/src/renderer/features/device-link/useDeviceLinkRemoteProjects.ts
@@ -268,9 +268,11 @@ export function useDeviceLinkRemoteProjects(): void {
// 2. 补读可能因待清队列 / owner 边界复核 / 瞬时 IPC 错误失败,失败后**重试**直到锚点
// 就位 —— 否则订阅仍持续排程 undefined 写入,冷缓存停写到重挂载(本线程)。
// 账号在重试期间再次变化时,effect 重跑清掉定时器,旧账号的重试不再继续。
- if (!dataOwnerId) return;
+ // local mode 也有 `local-v1` data owner,但没有 Device Link capability;只看 dataOwnerId
+ // 会让跳过登录的客户端无限重试一个必然被 main 以 PERMISSION_DENIED 拒绝的 IPC。
+ if (!isAuthenticated || !dataOwnerId) return;
return startSessionListTokenRefresh();
- }, [ownerBoundaryGeneration]);
+ }, [ownerBoundaryGeneration, isAuthenticated]);
useEffect(() => {
if (!isAuthenticated || !selfDeviceId) {