Centralize provider request preparation auth checks (#58121)

## What changed

Move the model client's request-preparation loop into the shared `prepare_provider_request` helper and add `RequestAuthChange` to classify auth state changes. For managed auth, retry preparation when credentials change and reject account changes, including when the provider starts or stops exposing an auth manager during setup.

Capture the auth owner and credential generations together and pass them into the preparation callback so the resulting client setup records the state used for that attempt.

## Testing

Add a test verifying that an explicit provider bearer token survives a workspace switch during setup without retrying preparation.

GitOrigin-RevId: ed8710d1c344b48ebf6182e25e6269214dd2d3fe
This commit is contained in:
sgarg-oai
2026-10-29 16:39:38 +00:00
committed by copyberry
parent 6174d05a6a
commit 53d3d7f64e
7 changed files with 184 additions and 56 deletions
+21
View File
@@ -19,6 +19,27 @@ pub struct AuthChangeState {
pub login_generation: u64,
}
/// Whether request preparation can use the credentials it selected.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum RequestAuthChange {
Unchanged,
CredentialsChanged,
OwnerChanged,
}
impl AuthChangeState {
/// Compare a completed attempt with its original owner and starting credential revision.
pub fn compare_for_request(&self, expected_owner: u64, prepared: Self) -> RequestAuthChange {
if self.owner_generation != expected_owner {
RequestAuthChange::OwnerChanged
} else if self.generation != prepared.generation {
RequestAuthChange::CredentialsChanged
} else {
RequestAuthChange::Unchanged
}
}
}
pub(super) fn same_owner(previous: Option<&CodexAuth>, current: Option<&CodexAuth>) -> bool {
let (Some(previous), Some(current)) = (previous, current) else {
return false;
+1
View File
@@ -24,6 +24,7 @@ pub use bedrock_access_keys::login_with_bedrock_access_keys;
pub use bedrock_api_key::BedrockApiKeyAuth;
pub use bedrock_api_key::login_with_bedrock_api_key;
pub use change_state::AuthChangeState;
pub use change_state::RequestAuthChange;
pub use error::RefreshTokenFailedError;
pub use error::RefreshTokenFailedReason;
pub use manager::*;
+1
View File
@@ -58,6 +58,7 @@ pub use auth::OPENAI_API_KEY_ENV_VAR;
pub use auth::REFRESH_TOKEN_URL_OVERRIDE_ENV_VAR;
pub use auth::REVOKE_TOKEN_URL_OVERRIDE_ENV_VAR;
pub use auth::RefreshTokenError;
pub use auth::RequestAuthChange;
pub use auth::UnauthorizedRecovery;
pub use auth::default_client;
pub use auth::enforce_login_restrictions;