From 6ed45b053cde89e085a655a5204f839d2891154b Mon Sep 17 00:00:00 2001 From: jif-oai Date: Thu, 25 Jun 2026 12:56:25 +0100 Subject: [PATCH] Resolve selected roots through current environments --- codex-rs/core/src/environment_selection.rs | 74 ------------------- codex-rs/core/src/session/mod.rs | 8 +- codex-rs/exec-server/src/environment.rs | 46 +++++++++++- .../exec-server/src/resolved_capability.rs | 73 +++++++++++++++++- .../tests/selected_capability_roots.rs | 70 ++++++++++++++++++ 5 files changed, 191 insertions(+), 80 deletions(-) create mode 100644 codex-rs/exec-server/tests/selected_capability_roots.rs diff --git a/codex-rs/core/src/environment_selection.rs b/codex-rs/core/src/environment_selection.rs index fcab147725..fa86dded55 100644 --- a/codex-rs/core/src/environment_selection.rs +++ b/codex-rs/core/src/environment_selection.rs @@ -7,9 +7,6 @@ use codex_exec_server::Environment; use codex_exec_server::EnvironmentManager; use codex_exec_server::ExecServerError; use codex_exec_server::ExecutorFileSystem; -use codex_exec_server::ResolvedSelectedCapabilityRoot; -use codex_protocol::capabilities::CapabilityRootLocation; -use codex_protocol::capabilities::SelectedCapabilityRoot; use codex_protocol::protocol::TurnEnvironmentSelection; use codex_utils_absolute_path::AbsolutePathBuf; use codex_utils_path_uri::PathUri; @@ -230,27 +227,6 @@ pub(crate) struct TurnEnvironmentSnapshot { } impl TurnEnvironmentSnapshot { - pub(crate) fn resolve_selected_capability_roots( - &self, - selected_roots: &[SelectedCapabilityRoot], - ) -> Vec { - selected_roots - .iter() - .filter_map(|selected_root| { - let CapabilityRootLocation::Environment { environment_id, .. } = - &selected_root.location; - let environment = self - .turn_environments - .iter() - .find(|environment| environment.environment_id == *environment_id)?; - Some(ResolvedSelectedCapabilityRoot::new( - selected_root.clone(), - Arc::clone(&environment.environment), - )) - }) - .collect() - } - pub(crate) fn primary(&self) -> Option<&TurnEnvironment> { self.turn_environments.first() } @@ -584,56 +560,6 @@ url = "ws://127.0.0.1:8765" assert_eq!(resolved.snapshot().await.to_selections(), vec![local]); } - #[tokio::test] - async fn selected_capability_roots_bind_only_to_their_ready_environment() { - let cwd = PathUri::parse("file:///workspace").expect("cwd URI"); - let primary = Arc::new(Environment::default_for_tests()); - let selected = Arc::new(Environment::default_for_tests()); - let snapshot = TurnEnvironmentSnapshot { - turn_environments: vec![ - TurnEnvironment::new( - "primary".to_string(), - Arc::clone(&primary), - cwd.clone(), - /*shell*/ None, - ), - TurnEnvironment::new( - "selected".to_string(), - Arc::clone(&selected), - cwd, - /*shell*/ None, - ), - ], - starting: Vec::new(), - }; - let plugin_path = PathUri::parse("file:///plugins/demo").expect("plugin URI"); - let roots = [ - SelectedCapabilityRoot { - id: "demo@1".to_string(), - location: CapabilityRootLocation::Environment { - environment_id: "selected".to_string(), - path: plugin_path.clone(), - }, - }, - SelectedCapabilityRoot { - id: "pending@1".to_string(), - location: CapabilityRootLocation::Environment { - environment_id: "pending".to_string(), - path: plugin_path, - }, - }, - ]; - - let resolved = snapshot.resolve_selected_capability_roots(&roots); - - let [resolved] = resolved.as_slice() else { - panic!("only the ready selected environment should resolve"); - }; - assert_eq!(resolved.selected_root(), &roots[0]); - assert!(Arc::ptr_eq(resolved.environment(), &selected)); - assert!(!Arc::ptr_eq(resolved.environment(), &primary)); - } - #[tokio::test] async fn blocking_snapshot_waits_for_starting_environment() { let listener = TcpListener::bind("127.0.0.1:0") diff --git a/codex-rs/core/src/session/mod.rs b/codex-rs/core/src/session/mod.rs index e006bd8c34..f86f432a69 100644 --- a/codex-rs/core/src/session/mod.rs +++ b/codex-rs/core/src/session/mod.rs @@ -2828,8 +2828,12 @@ impl Session { .await; } let loaded_agents_md = self.services.agents_md_manager.get_loaded().await; - let selected_capability_roots = environments - .resolve_selected_capability_roots(&self.services.selected_capability_roots); + let selected_capability_roots = self + .services + .turn_environments + .environment_manager() + .resolve_selected_capability_roots(&self.services.selected_capability_roots) + .await; Arc::new(StepContext::new( turn_context, environments, diff --git a/codex-rs/exec-server/src/environment.rs b/codex-rs/exec-server/src/environment.rs index 664ef730d0..ba0094c96a 100644 --- a/codex-rs/exec-server/src/environment.rs +++ b/codex-rs/exec-server/src/environment.rs @@ -54,7 +54,7 @@ pub const CODEX_EXEC_SERVER_NOISE_CHATGPT_ACCOUNT_ID_ENV_VAR: &str = #[derive(Debug)] pub struct EnvironmentManager { default_environment: Option, - environments: RwLock>>, + pub(super) environments: RwLock>>, local_environment: Option>, local_runtime_paths: Option, } @@ -574,6 +574,25 @@ impl Environment { } } + /// Starts the initial connection after an environment is actually selected for use. + pub(crate) fn start_connecting_for_use(environment: &Arc) { + if environment.remote_client.is_none() { + return; + } + let mut startup_task = environment + .startup_task + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner); + if startup_task.is_none() { + let environment = Arc::clone(environment); + *startup_task = Some(AbortOnDropHandle::new(tokio::spawn(async move { + if let Err(error) = environment.wait_until_ready().await { + tracing::debug!(%error, "exec-server environment startup failed"); + } + }))); + } + } + /// Returns whether initial startup has either succeeded or permanently failed. pub fn startup_finished(&self) -> bool { self.remote_client @@ -619,6 +638,8 @@ mod tests { use crate::client_api::StdioExecServerCommand; use crate::environment_provider::EnvironmentDefault; use crate::environment_provider::EnvironmentProviderSnapshot; + use codex_protocol::capabilities::CapabilityRootLocation; + use codex_protocol::capabilities::SelectedCapabilityRoot; use codex_utils_path_uri::PathUri; use pretty_assertions::assert_eq; use tokio::net::TcpListener; @@ -1035,7 +1056,7 @@ mod tests { } #[tokio::test] - async fn environment_manager_leaves_stdio_environment_lazy() { + async fn selected_capability_root_starts_lazy_stdio_environment() { let environment = Environment::remote_with_transport( ExecServerTransportParams::StdioCommand { command: StdioExecServerCommand { @@ -1058,10 +1079,29 @@ mod tests { ) .expect("environment manager"); let environment = manager.get_environment("stdio").expect("stdio environment"); + let selected_root = SelectedCapabilityRoot { + id: "demo@1".to_string(), + location: CapabilityRootLocation::Environment { + environment_id: "stdio".to_string(), + path: PathUri::parse("file:///plugins/demo").expect("plugin URI"), + }, + }; assert!(!environment.startup_finished()); + assert!( + manager + .resolve_selected_capability_roots(std::slice::from_ref(&selected_root)) + .await + .is_empty() + ); + timeout(Duration::from_secs(5), async { + while !environment.startup_finished() { + tokio::task::yield_now().await; + } + }) + .await + .expect("selected root should start the lazy stdio environment"); assert!(environment.wait_until_ready().await.is_err()); - assert!(environment.startup_finished()); } #[tokio::test] diff --git a/codex-rs/exec-server/src/resolved_capability.rs b/codex-rs/exec-server/src/resolved_capability.rs index 5e72b23c5a..a40a4d02c5 100644 --- a/codex-rs/exec-server/src/resolved_capability.rs +++ b/codex-rs/exec-server/src/resolved_capability.rs @@ -1,9 +1,12 @@ +use std::collections::HashMap; use std::fmt; use std::sync::Arc; +use codex_protocol::capabilities::CapabilityRootLocation; use codex_protocol::capabilities::SelectedCapabilityRoot; use crate::Environment; +use crate::EnvironmentManager; use crate::ExecutorFileSystem; /// A selected capability root pinned to the exact environment instance that owns it. @@ -17,7 +20,7 @@ pub struct ResolvedSelectedCapabilityRoot { } impl ResolvedSelectedCapabilityRoot { - pub fn new(selected_root: SelectedCapabilityRoot, environment: Arc) -> Self { + fn new(selected_root: SelectedCapabilityRoot, environment: Arc) -> Self { Self { selected_root, environment, @@ -37,6 +40,74 @@ impl ResolvedSelectedCapabilityRoot { } } +impl EnvironmentManager { + /// Binds selected roots to the environment instances in one registry snapshot. + /// + /// The environments may still be starting. Consumers that access their files use the normal + /// environment startup path. Model steps should use [`Self::resolve_selected_capability_roots`] + /// when they need a non-blocking view containing only ready environments. + pub fn bind_selected_capability_roots( + &self, + selected_roots: &[SelectedCapabilityRoot], + ) -> Vec { + let environments = self + .environments + .read() + .unwrap_or_else(std::sync::PoisonError::into_inner); + selected_roots + .iter() + .filter_map(|selected_root| { + let CapabilityRootLocation::Environment { environment_id, .. } = + &selected_root.location; + environments.get(environment_id).map(|environment| { + ResolvedSelectedCapabilityRoot::new( + selected_root.clone(), + Arc::clone(environment), + ) + }) + }) + .collect() + } + + /// Binds selected roots to the ready environment instances currently registered for them. + /// + /// The registry and readiness are each captured once per environment ID, so every root owned + /// by one environment uses the same executor instance and readiness result. Missing, starting, + /// or failed environments are omitted. A lazy environment is started for a later step. + pub async fn resolve_selected_capability_roots( + &self, + selected_roots: &[SelectedCapabilityRoot], + ) -> Vec { + let candidates = self.bind_selected_capability_roots(selected_roots); + + let mut readiness = HashMap::new(); + for candidate in &candidates { + let CapabilityRootLocation::Environment { environment_id, .. } = + &candidate.selected_root().location; + if readiness.contains_key(environment_id) { + continue; + } + let environment = candidate.environment(); + let ready = if environment.startup_finished() { + environment.wait_until_ready().await.is_ok() + } else { + Environment::start_connecting_for_use(environment); + false + }; + readiness.insert(environment_id.clone(), ready); + } + + candidates + .into_iter() + .filter(|candidate| { + let CapabilityRootLocation::Environment { environment_id, .. } = + &candidate.selected_root().location; + readiness.get(environment_id).copied().unwrap_or(false) + }) + .collect() + } +} + impl fmt::Debug for ResolvedSelectedCapabilityRoot { fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { formatter diff --git a/codex-rs/exec-server/tests/selected_capability_roots.rs b/codex-rs/exec-server/tests/selected_capability_roots.rs new file mode 100644 index 0000000000..347f58a6f5 --- /dev/null +++ b/codex-rs/exec-server/tests/selected_capability_roots.rs @@ -0,0 +1,70 @@ +#![cfg(unix)] + +mod common; + +use std::sync::Arc; + +use codex_exec_server::EnvironmentManager; +use codex_protocol::capabilities::CapabilityRootLocation; +use codex_protocol::capabilities::SelectedCapabilityRoot; +use codex_utils_path_uri::PathUri; +use common::exec_server::exec_server; +use pretty_assertions::assert_eq; + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn selected_capability_roots_follow_same_id_environment_replacement() -> anyhow::Result<()> { + let mut executor_a = exec_server().await?; + let mut executor_b = exec_server().await?; + let manager = EnvironmentManager::without_environments(); + let selected_root = SelectedCapabilityRoot { + id: "demo@1".to_string(), + location: CapabilityRootLocation::Environment { + environment_id: "tools".to_string(), + path: PathUri::parse("file:///plugins/demo")?, + }, + }; + + manager.upsert_environment( + "tools".to_string(), + executor_a.websocket_url().to_string(), + /*connect_timeout*/ None, + )?; + let environment_a = manager + .get_environment("tools") + .expect("executor A should be registered"); + environment_a.wait_until_ready().await?; + let resolved_a = manager + .resolve_selected_capability_roots(std::slice::from_ref(&selected_root)) + .await; + let [resolved_a] = resolved_a.as_slice() else { + anyhow::bail!("selected root should resolve through executor A"); + }; + + manager.upsert_environment( + "tools".to_string(), + executor_b.websocket_url().to_string(), + /*connect_timeout*/ None, + )?; + let environment_b = manager + .get_environment("tools") + .expect("executor B should be registered"); + environment_b.wait_until_ready().await?; + let resolved_b = manager + .resolve_selected_capability_roots(std::slice::from_ref(&selected_root)) + .await; + let [resolved_b] = resolved_b.as_slice() else { + anyhow::bail!("selected root should resolve through executor B"); + }; + + assert_eq!(resolved_b.selected_root(), &selected_root); + assert!(Arc::ptr_eq(resolved_a.environment(), &environment_a)); + assert!(Arc::ptr_eq(resolved_b.environment(), &environment_b)); + assert!(!Arc::ptr_eq( + resolved_a.environment(), + resolved_b.environment() + )); + + executor_a.shutdown().await?; + executor_b.shutdown().await?; + Ok(()) +}