From 81e850d0af8bc57ae011b2bdc0c7990e494a19c1 Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Sun, 31 May 2026 03:40:28 -0700 Subject: [PATCH] fix: prune github owner repo cache (#4081) --- src/main/github/gh-utils.test.ts | 24 ++++++++++++++++++++++ src/main/github/gh-utils.ts | 34 ++++++++++++++++++++++++++------ 2 files changed, 52 insertions(+), 6 deletions(-) diff --git a/src/main/github/gh-utils.test.ts b/src/main/github/gh-utils.test.ts index a0705b476..8a2a313f6 100644 --- a/src/main/github/gh-utils.test.ts +++ b/src/main/github/gh-utils.test.ts @@ -15,6 +15,7 @@ vi.mock('../providers/ssh-git-dispatch', () => ({ })) import { + _getOwnerRepoCacheSize, _resetOwnerRepoCache, classifyGhError, classifyListIssuesError, @@ -166,6 +167,29 @@ describe('github owner/repo resolution', () => { await expect(getOwnerRepo('/repo', 'ssh-1')).resolves.toEqual({ owner: 'remote', repo: 'orca' }) }) + it('prunes expired distinct owner/repo cache entries on later lookups', async () => { + const nowSpy = vi.spyOn(Date, 'now') + try { + nowSpy.mockReturnValue(1_000) + gitExecFileAsyncMock.mockResolvedValueOnce({ + stdout: 'git@github.com:stablyai/orca.git\n' + }) + await expect(getOwnerRepo('/repo-a')).resolves.toEqual({ owner: 'stablyai', repo: 'orca' }) + expect(_getOwnerRepoCacheSize()).toBe(1) + + nowSpy.mockReturnValue(32_000) + gitExecFileAsyncMock.mockResolvedValueOnce({ + stdout: 'git@github.com:acme/widgets.git\n' + }) + await expect(getOwnerRepo('/repo-b')).resolves.toEqual({ owner: 'acme', repo: 'widgets' }) + + expect(_getOwnerRepoCacheSize()).toBe(1) + expect(gitExecFileAsyncMock).toHaveBeenCalledTimes(2) + } finally { + nowSpy.mockRestore() + } + }) + it('resolves PR candidates as upstream then origin and de-dupes matching slugs', async () => { gitExecFileAsyncMock .mockResolvedValueOnce({ stdout: 'git@github.com:Acme/Orca.git\n' }) diff --git a/src/main/github/gh-utils.ts b/src/main/github/gh-utils.ts index 2a98bb517..5bb3572a4 100644 --- a/src/main/github/gh-utils.ts +++ b/src/main/github/gh-utils.ts @@ -138,6 +138,7 @@ export function ghRepoExecOptions(context: GitHubRepoContext): { } const OWNER_REPO_CACHE_TTL_MS = 30_000 +const OWNER_REPO_CACHE_MAX_ENTRIES = 512 type OwnerRepoCacheEntry = { value: OwnerRepo | null @@ -151,6 +152,26 @@ export function _resetOwnerRepoCache(): void { ownerRepoCache.clear() } +/** @internal — exposed for tests only */ +export function _getOwnerRepoCacheSize(): number { + return ownerRepoCache.size +} + +function pruneOwnerRepoCache(now: number): void { + for (const [key, entry] of ownerRepoCache) { + if (entry.expiresAt <= now) { + ownerRepoCache.delete(key) + } + } + while (ownerRepoCache.size > OWNER_REPO_CACHE_MAX_ENTRIES) { + const oldestKey = ownerRepoCache.keys().next().value + if (oldestKey === undefined) { + return + } + ownerRepoCache.delete(oldestKey) + } +} + export function parseGitHubOwnerRepo(remoteUrl: string): OwnerRepo | null { const identity = parseGitHubRemoteIdentity(remoteUrl) if (!identity || identity.host.toLowerCase() !== 'github.com') { @@ -222,27 +243,28 @@ export async function getOwnerRepoForRemote( ): Promise { const context = githubRepoContext(repoPath, connectionId) const cacheKey = `${context.connectionId ?? 'local'}\0${context.repoPath}\0${remoteName}` + const now = Date.now() + pruneOwnerRepoCache(now) const cached = ownerRepoCache.get(cacheKey) - if (cached && cached.expiresAt > Date.now()) { + if (cached && cached.expiresAt > now) { return cached.value } - if (cached) { - ownerRepoCache.delete(cacheKey) - } try { const remoteUrl = await getRemoteUrlForRepo(context, remoteName) const result = remoteUrl ? parseGitHubOwnerRepo(remoteUrl) : null if (result) { ownerRepoCache.set(cacheKey, { value: result, - expiresAt: Date.now() + OWNER_REPO_CACHE_TTL_MS + expiresAt: now + OWNER_REPO_CACHE_TTL_MS }) + pruneOwnerRepoCache(now) return result } } catch { // ignore — non-GitHub remote or no remote } - ownerRepoCache.set(cacheKey, { value: null, expiresAt: Date.now() + OWNER_REPO_CACHE_TTL_MS }) + ownerRepoCache.set(cacheKey, { value: null, expiresAt: now + OWNER_REPO_CACHE_TTL_MS }) + pruneOwnerRepoCache(now) return null }