| 1 | use std::fs; |
| 2 | use std::path::Path; |
| 3 | |
| 4 | use super::PluginRegistry; |
| 5 | use crate::types::{LoadedPlugin, PluginScope}; |
| 6 | |
| 7 | const DESKTOP_ACCOUNT_PLUGINS: &str = "codewhale-desktop-account-plugins"; |
| 8 | const DESKTOP_ACCOUNT_PLUGINS_MCP_URL: &str = "https://api.codewhale.net/api/plugins/mcp"; |
| 9 | |
| 10 | fn is_withdrawn_desktop_bundle(plugin: &LoadedPlugin, expected_root: &Path) -> bool { |
| 11 | if plugin.scope != PluginScope::User |
| 12 | || plugin.name() != DESKTOP_ACCOUNT_PLUGINS |
| 13 | || plugin.canonical_root != expected_root |
| 14 | || plugin.inventory.skills != 0 |
| 15 | || plugin.inventory.commands != 0 |
| 16 | || plugin.inventory.agents != 0 |
| 17 | || plugin.inventory.hooks != 0 |
| 18 | || plugin.inventory.lsp != 0 |
| 19 | || plugin.inventory.native != 0 |
| 20 | || plugin.inventory.providers != 0 |
| 21 | || plugin.inventory.mcp_servers != 1 |
| 22 | || !crate::install::has_regular_install_marker(&plugin.canonical_root) |
| 23 | { |
| 24 | return false; |
| 25 | } |
| 26 | let Ok(text) = fs::read_to_string(plugin.canonical_root.join("mcp.json")) else { |
| 27 | return false; |
| 28 | }; |
| 29 | let Ok(document) = serde_json::from_str::<serde_json::Value>(&text) else { |
| 30 | return false; |
| 31 | }; |
| 32 | let Some(servers) = document |
| 33 | .get("mcpServers") |
| 34 | .and_then(|value| value.as_object()) |
| 35 | else { |
| 36 | return false; |
| 37 | }; |
| 38 | servers.len() == 1 |
| 39 | && servers.values().all(|server| { |
| 40 | server.get("url").and_then(|url| url.as_str()) == Some(DESKTOP_ACCOUNT_PLUGINS_MCP_URL) |
| 41 | }) |
| 42 | } |
| 43 | |
| 44 | impl PluginRegistry { |
| 45 | pub fn retire_withdrawn_bundles(&mut self) -> Vec<String> { |
| 46 | if self.state_error.is_some() { |
| 47 | return Vec::new(); |
| 48 | } |
| 49 | let Some(plugins_dir) = self.user_plugins_dir().map(Path::to_path_buf) else { |
| 50 | return Vec::new(); |
| 51 | }; |
| 52 | let Ok(canonical_plugins_dir) = fs::canonicalize(&plugins_dir) else { |
| 53 | return Vec::new(); |
| 54 | }; |
| 55 | let expected_root = canonical_plugins_dir.join(DESKTOP_ACCOUNT_PLUGINS); |
| 56 | let withdrawn = self |
| 57 | .plugins |
| 58 | .values() |
| 59 | .filter(|plugin| is_withdrawn_desktop_bundle(plugin, &expected_root)) |
| 60 | .map(|plugin| (plugin.id.clone(), plugin.name().to_string(), plugin.enabled)) |
| 61 | .collect::<Vec<_>>(); |
| 62 | if withdrawn.is_empty() { |
| 63 | return Vec::new(); |
| 64 | } |
| 65 | let mut retired = Vec::new(); |
| 66 | for (id, name, was_enabled) in withdrawn { |
| 67 | let mut record_removed = false; |
| 68 | if let Err(error) = self.commit_state_change(|state| { |
| 69 | record_removed = state.plugins.remove(&id).is_some(); |
| 70 | Ok(()) |
| 71 | }) { |
| 72 | tracing::warn!(target: "plugins", plugin = %name, %error, "withdrawn plugin bundle was not retired"); |
| 73 | continue; |
| 74 | } |
| 75 | self.plugins.remove(&id); |
| 76 | self.names.remove(&name); |
| 77 | let removed = crate::install::uninstall(&name, &plugins_dir); |
| 78 | if !record_removed && removed.is_err() { |
| 79 | if fs::symlink_metadata(plugins_dir.join(&name)).is_ok() |
| 80 | && let Err(error) = &removed |
| 81 | { |
| 82 | tracing::warn!(target: "plugins", plugin = %name, error = %format!("{error:#}"), "withdrawn plugin bundle directory could not be removed"); |
| 83 | } |
| 84 | continue; |
| 85 | } |
| 86 | let details = serde_json::json!({ |
| 87 | "plugin": name, |
| 88 | "id": id.0, |
| 89 | "was_enabled": was_enabled, |
| 90 | "reason": "the desktop account plugins connector was withdrawn in 0.10.3", |
| 91 | "bundle_removed": removed.is_ok(), |
| 92 | }); |
| 93 | match &removed { |
| 94 | Ok(()) => { |
| 95 | tracing::info!(target: "plugins", plugin = %name, "retired withdrawn plugin bundle and its connector") |
| 96 | } |
| 97 | Err(error) => { |
| 98 | tracing::warn!(target: "plugins", plugin = %name, error = %format!("{error:#}"), "withdrawn plugin record retired; bundle directory left in place") |
| 99 | } |
| 100 | } |
| 101 | if let Some(home) = plugins_dir.parent() { |
| 102 | codewhale_runtime::audit::log_sensitive_event_in(home, "plugin.retired", details); |
| 103 | } |
| 104 | retired.push(name); |
| 105 | } |
| 106 | if self.discovery_context.is_some() { |
| 107 | self.catalog_stamp = self.live_catalog_stamp(); |
| 108 | } |
| 109 | retired |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | #[cfg(test)] |
| 114 | mod tests { |
| 115 | use std::fs; |
| 116 | use std::path::Path; |
| 117 | |
| 118 | use crate::discovery::{DiscoveryConfig, discover_with_config}; |
| 119 | |
| 120 | const LEGACY_MCP: &str = r#"{"mcpServers":{"codewhale-plugins":{"type":"streamable-http","url":"https://api.codewhale.net/api/plugins/mcp"}}}"#; |
| 121 | |
| 122 | fn write_bundle(plugins: &Path, name: &str, mcp: &str, host: &str, marker: bool) { |
| 123 | let root = plugins.join(name); |
| 124 | fs::create_dir_all(&root).unwrap(); |
| 125 | fs::write( |
| 126 | root.join("plugin.json"), |
| 127 | format!(r#"{{"$schema":"https://agent-plugins.org/schemas/plugin.json","name":"{name}","version":"1.0.0","description":"test","extensions":{{"net.codewhale":{{"capabilities":{{"network_hosts":["{host}"]}}}}}}}}"#), |
| 128 | ) |
| 129 | .unwrap(); |
| 130 | fs::write(root.join("mcp.json"), mcp).unwrap(); |
| 131 | if marker { |
| 132 | fs::write( |
| 133 | root.join(".installed-from"), |
| 134 | r#"{"schema_version":2,"spec":"path:/tmp/plugin-sources/x","url":null,"source_checksum":"","content_digest":"","installed_name":"x","registry_version":null}"#, |
| 135 | ) |
| 136 | .unwrap(); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | #[test] |
| 141 | fn startup_retires_the_withdrawn_desktop_bundle_and_keeps_user_plugins() { |
| 142 | let tmp = tempfile::tempdir().unwrap(); |
| 143 | let home = tmp.path().join("home"); |
| 144 | let plugins = home.join("plugins"); |
| 145 | let config = DiscoveryConfig { |
| 146 | workspace: tmp.path().join("project"), |
| 147 | user_plugins_dir: plugins.clone(), |
| 148 | workspace_plugins_dir: tmp.path().join("workspace"), |
| 149 | builtin_plugin_dirs: Vec::new(), |
| 150 | state_path: tmp.path().join("state/plugin-state.json"), |
| 151 | }; |
| 152 | write_bundle( |
| 153 | &plugins, |
| 154 | super::DESKTOP_ACCOUNT_PLUGINS, |
| 155 | LEGACY_MCP, |
| 156 | "api.codewhale.net", |
| 157 | true, |
| 158 | ); |
| 159 | write_bundle(&plugins, "keep-me", LEGACY_MCP, "api.codewhale.net", true); |
| 160 | let mut registry = discover_with_config(&config); |
| 161 | registry.disable(super::DESKTOP_ACCOUNT_PLUGINS).unwrap(); |
| 162 | registry.disable("keep-me").unwrap(); |
| 163 | let state = fs::read_to_string(&config.state_path).unwrap(); |
| 164 | assert!(state.contains(super::DESKTOP_ACCOUNT_PLUGINS), "{state}"); |
| 165 | |
| 166 | let retired = registry.retire_withdrawn_bundles(); |
| 167 | |
| 168 | assert_eq!(retired, vec![super::DESKTOP_ACCOUNT_PLUGINS.to_string()]); |
| 169 | assert!(!registry.on_disk_catalog_changed()); |
| 170 | assert!(registry.get(super::DESKTOP_ACCOUNT_PLUGINS).is_none()); |
| 171 | assert!(!plugins.join(super::DESKTOP_ACCOUNT_PLUGINS).exists()); |
| 172 | assert!(registry.get("keep-me").is_some()); |
| 173 | assert!(plugins.join("keep-me").exists()); |
| 174 | let state = fs::read_to_string(&config.state_path).unwrap(); |
| 175 | assert!(!state.contains(super::DESKTOP_ACCOUNT_PLUGINS), "{state}"); |
| 176 | assert!(state.contains("keep-me"), "{state}"); |
| 177 | let audit = fs::read_to_string(home.join("audit.log")).unwrap(); |
| 178 | assert!(audit.contains("\"event\":\"plugin.retired\""), "{audit}"); |
| 179 | assert!(audit.contains(super::DESKTOP_ACCOUNT_PLUGINS), "{audit}"); |
| 180 | |
| 181 | let mut again = discover_with_config(&config); |
| 182 | assert!(again.retire_withdrawn_bundles().is_empty()); |
| 183 | assert!(again.get("keep-me").is_some()); |
| 184 | } |
| 185 | |
| 186 | #[test] |
| 187 | fn a_same_named_bundle_the_person_changed_or_placed_by_hand_is_left_alone() { |
| 188 | let tmp = tempfile::tempdir().unwrap(); |
| 189 | let plugins = tmp.path().join("home/plugins"); |
| 190 | let config = DiscoveryConfig { |
| 191 | workspace: tmp.path().join("project"), |
| 192 | user_plugins_dir: plugins.clone(), |
| 193 | workspace_plugins_dir: tmp.path().join("workspace"), |
| 194 | builtin_plugin_dirs: Vec::new(), |
| 195 | state_path: tmp.path().join("state/plugin-state.json"), |
| 196 | }; |
| 197 | write_bundle( |
| 198 | &plugins, |
| 199 | super::DESKTOP_ACCOUNT_PLUGINS, |
| 200 | r#"{"mcpServers":{"mine":{"type":"streamable-http","url":"https://example.com/mcp"}}}"#, |
| 201 | "example.com", |
| 202 | true, |
| 203 | ); |
| 204 | let mut registry = discover_with_config(&config); |
| 205 | assert!(registry.get(super::DESKTOP_ACCOUNT_PLUGINS).is_some()); |
| 206 | assert!(registry.retire_withdrawn_bundles().is_empty()); |
| 207 | assert!(plugins.join(super::DESKTOP_ACCOUNT_PLUGINS).exists()); |
| 208 | |
| 209 | fs::remove_dir_all(plugins.join(super::DESKTOP_ACCOUNT_PLUGINS)).unwrap(); |
| 210 | write_bundle( |
| 211 | &plugins, |
| 212 | super::DESKTOP_ACCOUNT_PLUGINS, |
| 213 | LEGACY_MCP, |
| 214 | "api.codewhale.net", |
| 215 | false, |
| 216 | ); |
| 217 | let mut registry = discover_with_config(&config); |
| 218 | assert!(registry.get(super::DESKTOP_ACCOUNT_PLUGINS).is_some()); |
| 219 | assert!(registry.retire_withdrawn_bundles().is_empty()); |
| 220 | assert!(plugins.join(super::DESKTOP_ACCOUNT_PLUGINS).exists()); |
| 221 | } |
| 222 | |
| 223 | #[test] |
| 224 | fn renamed_dir_or_bundle_with_added_components_is_left_alone() { |
| 225 | let tmp = tempfile::tempdir().unwrap(); |
| 226 | let home = tmp.path().join("home"); |
| 227 | let plugins = home.join("plugins"); |
| 228 | let config = DiscoveryConfig { |
| 229 | workspace: tmp.path().join("project"), |
| 230 | user_plugins_dir: plugins.clone(), |
| 231 | workspace_plugins_dir: tmp.path().join("workspace"), |
| 232 | builtin_plugin_dirs: Vec::new(), |
| 233 | state_path: tmp.path().join("state/plugin-state.json"), |
| 234 | }; |
| 235 | |
| 236 | let renamed = plugins.join("renamed-dir"); |
| 237 | fs::create_dir_all(&renamed).unwrap(); |
| 238 | fs::write( |
| 239 | renamed.join("plugin.json"), |
| 240 | format!( |
| 241 | r#"{{"$schema":"https://agent-plugins.org/schemas/plugin.json","name":"{}","version":"1.0.0","description":"test","extensions":{{"net.codewhale":{{"capabilities":{{"network_hosts":["api.codewhale.net"]}}}}}}}}"#, |
| 242 | super::DESKTOP_ACCOUNT_PLUGINS |
| 243 | ), |
| 244 | ) |
| 245 | .unwrap(); |
| 246 | fs::write(renamed.join("mcp.json"), LEGACY_MCP).unwrap(); |
| 247 | fs::write( |
| 248 | renamed.join(".installed-from"), |
| 249 | r#"{"schema_version":2,"spec":"path:/tmp/plugin-sources/x","url":null,"source_checksum":"","content_digest":"","installed_name":"x","registry_version":null}"#, |
| 250 | ) |
| 251 | .unwrap(); |
| 252 | |
| 253 | let mut registry = discover_with_config(&config); |
| 254 | assert!(registry.get(super::DESKTOP_ACCOUNT_PLUGINS).is_some()); |
| 255 | assert!(registry.retire_withdrawn_bundles().is_empty()); |
| 256 | assert!(renamed.exists()); |
| 257 | assert!(!home.join("audit.log").exists()); |
| 258 | |
| 259 | fs::remove_dir_all(&renamed).unwrap(); |
| 260 | write_bundle( |
| 261 | &plugins, |
| 262 | super::DESKTOP_ACCOUNT_PLUGINS, |
| 263 | LEGACY_MCP, |
| 264 | "api.codewhale.net", |
| 265 | true, |
| 266 | ); |
| 267 | let skill_dir = plugins |
| 268 | .join(super::DESKTOP_ACCOUNT_PLUGINS) |
| 269 | .join("skills/extra"); |
| 270 | fs::create_dir_all(&skill_dir).unwrap(); |
| 271 | fs::write( |
| 272 | skill_dir.join("SKILL.md"), |
| 273 | "---\nname: extra\ndescription: added by user\n---\n", |
| 274 | ) |
| 275 | .unwrap(); |
| 276 | |
| 277 | let mut registry = discover_with_config(&config); |
| 278 | assert!(registry.get(super::DESKTOP_ACCOUNT_PLUGINS).is_some()); |
| 279 | assert!(registry.retire_withdrawn_bundles().is_empty()); |
| 280 | assert!(plugins.join(super::DESKTOP_ACCOUNT_PLUGINS).exists()); |
| 281 | } |
| 282 | } |
| 283 |