From 5dc990ebbcc6b921acb92f336fcbdb3a4fd513ad Mon Sep 17 00:00:00 2001 From: jif-oai Date: Thu, 25 Jun 2026 18:18:26 +0100 Subject: [PATCH] Keep capability handles step-consistent --- codex-rs/core/src/environment_selection.rs | 12 +++++++--- codex-rs/core/src/session/mod.rs | 2 +- .../exec-server/src/resolved_capability.rs | 20 ++++++++-------- .../tests/selected_capability_roots.rs | 24 +++++++++++++------ 4 files changed, 37 insertions(+), 21 deletions(-) diff --git a/codex-rs/core/src/environment_selection.rs b/codex-rs/core/src/environment_selection.rs index 59ee523c8a..ef6a3ec5ea 100644 --- a/codex-rs/core/src/environment_selection.rs +++ b/codex-rs/core/src/environment_selection.rs @@ -228,14 +228,20 @@ pub(crate) struct TurnEnvironmentSnapshot { } impl TurnEnvironmentSnapshot { - pub(crate) fn captured_environment_availability(&self) -> HashMap { + /// Maps each captured environment to its exact ready handle, or `None` when it was starting. + pub(crate) fn captured_environments(&self) -> HashMap>> { self.turn_environments .iter() - .map(|environment| (environment.environment_id.clone(), true)) + .map(|environment| { + ( + environment.environment_id.clone(), + Some(Arc::clone(&environment.environment)), + ) + }) .chain( self.starting .iter() - .map(|environment| (environment.selection.environment_id.clone(), false)), + .map(|environment| (environment.selection.environment_id.clone(), None)), ) .collect() } diff --git a/codex-rs/core/src/session/mod.rs b/codex-rs/core/src/session/mod.rs index 9d09e78acf..94bef646aa 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_environment_availability(), + &environments.captured_environments(), ) .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 93cceaa630..f590133eec 100644 --- a/codex-rs/exec-server/src/resolved_capability.rs +++ b/codex-rs/exec-server/src/resolved_capability.rs @@ -44,14 +44,14 @@ impl ResolvedSelectedCapabilityRoot { impl EnvironmentManager { /// Resolves selected roots whose stable environments are ready for the current model 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 + /// Environment identity comes from the selected root's stable environment ID. A ready + /// environment captured for the step carries its exact process-local handle so readiness and + /// execution cannot come from different registry snapshots. 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_availability: &HashMap, + captured_environments: &HashMap>>, ) -> Vec { let candidates = { let environments = self @@ -63,12 +63,12 @@ impl EnvironmentManager { .filter_map(|selected_root| { let CapabilityRootLocation::Environment { environment_id, .. } = &selected_root.location; - 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)?); + 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), + }; 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 d9b7e0d5d6..639d652ca8 100644 --- a/codex-rs/exec-server/tests/selected_capability_roots.rs +++ b/codex-rs/exec-server/tests/selected_capability_roots.rs @@ -3,6 +3,7 @@ mod common; use std::collections::HashMap; +use std::sync::Arc; use codex_exec_server::EnvironmentManager; use codex_protocol::capabilities::CapabilityRootLocation; @@ -12,7 +13,7 @@ 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_captured_availability() -> anyhow::Result<()> { +async fn selected_capability_roots_use_captured_handle_after_replacement() -> anyhow::Result<()> { let mut executor = exec_server().await?; let manager = EnvironmentManager::without_environments(); let selected_root = SelectedCapabilityRoot { @@ -28,23 +29,32 @@ async fn selected_capability_roots_follow_captured_availability() -> anyhow::Res executor.websocket_url().to_string(), /*connect_timeout*/ None, )?; - let environment = manager + let environment_a = manager .get_environment("tools") - .expect("executor should be registered"); - environment.wait_until_ready().await?; + .expect("executor A should be registered"); + environment_a.wait_until_ready().await?; let unavailable = manager .resolve_selected_capability_roots( std::slice::from_ref(&selected_root), - &HashMap::from([("tools".to_string(), false)]), + &HashMap::from([("tools".to_string(), None)]), ) .await; assert!(unavailable.is_empty()); + let captured_environments = + HashMap::from([("tools".to_string(), Some(Arc::clone(&environment_a)))]); + // Replace only the process-local handle; the stable environment ID and executor stay the same. + manager.upsert_environment( + "tools".to_string(), + executor.websocket_url().to_string(), + /*connect_timeout*/ None, + )?; + let available = manager .resolve_selected_capability_roots( std::slice::from_ref(&selected_root), - &HashMap::from([("tools".to_string(), true)]), + &captured_environments, ) .await; let [resolved] = available.as_slice() else { @@ -52,7 +62,7 @@ async fn selected_capability_roots_follow_captured_availability() -> anyhow::Res }; assert_eq!(resolved.selected_root(), &selected_root); - assert!(std::sync::Arc::ptr_eq(resolved.environment(), &environment)); + assert!(Arc::ptr_eq(resolved.environment(), &environment_a)); executor.shutdown().await?; Ok(())