feat(quota): show Codex limit resets in the Plan tab, hover card and CLI

The Plan tab row #724 added showed a count and an expiry. It now also shows how
many of those credits can be applied right now and when the most recent one
landed, which is what a user deciding whether to push through or throttle back
actually needs. The same sentence appears in the agent-tab quota hover card and
in `codeburn quota`.

One formatter produces it, mirrored line for line in Swift and TypeScript, down
to the compact-age rules — deliberately not RelativeDateTimeFormatter or
Intl.RelativeTimeFormat, because the two surfaces have to print the same
characters, and a locale-aware formatter would not. `codeburn quota` grows a
`notes` channel for provider facts that are not a window; only Codex sets it, so
every other provider's output is byte-identical to before.

The CLI reads the credits off the `rate_limit_reset_credits` block of the usage
response it already fetches and never calls the companion endpoint, so it prints
the count without the grant and expiry captions that only the per-credit list
carries.

Refs #725. No new polling loop and no new data source: this reads fields from a
response already fetched on the existing cadence.
This commit is contained in:
ozymandiashh 2026-09-12 23:31:31 +03:00 • committed by iamtoruk
parent 79d1482951
commit 6be422ac52
7 changed files with 317 additions and 16 deletions

View file

@ -204,7 +204,119 @@ function spendControlWindow(data: Record<string, any>): QuotaWindow | null {
return { label: reached ? `${label} · limit reached` : label, percent, resetsAt }
}
export function decodeCodexUsage(body: unknown): QuotaProvider {
// Banked limit-reset credits. The Swift menubar owns the same three functions
// (mac/Sources/CodeBurnMenubar/Data/CodexBankedResets.swift); they are mirrored
// line for line so both surfaces print the same sentence. The CLI reads them off
// the `rate_limit_reset_credits` block of the usage response it already fetches
// and never calls the companion endpoint, so no new request and no new cadence.
type ResetGrant = { id: string; resetType: string | null; grantedAt: number | null }
type ResetCredits = {
availableCount: number
/** How many can be applied right now. Null is unknown, not zero. */
applicableAvailableCount: number | null
grants: ResetGrant[]
nextExpiresAt: number | null
}
function isoMillis(value: unknown): number | null {
if (typeof value !== 'string' || !value.trim()) return null
const at = Date.parse(value)
return Number.isFinite(at) ? at : null
}
/** Null on any unexpected shape: the row hides, the quota read still succeeds. */
export function decodeResetCredits(value: unknown, now: number): ResetCredits | null {
if (!value || typeof value !== 'object') return null
const block = value as Record<string, unknown>
const count = num(block.available_count)
if (count === null || count < 0 || !Number.isInteger(count)) return null
const rows = Array.isArray(block.credits) ? block.credits : []
const available = rows.filter((row): row is Record<string, unknown> =>
Boolean(row) && typeof row === 'object'
&& String((row as Record<string, unknown>).status ?? '').toLowerCase() === 'available')
const expiries = available
.map(row => isoMillis(row.expires_at))
.filter((at): at is number => at !== null && at > now)
// Identity first, and only then a grant: a credit we cannot name is a credit
// we could re-announce on every refresh.
const grants: ResetGrant[] = []
for (const row of available) {
const identity = [row.id, row.granted_at]
.map(candidate => typeof candidate === 'string' ? candidate.trim() : '')
.find(candidate => candidate !== '')
if (!identity) continue
grants.push({
id: identity,
resetType: typeof row.reset_type === 'string' && row.reset_type ? row.reset_type : null,
grantedAt: isoMillis(row.granted_at),
})
}
const applicable = num(block.applicable_available_count)
return {
availableCount: count,
applicableAvailableCount: applicable !== null && applicable >= 0 && Number.isInteger(applicable) ? applicable : null,
grants,
nextExpiresAt: expiries.length > 0 ? Math.min(...expiries) : null,
}
}
/** Deliberately not `Intl.RelativeTimeFormat`: this string has to come out
* character-identical in TypeScript and in Swift, so the rules are spelled out
* instead of delegated to a locale-aware formatter. */
export function compactAge(at: number, now: number): string {
const elapsed = (now - at) / 1000
if (elapsed >= 0) {
if (elapsed < 60) return 'just now'
if (elapsed < 3600) return `${Math.floor(elapsed / 60)}m ago`
if (elapsed < 86_400) return `${Math.floor(elapsed / 3600)}h ago`
return `${Math.floor(elapsed / 86_400)}d ago`
}
const ahead = -elapsed
if (ahead < 60) return 'in under a minute'
if (ahead < 3600) return `in ${Math.floor(ahead / 60)}m`
if (ahead < 86_400) return `in ${Math.floor(ahead / 3600)}h`
return `in ${Math.floor(ahead / 86_400)}d`
}
/** "weekly" -> "weekly reset". An absent `reset_type` degrades to the bare noun
* rather than inventing a scope the payload did not state. */
function resetTypeLabel(resetType: string | null): string {
const raw = (resetType ?? '').trim().replace(/[_-]/g, ' ').toLowerCase()
return raw ? `${raw} reset` : 'limit reset'
}
function latestGrant(credits: ResetCredits): ResetGrant | null {
let latest: ResetGrant | null = null
for (const grant of credits.grants) {
if (grant.grantedAt === null) continue
if (latest === null || grant.grantedAt > (latest.grantedAt ?? -Infinity)) latest = grant
}
return latest
}
/** `Limit resets · 2 available · 1 usable now · latest weekly reset granted 2h ago`.
* Null when the account holds nothing — the line hides rather than printing a zero. */
export function resetCreditsLine(credits: ResetCredits | null, now: number): string | null {
if (!credits || credits.availableCount <= 0) return null
const parts = [`${credits.availableCount} available`]
// Only worth saying when it disagrees with the headline count.
if (credits.applicableAvailableCount !== null && credits.applicableAvailableCount !== credits.availableCount) {
parts.push(`${credits.applicableAvailableCount} usable now`)
}
const grant = latestGrant(credits)
if (grant?.grantedAt != null) {
const type = resetTypeLabel(grant.resetType)
parts.push(grant.grantedAt > now
? `next ${type} lands ${compactAge(grant.grantedAt, now)}`
: `latest ${type} granted ${compactAge(grant.grantedAt, now)}`)
}
if (credits.nextExpiresAt !== null) parts.push(`next expires ${compactAge(credits.nextExpiresAt, now)}`)
return `Limit resets · ${parts.join(' · ')}`
}
export function decodeCodexUsage(body: unknown, now: number = Date.now()): QuotaProvider {
const data = body && typeof body === 'object' ? body as Record<string, any> : {}
const primaryRaw = windowOf(data.rate_limit?.primary_window)
const secondaryRaw = windowOf(data.rate_limit?.secondary_window)
@ -234,10 +346,15 @@ export function decodeCodexUsage(body: unknown): QuotaProvider {
}
// Uncapped on purpose, so a bar-less card does not read as a failed fetch.
if (!credits && data.credits?.unlimited === true) footerLines.push('Credits · Unlimited')
// Limit-reset credits, banked ones included. `notes` is what `codeburn quota`
// prints; the footer keeps the desktop surfaces in step.
const resets = resetCreditsLine(decodeResetCredits(data.rate_limit_reset_credits, now), now)
if (resets) footerLines.push(resets)
return {
provider: 'codex', connection: 'connected', primary: primary ?? credits, details,
planLabel: planLabel(data.plan_type),
footerLines,
...(resets ? { notes: [resets] } : {}),
}
}

View file

@ -27,6 +27,9 @@ export type QuotaCommandProvider = {
plan?: string
windows: QuotaCommandWindow[]
error?: string
/** Provider facts that are not a window, such as Codex's limit-reset credits.
* Printed under the provider's rows and carried in `--format json`. */
notes?: string[]
}
export type QuotaReport = { providers: QuotaCommandProvider[] }
@ -81,6 +84,7 @@ export function toCommandProvider(id: ProviderName, name: string, quota: QuotaPr
...(quota.planLabel ? { plan: quota.planLabel } : {}),
windows: toWindows(quota),
...(error ? { error } : {}),
...(quota.notes?.length ? { notes: quota.notes } : {}),
}
}
@ -119,11 +123,14 @@ export function renderQuotaTable(report: QuotaReport, opts: { color?: boolean }
const title = provider.plan ? `${provider.name} (${provider.plan})` : provider.name
if (provider.windows.length === 0) {
rows.push([title, provider.error ?? 'Not connected', '', ''])
// A provider with no readable window can still hold a fact worth saying.
for (const note of provider.notes ?? []) rows.push(['', note, '', ''])
continue
}
provider.windows.forEach((window, index) => {
rows.push([index === 0 ? title : '', window.label, `${window.usedPct}%`, resetLabel(window.resetsAt)])
})
for (const note of provider.notes ?? []) rows.push(['', note, '', ''])
}
const columns = [{ header: 'Provider' }, { header: 'Window' }, { header: 'Used', right: true }, { header: 'Resets' }]
return renderTable(columns, rows, { color: opts.color })

View file

@ -15,6 +15,10 @@ export type QuotaProvider = {
* upstream quota endpoint), so the UI can say so honestly instead of the
* generic "waiting" copy. */
rateLimited?: boolean
/** Facts that belong in `codeburn quota` output, not only in a hover card:
* a `footerLines` entry is a detail panel row, a note is printed under the
* provider's windows in the table and carried in `--format json`. */
notes?: string[]
}
export type ProviderName = QuotaProvider['provider']