Prewarm MCP servers during cloud catalog refresh (#62876)

## Why

A slow cloud skill lookup can delay MCP startup even after the plugin
catalog is ready. If the refresh is canceled while waiting for skills,
it can miss the chance to prewarm those servers before the first turn.

## What changed

Queue MCP prewarm as soon as plugin discovery changes or clears the
catalog. Skill discovery alone leaves the MCP server set unchanged.

At the end of the refresh, queue a retry for eligible MCP startup
failures. If discovery is canceled, check for those failures the next
time the catalog lock is acquired. Reuse pending handshakes and leave
authentication failures and Apps-managed reconnects to their existing
recovery paths.

## Testing

Add regression tests for first-turn tool availability after cancellation,
startup recovery with cloud plugins disabled, and a single retry for a
persistent startup failure.

GitOrigin-RevId: f92162d1629358953d8810d20244e55f07416a01
This commit is contained in:
Jay Chen
2026-11-21 00:54:05 +00:00
committed by copyberry
parent 0bfb8467da
commit 66618f0512
16 changed files with 564 additions and 57 deletions
@@ -64,6 +64,7 @@ pub use turn_input::TurnInputContext;
pub use turn_input::TurnInputEnvironment;
pub use turn_lifecycle::CatalogRefreshInput;
pub use turn_lifecycle::CloudCatalogRefreshInput;
pub use turn_lifecycle::CloudCatalogRefreshOutcome;
pub use turn_lifecycle::TurnAbortInput;
pub use turn_lifecycle::TurnErrorInput;
pub use turn_lifecycle::TurnStartInput;
@@ -249,16 +250,16 @@ pub trait TurnLifecycleContributor: Send + Sync {
}
/// Publishes cloud catalogs after explicit invalidation, without requiring a turn.
/// Core marks MCP dirty after publication. Accepted complete or partial catalogs
/// replace the previous result; failed or superseded discovery returns an error.
/// Report whether MCP needs updating so Core can start clients while other
/// providers are still running.
/// Core invalidates resource keys before calling. MCP-backed catalogs record
/// the new key only on acceptance, keeping failed or cancelled refreshes stale.
/// Called under the same per-thread catalog lock as step preparation.
fn refresh_cloud_catalog<'a>(
&'a self,
_input: CloudCatalogRefreshInput<'a>,
) -> ExtensionFuture<'a, Result<(), String>> {
Box::pin(async { Ok(()) })
) -> ExtensionFuture<'a, CloudCatalogRefreshOutcome> {
Box::pin(async { CloudCatalogRefreshOutcome::UNCHANGED })
}
/// Observes a completed item without changing it or delaying streamed deltas.
@@ -58,6 +58,21 @@ impl<'a> From<CatalogRefreshInput<'a>> for CloudCatalogRefreshInput<'a> {
}
}
/// Result of explicit discovery and its effect on the thread's MCP servers.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CloudCatalogRefreshOutcome {
pub result: Result<(), String>,
/// Also true when discovery failed after clearing a stale plugin catalog.
pub needs_mcp_refresh: bool,
}
impl CloudCatalogRefreshOutcome {
pub const UNCHANGED: Self = Self {
result: Ok(()),
needs_mcp_refresh: false,
};
}
/// Input supplied when the host completes a turn.
pub struct TurnStopInput<'a> {
/// Store scoped to the host session runtime.
+1
View File
@@ -59,6 +59,7 @@ pub use contributors::ApprovalDecisionInput;
pub use contributors::ApprovalReviewContributor;
pub use contributors::CatalogRefreshInput;
pub use contributors::CloudCatalogRefreshInput;
pub use contributors::CloudCatalogRefreshOutcome;
pub use contributors::CodeModeCellTiming;
pub use contributors::CommandStartInput;
pub use contributors::ConfigContributor;
+15 -5
View File
@@ -12,6 +12,7 @@ use codex_core_plugins::PluginIdentity;
use codex_extension_api::CatalogRefreshBackoff;
use codex_extension_api::CatalogRefreshInput as StepCatalogRefreshInput;
use codex_extension_api::CloudCatalogRefreshInput;
use codex_extension_api::CloudCatalogRefreshOutcome;
use codex_extension_api::ConfigContributor;
use codex_extension_api::ExtensionData;
use codex_extension_api::ExtensionFuture;
@@ -232,17 +233,26 @@ impl TurnLifecycleContributor for PluginContributor {
fn refresh_cloud_catalog<'a>(
&'a self,
input: CloudCatalogRefreshInput<'a>,
) -> ExtensionFuture<'a, Result<(), String>> {
) -> ExtensionFuture<'a, CloudCatalogRefreshOutcome> {
Box::pin(async move {
let Some(state) = input.thread_store.get::<PluginsThreadState>() else {
return Ok(());
return CloudCatalogRefreshOutcome::UNCHANGED;
};
// A failed explicit attempt gets an immediate retry on the next step.
state.contributor_state().cloud_retry = None;
self.discover_cloud_catalog(&state, cloud_catalog_query(input, /*turn_id*/ None))
match self
.discover_cloud_catalog(&state, cloud_catalog_query(input, /*turn_id*/ None))
.await
.map(|_| ())
.map_err(|failure| failure.error)
{
Ok(needs_mcp_refresh) => CloudCatalogRefreshOutcome {
result: Ok(()),
needs_mcp_refresh,
},
Err(failure) => CloudCatalogRefreshOutcome {
result: Err(failure.error),
needs_mcp_refresh: failure.catalog_cleared,
},
}
})
}
}
+35 -8
View File
@@ -79,13 +79,13 @@ async fn cloud_source_policy_skips_startup_discovery() -> anyhow::Result<()> {
thread_store: &thread_store,
})
.await;
contributor
let outcome = contributor
.refresh_cloud_catalog(CloudCatalogRefreshInput {
session_store: &session_store,
thread_store: &thread_store,
})
.await
.expect("disabled discovery succeeds");
.await;
assert_eq!(outcome, CloudCatalogRefreshOutcome::UNCHANGED);
assert_eq!(provider.calls.load(Ordering::SeqCst), 0);
Ok(())
}
@@ -431,13 +431,40 @@ async fn explicit_cloud_refresh_replaces_cached_catalog() {
};
provider.set_reply(Ok(refreshed_catalog.clone()));
assert!(!contributor.refresh_catalog_for_step(input).await);
contributor
.refresh_cloud_catalog(input.into())
.await
.unwrap();
assert_eq!(state.cloud_catalog(), Some(refreshed_catalog));
let outcome = contributor.refresh_cloud_catalog(input.into()).await;
assert_eq!(
outcome,
CloudCatalogRefreshOutcome {
result: Ok(()),
needs_mcp_refresh: true
}
);
assert_eq!(state.cloud_catalog(), Some(refreshed_catalog.clone()));
assert!(!contributor.refresh_catalog_for_step(input).await);
assert_eq!(provider.calls.load(Ordering::SeqCst), 2);
provider.set_reply(Err("discovery failed".to_string()));
assert_eq!(
contributor.refresh_cloud_catalog(input.into()).await,
CloudCatalogRefreshOutcome {
result: Err("discovery failed".to_string()),
needs_mcp_refresh: false,
}
);
assert_eq!(state.cloud_catalog(), Some(refreshed_catalog));
// Switching auth scopes clears the old catalog before the failed lookup.
session.insert(McpResourceClient::new(Arc::new(
codex_mcp::McpRuntime::empty(/*prefix_mcp_tool_names*/ false),
)));
assert_eq!(
contributor.refresh_cloud_catalog(input.into()).await,
CloudCatalogRefreshOutcome {
result: Err("discovery failed".to_string()),
needs_mcp_refresh: true,
}
);
assert_eq!(state.cloud_catalog(), None);
}
struct CloudProvider {
+9 -4
View File
@@ -2,6 +2,7 @@
use codex_extension_api::CatalogRefreshInput as StepCatalogRefreshInput;
use codex_extension_api::CloudCatalogRefreshInput;
use codex_extension_api::CloudCatalogRefreshOutcome;
use codex_extension_api::ExtensionFuture;
use codex_extension_api::TurnLifecycleContributor;
use std::sync::Arc;
@@ -68,15 +69,19 @@ impl<C: Send + Sync + 'static> TurnLifecycleContributor for SkillsExtension<C> {
fn refresh_cloud_catalog<'a>(
&'a self,
input: CloudCatalogRefreshInput<'a>,
) -> ExtensionFuture<'a, Result<(), String>> {
) -> ExtensionFuture<'a, CloudCatalogRefreshOutcome> {
Box::pin(async move {
let Some((state, query)) = cloud_catalog_query(input, /*turn_id*/ None) else {
return Ok(());
return CloudCatalogRefreshOutcome::UNCHANGED;
};
state
let result = state
.refresh_cloud_catalog(&self.providers, query)
.await
.map_err(|error| error.to_string())
.map_err(|error| error.to_string());
CloudCatalogRefreshOutcome {
result,
needs_mcp_refresh: false,
}
})
}
}
+23 -8
View File
@@ -2713,18 +2713,33 @@ async fn cloud_discovery_is_gated_and_reports_failures() -> TestResult {
refresh_registered_catalogs_for_step(&registry, &session_store, &thread_store, "turn-3")
.await;
assert_eq!(list_calls.load(Ordering::Relaxed), 2);
for contributor in registry.turn_lifecycle_contributors() {
contributor
.refresh_cloud_catalog(codex_extension_api::CloudCatalogRefreshInput {
session_store: &session_store,
thread_store: &thread_store,
})
.await
.map_err(|error| format!("partial catalogs should be accepted: {error}"))?;
let input = codex_extension_api::CloudCatalogRefreshInput {
session_store: &session_store,
thread_store: &thread_store,
};
let contributors = registry.turn_lifecycle_contributors();
for contributor in contributors {
assert_eq!(
contributor.refresh_cloud_catalog(input).await,
codex_extension_api::CloudCatalogRefreshOutcome::UNCHANGED
);
}
refresh_registered_catalogs_for_step(&registry, &session_store, &thread_store, "turn-4")
.await;
assert_eq!(list_calls.load(Ordering::Relaxed), 3);
// Re-arm the provider's first-call failure for an explicit refresh.
list_calls.store(0, Ordering::Relaxed);
let mut errors = Vec::new();
for contributor in contributors {
let outcome = contributor.refresh_cloud_catalog(input).await;
assert!(!outcome.needs_mcp_refresh);
errors.extend(outcome.result.err());
}
assert_eq!(
errors,
["cloud skills unavailable: temporary cloud failure"]
);
}
Ok(())
}