Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
perf(desktop): PR 状态刷新加两道省流门控——失焦暂停、断线跳过
Dash0f96650ce9cf
diff --git a/apps/desktop/src/renderer/__tests__/prRefsRefreshGating.test.ts b/apps/desktop/src/renderer/__tests__/prRefsRefreshGating.test.tsnew file mode 100644index 0000000000..e36cfcfc04--- /dev/null+++ b/apps/desktop/src/renderer/__tests__/prRefsRefreshGating.test.ts@@ -0,0 +1,68 @@+/**+ * prRefsRefreshGating.test.ts+ * ---------------------------------------------------------------------------+ * PR 状态刷新的两道省流门控(2026-08-13 用户裁决):+ * 1. 窗口失焦 / 隐藏时跳过 90s 周期刷新——没人在看,后台空转的查询(GitHub+ * 配额 + device-link 隧道)纯属浪费;回到前台由既有的 focus 监听立即全量+ * 补一次,数据不停留在过期态。+ * 2. 设备明确断线时跳过远程隧道查询——失败路径刻意不写 TTL 时间戳(瞬断要+ * 立即重试),但长离线下就成了每周期一轮注定失败的调用 + 告警日志;断线+ * 标记本地同步可得,先看一眼再发。fail-open:shard 缺失(尚未建立 / 设备+ * 已移除)照常尝试,不能把首次查询吞掉。+ *+ * 门控位置用静态扫描守卫(renderer 测试环境无 jsdom;跳过行为的端到端用例见+ * hooks/__tests__/useSessionGitContext.remote.test.ts);断线判定本身是纯 store+ * 读取,直接行为测试。+ */++import { describe, it, expect } from 'vitest';+import { readFileSync } from 'node:fs';+import { resolve } from 'node:path';++import {+ isRemoteDeviceMarkedDisconnected,+ remoteProjectsStore,+} from '@/features/device-link/remoteProjectsStore';++const read = (...seg: string[]) => readFileSync(resolve(__dirname, '..', ...seg), 'utf8');++const prRefsSource = read('contexts', 'PrRefsContext.tsx');++describe('PR 状态刷新的省流门控', () => {+ it('周期刷新在失焦/隐藏时跳过,聚焦监听保留(回前台即补)', () => {+ // 守卫必须在 refreshAll 开头——插在循环里或别处等于每会话重复判断/漏判。+ expect(prRefsSource).toMatch(+ /const refreshAll = \(\) => \{\s*\n(?:\s*\/\/[^\n]*\n)*\s*if \(typeof document !== 'undefined' && \(document\.hidden \|\| !document\.hasFocus\(\)\)\) return;/,+ );+ // 暂停的前提:focus 时立即全量刷新的监听还在。删掉它,后台停摆会延续到下个周期。+ expect(prRefsSource).toContain("window.addEventListener('focus', refreshAll)");+ });++ it('远程引用与状态查询都先看设备断线标记', () => {+ // 引用拉取(fetchRefsForRemoteSession):deviceId 是必传参,直接判。+ expect(prRefsSource).toContain('if (isRemoteDeviceMarkedDisconnected(deviceId)) return;');+ // 状态查询(fetchStatusesForRefs 的远程分支):deviceId 可空,先短路。+ expect(prRefsSource).toContain(+ 'if (deviceId && isRemoteDeviceMarkedDisconnected(deviceId)) return;',+ );+ });++ it('断线判定只认明确的 disconnected 标记(fail-open)', () => {+ const deviceId = 'pr-gating-test-device';+ try {+ // shard 不存在 → false(首次查询不能被吞掉)。+ expect(isRemoteDeviceMarkedDisconnected(deviceId)).toBe(false);+ // 权威列表到达 = connected。+ remoteProjectsStore.setDeviceSessions(deviceId, 'Test Device', []);+ expect(isRemoteDeviceMarkedDisconnected(deviceId)).toBe(false);+ // 断线快照仍在侧栏展示,正是要跳过的长离线场景。+ remoteProjectsStore.markDeviceDisconnected(deviceId);+ expect(isRemoteDeviceMarkedDisconnected(deviceId)).toBe(true);+ // 重连(权威列表再次到达)即恢复。+ remoteProjectsStore.setDeviceSessions(deviceId, 'Test Device', []);+ expect(isRemoteDeviceMarkedDisconnected(deviceId)).toBe(false);+ } finally {+ remoteProjectsStore.removeDevice(deviceId);+ }+ });+});diff --git a/apps/desktop/src/renderer/contexts/PrRefsContext.tsx b/apps/desktop/src/renderer/contexts/PrRefsContext.tsxindex 76619e1c16..10ecb44c29 100644--- a/apps/desktop/src/renderer/contexts/PrRefsContext.tsx+++ b/apps/desktop/src/renderer/contexts/PrRefsContext.tsx@@ -28,6 +28,7 @@ import {import type { PrStatusResult, SessionPrRef } from '@/lib/gitContext.types';import { prStatusKey, MAX_STATUS_QUERIES, PR_STATUS_REFRESH_INTERVAL_MS } from '@/lib/prStatus';import { useAuth } from '@/contexts/AuthContext';+import { isRemoteDeviceMarkedDisconnected } from '@/features/device-link/remoteProjectsStore';import { createLogger } from '@/lib/logger';const log = createLogger('PrRefsContext');@@ -214,6 +215,9 @@ export function PrRefsProvider({ children }: { children: ReactNode }) {const deviceId =remoteDeviceBySession.current.get(sessionId) ??prConsumers.current.get(sessionId)?.deviceId;+ // 设备明确断线时不发注定失败的隧道调用(fail-open:shard 缺失照常尝试)。+ // 不写任何状态,重连后的下一个触发点(周期 / 聚焦 / 引用到位)自然恢复。+ if (deviceId && isRemoteDeviceMarkedDisconnected(deviceId)) return;const results = deviceId? ((await window.electronAPI.deviceLink.invoke(deviceId, 'git-context:pr-status', [{ sessionId, queries },@@ -242,6 +246,11 @@ export function PrRefsProvider({ children }: { children: ReactNode }) {// 但**不是终态**:下个周期重查(远端新增 PR / 短暂竞态都靠这条自愈)。const fetchRefsForRemoteSession = useRef((sessionId: string, deviceId: string) => {remoteDeviceBySession.current.set(sessionId, deviceId);+ // 设备明确断线 → 跳过(簿记保留,供状态查询路由)。此前失败路径刻意不写时间戳+ // 以便瞬断立即重试,但长离线下就成了每个周期一轮注定失败的隧道调用 + 告警日志;+ // 断线判定本地同步可得,先看一眼再发(2026-08-13 用户裁决)。fail-open:+ // shard 缺失(尚未建立 / 设备已移除)照常尝试,语义见 isRemoteDeviceMarkedDisconnected。+ if (isRemoteDeviceMarkedDisconnected(deviceId)) return;if (remoteRefsInFlight.current.has(sessionId)) return;// TTL 门:距上次成功不足一个刷新周期就跳过(interval / 聚焦 / 行重挂载都会// 频繁触发,靠它避免风暴);失败路径不写时间戳,天然立即可重试。@@ -307,6 +316,11 @@ export function PrRefsProvider({ children }: { children: ReactNode }) {// 等远端状态变化也随节拍收敛(main / 被控端各有 60s TTL,重复查询便宜)。useEffect(() => {const refreshAll = () => {+ // 失焦/隐藏时跳过周期刷新:没人在看,后台空转的查询(GitHub 配额 ++ // device-link 隧道)纯属浪费;下面的 focus 监听会在回到前台的瞬间全量补一次,+ // 数据不会停留在过期态(2026-08-13 用户裁决)。focus 事件触发的调用天然+ // 通过本判断(事件发生时已聚焦)。+ if (typeof document !== 'undefined' && (document.hidden || !document.hasFocus())) return;for (const [sessionId, entry] of prConsumers.current) {refreshConsumer(sessionId, entry.deviceId);}diff --git a/apps/desktop/src/renderer/features/device-link/remoteProjectsStore.ts b/apps/desktop/src/renderer/features/device-link/remoteProjectsStore.tsindex 84803e9c0c..d152806399 100644--- a/apps/desktop/src/renderer/features/device-link/remoteProjectsStore.ts+++ b/apps/desktop/src/renderer/features/device-link/remoteProjectsStore.ts@@ -1048,3 +1048,15 @@ export const remoteProjectsStore = { ...actions, subscribe, subscribeRename };export function getSessionDeviceId(sessionId: string): string | undefined {return actions.getSessionDeviceId(sessionId);}++/**+ * 同步读:该设备的 shard 是否**明确标记为断线**。给周期性远程调用(如 PR 状态+ * 刷新)做"注定失败就别发"的前置判断。语义刻意收窄成三值里只认一种:+ * - 'disconnected' → true(断线快照仍在侧栏展示,正是要跳过的长离线场景);+ * - 'connected' 或 shard 不存在 → false(不知道就照常尝试,fail-open——+ * shard 尚未建立时不能把首次查询吞掉,设备已移除时会话行随之消失、+ * 消费者自然注销,不需要这里兜)。+ */+export function isRemoteDeviceMarkedDisconnected(deviceId: string): boolean {+ return shards.get(deviceId)?.connectionStatus === 'disconnected';+}diff --git a/apps/desktop/src/renderer/hooks/__tests__/useSessionGitContext.remote.test.ts b/apps/desktop/src/renderer/hooks/__tests__/useSessionGitContext.remote.test.tsindex 84c6d7dffa..2e0217e99c 100644--- a/apps/desktop/src/renderer/hooks/__tests__/useSessionGitContext.remote.test.ts+++ b/apps/desktop/src/renderer/hooks/__tests__/useSessionGitContext.remote.test.ts@@ -19,6 +19,7 @@ vi.mock('@/contexts/AuthContext', () => ({import { useSessionGitContext } from '../useSessionGitContext';import { PrRefsProvider } from '@/contexts/PrRefsContext';+import { remoteProjectsStore } from '@/features/device-link/remoteProjectsStore';/** 统一管线经 PrRefsProvider 拉取 PR;hook 测试统一套一层 provider。 */const wrapper = ({ children }: { children: ReactNode }) =>@@ -236,4 +237,50 @@ describe('useSessionGitContext remote routing', () => {});unmount();});++ // 2026-08-13 用户裁决:设备明确断线时不发注定失败的 PR 隧道查询(fail-open:+ // shard 缺失照常尝试,见 prRefsRefreshGating.test.ts 的判定语义)。+ it('被控端标记断线时跳过 PR 引用的隧道查询,重连后恢复', async () => {+ const gitContext = makeGitContext();+ const invoke = vi.fn(async (_deviceId: string, channel: string) => {+ if (channel === 'git-context:get-for-session') {+ return {+ workdir: '/controlled/repo',+ head: { kind: 'branch', branch: 'feature/offline', shortSha: null },+ source: 'remote',+ };+ }+ return [];+ });+ window.electronAPI = {+ gitContext,+ deviceLink: { invoke },+ } as never;++ const session = { ...sessionBase, deviceLinkDeviceId: 'device-1' };+ try {+ // 断线快照:shard 仍在(会话行留在侧栏),连接标记为 disconnected。+ remoteProjectsStore.setDeviceSessions('device-1', 'Test Device', [session]);+ remoteProjectsStore.markDeviceDisconnected('device-1');++ const first = renderHook(() => useSessionGitContext(session), { wrapper });+ await waitFor(() => expect(first.result.current.head?.branch).toBe('feature/offline'));+ // HEAD 走独立通道不受门控;PR 引用的隧道查询被跳过。+ expect(invoke).not.toHaveBeenCalledWith('device-1', 'git-context:pr-refs:list', [+ 'session-1',+ ]);+ first.unmount();++ // 重连(权威列表再次到达)→ 下一个触发点(这里用重挂载等价周期/聚焦刷新)恢复查询。+ remoteProjectsStore.setDeviceSessions('device-1', 'Test Device', [session]);+ const second = renderHook(() => useSessionGitContext(session), { wrapper });+ await waitFor(() => {+ expect(invoke).toHaveBeenCalledWith('device-1', 'git-context:pr-refs:list', ['session-1']);+ });+ second.unmount();+ } finally {+ // 清掉 shard,别让断线标记漂进同文件其它用例。+ remoteProjectsStore.removeDevice('device-1');+ }+ });});