From 7b6cd5dcedd8daee71852e7e388653fc55c952a2 Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Thu, 12 Nov 2026 03:41:37 -0800 Subject: [PATCH] fix: retain History candidates across equivalent session batches Keep the warmed candidate projection when normalized recording hosts remain unchanged, while matching new records and refreshing real host changes. Count unrelated host visits in the existing performance regression. Co-authored-by: Wooseong Kim --- .../ai-vault-history-candidates.perf.test.tsx | 41 ++++++++++++++- .../history/ai-vault-history-candidates.ts | 50 +++++++++++++------ .../existing-workspace-content.perf.test.tsx | 2 +- 3 files changed, 74 insertions(+), 19 deletions(-) diff --git a/src/renderer/src/components/right-sidebar/ai-vault/scanning/ai-vault-history-candidates.perf.test.tsx b/src/renderer/src/components/right-sidebar/ai-vault/scanning/ai-vault-history-candidates.perf.test.tsx index 7d517729782..b2d2392acf3 100644 --- a/src/renderer/src/components/right-sidebar/ai-vault/scanning/ai-vault-history-candidates.perf.test.tsx +++ b/src/renderer/src/components/right-sidebar/ai-vault/scanning/ai-vault-history-candidates.perf.test.tsx @@ -211,6 +211,7 @@ it.each([100, 1000])( 'keeps closed-record matching current through stopped metadata and ignores attention/foreign writes among %i rows', (count) => { let candidateReads = 0 + let foreignHostReads = 0 let sessionReads = 0 let renders = 0 const records: AiVaultSession[] = Array.from({ length: count }, (_, index) => ({ @@ -224,7 +225,10 @@ it.each([100, 1000])( ...bare, isBare: false, id: `repo-1::/foreign/${index}`, - hostId: 'runtime:other' as const, + get hostId() { + foreignHostReads++ + return 'runtime:other' as const + }, get path() { candidateReads++ return `/foreign/${index}` @@ -301,6 +305,37 @@ it.each([100, 1000])( expect(candidateReads).toBe(0) expect(sessionReads).toBe(0) expect(renders).toBe(warmRenders) + const added = session('another-record', '/repo/checkout/src', 'Another record') + const acceptedRecords = [...records, added] + // This batch changes matching output, not which hosts' catalog rows can participate. + foreignHostReads = 0 + mounted.rerender({ records: acceptedRecords }) + expect(foreignHostReads).toBe(0) + expect(mounted.result.current.candidates).toBe(warm.candidates) + expect(mounted.result.current.map).not.toBe(warm.map) + expect(sessionReads).toBeGreaterThan(0) + expect(mounted.result.current.map.get(records[0].id)?.worktreeId).toBe(bareId) + expect(mounted.result.current.map.get(added.id)?.worktreeId).toBe(checkout.id) + + const foreignRecord: AiVaultSession = { + ...session('foreign-record', '/foreign/1/src', 'Foreign record'), + executionHostId: 'runtime:other' + } + foreignHostReads = 0 + mounted.rerender({ records: [...acceptedRecords, foreignRecord] }) + expect(foreignHostReads).toBeGreaterThan(0) + expect(mounted.result.current.map.get(foreignRecord.id)?.worktreeId).toBe('repo-1::/foreign/1') + const expandedCandidates = mounted.result.current.candidates + mounted.rerender({ records: [foreignRecord, ...acceptedRecords] }) + expect(mounted.result.current.candidates).toBe(expandedCandidates) + expect(mounted.result.current.map.get(foreignRecord.id)?.worktreeId).toBe('repo-1::/foreign/1') + foreignHostReads = 0 + mounted.rerender({ records: acceptedRecords }) + expect(foreignHostReads).toBeGreaterThan(0) + expect(mounted.result.current.candidates.some((row) => row.id === 'repo-1::/foreign/1')).toBe( + false + ) + expect(mounted.result.current.map.get(added.id)?.worktreeId).toBe(checkout.id) const detected = selectDetectedWorktreesByRepo(useAppStore.getState()) const listing = detected[repo.id] const changed = replaceDetectedWorktreeListing(detected, repo.id, { @@ -316,8 +351,11 @@ it.each([100, 1000])( : row ) }) + const beforeMetadata = mounted.result.current + sessionReads = 0 act(() => useAppStore.setState({ detectedWorktreesByRepo: changed })) expect(sessionReads).toBeGreaterThan(0) + expect(mounted.result.current.map).not.toBe(beforeMetadata.map) expect(mounted.result.current.map).not.toBe(warm.map) expect(mounted.result.current.map.get(records[0].id)).toMatchObject({ worktreeId: bareId, @@ -335,7 +373,6 @@ it.each([100, 1000])( })) ) expect(mounted.result.current.map.get(records[0].id)?.worktreeId).toBeUndefined() - const added = session('another-record', '/repo/checkout/src', 'Another record') mounted.rerender({ records: [...records, added] }) expect(mounted.result.current.map.get(added.id)?.worktreeId).toBe(checkout.id) } diff --git a/src/renderer/src/lib/agent-session/resume/history/ai-vault-history-candidates.ts b/src/renderer/src/lib/agent-session/resume/history/ai-vault-history-candidates.ts index 410728e9116..1079532d8f1 100644 --- a/src/renderer/src/lib/agent-session/resume/history/ai-vault-history-candidates.ts +++ b/src/renderer/src/lib/agent-session/resume/history/ai-vault-history-candidates.ts @@ -190,24 +190,42 @@ export function useAiVaultHistoryWorktrees(visible: readonly Worktree[]): readon return useAppStore((state) => select(visible, state)) } +type RecordingHosts = ReadonlySet> + +function createAiVaultHistoryRecordCandidateSelector() { + let previousHosts: RecordingHosts | undefined + let select: ReturnType | undefined + return (worktrees: readonly Worktree[], state: Inputs, hosts: RecordingHosts) => { + if ( + !select || + (previousHosts !== hosts && + (previousHosts?.size !== hosts.size || + [...hosts].some((host) => !previousHosts?.has(host)))) + ) { + const includes = (row: DetectedWorktree): boolean => { + const host = normalizeExecutionHostId(row.hostId) + return hosts.size > 0 && (!host || hosts.has(null) || hosts.has(host)) + } + select = createAiVaultHistoryCandidateSelector( + (visible, inputs) => aiVaultHistoryRecordWorktrees(visible, inputs, includes), + (previous, next) => + includes(previous) === includes(next) && + (!includes(next) || sameDetectedRecordCandidate(previous, next)) + ) + } + previousHosts = hosts + return select(worktrees, state) + } +} + export function useAiVaultHistoryRecordWorktrees( worktrees: readonly Worktree[], sessions: readonly AiVaultSession[] ): readonly Worktree[] { - const select = useMemo(() => { - const hosts = new Set( - sessions.map((session) => normalizeExecutionHostId(session.executionHostId)) - ) - const includes = (row: DetectedWorktree): boolean => { - const host = normalizeExecutionHostId(row.hostId) - return hosts.size > 0 && (!host || hosts.has(null) || hosts.has(host)) - } - return createAiVaultHistoryCandidateSelector( - (visible, state) => aiVaultHistoryRecordWorktrees(visible, state, includes), - (previous, next) => - includes(previous) === includes(next) && - (!includes(next) || sameDetectedRecordCandidate(previous, next)) - ) - }, [sessions]) - return useAppStore((state) => select(worktrees, state)) + const hosts = useMemo( + () => new Set(sessions.map((session) => normalizeExecutionHostId(session.executionHostId))), + [sessions] + ) + const select = useMemo(() => createAiVaultHistoryRecordCandidateSelector(), []) + return useAppStore((state) => select(worktrees, state, hosts)) } diff --git a/src/renderer/src/lib/workspace/tabs/existing-workspace-content.perf.test.tsx b/src/renderer/src/lib/workspace/tabs/existing-workspace-content.perf.test.tsx index 3274e7298e0..a7b45ac1c02 100644 --- a/src/renderer/src/lib/workspace/tabs/existing-workspace-content.perf.test.tsx +++ b/src/renderer/src/lib/workspace/tabs/existing-workspace-content.perf.test.tsx @@ -198,7 +198,7 @@ it.each([ const entries = Object.entries let unrelatedEnumeratedBuckets = 0 - const enumeration = vi.spyOn(Object, 'entries').mockImplementation((object: object) => { + const enumeration = vi.spyOn(Object, 'entries').mockImplementation((object) => { const result = entries(object) if (isConsumerRead()) { for (const [workspaceId] of result) {