From e3202012ed28e6def1a665b3441cbce19857361f Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Sun, 31 May 2026 03:37:32 -0700 Subject: [PATCH] fix: prune smart github submit cache (#4079) --- .../src/lib/smart-github-submit.test.ts | 46 +++++++++++++++++++ src/renderer/src/lib/smart-github-submit.ts | 22 +++++++++ 2 files changed, 68 insertions(+) diff --git a/src/renderer/src/lib/smart-github-submit.test.ts b/src/renderer/src/lib/smart-github-submit.test.ts index 847a3a375..059d7770e 100644 --- a/src/renderer/src/lib/smart-github-submit.test.ts +++ b/src/renderer/src/lib/smart-github-submit.test.ts @@ -3,6 +3,7 @@ import { clearSmartGitHubSubmitLookupCacheForTests, getSmartGitHubSubmitIntent, getSmartGitHubSubmitResolution, + getSmartGitHubSubmitLookupCacheSizeForTests, lookupSmartGitHubSubmitItem } from './smart-github-submit' @@ -179,6 +180,51 @@ describe('lookupSmartGitHubSubmitItem', () => { expect(workItemByOwnerRepo).toHaveBeenCalledTimes(2) expect(workItem).not.toHaveBeenCalled() }) + + it('prunes expired distinct lookup entries on later lookups', async () => { + const nowSpy = vi.spyOn(Date, 'now') + const workItemByOwnerRepo = vi.fn() + const workItem = vi.fn().mockImplementation(({ number }) => + Promise.resolve({ + id: `issue-${number}`, + type: 'issue' as const, + number, + title: `Issue ${number}`, + state: 'open' as const, + url: `https://github.com/stablyai/orca/issues/${number}`, + labels: [], + updatedAt: '2026-05-26T00:00:00.000Z', + author: 'octocat', + repoId: 'repo-1' + }) + ) + + try { + nowSpy.mockReturnValue(1_000) + await lookupSmartGitHubSubmitItem({ + repoId: 'repo-1', + repoPath: '/repo', + intent: { kind: 'hash-number', number: 2049 }, + workItem, + workItemByOwnerRepo + }) + expect(getSmartGitHubSubmitLookupCacheSizeForTests()).toBe(1) + + nowSpy.mockReturnValue(62_000) + await lookupSmartGitHubSubmitItem({ + repoId: 'repo-1', + repoPath: '/repo', + intent: { kind: 'hash-number', number: 2050 }, + workItem, + workItemByOwnerRepo + }) + + expect(getSmartGitHubSubmitLookupCacheSizeForTests()).toBe(1) + expect(workItem).toHaveBeenCalledTimes(2) + } finally { + nowSpy.mockRestore() + } + }) }) describe('getSmartGitHubSubmitResolution', () => { diff --git a/src/renderer/src/lib/smart-github-submit.ts b/src/renderer/src/lib/smart-github-submit.ts index 17b8aa2e6..cc20ef0d7 100644 --- a/src/renderer/src/lib/smart-github-submit.ts +++ b/src/renderer/src/lib/smart-github-submit.ts @@ -44,6 +44,7 @@ export type SmartGitHubSubmitLookup = { } const SMART_GITHUB_SUBMIT_LOOKUP_TTL_MS = 60_000 +const SMART_GITHUB_SUBMIT_LOOKUP_CACHE_MAX_ENTRIES = 128 type SmartGitHubSubmitLookupCacheEntry = { expiresAt: number @@ -52,6 +53,21 @@ type SmartGitHubSubmitLookupCacheEntry = { const smartGitHubSubmitLookupCache = new Map() +function pruneSmartGitHubSubmitLookupCache(now: number): void { + for (const [key, entry] of smartGitHubSubmitLookupCache) { + if (entry.expiresAt <= now) { + smartGitHubSubmitLookupCache.delete(key) + } + } + while (smartGitHubSubmitLookupCache.size > SMART_GITHUB_SUBMIT_LOOKUP_CACHE_MAX_ENTRIES) { + const oldestKey = smartGitHubSubmitLookupCache.keys().next().value + if (oldestKey === undefined) { + return + } + smartGitHubSubmitLookupCache.delete(oldestKey) + } +} + export function getSmartGitHubSubmitIntent(input: string): SmartGitHubSubmitIntent | null { const trimmed = input.trim() if (!trimmed) { @@ -106,6 +122,7 @@ export function lookupSmartGitHubSubmitItem({ }: SmartGitHubSubmitLookup): Promise { const key = getSmartGitHubSubmitLookupCacheKey({ repoId, repoPath, intent }) const now = Date.now() + pruneSmartGitHubSubmitLookupCache(now) const cached = smartGitHubSubmitLookupCache.get(key) if (cached && cached.expiresAt > now) { return cached.promise @@ -131,6 +148,7 @@ export function lookupSmartGitHubSubmitItem({ promise: stampedPromise, expiresAt: now + SMART_GITHUB_SUBMIT_LOOKUP_TTL_MS }) + pruneSmartGitHubSubmitLookupCache(now) // Why: transient GitHub/IPC failures should dedupe while in flight, but // must not poison immediate create retries for the full cache TTL. void stampedPromise.catch(() => { @@ -145,6 +163,10 @@ export function clearSmartGitHubSubmitLookupCacheForTests(): void { smartGitHubSubmitLookupCache.clear() } +export function getSmartGitHubSubmitLookupCacheSizeForTests(): number { + return smartGitHubSubmitLookupCache.size +} + export function getSmartGitHubSubmitResolution( item: Pick ): SmartGitHubSubmitResolution {