Prewarm executor capability discovery during remote startup (#62674)

## Why

Step-time discovery can make the first step that needs a remote executor
inventory wait for a discovery round trip. Starting discovery during
environment setup lets deferred turns begin sampling while the inventory
is being fetched.

## What changed

When executor capability discovery V2 is enabled, prewarm the thread's
discovery cache for supported remote executors before marking their
environments ready. Reserve cache capacity for all selected environments
so the first step can reuse their inventories.

Give each prewarm up to five seconds and let environment startup proceed
after discovery errors or a timeout. Step-time discovery reuses cached
successes and permanent errors and can retry transient failures and
timed-out loads.

## Testing

Add integration coverage for deferred readiness and cache reuse after
success, permanent errors, transient failures, and timeouts. Also cover
cache retention across multiple environments and session startup when
discovery receives no reply.

GitOrigin-RevId: d07fc78bd42953a4caf9e4b39528db8a254eb2b8
This commit is contained in:
Anthony Tafoya
2026-11-20 05:17:34 +00:00
committed by copyberry
parent a0a30c40e8
commit ffa05c8bfb
12 changed files with 739 additions and 56 deletions
@@ -1,8 +1,10 @@
//! Core owns a thread discovery cache shared across turns. Explicit refresh dirties every entry.
//! In-flight readers may keep their inventories; invalidation applies to the next lookup.
//! Prewarming bounds discovery before readiness without publishing MCP state or failing startup.
use super::Session;
use super::TurnContext;
use super::turn_context::TurnEnvironment;
use crate::McpRefreshReason;
use crate::config::Config;
use crate::environment_selection::TurnEnvironmentSnapshot;
@@ -13,6 +15,9 @@ use codex_exec_server::ExecutorCapabilityDiscoveryV2Snapshot;
use codex_exec_server::supports_capability_discovery_v2;
use codex_features::Feature;
use std::sync::Arc;
use std::time::Duration;
const DISCOVERY_PREWARM_TIMEOUT: Duration = Duration::from_secs(5);
#[derive(Default)]
pub(crate) struct ExecutorDiscovery {
@@ -20,10 +25,52 @@ pub(crate) struct ExecutorDiscovery {
}
impl ExecutorDiscovery {
pub(crate) fn ensure_capacity(&self, minimum: usize) {
self.cache.ensure_capacity(minimum);
}
#[expect(dead_code, reason = "Reserved for future environment-scoped refresh")]
pub(crate) fn invalidate_environment(&self, environment: &Arc<Environment>) {
self.cache.invalidate_environment(environment);
}
pub(crate) async fn prewarm(&self, selected: &TurnEnvironment) {
let prewarm = async {
let Ok(info) = selected.environment().info().await else {
tracing::warn!(
environment_id = selected.selection.environment_id,
"could not read executor discovery V2 support during prewarm"
);
return;
};
if !supports_capability_discovery_v2(&info) {
return;
}
// Discovery logs failures and retains them according to its existing retry policy.
let _ = self
.cache
.discover(
&selected.selection.environment_id,
selected.environment(),
DiscoverV2CapabilitiesRequest {
cwd: selected.cwd().clone(),
sandbox: Some(selected.sandbox_context(/*additional_permissions*/ None)),
},
)
.await;
};
if tokio::time::timeout(DISCOVERY_PREWARM_TIMEOUT, prewarm)
.await
.is_err()
{
// Dropping the load releases the cache's OnceCell for a later step to retry.
tracing::warn!(
environment_id = selected.selection.environment_id,
timeout_ms = DISCOVERY_PREWARM_TIMEOUT.as_millis(),
"executor discovery V2 prewarm timed out; continuing environment startup"
);
}
}
}
impl Session {
+1
View File
@@ -261,6 +261,7 @@ pub(crate) mod context_window;
mod daemon_recovery;
mod environment;
mod executor_discovery;
pub(crate) use executor_discovery::ExecutorDiscovery;
mod extension_interruption;
pub(crate) mod extension_metrics;
mod guardian_checkpoint;
+3
View File
@@ -1548,6 +1548,9 @@ impl Session {
shell_snapshot,
inherited_environments.clone(),
config.features.enabled(Feature::DeferredExecutor),
config.features.enabled(Feature::ExecutorCapabilityDiscoveryV2).then(|| {
thread_extension_data.get_or_init(super::ExecutorDiscovery::default)
}),
));
turn_environments.update_configured_selections(environment_selections);
let configured_environments = turn_environments.configured_snapshot_now();
+3
View File
@@ -5311,6 +5311,7 @@ async fn resolved_environments_for_configuration(
ShellSnapshot::disabled(),
TurnEnvironmentSnapshot::default(),
/*non_blocking_snapshots*/ false,
/*executor_discovery*/ None,
);
turn_environments.update_selections(environment_selections);
(environment_manager, turn_environments.snapshot().await)
@@ -6771,6 +6772,7 @@ pub(crate) async fn make_session_and_context() -> (Session, TurnContext) {
ShellSnapshot::disabled(),
resolved_environments,
/*non_blocking_snapshots*/ false,
/*executor_discovery*/ None,
));
turn_environments.activate_configured(tx_event.clone());
let environment = Arc::clone(
@@ -9131,6 +9133,7 @@ where
ShellSnapshot::disabled(),
resolved_turn_environments.clone(),
/*non_blocking_snapshots*/ false,
/*executor_discovery*/ None,
));
turn_environments.activate_configured(tx_event.clone());
let environment = Arc::clone(