sub2api icon indicating copy to clipboard operation
sub2api copied to clipboard

feat: add grok subscription support

Open heathermhuang opened this issue 5 months ago • 2 comments

heathermhuang avatar Jun 16 '26 10:06 heathermhuang

All contributors have signed the CLA. ✅
Posted by the CLA Assistant Lite bot.

github-actions[bot] avatar Jun 16 '26 10:06 github-actions[bot]

I have read the CLA Document and I hereby sign the CLA

heathermhuang avatar Jun 16 '26 11:06 heathermhuang

Maintainer action requested: GitHub Actions for the latest head 24b6fdf9d988b441c0e6654b425ac79fed38d776 are still gated with action_required; both CI and Security Scan created runs with no jobs. Please approve/run the workflows when convenient so the PR can get base-repo CI evidence.

heathermhuang avatar Jun 17 '26 00:06 heathermhuang

@Wei-Shaw Latest head is 82da8a3816a78a817405ecd16327973cfd52015f.

Latest-head local validation is green:

  • backend: go test -tags=unit ./... -count=1
  • frontend: pnpm --dir frontend typecheck
  • frontend: pnpm --dir frontend lint:check
  • frontend: pnpm --dir frontend build
  • git diff --check

Public Grok scope remains /v1/responses only; Grok Chat Completions is intentionally fenced out of this PR's production scope.

Could you please review and confirm whether base-repo CI/Security checks need to be manually enabled for this fork PR? Currently the only visible checks on this head are CLA (cla-check passed, cla-lock skipped).

heathermhuang avatar Jun 18 '26 09:06 heathermhuang

@Wei-Shaw nudging this now that the branch is rebased onto current main and CI is visible again.

Current head: 720db8983f5d166ffe81ef11030d8870dac05926 Base: fd9c945fab40acf67fdcd3c89a3f6fe92ee62e9a

GitHub status at the time of this comment:

  • Merge state is UNSTABLE because checks are still settling, not because the branch is conflicted.
  • Green so far: cla-check, backend-security, frontend, frontend-security, golangci-lint.
  • test is still pending/queued.

Local validation after the rebase:

  • go test -tags unit ./internal/pkg/xai ./internal/service -run 'Grok|XAI|ChatCompletions|Billing|CodexCLIOnly'
  • go test -tags unit ./internal/handler/admin -run 'Grok|XAI'

Could you take a look in the next maintainer sweep? If this is too large to review as one PR, I can split it into smaller pieces, for example:

  • Grok OAuth/admin wiring
  • gateway/raw Chat Completions path
  • quota probe and admin UI readiness

heathermhuang avatar Jun 26 '26 03:06 heathermhuang

Thanks for the very thorough PR — scope notes and route-level fence are well done. Two issues I'd like fixed before merging.


1. Per-account concurrency cap is documentation-only

The README and runtime-sanity advertise a default cap of 1 for Grok OAuth accounts, overridable by XAI_GROK_UNSAFE_ALLOW_CONCURRENCY_GT_ONE. But the code doesn't enforce it.

backend/internal/service/admin_service.go:2575-2580

func normalizeAccountConcurrency(platform, accountType string, concurrency int) int {
    if platform == PlatformGrok && accountType == AccountTypeOAuth && concurrency <= 0 {
        return 1
    }
    return concurrency
}

This only sets a default when concurrency <= 0. An admin submitting concurrency: 50 on a Grok OAuth account gets 50
written verbatim, and AllowUnsafeHighConcurrency() is never consulted on the request path (grep confirms it appears
only in runtime-sanity JSON and its own definition).

The PR description references a test NormalizeAccountConcurrencyCapsGrokOAuthUnlessUnsafe, but I can't find it in the
current head — was it dropped during a rebase?

Please clamp at write time:
if platform == PlatformGrok && accountType == AccountTypeOAuth {
    if concurrency <= 0 {
        return 1
    }
    if concurrency > 1 && !xai.AllowUnsafeHighConcurrency() {
        return 1
    }
}

And please restore/add the regression test.

2. Architecture issues around the new Grok wiring

