Keep capability handles step-consistent

This commit is contained in:
jif-oai
2026-06-25 18:18:26 +01:00
parent 21993f14f5
commit 5dc990ebbc
4 changed files with 37 additions and 21 deletions
+9 -3
View File
@@ -228,14 +228,20 @@ pub(crate) struct TurnEnvironmentSnapshot {
}
impl TurnEnvironmentSnapshot {
pub(crate) fn captured_environment_availability(&self) -> HashMap<String, bool> {
/// Maps each captured environment to its exact ready handle, or `None` when it was starting.
pub(crate) fn captured_environments(&self) -> HashMap<String, Option<Arc<Environment>>> {
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()
}
+1 -1
View File
@@ -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(
+10 -10
View File
@@ -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<String, bool>,
captured_environments: &HashMap<String, Option<Arc<Environment>>>,
) -> Vec<ResolvedSelectedCapabilityRoot> {
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,
@@ -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(())