Trace MCP refresh causes and plugin metadata changes (#60505)

## What changed

- Track and log `McpRefreshReason` values for runtime refreshes, including invalidations received during thread startup and forced refreshes. Restore claimed reasons when a refresh is cancelled before publication, while preserving concurrent invalidations.
- Correlate installed-plugin metadata invalidations with downstream MCP refreshes using a shared change ID. Log changed field names by plugin ID, additions, removals, and reordering without logging field values.
- Record whether background installed-plugin bundle updates were requested by startup, plugin listing, or installation.

## Testing

Add coverage for metadata change attribution across membership changes and reordering, and for retaining refresh causes across cancellation and concurrent invalidation.

GitOrigin-RevId: 3d4485ccfcc1d8788eb9909a060ac200c467af13
This commit is contained in:
Charlie Marsh
2026-11-10 02:45:30 +00:00
committed by copyberry
parent 0e6f153732
commit aa39ded153
28 changed files with 534 additions and 196 deletions
@@ -1,3 +1,4 @@
use codex_core::McpRefreshReason;
use std::collections::BTreeSet;
use std::sync::Arc;
@@ -32,8 +33,14 @@ pub(crate) fn effective_plugins_changed_callback(
thread_manager.skills_service().clear_cache();
let refresh_thread_manager = Arc::clone(&thread_manager);
let reason = change.metadata_change_id.map_or(
McpRefreshReason::EffectivePluginsChanged,
McpRefreshReason::InstalledPluginMetadata,
);
tokio::spawn(async move {
refresh_thread_manager.invalidate_mcp_runtimes().await;
refresh_thread_manager
.invalidate_mcp_runtimes_for(reason)
.await;
refresh_thread_manager.refresh_hook_runtimes().await;
});
@@ -10,6 +10,7 @@ use crate::outgoing_message::AccountNotification;
use chrono::DateTime;
use codex_app_server_protocol::DesktopOnboardingEntrypoint;
use codex_app_server_protocol::GetAccountRateLimitsParams;
use codex_core::McpRefreshReason;
use codex_login::LoginOnboardingEntrypoint;
use codex_login::login_with_bedrock_access_keys;
use codex_mcp::ema_auth_scope;
@@ -297,16 +298,21 @@ impl AccountRequestProcessor {
Self::spawn_effective_plugins_changed_task(
Arc::clone(thread_manager),
config_manager.clone(),
McpRefreshReason::AccountPluginCacheChanged,
);
let plugins_config = config.plugins_config_input();
let refresh_thread_manager = Arc::clone(thread_manager);
let refresh_config_manager = config_manager.clone();
let on_effective_plugins_changed: Arc<
dyn Fn(codex_core_plugins::EffectivePluginsChange) + Send + Sync,
> = Arc::new(move |_change| {
> = Arc::new(move |change| {
Self::spawn_effective_plugins_changed_task(
Arc::clone(&refresh_thread_manager),
refresh_config_manager.clone(),
change.metadata_change_id.map_or(
McpRefreshReason::AccountPluginCacheChanged,
McpRefreshReason::InstalledPluginMetadata,
),
);
});
thread_manager
@@ -338,13 +344,14 @@ impl AccountRequestProcessor {
fn spawn_effective_plugins_changed_task(
thread_manager: Arc<ThreadManager>,
config_manager: ConfigManager,
reason: McpRefreshReason,
) {
tokio::spawn(async move {
thread_manager.plugins_manager().clear_cache();
thread_manager.skills_service().clear_cache();
crate::mcp_refresh::reload_mcp_config_best_effort(&thread_manager, &config_manager)
.await;
thread_manager.invalidate_mcp_runtimes().await;
thread_manager.invalidate_mcp_runtimes_for(reason).await;
});
}
@@ -954,7 +961,9 @@ impl AccountRequestProcessor {
}
async fn send_login_success_notifications(&self, login_id: Option<Uuid>) {
self.thread_manager.invalidate_mcp_runtimes().await;
self.thread_manager
.invalidate_mcp_runtimes_for(McpRefreshReason::AccountLoginCompleted)
.await;
self.send_account_login_notifications(AccountLoginCompletedNotification {
login_id: login_id.map(|id| id.to_string()),
success: true,
@@ -1016,7 +1025,9 @@ impl AccountRequestProcessor {
self.auth_manager.reload().await;
let auth_changes = self.auth_manager.auth_change_state_receiver();
let owner_generation = auth_changes.borrow().owner_generation;
self.thread_manager.invalidate_mcp_runtimes().await;
self.thread_manager
.invalidate_mcp_runtimes_for(McpRefreshReason::AccountAuthReloaded)
.await;
self.config_manager
.update_managed_config_loader(self.auth_manager.clone(), &self.config);
self.config_manager
@@ -1041,7 +1052,9 @@ impl AccountRequestProcessor {
let config =
logout_configured_account(&self.config_manager, &self.auth_manager, &self.config)
.await?;
self.thread_manager.invalidate_mcp_runtimes().await;
self.thread_manager
.invalidate_mcp_runtimes_for(McpRefreshReason::AccountLoggedOut)
.await;
clear_account_configuration(&self.config_manager, &config).await?;
self.model_catalog.invalidate().await;
@@ -8,6 +8,7 @@ use codex_app_server_protocol::AccountSessionsListParams;
use codex_app_server_protocol::AccountSessionsResponse;
use codex_app_server_protocol::AccountSessionsSwitchParams;
use codex_app_server_protocol::AccountSessionsSwitchResponse;
use codex_core::McpRefreshReason;
impl AccountRequestProcessor {
pub(crate) async fn list_account_sessions(
@@ -204,7 +205,9 @@ impl AccountRequestProcessor {
"Workspace validation failed: {error}"
)));
}
self.thread_manager.invalidate_mcp_runtimes().await;
self.thread_manager
.invalidate_mcp_runtimes_for(McpRefreshReason::AccountWorkspaceSwitched)
.await;
self.model_catalog.invalidate().await;
Self::maybe_refresh_plugin_caches_for_current_config(
&self.config_manager,
@@ -1,5 +1,6 @@
use super::*;
use crate::plugin_config_reload;
use codex_core::McpRefreshReason;
#[derive(Clone)]
pub(crate) struct MarketplaceRequestProcessor {
@@ -89,7 +90,9 @@ impl MarketplaceRequestProcessor {
if !outcome.upgraded_roots.is_empty() {
self.thread_manager.plugins_manager().clear_cache();
self.thread_manager.skills_service().clear_cache();
self.thread_manager.invalidate_mcp_runtimes().await;
self.thread_manager
.invalidate_mcp_runtimes_for(McpRefreshReason::MarketplaceUpgraded)
.await;
self.thread_manager.refresh_hook_runtimes().await;
}
@@ -3,6 +3,7 @@ use super::account_processor::EnterpriseLoginTarget;
use super::thread_input::ensure_direct_input_allowed;
use super::*;
use codex_config::types::McpServerAuth;
use codex_core::McpRefreshReason;
use codex_mcp::McpServerSource;
use codex_mcp::ReadResourceRequestParams;
use codex_mcp::ema_auth_scope;
@@ -351,7 +352,9 @@ impl McpRequestProcessor {
Err(err) => (false, Some(err.to_string())),
};
if success && !enterprise {
thread_manager.invalidate_mcp_runtimes().await;
thread_manager
.invalidate_mcp_runtimes_for(McpRefreshReason::McpOAuthLoginCompleted)
.await;
}
let notification = ServerNotification::McpServerOauthLoginCompleted(
@@ -9,6 +9,7 @@ use codex_app_server_protocol::PluginSharePrincipalRole;
use codex_app_server_protocol::PluginShareTargetRole;
use codex_config::types::McpServerConfig;
use codex_config::types::PluginsConfigToml;
use codex_core::McpRefreshReason;
use codex_core_plugins::OPENAI_CURATED_MARKETPLACE_NAME;
use codex_core_plugins::PluginListBackgroundTaskOptions;
use codex_core_plugins::PluginMarketplaceContext;
@@ -533,9 +534,11 @@ impl PluginRequestProcessor {
Arc::clone(&self.on_effective_plugins_changed)
}
async fn on_effective_plugins_changed(&self) {
async fn on_effective_plugins_changed(&self, reason: McpRefreshReason) {
self.clear_plugin_related_caches();
self.thread_manager.invalidate_mcp_runtimes().await;
self.thread_manager
.invalidate_mcp_runtimes_for(reason)
.await;
self.thread_manager.refresh_hook_runtimes().await;
}
@@ -596,7 +599,8 @@ impl PluginRequestProcessor {
.refresh_non_curated_plugin_cache_for_context(&context)
.await
{
self.on_effective_plugins_changed().await;
self.on_effective_plugins_changed(McpRefreshReason::NonCuratedPluginCacheRefreshed)
.await;
}
let include_vertical = context.global_config.plugins_enabled
&& marketplace_kinds.contains(&PluginListMarketplaceKind::Vertical);
@@ -875,6 +879,7 @@ impl PluginRequestProcessor {
&context.global_config,
auth.clone(),
Some(self.effective_plugins_changed_callback()),
codex_core_plugins::InstalledPluginSyncSource::PluginInstalled,
);
let (mut data, marketplace_load_errors) = self
@@ -1584,7 +1589,9 @@ impl PluginRequestProcessor {
self.clear_plugin_related_caches();
reload_user_config(&self.config_manager, &self.thread_manager).await;
self.thread_manager.invalidate_mcp_runtimes().await;
self.thread_manager
.invalidate_mcp_runtimes_for(McpRefreshReason::PluginInstalled)
.await;
self.thread_manager.refresh_hook_runtimes().await;
let plugin_mcp_servers = load_configured_plugin_mcp_servers(
@@ -2037,7 +2044,9 @@ impl PluginRequestProcessor {
Err(err) => (false, Some(err.to_string())),
};
if success {
thread_manager.invalidate_mcp_runtimes().await;
thread_manager
.invalidate_mcp_runtimes_for(McpRefreshReason::PluginOAuthLoginCompleted)
.await;
}
let notification = ServerNotification::McpServerOauthLoginCompleted(
@@ -2074,7 +2083,10 @@ impl PluginRequestProcessor {
.await
.map_err(Self::plugin_uninstall_error)?;
match self.load_latest_config(/*fallback_cwd*/ None).await {
Ok(_) => self.on_effective_plugins_changed().await,
Ok(_) => {
self.on_effective_plugins_changed(McpRefreshReason::PluginUninstalled)
.await
}
Err(err) => {
warn!(
"failed to reload config after plugin uninstall, clearing plugin-related caches only: {err:?}"
@@ -2169,7 +2181,8 @@ impl PluginRequestProcessor {
self.analytics_events_client
.track_plugin_uninstalled(outcome.telemetry);
if outcome.effective_plugins_changed {
self.on_effective_plugins_changed().await;
self.on_effective_plugins_changed(McpRefreshReason::RemotePluginUninstalled)
.await;
}
if let Some(err) = outcome.cache_removal_error {
return Err(remote_plugin_catalog_error_to_jsonrpc(