|
| 1 | +use crate::auth_mode::auth_mode_to_api; |
1 | 2 | use crate::bespoke_event_handling::apply_bespoke_event_handling; |
2 | 3 | use crate::command_exec::CommandExecManager; |
3 | 4 | use crate::command_exec::StartCommandExecParams; |
@@ -394,6 +395,7 @@ use codex_feedback::FeedbackUploadOptions; |
394 | 395 | use codex_git_utils::git_diff_to_remote; |
395 | 396 | use codex_git_utils::resolve_root_git_project_for_trust; |
396 | 397 | use codex_login::AuthManager; |
| 398 | +use codex_login::AuthReloadStatus; |
397 | 399 | use codex_login::CODEX_OPEN_APP_URL; |
398 | 400 | use codex_login::CodexAuth; |
399 | 401 | use codex_login::LoginSuccessPage; |
@@ -517,6 +519,81 @@ use uuid::Uuid; |
517 | 519 | #[cfg(test)] |
518 | 520 | use codex_app_server_protocol::ServerRequest; |
519 | 521 |
|
| 522 | +async fn reload_auth_from_storage_if_idle( |
| 523 | + auth_manager: &Arc<AuthManager>, |
| 524 | + thread_manager: &Arc<ThreadManager>, |
| 525 | + config_manager: &ConfigManager, |
| 526 | + outgoing: &OutgoingMessageSender, |
| 527 | + thread_watch_manager: &ThreadWatchManager, |
| 528 | + chatgpt_base_url: &str, |
| 529 | + reason: &str, |
| 530 | +) { |
| 531 | + if *thread_watch_manager.subscribe_running_turn_count().borrow() != 0 { |
| 532 | + return; |
| 533 | + } |
| 534 | + |
| 535 | + let status = auth_manager.reload_with_status().await; |
| 536 | + match handle_auth_reload_status( |
| 537 | + status, |
| 538 | + auth_manager, |
| 539 | + thread_manager, |
| 540 | + config_manager, |
| 541 | + outgoing, |
| 542 | + chatgpt_base_url, |
| 543 | + reason, |
| 544 | + ) |
| 545 | + .await |
| 546 | + { |
| 547 | + AuthReloadStatus::Reloaded { .. } => {} |
| 548 | + AuthReloadStatus::Failed => { |
| 549 | + warn!("failed to reload auth from storage before {reason}"); |
| 550 | + } |
| 551 | + } |
| 552 | +} |
| 553 | + |
| 554 | +async fn handle_auth_reload_status( |
| 555 | + status: AuthReloadStatus, |
| 556 | + auth_manager: &Arc<AuthManager>, |
| 557 | + thread_manager: &Arc<ThreadManager>, |
| 558 | + config_manager: &ConfigManager, |
| 559 | + outgoing: &OutgoingMessageSender, |
| 560 | + chatgpt_base_url: &str, |
| 561 | + reason: &str, |
| 562 | +) -> AuthReloadStatus { |
| 563 | + match status { |
| 564 | + AuthReloadStatus::Reloaded { changed } => { |
| 565 | + if changed { |
| 566 | + let invalidated_thread_count = |
| 567 | + thread_manager.invalidate_model_transport_caches().await; |
| 568 | + info!( |
| 569 | + "auth reloaded from storage before {reason}; invalidated model transport caches for {invalidated_thread_count} tracked thread(s)" |
| 570 | + ); |
| 571 | + config_manager.replace_cloud_config_bundle_loader( |
| 572 | + Arc::clone(auth_manager), |
| 573 | + chatgpt_base_url.to_string(), |
| 574 | + ); |
| 575 | + config_manager |
| 576 | + .sync_default_client_residency_requirement() |
| 577 | + .await; |
| 578 | + let auth = auth_manager.auth_cached(); |
| 579 | + outgoing |
| 580 | + .send_server_notification(ServerNotification::AccountUpdated( |
| 581 | + AccountUpdatedNotification { |
| 582 | + auth_mode: auth |
| 583 | + .as_ref() |
| 584 | + .map(CodexAuth::api_auth_mode) |
| 585 | + .map(auth_mode_to_api), |
| 586 | + plan_type: auth.as_ref().and_then(CodexAuth::account_plan_type), |
| 587 | + }, |
| 588 | + )) |
| 589 | + .await; |
| 590 | + } |
| 591 | + AuthReloadStatus::Reloaded { changed } |
| 592 | + } |
| 593 | + AuthReloadStatus::Failed => AuthReloadStatus::Failed, |
| 594 | + } |
| 595 | +} |
| 596 | + |
520 | 597 | mod account_processor; |
521 | 598 | mod apps_processor; |
522 | 599 | mod bedrock_auth; |
|
0 commit comments