diff --git a/internal/library/skillnew.go b/internal/library/skillnew.go index bfef11366..1a9c17097 100644 --- a/internal/library/skillnew.go +++ b/internal/library/skillnew.go @@ -115,8 +115,8 @@ type gitEntry struct { // skillDirsIn are the folders with a SKILL.md under base in a repository's // tree, as candidates would find them: a few folders deep, none in -// node_modules, a hidden folder only through its skills folder, and none -// inside another skill or a copy of one outside hidden folders. +// node_modules, a hidden folder only through its skills folder, none +// inside another skill, and each skill once (unhiddenFirst). func skillDirsIn(t *gitTree, base string) []string { var dirs []string for _, e := range t.Tree { diff --git a/internal/library/skillnew_test.go b/internal/library/skillnew_test.go index 5f13ea2f5..d95e5fce7 100644 --- a/internal/library/skillnew_test.go +++ b/internal/library/skillnew_test.go @@ -322,3 +322,46 @@ func TestMarketInstallsASkillKeptInClaudesFolder(t *testing.T) { t.Errorf("skills in the tree %v, want %v", got, want) } } + +// 碳碳双键's report on Discord: tw93/waza keeps its eight skills in skills/ +// and again in plugins/waza/skills/ (some copies behind the top ones), and +// typing "tw93/waza" listed 16 skills, each twice. The paths are the +// repository's own. +func TestRepoWithAPluginCopyListsEachSkillOnce(t *testing.T) { + sandbox(t) + names := []string{"check", "health", "hunt", "learn", "read", "think", "ui", "write"} + files := map[string]string{} + for _, n := range names { + files["skills/"+n+"/SKILL.md"] = "---\nname: " + n + "\ndescription: the top one\n---\n" + files["plugins/waza/skills/"+n+"/SKILL.md"] = "---\nname: " + n + "\ndescription: the plugin's copy\n---\n" + } + fakeSkillRepo(t, files) + + p, err := ProbeSkills("tw93/waza") + if err != nil { + t.Fatal(err) + } + var paths []string + for _, c := range p.Candidates { + paths = append(paths, c.Path) + if c.Description != "the top one" { + t.Errorf("%s: %q, want the top one", c.Path, c.Description) + } + } + var want []string + for _, n := range names { + want = append(want, "skills/"+n) + } + if !slices.Equal(paths, want) { + t.Errorf("candidates %v, want %v", paths, want) + } + + tree := &gitTree{} + for p := range files { + tree.Tree = append(tree.Tree, gitEntry{Path: p, Type: "blob"}) + } + sort.Slice(tree.Tree, func(i, j int) bool { return tree.Tree[i].Path < tree.Tree[j].Path }) + if got := skillDirsIn(tree, ""); !slices.Equal(got, want) { + t.Errorf("skills in the tree %v, want %v", got, want) + } +} diff --git a/internal/library/skills.go b/internal/library/skills.go index a8e008edd..79d7e7ed4 100644 --- a/internal/library/skills.go +++ b/internal/library/skills.go @@ -877,8 +877,8 @@ func untar(r io.Reader, dst string) error { // looked for a few folders deep, and not inside another skill. A hidden // folder is an agent's own (.claude, .agents) and is looked in only for its // skills folder: some repositories keep their skill only in -// .claude/skills (#1964, UFO404). A skill found there that the repository -// also has outside a hidden folder is that one, listed once. +// .claude/skills (#1964, UFO404). A skill the repository holds twice, in a +// hidden folder or a plugin's, is listed once (unhiddenFirst). func candidates(root, sub string) []Candidate { base := filepath.Join(root, filepath.FromSlash(sub)) var out []Candidate @@ -926,17 +926,36 @@ func inHidden(path string) bool { return slices.ContainsFunc(strings.Split(path, "/"), func(p string) bool { return strings.HasPrefix(p, ".") }) } -// unhiddenFirst drops a skill in a hidden folder that is the same skill as -// one outside every hidden folder, or as a hidden one listed before it: an -// agent's folder often holds a copy of the repository's own skills. +// unhiddenFirst lists each skill once: of copies of the same skill it keeps +// the one outside every hidden folder, then the one fewest folders deep, +// then the first. An agent's folder often holds a copy of the repository's +// own skills, and a repository that is also a plugin often holds another +// under its plugin's folder: tw93/waza has skills/check and +// plugins/waza/skills/check, and every skill was listed twice (碳碳双键 on +// Discord). func unhiddenFirst[T any](list []T, path func(T) string, same func(a, b T) bool) []T { + depth := func(c T) int { return strings.Count(path(c), "/") } + before := func(o T, j int, c T, i int) bool { + if ho, hc := inHidden(path(o)), inHidden(path(c)); ho != hc { + return !ho + } + if do, dc := depth(o), depth(c); do != dc { + return do < dc + } + return j < i + } out := make([]T, 0, len(list)) for i, c := range list { - if inHidden(path(c)) && (slices.ContainsFunc(list, func(o T) bool { return !inHidden(path(o)) && same(o, c) }) || - slices.ContainsFunc(list[:i], func(o T) bool { return inHidden(path(o)) && same(o, c) })) { - continue + dup := false + for j, o := range list { + if j != i && same(o, c) && before(o, j, c, i) { + dup = true + break + } + } + if !dup { + out = append(out, c) } - out = append(out, c) } return out }