fix(grok): honor zero-usage weekly reset (#9219)
* fix(grok): honor zero-usage weekly reset * fix(grok): preserve ambiguous monthly billing fallback
This commit is contained in:
@@ -100,6 +100,31 @@ describe('fetchGrokRateLimits', () => {
|
||||
)
|
||||
})
|
||||
|
||||
it('maps an omitted protobuf percentage as zero for a weekly credits period', async () => {
|
||||
authState.file = freshAuthJson()
|
||||
netFetchMock.mockResolvedValueOnce(
|
||||
jsonResponse({
|
||||
config: {
|
||||
currentPeriod: {
|
||||
type: 'USAGE_PERIOD_TYPE_WEEKLY',
|
||||
start: '2026-07-17T19:38:56.948570+00:00',
|
||||
end: '2026-07-24T19:38:56.948570+00:00'
|
||||
},
|
||||
billingPeriodStart: '2026-07-17T19:38:56.948570+00:00',
|
||||
billingPeriodEnd: '2026-07-24T19:38:56.948570+00:00',
|
||||
isUnifiedBillingUser: true
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
const result = await fetchGrokRateLimits()
|
||||
expect(result.status).toBe('ok')
|
||||
expect(result.weekly?.usedPercent).toBe(0)
|
||||
expect(result.weekly?.resetsAt).toBe(Date.parse('2026-07-24T19:38:56.948570+00:00'))
|
||||
expect(result.monthly).toBeUndefined()
|
||||
expect(netFetchMock).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
it('returns unavailable when not signed in even if a token-less auth file exists', async () => {
|
||||
authState.file = JSON.stringify({})
|
||||
const result = await fetchGrokRateLimits()
|
||||
@@ -119,7 +144,7 @@ describe('fetchGrokRateLimits', () => {
|
||||
expect(result.monthly).toBeUndefined()
|
||||
})
|
||||
|
||||
it('maps monthly included usage for unified-billing accounts without weekly credits', async () => {
|
||||
it('maps monthly included usage when a unified-billing response has an ambiguous weekly period', async () => {
|
||||
authState.file = freshAuthJson()
|
||||
netFetchMock
|
||||
.mockResolvedValueOnce(
|
||||
@@ -162,6 +187,7 @@ describe('fetchGrokRateLimits', () => {
|
||||
headers: expect.objectContaining({ Authorization: 'Bearer access-token' })
|
||||
})
|
||||
)
|
||||
expect(netFetchMock).toHaveBeenCalledTimes(2)
|
||||
})
|
||||
|
||||
// Why: 'unavailable' would make applyStalePolicy discard the last good
|
||||
|
||||
@@ -16,8 +16,8 @@ const GROK_CLI_PROXY_BASE =
|
||||
process.env.GROK_CLI_CHAT_PROXY_BASE_URL?.trim().replace(/\/$/, '') ||
|
||||
'https://cli-chat-proxy.grok.com/v1'
|
||||
const BILLING_CREDITS_URL = `${GROK_CLI_PROXY_BASE}/billing?format=credits`
|
||||
// Why: unified-billing accounts have no weekly credits; their included monthly
|
||||
// budget is only present in the default (format-less) billing view.
|
||||
// Why: some unified-billing accounts expose only a monthly included budget,
|
||||
// which is present in the default (format-less) billing view.
|
||||
const BILLING_DEFAULT_URL = `${GROK_CLI_PROXY_BASE}/billing`
|
||||
const API_TIMEOUT_MS = 10_000
|
||||
const WEEKLY_WINDOW_MINUTES = 10_080
|
||||
@@ -81,8 +81,28 @@ function parseResetDescription(isoString: string | undefined): string | null {
|
||||
: date.toLocaleDateString(undefined, { weekday: 'short', hour: 'numeric', minute: '2-digit' })
|
||||
}
|
||||
|
||||
function timestampsMatch(left: string | undefined, right: string | undefined): boolean {
|
||||
const leftTimestamp = left ? Date.parse(left) : Number.NaN
|
||||
const rightTimestamp = right ? Date.parse(right) : Number.NaN
|
||||
return Number.isFinite(leftTimestamp) && leftTimestamp === rightTimestamp
|
||||
}
|
||||
|
||||
function hasConfirmedWeeklyPeriod(config: GrokBillingConfig): boolean {
|
||||
const period = config.currentPeriod
|
||||
// Why: monthly unified-billing responses can also carry a weekly currentPeriod;
|
||||
// matching billing bounds identify Grok's omitted protobuf zero unambiguously.
|
||||
return (
|
||||
period?.type === 'USAGE_PERIOD_TYPE_WEEKLY' &&
|
||||
timestampsMatch(period.start, config.billingPeriodStart) &&
|
||||
timestampsMatch(period.end, config.billingPeriodEnd)
|
||||
)
|
||||
}
|
||||
|
||||
function mapWeeklyCredits(config: GrokBillingConfig): RateLimitWindow | null {
|
||||
const usedPercent = config.creditUsagePercent
|
||||
const usedPercent =
|
||||
config.creditUsagePercent === undefined && hasConfirmedWeeklyPeriod(config)
|
||||
? 0
|
||||
: config.creditUsagePercent
|
||||
if (typeof usedPercent !== 'number' || !Number.isFinite(usedPercent)) {
|
||||
return null
|
||||
}
|
||||
@@ -257,9 +277,8 @@ export async function fetchGrokRateLimits(
|
||||
if (weekly) {
|
||||
return billingUsageResult({ weekly }, config, session)
|
||||
}
|
||||
// Why: unified-billing accounts report a monthly included-usage budget
|
||||
// instead of weekly credits; the credits view omits creditUsagePercent
|
||||
// for them, so read the default billing view before giving up.
|
||||
// Why: some unified-billing accounts expose only a monthly included budget;
|
||||
// their credits view omits creditUsagePercent, so read the default view.
|
||||
const fallback = await fetchMonthlyUsageFallback(session, options.signal)
|
||||
if (fallback.kind === 'result') {
|
||||
return fallback.result
|
||||
|
||||
Reference in New Issue
Block a user