mirror of
https://github.com/openai/codex.git
synced 2026-11-22 23:00:42 +00:00
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
This commit is contained in:
committed by
copyberry
parent
2ef0f66901
commit
a0995c2b2b
@@ -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::<Vec<_>>()
|
||||
.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;
|
||||
|
||||
@@ -77,6 +77,11 @@ impl AppServerSession {
|
||||
session: &ThreadSessionState,
|
||||
) -> Result<SessionAccount> {
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user