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
This commit is contained in:
Charlie Marsh
2026-11-16 19:28:36 +00:00
committed by copyberry
parent 457a191479
commit 237d8b104e
8 changed files with 271 additions and 60 deletions
+20
View File
@@ -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<String> {
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)
}
@@ -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();
@@ -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;