fix(win): release PR-refresh aliases when a worktree is removed (#6861)

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 <neil@stably.ai>
This commit is contained in:
Brennan Benson
2026-06-30 00:55:54 -07:00
committed by GitHub
co-authored by Neil
parent 9078ecaef5
commit 5b7611ddcb
3 changed files with 117 additions and 0 deletions
@@ -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)
})
})
})
+52
View File
@@ -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<PRRefreshOutcome> {
const alias = aliasFromCandidate(candidate)
const key = refreshKey(candidate)