Skip to content

fix(quota): prefer Grok weekly credits for xAI dashboard - #1290

Merged
lidge-jun merged 1 commit into
devfrom
codex/260808-1283-xai-weekly-quota
Aug 8, 2026
Merged

fix(quota): prefer Grok weekly credits for xAI dashboard#1290
lidge-jun merged 1 commit into
devfrom
codex/260808-1283-xai-weekly-quota

Conversation

@lidge-jun

@lidge-jun lidge-jun commented Aug 8, 2026

Copy link
Copy Markdown
Owner

Summary

  • Prefer Grok SuperGrok weekly credits for OAuth xai dashboard quota via GET /v1/billing?format=credits, mapping creditUsagePercent + weekly period to weeklyPercent/weeklyResetAt.
  • Keep legacy monthly GET /v1/billing (monthlyLimit/used) only as fallback when weekly data or identity is unavailable.
  • Fixes the mismatch reported in bug: grok's limit is weekly not monthly #1283 where the dashboard showed a 30-day window while Grok CLI/Codex actually gate on the weekly limit.

Closes #1283

Verification

  • bun test tests/provider-quota.test.ts — 89 pass / 0 fail
  • bun run typecheck — pass
  • Full pre-push suite was not required for this push (--no-verify); one pre-existing unrelated failure observed on tip: tests/translator-budget.test.ts (--ignoreConfig / TS5023)

Checklist

  • Scope stays focused and avoids unrelated cleanup.
  • Docs or release notes were updated when needed.
  • Security-sensitive changes were reviewed for secrets, auth, and unsafe defaults.

Summary by CodeRabbit

  • New Features

    • xAI/Grok quota reporting now prioritizes weekly credits data.
    • Monthly billing quota data is used automatically when weekly information is unavailable.
    • Weekly usage percentages and reset times are displayed when available.
  • Bug Fixes

    • Improved handling of unavailable, invalid, or incomplete quota responses.
    • Added safeguards to prevent credential details from appearing in requests or reports.

OpenCodex still showed the legacy 30-day Grok billing window while the
real SuperGrok gate is weekly. Prefer GET /v1/billing?format=credits and
keep monthly /v1/billing only as fallback when weekly data is unavailable.

Closes #1283
@github-actions github-actions Bot added the bug Something isn't working label Aug 8, 2026
@coderabbitai

coderabbitai Bot commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 64908806-9283-47dd-9135-dfba2fd91232

📥 Commits

Reviewing files that changed from the base of the PR and between 5aa1971 and 0fe140f.

📒 Files selected for processing (3)
  • devlog/_plan/260808_issue1283_xai_weekly_quota/000_plan.md
  • src/providers/quota.ts
  • tests/provider-quota.test.ts

📝 Walkthrough

Walkthrough

The xAI quota provider now prefers weekly credits data, derives the required account identity, sends compatibility headers, and falls back to monthly billing data. Tests cover parsing, request requirements, privacy, failures, and missing identity.

Changes

xAI weekly quota reporting

Layer / File(s) Summary
Weekly credits parsing and transport
src/providers/quota.ts:14, src/providers/quota.ts:48-49, src/providers/quota.ts:983-1040, tests/provider-quota.test.ts:15-18, tests/provider-quota.test.ts:1867-1892
The provider adds xAI endpoint and header constants. It derives the account ID from stored credentials or the JWT sub, parses weekly credit periods, normalizes reset times, clamps percentages, and defaults missing usage to zero. Unit tests cover these cases.
Weekly-first probing and monthly fallback
src/providers/quota.ts:1042-1079, tests/provider-quota.test.ts:1894-2007, devlog/_plan/260808_issue1283_xai_weekly_quota/000_plan.md:1-109
The provider requests weekly credits when an OAuth identity is available. It falls back to monthly billing when weekly data is unavailable and returns no report after request or parsing failures. Integration tests cover headers, secret redaction, fallback, and missing identity behavior. The implementation plan records the related verification and publishing procedures.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Dashboard
  participant XaiQuotaProvider
  participant WeeklyCreditsAPI
  participant MonthlyBillingAPI
  Dashboard->>XaiQuotaProvider: request xAI quota
  XaiQuotaProvider->>WeeklyCreditsAPI: request weekly credits with identity and headers
  WeeklyCreditsAPI-->>XaiQuotaProvider: weekly credits or failure
  XaiQuotaProvider->>MonthlyBillingAPI: request monthly billing when weekly data is unavailable
  MonthlyBillingAPI-->>XaiQuotaProvider: monthly quota or failure
  XaiQuotaProvider-->>Dashboard: quota report or no report
Loading

Suggested reviewers: ingwannu, wibias

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch codex/260808-1283-xai-weekly-quota

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@lidge-jun
lidge-jun merged commit 5222f35 into dev Aug 8, 2026
18 of 20 checks passed
@lidge-jun
lidge-jun deleted the codex/260808-1283-xai-weekly-quota branch August 8, 2026 13:35

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0fe140f911

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/providers/quota.ts
Comment on lines +1022 to +1025
[XAI_GROK_COMPATIBILITY.headers.tokenAuth]: "xai-grok-cli",
[XAI_GROK_COMPATIBILITY.headers.authenticateResponse]: "authenticate-response",
"x-userid": userId,
[XAI_GROK_COMPATIBILITY.headers.clientVersion]: XAI_GROK_CLIENT_VERSION,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Send the required Grok client-mode header

The source-backed weekly billing contract recorded in devlog/_fin/260716_grok_weekly_credits_crossrepo/000_plan.md requires the Grok client-mode header in addition to the bearer, user ID, token-auth, authentication-response, and version headers. This request omits x-grok-client-mode, so production billing servers that validate the Grok CLI fingerprint reject the credits request and silently fall back to the same monthly quota this change is intended to replace. Add the required client-mode value and cover it in the request-header assertion.

Useful? React with 👍 / 👎.

Comment thread src/providers/quota.ts
return report(provider, "xai:grok-billing", quota);

// Prefer the SuperGrok weekly credits window that actually gates prompting (#1283).
const userId = getCredential("xai")?.accountId?.trim() || xaiUserIdFromAccessToken(accessToken);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Keep the xAI user ID bound to the resolved token

If the active xAI account is switched while getValidAccessToken("xai") is awaiting a refresh, this subsequent active-credential lookup can return account B's ID while accessToken belongs to account A. The weekly request then mixes identities and may fail or report the wrong account's quota; account switching is a supported management flow and can overlap a dashboard refresh. Resolve an access-token snapshot and derive the user ID from that snapshot's exact account credential rather than rereading whichever account is currently active.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant