diff --git a/DESIGN.md b/DESIGN.md index 8bd6fc7..0ebe146 100644 --- a/DESIGN.md +++ b/DESIGN.md @@ -27,7 +27,10 @@ are resolved before comparison with installed claims. `apply::compare` reports present entries, missing entries, and extras. Planning asks each `Harness` (`plan_install`) to turn missing entries into actions, and reuses -removal plans for extras only in exact mode. Snapshotting converts typed installed +removal plans for extras only in exact mode. A missing skill entry is the exception: its +source is fetched once per plan, and each agent whose marketplace supplies every named skill +installs the owning plugins by the rule `install --skill` uses (`install::native_skills`); the +other agents, and every agent when the fetch fails, use `npx skills`. Snapshotting converts typed installed facts into reproducible entries; it reports missing sources, invalid definitions, and secret-bearing MCP servers as skipped. @@ -42,8 +45,9 @@ asks which to install and returns its steps (`plan_add_source`); the remaining a `npx skills`. The steps are shown as one plan and run only once approved. The CLI and the workspace share `Session::install_source`, which also records what arrived in the library. Explicit skill selections -use the owning plugins when the marketplace supplies all selected skills; sources without a -matching plugin use loose skills. Codex recognizes native and legacy Claude marketplaces. +use the owning plugins when the marketplace supplies all selected skills, none refused for a +symlink that leaves the source; sources without a matching plugin use loose skills. Library +installs and profile applies share this rule. Codex recognizes native and legacy Claude marketplaces. Its project plugin plans download Git marketplace metadata without registering it globally, then stage marketplace declarations and plugin enablement in the project's config, once per marketplace however many of its plugins are picked. Codex materializes the declared plugins in @@ -88,8 +92,9 @@ so an isolated command launches only after its profile is ready. stored in profiles and the library. The name suggests a declarative desired state. - **`Installed`**: what a harness reports it has, with identity, scope, agents, typed details and the removal `Action`s. -- **`Action`**: one side effect (run a command, remove a path, edit a file). Planning is pure; - `harness::action` executes plans with staging. +- **`Action`**: one side effect (run a command, remove a path, edit a file). Planning changes nothing: + it reads, and fetches skill sources through the injected runner; `harness::action` executes + plans with staging. - **`Harness`**: one coding tool (Claude, Codex). It reports `installed` and plans `plan_install`, `plan_update` and `plan_add_source`. The `npx` module (free functions named like the trait methods, `installed`, `plan_install` and `plan_update`, with their own parameters; not a diff --git a/README.md b/README.md index a300a60..4ace5cf 100644 --- a/README.md +++ b/README.md @@ -75,8 +75,10 @@ skills, and installs everything such a plugin ships. The plan names each plugin skills it brings, and for `--all` lists the skills of the source that no plugin supplies, which are not installed (name them with `--skill`). Sources without a matching plugin use `npx skills`. A malformed marketplace or plugin manifest in the source never fails the install, its -skills install loose instead. `library install` and profile applies still install a loose -skill entry with `npx skills`, not through the plugin its source may offer. +skills install loose instead. `library install`, `use`, `run` and the list install a skill entry +the same way: the plan fetches its source, and each agent whose marketplace supplies every named +skill installs the owning plugin; the others, and all of them when the fetch fails, use `npx +skills`. A Codex project install writes the marketplace and plugin into `.codex/config.toml`, which a team usually commits, so skillz refuses a source with a user, password or token in its URL (use ssh or @@ -127,7 +129,7 @@ extends = ["base"] # optional, see below agents = ["claude", "codex"] # default for entries that name none (this is the default) [[entry]] -kind = "skill" # installed with `npx skills` +kind = "skill" # the owning plugin, else `npx skills` source = "mattpocock/skills" # org/repo, git URL or local path names = ["tdd", "grilling"] @@ -163,7 +165,7 @@ Every value must be non-empty and must not start with `-`; an MCP entry has exac | Kind | Claude | Codex | | --- | --- | --- | -| skill | `npx skills add`, all agents at once | same | +| skill | the owning plugin when the source's marketplace supplies every name, else `npx skills add` with the other agents | same | | plugin | `claude plugin marketplace add` + `plugin install`; project scope uses Claude's `local` scope, never the file the team shares | Global: `codex plugin marketplace add` + `plugin add`. Project: marketplace and plugin settings in `.codex/config.toml`; Codex materializes them on a marketplace refresh in a trusted project | | subagent | `.md` files into `.claude/agents` (`~/.claude/agents` global) | `.toml` files into `.codex/agents` (`~/.codex/agents` global) | | hook | `settings.json` (global) or `.claude/settings.local.json` (project) | `hooks.json` in the same scope | @@ -189,7 +191,7 @@ wins. ```sh skillz profile save web # snapshot what this project has installed (--force replaces) skillz profile ls | show web | rm web -skillz use web --dry-run # what installing `web` into this project would do +skillz use web --dry-run # what installing `web` would do; installs nothing, but still fetches skill sources to plan skillz use web # shows the plan, asks, then installs what is missing skillz use web --exact # also removes what `web` does not want (-y skips the question) skillz use web -g # the same at user level: plugins, skills and the rest for every project @@ -243,7 +245,8 @@ change. It only ever adds: a second run adds nothing, labels you gave are kept, and items that are no longer installed stay. The only way out of the library is `skillz library remove` or removing it by hand in `store.json`. `SKILLZ_NO_SYNC=1` turns the startup scan off. A hand-made skill or subagent is recorded by the directory that holds it (engine `local`, -marked detected), so it is reinstalled with `npx skills add --skill `. +marked detected), so it is reinstalled from that directory: through the owning plugin when the +directory's marketplace supplies it, else with `npx skills add --skill `. Items needing secrets, and items with nothing to reinstall them from, are not recorded. **Moving a folder.** skillz keeps no registry of projects, so moving a project is fine, but a diff --git a/src/app/apply/each_tests.rs b/src/app/apply/each_tests.rs index e0a4ac6..df5ec57 100644 --- a/src/app/apply/each_tests.rs +++ b/src/app/apply/each_tests.rs @@ -89,6 +89,7 @@ fn watched_with( env: &env, scope: Scope::Project, probe, + runner, }; let mut events = Vec::new(); let each = applier.run( diff --git a/src/app/apply/mod.rs b/src/app/apply/mod.rs index 1e07b53..ef986cb 100644 --- a/src/app/apply/mod.rs +++ b/src/app/apply/mod.rs @@ -23,6 +23,7 @@ use crate::app::stale::Probe; use skillz_harness::agents::{self, Harness}; use skillz_harness::env::Env; use skillz_harness::model::{Installed, Scope}; +use skillz_harness::runner::CommandRunner; /// Everything a switch of one scope works with. pub struct Applier<'a> { @@ -31,6 +32,8 @@ pub struct Applier<'a> { pub scope: Scope, /// Tells whether a local source still exists before anything is installed from it. pub probe: &'a dyn Probe, + /// Fetches a skill entry's source at plan time to see whether its marketplace supplies it. + pub runner: &'a dyn CommandRunner, } impl Applier<'_> { diff --git a/src/app/apply/plan.rs b/src/app/apply/plan.rs index 3388257..729cd0b 100644 --- a/src/app/apply/plan.rs +++ b/src/app/apply/plan.rs @@ -1,6 +1,11 @@ +use std::collections::{BTreeSet, HashMap}; + +use crate::app::install::{NativeSkills, native_skills}; use crate::app::profile::Profile; use skillz_harness::Spec; use skillz_harness::action::Action; +use skillz_harness::clone; +use skillz_harness::model::Agent; use skillz_harness::provision::{self, Unsupported}; use super::Applier; @@ -9,7 +14,8 @@ use super::switch::Switch; impl Applier<'_> { /// What the scope needs to hold `target`: what is missing gets installed and, when `exact`, - /// what the profile does not want gets removed. Reads source locations and the installed inventory. + /// what the profile does not want gets removed. Reads source locations and the installed + /// inventory, and fetches the sources of missing skills. pub fn plan(&self, target: &Profile, exact: bool) -> anyhow::Result { let target = target.clone().in_env(self.env); let standing = compare(&target.resolved(), &self.installed()?); @@ -30,22 +36,77 @@ impl Applier<'_> { }) } - /// The install steps for each entry, and what some agent cannot take. + /// The install steps for each entry, and what some agent cannot take. A skill's agents whose + /// source marketplace supplies it install the owning plugins; the others use `npx skills`. fn installs_for(&self, entries: &[Spec]) -> (Vec<(Spec, Vec)>, Vec) { + let mut native = self.native_skills_by_entry(entries); let mut installs = Vec::new(); let mut unsupported = Vec::new(); - for entry in entries { - let planned = provision::plan_entries( - self.adapters, - std::slice::from_ref(entry), - self.scope, - self.env, - ); - unsupported.extend(planned.unsupported); - if !planned.steps.is_empty() { - installs.push((entry.clone(), planned.steps)); + for (index, entry) in entries.iter().enumerate() { + let mut steps = Vec::new(); + let through_npx = match native.remove(&index) { + Some(split) if !split.native.is_empty() => { + steps.extend(split.native.into_iter().flat_map(|(_, steps)| steps)); + (!split.rest.is_empty()).then(|| entry.clone().with_agents(split.rest)) + } + _ => Some(entry.clone()), + }; + if let Some(through_npx) = through_npx { + let planned = provision::plan_entries( + self.adapters, + std::slice::from_ref(&through_npx), + self.scope, + self.env, + ); + unsupported.extend(planned.unsupported); + steps.extend(planned.steps); + } + if !steps.is_empty() { + installs.push((entry.clone(), steps)); } } (installs, unsupported) } + + /// How each skill entry divides between agents installing its owning plugins and agents + /// keeping `npx skills`, by its index in `entries`. Each source is fetched once; an entry whose + /// source cannot be fetched or read is left out, so all its agents keep `npx skills`. + fn native_skills_by_entry(&self, entries: &[Spec]) -> HashMap { + let skills: Vec<(usize, &str, &[String], &[Agent])> = entries + .iter() + .enumerate() + .filter_map(|(index, entry)| match entry { + Spec::Skill { + source, + names, + agents, + } => Some((index, source.as_str(), names.as_slice(), agents.as_slice())), + _ => None, + }) + .collect(); + let sources: BTreeSet<&str> = skills.iter().map(|&(_, source, ..)| source).collect(); + let mut split = HashMap::new(); + for source in sources { + let fetched = + clone::with_source(source, &self.env.project, self.runner, |src, resolved| { + let from_source = skills.iter().filter(|&&(_, from, ..)| from == source); + Ok(from_source + .filter_map(|&(index, _, names, agents)| { + let native = native_skills( + self.adapters, + self.env, + self.scope, + (src, resolved), + names, + agents, + ) + .ok()?; + Some((index, native)) + }) + .collect::>()) + }); + split.extend(fetched.unwrap_or_default()); + } + split + } } diff --git a/src/app/apply/tests.rs b/src/app/apply/tests.rs index 751dc45..df4f7c4 100644 --- a/src/app/apply/tests.rs +++ b/src/app/apply/tests.rs @@ -5,9 +5,13 @@ use std::fs; use std::path::Path; use super::*; +use crate::app::install::test_support::{ + FakeRunner, link, marketplace_fixture, mattpocock_fixture, npx, s, skill, skills_fixture, +}; use crate::app::profile::Profile; use crate::app::stale::Disk; use skillz_harness::Spec; +use skillz_harness::action::{Action, apply_edit}; use skillz_harness::agents::registry; use skillz_harness::model::Agent; use skillz_harness::runner::{CommandRunner, Output}; @@ -84,6 +88,8 @@ struct Fx { _dirs: (tempfile::TempDir, tempfile::TempDir), env: Env, adapters: Vec>, + /// Fetches skill sources at plan time: an empty clone, so every skill keeps `npx skills`. + fetch: FakeRunner, } impl Fx { @@ -92,6 +98,7 @@ impl Fx { Fx { env: Env::new(home.path().into(), project.path().into()), adapters: registry(), + fetch: FakeRunner::new(|_| {}), _dirs: (home, project), } } @@ -102,6 +109,7 @@ impl Fx { env: &self.env, scope: Scope::Project, probe: &Disk, + runner: &self.fetch, } } @@ -330,3 +338,321 @@ fn a_relative_local_subagent_source_matches_its_canonical_install() { assert!(again.is_empty()); assert_eq!(again.present.len(), 1); } + +fn skill_entry(source: &str, names: &[&str], agents: &[Agent]) -> Spec { + Spec::Skill { + source: source.into(), + names: s(names), + agents: agents.to_vec(), + } +} + +/// The steps that install `entries` at `scope`, their sources fetched through `fetch`. +fn planned_steps(fx: &Fx, fetch: &FakeRunner, scope: Scope, entries: Vec) -> Vec { + let mut library = Profile::new("library"); + library.entries = entries; + let applier = Applier { + scope, + runner: fetch, + ..fx.applier() + }; + let switch = applier.plan(&library, false).unwrap(); + switch + .installs + .into_iter() + .flat_map(|(_, steps)| steps) + .collect() +} + +/// Each command among `steps`, as its program and arguments. +fn commands(steps: &[Action]) -> Vec<(String, Vec)> { + steps + .iter() + .filter_map(|step| match step { + Action::Run { program, args, .. } => Some((program.clone(), args.clone())), + _ => None, + }) + .collect() +} + +/// The `npx skills add` that installs `names` from `source` for Claude in the project. +fn npx_for_claude(source: &str, names: &[&str]) -> (String, Vec) { + let mut rest: Vec<&str> = names.iter().flat_map(|name| ["--skill", name]).collect(); + rest.extend(["-a", "claude-code", "-y"]); + ("npx".into(), npx(source, &rest)) +} + +fn git_calls(fetch: &FakeRunner) -> Vec> { + fetch + .calls() + .into_iter() + .filter(|(program, ..)| program == "git") + .map(|(_, args, _)| args) + .collect() +} + +/// The project's `.codex/config.toml` as the steps' edits leave a missing one. +fn codex_project_config(fx: &Fx, steps: &[Action]) -> toml_edit::DocumentMut { + let config = fx.env.project.join(".codex/config.toml"); + steps + .iter() + .fold(String::new(), |text, step| match step { + Action::Edit { file, edit } if *file == config => { + apply_edit(file, edit, &text).unwrap() + } + _ => text, + }) + .parse() + .unwrap() +} + +/// Marketplace `mp` whose `p1` ships `alpha` and `p2` ships `beta`, beside a `gamma` no plugin +/// ships. +fn supplied_and_loose_fixture(root: &Path) { + marketplace_fixture(root); + fs::write( + root.join(".claude-plugin/marketplace.json"), + r#"{"name":"mp","plugins":[{"name":"p1","source":"./plugins/p1"},{"name":"p2","source":"./plugins/p2"}]}"#, + ) + .unwrap(); + skill(root, "loose/gamma"); +} + +/// A link at `root/rel` to a file outside the source. +fn escaping_link(root: &Path, rel: &str) { + link(root, rel, "/etc/hosts"); +} + +#[test] +fn a_skill_its_source_marketplace_supplies_installs_the_owning_plugin_in_the_project() { + let fx = Fx::new(); + let fetch = FakeRunner::new(mattpocock_fixture); + let entry = skill_entry("o/r", &["tdd"], &[Agent::Claude, Agent::Codex]); + + let steps = planned_steps(&fx, &fetch, Scope::Project, vec![entry]); + + let install = s(&[ + "plugin", + "install", + "mattpocock-skills@mattpocock", + "--scope", + "local", + ]); + assert!(commands(&steps).contains(&("claude".into(), install))); + let config = codex_project_config(&fx, &steps); + assert_eq!( + config["plugins"]["mattpocock-skills@mattpocock"]["enabled"].as_bool(), + Some(true) + ); + assert!(commands(&steps).iter().all(|(program, _)| program != "npx")); +} + +#[test] +fn a_skill_its_source_marketplace_supplies_installs_the_owning_plugin_for_the_user() { + let fx = Fx::new(); + let fetch = FakeRunner::new(mattpocock_fixture); + let entry = skill_entry("o/r", &["tdd"], &[Agent::Claude, Agent::Codex]); + + let steps = planned_steps(&fx, &fetch, Scope::Global, vec![entry]); + + let add = s(&["plugin", "add", "mattpocock-skills@mattpocock"]); + assert!(commands(&steps).contains(&("codex".into(), add))); + assert!(commands(&steps).iter().all(|(program, _)| program != "npx")); +} + +/// The commands that install skill `names` of `o/r` for Claude in the project, its clone made +/// by `fixture`. +fn claude_commands( + fixture: impl Fn(&Path) + 'static, + names: &[&str], +) -> Vec<(String, Vec)> { + let fx = Fx::new(); + let fetch = FakeRunner::new(fixture); + let entry = skill_entry("o/r", names, &[Agent::Claude]); + commands(&planned_steps(&fx, &fetch, Scope::Project, vec![entry])) +} + +#[test] +fn a_skill_from_a_source_without_a_marketplace_keeps_the_npx_command() { + let commands = claude_commands(skills_fixture, &["tdd"]); + + assert_eq!(commands, [npx_for_claude("o/r", &["tdd"])]); +} + +#[test] +fn a_skill_no_plugin_supplies_keeps_the_npx_command() { + let commands = claude_commands(supplied_and_loose_fixture, &["gamma"]); + + assert_eq!(commands, [npx_for_claude("o/r", &["gamma"])]); +} + +#[test] +fn skills_the_marketplace_supplies_only_some_of_all_keep_the_npx_command() { + let commands = claude_commands(supplied_and_loose_fixture, &["alpha", "gamma"]); + + assert_eq!(commands, [npx_for_claude("o/r", &["alpha", "gamma"])]); +} + +#[test] +fn an_agent_that_refuses_the_source_keeps_npx_and_the_others_still_install_natively() { + let fx = Fx::new(); + let fetch = FakeRunner::new(mattpocock_fixture); + let credentialed = "https://user:s3cret@host/o/r.git"; + let entry = skill_entry(credentialed, &["tdd"], &[Agent::Claude, Agent::Codex]); + + let steps = planned_steps(&fx, &fetch, Scope::Project, vec![entry]); + + let commands = commands(&steps); + let install = s(&[ + "plugin", + "install", + "mattpocock-skills@mattpocock", + "--scope", + "local", + ]); + assert!(commands.contains(&("claude".into(), install))); + let codex_only = npx(credentialed, &["--skill", "tdd", "-a", "codex", "-y"]); + assert!(commands.contains(&("npx".into(), codex_only))); +} + +#[test] +fn each_source_is_fetched_once_however_many_of_its_skills_are_missing() { + let fx = Fx::new(); + let fetch = FakeRunner::new(skills_fixture); + let entries = vec![ + skill_entry("o/r", &["tdd"], &[Agent::Claude]), + skill_entry("o/r", &["git-commit"], &[Agent::Claude]), + skill_entry("o/s", &["zeta"], &[Agent::Claude]), + ]; + + planned_steps(&fx, &fetch, Scope::Project, entries); + + let cloned: Vec = git_calls(&fetch) + .into_iter() + .map(|args| args[4].clone()) + .collect(); + assert_eq!( + cloned, + ["https://github.com/o/r.git", "https://github.com/o/s.git"] + ); +} + +#[test] +fn a_source_whose_fetch_fails_keeps_the_npx_command() { + let fx = Fx::new(); + let fetch = FakeRunner::new(mattpocock_fixture).failing("git"); + let entry = skill_entry("o/r", &["tdd"], &[Agent::Claude]); + + let steps = planned_steps(&fx, &fetch, Scope::Project, vec![entry]); + + assert_eq!(commands(&steps), [npx_for_claude("o/r", &["tdd"])]); +} + +#[test] +fn a_source_the_fetch_refuses_is_never_cloned_and_keeps_the_npx_command() { + let fx = Fx::new(); + let fetch = FakeRunner::new(mattpocock_fixture); + let entry = skill_entry("o/r/extra", &["tdd"], &[Agent::Claude]); + + let steps = planned_steps(&fx, &fetch, Scope::Project, vec![entry]); + + assert!(git_calls(&fetch).is_empty()); + assert_eq!(commands(&steps), [npx_for_claude("o/r/extra", &["tdd"])]); +} + +#[test] +fn a_skill_whose_plugin_holds_a_symlink_leaving_the_source_keeps_npx() { + let leaky_plugin = |root: &Path| { + supplied_and_loose_fixture(root); + escaping_link(root, "plugins/p1/skills/alpha/secret"); + }; + + let commands = claude_commands(leaky_plugin, &["alpha"]); + + assert_eq!(commands, [npx_for_claude("o/r", &["alpha"])]); +} + +#[test] +fn a_clean_skill_whose_plugin_holds_a_symlink_elsewhere_keeps_npx() { + // Only the plugin is refused: `alpha` itself holds no link, so the source still offers it. + let leaky_plugin = |root: &Path| { + supplied_and_loose_fixture(root); + escaping_link(root, "plugins/p1/hooks/secret"); + }; + + let commands = claude_commands(leaky_plugin, &["alpha"]); + + assert_eq!(commands, [npx_for_claude("o/r", &["alpha"])]); +} + +#[test] +fn a_skill_with_a_copy_outside_its_plugin_that_leaves_the_source_keeps_npx() { + let leaky_copy = |root: &Path| { + supplied_and_loose_fixture(root); + skill(root, "loose/alpha"); + escaping_link(root, "loose/alpha/secret"); + }; + + let commands = claude_commands(leaky_copy, &["alpha"]); + + assert_eq!(commands, [npx_for_claude("o/r", &["alpha"])]); +} + +#[test] +fn a_local_source_installs_natively_from_its_canonical_path_without_fetching() { + let fx = Fx::new(); + let fetch = FakeRunner::new(|_| {}); + let local = tempfile::tempdir().unwrap(); + mattpocock_fixture(local.path()); + let canonical = local.path().canonicalize().unwrap(); + let source = local.path().to_string_lossy(); + let entry = skill_entry(&source, &["tdd"], &[Agent::Claude]); + + let steps = planned_steps(&fx, &fetch, Scope::Project, vec![entry]); + + let add = s(&[ + "plugin", + "marketplace", + "add", + &canonical.to_string_lossy(), + "--scope", + "local", + ]); + assert!(fetch.calls().is_empty()); + assert!(commands(&steps).contains(&("claude".into(), add))); +} + +#[test] +fn an_agent_the_marketplace_does_not_supply_keeps_npx_for_itself_alone() { + let fx = Fx::new(); + let fetch = FakeRunner::new(|root| { + skill(root, "skills/alpha"); + skill(root, "extras/beta"); + fs::create_dir_all(root.join(".claude-plugin")).unwrap(); + fs::write( + root.join(".claude-plugin/marketplace.json"), + r#"{"name":"mp","plugins":[{"name":"p","source":"./"}]}"#, + ) + .unwrap(); + // Claude adds `extras` to its default `skills`; Codex reads `extras` instead of them. + fs::write( + root.join(".claude-plugin/plugin.json"), + r#"{"name":"p","skills":"./extras"}"#, + ) + .unwrap(); + }); + let entry = skill_entry("o/r", &["alpha"], &[Agent::Claude, Agent::Codex]); + + let steps = planned_steps(&fx, &fetch, Scope::Project, vec![entry]); + + let commands = commands(&steps); + let install = s(&["plugin", "install", "p@mp", "--scope", "local"]); + assert!(commands.contains(&("claude".into(), install))); + let codex_only = npx("o/r", &["--skill", "alpha", "-a", "codex", "-y"]); + let npx_runs: Vec<&Vec> = commands + .iter() + .filter(|(program, _)| program == "npx") + .map(|(_, args)| args) + .collect(); + assert_eq!(npx_runs, [&codex_only]); +} diff --git a/src/app/install/mod.rs b/src/app/install/mod.rs index 4143d87..01e4943 100644 --- a/src/app/install/mod.rs +++ b/src/app/install/mod.rs @@ -188,20 +188,10 @@ fn agent_plugins( SkillSelection::Patterns(_) => { let available = skills::source_skills(tree.src)?.offered; let selected = skills::select_skills(&available, &req.selection); - if selected.is_empty() || !left_out(offer, &selected).is_empty() { + let Some(plugins) = supplying_plugins(offer, &available, &selected) else { return Ok(AgentPlugins::Unoffered); - } - offer - .plugins - .iter() - .filter(|name| { - offer - .skills - .get(*name) - .is_some_and(|names| names.iter().any(|name| selected.contains(name))) - }) - .cloned() - .collect() + }; + plugins } }; if chosen.is_empty() { @@ -235,6 +225,77 @@ fn agent_plugins( ) } +/// What installing skill `names` from a fetched tree comes to, by the rule `install --skill` +/// uses: each agent whose offer supplies every name, none refused for a symlink that leaves the +/// source, installs the owning plugins; the rest keep `npx skills`. +pub(crate) struct NativeSkills { + pub native: Vec<(Agent, Vec)>, + pub rest: Vec, +} + +pub(crate) fn native_skills( + adapters: &[Box], + env: &Env, + scope: Scope, + (src, source): (&Path, &str), + names: &[String], + agents: &[Agent], +) -> anyhow::Result { + let offered = skills::source_skills(src)?.offered; + let plugin_steps = |adapter: &dyn Harness| -> anyhow::Result>> { + let Some(offer) = adapter.plugin_offer(src, scope)? else { + return Ok(None); + }; + let Some(plugins) = supplying_plugins(&offer, &offered, names) else { + return Ok(None); + }; + // An agent that refuses the source keeps `npx skills` for these skills. + Ok(adapter + .plan_add_source(&offer, &plugins, source, scope, env) + .ok()) + }; + let mut split = NativeSkills { + native: vec![], + rest: vec![], + }; + for &agent in agents { + match adapter_for(adapters, agent) + .map(plugin_steps) + .transpose()? + .flatten() + { + Some(steps) => split.native.push((agent, steps)), + None => split.rest.push(agent), + } + } + Ok(split) +} + +/// The offer's plugins that ship skills `names`; `None` unless every name is one the source +/// `offered` and some plugin supplies it. +fn supplying_plugins( + offer: &PluginOffer, + offered: &[String], + names: &[String], +) -> Option> { + let supplied = !names.is_empty() + && names.iter().all(|name| offered.contains(name)) + && left_out(offer, names).is_empty(); + supplied.then(|| { + offer + .plugins + .iter() + .filter(|plugin| { + offer + .skills + .get(*plugin) + .is_some_and(|ships| ships.iter().any(|name| names.contains(name))) + }) + .cloned() + .collect() + }) +} + /// The skills in `names` that none of the offer's plugins supplies: installing the plugins /// leaves them out. fn left_out(offer: &PluginOffer, names: &[String]) -> Vec { diff --git a/src/cli/session/change.rs b/src/cli/session/change.rs index bb7d4a4..2fa6ba5 100644 --- a/src/cli/session/change.rs +++ b/src/cli/session/change.rs @@ -142,6 +142,7 @@ impl Job<'_> { env: &self.env, scope: self.scope, probe: &Disk, + runner: self.deps.runner, } } diff --git a/src/cli/session/change/scope_tests.rs b/src/cli/session/change/scope_tests.rs index 510333a..1a98691 100644 --- a/src/cli/session/change/scope_tests.rs +++ b/src/cli/session/change/scope_tests.rs @@ -58,8 +58,9 @@ fn installing_at_global_scope_gives_npx_the_global_flag_and_project_scope_does_n &Change::Selection(&picked), ); - assert!(global[0].contains(&"-g".to_string())); - assert!(!local[0].contains(&"-g".to_string())); + // The install runs after the plan fetched the source, so it is the last command. + assert!(global.last().unwrap().contains(&"-g".to_string())); + assert!(!local.last().unwrap().contains(&"-g".to_string())); } #[test] diff --git a/src/cli/session/change/selection_tests.rs b/src/cli/session/change/selection_tests.rs index 543c141..c833fad 100644 --- a/src/cli/session/change/selection_tests.rs +++ b/src/cli/session/change/selection_tests.rs @@ -2,7 +2,7 @@ use std::path::Path; use super::tests::{project_with_skill, run_change, session, skill_entry, skill_id}; use super::*; -use crate::app::install::test_support::ScriptedPrompter; +use crate::app::install::test_support::{FakeRunner, ScriptedPrompter, mattpocock_fixture}; use skillz_harness::Spec; use skillz_harness::agents::registry; use skillz_harness::runner::{CommandRunner, Output}; @@ -146,6 +146,26 @@ fn one_failing_install_is_reported_and_the_others_still_run() { assert!(tried_good); } +#[test] +fn a_skill_its_source_marketplace_supplies_installs_through_claude_and_never_npx() { + let dir = tempfile::tempdir().unwrap(); + let entries = [skill_entry("tdd")]; + let runner = FakeRunner::new(mattpocock_fixture); + let prompter = ScriptedPrompter::new(&[], true); + let deps = Deps::for_test(dir.path(), registry(), (&runner, &prompter)); + + change( + &session(&deps, dir.path()), + Scope::Project, + &Change::Selection(&selection(&entries, &[])), + ) + .unwrap(); + + let programs: Vec = runner.calls().into_iter().map(|call| call.0).collect(); + assert!(programs.contains(&"claude".to_string())); + assert!(!programs.contains(&"npx".to_string())); +} + #[test] fn an_unsupported_install_is_skipped_without_counting_as_installed() { let dir = tempfile::tempdir().unwrap(); diff --git a/tests/profile.rs b/tests/profile.rs index ec5849f..182095a 100644 --- a/tests/profile.rs +++ b/tests/profile.rs @@ -45,14 +45,23 @@ fn apply_all(d: &Deps, profiles: &[&str]) { } } +/// How many commands ran beyond the `git` that fetches a skill's source to plan it. +fn commands_beyond_fetch(r: &Recorder) -> usize { + r.calls() + .iter() + .filter(|(program, _)| program != "git") + .count() +} + #[test] -fn a_dry_run_use_runs_no_command() { +fn a_dry_run_use_only_fetches_and_installs_nothing() { let w = world_with_web(); let (r, p) = (Recorder::default(), Scripted::yes()); run_quiet(use_cmd("web", true), &deps(&w, &r, &p)).unwrap(); - assert_eq!(r.count(), 0); + let programs: Vec = r.calls().into_iter().map(|(program, _)| program).collect(); + assert_eq!(programs, ["git"]); } #[test] @@ -62,7 +71,7 @@ fn using_a_profile_installs_its_skills_once() { apply_all(&deps(&w, &r, &p), &["web"]); - assert_eq!(r.count(), 1); + assert_eq!(commands_beyond_fetch(&r), 1); } #[test] @@ -72,7 +81,7 @@ fn using_two_profiles_installs_each_ones_skills() { apply_all(&deps(&w, &r, &p), &["web", "api"]); - assert_eq!(r.count(), 2); + assert_eq!(commands_beyond_fetch(&r), 2); } #[test]