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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,9 @@ subagent of the same name (it keeps your label, refreshes the provenance, and fo
entries that named the old path); the toast says `moved: N` and `sync --json` has `repointed`. If
several places match it stays stale (`ambiguous`), and if the library already has the live item the
stale duplicate is dropped. Repository and URL sources are never touched. Installing or applying
an entry whose directory is missing fails that entry, naming it and the path, before `npx` runs;
an entry whose directory is missing, or a skill entry whose directory exists but no longer holds
that skill, fails that entry, naming it and the path, before `npx` runs (only a missing directory
is followed by `skillz sync`);
the other entries still install. `skillz run` environments are keyed by profile only, so a moved
project reuses its own.
Installing a skill again fetches it from its recorded
Expand Down
121 changes: 112 additions & 9 deletions src/app/apply/source_gone.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,136 @@
use std::fmt;

use crate::app::stale::{Probe, missing_source};
use crate::app::stale::{Probe, missing_source, source_without_skill};
use skillz_harness::Spec;

/// An entry cannot be installed because the local folder it names is gone, most likely moved.
/// Why an entry's local source cannot be installed from.
#[derive(Debug, PartialEq, Eq)]
pub enum Cause {
FolderMissing,
SkillMissing,
}

/// An entry cannot be installed because the local folder it names is gone, or no longer holds
/// the skill.
#[derive(Debug, PartialEq, Eq)]
pub struct SourceGone {
pub entry: String,
pub path: String,
pub cause: Cause,
}

impl SourceGone {
/// Why `entry` cannot be installed, when its local source is gone.
pub fn of(entry: &Spec, probe: &dyn Probe) -> Option<Self> {
missing_source(entry, probe).map(|path| SourceGone {
let gone = |path: &str, cause| SourceGone {
entry: entry.to_string(),
path: path.to_string(),
})
cause,
};
missing_source(entry, probe)
.map(|path| gone(path, Cause::FolderMissing))
.or_else(|| {
source_without_skill(entry, probe).map(|path| gone(path, Cause::SkillMissing))
})
}
}

impl fmt::Display for SourceGone {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{} cannot be installed: {} does not exist; if the folder moved, run `skillz sync` to follow it",
self.entry, self.path
)
match self.cause {
Cause::FolderMissing => write!(
f,
"{} cannot be installed: {} does not exist; if the folder moved, run `skillz sync` to follow it",
self.entry, self.path
),
Cause::SkillMissing => write!(
f,
"{} cannot be installed: {} exists but no longer holds it; restore the skill there or point the entry at the folder that has it",
self.entry, self.path
),
}
}
}

impl std::error::Error for SourceGone {}

#[cfg(test)]
mod tests {
use std::collections::HashSet;
use std::path::Path;

use super::*;
use crate::app::stale::tests::{Existing, skill};

/// Every folder exists; each holds only the skills listed for it.
struct Holding(HashSet<(&'static str, &'static str)>);

impl Probe for Holding {
fn exists(&self, _path: &Path) -> bool {
true
}

fn holds_skill(&self, dir: &Path, name: &str) -> bool {
self.0
.iter()
.any(|(d, n)| Path::new(d) == dir && *n == name)
}
}

fn holding(skills: &[(&'static str, &'static str)]) -> Holding {
Holding(skills.iter().copied().collect())
}

#[test]
fn a_folder_that_no_longer_holds_the_skill_is_gone() {
let gone = SourceGone::of(&skill("/d", "x"), &holding(&[("/d", "y")]));

assert_eq!(gone.map(|g| g.cause), Some(Cause::SkillMissing));
}

#[test]
fn a_missing_folder_is_gone_for_that_reason() {
let gone = SourceGone::of(&skill("/d", "x"), &Existing::of(&[]));

assert_eq!(gone.map(|g| g.cause), Some(Cause::FolderMissing));
}

#[test]
fn one_named_skill_missing_from_the_folder_is_enough() {
let entry = Spec::Skill {
source: "/d".into(),
names: vec!["x".into(), "y".into()],
agents: vec![],
};

let gone = SourceGone::of(&entry, &holding(&[("/d", "y")]));

assert_eq!(gone.map(|g| g.cause), Some(Cause::SkillMissing));
}

#[test]
fn a_subagent_entry_is_never_flagged_for_a_missing_skill() {
let entry = Spec::Subagent {
source: "/d".into(),
names: vec!["x".into()],
agents: vec![],
};

assert_eq!(SourceGone::of(&entry, &holding(&[])), None);
}

#[test]
fn a_folder_that_holds_the_skill_is_not_gone() {
assert_eq!(
SourceGone::of(&skill("/d", "x"), &holding(&[("/d", "x")])),
None
);
}

#[test]
fn git_and_url_sources_are_not_gone_whatever_the_probe_holds() {
for source in ["org/repo", "https://h/o/r.git"] {
assert_eq!(SourceGone::of(&skill(source, "x"), &holding(&[])), None);
}
}
}
38 changes: 37 additions & 1 deletion src/app/stale.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@
use std::path::Path;

use skillz_harness::Spec;
use skillz_harness::agent_skills::skill_names_under;

/// Whether a path exists; injected so the rules can be tested without a disk.
/// What is on disk; injected so the rules can be tested without a disk.
pub trait Probe {
fn exists(&self, path: &Path) -> bool;

/// Whether the directory holds a skill called `name`. A probe that cannot tell says yes.
fn holds_skill(&self, _dir: &Path, _name: &str) -> bool {
true
}
}

pub struct Disk;
Expand All @@ -16,6 +22,10 @@ impl Probe for Disk {
fn exists(&self, path: &Path) -> bool {
path.exists()
}

fn holds_skill(&self, dir: &Path, name: &str) -> bool {
skill_names_under(dir).map_or(true, |names| names.iter().any(|n| n == name))
}
}

/// The directory a skill or subagent entry is installed from, when its source is an absolute
Expand Down Expand Up @@ -47,6 +57,18 @@ pub fn sole_name(entry: &Spec) -> Option<&str> {
}
}

/// The local source of a skill entry that exists but no longer holds a skill the entry names.
pub fn source_without_skill<'a>(entry: &'a Spec, probe: &dyn Probe) -> Option<&'a str> {
let Spec::Skill { names, .. } = entry else {
return None;
};
local_source(entry).filter(|source| {
names
.iter()
.any(|name| !probe.holds_skill(Path::new(source), name))
})
}

/// The same entry installed from `new_source`.
pub fn with_source(mut entry: Spec, new_source: &str) -> Spec {
if let Spec::Skill { source, .. } | Spec::Subagent { source, .. } = &mut entry {
Expand Down Expand Up @@ -112,6 +134,20 @@ pub(crate) mod tests {
}
}

#[test]
fn the_disk_holds_the_skills_found_under_a_folder() {
let dir = tempfile::tempdir().unwrap();
skillz_harness::test_support::skill(dir.path(), "nested/x");

assert!(Disk.holds_skill(dir.path(), "x"));
assert!(!Disk.holds_skill(dir.path(), "y"));
}

#[test]
fn a_folder_that_cannot_be_read_is_not_reported_as_missing_the_skill() {
assert!(Disk.holds_skill(Path::new("/no/such/folder"), "x"));
}

#[test]
fn only_entries_naming_one_thing_have_a_sole_name() {
let two = Spec::Skill {
Expand Down
Loading