Support saved environments when starting Cloud threads (#61956)

## What changed

- Add `EnvironmentSelection` to start Cloud threads with either a saved environment ID or a project checkout in the default environment.
- Expose saved environment discovery through the authenticated Cloud request handle, rejecting results if the account changes.
- Thread environment selection through the app-server client and TUI session startup, and defer executor readiness for either explicit environment choice.

## Testing

Extend prewarm request tests to cover saved environment IDs and deferred readiness. Extend TUI session tests to verify saved environment routing without local working-directory or workspace-root overrides.

GitOrigin-RevId: 626cec9926dad8f1a89b1a3ee9e4470850ae6a76
This commit is contained in:
Benjamin Carlsson
2026-11-17 03:17:05 +00:00
committed by copyberry
parent 50c227a6e2
commit e9a2a9e115
14 changed files with 195 additions and 79 deletions
+22 -7
View File
@@ -35,11 +35,11 @@ pub enum CloudOperation {
},
/// Sent on the scoped channel after the caller has prepared the replacement view.
CommitPreparedResume,
/// Host project metadata stays on the private transport, outside the app-server wire.
StartProjectThread {
/// Environment selection stays on the private transport, outside the app-server wire.
StartThreadWithEnvironment {
request_id: RequestId,
params: Box<codex_app_server_protocol::ThreadStartParams>,
project: codex_cloud_client::ProjectEnvironment,
environment: codex_cloud_client::EnvironmentSelection,
},
SubmitUserMessage(Box<TurnStartParams>),
/// Apply the result of a name write that already succeeded outside the command queue.
@@ -339,16 +339,31 @@ impl CloudRequestHandle {
})
}
pub(crate) async fn start_project_thread(
/// Discover saved environments using this Cloud connection's authenticated account.
pub async fn list_saved_environments(
&self,
) -> Result<Vec<codex_cloud_client::SavedEnvironment>, codex_cloud_client::Error> {
let catalog = self
.native
.as_ref()
.ok_or(codex_cloud_client::Error::InvalidCredentials)?;
let environments = catalog.list_saved_environments().await?;
if !catalog.model_catalog_is_current() {
return Err(codex_cloud_client::Error::InvalidCredentials);
}
Ok(environments)
}
pub(crate) async fn start_thread_with_environment(
&self,
request_id: RequestId,
params: codex_app_server_protocol::ThreadStartParams,
project: codex_cloud_client::ProjectEnvironment,
environment: codex_cloud_client::EnvironmentSelection,
) -> io::Result<RequestResult> {
self.send(CloudOperation::StartProjectThread {
self.send(CloudOperation::StartThreadWithEnvironment {
request_id,
params: Box::new(params),
project,
environment,
})
.await
}
@@ -74,7 +74,7 @@ async fn rejected_rename_preserves_loaded_cloud_thread_name() -> Result<()> {
},
},
cloud::SubmissionMode::StartIfIdle,
/*project*/ None,
/*environment*/ None,
&mut requests,
&mut replies,
&output,
@@ -154,7 +154,7 @@ async fn failed_archive_loaded_list_tracks_attachment_not_cached_metadata() -> R
},
},
cloud::SubmissionMode::StartIfIdle,
/*project*/ None,
/*environment*/ None,
&mut requests,
&mut replies,
&output,
@@ -168,7 +168,7 @@ async fn failed_archive_loaded_list_tracks_attachment_not_cached_metadata() -> R
params: Default::default(),
},
cloud::SubmissionMode::StartIfIdle,
/*project*/ None,
/*environment*/ None,
&mut requests,
&mut replies,
&output,
@@ -202,7 +202,7 @@ async fn failed_archive_loaded_list_tracks_attachment_not_cached_metadata() -> R
params: Default::default(),
},
cloud::SubmissionMode::StartIfIdle,
/*project*/ None,
/*environment*/ None,
&mut requests,
&mut replies,
&output,
@@ -266,14 +266,14 @@ async fn dropping_an_in_flight_start_unblocks_the_native_worker() -> Result<()>
request_id: protocol::RequestId::Integer(1),
params: Default::default(),
})),
CloudOperation::StartProjectThread {
CloudOperation::StartThreadWithEnvironment {
request_id: protocol::RequestId::Integer(1),
params: Box::default(),
project: cloud::ProjectEnvironment {
environment: cloud::EnvironmentSelection::Project(cloud::ProjectEnvironment {
name: "project".into(),
origin_urls: vec!["https://github.com/example/project.git".into()],
cwd: None,
},
}),
},
] {
assert_abandoned_request_unblocks_worker("Prewarm", operation).await?;
@@ -337,13 +337,13 @@ async fn assert_abandoned_request_unblocks_worker(
let mut pending = Box::pin(async {
match operation {
CloudOperation::Request(request) => client.request(*request).await,
CloudOperation::StartProjectThread {
CloudOperation::StartThreadWithEnvironment {
request_id,
params,
project,
environment,
} => {
handle
.start_project_thread(request_id, *params, project)
.start_thread_with_environment(request_id, *params, environment)
.await
}
_ => unreachable!("fixture supports Cloud lists and starts"),
@@ -362,7 +362,7 @@ async fn run_worker_with_prepared(
protocol::ClientRequest::ThreadResume { .. } => (false, true),
_ => (false, false),
},
CloudOperation::StartProjectThread { .. } => (true, true),
CloudOperation::StartThreadWithEnvironment { .. } => (true, true),
CloudOperation::PrepareThreadResume { .. } if command.response.is_closed() => continue,
CloudOperation::PrepareThreadResume { .. } => (false, true),
_ => (false, false),
@@ -419,17 +419,17 @@ async fn run_worker_with_prepared(
CloudOperation::CommitPreparedResume => Err(eyre!(
"Cloud resume commit requires its scoped history handle."
)),
CloudOperation::StartProjectThread {
CloudOperation::StartThreadWithEnvironment {
request_id,
params,
project,
environment,
} => Ok((
protocol::ClientRequest::ThreadStart {
request_id,
params: *params,
},
cloud::SubmissionMode::StartIfIdle,
Some(project),
Some(environment),
)),
CloudOperation::SubmitUserMessage(params) => Ok((
protocol::ClientRequest::TurnStart {
@@ -448,7 +448,7 @@ async fn run_worker_with_prepared(
)),
};
let result = match request {
Ok((request, mode, project)) => {
Ok((request, mode, environment)) => {
let unloads = prepared.as_ref().is_some_and(|candidate| {
let id = match &request {
protocol::ClientRequest::ThreadArchive { params, .. } => &params.thread_id,
@@ -479,7 +479,7 @@ async fn run_worker_with_prepared(
}
continue;
}
result = handle_request(request, mode, project, requests, replies, output) => result,
result = handle_request(request, mode, environment, requests, replies, output) => result,
};
if unloads
&& result.is_ok()
@@ -556,7 +556,7 @@ async fn prepare_thread_resume(
_ = ready.closed() => return None,
prepared = async {
let response = replies.while_listening(
requests.handle(request, cloud::SubmissionMode::StartIfIdle, /*project*/ None, output),
requests.handle(request, cloud::SubmissionMode::StartIfIdle, /*environment*/ None, output),
output,
).await?;
let requests::Response::Attach(settings) = response else {
@@ -655,7 +655,7 @@ async fn advance_prepared_resume(
_ = response.closed() => None,
_ = output.closed() => None,
page = replies.while_listening(
requests.handle(*request, cloud::SubmissionMode::StartIfIdle, /*project*/ None, &warning_output),
requests.handle(*request, cloud::SubmissionMode::StartIfIdle, /*environment*/ None, &warning_output),
output,
) => Some(page),
};
@@ -688,7 +688,7 @@ async fn advance_prepared_resume(
async fn handle_request(
request: protocol::ClientRequest,
mode: cloud::SubmissionMode,
project: Option<cloud::ProjectEnvironment>,
environment: Option<cloud::EnvironmentSelection>,
requests: &mut requests::Requests,
replies: &mut Replies,
output: &mpsc::UnboundedSender<AppServerEvent>,
@@ -731,7 +731,7 @@ async fn handle_request(
_ => false,
};
let response = {
let handling = requests.handle(request, mode, project, output);
let handling = requests.handle(request, mode, environment, output);
tokio::pin!(handling);
let immediate = if selected_mutation && let Some(recovery) = &mut replies.idle {
// Local admission validation finishes before the RPC yields. Poll without
@@ -78,7 +78,7 @@ impl Requests {
&mut self,
request: protocol::ClientRequest,
mode: cloud::SubmissionMode,
project: Option<cloud::ProjectEnvironment>,
environment: Option<cloud::EnvironmentSelection>,
output: &mpsc::UnboundedSender<crate::AppServerEvent>,
) -> Result<Response> {
use protocol::ClientRequest as Request;
@@ -130,7 +130,10 @@ impl Requests {
"Cloud conversations start with server defaults. Custom settings are not supported yet."
);
}
let settings = self.client.prewarm_thread(model, provider, project).await?;
let settings = self
.client
.prewarm_thread(model, provider, environment)
.await?;
return attach_response(settings);
}
Request::ThreadResume { params, .. } => {
+7 -5
View File
@@ -22,7 +22,9 @@
mod cloud;
pub mod cloud_backend;
pub use codex_cloud_client::EnvironmentSelection;
pub use codex_cloud_client::ProjectEnvironment;
pub use codex_cloud_client::SavedEnvironment;
mod path;
mod remote;
mod remote_addr;
@@ -725,22 +727,22 @@ impl InProcessAppServerRequestHandle {
}
impl AppServerRequestHandle {
/// Starts a native cloud thread with host project metadata without adding wire fields.
pub async fn start_cloud_project_thread(
/// Starts a native Cloud thread with a saved config or a default project checkout.
pub async fn start_cloud_thread_with_environment(
&self,
request_id: RequestId,
params: codex_app_server_protocol::ThreadStartParams,
project: codex_cloud_client::ProjectEnvironment,
environment: codex_cloud_client::EnvironmentSelection,
) -> Result<codex_app_server_protocol::ThreadStartResponse, TypedRequestError> {
let response = match self {
Self::Cloud(handle) => {
handle
.start_project_thread(request_id, params, project)
.start_thread_with_environment(request_id, params, environment)
.await
}
Self::InProcess(_) | Self::Remote(_) => Err(IoError::new(
std::io::ErrorKind::Unsupported,
"Project checkout requires the native cloud transport.",
"Selecting an environment requires the native Cloud transport.",
)),
};
decode_typed_response("thread/start", response)