@@ -339,6 +339,7 @@ use codex_feedback::FeedbackUploadOptions;
339339use codex_git_utils:: git_diff_to_remote;
340340use codex_git_utils:: resolve_root_git_project_for_trust;
341341use codex_login:: AuthManager ;
342+ use codex_login:: AuthReloadStatus ;
342343use codex_login:: CLIENT_ID ;
343344use codex_login:: CodexAuth ;
344345use codex_login:: ServerOptions as LoginServerOptions ;
@@ -455,6 +456,78 @@ use uuid::Uuid;
455456#[ cfg( test) ]
456457use codex_app_server_protocol:: ServerRequest ;
457458
459+ async fn reload_auth_from_storage_if_idle (
460+ auth_manager : & Arc < AuthManager > ,
461+ thread_manager : & Arc < ThreadManager > ,
462+ config_manager : & ConfigManager ,
463+ outgoing : & OutgoingMessageSender ,
464+ thread_watch_manager : & ThreadWatchManager ,
465+ chatgpt_base_url : & str ,
466+ reason : & str ,
467+ ) {
468+ if * thread_watch_manager. subscribe_running_turn_count ( ) . borrow ( ) != 0 {
469+ return ;
470+ }
471+
472+ let status = auth_manager. reload_with_status ( ) . await ;
473+ match handle_auth_reload_status (
474+ status,
475+ auth_manager,
476+ thread_manager,
477+ config_manager,
478+ outgoing,
479+ chatgpt_base_url,
480+ reason,
481+ )
482+ . await
483+ {
484+ AuthReloadStatus :: Reloaded { .. } => { }
485+ AuthReloadStatus :: Failed => {
486+ warn ! ( "failed to reload auth from storage before {reason}" ) ;
487+ }
488+ }
489+ }
490+
491+ async fn handle_auth_reload_status (
492+ status : AuthReloadStatus ,
493+ auth_manager : & Arc < AuthManager > ,
494+ thread_manager : & Arc < ThreadManager > ,
495+ config_manager : & ConfigManager ,
496+ outgoing : & OutgoingMessageSender ,
497+ chatgpt_base_url : & str ,
498+ reason : & str ,
499+ ) -> AuthReloadStatus {
500+ match status {
501+ AuthReloadStatus :: Reloaded { changed } => {
502+ if changed {
503+ let invalidated_thread_count =
504+ thread_manager. invalidate_model_transport_caches ( ) . await ;
505+ info ! (
506+ "auth reloaded from storage before {reason}; invalidated model transport caches for {invalidated_thread_count} tracked thread(s)"
507+ ) ;
508+ config_manager. replace_cloud_requirements_loader (
509+ Arc :: clone ( auth_manager) ,
510+ chatgpt_base_url. to_string ( ) ,
511+ ) ;
512+ config_manager
513+ . sync_default_client_residency_requirement ( )
514+ . await ;
515+ let auth = auth_manager. auth_cached ( ) ;
516+ outgoing
517+ . send_server_notification ( ServerNotification :: AccountUpdated (
518+ AccountUpdatedNotification {
519+ auth_mode : auth. as_ref ( ) . map ( CodexAuth :: api_auth_mode) ,
520+ plan_type : auth. as_ref ( ) . and_then ( CodexAuth :: account_plan_type) ,
521+ } ,
522+ ) )
523+ . await ;
524+ }
525+ AuthReloadStatus :: Reloaded { changed }
526+ }
527+ AuthReloadStatus :: Failed => AuthReloadStatus :: Failed ,
528+ }
529+ }
530+
458531mod account_processor;
459532mod apps_processor;
460533mod catalog_processor;
0 commit comments