A few places where the Grok code diverges from the ports-and-adapters pattern the rest of the codebase follows (Service depends on interfaces; DI provides every dependency; no silent proxy drop):

2.1 GrokQuotaService silently drops the account proxy. backend/internal/service/grok_quota_service.go:137-140 proxyURL := "" if account.ProxyID != nil && account.Proxy != nil { proxyURL = account.Proxy.URL() } Compare openai_quota_service.go:249-260, which falls back to a proxyRepo lookup when account.Proxy isn't eager-loaded. As-is, any caller loading the account without resolving the proxy edge will probe xAI directly, bypassing the operator-configured egress proxy — a quiet correctness/compliance regression. Please inject ProxyRepository into GrokQuotaService (update ProvideGrokQuotaService in service/wire.go:128-134) and mirror OpenAIQuotaService.resolveProxyURL.

2.2 GrokTokenProvider.grokOAuthService is a dead field. grok_token_provider.go:26,36,41 — declared, accepted in the constructor, assigned, but no method on *GrokTokenProvider ever reads it (the actual refresh goes through p.executor, i.e. *GrokTokenRefresher). Please drop the field and the constructor parameter, and update the three call sites in grok_token_provider_test.go / grok_quota_service_test.go.

2.3 GrokTokenRefresher should depend on an interface, not *GrokOAuthService. grok_token_refresher.go:13 holds the concrete type but only needs two methods (RefreshAccountToken, BuildAccountCredentials). Other Service-layer types in this repo depend on narrow interfaces rather than concrete implementations; please extract one next to GrokOAuthClient in service/oauth_service.go and depend on that. I know AntigravityTokenProvider has the same wart — that's inherited debt, but I'd prefer not to propagate it.

Bonus: account_usage_service.go:866-869 has a lazy NewGrokQuotaFetcher() fallback that bypasses DI. Wire already provides the fetcher (wire.go:614, wire_gen.go:184), so this branch is unreachable in production and just hurts testability. Please remove it.


Once these two are addressed (with the missing test restored), happy to merge. Thanks again.

Wei-Shaw avatar Jun 26 '26 06:06 Wei-Shaw

@Wei-Shaw thanks for the detailed review. Addressed the requested fixes on current head 44f502bab8b2ac26ec3c51bccb94dee371bd41c2:

  • Clamped Grok OAuth account concurrency at write time unless XAI_GROK_UNSAFE_ALLOW_CONCURRENCY_GT_ONE is enabled, and restored regression coverage.
  • Injected ProxyRepository into GrokQuotaService and added fallback proxy resolution when account.Proxy is not eager-loaded.
  • Removed the dead GrokTokenProvider.grokOAuthService field/constructor parameter.
  • Changed GrokTokenRefresher to depend on the narrow GrokOAuthTokenService interface.
  • Removed the lazy NewGrokQuotaFetcher() fallback from account usage service.

Local validation on this head:

  • cd backend && GOCACHE=/private/tmp/sub2api-go-cache /Users/heatherm/.cache/codex/toolchains/go1.26.4-darwin-arm64/go/bin/go test -tags unit ./internal/service ./internal/handler/admin -run 'NormalizeAccountConcurrency|GrokQuota|GrokToken|GrokOAuthHandler'
  • cd backend && GOCACHE=/private/tmp/sub2api-go-cache /Users/heatherm/.cache/codex/toolchains/go1.26.4-darwin-arm64/go/bin/go test -tags unit ./cmd/server
  • cd backend && GOCACHE=/private/tmp/sub2api-go-cache /Users/heatherm/.cache/codex/toolchains/go1.26.4-darwin-arm64/go/bin/go test -tags unit ./internal/service
  • cd backend && GOCACHE=/private/tmp/sub2api-go-cache /Users/heatherm/.cache/codex/toolchains/go1.26.4-darwin-arm64/go/bin/go test -tags unit ./internal/handler/admin
  • git diff --check

I also updated the PR description with the current-head review follow-up, validation notes, and CI state. CLA is green; CI and Security Scan are currently action_required for the new fork commit and need maintainer approval/rerun.

heathermhuang avatar Jun 26 '26 06:06 heathermhuang