Files
codex/codex-rs/tui/src/app/backend_preferences.rs
T
Benjamin Carlsson 854e723882 Add --cloud to launch interactive Codex sessions on Cloud (#65063)
## What changed

- Select the Codex Cloud backend at startup with `codex --cloud`,
  `codex agents --cloud`, or `codex resume --cloud`.
- Use `--env <ENV_ID>` to select a saved Cloud environment for new
  threads. Resume by picker, most recent session, ID, or name using the
  thread's saved environment and model.
- Reject local execution overrides and managed execution requirements
  that Cloud cannot enforce. Recheck managed requirements for the
  ChatGPT workspace selected when connecting.

## Testing

Cover initial prompt delivery, resume by ID and name, and rejection of
unsupported settings, including managed execution requirements from a
workspace selected after startup configuration was loaded.

GitOrigin-RevId: 6144decb9679f903607a586466cff37e402ab4a4
2026-12-01 19:17:41 +00:00

108 lines
3.8 KiB
Rust

//! Local project and user identity for persisted new-thread preferences.
//! Cloud workspace paths and credentials must not determine the host-side preference scope.
use super::*;
/// Host inputs for the new-thread preference. A cloud thread's cwd and credentials
/// belong to the executor and must never determine where this preference is saved.
#[derive(Clone)]
pub(crate) struct BackendPreferenceContext {
pub(crate) local_cwd: Option<PathBuf>,
pub(crate) cli_cloud_environment: Option<String>,
auth: codex_login::AuthConfig,
project_root_markers: Vec<String>,
#[cfg(test)]
pub(super) test_cloud_environments: Option<Vec<codex_app_server_client::SavedEnvironment>>,
}
impl BackendPreferenceContext {
pub(crate) fn new(config: &Config, local_cwd: Option<PathBuf>) -> Self {
let project_root_markers = config
.config_layer_stack
.layers_high_to_low()
.filter(|layer| !matches!(layer.name, codex_config::ConfigLayerSource::Project { .. }))
.find_map(|layer| {
codex_config::project_root_markers_from_config(&layer.config)
.ok()
.flatten()
})
.unwrap_or_else(codex_config::default_project_root_markers);
Self {
local_cwd,
cli_cloud_environment: None,
auth: config.auth_config(),
project_root_markers,
#[cfg(test)]
test_cloud_environments: None,
}
}
pub(super) async fn cloud_environment(
&self,
settings: &crate::local_settings::LocalSettings,
handle: &AppServerRequestHandle,
) -> Option<codex_app_server_client::SavedEnvironment> {
let AppServerRequestHandle::Cloud(handle) = handle else {
return None;
};
if let Some(id) = &self.cli_cloud_environment {
// An explicit CLI selection takes precedence without fetching the catalog.
return Some(codex_app_server_client::SavedEnvironment {
id: id.clone(),
name: id.clone(),
workspace_shared: false,
});
}
let defaults = &settings.tui.default_cloud_environments;
if defaults.is_empty() {
return None;
}
let (project, user) = self.scope().await?;
let id = defaults.get(&project)?.get(&user)?;
#[cfg(test)]
if let Some(catalog) = &self.test_cloud_environments {
return catalog.iter().find(|env| &env.id == id).cloned();
}
handle
.list_saved_environments()
.await
.ok()?
.into_iter()
.find(|env| &env.id == id)
}
pub(super) async fn scope(&self) -> Option<(String, String)> {
let cwd = AbsolutePathBuf::from_absolute_path(self.local_cwd.as_deref()?).ok()?;
let project = codex_config::loader::find_project_root(
codex_exec_server::LOCAL_FS.as_ref(),
&cwd,
&self.project_root_markers,
)
.await
.ok()?;
let auth = self
.auth
.load_auth(/*enable_codex_api_key_env*/ false)
.await
.ok()?;
let user = if let Some(id) = auth
.as_ref()
.and_then(codex_login::CodexAuth::get_chatgpt_user_id)
{
format!("chatgpt:{id}")
} else if auth
.as_ref()
.is_some_and(codex_login::CodexAuth::is_chatgpt_auth)
{
return None;
} else {
// A local profile without an account is already private to its CODEX_HOME.
"local".to_string()
};
Some((
codex_config::loader::project_trust_key(project.as_path()),
user,
))
}
}