From 237d8b104ed06ae46c5ed9a54693672dea21856c Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Mon, 16 Nov 2026 18:54:06 +0000 Subject: [PATCH] Report remote plugin local versions from matching cached packages (#61865) ## Why A remote plugin's catalog version can differ from the package available locally. Matching names and versions also do not establish that a cached package belongs to the same remote plugin. ## What changed - Resolve `local_version` for remote plugin lists, details, and shares from the active local package, requiring a matching remote plugin ID and an existing manifest. Perform these filesystem reads on a blocking task. - Download the package when a same-version cache has a conflicting or unreadable identity receipt. Preserve migration for older caches without receipts, and retain the original receipt if the replacement download fails. ## Testing Add coverage for local package identity and manifest requirements, differing catalog and local versions, and preservation of the original receipt after a failed replacement download. Update share expectations to leave `local_version` unset when no matching local package exists. GitOrigin-RevId: 5d86f834ac3861a4a3d3bf0d042bfe3f991a7cad --- codex-rs/app-server/src/request_processors.rs | 1 - .../src/request_processors/plugins.rs | 137 ++++++++++++------ .../src/request_processors/plugins/search.rs | 6 +- .../app-server/tests/suite/v2/plugin_list.rs | 21 ++- .../app-server/tests/suite/v2/plugin_share.rs | 6 +- codex-rs/core-plugins/src/manager.rs | 20 +++ codex-rs/core-plugins/src/manager_tests.rs | 24 +++ .../remote/remote_installed_plugin_sync.rs | 116 +++++++++++++-- 8 files changed, 271 insertions(+), 60 deletions(-) diff --git a/codex-rs/app-server/src/request_processors.rs b/codex-rs/app-server/src/request_processors.rs index 82dbae7584..8d3cf0e4d5 100644 --- a/codex-rs/app-server/src/request_processors.rs +++ b/codex-rs/app-server/src/request_processors.rs @@ -393,7 +393,6 @@ use codex_core_plugins::remote::RemotePluginCatalogError; use codex_core_plugins::remote::RemotePluginDetail as RemoteCatalogPluginDetail; use codex_core_plugins::remote::RemotePluginServiceConfig; use codex_core_plugins::remote::RemotePluginShareContext as RemoteCatalogPluginShareContext; -use codex_core_plugins::remote::RemotePluginShareSummary as RemoteCatalogPluginShareSummary; use codex_core_plugins::remote::RemotePluginSummary as RemoteCatalogPluginSummary; use codex_exec_server::EnvironmentManager; use codex_exec_server::EnvironmentObservedStatus; diff --git a/codex-rs/app-server/src/request_processors/plugins.rs b/codex-rs/app-server/src/request_processors/plugins.rs index ed74f924a7..7a6b99f80a 100644 --- a/codex-rs/app-server/src/request_processors/plugins.rs +++ b/codex-rs/app-server/src/request_processors/plugins.rs @@ -613,7 +613,6 @@ impl PluginRequestProcessor { include_global_remote && auth_mode.is_some_and(DomainAuthMode::uses_codex_backend); let remote_plugin_service_config = remote_plugin_service_config(&self.thread_manager, &config)?; - let plugin_policy = &config.plugins; let remote_catalog_cache_mode = if force_refetch { RemotePluginCatalogCacheMode::ForceRefetch } else { @@ -689,6 +688,7 @@ impl PluginRequestProcessor { (Vec::new(), Vec::new(), Vec::new()) }; + let mut remote_marketplaces = Vec::new(); // TODO(remote plugins): Remove this once remote plugins are ready and vertical plugins are // served directly from the normal remote catalog. if include_vertical && !config.features.enabled(Feature::RemotePlugin) { @@ -702,10 +702,7 @@ impl PluginRequestProcessor { .map(|outcome| outcome.marketplace) { Ok(Some(remote_marketplace)) => { - data.push(remote_marketplace_to_info( - remote_marketplace, - plugin_policy, - )); + remote_marketplaces.push(remote_marketplace); } Ok(None) => {} Err(RemotePluginCatalogError::UnsupportedAuthMode) => {} @@ -763,13 +760,7 @@ impl PluginRequestProcessor { match outcome { Ok(outcome) => { remote_catalog_cache_refresh_scopes = outcome.catalog_cache_refresh_scopes; - for remote_marketplace in outcome - .marketplaces - .into_iter() - .map(|marketplace| remote_marketplace_to_info(marketplace, plugin_policy)) - { - data.push(remote_marketplace); - } + remote_marketplaces.extend(outcome.marketplaces); } Err( err @ (RemotePluginCatalogError::AuthRequired @@ -815,6 +806,10 @@ impl PluginRequestProcessor { ); } + data.extend( + self.convert_remote_marketplaces(remote_marketplaces, &config.plugins) + .await?, + ); let featured_plugin_ids = if data.iter().any(|marketplace| { marketplace.name == OPENAI_CURATED_MARKETPLACE_NAME || marketplace.name == REMOTE_GLOBAL_MARKETPLACE_NAME @@ -892,14 +887,17 @@ impl PluginRequestProcessor { .await?; if context.global_config.plugins_enabled { - data.extend( - self.load_remote_installed_plugins( + let remote_marketplaces = self + .load_remote_installed_plugins( plugins_manager, &context.global_config, &remote_installed_plugin_visible_marketplaces, auth.as_ref(), ) - .await, + .await; + data.extend( + self.convert_remote_marketplaces(remote_marketplaces, &config.plugins) + .await?, ); } filter_openai_curated_installed_conflicts(&mut data, use_remote_global_catalog); @@ -910,6 +908,43 @@ impl PluginRequestProcessor { }) } + /// Build remote plugin responses on a blocking task, where installed packages can be read. + async fn convert_remote_plugins( + &self, + plugin_policy: &PluginsConfigToml, + convert: impl FnOnce(&codex_core_plugins::PluginsManager, &PluginsConfigToml) -> T + + Send + + 'static, + ) -> Result + where + T: Send + 'static, + { + let manager = self.thread_manager.plugins_manager(); + let plugin_policy = plugin_policy.clone(); + tokio::task::spawn_blocking(move || convert(&manager, &plugin_policy)) + .await + .map_err(|err| { + internal_error(format!("failed to resolve local plugin versions: {err}")) + }) + } + + async fn convert_remote_marketplaces( + &self, + marketplaces: Vec, + plugin_policy: &PluginsConfigToml, + ) -> Result, JSONRPCErrorError> { + if marketplaces.is_empty() { + return Ok(Vec::new()); + } + self.convert_remote_plugins(plugin_policy, move |manager, policy| { + marketplaces + .into_iter() + .map(|marketplace| remote_marketplace_to_info(marketplace, policy, manager)) + .collect() + }) + .await + } + async fn load_local_installed_and_suggested_plugins( &self, plugins_manager: Arc, @@ -995,8 +1030,7 @@ impl PluginRequestProcessor { plugins_input: &codex_core_plugins::PluginsConfigInput, visible_marketplaces: &[&str], auth: Option<&CodexAuth>, - ) -> Vec { - let plugin_policy = &plugins_input.plugins; + ) -> Vec { let remote_marketplaces = if let Some(remote_marketplaces) = plugins_manager .build_remote_installed_plugin_marketplaces_from_cache(visible_marketplaces) { @@ -1013,10 +1047,7 @@ impl PluginRequestProcessor { }; match remote_marketplaces { - Ok(remote_marketplaces) => remote_marketplaces - .into_iter() - .map(|marketplace| remote_marketplace_to_info(marketplace, plugin_policy)) - .collect(), + Ok(remote_marketplaces) => remote_marketplaces, Err( RemotePluginCatalogError::AuthRequired | RemotePluginCatalogError::UnsupportedAuthMode, @@ -1244,8 +1275,10 @@ impl PluginRequestProcessor { &app_category_by_id, ) .await; - let plugin_policy = &config.plugins; - remote_plugin_detail_to_info(remote_detail, app_summaries, plugin_policy) + self.convert_remote_plugins(&config.plugins, move |manager, policy| { + remote_plugin_detail_to_info(remote_detail, app_summaries, policy, manager) + }) + .await? } }; @@ -1414,27 +1447,31 @@ impl PluginRequestProcessor { let (config, auth) = self.load_plugin_share_config_and_auth().await?; let remote_plugin_service_config = remote_plugin_service_config(&self.thread_manager, &config)?; - let plugin_policy = &config.plugins; - let data = codex_core_plugins::remote::list_remote_plugin_shares( + let shares = codex_core_plugins::remote::list_remote_plugin_shares( &remote_plugin_service_config, auth.as_ref(), config.codex_home.as_path(), ) .await - .map_err(|err| remote_plugin_catalog_error_to_jsonrpc(err, "list remote plugin shares"))? - .into_iter() - .map(|summary| { - let RemoteCatalogPluginShareSummary { - summary, - local_plugin_path, - } = summary; - let plugin = remote_plugin_summary_to_info(summary, plugin_policy); - PluginShareListItem { - plugin, - local_plugin_path, - } - }) - .collect(); + .map_err(|err| remote_plugin_catalog_error_to_jsonrpc(err, "list remote plugin shares"))?; + if shares.is_empty() { + return Ok(PluginShareListResponse { data: Vec::new() }); + } + let data = self + .convert_remote_plugins(&config.plugins, move |manager, policy| { + shares + .into_iter() + .map(|share| PluginShareListItem { + plugin: installed_remote_plugin_summary_to_info( + share.summary, + policy, + manager, + ), + local_plugin_path: share.local_plugin_path, + }) + .collect() + }) + .await?; Ok(PluginShareListResponse { data }) } @@ -2282,6 +2319,7 @@ fn plugin_app_category_by_id_from_value(value: &serde_json::Value) -> HashMap PluginMarketplaceEntry { PluginMarketplaceEntry { name: marketplace.name, @@ -2292,21 +2330,35 @@ fn remote_marketplace_to_info( plugins: marketplace .plugins .into_iter() - .map(|summary| remote_plugin_summary_to_info(summary, plugin_policy)) + .map(|summary| installed_remote_plugin_summary_to_info(summary, plugin_policy, manager)) .collect(), } } +fn installed_remote_plugin_summary_to_info( + summary: RemoteCatalogPluginSummary, + plugin_policy: &PluginsConfigToml, + manager: &codex_core_plugins::PluginsManager, +) -> PluginSummary { + let local_version = if summary.installed { + manager.local_remote_plugin_version(&summary.id, &summary.remote_plugin_id) + } else { + None + }; + remote_plugin_summary_to_info(summary, plugin_policy, local_version) +} + fn remote_plugin_summary_to_info( summary: RemoteCatalogPluginSummary, plugin_policy: &PluginsConfigToml, + local_version: Option, ) -> PluginSummary { let enabled = summary.enabled && plugin_policy.allows_plugin(&summary.id); PluginSummary { id: summary.id, remote_plugin_id: Some(summary.remote_plugin_id), version: summary.version, - local_version: summary.local_version, + local_version, name: summary.name, share_context: summary .share_context @@ -2371,8 +2423,9 @@ fn remote_plugin_detail_to_info( detail: RemoteCatalogPluginDetail, apps: Vec, plugin_policy: &PluginsConfigToml, + manager: &codex_core_plugins::PluginsManager, ) -> PluginDetail { - let summary = remote_plugin_summary_to_info(detail.summary, plugin_policy); + let summary = installed_remote_plugin_summary_to_info(detail.summary, plugin_policy, manager); let app_templates = detail .app_templates .into_iter() diff --git a/codex-rs/app-server/src/request_processors/plugins/search.rs b/codex-rs/app-server/src/request_processors/plugins/search.rs index f259544ac2..aa93ac2de0 100644 --- a/codex-rs/app-server/src/request_processors/plugins/search.rs +++ b/codex-rs/app-server/src/request_processors/plugins/search.rs @@ -115,7 +115,11 @@ impl PluginRequestProcessor { continue; } remote_results.push(plugin_search_result( - remote_plugin_summary_to_info(plugin, plugin_policy), + remote_plugin_summary_to_info( + plugin, + plugin_policy, + /*local_version*/ None, + ), plugin_id.marketplace_name, /*marketplace_path*/ None, )); diff --git a/codex-rs/app-server/tests/suite/v2/plugin_list.rs b/codex-rs/app-server/tests/suite/v2/plugin_list.rs index e93746deee..b5d0d209e7 100644 --- a/codex-rs/app-server/tests/suite/v2/plugin_list.rs +++ b/codex-rs/app-server/tests/suite/v2/plugin_list.rs @@ -291,6 +291,12 @@ async fn plugin_installed_prefers_remote_curated_conflicts_when_remote_plugin_en write_openai_curated_marketplace(codex_home.path(), &["linear", "calendar"])?; write_installed_plugin(&codex_home, "openai-curated", "linear")?; write_installed_plugin(&codex_home, "openai-curated", "calendar")?; + write_installed_plugin_with_version(&codex_home, "openai-curated-remote", "linear", "1.2.2")?; + codex_core_plugins::store::PluginStore::new(codex_home.path().to_path_buf()) + .write_remote_plugin_id( + &codex_plugin::PluginId::new("linear".into(), "openai-curated-remote".into())?, + "plugins~Plugin_00000000000000000000000000000000", + )?; std::fs::write( codex_home.path().join("config.toml"), format!( @@ -378,6 +384,8 @@ enabled = true .map(|plugin| { ( plugin.id.clone(), + plugin.version.clone(), + plugin.local_version.clone(), plugin.enabled, plugin.install_policy_source, plugin.must_show_installation_interstitial, @@ -387,12 +395,16 @@ enabled = true vec![ ( "linear@openai-curated-remote".to_string(), + Some("1.2.3".to_string()), + Some("1.2.2".to_string()), expected_remote_enabled, Some(PluginInstallPolicySource::WorkspaceSetting), Some(false), ), ( "remote-only@openai-curated-remote".to_string(), + Some("1.2.3".to_string()), + None, expected_remote_enabled, Some(PluginInstallPolicySource::WorkspaceSetting), Some(false), @@ -2183,7 +2195,12 @@ async fn plugin_list_includes_remote_marketplaces_when_remote_plugin_enabled( .chatgpt_account_id("account-123"), AuthCredentialsStoreMode::File, )?; - write_installed_plugin_with_version(&codex_home, "openai-curated-remote", "linear", "1.2.3")?; + write_installed_plugin_with_version(&codex_home, "openai-curated-remote", "linear", "1.2.2")?; + codex_core_plugins::store::PluginStore::new(codex_home.path().to_path_buf()) + .write_remote_plugin_id( + &codex_plugin::PluginId::new("linear".into(), "openai-curated-remote".into())?, + "plugins~Plugin_00000000000000000000000000000000", + )?; let repo = TempDir::new()?; let cwds = if project_configuration != ProjectPluginConfiguration::None { @@ -2398,7 +2415,7 @@ async fn plugin_list_includes_remote_marketplaces_when_remote_plugin_enabled( ); assert_eq!( remote_marketplace.plugins[0].local_version.as_deref(), - Some("1.2.3") + Some("1.2.2") ); assert_eq!(remote_marketplace.plugins[0].installed, true); assert_eq!( diff --git a/codex-rs/app-server/tests/suite/v2/plugin_share.rs b/codex-rs/app-server/tests/suite/v2/plugin_share.rs index 06ca0a5874..c58651b6bb 100644 --- a/codex-rs/app-server/tests/suite/v2/plugin_share.rs +++ b/codex-rs/app-server/tests/suite/v2/plugin_share.rs @@ -167,7 +167,7 @@ async fn plugin_share_save_uploads_local_plugin() -> Result<()> { id: "demo-plugin@workspace-shared-with-me".to_string(), remote_plugin_id: Some("plugins_123".to_string()), version: Some("0.1.0".to_string()), - local_version: Some("0.1.0".to_string()), + local_version: None, name: "demo-plugin".to_string(), share_context: Some(expected_share_context("plugins_123")), source: PluginSource::Remote, @@ -588,7 +588,7 @@ async fn plugin_share_list_returns_created_workspace_plugins() -> Result<()> { id: "demo-plugin@workspace-shared-with-me".to_string(), remote_plugin_id: Some("plugins_123".to_string()), version: Some("0.1.0".to_string()), - local_version: Some("0.1.0".to_string()), + local_version: None, name: "demo-plugin".to_string(), share_context: Some(expected_share_context("plugins_123")), source: PluginSource::Remote, @@ -1251,7 +1251,7 @@ async fn plugin_share_delete_removes_created_workspace_plugin() -> Result<()> { id: "demo-plugin@workspace-shared-with-me".to_string(), remote_plugin_id: Some("plugins_123".to_string()), version: Some("0.1.0".to_string()), - local_version: Some("0.1.0".to_string()), + local_version: None, name: "demo-plugin".to_string(), share_context: Some(expected_share_context("plugins_123")), source: PluginSource::Remote, diff --git a/codex-rs/core-plugins/src/manager.rs b/codex-rs/core-plugins/src/manager.rs index cac09dd438..d9861f28b1 100644 --- a/codex-rs/core-plugins/src/manager.rs +++ b/codex-rs/core-plugins/src/manager.rs @@ -785,6 +785,26 @@ impl PluginsManager { Arc::clone(&self.auth_manager) } + /// Return the version of the materialized remote package matching its catalog identity. + pub fn local_remote_plugin_version( + &self, + plugin_key: &str, + remote_plugin_id: &str, + ) -> Option { + let plugin_id = PluginId::parse(plugin_key).ok()?; + let installation = self.store.active_plugin_installation(&plugin_id)?; + if installation.persisted_remote_plugin_id().ok()?.as_deref() != Some(remote_plugin_id) { + return None; + } + codex_utils_plugins::find_plugin_manifest_path(installation.root.as_path())?; + installation + .root + .as_path() + .file_name()? + .to_str() + .map(str::to_owned) + } + pub fn runtime_auth_slot(&self) -> RuntimeAuthSlot { Arc::clone(&self.runtime_auth) } diff --git a/codex-rs/core-plugins/src/manager_tests.rs b/codex-rs/core-plugins/src/manager_tests.rs index b5d596b6be..dc824773c2 100644 --- a/codex-rs/core-plugins/src/manager_tests.rs +++ b/codex-rs/core-plugins/src/manager_tests.rs @@ -133,6 +133,30 @@ fn plugins_config_input_with_requirements( ) } +#[test] +fn local_remote_plugin_version_requires_a_materialized_package_and_matching_identity() { + let home = TempDir::new().unwrap(); + let manager = test_plugins_manager(home.path().to_path_buf()); + let key = "sample@openai-curated-remote"; + let id = PluginId::parse(key).unwrap(); + let root = manager.store.plugin_root(&id, "1.0.0"); + let manifest = root.join(".codex-plugin/plugin.json"); + write_file(&manifest, r#"{"name":"sample","version":"1.0.0"}"#); + + assert_eq!(manager.local_remote_plugin_version(key, "owner"), None); + manager.store.write_remote_plugin_id(&id, "owner").unwrap(); + assert_eq!( + manager.local_remote_plugin_version(key, "owner"), + Some("1.0.0".to_string()) + ); + assert_eq!( + manager.local_remote_plugin_version(key, "different-owner"), + None + ); + fs::remove_file(manifest.as_path()).unwrap(); + assert_eq!(manager.local_remote_plugin_version(key, "owner"), None); +} + #[tokio::test] async fn plugins_manager_reads_auth_mode_from_auth_manager() { let tmp = TempDir::new().unwrap(); diff --git a/codex-rs/core-plugins/src/remote/remote_installed_plugin_sync.rs b/codex-rs/core-plugins/src/remote/remote_installed_plugin_sync.rs index 00748af827..c09f0efec1 100644 --- a/codex-rs/core-plugins/src/remote/remote_installed_plugin_sync.rs +++ b/codex-rs/core-plugins/src/remote/remote_installed_plugin_sync.rs @@ -236,18 +236,30 @@ pub(crate) async fn sync_remote_installed_plugin_bundles_once_with_snapshot( if let Some(release_version) = release_version && store.active_plugin_version(&plugin_id).as_deref() == Some(release_version) { - if let Err(err) = store.write_remote_plugin_id(&plugin_id, &plugin.id) { - warn!( - remote_plugin_id = %plugin.id, - plugin = %plugin.name, - marketplace = %marketplace_name, - error = %err, - "failed to persist identity for cached remote installed plugin" - ); - failed_remote_plugin_ids.insert(plugin.id); + match store.remote_plugin_id(&plugin_id) { + Ok(Some(id)) if id == plugin.id => { + installed_plugins.push(cached_plugin); + continue; + } + // Preserve migration of older caches that predate remote install receipts. + Ok(None) => { + if let Err(err) = store.write_remote_plugin_id(&plugin_id, &plugin.id) { + warn!( + remote_plugin_id = %plugin.id, + plugin = %plugin.name, + marketplace = %marketplace_name, + error = %err, + "failed to persist identity for cached remote installed plugin" + ); + failed_remote_plugin_ids.insert(plugin.id); + } + installed_plugins.push(cached_plugin); + continue; + } + // A conflicting or unreadable receipt cannot establish ownership, even if + // the catalog advertises the same name and version. Install the package instead. + Ok(Some(_)) | Err(_) => {} } - installed_plugins.push(cached_plugin); - continue; } let bundle = match crate::remote_bundle::validate_remote_plugin_bundle( @@ -624,6 +636,88 @@ mod tests { ); } + #[tokio::test] + async fn sync_same_version_conflicting_identity_preserves_receipt_after_download_failure() { + let server = MockServer::start().await; + let codex_home = tempfile::tempdir().expect("create codex home"); + let plugin_id = PluginId::new( + "linear".to_string(), + REMOTE_GLOBAL_MARKETPLACE_NAME.to_string(), + ) + .expect("valid plugin id"); + let store = PluginStore::new(codex_home.path().to_path_buf()); + let manifest = store + .plugin_root(&plugin_id, "1.2.3") + .join(".codex-plugin/plugin.json"); + std::fs::create_dir_all(manifest.as_path().parent().expect("manifest parent")) + .expect("create cached plugin directory"); + std::fs::write(manifest.as_path(), r#"{"name":"linear","version":"1.2.3"}"#) + .expect("write cached plugin manifest"); + store + .write_remote_plugin_id(&plugin_id, "plugins~Plugin_original") + .expect("persist original identity"); + + // This HTTP mock cannot complete a TLS handshake, so the valid HTTPS bundle URL fails + // during download without contacting any external service. + let bundle_url = format!( + "{}/bundle.tar.gz", + server.uri().replacen("http://", "https://", 1) + ); + Mock::given(method("GET")) + .and(path("/backend-api/ps/plugins/installed")) + .and(query_param_is_missing("scope")) + .and(query_param("limit", "200")) + .and(query_param("includeDownloadUrls", "true")) + .respond_with(ResponseTemplate::new(200).set_body_json(json!({ + "plugins": [{ + "id": "plugins~Plugin_replacement", + "name": "linear", + "scope": "GLOBAL", + "installation_policy": "AVAILABLE", + "authentication_policy": "ON_USE", + "status": "ENABLED", + "release": { + "version": "1.2.3", + "display_name": "Linear", + "description": "Track work", + "interface": {}, + "bundle_download_url": bundle_url, + }, + "enabled": true, + }], + "pagination": {"next_page_token": null}, + }))) + .expect(1) + .mount(&server) + .await; + let config = RemotePluginServiceConfig::new( + format!("{}/backend-api", server.uri()), + crate::test_support::test_http_client_factory(), + /*product_sku*/ None, + ); + let auth = CodexAuth::create_dummy_chatgpt_auth_for_testing(); + + let outcome = sync_remote_installed_plugin_bundles_once( + codex_home.path().to_path_buf(), + &config, + Some(&auth), + ) + .await + .expect("sync remote catalog"); + + assert_eq!( + outcome.failed_materialization_remote_plugin_ids, + vec!["plugins~Plugin_replacement".to_string()] + ); + assert_eq!( + store + .remote_plugin_id(&plugin_id) + .expect("read original receipt"), + Some("plugins~Plugin_original".to_string()) + ); + assert!(manifest.as_path().is_file()); + } + #[tokio::test] async fn sync_all_scopes_paginates_and_reconciles_each_marketplace() { let server = MockServer::start().await;