fix(rate-limits): run a usage refresh queued while another refresh is finishing

The fetch queue checked each queued-refresh flag once per loop, in a fixed
order. A forced refresh queued after its flag was checked stayed set when the
loop ended, so nothing ran it: after two quick account switches during a
refresh the meter stayed on fetching with no numbers, and the switch never
read the new account. One drain loop now re-reads every flag after each cycle.
This commit is contained in:
Kelvin Amoaba
2026-11-13 02:02:23 +00:00
parent 5bee67a03b
commit 7682d8bdb8
3 changed files with 61 additions and 193 deletions
@@ -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()
}
@@ -1,5 +1,7 @@
import { RateLimitServiceProviderCycles } from './service-provider-cycles'
type FetchCycle = (signal: AbortSignal) => Promise<void>
export abstract class RateLimitServiceFetchQueue extends RateLimitServiceProviderCycles {
protected async fetchAll(options?: { force?: boolean }): Promise<void> {
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<void> {
@@ -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<void> {
@@ -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<void> {
@@ -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<void> {
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
}
}
@@ -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<ProviderRateLimits>(), deferred<ProviderRateLimits>()]
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)
})
})