Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion DESIGN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
17 changes: 1 addition & 16 deletions crates/skillz-harness/src/agents/codex/plugins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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 {
Expand Down
50 changes: 50 additions & 0 deletions crates/skillz-harness/src/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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<String> = 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<String> = 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
Expand Down
133 changes: 121 additions & 12 deletions src/app/apply/standing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -100,16 +101,36 @@ fn claims_of(item: &Installed) -> Vec<Claim> {
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<Claim>)],
wanted_claims: &HashSet<Claim>,
) -> 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<Claim>,
wanted: &[Spec],
drawn_on: &HashSet<&str>,
) -> Option<Extra> {
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,
Expand All @@ -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],
}
Expand Down Expand Up @@ -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<Kind> = 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)];
Expand Down
88 changes: 88 additions & 0 deletions src/app/apply/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>)> {
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<String>) {
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);
}
Loading
Loading