fix(client): auto-retry agent requests with Bearer when API key is rejected#64
Merged
Conversation
…jected When a profile's API key is stale/revoked and the server returns 401, agent commands now automatically retry with the login session token instead of failing with "API Key Not Valid". This prevents users who have a valid login session from being blocked by an expired API key in their agents.json profile. The new _agent_request() wrapper centralizes all 11 agent endpoint calls through a single retry path: try X-API-KEY first, and on 401 fall back to Bearer if available. No retry occurs when already using Bearer or when the error is not 401. Co-authored-by: Cursor <cursoragent@cursor.com>
All endpoints now use Bearer-first, API-key-second priority, matching the behavior of _headers(). The _agent_request() retry is now bidirectional: Bearer rejected → retry with API key, and API key rejected → retry with Bearer. This eliminates the confusing inconsistency where user commands preferred Bearer but agent commands preferred API key. Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
2 tasks
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
_agent_request()wrapper that centralizes all 11 agent endpoint calls through a single retry path: tryX-API-KEYfirst, on 401 fall back to Bearer if availableRoot cause
When
agents.jsonhas a default profile with an expired API key, the CLI sendsX-API-KEY→ gets 401 → fails. Even though the user has a valid login session (Bearer token fromdailybot login), the CLI never tried it because the profile explicitly declared an API key.Test plan
TestAgentBearerFallback:Made with Cursor