From a0995c2b2b68248960c69cb812b5f4898dac7e4c Mon Sep 17 00:00:00 2001 From: Benjamin Carlsson Date: Sun, 22 Nov 2026 18:00:23 +0000 Subject: [PATCH] Fix account refresh for new cloud sessions (#63171) ## Why When a provider change triggers an account refresh for a new cloud session, the TUI tries to read configuration that the cloud backend does not expose. The cloud account belongs to the authenticated connection and is independent of the thread's model provider. ## What changed Use the cloud backend's `account/read` response directly and skip `config/read` during the refresh. ## Testing Add a regression test covering both a pending provider change and a different provider returned by the new thread. It checks that the TUI sends only `account/read`, attaches the thread, and applies the returned authentication and plan details. GitOrigin-RevId: a195fc6735227ff65e0347d6dd9588b64dbb6ada --- .../tui/src/app/tests/new_session_tests.rs | 110 ++++++++++++++++++ .../tui/src/app_server_session/account.rs | 5 + 2 files changed, 115 insertions(+) diff --git a/codex-rs/tui/src/app/tests/new_session_tests.rs b/codex-rs/tui/src/app/tests/new_session_tests.rs index ac77d7c039..563c4be407 100644 --- a/codex-rs/tui/src/app/tests/new_session_tests.rs +++ b/codex-rs/tui/src/app/tests/new_session_tests.rs @@ -166,6 +166,116 @@ async fn new_session_refreshes_account_for_the_started_provider() -> Result<()> Ok(()) } +#[tokio::test] +async fn cloud_fresh_session_refreshes_account_without_project_config() -> Result<()> { + use crate::app::session_lifecycle::PreparedFreshSession; + use crate::app::session_lifecycle::ThreadAttachPresentation; + use codex_app_server_client::AppServerClient; + use codex_app_server_client::CloudClient; + use codex_app_server_client::CloudOperation; + + // Either a pending provider change or a different resolved provider refreshes the account. + for (provider, provider_may_have_changed) in [("openai", true), ("cloud-provider", false)] { + let (mut app, mut events, _ops) = make_test_app_with_channels().await; + app.app_server_target = AppServerTarget::Cloud; + app.local_settings.tui.show_tooltips = false; + *app.model_catalog.provider_write.lock().await = provider_may_have_changed; + let id = ThreadId::new(); + let mut session = test_thread_session(id, app.config.cwd.to_path_buf()); + session.model_provider_id = provider.into(); + let prepared = PreparedFreshSession { + started: AppServerStartedThread { + session, + turns: Vec::new(), + blocks_direct_input: false, + task_tools_available: false, + }, + config: app.config.clone(), + local_settings: app.local_settings.clone(), + }; + let (client, mut commands, _server_events) = CloudClient::channel(/*capacity*/ 1); + let mut server = AppServerSession::new( + crate::backend_id::BackendId::new(), + AppServerClient::Cloud(client), + crate::ThreadParamsMode::Cloud, + ); + let mut tui = crate::tui::test_support::make_test_tui()?; + while events.try_recv().is_ok() {} + let mut requests = Vec::new(); + { + let mut adopt = Box::pin(app.adopt_fresh_session( + &mut tui, + &mut server, + prepared, + ThreadAttachPresentation::Fresh, + /*initial_user_message*/ None, + /*new_thread_name*/ None, + )); + loop { + tokio::select! { + () = &mut adopt => break, + command = commands.recv() => { + let command = command.expect("account request"); + let CloudOperation::Request(request) = command.operation else { + panic!("expected account/read"); + }; + requests.push(request.method_name().to_owned()); + let response = match *request { + ClientRequest::GetAccount { .. } => Ok(serde_json::json!({ + "account": { + "type": "chatgpt", + "email": "user@example.com", + "planType": "pro", + "workspaceName": "Cloud workspace" + }, + "requiresOpenaiAuth": true + })), + _ => Err(JSONRPCErrorError { + code: -32601, + message: format!("{} is not supported by the cloud backend yet.", request.method_name()), + data: None, + }), + }; + command.response.send(Ok(response)).unwrap(); + } + } + } + } + assert_eq!(requests, ["account/read"]); + assert_eq!(app.active_thread_id, Some(id)); + assert_eq!( + ( + app.chat_widget.has_chatgpt_account(), + app.chat_widget.has_codex_backend_auth(), + app.chat_widget.requires_openai_auth, + app.chat_widget.current_plan_type(), + ), + ( + true, + true, + true, + Some(codex_protocol::account::PlanType::Pro) + ) + ); + let history = std::iter::from_fn(|| events.try_recv().ok()) + .filter_map(|event| match event { + AppEvent::InsertHistoryCell(cell) => { + Some(lines_to_single_string(&cell.display_lines(/*width*/ 1000))) + } + _ => None, + }) + .collect::>() + .join("\n"); + insta::allow_duplicates! { + insta::assert_snapshot!(normalize_snapshot_paths(history).trim(), @" + >_ OpenAI Codex (v0.0.0) + /tmp/project + "); + } + } + Ok(()) +} + #[tokio::test] async fn new_session_on_same_provider_preserves_quota_and_pending_recovery() -> Result<()> { let (mut app, mut events, _ops) = make_test_app_with_channels().await; diff --git a/codex-rs/tui/src/app_server_session/account.rs b/codex-rs/tui/src/app_server_session/account.rs index 0077a117b5..20ad36a1cf 100644 --- a/codex-rs/tui/src/app_server_session/account.rs +++ b/codex-rs/tui/src/app_server_session/account.rs @@ -77,6 +77,11 @@ impl AppServerSession { session: &ThreadSessionState, ) -> Result { let response = self.read_account().await?; + // Cloud's account identifies the authenticated connection, independent of the thread's + // model provider. Cloud does not expose the project configuration used by app servers. + if self.thread_params_mode() == ThreadParamsMode::Cloud { + return Ok(response.into()); + } // account/read resolves the server's non-project configuration. A thread can override it. let request_id = self.next_request_id(); let defaults: EffectiveConfig = self