diff --git a/DESIGN.md b/DESIGN.md index 0ebe146..a2b3991 100644 --- a/DESIGN.md +++ b/DESIGN.md @@ -23,7 +23,12 @@ Profiles contain typed `Spec` values. `profile::Store` reads the store once per resolution, then resolves inheritance in memory with cycle detection and cached results. Child entries replace inherited entries with matching identities; partially overlapping skill/subagent entries retain the other names. Agent lists -are resolved before comparison with installed claims. +are resolved before comparison with installed claims. An installed plugin claims +each skill it ships under its marketplace's source. Keys hold a repository +source as its identity, so `o/r` and `https://github.com/o/r.git` match; an +`owner/repo` spelling always means the repository, even if a directory has that +name. A path that is absolute, starts with `./`, `../` or `~/`, or is more than +one segment deep stays as written. `apply::compare` reports present entries, missing entries, and extras. Planning asks each `Harness` (`plan_install`) to turn missing entries into actions, and reuses diff --git a/README.md b/README.md index 95f0c52..084b851 100644 --- a/README.md +++ b/README.md @@ -213,6 +213,8 @@ installed item the profile does not want, after showing the plan; items with no (plugins without a marketplace, skills `npx skills` cannot see) are flagged because no profile can bring them back. An item shared with a wanted entry, such as a skill for two agents of which the profile wants one, stays. +A skill installed through its owning plugin counts as there, so that plugin and its marketplace +stay too. `use` acts on one level only. In a project it installs into that project (Claude plugins at `local` scope, so only that project has them) and counts only what the project holds; with `-g` diff --git a/crates/skillz-harness/src/agents/codex/plugins.rs b/crates/skillz-harness/src/agents/codex/plugins.rs index 6ba36e2..0eaaf44 100644 --- a/crates/skillz-harness/src/agents/codex/plugins.rs +++ b/crates/skillz-harness/src/agents/codex/plugins.rs @@ -8,6 +8,7 @@ use crate::Spec; use crate::action::{Action, Edit, RemovalTarget, TomlValue, apply_edit}; use crate::agents::PluginOffer; use crate::agents::plugin::{declared_skill_paths, read_marketplace, tolerate_malformed}; +use crate::clone::repo_identity; use crate::env::Env; use crate::hooks; use crate::model::{ @@ -192,22 +193,6 @@ fn replaces_user_copy(user: &DocumentMut, marketplace: &str, source: &str, git_r }) } -/// A git source as `host/owner/repo`, whichever way it is spelled: shorthand, https, ssh or scp -/// form, with or without `.git`, a trailing slash or capitals. -fn repo_identity(source: &str) -> String { - let url = crate::clone::clone_url(source).to_ascii_lowercase(); - let rest = url.split_once("://").map_or(url.as_str(), |(_, rest)| rest); - let rest = match rest.split_once('@') { - Some((user, after)) if !user.contains('/') => after, - _ => rest, - }; - rest.replacen(':', "/", 1) - .trim_end_matches('/') - .trim_end_matches(".git") - .trim_end_matches('/') - .to_string() -} - /// Whether a git URL carries a secret, by what `redact` would hide: userinfo, a query string. The /// account of an ssh URL (`ssh://git@host`) names a user, not a secret, so it is set aside first. fn has_credentials(url: &str) -> bool { diff --git a/crates/skillz-harness/src/clone.rs b/crates/skillz-harness/src/clone.rs index e3dd2a1..d669f50 100644 --- a/crates/skillz-harness/src/clone.rs +++ b/crates/skillz-harness/src/clone.rs @@ -81,6 +81,33 @@ pub(crate) fn clone_url(source: &str) -> String { } } +/// A git source as `host/owner/repo`, whichever way it is spelled: shorthand, https, ssh or scp +/// form, with or without `.git`, a trailing slash or capitals. +pub(crate) fn repo_identity(source: &str) -> String { + let url = clone_url(source).to_ascii_lowercase(); + let rest = url.split_once("://").map_or(url.as_str(), |(_, rest)| rest); + let rest = match rest.split_once('@') { + Some((user, after)) if !user.contains('/') => after, + _ => rest, + }; + rest.replacen(':', "/", 1) + .trim_end_matches('/') + .trim_end_matches(".git") + .trim_end_matches('/') + .to_string() +} + +/// A repository source (a URL, scp form or `owner/repo`) as its `repo_identity`; anything else +/// exactly as written, since only the filesystem knows whether two spellings of a path are the +/// same directory. +pub fn source_identity(source: &str) -> String { + if check_repo_name(source).is_ok() { + repo_identity(source) + } else { + source.to_string() + } +} + fn clone_command(source: &str, dest: &Path) -> Cmd { let args = [ "clone", @@ -109,6 +136,29 @@ mod tests { assert_eq!(clone_url("a/b"), "https://github.com/a/b.git"); } + #[test] + fn a_repository_has_one_identity_however_it_is_spelled() { + let spellings = [ + "o/r", + "https://github.com/o/r.git", + "ssh://git@github.com/o/r", + "git@github.com:o/r.git", + ]; + + let identities: Vec = spellings.iter().map(|s| source_identity(s)).collect(); + + assert!(identities.iter().all(|id| *id == identities[0])); + } + + #[test] + fn a_source_that_is_no_repository_stays_as_written() { + let written = ["/src/Skills", "./x/Y", "", "repo"]; + + let identities: Vec = written.iter().map(|s| source_identity(s)).collect(); + + assert_eq!(identities, written); + } + #[test] fn option_looking_and_blank_sources_are_refused() { // both would be read by git/npx as options or nothing diff --git a/src/app/apply/standing.rs b/src/app/apply/standing.rs index a9249a6..9437c3b 100644 --- a/src/app/apply/standing.rs +++ b/src/app/apply/standing.rs @@ -6,7 +6,7 @@ use std::collections::HashSet; use crate::app::profile::{Claim, Profile, claims, installed_claims, lacking}; use crate::app::snapshot; use skillz_harness::Spec; -use skillz_harness::model::{Installed, Kind}; +use skillz_harness::model::{Installed, Kind, split_plugin_id}; /// How a profile's entries and the installed items overlap. #[derive(Debug)] @@ -67,9 +67,10 @@ pub fn compare(wanted: &[Spec], installed: &[Installed]) -> Standing { None => present.push(entry.clone()), } } + let drawn_on = marketplaces_drawn_on(&wanted, &held, &wanted_claims); let extras = held - .into_iter() - .filter_map(|(item, held)| extra(item, &held, &wanted_claims, &wanted)) + .iter() + .filter_map(|(item, held)| extra(item, held, &wanted_claims, &drawn_on)) .collect(); Standing { present, @@ -100,16 +101,36 @@ fn claims_of(item: &Installed) -> Vec { item.parts().iter().flat_map(installed_claims).collect() } +/// The marketplaces a wanted entry draws on: those of wanted plugins, and those of installed +/// plugins holding a wanted claim, such as a wanted skill they ship. +fn marketplaces_drawn_on<'a>( + wanted: &'a [Spec], + held: &[(&'a Installed, Vec)], + wanted_claims: &HashSet, +) -> HashSet<&'a str> { + let of_wanted_plugins = wanted.iter().filter_map(|entry| match entry { + Spec::Plugin { marketplace, .. } => Some(marketplace.as_str()), + _ => None, + }); + let of_held_plugins = held + .iter() + .filter(|(item, claims)| { + item.kind == Kind::Plugin && claims.iter().any(|c| wanted_claims.contains(c)) + }) + .map(|(item, _)| split_plugin_id(&item.name).1); + of_wanted_plugins.chain(of_held_plugins).collect() +} + /// An item is wanted when any one of its claims is, so one shared with a wanted entry stays. -/// A marketplace is wanted while a wanted plugin comes from it. +/// A marketplace is wanted while a wanted entry draws on it. fn extra( item: &Installed, held: &[Claim], wanted_claims: &HashSet, - wanted: &[Spec], + drawn_on: &HashSet<&str>, ) -> Option { if item.kind == Kind::Marketplace { - let used = wanted.iter().any(|e| draws_on(e, &item.name)); + let used = drawn_on.contains(item.name.as_str()); return (!used).then(|| Extra { item: item.clone(), sourced: true, @@ -127,18 +148,18 @@ fn extra( }) } -fn draws_on(entry: &Spec, marketplace: &str) -> bool { - matches!(entry, Spec::Plugin { marketplace: m, .. } if m == marketplace) -} - #[cfg(test)] mod tests { use super::*; - use skillz_harness::model::{Agent, Installer, Scope}; + use skillz_harness::model::{Agent, Details, Installer, Scope}; fn skill_entry(names: &[&str]) -> Spec { + skill_entry_from("o/r", names) + } + + fn skill_entry_from(source: &str, names: &[&str]) -> Spec { Spec::Skill { - source: "o/r".into(), + source: source.into(), names: names.iter().map(|n| n.to_string()).collect(), agents: vec![Agent::Claude], } @@ -225,6 +246,94 @@ mod tests { assert_eq!(not_wanting.extras.len(), 1); } + /// Claude's plugin `p@mp`, from a marketplace at `source`, shipping `shipped`. + fn plugin_from(source: &str, shipped: &[&str]) -> Installed { + Installed { + details: Details::Plugin { + location: "/plugins/p".into(), + skills: shipped.iter().map(|s| s.to_string()).collect(), + }, + installer: Installer::ClaudeCli, + ..installed("p@mp", Kind::Plugin, Some(source)) + } + } + + fn marketplace_mp() -> Installed { + Installed { + installer: Installer::ClaudeCli, + ..installed("mp", Kind::Marketplace, Some("o/r")) + } + } + + #[test] + fn a_skill_its_plugin_ships_is_present_and_the_plugin_is_wanted() { + let live = [plugin_from("o/r", &["a"])]; + + let standing = compare(&[skill_entry(&["a"])], &live); + + assert_eq!(standing.present, [skill_entry(&["a"])]); + assert!(standing.missing.is_empty()); + assert!(standing.extras.is_empty()); + } + + #[test] + fn a_skill_its_plugin_ships_is_present_however_the_source_is_spelled() { + let url = "https://github.com/o/r.git"; + for (wanted, recorded) in [(url, "o/r"), ("o/r", url)] { + let entry = skill_entry_from(wanted, &["a"]); + + let standing = compare( + std::slice::from_ref(&entry), + &[plugin_from(recorded, &["a"])], + ); + + assert_eq!(standing.present, [entry]); + assert!(standing.extras.is_empty()); + } + } + + #[test] + fn only_the_skills_the_plugin_does_not_ship_are_missing() { + let live = [plugin_from("o/r", &["a"])]; + + let standing = compare(&[skill_entry(&["a", "b"])], &live); + + assert_eq!(standing.missing, [skill_entry(&["b"])]); + assert!(standing.extras.is_empty()); + } + + #[test] + fn a_plugin_and_its_marketplace_stay_only_while_a_wanted_skill_comes_through_them() { + let live = [plugin_from("o/r", &["a"]), marketplace_mp()]; + + let wanting = compare(&[skill_entry(&["a"])], &live); + let not_wanting = compare(&[], &live); + + assert!(wanting.extras.is_empty()); + let extra_kinds: Vec = not_wanting.extras.iter().map(|e| e.item.kind).collect(); + assert_eq!(extra_kinds, [Kind::Plugin, Kind::Marketplace]); + } + + #[test] + fn a_plugin_does_not_ship_the_same_skill_name_from_another_source() { + let entry = skill_entry_from("o/other", &["a"]); + + let standing = compare(std::slice::from_ref(&entry), &[plugin_from("o/r", &["a"])]); + + assert_eq!(standing.missing, [entry]); + assert_eq!(standing.extras.len(), 1); + } + + #[test] + fn a_plugin_for_one_agent_leaves_the_skill_missing_for_another() { + let entry = skill_entry(&["a"]).with_agents(vec![Agent::Claude, Agent::Codex]); + + let standing = compare(std::slice::from_ref(&entry), &[plugin_from("o/r", &["a"])]); + + assert_eq!(standing.missing, [entry]); + assert!(standing.extras.is_empty()); + } + fn profile(name: &str, skills: &[&str]) -> Profile { let mut p = Profile::new(name); p.entries = vec![skill_entry(skills)]; diff --git a/src/app/apply/tests.rs b/src/app/apply/tests.rs index df4f7c4..fc6774b 100644 --- a/src/app/apply/tests.rs +++ b/src/app/apply/tests.rs @@ -656,3 +656,91 @@ fn an_agent_the_marketplace_does_not_supply_keeps_npx_for_itself_alone() { .collect(); assert_eq!(npx_runs, [&codex_only]); } + +/// Claude's records once `claude plugin install p1@mp --scope local` has run in the project: +/// marketplace `mp` added from `o/r`, and the plugin's tree shipping `alpha`. +fn record_p1_installed_for_claude(fx: &Fx) { + let plugins = fx.env.home.join(".claude/plugins"); + let tree = plugins.join("cache/mp/p1/1.0"); + skill(&tree, "skills/alpha"); + let marketplace = serde_json::json!({"source": {"source": "github", "repo": "o/r"}}); + let installed = serde_json::json!({"version": 2, "plugins": {"p1@mp": [ + {"scope": "local", "projectPath": fx.env.project, "installPath": tree} + ]}}); + fs::write( + plugins.join("known_marketplaces.json"), + serde_json::json!({"mp": marketplace}).to_string(), + ) + .unwrap(); + fs::write( + plugins.join("installed_plugins.json"), + installed.to_string(), + ) + .unwrap(); + let settings = fx.env.project.join(".claude/settings.local.json"); + fs::create_dir_all(settings.parent().unwrap()).unwrap(); + let declared = serde_json::json!({ + "extraKnownMarketplaces": {"mp": marketplace}, + "enabledPlugins": {"p1@mp": true} + }); + fs::write(settings, declared.to_string()).unwrap(); +} + +const URL: &str = "https://github.com/o/r.git"; + +/// The commands of a first plan for skill `alpha` of `source`, which `p1@mp` ships. +fn first_plan_for_alpha(source: &str) -> Vec<(String, Vec)> { + let fx = Fx::new(); + let fetch = FakeRunner::new(supplied_and_loose_fixture); + let entry = skill_entry(source, &["alpha"], &[Agent::Claude]); + commands(&planned_steps(&fx, &fetch, Scope::Project, vec![entry])) +} + +/// An exact plan for skill `alpha` of `source` once Claude holds `p1@mp`, which ships it. +fn exact_plan_for_alpha_after_p1_installed(source: &str) -> Switch { + let fx = Fx::new(); + let fetch = FakeRunner::new(supplied_and_loose_fixture); + record_p1_installed_for_claude(&fx); + let mut library = Profile::new("library"); + library.entries = vec![skill_entry(source, &["alpha"], &[Agent::Claude])]; + let applier = Applier { + runner: &fetch, + ..fx.applier() + }; + applier.plan(&library, true).unwrap() +} + +fn claude_installs_p1() -> (String, Vec) { + let install = s(&["plugin", "install", "p1@mp", "--scope", "local"]); + ("claude".into(), install) +} + +#[test] +fn a_library_skill_from_a_shorthand_source_installs_its_plugin_through_claude() { + let commands = first_plan_for_alpha("o/r"); + + assert!(commands.contains(&claude_installs_p1())); +} + +#[test] +fn a_library_skill_from_a_url_source_installs_its_plugin_through_claude() { + let commands = first_plan_for_alpha(URL); + + assert!(commands.contains(&claude_installs_p1())); +} + +#[test] +fn a_shorthand_skill_its_installed_plugin_ships_plans_nothing_under_exact() { + let again = exact_plan_for_alpha_after_p1_installed("o/r"); + + assert!(again.is_empty()); + assert_eq!(again.present.len(), 1); +} + +#[test] +fn a_url_skill_its_installed_plugin_ships_plans_nothing_under_exact() { + let again = exact_plan_for_alpha_after_p1_installed(URL); + + assert!(again.is_empty()); + assert_eq!(again.present.len(), 1); +} diff --git a/src/app/profile/identity.rs b/src/app/profile/identity.rs index bba083f..c842676 100644 --- a/src/app/profile/identity.rs +++ b/src/app/profile/identity.rs @@ -5,10 +5,15 @@ //! replaces a parent entry holding the same key; a parent entry that only partly overlaps keeps //! its other names, and disappears when none are left. The agents of an entry are not part of its //! identity: the child's agents win along with the entry. +//! +//! A skill or subagent key holds a repository source as its identity, so `o/r` and +//! `https://github.com/o/r.git` name the same source. An `owner/repo` spelling always means the +//! repository; any other path stays as written (`source_identity`). use std::collections::HashSet; use skillz_harness::Spec; +use skillz_harness::clone::source_identity; use skillz_harness::model::{Agent, Details, Installed, Installer, Kind, split_plugin_id}; #[derive(Debug, Clone, PartialEq, Eq, Hash)] @@ -55,6 +60,8 @@ pub(crate) fn installed_claims(item: &Installed) -> Vec { .collect() } +/// A plugin also holds each skill it ships, from its marketplace's source, so a skill entry +/// installed through its owning plugin is in place. pub(crate) fn installed_keys(item: &Installed) -> Vec { let source = || match item.installer { Installer::Npx => item.source.as_ref(), @@ -67,20 +74,26 @@ pub(crate) fn installed_keys(item: &Installed) -> Vec { Details::Skill { install_name, .. } => install_name, _ => &item.name, }; - vec![Key::Skill(source.clone(), name.clone())] + vec![sourced(Key::Skill, source, name)] }) .unwrap_or_default(), Kind::Subagent => match (source(), &item.details) { (Some(source), Details::Subagent { file }) => file .file_stem() .and_then(|stem| stem.to_str()) - .map(|name| vec![Key::Subagent(source.clone(), name.into())]) + .map(|name| vec![sourced(Key::Subagent, source, name)]) .unwrap_or_default(), _ => vec![], }, Kind::Plugin => { let (name, marketplace) = split_plugin_id(&item.name); - vec![Key::Plugin(marketplace.into(), name.into())] + let shipped = source().into_iter().flat_map(|source| { + item.plugin_skills() + .map(move |skill| sourced(Key::Skill, source, skill)) + }); + std::iter::once(Key::Plugin(marketplace.into(), name.into())) + .chain(shipped) + .collect() } Kind::Hook => item .hooks @@ -133,10 +146,14 @@ pub(crate) fn keys(entry: &Spec) -> Vec { fn per_name(source: &str, names: &[String], key: fn(String, String) -> Key) -> Vec { names .iter() - .map(|name| key(source.to_string(), name.clone())) + .map(|name| sourced(key, source, name)) .collect() } +fn sourced(key: fn(String, String) -> Key, source: &str, name: &str) -> Key { + key(source_identity(source), name.into()) +} + /// The entry minus the names whose key is taken; `None` when nothing of it is left. fn without(mut entry: Spec, taken: &HashSet) -> Option { let all = keys(&entry); @@ -154,7 +171,6 @@ fn without(mut entry: Spec, taken: &HashSet) -> Option { #[cfg(test)] mod tests { use super::*; - use skillz_harness::model::Agent; fn skills(source: &str, names: &[&str]) -> Spec { Spec::Skill { @@ -187,12 +203,12 @@ mod tests { #[test] fn only_the_names_missing_for_some_agent_are_lacking() { let entry = skills("o/r", &["a", "b"]).with_agents(vec![Agent::Claude, Agent::Codex]); - let have: HashSet = [ - (Key::Skill("o/r".into(), "a".into()), Agent::Claude), - (Key::Skill("o/r".into(), "a".into()), Agent::Codex), - (Key::Skill("o/r".into(), "b".into()), Agent::Claude), - ] - .into(); + let both = skills("o/r", &["a"]).with_agents(vec![Agent::Claude, Agent::Codex]); + let claude_only = skills("o/r", &["b"]).with_agents(vec![Agent::Claude]); + let have: HashSet = claims(&both) + .into_iter() + .chain(claims(&claude_only)) + .collect(); let lacking = lacking(&entry, &have); @@ -263,6 +279,80 @@ mod tests { assert!(!same_thing(&a, &mcp("a", "x"))); } + #[test] + fn a_source_is_the_same_however_it_is_spelled() { + let short = skills("o/r", &["a"]); + + assert!(same_thing( + &short, + &skills("https://github.com/o/r.git", &["a"]) + )); + assert!(same_thing( + &short, + &skills("git@github.com:O/R.git", &["a"]) + )); + assert!(!same_thing(&short, &skills("o/other", &["a"]))); + } + + #[test] + fn local_paths_differing_only_by_case_are_different_sources() { + let lower = skills("/src/skills", &["a"]); + + assert!(!same_thing(&lower, &skills("/src/Skills", &["a"]))); + assert!(same_thing(&lower, &skills("/src/skills", &["a"]))); + } + + fn installed(name: &str, kind: Kind, source: Option<&str>) -> Installed { + Installed { + details: Default::default(), + hooks: vec![], + members: vec![], + id: format!("{kind}:{name}"), + name: name.into(), + kind, + agents: vec![Agent::Claude], + scope: skillz_harness::model::Scope::Project, + group: "g".into(), + source: source.map(Into::into), + installer: Installer::Npx, + origin: Default::default(), + children: vec![], + removal: vec![], + } + } + + fn plugin_shipping(source: Option<&str>, shipped: &[&str]) -> Installed { + Installed { + details: Details::Plugin { + location: "/plugins/p".into(), + skills: shipped.iter().map(|s| s.to_string()).collect(), + }, + installer: Installer::ClaudeCli, + ..installed("p@mp", Kind::Plugin, source) + } + } + + #[test] + fn a_plugin_holds_itself_and_each_skill_it_ships_from_its_source() { + let plugin = plugin_shipping(Some("o/r"), &["a", "b"]); + + let held = installed_keys(&plugin); + + let mut expected = vec![Key::Plugin("mp".into(), "p".into())]; + expected.extend(keys(&skills("https://github.com/o/r.git", &["a", "b"]))); + assert_eq!(held, expected); + } + + #[test] + fn a_plugin_without_a_known_source_holds_only_itself() { + let plugin = plugin_shipping(None, &["a"]); + + assert_eq!( + installed_keys(&plugin), + [Key::Plugin("mp".into(), "p".into())] + ); + } + #[test] fn a_plugin_is_identified_by_marketplace_and_name() { let plugin = |source: &str| Spec::Plugin {