fix(auxiliary): detect xAI OAuth 403 bad-credentials as auth error (#31527)#34431
Merged
Conversation
xAI returns HTTP 403 (not 401) with unauthenticated:bad-credentials when an OAuth2 access token has expired or is invalid. The existing _is_auth_error() only checked for 401 status codes, so these tokens were never refreshed and the 403 propagated as a generic permission denied error. Three fixes: 1. _is_auth_error: Recognize xAI's 403+bad-credentials pattern as an auth failure, triggering token refresh instead of silent failure. 2. _refresh_provider_credentials: Add xai-oauth branch with pool-level refresh (try_refresh_current with select to ensure current entry) then fallback to singleton resolver with force_refresh=True. 3. _recoverable_pool_provider: Map api.x.ai host to xai-oauth pool for auto-resolved providers, matching existing pattern for openai-codex/openrouter/nous/anthropic. Includes 14 tests covering the new detection logic, host mapping, and graceful fallback behavior. Signed-off-by: moikapy <moikapy@devmoi.com>
Contributor
🔎 Lint report:
|
| Rule | Count |
|---|---|
unresolved-import |
1 |
unresolved-attribute |
1 |
First entries
tests/agent/test_auxiliary_client_xai_oauth_recovery.py:11: [unresolved-import] unresolved-import: Cannot resolve imported module `pytest`
tests/agent/test_auxiliary_client_xai_oauth_recovery.py:78: [unresolved-attribute] unresolved-attribute: Unresolved attribute `status_code` on type `Exception`
✅ Fixed issues: none
Unchanged: 4891 pre-existing issues carried over.
Diagnostics are surfaced as warnings — this check never fails the build.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The auxiliary client now recognizes xAI's HTTP 403 +
unauthenticated:bad-credentialsas an expired-OAuth-token signal and triggers refresh/rotation, instead of letting title generation / compression / vision routing silently fail when an aux task runs against xAI OAuth.Salvages PR #31527 (@Moikapy). Distinct from the recently-merged #30872 / #29344 fix, which covered the main agent loop's
run_agent._is_entitlement_failure+_recover_with_credential_poolpaths. The auxiliary client has its own parallel recovery system (_is_auth_error,_refresh_provider_credentials,_recoverable_pool_provider) that the previous fix didn't touch.Three gaps
_is_auth_errorbad-credentials, andunauthenticated:bad-credentialssubstring_refresh_provider_credentialsopenai-codex,nous,anthropicxai-oauthbranch: pool-leveltry_refresh_current()first, falls back toresolve_xai_oauth_runtime_credentials(force_refresh=True)_recoverable_pool_providerapi.x.ai→xai-oauthSalvage details
Resolved one merge conflict in
_recoverable_pool_provider— main grew amain_runtime-based PROVIDER_REGISTRY fallback block between the PR's branch point and now. Kept both: the newapi.x.aihost check + the PROVIDER_REGISTRY fallback. Verified by behavior — the PR's tests still pass against this resolution.Validation
PR ships 14 new tests covering all three functions (detection, host mapping, refresh structure) — no follow-up tests needed from us; the existing coverage was already complete.
Credit
@Moikapy — clean diagnosis, three minimal-scope fixes at the right intervention points. The tests directly mirror the bug pattern (xAI's actual 403 response shape) so future regressions get caught.