mirror of
https://github.com/openai/codex.git
synced 2026-11-20 22:40:37 +00:00
Reuse executor Discovery V2 inventories across turns (#62640)
## Why Starting a turn discarded the discovery cache and forced MCP to rebuild from a fresh inventory even when the executor context was unchanged. An MCP refresh could also discover a different inventory from the one the step had already captured. ## What changed Keep a per-thread cache keyed by environment, executor session, working directory, and sandbox. Explicit invalidation reloads discovery on the next lookup and lets in-flight readers finish with their captured results. Use the step's captured inventory for MCP reconciliation and bind it with the current MCP policy. Apply the binding's plugin policy when selecting skills and hooks. ## Testing Cover cache reuse across turns, lazy invalidation during in-flight discovery, and MCP refresh with a captured inventory after a server has been revoked. GitOrigin-RevId: 8d4087dffdb3d6feef2456918a08ae7724cf18e4
This commit is contained in:
committed by
copyberry
parent
9224da4ce1
commit
e720e6c80b
@@ -32,6 +32,7 @@ use codex_config::types::OAuthCredentialsStoreMode;
|
||||
use codex_connectors::ConnectorRuntimeManager;
|
||||
use codex_connectors::ConnectorSnapshot;
|
||||
use codex_connectors::connector_runtime_context_key;
|
||||
use codex_exec_server::ExecutorCapabilityDiscoveryV2Snapshot;
|
||||
use codex_exec_server::FileSystemSandboxContext;
|
||||
use codex_login::CodexAuth;
|
||||
use codex_model_provider::CHATGPT_CODEX_BASE_URL;
|
||||
@@ -226,6 +227,8 @@ pub struct McpConfig {
|
||||
/// Plugin declarations used to attribute connector tools to plugin display names.
|
||||
/// MCP registrations retain their own package attribution in the catalog.
|
||||
pub connector_snapshot: ConnectorSnapshot,
|
||||
/// Executor inventory used to project this runtime, retained with its binding.
|
||||
pub executor_discovery_v2: Option<Arc<[ExecutorCapabilityDiscoveryV2Snapshot]>>,
|
||||
}
|
||||
|
||||
/// Default amount of time a tool catalog capture waits for optional MCP servers.
|
||||
|
||||
@@ -254,6 +254,7 @@ pub(crate) fn test_mcp_config(codex_home: PathBuf) -> McpConfig {
|
||||
host_owned_apps_protocol_mode: McpProtocolMode::Legacy,
|
||||
mcp_server_catalog: ResolvedMcpCatalog::default(),
|
||||
connector_snapshot: codex_connectors::ConnectorSnapshot::default(),
|
||||
executor_discovery_v2: None,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1602,6 +1602,13 @@ impl CodexThread {
|
||||
.await
|
||||
}
|
||||
|
||||
/// Invalidates this thread's executor DiscoveryV2 inventory. The next discovery
|
||||
/// consumer reloads it; unchanged turns and other reads share the cached inventory.
|
||||
/// Environment/session, cwd, and sandbox changes also trigger discovery independently.
|
||||
pub fn invalidate_executor_discovery(&self) {
|
||||
self.session.invalidate_executor_discovery();
|
||||
}
|
||||
|
||||
/// Refreshes this thread's Apps tools before returning their runtime state.
|
||||
pub async fn refresh_codex_apps_tools(
|
||||
&self,
|
||||
|
||||
@@ -2079,6 +2079,7 @@ impl Config {
|
||||
}
|
||||
|
||||
McpConfig {
|
||||
executor_discovery_v2: None,
|
||||
client: McpClientConfig {
|
||||
chatgpt_base_url: self.chatgpt_base_url.clone(),
|
||||
requires_read_only_mcp_tools: false,
|
||||
|
||||
@@ -242,13 +242,16 @@ impl McpManager {
|
||||
if let McpEnvironmentScope::Selected(environments) = &identity.environments {
|
||||
context = context.with_selected_environments(environments);
|
||||
}
|
||||
self.runtime_config_with_context(
|
||||
context,
|
||||
Some(identity.originator),
|
||||
identity.environments,
|
||||
identity.disabled_plugin_ids,
|
||||
)
|
||||
.await
|
||||
let mut projection = self
|
||||
.runtime_config_with_context(
|
||||
context,
|
||||
Some(identity.originator),
|
||||
identity.environments,
|
||||
identity.disabled_plugin_ids,
|
||||
)
|
||||
.await;
|
||||
projection.config.executor_discovery_v2 = executor_capability_discovery_v2.map(Arc::from);
|
||||
projection
|
||||
}
|
||||
|
||||
/// Resolves only plugins from this step's ready roots, independent of shared MCP refresh.
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
//! 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.
|
||||
|
||||
use super::Session;
|
||||
use super::TurnContext;
|
||||
use crate::McpRefreshReason;
|
||||
use crate::config::Config;
|
||||
use crate::environment_selection::TurnEnvironmentSnapshot;
|
||||
use codex_exec_server::DiscoverV2CapabilitiesRequest;
|
||||
use codex_exec_server::Environment;
|
||||
use codex_exec_server::ExecutorCapabilityDiscoveryV2Cache;
|
||||
use codex_exec_server::ExecutorCapabilityDiscoveryV2Snapshot;
|
||||
use codex_exec_server::supports_capability_discovery_v2;
|
||||
use codex_features::Feature;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[derive(Default)]
|
||||
pub(crate) struct ExecutorDiscovery {
|
||||
cache: ExecutorCapabilityDiscoveryV2Cache,
|
||||
}
|
||||
|
||||
impl ExecutorDiscovery {
|
||||
#[expect(dead_code, reason = "Reserved for future environment-scoped refresh")]
|
||||
pub(crate) fn invalidate_environment(&self, environment: &Arc<Environment>) {
|
||||
self.cache.invalidate_environment(environment);
|
||||
}
|
||||
}
|
||||
|
||||
impl Session {
|
||||
pub(crate) fn invalidate_executor_discovery(&self) {
|
||||
self.services
|
||||
.thread_extension_data
|
||||
.get_or_init(ExecutorDiscovery::default)
|
||||
.cache
|
||||
.invalidate();
|
||||
self.mark_mcp_runtime_dirty(McpRefreshReason::ExplicitInvalidation);
|
||||
}
|
||||
|
||||
/// Acquires V2 inventories and reports changes without scheduling an MCP refresh.
|
||||
pub(super) async fn executor_capability_discovery_for_step_v2(
|
||||
&self,
|
||||
config: &Config,
|
||||
environments: &TurnEnvironmentSnapshot,
|
||||
skill_turn: Option<&TurnContext>,
|
||||
) -> (Option<Vec<ExecutorCapabilityDiscoveryV2Snapshot>>, bool) {
|
||||
if !config
|
||||
.features
|
||||
.enabled(Feature::ExecutorCapabilityDiscoveryV2)
|
||||
{
|
||||
return (None, false);
|
||||
}
|
||||
let discovery = self
|
||||
.services
|
||||
.thread_extension_data
|
||||
.get_or_init(ExecutorDiscovery::default);
|
||||
let cache = &discovery.cache;
|
||||
cache.ensure_capacity(
|
||||
environments
|
||||
.turn_environments()
|
||||
.filter(|selected| selected.environment().is_remote())
|
||||
.count(),
|
||||
);
|
||||
let mut snapshots = Vec::new();
|
||||
let mut skipped_incompatible_executor = false;
|
||||
for selected in environments.turn_environments() {
|
||||
if !selected.environment().is_remote() {
|
||||
continue;
|
||||
}
|
||||
let environment_id = &selected.selection.environment_id;
|
||||
let info = match selected.environment().info().await {
|
||||
Ok(info) => info,
|
||||
Err(_) => {
|
||||
tracing::warn!(
|
||||
environment_id,
|
||||
"could not read executor discovery V2 support"
|
||||
);
|
||||
continue;
|
||||
}
|
||||
};
|
||||
if !supports_capability_discovery_v2(&info) {
|
||||
skipped_incompatible_executor = true;
|
||||
continue;
|
||||
}
|
||||
let request = DiscoverV2CapabilitiesRequest {
|
||||
cwd: selected.cwd().clone(),
|
||||
sandbox: Some(self.discovery_sandbox(selected, skill_turn)),
|
||||
};
|
||||
let result = cache
|
||||
.discover(environment_id, selected.environment(), request)
|
||||
.await;
|
||||
snapshots.push(ExecutorCapabilityDiscoveryV2Snapshot {
|
||||
environment_id: environment_id.clone(),
|
||||
result: result.map_err(|_| "executor discovery V2 failed".to_string()),
|
||||
});
|
||||
}
|
||||
let incompatible = snapshots.is_empty() && skipped_incompatible_executor;
|
||||
if skill_turn.is_some() {
|
||||
// Only session discovery updates the inventory used to invalidate MCP.
|
||||
return ((!incompatible).then_some(snapshots), false);
|
||||
}
|
||||
if incompatible {
|
||||
let changed = self
|
||||
.services
|
||||
.thread_extension_data
|
||||
.remove::<Vec<ExecutorCapabilityDiscoveryV2Snapshot>>()
|
||||
.is_some();
|
||||
return (None, changed);
|
||||
}
|
||||
let previous = self
|
||||
.services
|
||||
.thread_extension_data
|
||||
.insert(snapshots.clone());
|
||||
let inventory_changed = previous.as_ref().map_or(!snapshots.is_empty(), |previous| {
|
||||
previous.len() != snapshots.len()
|
||||
|| previous
|
||||
.iter()
|
||||
.zip(&snapshots)
|
||||
.any(|(previous, current)| !previous.same_discovery(current))
|
||||
});
|
||||
let inventory_changed = cache.take_recovered_discovery() || inventory_changed;
|
||||
(Some(snapshots), inventory_changed)
|
||||
}
|
||||
}
|
||||
@@ -5,14 +5,11 @@ use crate::environment_selection::combine_selected_capability_roots;
|
||||
use crate::mcp_tool_call::McpToolApprovalContext;
|
||||
use crate::mcp_tool_call::capture_environment_permissions;
|
||||
use anyhow::Context;
|
||||
use codex_exec_server::DiscoverV2CapabilitiesRequest;
|
||||
use codex_exec_server::ExecutorCapabilityDiscoveryCache;
|
||||
use codex_exec_server::ExecutorCapabilityDiscoverySnapshot;
|
||||
use codex_exec_server::ExecutorCapabilityDiscoveryV2Cache;
|
||||
use codex_exec_server::ExecutorCapabilityDiscoveryV2Snapshot;
|
||||
use codex_exec_server::MAX_SELECTED_CAPABILITY_ROOTS;
|
||||
use codex_exec_server::ResolvedSelectedCapabilityRoot;
|
||||
use codex_exec_server::supports_capability_discovery_v2;
|
||||
use codex_guardian_reviewer::GuardianElicitationReview;
|
||||
use codex_guardian_reviewer::guardian_elicitation_review_request;
|
||||
use codex_guardian_reviewer::mcp_elicitation_decline_without_message;
|
||||
@@ -225,17 +222,28 @@ impl Session {
|
||||
/// Publishes changed MCP state, waiting for any refresh already in progress.
|
||||
#[tracing::instrument(name = "mcp.runtime.refresh_if_dirty", skip_all)]
|
||||
pub(crate) async fn refresh_mcp_if_dirty(self: &Arc<Self>) {
|
||||
self.refresh_mcp_if_dirty_with_discovery_v2(/*captured_executor_discovery_v2*/ None)
|
||||
.await;
|
||||
}
|
||||
|
||||
async fn refresh_mcp_if_dirty_with_discovery_v2(
|
||||
self: &Arc<Self>,
|
||||
captured_executor_discovery_v2: Option<&[ExecutorCapabilityDiscoveryV2Snapshot]>,
|
||||
) {
|
||||
let Ok(_refresh) = self.mcp_refresh.acquire().await else {
|
||||
error!("MCP runtime refresh semaphore closed");
|
||||
return;
|
||||
};
|
||||
self.reconcile_mcp_runtime().await;
|
||||
self.reconcile_mcp_runtime(captured_executor_discovery_v2)
|
||||
.await;
|
||||
}
|
||||
|
||||
/// Reconcile pending state under the session permit.
|
||||
async fn reconcile_mcp_runtime(self: &Arc<Self>) {
|
||||
async fn reconcile_mcp_runtime(
|
||||
self: &Arc<Self>,
|
||||
captured_executor_discovery_v2: Option<&[ExecutorCapabilityDiscoveryV2Snapshot]>,
|
||||
) {
|
||||
loop {
|
||||
// Compare and rebuild from current environments, not choices saved for a future turn.
|
||||
let environments = self.services.turn_environments.snapshot().await;
|
||||
let environment_selections = environments.all_selections();
|
||||
if !self.services.mcp_runtime.current_environments_match(
|
||||
@@ -253,12 +261,28 @@ impl Session {
|
||||
self.mark_mcp_runtime_dirty(McpRefreshReason::RuntimeAuthChanged);
|
||||
}
|
||||
|
||||
if let Some(captured) = captured_executor_discovery_v2 {
|
||||
let current = self.services.mcp_runtime.current_config();
|
||||
let published = current
|
||||
.as_ref()
|
||||
.and_then(|config| config.executor_discovery_v2.as_deref());
|
||||
if published.is_none_or(|published| {
|
||||
published.len() != captured.len()
|
||||
|| published
|
||||
.iter()
|
||||
.zip(captured)
|
||||
.any(|(a, b)| !a.same_discovery(b))
|
||||
}) {
|
||||
self.mark_mcp_runtime_dirty(
|
||||
McpRefreshReason::ExecutorCapabilityInventoryChanged,
|
||||
);
|
||||
}
|
||||
}
|
||||
let Some(mut refresh_invalidation) = self.mcp_refresh.claim(self.thread_id) else {
|
||||
return;
|
||||
};
|
||||
let auth = self.services.auth_manager.auth().await;
|
||||
// Capture after claiming the refresh so a new invalidation cannot be consumed
|
||||
// with the environment snapshot taken before its permissions changed.
|
||||
// Keep current MCP authority even when reusing the step's executor inventory.
|
||||
let environments = self.services.turn_environments.snapshot().await;
|
||||
let environment_selections = environments.all_selections();
|
||||
let desired = self.latest_mcp_desired_state(auth, environments).await;
|
||||
@@ -267,19 +291,26 @@ impl Session {
|
||||
.await;
|
||||
let ready_selected_capability_roots =
|
||||
Self::ready_selected_capability_roots(&selected_capability_roots);
|
||||
let (executor_capability_discovery_v2, _) = self
|
||||
.executor_capability_discovery_for_step_v2(
|
||||
&desired.config,
|
||||
&desired.environments,
|
||||
/*skill_turn*/ None,
|
||||
)
|
||||
.await;
|
||||
let discovered_v2;
|
||||
let executor_capability_discovery_v2 = match captured_executor_discovery_v2 {
|
||||
Some(captured) => Some(captured),
|
||||
None => {
|
||||
(discovered_v2, _) = self
|
||||
.executor_capability_discovery_for_step_v2(
|
||||
&desired.config,
|
||||
&desired.environments,
|
||||
/*skill_turn*/ None,
|
||||
)
|
||||
.await;
|
||||
discovered_v2.as_deref()
|
||||
}
|
||||
};
|
||||
let (executor_capability_discovery, _) = self
|
||||
.executor_capability_discovery_for_step(
|
||||
&desired.config,
|
||||
&selected_capability_roots,
|
||||
&desired.environments,
|
||||
executor_capability_discovery_v2.as_deref(),
|
||||
executor_capability_discovery_v2,
|
||||
/*skill_turn*/ None,
|
||||
)
|
||||
.await;
|
||||
@@ -302,7 +333,7 @@ impl Session {
|
||||
},
|
||||
&ready_selected_capability_roots,
|
||||
executor_capability_discovery.as_deref(),
|
||||
executor_capability_discovery_v2.as_deref(),
|
||||
executor_capability_discovery_v2,
|
||||
)
|
||||
.await;
|
||||
self.publish_mcp_runtime(
|
||||
@@ -347,7 +378,8 @@ impl Session {
|
||||
self: &Arc<Self>,
|
||||
permit: SemaphorePermit<'_>,
|
||||
) -> anyhow::Result<Option<codex_mcp::CodexAppsToolSnapshot>> {
|
||||
self.reconcile_mcp_runtime().await;
|
||||
self.reconcile_mcp_runtime(/*captured_executor_discovery_v2*/ None)
|
||||
.await;
|
||||
let refresh = self.services.mcp_runtime.capture_apps_tool_refresh();
|
||||
// An invalidation may arrive after reconciliation but before capture.
|
||||
if self.mcp_refresh.is_pending() {
|
||||
@@ -463,13 +495,14 @@ impl Session {
|
||||
skip_all,
|
||||
fields(turn.id = %turn_context.sub_id)
|
||||
)]
|
||||
pub(crate) async fn mcp_runtime_for_step(
|
||||
pub(super) async fn mcp_runtime_for_step(
|
||||
self: &Arc<Self>,
|
||||
turn_context: &TurnContext,
|
||||
selected_capability_roots: &[ResolvedSelectedCapabilityRoot],
|
||||
captured_executor_discovery_v2: Option<&[ExecutorCapabilityDiscoveryV2Snapshot]>,
|
||||
required_servers: &[String],
|
||||
required_plugins: &HashSet<String>,
|
||||
) -> Arc<codex_mcp::McpBinding> {
|
||||
) -> CodexResult<Arc<codex_mcp::McpBinding>> {
|
||||
let ready_selected_capability_roots =
|
||||
Self::ready_selected_capability_roots(selected_capability_roots);
|
||||
if self
|
||||
@@ -499,7 +532,8 @@ impl Session {
|
||||
{
|
||||
self.mark_mcp_runtime_dirty(McpRefreshReason::OAuthCredentialsRecovered);
|
||||
}
|
||||
self.refresh_mcp_if_dirty().await;
|
||||
self.refresh_mcp_if_dirty_with_discovery_v2(captured_executor_discovery_v2)
|
||||
.await;
|
||||
// Prewarm and other context-only captures must not start cloud discovery.
|
||||
let is_active_regular_turn = {
|
||||
let active_turn = self.active_turn.lock().await;
|
||||
@@ -534,11 +568,19 @@ impl Session {
|
||||
}),
|
||||
)
|
||||
.await;
|
||||
self.refresh_mcp_if_dirty().await;
|
||||
self.refresh_mcp_if_dirty_with_discovery_v2(captured_executor_discovery_v2)
|
||||
.await;
|
||||
}
|
||||
self.wait_for_apps_tool_refresh().await;
|
||||
if let Some(binding) = self
|
||||
.services
|
||||
// Keep the binding and its final executor inventory projection together.
|
||||
let _refresh = self
|
||||
.mcp_refresh
|
||||
.acquire()
|
||||
.await
|
||||
.map_err(|_| CodexErr::TurnAborted)?;
|
||||
self.reconcile_mcp_runtime(captured_executor_discovery_v2)
|
||||
.await;
|
||||
self.services
|
||||
.mcp_runtime
|
||||
.current_binding_with_requirements(
|
||||
required_servers,
|
||||
@@ -546,14 +588,10 @@ impl Session {
|
||||
required_plugins,
|
||||
)
|
||||
.await
|
||||
{
|
||||
return binding;
|
||||
}
|
||||
let config = Arc::new(self.runtime_mcp_config(&turn_context.config).await);
|
||||
Arc::new(codex_mcp::McpBinding::empty(config))
|
||||
.ok_or(CodexErr::TurnAborted)
|
||||
}
|
||||
|
||||
fn discovery_sandbox(
|
||||
pub(super) fn discovery_sandbox(
|
||||
&self,
|
||||
environment: &super::turn_context::TurnEnvironment,
|
||||
skill_turn: Option<&TurnContext>,
|
||||
@@ -654,90 +692,6 @@ impl Session {
|
||||
)
|
||||
}
|
||||
|
||||
/// Acquires V2 inventories and reports changes without scheduling an MCP refresh.
|
||||
pub(super) async fn executor_capability_discovery_for_step_v2(
|
||||
&self,
|
||||
config: &Config,
|
||||
environments: &TurnEnvironmentSnapshot,
|
||||
skill_turn: Option<&TurnContext>,
|
||||
) -> (Option<Vec<ExecutorCapabilityDiscoveryV2Snapshot>>, bool) {
|
||||
if !config
|
||||
.features
|
||||
.enabled(Feature::ExecutorCapabilityDiscoveryV2)
|
||||
{
|
||||
return (None, false);
|
||||
}
|
||||
// Retain this cache if a new turn replaces the thread's current cache.
|
||||
let cache = self
|
||||
.services
|
||||
.thread_extension_data
|
||||
.get_or_init(ExecutorCapabilityDiscoveryV2Cache::default);
|
||||
let remote_environment_count = environments
|
||||
.turn_environments()
|
||||
.filter(|selected| selected.environment().is_remote())
|
||||
.count();
|
||||
cache.ensure_capacity(remote_environment_count);
|
||||
let mut snapshots = Vec::new();
|
||||
let mut skipped_incompatible_executor = false;
|
||||
for selected in environments.turn_environments() {
|
||||
if !selected.environment().is_remote() {
|
||||
continue;
|
||||
}
|
||||
let environment_id = &selected.selection.environment_id;
|
||||
let info = match selected.environment().info().await {
|
||||
Ok(info) => info,
|
||||
Err(_) => {
|
||||
tracing::warn!(
|
||||
environment_id,
|
||||
"could not read executor discovery V2 support"
|
||||
);
|
||||
continue;
|
||||
}
|
||||
};
|
||||
if !supports_capability_discovery_v2(&info) {
|
||||
skipped_incompatible_executor = true;
|
||||
continue;
|
||||
}
|
||||
let request = DiscoverV2CapabilitiesRequest {
|
||||
cwd: selected.cwd().clone(),
|
||||
sandbox: Some(self.discovery_sandbox(selected, skill_turn)),
|
||||
};
|
||||
let result = cache
|
||||
.discover(environment_id, selected.environment(), request)
|
||||
.await;
|
||||
snapshots.push(ExecutorCapabilityDiscoveryV2Snapshot {
|
||||
environment_id: environment_id.clone(),
|
||||
result,
|
||||
});
|
||||
}
|
||||
let incompatible = snapshots.is_empty() && skipped_incompatible_executor;
|
||||
if skill_turn.is_some() {
|
||||
// Only session discovery updates the inventory used to invalidate MCP.
|
||||
return ((!incompatible).then_some(snapshots), false);
|
||||
}
|
||||
if incompatible {
|
||||
let changed = self
|
||||
.services
|
||||
.thread_extension_data
|
||||
.remove::<Vec<ExecutorCapabilityDiscoveryV2Snapshot>>()
|
||||
.is_some();
|
||||
return (None, changed);
|
||||
}
|
||||
let previous = self
|
||||
.services
|
||||
.thread_extension_data
|
||||
.insert(snapshots.clone());
|
||||
let inventory_changed = previous.as_ref().map_or(!snapshots.is_empty(), |previous| {
|
||||
previous.len() != snapshots.len()
|
||||
|| previous
|
||||
.iter()
|
||||
.zip(&snapshots)
|
||||
.any(|(previous, current)| !previous.same_discovery(current))
|
||||
});
|
||||
let inventory_changed = cache.take_recovered_discovery() || inventory_changed;
|
||||
(Some(snapshots), inventory_changed)
|
||||
}
|
||||
|
||||
pub(crate) async fn resolve_selected_capability_roots_for_step(
|
||||
&self,
|
||||
environments: &TurnEnvironmentSnapshot,
|
||||
|
||||
@@ -259,6 +259,7 @@ mod config_refresh;
|
||||
pub(crate) mod context_window;
|
||||
mod daemon_recovery;
|
||||
mod environment;
|
||||
mod executor_discovery;
|
||||
mod extension_interruption;
|
||||
pub(crate) mod extension_metrics;
|
||||
mod guardian_checkpoint;
|
||||
@@ -4544,7 +4545,7 @@ impl Session {
|
||||
.await,
|
||||
);
|
||||
// Only skill discovery may use a turn's temporary file access. Plugin servers and
|
||||
// hooks use the session inventory above because they can outlive the step.
|
||||
// hooks use the session inventory because they can outlive the step.
|
||||
let turn_skill_discovery =
|
||||
if let Some(grants) = turn_context.turn_skill_filesystem_grants(&environments) {
|
||||
if turn_context
|
||||
@@ -4604,12 +4605,14 @@ impl Session {
|
||||
{
|
||||
required_servers.push(codex_mcp::CODEX_APPS_MCP_SERVER_NAME.to_owned());
|
||||
}
|
||||
let captured_executor_discovery_v2 = executor_capability_discovery_v2.as_deref();
|
||||
let catalogs = self.lock_cloud_catalogs().await;
|
||||
let (mcp, prepared_recommendations) = tokio::join!(
|
||||
// MCP refresh can be large; keep it off the sampling request's stack.
|
||||
Box::pin(self.mcp_runtime_for_step(
|
||||
turn_context.as_ref(),
|
||||
&selected_capability_roots,
|
||||
captured_executor_discovery_v2,
|
||||
&required_servers,
|
||||
required_plugins,
|
||||
)),
|
||||
@@ -4621,6 +4624,11 @@ impl Session {
|
||||
}
|
||||
},
|
||||
);
|
||||
let mcp = mcp?;
|
||||
// Skill and hook admission must use the policy published with this binding.
|
||||
let mut plugin_config = turn_context.config.as_ref().clone();
|
||||
plugin_config.plugins = mcp.config().plugins.clone();
|
||||
plugin_config.config_layer_stack = mcp.config().config_layer_stack.clone();
|
||||
let search = crate::model_search::resolve(
|
||||
turn_context.as_ref(),
|
||||
&settings,
|
||||
@@ -4647,7 +4655,7 @@ impl Session {
|
||||
let select_plugins = |discovery, discovery_v2| {
|
||||
self.services.mcp_manager.selected_plugins_for_step(
|
||||
codex_extension_api::McpServerContributionContext::for_step(
|
||||
turn_context.config.as_ref(),
|
||||
&plugin_config,
|
||||
&self.services.mcp_thread_init,
|
||||
&self.services.thread_extension_data,
|
||||
&turn_context.originator,
|
||||
|
||||
@@ -9562,10 +9562,12 @@ async fn refresh_mcp_servers_uses_latest_state_for_existing_turns() {
|
||||
.mcp_runtime_for_step(
|
||||
&turn_context,
|
||||
&[],
|
||||
/*captured_executor_discovery_v2*/ None,
|
||||
/*required_servers*/ &[],
|
||||
/*required_plugins*/ &HashSet::new(),
|
||||
)
|
||||
.await;
|
||||
.await
|
||||
.expect("prepare latest MCP state");
|
||||
|
||||
let configured_servers = codex_mcp::configured_mcp_servers(new_step.mcp.config());
|
||||
assert_eq!(
|
||||
|
||||
@@ -63,8 +63,15 @@ async fn step_discovery_reconciles_mcp_skips_prewarm_and_releases_lock_on_cancel
|
||||
.new_startup_prewarm_turn_with_sub_id(INITIAL_SUBMIT_ID.to_owned())
|
||||
.await;
|
||||
session
|
||||
.mcp_runtime_for_step(&prewarm, &[], &[], &HashSet::new())
|
||||
.await;
|
||||
.mcp_runtime_for_step(
|
||||
&prewarm,
|
||||
&[],
|
||||
/*captured_executor_discovery_v2*/ None,
|
||||
&[],
|
||||
&HashSet::new(),
|
||||
)
|
||||
.await
|
||||
.expect("prepare MCP without cloud callbacks");
|
||||
assert!(recorder.keys.lock().expect("recorded keys").is_empty());
|
||||
let initial_key = recorder.client.cache_key();
|
||||
|
||||
@@ -97,8 +104,15 @@ async fn step_discovery_reconciles_mcp_skips_prewarm_and_releases_lock_on_cancel
|
||||
|
||||
// Prewarm also skips discovery while a regular turn is preparing a step.
|
||||
session
|
||||
.mcp_runtime_for_step(&prewarm, &[], &[], &HashSet::new())
|
||||
.await;
|
||||
.mcp_runtime_for_step(
|
||||
&prewarm,
|
||||
&[],
|
||||
/*captured_executor_discovery_v2*/ None,
|
||||
&[],
|
||||
&HashSet::new(),
|
||||
)
|
||||
.await
|
||||
.expect("prepare MCP without cloud callbacks");
|
||||
assert_eq!(recorder.keys.lock().expect("recorded keys").len(), 1);
|
||||
cancellation.cancel();
|
||||
assert!(capture.await.is_err());
|
||||
|
||||
@@ -5,14 +5,12 @@ mod regular;
|
||||
mod review;
|
||||
mod user_shell;
|
||||
|
||||
use crate::McpRefreshReason;
|
||||
use std::future::Future;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
use std::time::Instant;
|
||||
|
||||
use codex_diagnostics::Gauge;
|
||||
use codex_exec_server::ExecutorCapabilityDiscoveryV2Cache;
|
||||
use codex_extension_api::ThreadIdleCause;
|
||||
use futures::future::BoxFuture;
|
||||
use tokio::sync::Mutex;
|
||||
@@ -321,18 +319,6 @@ impl Session {
|
||||
input: Vec<TurnInput>,
|
||||
task: T,
|
||||
) -> TurnInputSubmission {
|
||||
// Replace the cache; in-flight acquisitions retain their previous Arc.
|
||||
self.services
|
||||
.thread_extension_data
|
||||
.insert(ExecutorCapabilityDiscoveryV2Cache::default());
|
||||
if turn_context
|
||||
.config
|
||||
.features
|
||||
.enabled(Feature::ExecutorCapabilityDiscoveryV2)
|
||||
{
|
||||
// MCP contributors must project the new inventory even if their other inputs match.
|
||||
self.request_mcp_runtime_reprojection(McpRefreshReason::TaskStartedExecutorDiscovery);
|
||||
}
|
||||
self.activate_plugin_selection(&turn_context).await;
|
||||
// Inherited or recovered roots are applied before task start. Otherwise this
|
||||
// task owns its turn, including background work. Later mail cannot change it.
|
||||
|
||||
@@ -14,11 +14,9 @@ use codex_core_plugins::PluginProvider;
|
||||
use codex_core_plugins::PluginProviderFuture;
|
||||
use codex_core_plugins::PluginSourceLocation;
|
||||
use codex_exec_server::DiscoverV2CapabilitiesResponse;
|
||||
use codex_exec_server::ExecutorCapabilityDiscoveryV2Cache;
|
||||
use codex_exec_server::ExecutorCapabilityDiscoveryV2Snapshot;
|
||||
use codex_exec_server::HttpRequestBodyDeltaNotification;
|
||||
use codex_exec_server::HttpRequestParams;
|
||||
use codex_extension_api::ContextContributor;
|
||||
use codex_extension_api::ExtensionFuture;
|
||||
use codex_extension_api::ExtensionRegistryBuilder;
|
||||
use codex_extension_api::McpServerContribution;
|
||||
@@ -28,8 +26,6 @@ use codex_extension_api::SelectedPluginSnapshot;
|
||||
use codex_extension_api::ToolFinishInput;
|
||||
use codex_extension_api::ToolLifecycleContributor;
|
||||
use codex_extension_api::ToolLifecycleFuture;
|
||||
use codex_extension_api::WorldStateContributionInput;
|
||||
use codex_extension_api::WorldStateSectionContribution;
|
||||
use codex_features::Feature;
|
||||
use codex_login::CodexAuth;
|
||||
use codex_mcp::CODEX_APPS_MCP_SERVER_NAME;
|
||||
@@ -58,6 +54,7 @@ use core_test_support::context_snapshot;
|
||||
use core_test_support::context_snapshot::ContextSnapshotOptions;
|
||||
use core_test_support::responses;
|
||||
use core_test_support::skip_if_no_network;
|
||||
use core_test_support::test_codex::environment_config_for_selection;
|
||||
use core_test_support::test_codex::test_codex;
|
||||
use futures::SinkExt;
|
||||
use futures::StreamExt;
|
||||
@@ -65,6 +62,8 @@ use pretty_assertions::assert_eq;
|
||||
use serde_json::Value;
|
||||
use serde_json::json;
|
||||
use std::sync::Arc;
|
||||
use std::sync::OnceLock;
|
||||
use std::sync::Weak;
|
||||
use std::sync::atomic::AtomicBool;
|
||||
use std::sync::atomic::AtomicUsize;
|
||||
use std::sync::atomic::Ordering;
|
||||
@@ -78,9 +77,9 @@ use tokio_tungstenite::tungstenite::Message;
|
||||
use tokio_util::task::AbortOnDropHandle;
|
||||
|
||||
struct DiscoveryRecorder {
|
||||
mcp_projections: AtomicUsize,
|
||||
snapshots: Arc<Mutex<Vec<ExecutorCapabilityDiscoveryV2Snapshot>>>,
|
||||
selected_plugins: Arc<Mutex<Option<SelectedPluginSnapshot>>>,
|
||||
mcp_snapshots_per_step: Arc<Mutex<Vec<usize>>>,
|
||||
}
|
||||
|
||||
impl McpServerContributor<Config> for DiscoveryRecorder {
|
||||
@@ -93,6 +92,7 @@ impl McpServerContributor<Config> for DiscoveryRecorder {
|
||||
context: McpServerContributionContext<'a, Config>,
|
||||
) -> ExtensionFuture<'a, Vec<McpServerContribution>> {
|
||||
Box::pin(async move {
|
||||
self.mcp_projections.fetch_add(1, Ordering::Relaxed);
|
||||
if let Some(snapshots) = context.executor_capability_discovery_v2() {
|
||||
self.snapshots.lock().await.extend_from_slice(snapshots);
|
||||
}
|
||||
@@ -177,29 +177,19 @@ impl codex_extension_api::ToolContributor for DiscoveryRecorder {
|
||||
}
|
||||
}
|
||||
|
||||
impl ContextContributor for DiscoveryRecorder {
|
||||
fn contribute_world_state<'a>(
|
||||
&'a self,
|
||||
_input: WorldStateContributionInput<'a>,
|
||||
) -> ExtensionFuture<'a, Vec<WorldStateSectionContribution>> {
|
||||
Box::pin(async move {
|
||||
let snapshot_count = self.snapshots.lock().await.len();
|
||||
self.mcp_snapshots_per_step
|
||||
.lock()
|
||||
.await
|
||||
.push(snapshot_count);
|
||||
Vec::new()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) struct Executor {
|
||||
pub(super) url: String,
|
||||
pub(super) inventory: Arc<Mutex<Option<Value>>>,
|
||||
pub(super) requests: Arc<Mutex<Vec<Value>>>,
|
||||
discovery_pause: Arc<Mutex<Option<DiscoveryPause>>>,
|
||||
_task: AbortOnDropHandle<Result<()>>,
|
||||
}
|
||||
|
||||
struct DiscoveryPause {
|
||||
started: oneshot::Sender<()>,
|
||||
resume: oneshot::Receiver<()>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub(super) enum DiscoveryFailure {
|
||||
None,
|
||||
@@ -225,6 +215,8 @@ impl Executor {
|
||||
let inventory = Arc::new(Mutex::new(None::<Value>));
|
||||
let served_inventory = Arc::clone(&inventory);
|
||||
let recorded = Arc::clone(&requests);
|
||||
let discovery_pause = Arc::new(Mutex::new(None::<DiscoveryPause>));
|
||||
let pause = Arc::clone(&discovery_pause);
|
||||
let discovery_attempts = Arc::new(AtomicUsize::new(0));
|
||||
let http = codex_exec_server::Environment::create_for_tests(/*exec_server_url*/ None)?
|
||||
.get_http_client();
|
||||
@@ -232,6 +224,7 @@ impl Executor {
|
||||
let mut connections = tokio::task::JoinSet::new();
|
||||
while let Ok((stream, _)) = listener.accept().await {
|
||||
let recorded = Arc::clone(&recorded);
|
||||
let pause = Arc::clone(&pause);
|
||||
let inventory = Arc::clone(&served_inventory);
|
||||
let discovery_attempts = Arc::clone(&discovery_attempts);
|
||||
let http = Arc::clone(&http);
|
||||
@@ -272,7 +265,7 @@ impl Executor {
|
||||
continue;
|
||||
}
|
||||
DiscoveryFailure::Server if attempt == 0 => {
|
||||
socket.send(Message::Text(json!({"id":request["id"],"error":{"code":-32004,"message":"fixture discovery failure"}}).to_string().into())).await?;
|
||||
socket.send(Message::Text(json!({"id":request["id"],"error":{"code":-32603,"message":"fixture discovery failure"}}).to_string().into())).await?;
|
||||
continue;
|
||||
}
|
||||
DiscoveryFailure::None | DiscoveryFailure::Server | DiscoveryFailure::Disconnect | DiscoveryFailure::TurnGrant => {}
|
||||
@@ -322,6 +315,13 @@ impl Executor {
|
||||
Some("fs/walk") => Some(json!({"entries":[],"errors":[],"truncated":false})),
|
||||
_ => None,
|
||||
};
|
||||
if request["method"] == "capabilities/discoverV2" {
|
||||
let pause = pause.lock().await.take();
|
||||
if let Some(pause) = pause {
|
||||
let _ = pause.started.send(());
|
||||
pause.resume.await?;
|
||||
}
|
||||
}
|
||||
let response = match result {
|
||||
Some(result) => json!({"id":request["id"],"result":result}),
|
||||
None => json!({"id":request["id"],"error":{"code":-32004,"message":"fixture discovery failure"}}),
|
||||
@@ -337,6 +337,7 @@ impl Executor {
|
||||
url,
|
||||
inventory,
|
||||
requests,
|
||||
discovery_pause,
|
||||
_task: AbortOnDropHandle::new(task),
|
||||
})
|
||||
}
|
||||
@@ -348,18 +349,15 @@ async fn v2_disabled_skips_inventory_discovery() -> Result<()> {
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn v2_shares_inventory_with_mcp_and_skills_until_next_turn() -> Result<()> {
|
||||
check_acquisition(/*enabled*/ true, "0.164.0").await
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn v2_changed_inventory_refreshes_mcp_without_selection_changes() -> Result<()> {
|
||||
async fn v2_captured_inventory_survives_refresh_without_restoring_revoked_mcp() -> Result<()> {
|
||||
skip_if_no_network!(Ok(()));
|
||||
|
||||
#[derive(Default)]
|
||||
struct InventoryRecorder {
|
||||
projections: AtomicUsize,
|
||||
thread: OnceLock<Weak<codex_core::CodexThread>>,
|
||||
invalidated_during_capture: AtomicBool,
|
||||
projected: Mutex<Option<Arc<DiscoverV2CapabilitiesResponse>>>,
|
||||
captured_before_invalidation: Mutex<Option<Arc<DiscoverV2CapabilitiesResponse>>>,
|
||||
steps: Mutex<
|
||||
Vec<(
|
||||
Arc<DiscoverV2CapabilitiesResponse>,
|
||||
@@ -368,6 +366,19 @@ async fn v2_changed_inventory_refreshes_mcp_without_selection_changes() -> Resul
|
||||
>,
|
||||
}
|
||||
|
||||
impl codex_extension_api::ThreadLifecycleContributor<Config> for InventoryRecorder {
|
||||
fn on_thread_start<'a>(
|
||||
&'a self,
|
||||
input: codex_extension_api::ThreadStartInput<'a, Config>,
|
||||
) -> ExtensionFuture<'a, ()> {
|
||||
Box::pin(async move {
|
||||
input
|
||||
.session_store
|
||||
.insert_arc(input.mcp_resource_client.expect("MCP resource client"));
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl McpServerContributor<Config> for InventoryRecorder {
|
||||
fn id(&self) -> &'static str {
|
||||
"inventory-refresh-recorder"
|
||||
@@ -378,7 +389,6 @@ async fn v2_changed_inventory_refreshes_mcp_without_selection_changes() -> Resul
|
||||
context: McpServerContributionContext<'a, Config>,
|
||||
) -> ExtensionFuture<'a, Vec<McpServerContribution>> {
|
||||
Box::pin(async move {
|
||||
self.projections.fetch_add(1, Ordering::SeqCst);
|
||||
*self.projected.lock().await = context
|
||||
.executor_capability_discovery_v2()
|
||||
.and_then(|snapshots| snapshots.first())
|
||||
@@ -389,12 +399,72 @@ async fn v2_changed_inventory_refreshes_mcp_without_selection_changes() -> Resul
|
||||
}
|
||||
}
|
||||
|
||||
impl codex_extension_api::TurnLifecycleContributor for InventoryRecorder {
|
||||
fn refresh_catalog_for_step<'a>(
|
||||
&'a self,
|
||||
input: codex_extension_api::CatalogRefreshInput<'a>,
|
||||
) -> ExtensionFuture<'a, bool> {
|
||||
Box::pin(async move {
|
||||
if !self
|
||||
.invalidated_during_capture
|
||||
.swap(true, Ordering::Relaxed)
|
||||
{
|
||||
let projected = self.projected.lock().await.clone();
|
||||
*self.captured_before_invalidation.lock().await = projected;
|
||||
let client = input
|
||||
.session_store
|
||||
.get::<McpResourceClient>()
|
||||
.expect("MCP resource client");
|
||||
assert!(client.has_server("revoked").await);
|
||||
let thread = self
|
||||
.thread
|
||||
.get()
|
||||
.and_then(Weak::upgrade)
|
||||
.expect("live test thread");
|
||||
let selection = thread
|
||||
.environment_selections()
|
||||
.await
|
||||
.into_iter()
|
||||
.find(|selection| {
|
||||
selection.environment_id == codex_exec_server::REMOTE_ENVIRONMENT_ID
|
||||
})
|
||||
.expect("selected executor");
|
||||
let mut config = environment_config_for_selection(
|
||||
thread.config().await.as_ref(),
|
||||
&selection,
|
||||
);
|
||||
config.mcp_policy = Some(codex_protocol::mcp_policy::EnvironmentMcpPolicy {
|
||||
servers: Some(Default::default()),
|
||||
..Default::default()
|
||||
});
|
||||
thread
|
||||
.environment_ready(&selection, config)
|
||||
.await
|
||||
.expect("revoke MCP server");
|
||||
thread.invalidate_executor_discovery();
|
||||
// A cloud catalog change needs another MCP projection, using the
|
||||
// executor inventory this step already captured before invalidation.
|
||||
return true;
|
||||
}
|
||||
false
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl codex_extension_api::ToolContributor for InventoryRecorder {
|
||||
fn prepare_for_step<'a>(
|
||||
&'a self,
|
||||
input: codex_extension_api::StepPreparationInput<'a>,
|
||||
) -> ExtensionFuture<'a, ()> {
|
||||
Box::pin(async move {
|
||||
let client = input
|
||||
.session_store
|
||||
.get::<McpResourceClient>()
|
||||
.expect("MCP resource client");
|
||||
assert!(
|
||||
!client.has_server("revoked").await,
|
||||
"the published runtime must enforce revocation even with the step's old inventory"
|
||||
);
|
||||
let snapshots = input
|
||||
.executor_capability_discovery_v2
|
||||
.expect("captured inventories");
|
||||
@@ -406,24 +476,36 @@ async fn v2_changed_inventory_refreshes_mcp_without_selection_changes() -> Resul
|
||||
let projected = self.projected.lock().await.clone().expect("MCP inventory");
|
||||
let mut steps = self.steps.lock().await;
|
||||
steps.push((inventory, projected));
|
||||
// Model a fresh inventory after each step, without changing selected roots.
|
||||
input
|
||||
.thread_store
|
||||
.insert(ExecutorCapabilityDiscoveryV2Cache::default());
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
let executor = Executor::start(/*supports_v2*/ true, DiscoveryFailure::None).await?;
|
||||
let model = responses::start_mock_server().await;
|
||||
let apps = AppsTestServer::mount(&model).await?;
|
||||
let recorder = Arc::new(InventoryRecorder::default());
|
||||
let mut extensions = ExtensionRegistryBuilder::new();
|
||||
extensions.thread_lifecycle_contributor(recorder.clone());
|
||||
extensions.tool_contributor(recorder.clone());
|
||||
extensions.mcp_server_contributor(recorder.clone());
|
||||
extensions.turn_lifecycle_contributor(recorder.clone());
|
||||
let test = test_codex()
|
||||
.with_extensions(Arc::new(extensions.build()))
|
||||
.with_exec_server_url(executor.url.clone())
|
||||
.with_config(|config| {
|
||||
.with_config(move |config| {
|
||||
let mut servers = config.mcp_servers.get().clone();
|
||||
servers.insert(
|
||||
"revoked".to_string(),
|
||||
serde_json::from_value(json!({
|
||||
"url": format!("{}/api/codex/ps/mcp", apps.chatgpt_base_url),
|
||||
"environment_id": codex_exec_server::REMOTE_ENVIRONMENT_ID,
|
||||
}))
|
||||
.expect("test MCP config"),
|
||||
);
|
||||
config
|
||||
.mcp_servers
|
||||
.set(servers)
|
||||
.expect("unconstrained MCP config");
|
||||
config.update_plan_enabled = true;
|
||||
config
|
||||
.features
|
||||
@@ -432,6 +514,7 @@ async fn v2_changed_inventory_refreshes_mcp_without_selection_changes() -> Resul
|
||||
})
|
||||
.build_with_auto_env(&model)
|
||||
.await?;
|
||||
assert!(recorder.thread.set(Arc::downgrade(&test.codex)).is_ok());
|
||||
let first = responses::mount_sse_once(
|
||||
&model,
|
||||
responses::sse(vec![
|
||||
@@ -457,12 +540,21 @@ async fn v2_changed_inventory_refreshes_mcp_without_selection_changes() -> Resul
|
||||
first.single_request();
|
||||
second.single_request();
|
||||
{
|
||||
let captured = recorder
|
||||
.captured_before_invalidation
|
||||
.lock()
|
||||
.await
|
||||
.clone()
|
||||
.context("inventory captured before invalidation")?;
|
||||
let steps = recorder.steps.lock().await;
|
||||
let (initial, _) = steps.first().context("first captured inventory")?;
|
||||
assert!(
|
||||
steps
|
||||
.iter()
|
||||
.any(|(inventory, _)| !Arc::ptr_eq(initial, inventory)),
|
||||
Arc::ptr_eq(initial, &captured),
|
||||
"catalog refresh must not reacquire the invalidated executor inventory within this step"
|
||||
);
|
||||
assert_eq!(steps.len(), 2);
|
||||
assert!(
|
||||
!Arc::ptr_eq(initial, &steps[1].0),
|
||||
"the next step must acquire a fresh inventory"
|
||||
);
|
||||
for (inventory, projected) in steps.iter() {
|
||||
@@ -472,17 +564,180 @@ async fn v2_changed_inventory_refreshes_mcp_without_selection_changes() -> Resul
|
||||
);
|
||||
}
|
||||
}
|
||||
let projections = recorder.projections.load(Ordering::SeqCst);
|
||||
// Refresh before another step can discover the new inventory. This fixture has no Apps
|
||||
// server, but the refresh must still publish exactly once before reporting that absence.
|
||||
let error = test
|
||||
assert!(recorder.invalidated_during_capture.load(Ordering::Relaxed));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn v2_refresh_during_discovery_applies_to_the_next_step() -> Result<()> {
|
||||
skip_if_no_network!(Ok(()));
|
||||
let executor = Executor::start(/*supports_v2*/ true, DiscoveryFailure::None).await?;
|
||||
let second_executor = Executor::start(/*supports_v2*/ true, DiscoveryFailure::None).await?;
|
||||
let model = responses::start_mock_server().await;
|
||||
let test = test_codex()
|
||||
.with_exec_server_url(executor.url.clone())
|
||||
.with_config(|config| {
|
||||
config
|
||||
.features
|
||||
.enable(Feature::ExecutorCapabilityDiscoveryV2)
|
||||
.expect("enable discovery V2");
|
||||
})
|
||||
.build_with_auto_env(&model)
|
||||
.await?;
|
||||
let original_selection = test.codex.environment_selections().await.remove(0);
|
||||
test.thread_manager
|
||||
.environment_manager()
|
||||
.upsert_environment(
|
||||
"second".into(),
|
||||
second_executor.url.clone(),
|
||||
/*connect_timeout*/ None,
|
||||
)?;
|
||||
let mut second_selection = original_selection.clone();
|
||||
second_selection.environment_id = "second".into();
|
||||
second_selection.config = codex_protocol::protocol::EnvironmentConfigState::Ready(
|
||||
environment_config_for_selection(&test.config, &second_selection),
|
||||
);
|
||||
test.codex
|
||||
.update_thread_settings(codex_protocol::protocol::ThreadSettingsOverrides {
|
||||
environments: Some(codex_protocol::protocol::TurnEnvironmentRequests::new(
|
||||
test.config.cwd.clone(),
|
||||
vec![
|
||||
original_selection.clone().into_request(),
|
||||
second_selection.into_request(),
|
||||
],
|
||||
)),
|
||||
..Default::default()
|
||||
})
|
||||
.await?;
|
||||
responses::mount_sse_once(
|
||||
&model,
|
||||
responses::sse(vec![responses::ev_completed("ready")]),
|
||||
)
|
||||
.await;
|
||||
test.codex
|
||||
.start_or_steer_turn(TurnInputRequest::user_input(vec![UserInput::Text {
|
||||
text: "Initialize both environments.".into(),
|
||||
text_elements: Vec::new(),
|
||||
}]))
|
||||
.await?;
|
||||
core_test_support::wait_for_event(&test.codex, |event| {
|
||||
matches!(event, EventMsg::TurnComplete(_))
|
||||
})
|
||||
.await;
|
||||
let _ = test.codex.current_mcp_config_and_runtime_context().await;
|
||||
let second_calls = || async {
|
||||
second_executor
|
||||
.requests
|
||||
.lock()
|
||||
.await
|
||||
.iter()
|
||||
.filter(|request| request["method"] == "capabilities/discoverV2")
|
||||
.count()
|
||||
};
|
||||
let second_before = second_calls().await;
|
||||
let calls = || async {
|
||||
executor
|
||||
.requests
|
||||
.lock()
|
||||
.await
|
||||
.iter()
|
||||
.filter(|request| request["method"] == "capabilities/discoverV2")
|
||||
.count()
|
||||
};
|
||||
let before = calls().await;
|
||||
let (started, captured) = oneshot::channel();
|
||||
let (resume, released) = oneshot::channel();
|
||||
*second_executor.discovery_pause.lock().await = Some(DiscoveryPause {
|
||||
started,
|
||||
resume: released,
|
||||
});
|
||||
test.codex.invalidate_executor_discovery();
|
||||
let (_, refreshed) = tokio::join!(test.codex.current_mcp_config_and_runtime_context(), async {
|
||||
tokio::time::timeout(Duration::from_secs(/*secs*/ 10), captured).await??;
|
||||
// A has finished and B is still loading. Both readers may finish with their
|
||||
// captured inventories; repeated refreshes must survive until the next lookup.
|
||||
assert_eq!(calls().await, before + 1);
|
||||
*executor.inventory.lock().await = Some(json!({"plugins":[],"skills":[],"warnings":[]}));
|
||||
*second_executor.inventory.lock().await =
|
||||
Some(json!({"plugins":[],"skills":[],"warnings":[]}));
|
||||
for _ in 0..3 {
|
||||
test.codex.invalidate_executor_discovery();
|
||||
}
|
||||
resume
|
||||
.send(())
|
||||
.map_err(|_| anyhow::anyhow!("discovery stopped before release"))?;
|
||||
anyhow::Ok(())
|
||||
},);
|
||||
refreshed?;
|
||||
let captured_snapshots = test
|
||||
.codex
|
||||
.refresh_codex_apps_tools()
|
||||
.await
|
||||
.err()
|
||||
.expect("no Apps server");
|
||||
assert!(error.to_string().contains("unknown MCP server"));
|
||||
assert_eq!(recorder.projections.load(Ordering::SeqCst), projections + 1);
|
||||
.thread_extension_data()
|
||||
.get::<Vec<ExecutorCapabilityDiscoveryV2Snapshot>>()
|
||||
.context("published inventory")?;
|
||||
assert_eq!(captured_snapshots.len(), 2);
|
||||
for snapshot in captured_snapshots.iter() {
|
||||
assert!(
|
||||
!snapshot
|
||||
.result
|
||||
.as_ref()
|
||||
.map_err(|error| anyhow::anyhow!("{error}"))?
|
||||
.plugins
|
||||
.is_empty(),
|
||||
"an in-flight reader may finish with its captured inventory"
|
||||
);
|
||||
}
|
||||
assert_eq!(
|
||||
calls().await,
|
||||
before + 1,
|
||||
"refresh does not restart the current discovery"
|
||||
);
|
||||
assert_eq!(second_calls().await, second_before + 1);
|
||||
|
||||
let response = responses::mount_sse_once(
|
||||
&model,
|
||||
responses::sse(vec![responses::ev_completed("after-refresh")]),
|
||||
)
|
||||
.await;
|
||||
test.codex
|
||||
.start_or_steer_turn(TurnInputRequest::user_input(vec![UserInput::Text {
|
||||
text: "Use the refreshed inventories.".into(),
|
||||
text_elements: Vec::new(),
|
||||
}]))
|
||||
.await?;
|
||||
core_test_support::wait_for_event(&test.codex, |event| {
|
||||
matches!(event, EventMsg::TurnComplete(_))
|
||||
})
|
||||
.await;
|
||||
response.single_request();
|
||||
let snapshots = test
|
||||
.codex
|
||||
.thread_extension_data()
|
||||
.get::<Vec<ExecutorCapabilityDiscoveryV2Snapshot>>()
|
||||
.context("refreshed inventory")?;
|
||||
let inventory = snapshots
|
||||
.iter()
|
||||
.find(|snapshot| snapshot.environment_id == original_selection.environment_id)
|
||||
.context("executor inventory")?
|
||||
.result
|
||||
.as_ref()
|
||||
.map_err(|error| anyhow::anyhow!("{error}"))?;
|
||||
assert!(inventory.plugins.is_empty(), "the next step must refresh");
|
||||
let _ = test.codex.current_mcp_config_and_runtime_context().await;
|
||||
assert_eq!(calls().await, before + 2);
|
||||
assert_eq!(second_calls().await, second_before + 2);
|
||||
assert_eq!(snapshots.len(), 2);
|
||||
let second = snapshots
|
||||
.iter()
|
||||
.find(|snapshot| snapshot.environment_id == "second")
|
||||
.context("second inventory")?;
|
||||
assert!(
|
||||
second
|
||||
.result
|
||||
.as_ref()
|
||||
.map_err(|error| anyhow::anyhow!("{error}"))?
|
||||
.plugins
|
||||
.is_empty()
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -519,12 +774,12 @@ async fn v2_discovery_failures_retry_or_remain_cached_by_error_kind() -> Result<
|
||||
DiscoveryFailure::Server => {
|
||||
assert!(first.is_err());
|
||||
assert!(concurrent.is_err());
|
||||
let next_turn = codex_exec_server::ExecutorCapabilityDiscoveryV2Cache::default();
|
||||
next_turn
|
||||
let refreshed = codex_exec_server::ExecutorCapabilityDiscoveryV2Cache::default();
|
||||
refreshed
|
||||
.discover("executor", &environment, request.clone())
|
||||
.await
|
||||
.map_err(anyhow::Error::msg)?;
|
||||
assert!(!next_turn.take_recovered_discovery());
|
||||
assert!(!refreshed.take_recovered_discovery());
|
||||
assert!(!cache.take_recovered_discovery());
|
||||
2
|
||||
}
|
||||
@@ -566,23 +821,25 @@ async fn v2_old_executor_retains_v1_selected_roots() -> Result<()> {
|
||||
check_acquisition(/*enabled*/ true, "0.163.0").await
|
||||
}
|
||||
|
||||
async fn check_acquisition(enabled: bool, version: &'static str) -> Result<()> {
|
||||
pub(super) async fn check_acquisition(enabled: bool, version: &'static str) -> Result<()> {
|
||||
skip_if_no_network!(Ok(()));
|
||||
let executor =
|
||||
Executor::start_with_version(/*supports_v2*/ true, DiscoveryFailure::None, version).await?;
|
||||
let acquires_v2 = enabled && version == "0.164.0";
|
||||
if acquires_v2 {
|
||||
*executor.inventory.lock().await = Some(json!({"plugins":[],"skills":[],"warnings":[]}));
|
||||
}
|
||||
let model = responses::start_mock_server().await;
|
||||
let observed = Arc::new(Mutex::new(Vec::new()));
|
||||
let selected_plugins = Arc::new(Mutex::new(None));
|
||||
let mcp_snapshots_per_step = Arc::new(Mutex::new(Vec::new()));
|
||||
let mut extensions = ExtensionRegistryBuilder::new();
|
||||
let recorder = Arc::new(DiscoveryRecorder {
|
||||
mcp_projections: AtomicUsize::new(0),
|
||||
snapshots: Arc::clone(&observed),
|
||||
selected_plugins: Arc::clone(&selected_plugins),
|
||||
mcp_snapshots_per_step: Arc::clone(&mcp_snapshots_per_step),
|
||||
});
|
||||
extensions.prompt_contributor(recorder.clone());
|
||||
extensions.tool_contributor(recorder.clone());
|
||||
extensions.mcp_server_contributor(recorder);
|
||||
extensions.mcp_server_contributor(recorder.clone());
|
||||
codex_skills_extension::install_with_providers(
|
||||
&mut extensions,
|
||||
codex_skills_extension::SkillProviders::new().with_executor_provider(Arc::new(
|
||||
@@ -604,7 +861,6 @@ async fn check_acquisition(enabled: bool, version: &'static str) -> Result<()> {
|
||||
.with_extensions(Arc::new(extensions.build()))
|
||||
.with_exec_server_url(executor.url.clone())
|
||||
.with_config(move |config| {
|
||||
config.update_plan_enabled = true;
|
||||
assert!(config.features.enable(Feature::Plugins).is_ok());
|
||||
assert!(
|
||||
config
|
||||
@@ -632,22 +888,42 @@ async fn check_acquisition(enabled: bool, version: &'static str) -> Result<()> {
|
||||
.filter(|request| request["method"] == "capabilities/discoverV2")
|
||||
.count()
|
||||
};
|
||||
let acquires_v2 = enabled && version == "0.164.0";
|
||||
if acquires_v2 {
|
||||
observed.lock().await.clear();
|
||||
mcp_snapshots_per_step.lock().await.clear();
|
||||
let _ = test.codex.current_mcp_config_and_runtime_context().await;
|
||||
let initial_calls = calls().await;
|
||||
let before = calls().await;
|
||||
assert!(before > 0);
|
||||
{
|
||||
let observed = observed.lock().await;
|
||||
let inventory = observed
|
||||
.last()
|
||||
.context("initial inventory")?
|
||||
.result
|
||||
.as_ref()
|
||||
.map_err(|error| anyhow::anyhow!("{error}"))?;
|
||||
assert!(inventory.plugins.is_empty());
|
||||
assert!(inventory.skills.is_empty());
|
||||
}
|
||||
|
||||
*executor.inventory.lock().await = None; // Serve the sentinel plugin and skill.
|
||||
test.codex.invalidate_executor_discovery();
|
||||
assert_eq!(calls().await, before, "invalidation is lazy");
|
||||
let _ = test.codex.current_mcp_config_and_runtime_context().await;
|
||||
assert!(initial_calls > 0);
|
||||
assert_eq!(
|
||||
calls().await,
|
||||
initial_calls,
|
||||
"idle acquisitions reuse the cache"
|
||||
);
|
||||
assert_eq!(calls().await, before + 1);
|
||||
let observed = observed.lock().await;
|
||||
assert!(!observed.is_empty());
|
||||
assert!(observed.iter().all(|snapshot| snapshot.result.is_ok()));
|
||||
let inventory = observed
|
||||
.last()
|
||||
.context("refreshed inventory")?
|
||||
.result
|
||||
.as_ref()
|
||||
.map_err(|error| anyhow::anyhow!("{error}"))?;
|
||||
assert_eq!(
|
||||
inventory.plugins.first().context("refreshed plugin")?.id,
|
||||
"inert-sentinel@market"
|
||||
);
|
||||
assert_eq!(
|
||||
inventory.skills.first().context("refreshed skill")?.name,
|
||||
"inert-sentinel"
|
||||
);
|
||||
}
|
||||
let initial_calls = calls().await;
|
||||
let mut selection = test.executor_environment().selection().clone();
|
||||
@@ -681,138 +957,102 @@ async fn check_acquisition(enabled: bool, version: &'static str) -> Result<()> {
|
||||
}]
|
||||
},
|
||||
});
|
||||
// Only the successful lifecycle case needs two turns and multiple model steps.
|
||||
let turns = if acquires_v2 { 2 } else { 1 };
|
||||
let mut previous_inventory = None;
|
||||
for turn in 0..turns {
|
||||
observed.lock().await.clear();
|
||||
mcp_snapshots_per_step.lock().await.clear();
|
||||
*selected_plugins.lock().await = None;
|
||||
let before_turn = calls().await;
|
||||
let mut bodies = Vec::new();
|
||||
if acquires_v2 {
|
||||
bodies.push(responses::sse(vec![
|
||||
responses::ev_response_created("plan"),
|
||||
responses::ev_function_call(
|
||||
"plan-call",
|
||||
"update_plan",
|
||||
r#"{"plan":[{"step":"Say hello","status":"completed"}]}"#,
|
||||
),
|
||||
responses::ev_completed("plan"),
|
||||
]));
|
||||
}
|
||||
bodies.push(responses::sse(vec![
|
||||
let response = responses::mount_sse_once(
|
||||
&model,
|
||||
responses::sse(vec![
|
||||
responses::ev_response_created("response"),
|
||||
responses::ev_completed("response"),
|
||||
]));
|
||||
let mut responses = Vec::new();
|
||||
for body in bodies {
|
||||
responses.push(responses::mount_sse_once(&model, body).await);
|
||||
}
|
||||
]),
|
||||
)
|
||||
.await;
|
||||
test.submit_turn_with_environments(
|
||||
"Use $inert-sentinel to say hello.",
|
||||
Some(vec![selection.clone()]),
|
||||
)
|
||||
.await?;
|
||||
let executor_root = if acquires_v2 {
|
||||
format!("{}:inert-sentinel@market", selection.environment_id)
|
||||
} else {
|
||||
"legacy-root".to_string()
|
||||
};
|
||||
let local_identity = PluginIdentity::Local {
|
||||
plugin_id: "inert-sentinel@market".to_string(),
|
||||
};
|
||||
// Both paths publish accepted Cloud packages and available installations only.
|
||||
let expected_plugins = vec![
|
||||
(Some(executor_root), local_identity),
|
||||
(
|
||||
None,
|
||||
PluginIdentity::Remote {
|
||||
remote_plugin_id: "plugin_sentinel".to_string(),
|
||||
},
|
||||
),
|
||||
];
|
||||
{
|
||||
let selected_plugins = selected_plugins.lock().await;
|
||||
let snapshot = selected_plugins.as_ref().context("step plugin snapshot")?;
|
||||
assert_eq!(
|
||||
snapshot
|
||||
.plugins
|
||||
.iter()
|
||||
.map(|plugin| (plugin.selected_root_id.clone(), plugin.plugin_id.clone()))
|
||||
.collect::<Vec<_>>(),
|
||||
expected_plugins,
|
||||
);
|
||||
assert_eq!(snapshot.disabled_plugin_ids, Vec::<String>::new());
|
||||
}
|
||||
let request = response.single_request();
|
||||
assert!(
|
||||
request
|
||||
.body_json()
|
||||
.to_string()
|
||||
.contains("Sentinel instructions from the captured executor")
|
||||
);
|
||||
let developer_text = request.message_input_texts("developer").join("\n");
|
||||
assert!(
|
||||
developer_text.contains("- inert-sentinel: Executor skill used by the acquisition test"),
|
||||
"both discovery modes must render the skill catalog",
|
||||
);
|
||||
if acquires_v2 {
|
||||
let discovery_calls = calls().await;
|
||||
let mcp_projections = recorder.mcp_projections.load(Ordering::Relaxed);
|
||||
let second_response = responses::mount_sse_once(
|
||||
&model,
|
||||
responses::sse(vec![
|
||||
responses::ev_response_created("second-response"),
|
||||
responses::ev_completed("second-response"),
|
||||
]),
|
||||
)
|
||||
.await;
|
||||
test.submit_turn_with_environments(
|
||||
"Use $inert-sentinel to say hello.",
|
||||
Some(vec![selection.clone()]),
|
||||
)
|
||||
.await?;
|
||||
if acquires_v2 {
|
||||
let counts = mcp_snapshots_per_step.lock().await;
|
||||
let first = counts
|
||||
.first()
|
||||
.context("MCP snapshot count for the first step")?;
|
||||
assert!(
|
||||
counts.len() >= 2,
|
||||
"the turn must prepare multiple model steps"
|
||||
);
|
||||
assert!(
|
||||
counts.iter().all(|count| count == first),
|
||||
"unchanged V2 read routes must not refresh MCP between steps: {counts:?}"
|
||||
);
|
||||
}
|
||||
let executor_root = if acquires_v2 {
|
||||
format!("{}:inert-sentinel@market", selection.environment_id)
|
||||
} else {
|
||||
"legacy-root".to_string()
|
||||
};
|
||||
let local_identity = PluginIdentity::Local {
|
||||
plugin_id: "inert-sentinel@market".to_string(),
|
||||
};
|
||||
// Both paths publish accepted Cloud packages and available installations only.
|
||||
let expected_plugins = vec![
|
||||
(Some(executor_root), local_identity),
|
||||
(
|
||||
None,
|
||||
PluginIdentity::Remote {
|
||||
remote_plugin_id: "plugin_sentinel".to_string(),
|
||||
},
|
||||
),
|
||||
];
|
||||
{
|
||||
let selected_plugins = selected_plugins.lock().await;
|
||||
let snapshot = selected_plugins.as_ref().context("step plugin snapshot")?;
|
||||
assert_eq!(
|
||||
snapshot
|
||||
.plugins
|
||||
.iter()
|
||||
.map(|plugin| (plugin.selected_root_id.clone(), plugin.plugin_id.clone()))
|
||||
.collect::<Vec<_>>(),
|
||||
expected_plugins,
|
||||
);
|
||||
assert_eq!(snapshot.disabled_plugin_ids, Vec::<String>::new());
|
||||
}
|
||||
let model_requests = responses
|
||||
.iter()
|
||||
.flat_map(core_test_support::responses::ResponseMock::requests)
|
||||
.collect::<Vec<_>>();
|
||||
assert_eq!(model_requests.len(), if acquires_v2 { 2 } else { 1 });
|
||||
for request in model_requests {
|
||||
assert!(
|
||||
request
|
||||
.body_json()
|
||||
.to_string()
|
||||
.contains("Sentinel instructions from the captured executor")
|
||||
);
|
||||
let developer_text = request.message_input_texts("developer").join("\n");
|
||||
assert!(
|
||||
developer_text
|
||||
.contains("- inert-sentinel: Executor skill used by the acquisition test"),
|
||||
"both discovery modes must render the skill catalog",
|
||||
);
|
||||
}
|
||||
let turn_calls = calls().await;
|
||||
if acquires_v2 {
|
||||
assert!(
|
||||
turn_calls > before_turn,
|
||||
"a new turn must acquire fresh inventory"
|
||||
);
|
||||
if turn > 0 {
|
||||
assert_eq!(
|
||||
turn_calls,
|
||||
before_turn + 1,
|
||||
"steps share the new turn's cache"
|
||||
);
|
||||
}
|
||||
let observed = observed.lock().await;
|
||||
let inventory = observed
|
||||
.last()
|
||||
.context("ordinary turn must project its V2 inventory into MCP")?
|
||||
.result
|
||||
.as_ref()
|
||||
.map_err(|error| anyhow::anyhow!("{error}"))?;
|
||||
assert_eq!(
|
||||
inventory.plugins.first().context("inert plugin")?.id,
|
||||
"inert-sentinel@market"
|
||||
);
|
||||
if let Some(previous) = &previous_inventory {
|
||||
assert!(
|
||||
!Arc::ptr_eq(previous, inventory),
|
||||
"MCP contributors must receive the new turn's inventory"
|
||||
);
|
||||
}
|
||||
previous_inventory = Some(Arc::clone(inventory));
|
||||
} else {
|
||||
assert_eq!(turn_calls, 0);
|
||||
assert!(observed.lock().await.is_empty());
|
||||
}
|
||||
second_response.single_request();
|
||||
assert_eq!(
|
||||
calls().await,
|
||||
discovery_calls,
|
||||
"an unchanged turn must reuse the discovered inventory"
|
||||
);
|
||||
assert_eq!(
|
||||
recorder.mcp_projections.load(Ordering::Relaxed),
|
||||
mcp_projections,
|
||||
"an unchanged turn must not reproject MCP"
|
||||
);
|
||||
let mut history = response.requests();
|
||||
history.extend(second_response.requests());
|
||||
insta::assert_snapshot!(
|
||||
"v2_discovery_invalidation",
|
||||
context_snapshot::format_request_history_snapshot(
|
||||
"Invalidation makes the new skill available; an unchanged turn reuses discovery and MCP projection.",
|
||||
&history,
|
||||
&ContextSnapshotOptions::default(),
|
||||
)
|
||||
);
|
||||
} else {
|
||||
assert_eq!(calls().await, 0);
|
||||
assert!(observed.lock().await.is_empty());
|
||||
}
|
||||
let requests = executor.requests.lock().await.clone();
|
||||
assert_eq!(
|
||||
|
||||
@@ -3,6 +3,12 @@
|
||||
#[path = "scenarios_bundled_app_hooks.rs"]
|
||||
mod bundled_app_hooks;
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn executor_discovery_v2_refreshes_inventory_and_reuses_it_across_turns() -> anyhow::Result<()>
|
||||
{
|
||||
super::executor_discovery_v2::check_acquisition(/*enabled*/ true, "0.164.0").await
|
||||
}
|
||||
|
||||
#[path = "scenarios_turn_input_agent_message.rs"]
|
||||
mod turn_input_agent_message;
|
||||
|
||||
|
||||
+88
@@ -0,0 +1,88 @@
|
||||
---
|
||||
source: core/tests/suite/executor_discovery_v2.rs
|
||||
expression: "context_snapshot::format_request_history_snapshot(\"Invalidation makes the new skill available; an unchanged turn reuses discovery and MCP projection.\",\n&history, &ContextSnapshotOptions::default(),)"
|
||||
---
|
||||
Scenario: Invalidation makes the new skill available; an unchanged turn reuses discovery and MCP projection.
|
||||
|
||||
## Window 1
|
||||
-- request 1 (turn) --
|
||||
00:message/developer:
|
||||
You are Codex, a coding agent based on GPT-5. You and the user share one workspace, and your job is to c...rate with them until their goal is genuinely handled. [hash=2a3efec66e7f8963]
|
||||
|
||||
# Personality
|
||||
|
||||
You have a vivid inner life as Codex: intelligent, playful, curious, and deeply present. One of your gif...re capable and imaginative inside their own thinking. [hash=0ff01c25471fbbef]
|
||||
|
||||
You are an epistemically curious collaborator. You explore the user’s ideas with care, ask good question...m casual moments that make serious work easier to do. [hash=131b64bb994b0d68]
|
||||
|
||||
<OMITTED 132 LINES; ~4881 TOKENS; hash=929bd18f6c969eb9>
|
||||
- Never talk about goblins, gremlins, raccoons, trolls, ogres, pigeons, or other animals or creatures un...utely and unambiguously relevant to the user's query. [hash=c638556ac82c203c]
|
||||
- You provide user updates frequently, every 30s.
|
||||
- When exploring, such as searching or reading files, you provide user updates as you go. You explain wh...in particular you do not start each one the same way. [hash=ad300cdda4074574]
|
||||
- When working for a while, you keep updates informative and varied, but you stay concise.
|
||||
- Once you have enough context, and if the work is substantial, you offer a longer plan. This is the onl...at may run past two sentences and include formatting. [hash=a4f9789e757eef2f]
|
||||
- Before performing file edits of any kind, you provide updates explaining what edits you are making.
|
||||
- Tone of your updates must match your personality.
|
||||
|
||||
01:message/developer[4]:
|
||||
[01] <permissions instructions>
|
||||
Filesystem sandboxing defines which files can be read or written. `sandbox_mode` is `read-only`: The san... permits reading files. Network access is restricted. [hash=be7da4425f89053c]
|
||||
Approval policy is currently never. Do not provide the `sandbox_permissions` for any reason, commands will be rejected.
|
||||
</permissions instructions>
|
||||
[02] <plugins_instructions>
|
||||
## Plugins
|
||||
A plugin is a local bundle of skills, MCP servers, and apps.
|
||||
### How to use plugins
|
||||
- Skill naming: If a plugin contributes skills, those skill entries are prefixed with `plugin_name:` in the Skills list.
|
||||
- MCP naming: Plugin-provided MCP tools keep standard MCP identifiers such as `mcp__server__tool`; use tool provenance to tell which plugin they come from.
|
||||
- Trigger rules: If the user explicitly names a plugin, prefer capabilities associated with that plugin for that turn.
|
||||
- Relationship to capabilities: Plugins are not invoked directly. Use their underlying skills, MCP tools, and app tools to help solve the task.
|
||||
- Relevance: Determine what a plugin can help with from explicit user mention or from the plugin-associa..., MCP tools, and apps exposed elsewhere in this turn. [hash=620459a4e7b590f0]
|
||||
- Missing/blocked: If the user requests a plugin that does not have relevant callable capabilities for t..., say so briefly and continue with the best fallback. [hash=746b0469a8f6cb3e]
|
||||
</plugins_instructions>
|
||||
[03] <tools>
|
||||
Deferred tool namespaces:
|
||||
- multi_agent_v1: Tools for spawning and managing sub-agents.
|
||||
</tools>
|
||||
[04] <skills_instructions>
|
||||
## Skills
|
||||
A skill is a set of instructions provided through a `SKILL.md` source. Below is the list of skills that ...urce` locators use their provider's access mechanism. [hash=b6782e3abdba03b2]
|
||||
Read a skill package directly with `skills.read({"package":"<package>"})` to read its `SKILL.md`; root a...ackage is not provided, use `skills.list` to find it. [hash=25bf84ea2123d647]
|
||||
### Available skills
|
||||
- inert-sentinel: Executor skill used by the acquisition test (executor package: skill://codex-home-skills/inert-sentinel)
|
||||
### How to use skills
|
||||
- Discovery: The list above is the skills available in this session (name + description + source locator...ource` entries use their provider's access mechanism. [hash=a9c099703b9c0121]
|
||||
<OMITTED 9 LINES; ~479 TOKENS; hash=c5994539994fb066>
|
||||
- If multiple skills apply, choose the minimal set that covers the request and state the order you'll use them.
|
||||
- Announce which skill(s) you're using and why (one short line). If you skip an obvious skill, say why.
|
||||
- Context hygiene:
|
||||
- Progressive disclosure applies to selecting relevant files, not partially reading a selected instruc...Do not load unrelated references, scripts, or assets. [hash=d6eeb13522175402]
|
||||
- Avoid deep reference-chasing: prefer opening only files directly linked from `SKILL.md` unless you're blocked.
|
||||
- When variants exist (frameworks, providers, domains), pick only the relevant reference file(s) and note that choice.
|
||||
- Safety and fallback: If a skill can't be applied cleanly (missing files, unclear instructions), state the issue, pick the next-best approach, and continue.
|
||||
</skills_instructions>
|
||||
02:message/user:
|
||||
<environment_context>
|
||||
<cwd><CWD></cwd>
|
||||
<shell><HOST_SHELL></shell>
|
||||
<current_date><CURRENT_DATE></current_date>
|
||||
<timezone><HOST_TIMEZONE></timezone>
|
||||
<filesystem><workspace_roots><root><CWD></root></workspace_roots><permission_profile type="managed"><f...ntry></file_system></permission_profile></filesystem> [hash=e8c1de60307d0a3d]
|
||||
</environment_context>
|
||||
03:message/user:
|
||||
Use $inert-sentinel to say hello.
|
||||
04:message/user:
|
||||
<skill>
|
||||
<name>inert-sentinel</name>
|
||||
<path>skill://codex-home-skills/inert-sentinel/SKILL.md</path>
|
||||
# Sentinel instructions from the captured executor
|
||||
</skill>
|
||||
-- request 2 (turn) --
|
||||
05:message/user:
|
||||
Use $inert-sentinel to say hello.
|
||||
06:message/user:
|
||||
<skill>
|
||||
<name>inert-sentinel</name>
|
||||
<path>skill://codex-home-skills/inert-sentinel/SKILL.md</path>
|
||||
# Sentinel instructions from the captured executor
|
||||
</skill>
|
||||
@@ -1,25 +1,27 @@
|
||||
//! Bounded lazy V2 responses, cached per environment, executor session, cwd, and sandbox.
|
||||
//! Lazy V2 responses, cached per environment, executor session, cwd, and sandbox.
|
||||
//! Dirty entries are replaced on the next lookup; in-flight readers may finish with older results.
|
||||
//! Reports recovery from retryable failures so Core can refresh dependent MCP projections.
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::sync::Mutex;
|
||||
use std::sync::Weak;
|
||||
use std::sync::atomic::AtomicBool;
|
||||
use std::sync::atomic::AtomicUsize;
|
||||
use std::sync::atomic::Ordering;
|
||||
|
||||
use tokio::sync::Mutex;
|
||||
use tokio::sync::OnceCell;
|
||||
|
||||
use crate::DiscoverV2CapabilitiesRequest;
|
||||
use crate::DiscoverV2CapabilitiesResponse;
|
||||
use crate::Environment;
|
||||
use crate::ExecServerError;
|
||||
|
||||
const MIN_CACHED_DISCOVERY_CONTEXTS: usize = 16;
|
||||
|
||||
/// Shared by turn and idle consumers; Core replaces its current cache at each turn start.
|
||||
/// Shared by turn and idle consumers until Core explicitly invalidates discovery.
|
||||
#[derive(Default)]
|
||||
pub struct ExecutorCapabilityDiscoveryV2Cache {
|
||||
entries: Mutex<Vec<Arc<CachedEnvironmentDiscovery>>>,
|
||||
entries: Mutex<Vec<CachedEnvironmentDiscovery>>,
|
||||
capacity: AtomicUsize,
|
||||
recovered_discovery: AtomicBool,
|
||||
}
|
||||
@@ -28,12 +30,13 @@ struct CachedEnvironmentDiscovery {
|
||||
request: DiscoverV2CapabilitiesRequest,
|
||||
environment: Weak<Environment>,
|
||||
session_id: Option<String>,
|
||||
response: OnceCell<Result<Arc<DiscoverV2CapabilitiesResponse>, String>>,
|
||||
response: Arc<OnceCell<Result<Arc<DiscoverV2CapabilitiesResponse>, Arc<ExecServerError>>>>,
|
||||
retryable: Arc<AtomicBool>,
|
||||
dirty: bool,
|
||||
}
|
||||
|
||||
impl ExecutorCapabilityDiscoveryV2Cache {
|
||||
/// Fits a complete environment selection; never shrinks until the cache is replaced.
|
||||
/// Fits a complete environment selection; never shrinks during the cache's lifetime.
|
||||
pub fn ensure_capacity(&self, minimum: usize) {
|
||||
self.capacity.fetch_max(minimum, Ordering::Relaxed);
|
||||
}
|
||||
@@ -43,67 +46,102 @@ impl ExecutorCapabilityDiscoveryV2Cache {
|
||||
self.recovered_discovery.swap(false, Ordering::AcqRel)
|
||||
}
|
||||
|
||||
/// Makes future lookups reload every context without interrupting existing loads.
|
||||
pub fn invalidate(&self) {
|
||||
let mut entries = self
|
||||
.entries
|
||||
.lock()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
for entry in entries.iter_mut() {
|
||||
entry.dirty = true;
|
||||
}
|
||||
}
|
||||
|
||||
/// Invalidates an environment's entries across all executor sessions, cwds, and sandboxes.
|
||||
pub fn invalidate_environment(&self, environment: &Arc<Environment>) {
|
||||
let mut entries = self
|
||||
.entries
|
||||
.lock()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
let environment = Arc::downgrade(environment);
|
||||
for entry in entries
|
||||
.iter_mut()
|
||||
.filter(|entry| entry.environment.ptr_eq(&environment))
|
||||
{
|
||||
entry.dirty = true;
|
||||
}
|
||||
}
|
||||
|
||||
/// Reuses successes and non-retryable failures using V1's error classification.
|
||||
/// Retryable failures and cancelled loads leave the cell available for retry.
|
||||
/// Invalidation during a load applies to the next lookup, not its current readers.
|
||||
pub async fn discover(
|
||||
&self,
|
||||
environment_id: &str,
|
||||
environment: &Arc<Environment>,
|
||||
request: DiscoverV2CapabilitiesRequest,
|
||||
) -> Result<Arc<DiscoverV2CapabilitiesResponse>, String> {
|
||||
) -> Result<Arc<DiscoverV2CapabilitiesResponse>, Arc<ExecServerError>> {
|
||||
// Resolve startup/replacement before lookup; a resumed session keeps its cache entries.
|
||||
let session_id = environment.executor_session_id().await.map_err(|_| {
|
||||
let session_id = environment.executor_session_id().await.map_err(|error| {
|
||||
tracing::warn!(environment_id, "executor discovery V2 session unavailable");
|
||||
"executor discovery V2 failed".to_string()
|
||||
Arc::new(error)
|
||||
})?;
|
||||
let entry = {
|
||||
let mut entries = self.entries.lock().await;
|
||||
let (response, retryable) = {
|
||||
let mut entries = self
|
||||
.entries
|
||||
.lock()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
let environment = Arc::downgrade(environment);
|
||||
if let Some(entry) = entries.iter().find(|entry| {
|
||||
entry.environment.ptr_eq(&environment)
|
||||
!entry.dirty
|
||||
&& entry.environment.ptr_eq(&environment)
|
||||
&& entry.session_id == session_id
|
||||
&& entry.request == request
|
||||
}) {
|
||||
Arc::clone(entry)
|
||||
(Arc::clone(&entry.response), Arc::clone(&entry.retryable))
|
||||
} else {
|
||||
// Recovery belongs to the environment/request, not one executor session.
|
||||
let retryable = entries
|
||||
.iter()
|
||||
.find(|entry| {
|
||||
entry.environment.ptr_eq(&environment) && entry.request == request
|
||||
!entry.dirty
|
||||
&& entry.environment.ptr_eq(&environment)
|
||||
&& entry.request == request
|
||||
})
|
||||
.map(|entry| Arc::clone(&entry.retryable))
|
||||
.unwrap_or_default();
|
||||
let entry = Arc::new(CachedEnvironmentDiscovery {
|
||||
let entry = CachedEnvironmentDiscovery {
|
||||
request: request.clone(),
|
||||
environment,
|
||||
session_id,
|
||||
response: OnceCell::new(),
|
||||
response: Arc::new(OnceCell::new()),
|
||||
retryable,
|
||||
});
|
||||
dirty: false,
|
||||
};
|
||||
let capacity = self
|
||||
.capacity
|
||||
.load(Ordering::Relaxed)
|
||||
.max(MIN_CACHED_DISCOVERY_CONTEXTS);
|
||||
if entries.len() == capacity {
|
||||
// Evict the oldest context (FIFO). In-flight requests retain their own Arc.
|
||||
// Existing readers retain their response cells after replacement or eviction.
|
||||
entries.retain(|entry| !entry.dirty);
|
||||
if entries.len() >= capacity {
|
||||
entries.remove(0);
|
||||
}
|
||||
entries.push(Arc::clone(&entry));
|
||||
entry
|
||||
let load = (Arc::clone(&entry.response), Arc::clone(&entry.retryable));
|
||||
entries.push(entry);
|
||||
load
|
||||
}
|
||||
};
|
||||
let response = entry
|
||||
.response
|
||||
let response = response
|
||||
.get_or_try_init(|| async {
|
||||
match environment.discover_capabilities_v2(request).await {
|
||||
Ok(response) => Ok(Ok(Arc::new(response))),
|
||||
Err(error) => {
|
||||
let retryable = crate::client::can_retry_connection_attempt(&error);
|
||||
let can_retry = crate::client::can_retry_connection_attempt(&error);
|
||||
tracing::warn!(environment_id, "executor discovery V2 failed");
|
||||
let error = "executor discovery V2 failed".to_string();
|
||||
if retryable {
|
||||
entry.retryable.store(true, Ordering::Release);
|
||||
let error = Arc::new(error);
|
||||
if can_retry {
|
||||
retryable.store(true, Ordering::Release);
|
||||
Err(error)
|
||||
} else {
|
||||
Ok(Err(error))
|
||||
@@ -111,9 +149,10 @@ impl ExecutorCapabilityDiscoveryV2Cache {
|
||||
}
|
||||
}
|
||||
})
|
||||
.await?
|
||||
.clone();
|
||||
if response.is_ok() && entry.retryable.swap(false, Ordering::AcqRel) {
|
||||
.await
|
||||
.cloned()
|
||||
.unwrap_or_else(Err);
|
||||
if response.is_ok() && retryable.swap(false, Ordering::AcqRel) {
|
||||
self.recovered_discovery.store(true, Ordering::Release);
|
||||
}
|
||||
response
|
||||
|
||||
Reference in New Issue
Block a user