From 21993f14f521bb89caf885bbd9222cdf250364d8 Mon Sep 17 00:00:00 2001 From: jif-oai Date: Thu, 25 Jun 2026 17:41:18 +0100 Subject: [PATCH] Treat environment IDs as stable capability identities --- codex-rs/core/src/environment_selection.rs | 11 +--- codex-rs/core/src/session/mod.rs | 2 +- .../exec-server/src/resolved_capability.rs | 30 ++++----- .../tests/selected_capability_roots.rs | 62 ++++++++----------- 4 files changed, 45 insertions(+), 60 deletions(-) diff --git a/codex-rs/core/src/environment_selection.rs b/codex-rs/core/src/environment_selection.rs index 6d8de052a7..59ee523c8a 100644 --- a/codex-rs/core/src/environment_selection.rs +++ b/codex-rs/core/src/environment_selection.rs @@ -228,19 +228,14 @@ pub(crate) struct TurnEnvironmentSnapshot { } impl TurnEnvironmentSnapshot { - pub(crate) fn captured_environments(&self) -> HashMap>> { + pub(crate) fn captured_environment_availability(&self) -> HashMap { self.turn_environments .iter() - .map(|environment| { - ( - environment.environment_id.clone(), - Some(Arc::clone(&environment.environment)), - ) - }) + .map(|environment| (environment.environment_id.clone(), true)) .chain( self.starting .iter() - .map(|environment| (environment.selection.environment_id.clone(), None)), + .map(|environment| (environment.selection.environment_id.clone(), false)), ) .collect() } diff --git a/codex-rs/core/src/session/mod.rs b/codex-rs/core/src/session/mod.rs index 94bef646aa..9d09e78acf 100644 --- a/codex-rs/core/src/session/mod.rs +++ b/codex-rs/core/src/session/mod.rs @@ -2834,7 +2834,7 @@ impl Session { .environment_manager() .resolve_selected_capability_roots( &self.services.selected_capability_roots, - &environments.captured_environments(), + &environments.captured_environment_availability(), ) .await; Arc::new(StepContext::new( diff --git a/codex-rs/exec-server/src/resolved_capability.rs b/codex-rs/exec-server/src/resolved_capability.rs index 666657e264..93cceaa630 100644 --- a/codex-rs/exec-server/src/resolved_capability.rs +++ b/codex-rs/exec-server/src/resolved_capability.rs @@ -9,10 +9,11 @@ use crate::Environment; use crate::EnvironmentManager; use crate::ExecutorFileSystem; -/// A selected capability root pinned to the exact environment instance that owns it. +/// A selected capability root paired with its currently ready environment handle. /// -/// This value is process-local and must not be persisted. Cloning it keeps the same -/// [`Environment`] alive so every consumer of one model step uses the same executor. +/// Environment IDs have stable identity and contents. This process-local value must not be +/// persisted: it only keeps the current connection handle alive while one model step uses the +/// stable environment. #[derive(Clone)] pub struct ResolvedSelectedCapabilityRoot { selected_root: SelectedCapabilityRoot, @@ -41,15 +42,16 @@ impl ResolvedSelectedCapabilityRoot { } impl EnvironmentManager { - /// Binds selected roots to the ready environment instances currently registered for them. + /// Resolves selected roots whose stable environments are ready for the current model step. /// - /// 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. + /// Environment identity comes from the selected root's stable environment ID. Captured + /// availability keeps this projection consistent with the step's environment World State; + /// it is not an executor-identity or content cache key. 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], - captured_environments: &HashMap>>, + captured_availability: &HashMap, ) -> Vec { let candidates = { let environments = self @@ -61,12 +63,12 @@ impl EnvironmentManager { .filter_map(|selected_root| { let CapabilityRootLocation::Environment { environment_id, .. } = &selected_root.location; - let (environment, already_ready) = - match captured_environments.get(environment_id) { - Some(Some(environment)) => (Arc::clone(environment), true), - Some(None) => return None, - None => (Arc::clone(environments.get(environment_id)?), false), - }; + let already_ready = match captured_availability.get(environment_id) { + Some(true) => true, + Some(false) => return None, + None => false, + }; + let environment = Arc::clone(environments.get(environment_id)?); Some(( ResolvedSelectedCapabilityRoot::new(selected_root.clone(), environment), already_ready, diff --git a/codex-rs/exec-server/tests/selected_capability_roots.rs b/codex-rs/exec-server/tests/selected_capability_roots.rs index e20fc5b68a..d9b7e0d5d6 100644 --- a/codex-rs/exec-server/tests/selected_capability_roots.rs +++ b/codex-rs/exec-server/tests/selected_capability_roots.rs @@ -3,7 +3,6 @@ mod common; use std::collections::HashMap; -use std::sync::Arc; use codex_exec_server::EnvironmentManager; use codex_protocol::capabilities::CapabilityRootLocation; @@ -13,9 +12,8 @@ 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?; +async fn selected_capability_roots_follow_captured_availability() -> anyhow::Result<()> { + let mut executor = exec_server().await?; let manager = EnvironmentManager::without_environments(); let selected_root = SelectedCapabilityRoot { id: "demo@1".to_string(), @@ -27,45 +25,35 @@ async fn selected_capability_roots_follow_same_id_environment_replacement() -> a manager.upsert_environment( "tools".to_string(), - executor_a.websocket_url().to_string(), + executor.websocket_url().to_string(), /*connect_timeout*/ None, )?; - let environment_a = manager + let environment = 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), &HashMap::new()) + .expect("executor should be registered"); + environment.wait_until_ready().await?; + + let unavailable = manager + .resolve_selected_capability_roots( + std::slice::from_ref(&selected_root), + &HashMap::from([("tools".to_string(), false)]), + ) .await; - let [resolved_a] = resolved_a.as_slice() else { - anyhow::bail!("selected root should resolve through executor A"); + assert!(unavailable.is_empty()); + + let available = manager + .resolve_selected_capability_roots( + std::slice::from_ref(&selected_root), + &HashMap::from([("tools".to_string(), true)]), + ) + .await; + let [resolved] = available.as_slice() else { + anyhow::bail!("selected root should resolve through its stable environment"); }; - 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), &HashMap::new()) - .await; - let [resolved_b] = resolved_b.as_slice() else { - anyhow::bail!("selected root should resolve through executor B"); - }; + assert_eq!(resolved.selected_root(), &selected_root); + assert!(std::sync::Arc::ptr_eq(resolved.environment(), &environment)); - 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?; + executor.shutdown().await?; Ok(()) }