From 20319c277cf46f8c262a0b7b9255bb0b37de847a Mon Sep 17 00:00:00 2001 From: Jinwoo-H Date: Wed, 13 May 2026 00:17:44 -0400 Subject: [PATCH] Refresh PR state on worktree activation Co-authored-by: Orca --- .../src/components/sidebar/WorktreeList.tsx | 3 +- src/renderer/src/store/slices/github.test.ts | 51 +++++++++++++++++++ src/renderer/src/store/slices/github.ts | 13 ++--- src/renderer/src/store/slices/worktrees.ts | 5 +- 4 files changed, 60 insertions(+), 12 deletions(-) diff --git a/src/renderer/src/components/sidebar/WorktreeList.tsx b/src/renderer/src/components/sidebar/WorktreeList.tsx index 59fc3b5e3..d10498b45 100644 --- a/src/renderer/src/components/sidebar/WorktreeList.tsx +++ b/src/renderer/src/components/sidebar/WorktreeList.tsx @@ -390,7 +390,7 @@ const VirtualizedWorktreeViewport = React.memo(function VirtualizedWorktreeViewp const virtualItems = virtualizer.getVirtualItems() useEffect(() => { - if (!cardProps.includes('pr') && !cardProps.includes('ci')) { + if (groupBy !== 'pr-status' && !cardProps.includes('pr') && !cardProps.includes('ci')) { if (lastVisibleRefreshKeyRef.current !== '__hidden__') { lastVisibleRefreshKeyRef.current = '__hidden__' reportVisibleGitHubPRRefreshCandidates([], Date.now()) @@ -420,6 +420,7 @@ const VirtualizedWorktreeViewport = React.memo(function VirtualizedWorktreeViewp reportVisibleGitHubPRRefreshCandidates(visibleWorktreeIds, Date.now()) }, [ cardProps, + groupBy, reportVisibleGitHubPRRefreshCandidates, rows, sshConnectedGeneration, diff --git a/src/renderer/src/store/slices/github.test.ts b/src/renderer/src/store/slices/github.test.ts index 3229a588d..775239af3 100644 --- a/src/renderer/src/store/slices/github.test.ts +++ b/src/renderer/src/store/slices/github.test.ts @@ -10,6 +10,7 @@ import type { PRInfo } from '../../../../shared/types' const mockApi = { gh: { prForBranch: vi.fn().mockResolvedValue(null), + enqueuePRRefresh: vi.fn().mockResolvedValue(undefined), issue: vi.fn().mockResolvedValue(null), prChecks: vi.fn().mockResolvedValue([]), listWorkItems: vi.fn() @@ -262,6 +263,56 @@ describe('createGitHubSlice.fetchPRForBranch', () => { }) }) +describe('createGitHubSlice.refreshGitHubForWorktreeIfStale', () => { + beforeEach(() => { + vi.clearAllMocks() + }) + + it('enqueues active PR refresh even when the cached PR is fresh', () => { + const store = createTestStore() + const repoPath = '/repo' + const branch = 'feature/test' + const worktreeId = 'wt-1' + + store.setState({ + repos: [{ id: 'repo-1', path: repoPath, name: 'repo', kind: 'git' }], + worktreesByRepo: { + 'repo-1': [ + { + id: worktreeId, + repoId: 'repo-1', + path: '/repo/worktrees/test', + branch, + displayName: 'test', + isMainWorktree: false, + isBare: false, + isArchived: false + } + ] + }, + prCache: { + [`${repoPath}::${branch}`]: { + data: makePR({ state: 'open' }), + fetchedAt: Date.now() + } + } + } as unknown as Partial) + + store.getState().refreshGitHubForWorktreeIfStale(worktreeId) + + expect(mockApi.gh.enqueuePRRefresh).toHaveBeenCalledWith({ + candidate: expect.objectContaining({ + repoPath, + branch, + cacheKey: `${repoPath}::${branch}`, + cachedPRState: 'open' + }), + reason: 'active', + priority: 80 + }) + }) +}) + describe('createGitHubSlice.fetchWorkItems source/error envelope', () => { beforeEach(() => { vi.clearAllMocks() diff --git a/src/renderer/src/store/slices/github.ts b/src/renderer/src/store/slices/github.ts index dd78cf18c..4496e6443 100644 --- a/src/renderer/src/store/slices/github.ts +++ b/src/renderer/src/store/slices/github.ts @@ -1802,9 +1802,9 @@ export const createGitHubSlice: StateCreator = (s }) }, - // Why: worktree switches previously force-refreshed GitHub data on every - // click, bypassing the 5-min TTL. This variant only fetches when stale, - // avoiding unnecessary API calls and latency during rapid switching. + // Why: activation is the user's strongest freshness signal. A PR can merge + // seconds after the last sidebar poll; enqueue through the coordinator so + // clicks revalidate PR state without bypassing coalescing/rate-limit guards. refreshGitHubForWorktreeIfStale: (worktreeId) => { const state = get() let worktree: Worktree | undefined @@ -1823,13 +1823,9 @@ export const createGitHubSlice: StateCreator = (s return } - const now = Date.now() const branch = worktree.branch.replace(/^refs\/heads\//, '') - const prKey = `${repo.path}::${branch}` - const prEntry = state.prCache[prKey] - const prStale = !prEntry || now - prEntry.fetchedAt >= CACHE_TTL - if (!worktree.isBare && branch && prStale) { + if (!worktree.isBare && branch) { const candidate = buildPRRefreshCandidate(state, worktree) if (candidate) { void window.api.gh.enqueuePRRefresh?.({ candidate, reason: 'active', priority: 80 }) @@ -1837,6 +1833,7 @@ export const createGitHubSlice: StateCreator = (s } if (worktree.linkedIssue) { + const now = Date.now() const issueKey = `${repo.path}::${worktree.linkedIssue}` const issueEntry = state.issueCache[issueKey] if (!issueEntry || now - issueEntry.fetchedAt >= CACHE_TTL) { diff --git a/src/renderer/src/store/slices/worktrees.ts b/src/renderer/src/store/slices/worktrees.ts index 5d5f98b75..54833980d 100644 --- a/src/renderer/src/store/slices/worktrees.ts +++ b/src/renderer/src/store/slices/worktrees.ts @@ -926,9 +926,8 @@ export const createWorktreeSlice: StateCreator } }) - // Why: force-refreshing GitHub data on every switch burned API rate limit - // quota and added 200-800ms latency. Only refresh when cache is actually - // stale (>5 min old). Users can still force-refresh via the sidebar button. + // Why: activation is explicit enough to revalidate PR state immediately; + // the GitHub coordinator still coalesces requests and applies rate guards. if (worktreeId) { get().refreshGitHubForWorktreeIfStale(worktreeId) }