From 9a56c2b1aa994f696b144ba863200f4b73a74c0a Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Tue, 17 Nov 2026 19:08:11 +0000 Subject: [PATCH] Preserve plugin runtimes across catalog metadata updates (#62105) ## Why An advertised plugin release or installation metadata change should preserve live MCP sessions and cached skills while the active local bundle remains unchanged, including when an upgrade fails. ## What changed - Compare runtime identity, enablement, connector ownership, and the active bundle separately from catalog release, policy, and display metadata. - Track the active bundle's directory version and persisted remote plugin ID so changes made by another process refresh the runtime. - Retain pending runtime notifications until a host callback is available and deliver them through `plugin/reconcile`. - Preserve cancellation recovery across retries while avoiding unnecessary runtime invalidation for catalog fetch failures. ## Testing Add unit and app-server regression coverage for catalog-only updates, failed upgrades, externally activated bundles, deferred notifications, and reconciliation recovery. GitOrigin-RevId: ab20012c1bfd760adea55614f49fbbc7ef86fe8b --- .../request_processors/plugins/reconcile.rs | 6 +- .../mcp_resource/plugin_metadata_refresh.rs | 158 ++++++++++++++- codex-rs/core-plugins/src/manager.rs | 188 +++++++++++++----- codex-rs/core-plugins/src/manager_tests.rs | 107 +++++++++- codex-rs/core-plugins/src/remote.rs | 8 + codex-rs/core-plugins/src/remote_metadata.rs | 114 +++++++---- .../src/remote_metadata_cache_tests.rs | 8 + .../core-plugins/src/remote_metadata_tests.rs | 40 +++- 8 files changed, 529 insertions(+), 100 deletions(-) diff --git a/codex-rs/app-server/src/request_processors/plugins/reconcile.rs b/codex-rs/app-server/src/request_processors/plugins/reconcile.rs index 775c7dc7dc..9f48efb639 100644 --- a/codex-rs/app-server/src/request_processors/plugins/reconcile.rs +++ b/codex-rs/app-server/src/request_processors/plugins/reconcile.rs @@ -32,7 +32,11 @@ impl PluginRequestProcessor { let outcome = self .thread_manager .plugins_manager() - .reconcile_remote_installed_plugins(&plugins_input, auth.as_ref()) + .reconcile_remote_installed_plugins_with_notify( + &plugins_input, + auth.as_ref(), + Some(self.effective_plugins_changed_callback()), + ) .await .map_err(|err| { internal_error(format!( diff --git a/codex-rs/app-server/tests/suite/v2/mcp_resource/plugin_metadata_refresh.rs b/codex-rs/app-server/tests/suite/v2/mcp_resource/plugin_metadata_refresh.rs index 0060cb3354..8aebfc6df3 100644 --- a/codex-rs/app-server/tests/suite/v2/mcp_resource/plugin_metadata_refresh.rs +++ b/codex-rs/app-server/tests/suite/v2/mcp_resource/plugin_metadata_refresh.rs @@ -14,7 +14,7 @@ use pretty_assertions::assert_eq; use tokio::sync::RwLock; #[tokio::test(flavor = "multi_thread", worker_threads = 2)] -async fn signed_image_renewal_preserves_live_mcp_and_plugin_skills() -> Result<()> { +async fn catalog_metadata_updates_preserve_live_mcp_and_plugin_skills() -> Result<()> { let responses_server = responses::start_mock_server().await; let listener = TcpListener::bind("127.0.0.1:0").await?; let base_url = format!("http://{}", listener.local_addr()?); @@ -156,9 +156,64 @@ async fn signed_image_renewal_preserves_live_mcp_and_plugin_skills() -> Result<( warm_skills ); + installed.write().await["installed_at"] = json!("2026-11-16T12:00:00Z"); + refresh_and_expect_logo(&mut app_server, renewed_logo).await?; + read_mcp_skill_resource(&mut app_server, &thread.id).await?; + assert_eq!(sessions.load(Ordering::SeqCst), warm_sessions); + + { + let mut plugin = installed.write().await; + plugin["release"]["version"] = json!("1.1.0"); + plugin["release"]["bundle_download_url"] = json!(format!("{base_url}/missing-bundle")); + } + let request = app_server + .send_raw_request("plugin/installed", Some(json!({}))) + .await?; + let _: PluginInstalledResponse = + timeout(DEFAULT_READ_TIMEOUT, app_server.read_response(request)).await??; + let request = app_server + .send_raw_request("plugin/reconcile", Some(json!({}))) + .await?; + let reconciled: PluginReconcileResponse = + timeout(DEFAULT_READ_TIMEOUT, app_server.read_response(request)).await??; + assert!(reconciled.changed_plugins.is_empty()); + assert_eq!( + reconciled.failed_remote_plugin_ids, + vec!["plugins~Plugin_00000000000000000000000000000000"] + ); + let request = app_server + .send_raw_request("plugin/installed", Some(json!({}))) + .await?; + let response: PluginInstalledResponse = + timeout(DEFAULT_READ_TIMEOUT, app_server.read_response(request)).await??; + let plugin = response + .marketplaces + .iter() + .flat_map(|marketplace| &marketplace.plugins) + .find(|plugin| plugin.name == "demo-plugin") + .context("installed plugin should remain visible after an advertised upgrade")?; + assert_eq!(plugin.version.as_deref(), Some("1.1.0")); + assert_eq!(plugin.local_version.as_deref(), Some("1.0.0")); + let request = app_server + .send_raw_request("plugin/reconcile", Some(json!({}))) + .await?; + let _: PluginReconcileResponse = + timeout(DEFAULT_READ_TIMEOUT, app_server.read_response(request)).await??; + read_mcp_skill_resource(&mut app_server, &thread.id).await?; + assert_eq!(sessions.load(Ordering::SeqCst), warm_sessions); + assert_eq!( + list_plugin_skills(&mut app_server, codex_home.path()).await?, + warm_skills + ); + // Runtime metadata must update the cached skill catalog without changing the bundle. - installed.write().await["release"]["interface"]["logo_url"] = json!(changed_logo); - installed.write().await["enabled"] = json!(false); + { + let mut plugin = installed.write().await; + plugin["release"]["version"] = json!("1.0.0"); + plugin["release"]["bundle_download_url"] = json!(format!("{base_url}/bundle")); + plugin["release"]["interface"]["logo_url"] = json!(changed_logo); + plugin["enabled"] = json!(false); + } refresh_and_expect_logo(&mut app_server, changed_logo).await?; assert!( list_plugin_skills(&mut app_server, codex_home.path()) @@ -167,15 +222,110 @@ async fn signed_image_renewal_preserves_live_mcp_and_plugin_skills() -> Result<( ); read_mcp_skill_resource(&mut app_server, &thread.id).await?; assert_eq!(sessions.load(Ordering::SeqCst), warm_sessions); - assert_eq!(calls.snapshot().main_prompt_reads, 4); + assert_eq!(calls.snapshot().main_prompt_reads, 6); assert_eq!( std::fs::read_to_string(warm_skills[0].path.as_str())?, SKILL_CONTENTS ); + // Restore the plugin, then simulate a different process activating a new bundle. + let plugin_root = codex_home + .path() + .join("plugins/cache/openai-curated-remote/demo-plugin"); + let original_root = plugin_root.join("1.0.0"); + write_fixture_mcp(&original_root, "fixture-original")?; + installed.write().await["enabled"] = json!(true); + reconcile_plugins(&mut app_server).await?; + expect_fixture_mcp(&mut app_server, &thread.id, "fixture-original").await?; + + { + let mut plugin = installed.write().await; + plugin["release"]["version"] = json!("1.1.0"); + plugin["release"]["bundle_download_url"] = json!(format!("{base_url}/missing-bundle")); + } + let failed = reconcile_plugins(&mut app_server).await?; + assert!(failed.changed_plugins.is_empty()); + assert_eq!(failed.failed_remote_plugin_ids.len(), 1); + expect_fixture_mcp(&mut app_server, &thread.id, "fixture-original").await?; + + let upgraded_root = plugin_root.join("1.1.0"); + std::fs::rename(&original_root, &upgraded_root)?; + write_fixture_mcp(&upgraded_root, "fixture-upgrade")?; + assert!( + reconcile_plugins(&mut app_server) + .await? + .changed_plugins + .is_empty() + ); + expect_fixture_mcp(&mut app_server, &thread.id, "fixture-upgrade").await?; + + // A different remote identity can replace the bundle without changing its directory version. + let replacement_id = "plugins~Plugin_00000000000000000000000000000001"; + installed.write().await["id"] = json!(replacement_id); + let failed = reconcile_plugins(&mut app_server).await?; + assert_eq!(failed.failed_remote_plugin_ids, vec![replacement_id]); + expect_fixture_mcp(&mut app_server, &thread.id, "fixture-upgrade").await?; + write_fixture_mcp(&upgraded_root, "fixture-replacement")?; + std::fs::write( + plugin_root.join(".codex-remote-plugin-install.json"), + serde_json::to_vec(&json!({"schema_version": 1, "remote_plugin_id": replacement_id}))?, + )?; + assert!( + reconcile_plugins(&mut app_server) + .await? + .changed_plugins + .is_empty() + ); + expect_fixture_mcp(&mut app_server, &thread.id, "fixture-replacement").await?; + server_handle.abort(); Ok(()) } +fn write_fixture_mcp(root: &std::path::Path, name: &str) -> Result<()> { + std::fs::write( + root.join(".mcp.json"), + serde_json::to_vec(&json!({ + "mcpServers": {(name): {"type": "http", "url": "http://127.0.0.1:9/mcp"}} + }))?, + )?; + Ok(()) +} + +async fn reconcile_plugins(app_server: &mut TestAppServer) -> Result { + let request = app_server + .send_raw_request("plugin/reconcile", Some(json!({}))) + .await?; + timeout(DEFAULT_READ_TIMEOUT, app_server.read_response(request)).await? +} + +async fn expect_fixture_mcp( + app_server: &mut TestAppServer, + thread_id: &str, + expected: &str, +) -> Result<()> { + timeout(DEFAULT_READ_TIMEOUT, async { + loop { + let request = app_server + .send_raw_request("mcpServerStatus/list", Some(json!({"threadId": thread_id}))) + .await?; + let response: serde_json::Value = app_server.read_response(request).await?; + let names = response["data"] + .as_array() + .context("missing MCP status data")? + .iter() + .filter_map(|row| row["name"].as_str()) + .filter(|name| name.starts_with("fixture-")) + .collect::>(); + if names == [expected] { + return Ok::<(), anyhow::Error>(()); + } + tokio::time::sleep(std::time::Duration::from_millis(25)).await; + } + }) + .await??; + Ok(()) +} + async fn refresh_and_expect_logo(app_server: &mut TestAppServer, logo: &str) -> Result<()> { // plugin/installed starts the real background sync with the production callback. // Reconcile acquires the same gate, so its completion fences the background pass. diff --git a/codex-rs/core-plugins/src/manager.rs b/codex-rs/core-plugins/src/manager.rs index d9861f28b1..d410c26b79 100644 --- a/codex-rs/core-plugins/src/manager.rs +++ b/codex-rs/core-plugins/src/manager.rs @@ -354,13 +354,32 @@ impl RemoteInstalledPluginsAuthIdentity { struct RemoteInstalledPluginsCache { generation: u64, auth_identity: Option, - reconciliation_generation: Option, - // A cancelled pass may mutate bundles before publication; retry must notify consumers. - needs_effective_plugins_refresh: bool, + reconciliation: ReconciliationState, plugins: Option>, + pending_runtime_change: Option, display_assets: InstalledPluginDisplayAssets, } +#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] +enum ReconciliationState { + #[default] + Idle, + Running, + // A cancelled pass may mutate bundles before publication; retry must notify consumers. + RetryRequired, + Retrying, +} + +impl ReconciliationState { + fn is_running(self) -> bool { + matches!(self, Self::Running | Self::Retrying) + } + + fn requires_retry(self) -> bool { + matches!(self, Self::RetryRequired | Self::Retrying) + } +} + struct InstalledPluginCacheUpdate { metadata_change_id: Option, } @@ -388,8 +407,10 @@ struct RemoteInstalledPluginsReconciliationGuard<'a> { impl Drop for RemoteInstalledPluginsReconciliationGuard<'_> { fn drop(&mut self) { if !self.committed { - self.manager - .abandon_remote_installed_plugins_reconcile(self.generation); + self.manager.abandon_remote_installed_plugins_reconcile( + self.generation, + /*require_refresh*/ true, + ); } } } @@ -1506,7 +1527,7 @@ impl PluginsManager { &plugins, visible_marketplaces, ); - let Some(update) = self.write_remote_installed_plugins_cache_snapshot( + let Some(_) = self.write_remote_installed_plugins_cache_snapshot( generation, plugins, auth, @@ -1524,11 +1545,11 @@ impl PluginsManager { ) .unwrap_or(marketplaces)); }; - if update.metadata_change_id.is_some() - && let Some(on_effective_plugins_changed) = on_effective_plugins_changed + if let Some(on_effective_plugins_changed) = on_effective_plugins_changed + && let Some(change_id) = self.take_pending_runtime_change() { on_effective_plugins_changed(EffectivePluginsChange { - metadata_change_id: update.metadata_change_id, + metadata_change_id: Some(change_id), ..Default::default() }); } @@ -1556,8 +1577,8 @@ impl PluginsManager { let cache_changed = if cache.auth_identity.as_ref() != Some(&auth_identity) { cache.generation = cache.generation.wrapping_add(1); cache.auth_identity = Some(auth_identity); - cache.reconciliation_generation = None; - cache.needs_effective_plugins_refresh = false; + cache.reconciliation = ReconciliationState::Idle; + cache.pending_runtime_change = None; cache.plugins.take().is_some() } else { false @@ -1581,23 +1602,32 @@ impl PluginsManager { return None; } cache.generation = cache.generation.wrapping_add(1); - cache.reconciliation_generation = Some(cache.generation); + cache.reconciliation = if cache.reconciliation.requires_retry() { + ReconciliationState::Retrying + } else { + ReconciliationState::Running + }; Some(cache.generation) } - fn abandon_remote_installed_plugins_reconcile(&self, generation: u64) { + fn abandon_remote_installed_plugins_reconcile(&self, generation: u64, require_refresh: bool) { let mut cache = match self.remote_installed_plugins_cache.write() { Ok(cache) => cache, Err(err) => err.into_inner(), }; - if cache.reconciliation_generation != Some(generation) { + if cache.generation != generation || !cache.reconciliation.is_running() { return; } - cache.reconciliation_generation = None; + cache.reconciliation = if require_refresh || cache.reconciliation.requires_retry() { + ReconciliationState::RetryRequired + } else { + ReconciliationState::Idle + }; cache.generation = cache.generation.wrapping_add(1); - cache.needs_effective_plugins_refresh = true; drop(cache); - self.clear_loaded_plugins_cache(); + if require_refresh { + self.clear_loaded_plugins_cache(); + } } fn remote_installed_plugins_cache_generation_if_current( @@ -1615,10 +1645,26 @@ impl PluginsManager { (cache.auth_identity.as_ref() == Some(&auth_identity)).then_some(cache.generation) } + fn active_remote_plugin_bundle( + &self, + id: &PluginId, + ) -> Option { + let installation = self.store.active_plugin_installation(id)?; + Some(crate::remote::ActiveRemotePluginBundle { + directory_version: installation + .root + .as_path() + .file_name()? + .to_str()? + .to_owned(), + remote_plugin_id: installation.persisted_remote_plugin_id().ok().flatten(), + }) + } + fn write_remote_installed_plugins_cache_snapshot( &self, generation: u64, - plugins: Vec, + mut plugins: Vec, auth: Option<&CodexAuth>, publication: RemoteInstalledPluginsCachePublication, ) -> Option { @@ -1642,24 +1688,29 @@ impl PluginsManager { publication, RemoteInstalledPluginsCachePublication::Reconcile ); - if !is_reconcile && cache.reconciliation_generation == Some(generation) { + if !is_reconcile && cache.reconciliation.is_running() { return None; } if cache.auth_identity.is_none() { cache.auth_identity = Some(auth_identity); } + let needs_effective_plugins_refresh = is_reconcile && cache.reconciliation.requires_retry(); if is_reconcile { - cache.reconciliation_generation = None; + cache.reconciliation = ReconciliationState::Idle; cache.generation = cache.generation.wrapping_add(1); } - let needs_effective_plugins_refresh = - is_reconcile && std::mem::take(&mut cache.needs_effective_plugins_refresh); cache.display_assets = match publication { RemoteInstalledPluginsCachePublication::Refresh(display_assets) => display_assets, RemoteInstalledPluginsCachePublication::Reconcile => { InstalledPluginDisplayAssets::Include } }; + for plugin in &mut plugins { + plugin.active_bundle = + PluginId::new(plugin.name.clone(), plugin.marketplace_name.clone()) + .ok() + .and_then(|id| self.active_remote_plugin_bundle(&id)); + } let previous = cache.plugins.as_deref(); let payload_changed = previous != Some(plugins.as_slice()); let metadata_changed = payload_changed @@ -1677,6 +1728,9 @@ impl PluginsManager { } else { Default::default() }; + if let Some(change_id) = metadata_change_id { + cache.pending_runtime_change = Some(change_id); + } if payload_changed { cache.plugins = Some(plugins); } @@ -1688,6 +1742,14 @@ impl PluginsManager { Some(InstalledPluginCacheUpdate { metadata_change_id }) } + fn take_pending_runtime_change(&self) -> Option { + self.remote_installed_plugins_cache + .write() + .unwrap_or_else(std::sync::PoisonError::into_inner) + .pending_runtime_change + .take() + } + #[cfg(test)] fn write_remote_installed_plugins_cache(&self, plugins: Vec) -> bool { let auth = self.auth_manager.auth_cached(); @@ -1712,8 +1774,8 @@ impl PluginsManager { }; cache.generation = cache.generation.wrapping_add(1); cache.auth_identity = None; - cache.reconciliation_generation = None; - cache.needs_effective_plugins_refresh = false; + cache.reconciliation = ReconciliationState::Idle; + cache.pending_runtime_change = None; if cache.plugins.take().is_none() { return false; } @@ -1732,8 +1794,8 @@ impl PluginsManager { } cache.generation = cache.generation.wrapping_add(1); cache.auth_identity = None; - cache.reconciliation_generation = None; - cache.needs_effective_plugins_refresh = false; + cache.reconciliation = ReconciliationState::Idle; + cache.pending_runtime_change = None; if cache.plugins.take().is_none() { return Some(false); } @@ -1850,13 +1912,14 @@ impl PluginsManager { metadata_change_id = ?update.metadata_change_id, "completed remote installed plugin bundle sync" ); - if (update.metadata_change_id.is_some() || !outcome.changed_plugins.is_empty()) - && let Some(on_effective_plugins_changed) = on_effective_plugins_changed - { - on_effective_plugins_changed(EffectivePluginsChange { - metadata_change_id: update.metadata_change_id, - materialized_remote_plugins: outcome.materialized_remote_plugins, - }); + if let Some(on_effective_plugins_changed) = on_effective_plugins_changed { + let change_id = manager.take_pending_runtime_change(); + if change_id.is_some() || !outcome.changed_plugins.is_empty() { + on_effective_plugins_changed(EffectivePluginsChange { + metadata_change_id: change_id, + materialized_remote_plugins: outcome.materialized_remote_plugins, + }); + } } } Err(err) => { @@ -1900,11 +1963,30 @@ impl PluginsManager { &self, config: &PluginsConfigInput, auth: Option<&CodexAuth>, + ) -> Result { + self.reconcile_remote_installed_plugins_with_notify(config, auth, /*callback*/ None) + .await + } + + /// Synchronize bundles and deliver any pending host runtime invalidation. + pub async fn reconcile_remote_installed_plugins_with_notify( + &self, + config: &PluginsConfigInput, + auth: Option<&CodexAuth>, + callback: Option, ) -> Result { let _guard = self.acquire_remote_installed_plugin_sync_guard().await?; let (outcome, _) = self .reconcile_remote_installed_plugins_after_acquiring_gate(config, auth) .await?; + if let Some(callback) = callback + && let Some(change_id) = self.take_pending_runtime_change() + { + callback(EffectivePluginsChange { + metadata_change_id: Some(change_id), + ..Default::default() + }); + } Ok(outcome) } @@ -1954,7 +2036,18 @@ impl PluginsManager { auth, &previous_plugin_ids, ) - .await?; + .await; + let result = match result { + Err(err @ RemoteInstalledPluginBundleSyncError::Catalog(_)) => { + // Catalog errors precede any bundle mutations, so no recovery refresh is needed. + self.abandon_remote_installed_plugins_reconcile( + generation, /*require_refresh*/ false, + ); + reconciliation.committed = true; + return Err(err); + } + result => result?, + }; let mut outcome = result.outcome; // The generation fence keeps this comparison on the snapshot replaced by this pass. // In a known snapshot, absence means inactive: cached reinstalls need the same hints @@ -3556,7 +3649,7 @@ impl PluginsManager { .await; match plugins { Ok(plugins) => { - let Some(update) = self.write_remote_installed_plugins_cache_snapshot( + let Some(_) = self.write_remote_installed_plugins_cache_snapshot( request.generation, plugins, request.auth.as_ref(), @@ -3566,19 +3659,20 @@ impl PluginsManager { ) else { continue; }; - let should_notify = update.metadata_change_id.is_some() - || !request.change.materialized_remote_plugins.is_empty() - || matches!( - request.notify, - RemoteInstalledPluginsCacheRefreshNotify::AfterSuccessfulRefresh - ); - if should_notify - && let Some(on_effective_plugins_changed) = - request.on_effective_plugins_changed + if let Some(on_effective_plugins_changed) = request.on_effective_plugins_changed { - let mut change = request.change; - change.metadata_change_id = update.metadata_change_id; - on_effective_plugins_changed(change); + let change_id = self.take_pending_runtime_change(); + if change_id.is_some() + || !request.change.materialized_remote_plugins.is_empty() + || matches!( + request.notify, + RemoteInstalledPluginsCacheRefreshNotify::AfterSuccessfulRefresh + ) + { + let mut change = request.change; + change.metadata_change_id = change_id; + on_effective_plugins_changed(change); + } } } Err( diff --git a/codex-rs/core-plugins/src/manager_tests.rs b/codex-rs/core-plugins/src/manager_tests.rs index dc824773c2..3134884b67 100644 --- a/codex-rs/core-plugins/src/manager_tests.rs +++ b/codex-rs/core-plugins/src/manager_tests.rs @@ -1049,6 +1049,7 @@ fn remote_installed_plugin_in_marketplace( marketplace_name: marketplace_name.to_string(), id: format!("plugins~Plugin_{name}"), version: None, + active_bundle: None, name: name.to_string(), installed_at: None, enabled: true, @@ -2037,6 +2038,80 @@ async fn unresolved_display_assets_does_not_satisfy_display_cache_reads() { ); } +#[tokio::test] +async fn publish_catalog_changes_without_invalidating_the_installed_runtime() { + let codex_home = TempDir::new().unwrap(); + let manager = test_plugins_manager(codex_home.path().to_path_buf()); + let auth = manager.auth_manager.auth_cached(); + let generation = manager + .prepare_remote_installed_plugins_cache_generation(auth.as_ref()) + .unwrap(); + let mut plugin = remote_installed_linear_plugin(); + let initial = manager + .write_remote_installed_plugins_cache_snapshot( + generation, + vec![plugin.clone()], + auth.as_ref(), + RemoteInstalledPluginsCachePublication::Refresh(InstalledPluginDisplayAssets::Include), + ) + .unwrap(); + assert!(initial.metadata_change_id.is_some()); + + plugin.version = Some("2.0.0".into()); + plugin.installed_at = chrono::DateTime::from_timestamp(/*secs*/ 1, /*nsecs*/ 0); + let update = manager + .write_remote_installed_plugins_cache_snapshot( + generation, + vec![plugin.clone()], + auth.as_ref(), + RemoteInstalledPluginsCachePublication::Refresh(InstalledPluginDisplayAssets::Include), + ) + .unwrap(); + assert!(update.metadata_change_id.is_none()); + assert_eq!( + manager.build_remote_installed_plugin_marketplaces_from_cache(&[ + REMOTE_GLOBAL_MARKETPLACE_NAME + ]), + Some( + crate::remote::group_remote_installed_plugins_by_marketplaces( + &[plugin], + &[REMOTE_GLOBAL_MARKETPLACE_NAME], + ) + ) + ); +} + +#[test] +fn active_directory_upgrade_is_retained_until_the_host_can_be_notified() { + let home = TempDir::new().unwrap(); + let manager = test_plugins_manager(home.path().to_path_buf()); + let base = home + .path() + .join("plugins/cache/openai-curated-remote/linear"); + let id = PluginId::parse("linear@openai-curated-remote").unwrap(); + write_plugin_with_version(&base, "0.6.0", "linear", Some("0.6.0")); + manager + .store + .write_remote_plugin_id(&id, "plugins~Plugin_old") + .unwrap(); + let mut plugin = remote_installed_linear_plugin(); + plugin.id = "plugins~Plugin_replacement".into(); + plugin.version = Some("0.9.0".into()); + assert!(manager.write_remote_installed_plugins_cache(vec![plugin.clone()])); + assert!(manager.take_pending_runtime_change().is_some()); + assert_eq!( + manager.local_remote_plugin_version(&id.as_key(), &plugin.id), + None + ); + + std::fs::remove_dir_all(base.join("0.6.0")).unwrap(); + write_plugin_with_version(&base, "0.7.0", "linear", Some("0.7.0")); + assert!(manager.write_remote_installed_plugins_cache(vec![plugin.clone()])); + assert!(!manager.write_remote_installed_plugins_cache(vec![plugin])); + assert!(manager.take_pending_runtime_change().is_some()); + assert!(manager.take_pending_runtime_change().is_none()); +} + #[tokio::test] async fn build_remote_installed_plugin_marketplaces_from_cache_filters_by_marketplace_name() { let codex_home = TempDir::new().unwrap(); @@ -7917,6 +7992,10 @@ remote_plugin = true ); previous_malformed.id = malformed_remote_plugin_id.to_string(); previous_malformed.version = Some("local".to_string()); + previous_malformed.active_bundle = Some(crate::remote::ActiveRemotePluginBundle { + directory_version: "local".into(), + remote_plugin_id: Some(malformed_remote_plugin_id.to_string()), + }); previous_malformed.enabled = false; manager.write_remote_installed_plugins_cache(vec![previous_malformed.clone()]); @@ -7988,7 +8067,12 @@ async fn failed_installed_plugin_pagination_preserves_inventory_and_releases_mut Some(Product::Codex), auth_manager, )); - let previous_inventory = vec![remote_installed_plugin("retained")]; + let mut retained = remote_installed_plugin("retained"); + retained.active_bundle = Some(crate::remote::ActiveRemotePluginBundle { + directory_version: "local".into(), + remote_plugin_id: None, + }); + let previous_inventory = vec![retained]; manager.write_remote_installed_plugins_cache(previous_inventory.clone()); manager.maybe_start_remote_installed_plugin_bundle_sync( @@ -8098,9 +8182,30 @@ fn reconciliation_fences_refresh_publication_and_recovers_after_cancellation() { .map(|update| update.metadata_change_id.is_some()), Some(true) ); + // Delivering an observed change must not consume the separate recovery obligation. + assert!(manager.take_pending_runtime_change().is_some()); + let failed_retry_generation = manager + .begin_remote_installed_plugins_reconcile(/*auth*/ None) + .expect("begin retry before catalog failure"); + manager.abandon_remote_installed_plugins_reconcile( + failed_retry_generation, + /*require_refresh*/ false, + ); + assert_eq!( + manager + .remote_installed_plugins_cache + .read() + .unwrap() + .reconciliation, + ReconciliationState::RetryRequired + ); let retry_generation = manager .begin_remote_installed_plugins_reconcile(/*auth*/ None) .expect("begin retry after cancellation"); + manager.abandon_remote_installed_plugins_reconcile( + failed_retry_generation, + /*require_refresh*/ true, + ); assert_eq!( manager .write_remote_installed_plugins_cache_snapshot( diff --git a/codex-rs/core-plugins/src/remote.rs b/codex-rs/core-plugins/src/remote.rs index fc53f053d7..4359ed945d 100644 --- a/codex-rs/core-plugins/src/remote.rs +++ b/codex-rs/core-plugins/src/remote.rs @@ -353,11 +353,18 @@ pub struct RemoteMarketplaceFetchOutcome { pub catalog_cache_used: bool, } +#[derive(Debug, Clone, PartialEq, Eq)] +pub(crate) struct ActiveRemotePluginBundle { + pub(crate) directory_version: String, + pub(crate) remote_plugin_id: Option, +} + #[derive(Debug, Clone, PartialEq)] pub struct RemoteInstalledPlugin { pub marketplace_name: String, pub id: String, pub version: Option, + pub(crate) active_bundle: Option, pub name: String, pub canonical_app_id: Option, pub installed_at: Option>, @@ -2049,6 +2056,7 @@ fn remote_installed_plugin_to_cache_entry( marketplace_name, id: plugin.id.clone(), version: plugin.release.version.clone(), + active_bundle: None, name: plugin.name.clone(), canonical_app_id: plugin.canonical_app_id.clone(), installed_at: installed_plugin.installed_at, diff --git a/codex-rs/core-plugins/src/remote_metadata.rs b/codex-rs/core-plugins/src/remote_metadata.rs index 0ab68a14c4..baf33947c6 100644 --- a/codex-rs/core-plugins/src/remote_metadata.rs +++ b/codex-rs/core-plugins/src/remote_metadata.rs @@ -1,52 +1,92 @@ -//! Compares the metadata that determines installed-plugin behavior. +//! Compares installed runtime inputs separately from remote catalog metadata. //! -//! Display metadata (including image URLs and store capability badges) is excluded. Callers must -//! still publish the unmodified payload so display consumers receive fresh metadata. +//! Runtime invalidation tracks plugin identity, enablement, canonical connector ownership, and the active bundle identity. +//! Catalog release, policy, and display changes still need to be published to catalog consumers. +use crate::remote::ActiveRemotePluginBundle; use crate::remote::RemoteInstalledPlugin; use crate::remote::RemoteMarketplace; use std::collections::BTreeMap; use std::collections::HashMap; -/// Use the same comparisons for invalidation and its diagnostics. Expose field names only. -fn changed_fields<'a>( - previous: &'a RemoteInstalledPlugin, - current: &'a RemoteInstalledPlugin, -) -> impl Iterator + 'a { - macro_rules! compare { - ($($field:ident),+ $(,)?) => { - [$( (stringify!($field), previous.$field != current.$field) ),+] - }; +/// The account metadata consumed when building installed plugin runtimes. +#[derive(PartialEq, Eq)] +struct InstalledPluginRuntimeState<'a> { + marketplace_name: &'a str, + id: &'a str, + name: &'a str, + canonical_app_id: Option<&'a str>, + enabled: bool, + active_bundle: Option<&'a ActiveRemotePluginBundle>, +} + +impl<'a> From<&'a RemoteInstalledPlugin> for InstalledPluginRuntimeState<'a> { + fn from(plugin: &'a RemoteInstalledPlugin) -> Self { + Self { + marketplace_name: &plugin.marketplace_name, + id: &plugin.id, + name: &plugin.name, + canonical_app_id: plugin.canonical_app_id.as_deref(), + enabled: plugin.enabled, + active_bundle: plugin + .enabled + .then_some(plugin.active_bundle.as_ref()) + .flatten(), + } + } +} + +impl InstalledPluginRuntimeState<'_> { + /// Report only changed field names, never their values. + fn changed_fields(self, current: Self) -> impl Iterator { + let Self { + marketplace_name, + id, + name, + canonical_app_id, + enabled, + active_bundle, + } = self; + [ + ( + "marketplace_name", + marketplace_name != current.marketplace_name, + ), + ("id", id != current.id), + ("name", name != current.name), + ( + "canonical_app_id", + canonical_app_id != current.canonical_app_id, + ), + ("enabled", enabled != current.enabled), + ( + "active_directory_version", + active_bundle.map(|bundle| &bundle.directory_version) + != current + .active_bundle + .map(|bundle| &bundle.directory_version), + ), + ( + "active_remote_plugin_id", + active_bundle.and_then(|bundle| bundle.remote_plugin_id.as_deref()) + != current + .active_bundle + .and_then(|bundle| bundle.remote_plugin_id.as_deref()), + ), + ] + .into_iter() + .filter_map(|(field, changed)| changed.then_some(field)) } - compare!( - marketplace_name, - id, - name, - canonical_app_id, - version, - installed_at, - enabled, - install_policy, - install_policy_source, - must_show_installation_interstitial, - auth_policy, - availability, - disabled_reason, - eligible_plan_types, - ) - .into_iter() - .filter_map(|(field, changed)| changed.then_some(field)) } pub(crate) fn installed_plugin_metadata_eq( previous: &[RemoteInstalledPlugin], current: &[RemoteInstalledPlugin], ) -> bool { - previous.len() == current.len() - && previous - .iter() - .zip(current) - .all(|(previous, current)| changed_fields(previous, current).next().is_none()) + previous + .iter() + .map(InstalledPluginRuntimeState::from) + .eq(current.iter().map(InstalledPluginRuntimeState::from)) } /// Called only for a changed snapshot. Match by remote ID so an insertion does @@ -66,7 +106,9 @@ pub(crate) fn installed_plugin_metadata_changes( let mut changes = BTreeMap::new(); for (index, plugin) in current.iter().enumerate() { let fields = if let Some((old_index, prior)) = old.remove(plugin.id.as_str()) { - let mut fields: Vec<_> = changed_fields(prior, plugin).collect(); + let mut fields: Vec<_> = InstalledPluginRuntimeState::from(prior) + .changed_fields(InstalledPluginRuntimeState::from(plugin)) + .collect(); if old_index != index && same_members { fields.push("position"); } diff --git a/codex-rs/core-plugins/src/remote_metadata_cache_tests.rs b/codex-rs/core-plugins/src/remote_metadata_cache_tests.rs index c93023c8bf..fb882ca9e6 100644 --- a/codex-rs/core-plugins/src/remote_metadata_cache_tests.rs +++ b/codex-rs/core-plugins/src/remote_metadata_cache_tests.rs @@ -28,6 +28,10 @@ enabled = true Some(AuthMode::Chatgpt), ); let mut remote = remote_installed_plugin("sample"); + remote.active_bundle = Some(crate::remote::ActiveRemotePluginBundle { + directory_version: "local".into(), + remote_plugin_id: None, + }); remote.interface = Some( serde_json::from_value(serde_json::json!({ "capabilities": [], "screenshots": [], "screenshotUrls": [], @@ -110,6 +114,10 @@ enabled = true assert!(Arc::ptr_eq(&suggestions, &refreshed_suggestions)); remote.version = Some("2.0.0".to_string()); + assert!(!manager.write_remote_installed_plugins_cache(vec![remote.clone()])); + assert_eq!(manager.loaded_plugins_cache_generation(), loaded_generation); + + remote.enabled = false; assert!(manager.write_remote_installed_plugins_cache(vec![remote])); assert_eq!( manager.loaded_plugins_cache_generation(), diff --git a/codex-rs/core-plugins/src/remote_metadata_tests.rs b/codex-rs/core-plugins/src/remote_metadata_tests.rs index 6397addc1d..89ec0cf0c1 100644 --- a/codex-rs/core-plugins/src/remote_metadata_tests.rs +++ b/codex-rs/core-plugins/src/remote_metadata_tests.rs @@ -17,6 +17,7 @@ fn plugin(renewal: &str) -> RemoteInstalledPlugin { marketplace_name: REMOTE_GLOBAL_MARKETPLACE_NAME.to_string(), id: "plugin-test".to_string(), version: Some("1.0.0".to_string()), + active_bundle: None, name: "test".to_string(), canonical_app_id: None, installed_at: None, @@ -45,16 +46,38 @@ fn catalog(plugins: &[RemoteInstalledPlugin]) -> Vec { } #[test] -fn behavioral_metadata_changes_still_invalidate() { +fn runtime_metadata_changes_invalidate() { let changes: &[fn(&mut RemoteInstalledPlugin)] = &[ + |plugin| plugin.marketplace_name.push_str("-other"), |plugin| plugin.id.push_str("-other"), |plugin| plugin.name.push_str("-other"), + |plugin| plugin.canonical_app_id = Some("new-app".to_string()), + |plugin| plugin.enabled = false, + |plugin| { + plugin.active_bundle = Some(crate::remote::ActiveRemotePluginBundle { + directory_version: "2.0.0".into(), + remote_plugin_id: None, + }) + }, + ]; + for change in changes { + let previous = [plugin("old")]; + let mut current = previous.clone(); + change(&mut current[0]); + assert!(!installed_plugin_metadata_eq(&previous, ¤t)); + } + assert!(!installed_plugin_metadata_eq(&[plugin("old")], &[])); + assert!(!remote_catalog_metadata_eq(&catalog(&[plugin("old")]), &[])); +} + +#[test] +fn catalog_metadata_changes_do_not_invalidate_the_installed_runtime() { + let changes: &[fn(&mut RemoteInstalledPlugin)] = &[ |plugin| plugin.version = Some("2.0.0".to_string()), |plugin| { plugin.installed_at = chrono::DateTime::from_timestamp(/*secs*/ 1, /*nsecs*/ 0) }, - |plugin| plugin.enabled = false, |plugin| plugin.install_policy = PluginInstallPolicy::NotAvailable, |plugin| { plugin.install_policy_source = @@ -67,20 +90,15 @@ fn behavioral_metadata_changes_still_invalidate() { ]; for change in changes { let previous = [plugin("old")]; - let mut current = [plugin("new")]; + let mut current = previous.clone(); change(&mut current[0]); - assert!(!installed_plugin_metadata_eq(&previous, ¤t)); + assert!(installed_plugin_metadata_eq(&previous, ¤t)); + assert!(installed_plugin_metadata_changes(&previous, ¤t).is_empty()); assert!(!remote_catalog_metadata_eq( &catalog(&previous), &catalog(¤t) )); } - let previous = [plugin("old")]; - let mut current = [plugin("new")]; - current[0].canonical_app_id = Some("new-app".to_string()); - assert!(!installed_plugin_metadata_eq(&previous, ¤t)); - assert!(!installed_plugin_metadata_eq(&previous, &[])); - assert!(!remote_catalog_metadata_eq(&catalog(&previous), &[])); } #[test] @@ -212,7 +230,7 @@ fn diagnostics_attribute_fields_by_plugin_id_across_membership_changes() { std::collections::BTreeMap::from([ ("added".into(), vec!["added"]), ("removed".into(), vec!["removed"]), - (id, vec!["version", "enabled"]), + (id, vec!["enabled"]), ]) ); }