diff --git a/src/main/rate-limits/service/account-runtime-target-sync.ts b/src/main/rate-limits/service/account-runtime-target-sync.ts index 06529204995..fedae9b76cd 100644 --- a/src/main/rate-limits/service/account-runtime-target-sync.ts +++ b/src/main/rate-limits/service/account-runtime-target-sync.ts @@ -58,7 +58,7 @@ export function createAccountRuntimeTargetSettingsSync( } await Promise.all(refreshes) - // Antigravity has no targeted refresh; queueing a full one beside targeted ones can drop their reruns. + // Antigravity has no targeted refresh, so a usage target change needs a full one. if (usageTargetChanged) { await rateLimits.refresh() } diff --git a/src/main/rate-limits/service/service-fetch-queue.ts b/src/main/rate-limits/service/service-fetch-queue.ts index 6b04b1877b8..4958ca1f9aa 100644 --- a/src/main/rate-limits/service/service-fetch-queue.ts +++ b/src/main/rate-limits/service/service-fetch-queue.ts @@ -1,5 +1,7 @@ import { RateLimitServiceProviderCycles } from './service-provider-cycles' +type FetchCycle = (signal: AbortSignal) => Promise + export abstract class RateLimitServiceFetchQueue extends RateLimitServiceProviderCycles { protected async fetchAll(options?: { force?: boolean }): Promise { if (this.isFetching) { @@ -9,58 +11,10 @@ export abstract class RateLimitServiceFetchQueue extends RateLimitServiceProvide } return } - this.isFetching = true - - try { - let shouldContinue = true - // Only user-forced refreshes bypass Retry-After; queued reruns inherit that force. - let cycleForce = options?.force ?? false - while (shouldContinue) { - const signal = await this.runWithFetchAbortSignal((fetchSignal) => - this.runFetchAllCycle(fetchSignal, { force: cycleForce }) - ) - shouldContinue = false - cycleForce = true - if (signal.aborted) { - break - } - if (this.fullFetchQueued) { - this.fullFetchQueued = false - shouldContinue = true - continue - } - if (this.codexOnlyFetchQueued) { - this.codexOnlyFetchQueued = false - const codexSignal = await this.runWithFetchAbortSignal((fetchSignal) => - this.runFetchCodexOnlyCycle(fetchSignal) - ) - if (codexSignal.aborted) { - break - } - } - if (this.claudeOnlyFetchQueued) { - this.claudeOnlyFetchQueued = false - const claudeSignal = await this.runWithFetchAbortSignal((fetchSignal) => - this.runFetchClaudeOnlyCycle(fetchSignal, { force: true }) - ) - if (claudeSignal.aborted) { - break - } - } - if (this.grokOnlyFetchQueued) { - this.grokOnlyFetchQueued = false - const grokSignal = await this.runWithFetchAbortSignal((fetchSignal) => - this.runFetchGrokOnlyCycle(fetchSignal) - ) - if (grokSignal.aborted) { - break - } - } - } - } finally { - this.isFetching = false - this.resolveFetchIdleWaiters() - } + // Only user-forced refreshes bypass Retry-After; queued reruns are forced. + return this.runFetchQueue((signal) => + this.runFetchAllCycle(signal, { force: options?.force ?? false }) + ) } protected async fetchCodexOnly(options?: { force?: boolean }): Promise { @@ -71,55 +25,7 @@ export abstract class RateLimitServiceFetchQueue extends RateLimitServiceProvide } return } - this.isFetching = true - - try { - let shouldContinue = true - while (shouldContinue) { - const signal = await this.runWithFetchAbortSignal((fetchSignal) => - this.runFetchCodexOnlyCycle(fetchSignal) - ) - shouldContinue = false - if (signal.aborted) { - break - } - if (this.fullFetchQueued) { - this.fullFetchQueued = false - const fullSignal = await this.runWithFetchAbortSignal((fetchSignal) => - this.runFetchAllCycle(fetchSignal, { force: true }) - ) - if (fullSignal.aborted) { - break - } - continue - } - if (this.codexOnlyFetchQueued) { - this.codexOnlyFetchQueued = false - shouldContinue = true - } - if (this.claudeOnlyFetchQueued) { - this.claudeOnlyFetchQueued = false - const claudeSignal = await this.runWithFetchAbortSignal((fetchSignal) => - this.runFetchClaudeOnlyCycle(fetchSignal, { force: true }) - ) - if (claudeSignal.aborted) { - break - } - } - if (this.grokOnlyFetchQueued) { - this.grokOnlyFetchQueued = false - const grokSignal = await this.runWithFetchAbortSignal((fetchSignal) => - this.runFetchGrokOnlyCycle(fetchSignal) - ) - if (grokSignal.aborted) { - break - } - } - } - } finally { - this.isFetching = false - this.resolveFetchIdleWaiters() - } + return this.runFetchQueue((signal) => this.runFetchCodexOnlyCycle(signal)) } protected async fetchClaudeOnly(options?: { force?: boolean }): Promise { @@ -130,58 +36,9 @@ export abstract class RateLimitServiceFetchQueue extends RateLimitServiceProvide } return } - this.isFetching = true - - try { - let shouldContinue = true - // Only user-forced refreshes bypass Retry-After; queued reruns inherit that force. - let cycleForce = options?.force ?? false - while (shouldContinue) { - const signal = await this.runWithFetchAbortSignal((fetchSignal) => - this.runFetchClaudeOnlyCycle(fetchSignal, { force: cycleForce }) - ) - shouldContinue = false - cycleForce = true - if (signal.aborted) { - break - } - if (this.fullFetchQueued) { - this.fullFetchQueued = false - const fullSignal = await this.runWithFetchAbortSignal((fetchSignal) => - this.runFetchAllCycle(fetchSignal, { force: true }) - ) - if (fullSignal.aborted) { - break - } - continue - } - if (this.claudeOnlyFetchQueued) { - this.claudeOnlyFetchQueued = false - shouldContinue = true - } - if (this.codexOnlyFetchQueued) { - this.codexOnlyFetchQueued = false - const codexSignal = await this.runWithFetchAbortSignal((fetchSignal) => - this.runFetchCodexOnlyCycle(fetchSignal) - ) - if (codexSignal.aborted) { - break - } - } - if (this.grokOnlyFetchQueued) { - this.grokOnlyFetchQueued = false - const grokSignal = await this.runWithFetchAbortSignal((fetchSignal) => - this.runFetchGrokOnlyCycle(fetchSignal) - ) - if (grokSignal.aborted) { - break - } - } - } - } finally { - this.isFetching = false - this.resolveFetchIdleWaiters() - } + return this.runFetchQueue((signal) => + this.runFetchClaudeOnlyCycle(signal, { force: options?.force ?? false }) + ) } protected async fetchGrokOnly(options?: { force?: boolean }): Promise { @@ -192,54 +49,44 @@ export abstract class RateLimitServiceFetchQueue extends RateLimitServiceProvide } return } - this.isFetching = true + return this.runFetchQueue((signal) => this.runFetchGrokOnlyCycle(signal)) + } + private async runFetchQueue(first: FetchCycle): Promise { + this.isFetching = true try { - let shouldContinue = true - while (shouldContinue) { - const signal = await this.runWithFetchAbortSignal((fetchSignal) => - this.runFetchGrokOnlyCycle(fetchSignal) - ) - shouldContinue = false + let cycle: FetchCycle | null = first + while (cycle) { + const signal = await this.runWithFetchAbortSignal(cycle) if (signal.aborted) { break } - if (this.fullFetchQueued) { - this.fullFetchQueued = false - const fullSignal = await this.runWithFetchAbortSignal((fetchSignal) => - this.runFetchAllCycle(fetchSignal, { force: true }) - ) - if (fullSignal.aborted) { - break - } - continue - } - if (this.grokOnlyFetchQueued) { - this.grokOnlyFetchQueued = false - shouldContinue = true - } - if (this.codexOnlyFetchQueued) { - this.codexOnlyFetchQueued = false - const codexSignal = await this.runWithFetchAbortSignal((fetchSignal) => - this.runFetchCodexOnlyCycle(fetchSignal) - ) - if (codexSignal.aborted) { - break - } - } - if (this.claudeOnlyFetchQueued) { - this.claudeOnlyFetchQueued = false - const claudeSignal = await this.runWithFetchAbortSignal((fetchSignal) => - this.runFetchClaudeOnlyCycle(fetchSignal, { force: true }) - ) - if (claudeSignal.aborted) { - break - } - } + // Re-read every flag after each cycle: a request can queue while any cycle runs. + cycle = this.takeQueuedFetchCycle() } } finally { this.isFetching = false this.resolveFetchIdleWaiters() } } + + private takeQueuedFetchCycle(): FetchCycle | null { + if (this.fullFetchQueued) { + this.fullFetchQueued = false + return (signal) => this.runFetchAllCycle(signal, { force: true }) + } + if (this.codexOnlyFetchQueued) { + this.codexOnlyFetchQueued = false + return (signal) => this.runFetchCodexOnlyCycle(signal) + } + if (this.claudeOnlyFetchQueued) { + this.claudeOnlyFetchQueued = false + return (signal) => this.runFetchClaudeOnlyCycle(signal, { force: true }) + } + if (this.grokOnlyFetchQueued) { + this.grokOnlyFetchQueued = false + return (signal) => this.runFetchGrokOnlyCycle(signal) + } + return null + } } diff --git a/src/main/rate-limits/service/service-refresh-orchestration.test.ts b/src/main/rate-limits/service/service-refresh-orchestration.test.ts index 9a347132d93..c8e82267652 100644 --- a/src/main/rate-limits/service/service-refresh-orchestration.test.ts +++ b/src/main/rate-limits/service/service-refresh-orchestration.test.ts @@ -1,10 +1,12 @@ import { beforeEach, describe, expect, it, vi } from 'vitest' +import type { ProviderRateLimits } from '../../../shared/rate-limit-types' import { RateLimitService } from './rate-limit-service' import { fetchActiveClaudeRateLimits } from '../providers/claude/claude-active-usage-fetch' import { fetchCodexRateLimits } from '../providers/codex/codex-fetcher' import { fetchZcodeRateLimits } from '../providers/zcode/zcode-usage-fetcher' import { fetchOpenCodeGoUsage } from '../providers/opencode-go/opencode-go-usage-source-selection' import { + deferred, errorProvider, flushMicrotasks, okProvider, @@ -177,4 +179,23 @@ describe('RateLimitService', () => { vi.useRealTimers() } }) + + it('reads the account picked by a second switch made during a refresh', async () => { + const service = new RateLimitService() + const reads = [deferred(), deferred()] + vi.mocked(fetchActiveClaudeRateLimits) + .mockReturnValueOnce(reads[0].promise) + .mockReturnValueOnce(reads[1].promise) + .mockResolvedValueOnce(okProvider('claude', 55)) + void service.refresh() + await flushMicrotasks(20) + void service.refreshForClaudeAccountChange('acct-a') + reads[0].resolve(okProvider('claude', 0)) + await flushMicrotasks(40) + // The first switch's rerun is reading when the user switches again. + void service.refreshForClaudeAccountChange('acct-b') + reads[1].resolve(okProvider('claude', 11)) + await flushMicrotasks(60) + expect(service.getState().claude?.session?.usedPercent).toBe(55) + }) })