Treat environment IDs as stable capability identities

This commit is contained in:
jif-oai
2026-06-25 17:41:18 +01:00
parent 5fdac29fc6
commit 21993f14f5
4 changed files with 45 additions and 60 deletions
+3 -8
View File
@@ -228,19 +228,14 @@ pub(crate) struct TurnEnvironmentSnapshot {
}
impl TurnEnvironmentSnapshot {
pub(crate) fn captured_environments(&self) -> HashMap<String, Option<Arc<Environment>>> {
pub(crate) fn captured_environment_availability(&self) -> HashMap<String, bool> {
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()
}
+1 -1
View File
@@ -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(
+16 -14
View File
@@ -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<String, Option<Arc<Environment>>>,
captured_availability: &HashMap<String, bool>,
) -> Vec<ResolvedSelectedCapabilityRoot> {
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,
@@ -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(())
}