Retry provider auth commands after initial failures (#40523)

## Why

A provider auth command can fail during initial credential resolution, leaving
no cached authentication. A subsequent `401` should still give the configured
provider one bounded opportunity to recover.

## What changed

- Treat configured external authentication with an empty cache as eligible for
  unauthorized recovery.
- Allow authority refresh to invoke the external provider when no prior auth is
  cached, while preserving the existing requirements for managers without an
  external provider.

## Testing

Added unit and client coverage for a provider command that fails initially and
succeeds during `401` recovery.

GitOrigin-RevId: 7cab9a03f90ce29b412be8fbd97b297acdad4482
This commit is contained in:
mpc-oai authored and copyberry committed 2026-08-25 02:38:22 +00:00
1 parent c1db22a3cd
commit a7b86b6201
3 files changed
+113 -22

No files matched your search

+26 -22
View File
@@ -1890,7 +1890,7 @@ impl UnauthorizedRecovery {
}
pub fn has_next(&self) -> bool {
if self.manager.has_external_api_key_auth() {
if self.manager.has_refreshable_external_auth() {
return !matches!(self.step, UnauthorizedRecoveryStep::Done);
}
@@ -1911,7 +1911,7 @@ impl UnauthorizedRecovery {
}
pub fn unavailable_reason(&self) -> &'static str {
if self.manager.has_external_api_key_auth() {
if self.manager.has_refreshable_external_auth() {
return if matches!(self.step, UnauthorizedRecoveryStep::Done) {
"recovery_exhausted"
} else {
@@ -2741,12 +2741,12 @@ impl AuthManager {
.and_then(|external_auth| external_auth.clone())
}
fn has_external_api_key_auth(&self) -> bool {
fn has_refreshable_external_auth(&self) -> bool {
self.has_external_auth()
&& self
.auth_cached()
.as_ref()
.is_some_and(CodexAuth::is_api_key_auth)
.is_none_or(|auth| auth.is_api_key_auth() || auth.supports_unauthorized_recovery())
}
async fn resolve_external_auth(
@@ -2817,40 +2817,44 @@ impl AuthManager {
async fn refresh_token_from_authority_impl(&self) -> Result<(), RefreshTokenError> {
tracing::info!("Refreshing token");
let auth = match self.auth_cached() {
Some(auth) => auth,
None => return Ok(()),
};
if let Some(error) = self.refresh_failure_for_auth(&auth) {
let attempted_auth = self.auth_cached();
if let Some(error) = attempted_auth
.as_ref()
.and_then(|auth| self.refresh_failure_for_auth(auth))
{
return Err(RefreshTokenError::Permanent(error));
}
let attempted_auth = auth.clone();
let result = if self.has_external_auth() {
self.refresh_external_auth(ExternalAuthRefreshReason::Unauthorized)
.await
} else {
match auth {
CodexAuth::Chatgpt(chatgpt_auth) => {
match attempted_auth.as_ref() {
Some(CodexAuth::Chatgpt(chatgpt_auth)) => {
let token_data = chatgpt_auth.current_token_data().ok_or_else(|| {
RefreshTokenError::Transient(std::io::Error::other(
"Token data is not available.",
))
})?;
self.refresh_and_persist_chatgpt_token(&chatgpt_auth, token_data.refresh_token)
self.refresh_and_persist_chatgpt_token(chatgpt_auth, token_data.refresh_token)
.await
}
CodexAuth::ApiKey(_)
| CodexAuth::ChatgptAuthTokens(_)
| CodexAuth::Headers(_)
| CodexAuth::AgentIdentity(_)
| CodexAuth::PersonalAccessToken(_)
| CodexAuth::BedrockApiKey(_)
| CodexAuth::BedrockAccessKeys(_) => Ok(()),
Some(
CodexAuth::ApiKey(_)
| CodexAuth::ChatgptAuthTokens(_)
| CodexAuth::Headers(_)
| CodexAuth::AgentIdentity(_)
| CodexAuth::PersonalAccessToken(_)
| CodexAuth::BedrockApiKey(_)
| CodexAuth::BedrockAccessKeys(_),
)
| None => Ok(()),
}
};
if let Err(RefreshTokenError::Permanent(error)) = &result {
self.record_permanent_refresh_failure_if_unchanged(&attempted_auth, error);
if let Some(attempted_auth) = attempted_auth.as_ref()
&& let Err(RefreshTokenError::Permanent(error)) = &result
{
self.record_permanent_refresh_failure_if_unchanged(attempted_auth, error);
}
result
}