diff --git a/src/main/rate-limits/service.test.ts b/src/main/rate-limits/service.test.ts index 386cab3aa..5aa1f408e 100644 --- a/src/main/rate-limits/service.test.ts +++ b/src/main/rate-limits/service.test.ts @@ -485,6 +485,42 @@ describe('RateLimitService', () => { await firstFetch }) + it('keeps sibling inactive Codex preview fetches alive when one account is evicted', async () => { + const service = new RateLimitService() + const accountFetch = deferred() + let inactiveAccounts = [ + { id: 'account-a', managedHomePath: '/tmp/account-a/home' }, + { id: 'account-b', managedHomePath: '/tmp/account-b/home' } + ] + service.setInactiveCodexAccountsResolver(() => inactiveAccounts) + vi.mocked(fetchCodexRateLimits).mockReturnValueOnce(accountFetch.promise) + + const fetchOnOpen = service.fetchInactiveCodexAccountsOnOpen() + await Promise.resolve() + expect(service.getState().inactiveCodexAccounts).toEqual([ + { accountId: 'account-a', rateLimits: null, updatedAt: 0, isFetching: true }, + { accountId: 'account-b', rateLimits: null, updatedAt: 0, isFetching: true } + ]) + + inactiveAccounts = [{ id: 'account-a', managedHomePath: '/tmp/account-a/home' }] + service.evictInactiveCodexCache('account-b') + accountFetch.resolve(okProvider('codex', 64, Date.now())) + await fetchOnOpen + + expect(fetchCodexRateLimits).toHaveBeenCalledTimes(1) + expect(service.getState().inactiveCodexAccounts).toEqual([ + { + accountId: 'account-a', + rateLimits: expect.objectContaining({ + provider: 'codex', + session: expect.objectContaining({ usedPercent: 64 }) + }), + updatedAt: expect.any(Number), + isFetching: false + } + ]) + }) + it('does not recache an inactive Codex account that becomes active during fetch-on-open', async () => { const service = new RateLimitService() const accountFetch = deferred() diff --git a/src/main/rate-limits/service.ts b/src/main/rate-limits/service.ts index c0a6ad84e..51c267f3d 100644 --- a/src/main/rate-limits/service.ts +++ b/src/main/rate-limits/service.ts @@ -487,7 +487,11 @@ export class RateLimitService { } evictInactiveCodexCache(accountId: string): void { - this.inactiveCodexAccountsGeneration += 1 + // Why: only the evicted account's state should be cleared. The per-account + // isCurrentInactiveCodexAccount guard in fetchInactiveCodexAccountsOnOpen + // already catches a removed account when its resolver entry disappears, + // so bumping the generation here would also invalidate sibling fetches + // still in flight and discard their fresh results. this.inactiveCodexCache.delete(accountId) this.inactiveCodexFetching.delete(accountId) this.pushToRenderer()