diff --git a/internal/library/skill_from.go b/internal/library/skill_from.go index 6741ed5e1..b738ce84c 100644 --- a/internal/library/skill_from.go +++ b/internal/library/skill_from.go @@ -18,7 +18,9 @@ import ( // CC Switch's record, the skills CLI's lock (npx skills add, in // ~/.agents/.skill-lock.json), the git checkout a linked folder is in, // or the repository the user installed from since that has the very same -// skill (From). It only groups: such a skill is not updated from there. +// skill (From). It only groups: such a skill is not updated from there, +// unless the library's own copy is the repository's to the file, which +// then is the repository's skill (noteFrom). // repoOf is the repository a library skill came from: owner/repo on // GitHub, host/path elsewhere, "" when it can't be told. @@ -257,7 +259,8 @@ func remoteRepo(s string) string { } // noteFrom writes down, for the skills the library has already that a -// repository being installed from has too, that they came from there: +// repository being installed from has too, that they came from there (and +// binds a copy that is the repository's whole to it, to update from): // one whose SKILL.md is that repository's very own, and one traced to // nowhere by the same name when the library has others from there. A // pack added a while ago is mostly older copies (White Immortal on @@ -277,22 +280,37 @@ func noteFrom(src Source, root string, cands []Candidate) int { } tr := &tracer{} have := slices.ContainsFunc(l.Skills, func(s *Skill) bool { return strings.EqualFold(tr.repoOf(s), src.Repo) }) - noted := 0 + noted, bound := 0, 0 var same []*Skill for _, c := range cands { s := l.skill(c.Name) - if s == nil || (s.Source != nil && s.Source.Kind == "github") || strings.EqualFold(s.From, src.Repo) { + if s == nil || (s.Source != nil && s.Source.Kind == "github") { continue } + grouped := strings.EqualFold(s.From, src.Repo) mine, err1 := os.ReadFile(filepath.Join(skillDir(s.Name), "SKILL.md")) theirs, err2 := os.ReadFile(filepath.Join(root, filepath.FromSlash(c.Path), "SKILL.md")) if err1 != nil || err2 != nil { continue } if bytes.Equal(bytes.TrimSpace(mine), bytes.TrimSpace(theirs)) { - s.From, have = src.Repo, true - noted++ - } else if tr.repoOf(s) == "" { + if !grouped { + s.From, have = src.Repo, true + noted++ + } + // the library's own copy, every file of it the repository's: + // it is that skill, and updates from there as one installed + // from it does (RockyShowlle on X). One linked to a folder + // elsewhere is kept by whatever put it there (npx skills, a + // git checkout), and an edited one stays only grouped. + if dir := skillDir(s.Name); s.Source == nil && !linked(dir) && sameTree(dir, filepath.Join(root, filepath.FromSlash(c.Path))) { + s.Source = &Source{Kind: "github", Repo: src.Repo, Ref: src.Ref, Path: c.Path} + s.Hash, s.Commit, s.From = hashDir(dir), "", "" + l.seenAt(src.Repo, src.Ref, c.Path) + forgetCheck(s.Name) + bound++ + } + } else if !grouped && tr.repoOf(s) == "" { same = append(same, s) } } @@ -302,7 +320,7 @@ func noteFrom(src Source, root string, cands []Candidate) int { noted++ } } - if noted > 0 { + if noted > 0 || bound > 0 { l.save() } return noted diff --git a/internal/library/skill_from_test.go b/internal/library/skill_from_test.go index 41c32c05d..b2c8909bb 100644 --- a/internal/library/skill_from_test.go +++ b/internal/library/skill_from_test.go @@ -360,3 +360,70 @@ func TestRegroupOlderCopies(t *testing.T) { t.Errorf("edited after a failed regroup: %q", r) } } + +// A copy in the library's own folder that is a repository's skill to the +// file is bound to it when that repository is pasted again, and updates +// from there as one installed from it does (RockyShowlle on X: installed +// by hand before, now to sync with upstream). One with a file of the +// user's own beside it, and one linked to the skills CLI's folder, are +// grouped only: the user's file is never written over, and the skills CLI +// keeps what it put in ~/.agents. +func TestPastedRepoBindsSameCopy(t *testing.T) { + h := sandbox(t) + var rmu sync.Mutex + repo := map[string]string{ + "skills/same/SKILL.md": "---\nname: same\ndescription: Same\n---\nBody\n", + "skills/same/ref.md": "notes\n", + "skills/extra/SKILL.md": "---\nname: extra\ndescription: Extra\n---\n", + "skills/shared/SKILL.md": "---\nname: shared\ndescription: Shared\n---\n", + } + tar := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + rmu.Lock() + defer rmu.Unlock() + w.Write(tarball(t, repo)) + })) + t.Cleanup(tar.Close) + oldT := tarballURL + tarballURL = func(repo, ref string) string { return tar.URL + "/tar/" + repo } + t.Cleanup(func() { tarballURL = oldT }) + + write(t, filepath.Join(h, ".claude/skills/same/SKILL.md"), repo["skills/same/SKILL.md"]) + write(t, filepath.Join(h, ".claude/skills/same/ref.md"), repo["skills/same/ref.md"]) + write(t, filepath.Join(h, ".claude/skills/extra/SKILL.md"), repo["skills/extra/SKILL.md"]) + write(t, filepath.Join(h, ".claude/skills/extra/mine.md"), "my own notes\n") + write(t, filepath.Join(h, ".agents/skills/shared/SKILL.md"), repo["skills/shared/SKILL.md"]) + dirLink(filepath.Join(h, ".agents/skills/shared"), filepath.Join(h, ".claude/skills/shared")) + ok(t)(ImportSkills([]string{"same", "extra", "shared"})) + + if _, err := ProbeSkills("someone/upstream"); err != nil { + t.Fatal(err) + } + l := mustLoad(t) + if s := l.skill("same"); s.Source == nil || s.Source.Kind != "github" || s.Source.Repo != "someone/upstream" || s.Source.Path != "skills/same" || s.Hash == "" || s.From != "" { + t.Fatalf("same, the repository's to the file: source %+v hash %q from %q", s.Source, s.Hash, s.From) + } + if s := l.skill("extra"); s.Source != nil || s.From != "someone/upstream" { + t.Errorf("extra, with a file of the user's: source %+v from %q", s.Source, s.From) + } + if s := l.skill("shared"); s.Source == nil || s.Source.Kind != "folder" || s.From != "someone/upstream" { + t.Errorf("shared, the skills CLI's: source %+v from %q", s.Source, s.From) + } + if got := repoIn(t); got["same"] != "someone/upstream" || got["extra"] != "someone/upstream" || got["shared"] != "someone/upstream" { + t.Errorf("grouped: %v", got) + } + + // upstream changes: the bound copy updates from there + rmu.Lock() + repo["skills/same/SKILL.md"] = "---\nname: same\ndescription: Same\n---\nNew body\n" + rmu.Unlock() + ok(t)(UpdateSkill("same", false)) + if b, _ := os.ReadFile(filepath.Join(skillDir("same"), "SKILL.md")); !strings.Contains(string(b), "New body") { + t.Errorf("same after an update: %q", b) + } + if _, err := UpdateSkill("extra", false); err == nil { + t.Error("extra updated from GitHub") + } + if b, _ := os.ReadFile(filepath.Join(skillDir("extra"), "mine.md")); string(b) != "my own notes\n" { + t.Errorf("extra's own file: %q", b) + } +}