Reset ChatGPT Codex auth during OAuth setup (#8569)

Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
Co-authored-by: goose <goose@aaif.dev>
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Vincenzo Palazzo 2026-04-20 13:36:20 +02:00 committed by GitHub
parent e953a49bbf
commit f2350f8d68
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 31 additions and 3 deletions

View file

@ -806,6 +806,10 @@ impl ChatGptCodexAuthProvider {
}
}
fn clear_cached_tokens(&self) {
self.cache.clear();
}
async fn get_valid_token(&self) -> Result<TokenData> {
if let Some(mut token_data) = self.cache.load() {
if token_data.expires_at > Utc::now() + chrono::Duration::seconds(60) {
@ -865,6 +869,11 @@ pub struct ChatGptCodexProvider {
}
impl ChatGptCodexProvider {
pub async fn cleanup() -> Result<()> {
TokenCache::new().clear();
Ok(())
}
pub async fn from_env(model: ModelConfig) -> Result<Self> {
let auth_provider = Arc::new(ChatGptCodexAuthProvider::new(
ChatGptCodexAuthState::instance(),
@ -997,10 +1006,25 @@ impl Provider for ChatGptCodexProvider {
}
async fn configure_oauth(&self) -> Result<(), ProviderError> {
self.auth_provider
.get_valid_token()
let previous_token = self.auth_provider.cache.load();
self.auth_provider.clear_cached_tokens();
let result = perform_oauth_flow(self.auth_provider.state.as_ref())
.await
.map_err(|e| ProviderError::Authentication(format!("OAuth flow failed: {}", e)))?;
.and_then(|token_data| self.auth_provider.cache.save(&token_data));
if let Err(e) = result {
if let Some(previous_token) = previous_token.as_ref() {
if self.auth_provider.cache.load().is_none() {
let _ = self.auth_provider.cache.save(previous_token);
}
}
return Err(ProviderError::Authentication(format!(
"OAuth flow failed: {}",
e
)));
}
Ok(())
}

View file

@ -100,6 +100,10 @@ async fn init_registry() -> RwLock<ProviderRegistry> {
"kimi_code",
Arc::new(|| Box::pin(KimiCodeProvider::cleanup())),
);
registry.set_cleanup(
"chatgpt_codex",
Arc::new(|| Box::pin(ChatGptCodexProvider::cleanup())),
);
if let Err(e) = load_custom_providers_into_registry(&mut registry) {
tracing::warn!("Failed to load custom providers: {}", e);