Skip to content

feat(xai): report SuperGrok subscription usage#5903

Closed
oleksoleksoleks wants to merge 1 commit into
can1357:mainfrom
oleksoleksoleks:feat/xai-usage-stats
Closed

feat(xai): report SuperGrok subscription usage#5903
oleksoleksoleks wants to merge 1 commit into
can1357:mainfrom
oleksoleksoleks:feat/xai-usage-stats

Conversation

@oleksoleksoleks

Copy link
Copy Markdown
Contributor

What

  • Add xai-oauth usage reporting to omp usage through Grok's billing RPC.
  • Decode gRPC-web/protobuf credit utilization and reset timestamps into normalized OMP usage limits.
  • Register the provider, preserve account metadata, and cover malformed/error responses.

Why

SuperGrok OAuth accounts currently disappear from aggregate usage output because OMP has no usage provider for xai-oauth. The authenticated Grok billing surface exposes the current credit percentage and reset time.

Testing

  • bun test test/xai-usage.test.ts in packages/ai — 6 passed, 0 failed
  • bun run check:types in packages/ai
  • bun run check:types in packages/coding-agent
  • bun run dev usage --provider xai-oauth --redact --json — live OAuth account returned Grok Credits usage and reset timestamp

  • bun check passes
  • Tested locally
  • CHANGELOG updated (if user-facing)

Risk

The Grok billing RPC and protobuf schema are undocumented. Parsing is bounded and defensive; HTTP, gRPC, and parse failures flow through AuthStorage's existing last-known-good behavior.

AI Review Report

A focused reviewer identified that trailer-only gRPC-web failures could lose their definitive status. The parser now validates trailers before requiring a data frame, with regression coverage for trailer-only authentication failures.

Security Audit

  • Sends the OAuth bearer token only to the fixed HTTPS grok.com billing endpoint.
  • Does not log credentials or retain raw billing responses.
  • Explicitly rejects ordinary xai API-key credentials; only xai-oauth is registered.
  • Bounds protobuf recursion depth and validates frame lengths before scanning.

@github-actions github-actions Bot added the vouched Passed the vouch gate label Jul 17, 2026

@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: 1e58fd459b

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines +142 to +144
function assertGrpcStatus(status: string | null | undefined, message?: string | null): void {
if (status === null || status === undefined || status === "" || status === "0") return;
throw new Error(`Grok billing RPC failed with status ${status}${message ? `: ${message}` : ""}`);

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 Reject responses that omit gRPC status

When an HTTP 200 response contains a valid data frame but the gRPC-Web trailer is truncated or stripped by a proxy, status is absent and this branch treats it exactly like status 0. The parser then publishes the payload as successful even though the RPC never reported successful completion, bypassing the existing last-known-good failure behavior. Require an explicit zero status from either the HTTP headers or trailer frame before accepting a framed response.

Useful? React with 👍 / 👎.

@roboomp roboomp added auth Authentication and login flows feat provider:xai Provider-specific issues for xai providers LLM provider-specific issues review:p3 triaged labels Jul 17, 2026

@roboomp roboomp left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

review:p3#4874 already targets the same xai-oauth usage feature; maintainers should choose one endpoint and wire contract rather than merge competing default providers.
Blocking: the parser prefers legacy billing_period_end over current_period.end, producing a stale reset when both differ. Public exports also need repo-standard API docs.
Verification: the focused 6-test suite, packages/ai check, and both affected packages' type checks pass.
Thanks for the focused implementation and defensive gRPC handling.

Comment on lines +176 to +177
const preferredReset = resetCandidates
.filter(field => samePath(field.path, [1, 5, 1]))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

blocking — This prefers the legacy GrokCreditsConfig.billing_period_end.seconds path. In the current Grok protobuf descriptor, the live period is config.current_period.end.seconds, i.e. [1, 8, 3, 1]; Grok's own UI reads currentPeriod. When both fields differ, this parser returns [1, 5, 1] and reports the stale reset. I reproduced that with legacy end 2000000000 and current-period end 2010000000: parseGrokUsageResponse() returned 2000000000000. Please prefer [1, 8, 3, 1] and add a regression where the legacy and current fields disagree.

nextOrder: number;
}

export interface GrokUsageSnapshot {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

should-fixGrokUsageSnapshot is a new public package export (@oh-my-pi/pi-ai/usage/xai) but has no API documentation. The same applies to the exported parseGrokUsageResponse and xaiOAuthUsageProvider below. Repo convention requires every public symbol to document what it represents and why callers use it; please add concise doc comments or keep parser-only internals non-public.

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

Labels

auth Authentication and login flows feat provider:xai Provider-specific issues for xai providers LLM provider-specific issues review:p3 triaged vouched Passed the vouch gate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants