| 1 | use std::fs; |
| 2 | use std::path::{Path, PathBuf}; |
| 3 | |
| 4 | use codewhale_plugins::activation::PluginActivationCapability; |
| 5 | use codewhale_plugins::discovery::{DiscoveryConfig, discover_with_config}; |
| 6 | use codewhale_plugins::manifest::{PluginCompatibility, capability_hash_v1, capability_hash_v2}; |
| 7 | use codewhale_plugins::types::{PluginDiagnosticLevel, PluginTrustStatus}; |
| 8 | |
| 9 | fn config(root: &Path) -> DiscoveryConfig { |
| 10 | DiscoveryConfig { |
| 11 | workspace: root.join("project"), |
| 12 | user_plugins_dir: root.join("user"), |
| 13 | workspace_plugins_dir: root.join("workspace"), |
| 14 | builtin_plugin_dirs: Vec::new(), |
| 15 | state_path: root.join("state/plugin-state.json"), |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | fn write_plugin(config: &DiscoveryConfig, extra: &str) -> PathBuf { |
| 20 | write_named_plugin(config, "demo", extra) |
| 21 | } |
| 22 | |
| 23 | fn write_named_plugin(config: &DiscoveryConfig, name: &str, extra: &str) -> PathBuf { |
| 24 | let plugin = config.user_plugins_dir.join(name); |
| 25 | fs::create_dir_all(&plugin).unwrap(); |
| 26 | fs::write( |
| 27 | plugin.join("plugin.toml"), |
| 28 | format!("schema_version = 1\n[plugin]\nname = {name:?}\nversion = \"1.0.0\"\n{extra}"), |
| 29 | ) |
| 30 | .unwrap(); |
| 31 | plugin |
| 32 | } |
| 33 | |
| 34 | fn write_mixed_bundle(config: &DiscoveryConfig) -> PathBuf { |
| 35 | let plugin = write_plugin( |
| 36 | config, |
| 37 | "\n[skills]\npath = \"skills\"\n[commands]\npath = \"commands\"\n[hooks]\npath = \"hooks\"\n[lsp]\npath = \"lsp\"\n", |
| 38 | ); |
| 39 | fs::create_dir_all(plugin.join("skills/demo")).unwrap(); |
| 40 | fs::write( |
| 41 | plugin.join("skills/demo/SKILL.md"), |
| 42 | "---\nname: demo\ndescription: first\n---\nbody\n", |
| 43 | ) |
| 44 | .unwrap(); |
| 45 | fs::create_dir_all(plugin.join("commands")).unwrap(); |
| 46 | fs::create_dir_all(plugin.join("hooks")).unwrap(); |
| 47 | fs::create_dir_all(plugin.join("lsp")).unwrap(); |
| 48 | plugin |
| 49 | } |
| 50 | |
| 51 | fn replace_capability_hashes(value: &mut serde_json::Value, from: &str, to: &str) { |
| 52 | match value { |
| 53 | serde_json::Value::Object(map) => { |
| 54 | for (key, child) in map.iter_mut() { |
| 55 | if key == "capability_hash" && child.as_str() == Some(from) { |
| 56 | *child = serde_json::Value::String(to.to_string()); |
| 57 | } else { |
| 58 | replace_capability_hashes(child, from, to); |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | serde_json::Value::Array(items) => { |
| 63 | for item in items { |
| 64 | replace_capability_hashes(item, from, to); |
| 65 | } |
| 66 | } |
| 67 | _ => {} |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | #[test] |
| 72 | fn mixed_supported_and_unsupported_components_activate_only_supported_surfaces() { |
| 73 | let tmp = tempfile::tempdir().unwrap(); |
| 74 | let config = config(tmp.path()); |
| 75 | write_mixed_bundle(&config); |
| 76 | |
| 77 | let mut registry = discover_with_config(&config); |
| 78 | let plugin = registry.get("demo").unwrap(); |
| 79 | assert_eq!(plugin.compatibility(), PluginCompatibility::Partial); |
| 80 | assert_eq!( |
| 81 | plugin.inventory.supported_labels(), |
| 82 | vec!["skills", "commands", "hooks"] |
| 83 | ); |
| 84 | assert_eq!(plugin.inventory.unsupported_labels(), vec!["lsp"]); |
| 85 | assert!( |
| 86 | plugin.diagnostics.iter().any(|diagnostic| { |
| 87 | diagnostic.level == PluginDiagnosticLevel::Warning |
| 88 | && diagnostic.code == "component-inactive" |
| 89 | && diagnostic.message.contains("lsp") |
| 90 | }), |
| 91 | "inactive components must stay visible in diagnostics: {:?}", |
| 92 | plugin.diagnostics |
| 93 | ); |
| 94 | |
| 95 | registry.trust("demo").unwrap(); |
| 96 | registry.enable("demo").unwrap(); |
| 97 | let plugin = registry.get("demo").unwrap(); |
| 98 | assert!(plugin.active()); |
| 99 | assert_eq!(plugin.state_label(), "active"); |
| 100 | assert_eq!(plugin.compatibility(), PluginCompatibility::Partial); |
| 101 | assert_eq!(plugin.inventory.commands, 1); |
| 102 | assert_eq!(plugin.inventory.hooks, 1); |
| 103 | assert_eq!(plugin.skill_snapshots.len(), 1); |
| 104 | assert_eq!(plugin.skill_snapshots[0].name, "demo"); |
| 105 | assert!(plugin.component_active(PluginActivationCapability::Skills)); |
| 106 | assert!(plugin.component_active(PluginActivationCapability::Commands)); |
| 107 | assert!(plugin.component_active(PluginActivationCapability::Hooks)); |
| 108 | assert!(!plugin.component_active(PluginActivationCapability::Lsp)); |
| 109 | |
| 110 | let skills = codewhale_skills::discover_from_directories_with_plugins( |
| 111 | Vec::<PathBuf>::new(), |
| 112 | Some(®istry), |
| 113 | ); |
| 114 | assert_eq!(skills.get("demo:demo").unwrap().body.trim(), "body"); |
| 115 | } |
| 116 | |
| 117 | #[test] |
| 118 | fn mixed_bundle_revocation_and_trust_changes_deactivate_supported_surfaces() { |
| 119 | let tmp = tempfile::tempdir().unwrap(); |
| 120 | let config = config(tmp.path()); |
| 121 | let plugin = write_mixed_bundle(&config); |
| 122 | |
| 123 | let mut registry = discover_with_config(&config); |
| 124 | registry.trust("demo").unwrap(); |
| 125 | registry.enable("demo").unwrap(); |
| 126 | assert!(registry.is_active("demo")); |
| 127 | |
| 128 | registry.revoke_trust("demo").unwrap(); |
| 129 | let revoked = registry.get("demo").unwrap(); |
| 130 | assert!(revoked.enabled); |
| 131 | assert!(!revoked.trusted()); |
| 132 | assert!(!revoked.active()); |
| 133 | assert_eq!(revoked.compatibility(), PluginCompatibility::Partial); |
| 134 | let skills = codewhale_skills::discover_from_directories_with_plugins( |
| 135 | Vec::<PathBuf>::new(), |
| 136 | Some(®istry), |
| 137 | ); |
| 138 | assert!( |
| 139 | skills.get("demo:demo").is_none(), |
| 140 | "revoking trust must drop the supported Skill adapter" |
| 141 | ); |
| 142 | |
| 143 | registry.trust("demo").unwrap(); |
| 144 | assert!( |
| 145 | !registry.is_active("demo"), |
| 146 | "re-trust must not reuse the previous enablement bit" |
| 147 | ); |
| 148 | registry.enable("demo").unwrap(); |
| 149 | assert!(registry.is_active("demo")); |
| 150 | |
| 151 | fs::write( |
| 152 | plugin.join("skills/demo/SKILL.md"), |
| 153 | "---\nname: demo\ndescription: changed\n---\nbody two\n", |
| 154 | ) |
| 155 | .unwrap(); |
| 156 | let changed = discover_with_config(&config); |
| 157 | let plugin = changed.get("demo").unwrap(); |
| 158 | assert_eq!(plugin.trust_status, PluginTrustStatus::ContentChanged); |
| 159 | assert!(plugin.enabled); |
| 160 | assert!(!plugin.active()); |
| 161 | let skills = codewhale_skills::discover_from_directories_with_plugins( |
| 162 | Vec::<PathBuf>::new(), |
| 163 | Some(&changed), |
| 164 | ); |
| 165 | assert!(skills.get("demo:demo").is_none()); |
| 166 | } |
| 167 | |
| 168 | #[test] |
| 169 | fn legacy_trust_receipts_fail_closed_as_needs_review_under_v3() { |
| 170 | let tmp = tempfile::tempdir().unwrap(); |
| 171 | let config = config(tmp.path()); |
| 172 | let plugin = write_plugin(&config, "\n[skills]\npath = \"skills\"\n"); |
| 173 | fs::create_dir_all(plugin.join("skills/demo")).unwrap(); |
| 174 | fs::write( |
| 175 | plugin.join("skills/demo/SKILL.md"), |
| 176 | "---\nname: demo\ndescription: first\n---\nbody\n", |
| 177 | ) |
| 178 | .unwrap(); |
| 179 | |
| 180 | let mut first = discover_with_config(&config); |
| 181 | first.trust("demo").unwrap(); |
| 182 | first.enable("demo").unwrap(); |
| 183 | assert!(first.is_active("demo")); |
| 184 | let plugin = first.get("demo").unwrap(); |
| 185 | let legacy_hashes = [ |
| 186 | ("v1", capability_hash_v1(&plugin.inventory)), |
| 187 | ("v2", capability_hash_v2(&plugin.inventory)), |
| 188 | ]; |
| 189 | let v3_hash = plugin.capability_hash.clone(); |
| 190 | assert!( |
| 191 | legacy_hashes.iter().all(|(_, hash)| hash != &v3_hash), |
| 192 | "v3 receipts must not collide with either historical domain" |
| 193 | ); |
| 194 | let content_hash = plugin.content_hash.clone(); |
| 195 | |
| 196 | let raw = fs::read_to_string(&config.state_path).unwrap(); |
| 197 | for (version, legacy_hash) in legacy_hashes { |
| 198 | let mut parsed: serde_json::Value = serde_json::from_str(&raw).unwrap(); |
| 199 | replace_capability_hashes(&mut parsed, &v3_hash, &legacy_hash); |
| 200 | fs::write( |
| 201 | &config.state_path, |
| 202 | serde_json::to_string_pretty(&parsed).unwrap(), |
| 203 | ) |
| 204 | .unwrap(); |
| 205 | |
| 206 | let restarted = discover_with_config(&config); |
| 207 | let plugin = restarted.get("demo").unwrap(); |
| 208 | assert_eq!(plugin.content_hash, content_hash); |
| 209 | assert_eq!(plugin.capability_hash, v3_hash); |
| 210 | assert_eq!(plugin.trust_status, PluginTrustStatus::CapabilitiesChanged); |
| 211 | assert!(plugin.enabled); |
| 212 | assert!(!plugin.trusted()); |
| 213 | assert!(!plugin.active()); |
| 214 | assert!(restarted.authority_for("demo").is_some()); |
| 215 | let skills = codewhale_skills::discover_from_directories_with_plugins( |
| 216 | Vec::<PathBuf>::new(), |
| 217 | Some(&restarted), |
| 218 | ); |
| 219 | assert!( |
| 220 | skills.get("demo:demo").is_none(), |
| 221 | "a {version} receipt must not activate Skills after the v3 policy binding" |
| 222 | ); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | #[test] |
| 227 | fn workspace_scoped_registries_do_not_cross_load_skills() { |
| 228 | let tmp = tempfile::tempdir().unwrap(); |
| 229 | let left_config = config(&tmp.path().join("left")); |
| 230 | let right_config = config(&tmp.path().join("right")); |
| 231 | for (config, body) in [(&left_config, "left body"), (&right_config, "right body")] { |
| 232 | let plugin = write_plugin(config, "\n[skills]\npath = \"skills\"\n"); |
| 233 | fs::create_dir_all(plugin.join("skills/only")).unwrap(); |
| 234 | fs::write( |
| 235 | plugin.join("skills/only/SKILL.md"), |
| 236 | format!("---\nname: only\ndescription: scoped\n---\n{body}\n"), |
| 237 | ) |
| 238 | .unwrap(); |
| 239 | } |
| 240 | let mut left = discover_with_config(&left_config); |
| 241 | left.trust("demo").unwrap(); |
| 242 | left.enable("demo").unwrap(); |
| 243 | let mut right = discover_with_config(&right_config); |
| 244 | right.trust("demo").unwrap(); |
| 245 | right.enable("demo").unwrap(); |
| 246 | |
| 247 | let left_skills = codewhale_skills::discover_from_directories_with_plugins( |
| 248 | Vec::<PathBuf>::new(), |
| 249 | Some(&left), |
| 250 | ); |
| 251 | let right_skills = codewhale_skills::discover_from_directories_with_plugins( |
| 252 | Vec::<PathBuf>::new(), |
| 253 | Some(&right), |
| 254 | ); |
| 255 | assert_eq!(left_skills.get("demo:only").unwrap().body, "left body"); |
| 256 | assert_eq!(right_skills.get("demo:only").unwrap().body, "right body"); |
| 257 | assert_ne!(left.workspace(), right.workspace()); |
| 258 | } |
| 259 | |
| 260 | /// A DSH bundle package: one remote MCP row and one skill directory. |
| 261 | fn write_dsh_package(root: &Path, url: &str) -> PathBuf { |
| 262 | let package = root.join("dsh-source"); |
| 263 | fs::create_dir_all(package.join("pack-skills/guide")).unwrap(); |
| 264 | fs::write( |
| 265 | package.join("package.json"), |
| 266 | serde_json::json!({"name": "@demo/docs-dsh", "version": "1.0.0", |
| 267 | "dsh": {"bundle": {"patch": "./cordis.patch.yml"}}}) |
| 268 | .to_string(), |
| 269 | ) |
| 270 | .unwrap(); |
| 271 | fs::write( |
| 272 | package.join("cordis.patch.yml"), |
| 273 | format!( |
| 274 | "- insert:\n - id: docs\n name: '@deepseek-ai/dsh-mcp-client'\n config: {{serverName: docs, transport: streamable-http, url: '{url}'}}\n - id: skills\n name: '@deepseek-ai/dsh-skill-filesystem'\n config: {{customSkillDirs: [pack-skills]}}\n - id: theme\n name: '@deepseek-ai/dsh-client-ui-theme'\n" |
| 275 | ), |
| 276 | ) |
| 277 | .unwrap(); |
| 278 | fs::write( |
| 279 | package.join("pack-skills/guide/SKILL.md"), |
| 280 | "---\nname: guide\ndescription: Bundled guide\n---\nBody.\n", |
| 281 | ) |
| 282 | .unwrap(); |
| 283 | package |
| 284 | } |
| 285 | |
| 286 | fn allow_all_network() -> codewhale_net::network_policy::NetworkPolicy { |
| 287 | codewhale_net::network_policy::NetworkPolicy { |
| 288 | default: codewhale_net::network_policy::DecisionToml::Allow, |
| 289 | ..Default::default() |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | fn block_on<F: std::future::Future>(future: F) -> F::Output { |
| 294 | tokio::runtime::Builder::new_multi_thread() |
| 295 | .worker_threads(2) |
| 296 | .enable_all() |
| 297 | .build() |
| 298 | .unwrap() |
| 299 | .block_on(future) |
| 300 | } |
| 301 | |
| 302 | /// DSH import is the ordinary reviewed install: the preview hash is what an |
| 303 | /// exact install stages, the bundle lands disabled and untrusted, update |
| 304 | /// re-converts the recorded package, and a changed package cannot be |
| 305 | /// installed against a stale review or keep its trust. |
| 306 | #[test] |
| 307 | fn dsh_packages_import_through_the_reviewed_install_and_update_flow() { |
| 308 | codewhale_engine::extension_host::install_ports(); |
| 309 | let tmp = tempfile::tempdir().unwrap(); |
| 310 | let _home = codewhale_runtime::test_support::SealedHome::at(tmp.path()); |
| 311 | let config = config(tmp.path()); |
| 312 | let network = allow_all_network(); |
| 313 | let package = write_dsh_package(tmp.path(), "https://docs.example.invalid/mcp"); |
| 314 | let (conversion, reviewed) = codewhale_plugins::install::preview_dsh(&package).unwrap(); |
| 315 | assert_eq!(conversion.plugin_name, "docs-dsh"); |
| 316 | assert_eq!(conversion.remote_servers, ["docs"]); |
| 317 | assert_eq!(conversion.skills, ["guide"]); |
| 318 | assert!( |
| 319 | conversion |
| 320 | .outcomes |
| 321 | .iter() |
| 322 | .any(|o| o.needs_manual_port() && o.row.as_deref() == Some("theme")) |
| 323 | ); |
| 324 | |
| 325 | let source = |
| 326 | codewhale_plugins::install::PluginInstallSource::parse(package.to_str().unwrap()).unwrap(); |
| 327 | assert!( |
| 328 | matches!( |
| 329 | source, |
| 330 | codewhale_plugins::install::PluginInstallSource::Dsh(_) |
| 331 | ), |
| 332 | "{source:?}" |
| 333 | ); |
| 334 | let outcome = block_on( |
| 335 | codewhale_plugins::install::install_with_expected_content_hash( |
| 336 | source, |
| 337 | &config.user_plugins_dir, |
| 338 | codewhale_plugins::install::DEFAULT_MAX_SIZE_BYTES, |
| 339 | &network, |
| 340 | &|_| None, |
| 341 | &reviewed, |
| 342 | ), |
| 343 | ) |
| 344 | .unwrap(); |
| 345 | let codewhale_plugins::install::PluginInstallOutcome::Installed(installed) = outcome else { |
| 346 | panic!("DSH import must install"); |
| 347 | }; |
| 348 | assert_eq!(installed.content_hash, reviewed); |
| 349 | let marker = fs::read_to_string( |
| 350 | config |
| 351 | .user_plugins_dir |
| 352 | .join("docs-dsh") |
| 353 | .join(codewhale_skill_install::install::INSTALLED_FROM_MARKER), |
| 354 | ) |
| 355 | .unwrap(); |
| 356 | assert!(marker.contains("dsh:"), "{marker}"); |
| 357 | |
| 358 | let mut registry = discover_with_config(&config); |
| 359 | let plugin = registry.get("docs-dsh").unwrap(); |
| 360 | assert!(!plugin.enabled && !plugin.trusted()); |
| 361 | registry.trust("docs-dsh").unwrap(); |
| 362 | registry.enable("docs-dsh").unwrap(); |
| 363 | assert!(registry.is_active("docs-dsh")); |
| 364 | |
| 365 | let unchanged = block_on(codewhale_plugins::install::update( |
| 366 | "docs-dsh", |
| 367 | &config.user_plugins_dir, |
| 368 | codewhale_plugins::install::DEFAULT_MAX_SIZE_BYTES, |
| 369 | &network, |
| 370 | )) |
| 371 | .unwrap(); |
| 372 | assert!(matches!( |
| 373 | unchanged, |
| 374 | codewhale_plugins::install::PluginUpdateResult::NoChange |
| 375 | )); |
| 376 | |
| 377 | fs::remove_dir_all(&package).unwrap(); |
| 378 | write_dsh_package(tmp.path(), "https://docs-v2.example.invalid/mcp"); |
| 379 | let stale = block_on( |
| 380 | codewhale_plugins::install::install_with_expected_content_hash( |
| 381 | codewhale_plugins::install::PluginInstallSource::Dsh(package.clone()), |
| 382 | &tmp.path().join("other-plugins"), |
| 383 | codewhale_plugins::install::DEFAULT_MAX_SIZE_BYTES, |
| 384 | &network, |
| 385 | &|_| None, |
| 386 | &reviewed, |
| 387 | ), |
| 388 | ) |
| 389 | .unwrap_err(); |
| 390 | assert!( |
| 391 | stale.to_string().contains("changed after review"), |
| 392 | "{stale:#}" |
| 393 | ); |
| 394 | |
| 395 | let updated = block_on(codewhale_plugins::install::update( |
| 396 | "docs-dsh", |
| 397 | &config.user_plugins_dir, |
| 398 | codewhale_plugins::install::DEFAULT_MAX_SIZE_BYTES, |
| 399 | &network, |
| 400 | )) |
| 401 | .unwrap(); |
| 402 | assert!(matches!( |
| 403 | updated, |
| 404 | codewhale_plugins::install::PluginUpdateResult::Updated(_) |
| 405 | )); |
| 406 | let registry = discover_with_config(&config); |
| 407 | assert!( |
| 408 | !registry.get("docs-dsh").unwrap().trusted(), |
| 409 | "changed converted bytes invalidate the trust receipt" |
| 410 | ); |
| 411 | } |
| 412 |