From 5b7611ddcb7bfde023d075e0b497c300eab179ec Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Tue, 30 Jun 2026 00:55:54 -0700 Subject: [PATCH] fix(win): release PR-refresh aliases when a worktree is removed (#6861) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The OOM reports (F0BDMD16LJ2 and the taifunk many-worktree sessions) show memory creeping over long sessions with churning worktrees. One contributor: in pr-refresh-coordinator, many local worktrees that track the same linked PR coalesce into a single queue entry whose 'aliases' map keeps one entry per worktree. Aliases were only pruned when a candidate was re-enqueued as invalid — never when a worktree was simply removed/closed — so the maps grew unbounded across a session. Add pruneWorktreePRRefreshAliases(worktreeId) and call it from removeWorktreeMetadataAndTransientState (the existing central worktree-removal cleanup, alongside removeWorktreeMeta / forgetWorktree / deleteWorktreeHistoryDir). It drops the removed worktree's aliases, deletes the queue entry when none remain, and rebinds the representative candidate if the removed worktree owned it. Covered by 3 new coordinator tests (accumulate-then-prune, keep-entry- on-remaining-aliases with candidate rebind, no-op for unknown worktree) plus two test-only inspection helpers. Co-authored-by: Neil --- .../github/pr-refresh-coordinator.test.ts | 61 +++++++++++++++++++ src/main/github/pr-refresh-coordinator.ts | 52 ++++++++++++++++ src/main/ipc/worktrees.ts | 4 ++ 3 files changed, 117 insertions(+) diff --git a/src/main/github/pr-refresh-coordinator.test.ts b/src/main/github/pr-refresh-coordinator.test.ts index a341e097b..b7d82ea14 100644 --- a/src/main/github/pr-refresh-coordinator.test.ts +++ b/src/main/github/pr-refresh-coordinator.test.ts @@ -917,4 +917,65 @@ describe('pr-refresh-coordinator', () => { expect(getPRForBranchOutcomeMock).toHaveBeenCalledTimes(3) }) + + describe('pruneWorktreePRRefreshAliases', () => { + // Several local worktrees tracking the same linked PR coalesce into one + // queue entry (same refreshKey) whose alias map keeps one entry each. + const LINKED_PR_KEY = 'local::runtime:host::/repo::pr::42' + + function makeLinkedCandidate(worktreeId: string): GitHubPRRefreshCandidate { + return makeCandidate({ + worktreeId, + cacheKey: `/repo::${worktreeId}`, + branch: `feature/${worktreeId}`, + linkedPRNumber: 42 + }) + } + + it('drops a removed worktree alias and deletes the entry when it was the last', async () => { + const { enqueuePRRefresh, pruneWorktreePRRefreshAliases, _getPRRefreshAliasCountForTests } = + await import('./pr-refresh-coordinator') + + enqueuePRRefresh(makeLinkedCandidate('wt-1'), 'visible', 40, 1) + enqueuePRRefresh(makeLinkedCandidate('wt-2'), 'visible', 40, 1) + enqueuePRRefresh(makeLinkedCandidate('wt-3'), 'visible', 40, 1) + expect(_getPRRefreshAliasCountForTests(LINKED_PR_KEY)).toBe(3) + + pruneWorktreePRRefreshAliases('wt-2') + expect(_getPRRefreshAliasCountForTests(LINKED_PR_KEY)).toBe(2) + + pruneWorktreePRRefreshAliases('wt-1') + pruneWorktreePRRefreshAliases('wt-3') + // Last alias gone -> the whole queue entry is dropped. + expect(_getPRRefreshAliasCountForTests(LINKED_PR_KEY)).toBe(0) + }) + + it('keeps the entry alive and rebinds the candidate when other aliases remain', async () => { + const { + enqueuePRRefresh, + pruneWorktreePRRefreshAliases, + _getPRRefreshAliasCountForTests, + _getPRRefreshQueueSizeForTests + } = await import('./pr-refresh-coordinator') + + // wt-1 becomes the entry's representative candidate (enqueued first). + enqueuePRRefresh(makeLinkedCandidate('wt-1'), 'visible', 40, 1) + enqueuePRRefresh(makeLinkedCandidate('wt-2'), 'visible', 40, 1) + expect(_getPRRefreshQueueSizeForTests()).toBe(1) + + // Removing the representative worktree must not orphan the entry. + pruneWorktreePRRefreshAliases('wt-1') + expect(_getPRRefreshAliasCountForTests(LINKED_PR_KEY)).toBe(1) + expect(_getPRRefreshQueueSizeForTests()).toBe(1) + }) + + it('is a no-op for a worktree with no queued aliases', async () => { + const { enqueuePRRefresh, pruneWorktreePRRefreshAliases, _getPRRefreshAliasCountForTests } = + await import('./pr-refresh-coordinator') + + enqueuePRRefresh(makeLinkedCandidate('wt-1'), 'visible', 40, 1) + pruneWorktreePRRefreshAliases('wt-unknown') + expect(_getPRRefreshAliasCountForTests(LINKED_PR_KEY)).toBe(1) + }) + }) }) diff --git a/src/main/github/pr-refresh-coordinator.ts b/src/main/github/pr-refresh-coordinator.ts index fc266ee5c..fccc42267 100644 --- a/src/main/github/pr-refresh-coordinator.ts +++ b/src/main/github/pr-refresh-coordinator.ts @@ -148,6 +148,50 @@ export function clearVisiblePRRefreshWindow(windowId: number): void { } } +/** + * Drop a removed worktree's aliases from every queue entry. + * + * Why: many local branches/worktrees that resolve to the same PR coalesce into + * one queue entry whose `aliases` map keeps one entry per worktree. Aliases are + * only pruned when a candidate is re-enqueued as invalid — never when a worktree + * is simply removed. Over a long session with churning worktrees these maps grow + * unbounded, contributing to the renderer/process memory creep behind the OOM + * reports. Called from worktree removal so stale aliases are released promptly. + */ +export function pruneWorktreePRRefreshAliases(worktreeId: string): void { + for (const [key, entry] of queue) { + let removed = false + for (const [cacheKey, alias] of entry.aliases) { + if (alias.worktreeId === worktreeId) { + entry.aliases.delete(cacheKey) + removed = true + } + } + if (!removed) { + continue + } + // No aliases left means no worktree still cares about this refresh. + if (entry.aliases.size === 0) { + queue.delete(key) + errorBackoff.delete(key) + continue + } + // Keep the entry alive but ensure its representative candidate is not a + // dangling reference to the removed worktree. + if (entry.candidate.worktreeId === worktreeId) { + const replacementAlias = entry.aliases.values().next().value + if (replacementAlias) { + entry.candidate = { + ...entry.candidate, + cacheKey: replacementAlias.cacheKey, + branch: replacementAlias.branch, + worktreeId: replacementAlias.worktreeId + } + } + } + } +} + function nextSequence(): number { sequence += 1 return sequence @@ -809,6 +853,14 @@ export function _getPRRefreshErrorBackoffCountForTests(): number { return errorBackoff.size } +export function _getPRRefreshQueueSizeForTests(): number { + return queue.size +} + +export function _getPRRefreshAliasCountForTests(key: string): number { + return queue.get(key)?.aliases.size ?? 0 +} + export async function refreshPRNow(candidate: GitHubPRRefreshCandidate): Promise { const alias = aliasFromCandidate(candidate) const key = refreshKey(candidate) diff --git a/src/main/ipc/worktrees.ts b/src/main/ipc/worktrees.ts index 7307026cc..890449f51 100644 --- a/src/main/ipc/worktrees.ts +++ b/src/main/ipc/worktrees.ts @@ -44,6 +44,7 @@ import { gitExecFileAsync } from '../git/runner' import { withWorktreeSpan } from '../observability/instrumentation' import { resolveGitHubPrStartPoint } from '../github/pr-start-point' import { fetchPrHeadTrackingRef } from '../github/pr-head-tracking-ref' +import { pruneWorktreePRRefreshAliases } from '../github/pr-refresh-coordinator' import { getDefaultRemote } from '../git/repo' import { listRepoWorktrees } from '../repo-worktrees' import { getSshGitProvider, requireSshGitProvider } from '../providers/ssh-git-dispatch' @@ -161,6 +162,9 @@ function removeWorktreeMetadataAndTransientState(store: Store, worktreeId: strin store.removeWorktreeMeta(worktreeId) advertisedUrlWatcher.forgetWorktree(worktreeId) deleteWorktreeHistoryDir(worktreeId) + // Why: release the removed worktree's PR-refresh aliases so coalesced queue + // entries do not retain it for the rest of the session (memory creep). + pruneWorktreePRRefreshAliases(worktreeId) } async function closeLocalWatcherForRemoval(worktreePath: string): Promise {