Files
magpie/internal/library/skill_stale_origin_test.go
T
yetone 178a781ff9 library: a skill made again under the same name after its repo's copy was deleted is filed under 本机 (X: AzheRise)
AzheRise on X: a skill downloaded from a GitHub repository was deleted,
another of the same name was made in an agent's own folder
(~/.dsh/skills), and once brought into the Library it sat under the old
repository instead of This computer.

Cause: two records outlive the skill and are matched by name only.
- The skills CLI's lock (~/.agents/.skill-lock.json, or under
  $XDG_STATE_HOME) keeps an entry for a skill deleted by hand or from
  magpie. repoOf filed the library's own copy (a folder moved in) under
  the lock's repository by its name, and tracer.folder did the same for
  one in ~/.agents/skills, which importSkill then kept as From. On this
  machine's real lock, agent-browser is such an entry, with no folder.
- CC Switch's folder (~/.cc-switch/skills/<name>) outlives a copy of it
  deleted from the library. ccSwitchOrigin filed any later own copy of
  that name under CC Switch's repository, and made it "updatable", so
  Update would have put the repository's skill in place of the user's.
- Sibling: a removed skill stayed in the user's own group (SkillGroups),
  hidden only while no skill had its name, so a new one of that name came
  back in the group.

Fix:
- A lock entry counts for a folder only when the CLI wrote that folder: a
  file in it is no newer than the entry's updatedAt (else installedAt)
  plus a minute (lockRepo, madeBy). On this machine's 27 live entries the
  CLI's files are 0.4 to 2.8s older than updatedAt. A lock without times
  groups by name as before. Brought in from an agent's folder, a copy the
  CLI wrote is kept as From at once, so an edit later leaves it grouped.
- Existing data is judged again on every read: a moved-in copy has no
  From and goes through lockRepo, and a From that is the lock's
  repository for a folder in ~/.agents/skills the CLI didn't write is
  passed over (staleFrom). So the reporter's skill moves to This computer
  with no action.
- ccSwitchOrigin takes a library-own copy as CC Switch's only when its
  SKILL.md is CC Switch's own.
- removeSkill drops the skill from the user's groups.

Not changed: noteFrom's pack rule (a skill traced nowhere, of a name a
repository being installed from has, is grouped with it when the library
has others from there). That is by name on purpose (White Immortal's
edited older copies), and runs only when the user installs from or
regroups that repository, not on import. MCP servers, hooks and
subagents keep no origin by name (a server's icon is by what it runs).

Tests: TestDeletedSkillsLockNotInherited (the reporter's case, one made
in ~/.agents/skills, the CLI's --copy copy and linked skill, data as
brought in before, an edited CLI copy), TestUndatedLockStillGroups,
TestRemovedSkillLeavesItsGroup, TestCCSwitchNameNotInherited.

Run at 76c130b29 plus this change, through mote-run, and again after the rebase onto dd7442f28 (vet of internal/library for darwin, linux and windows, and its tests, ok in 165.9s): gofmt -l internal clean;
go vet -tags nogui ./... for darwin, linux and windows ok; go test
-tags nogui -count=1 ./internal/library ok (186.7s). The new tests and
TestSkillRepo*, TestCCSwitch* also pass on Windows (win-run).
Broken by hand: with the three source files back at 76c130b29, three of the
new tests fail (notes and shared filed under someone/skills, the CLI
copy given no From, the group keeping notes, the user's pdf with CC
Switch's origin and updatable); TestUndatedLockStillGroups passes, as it
should.
Not tried: a real npx skills add / delete / re-create in the GUI, real
CC Switch, the full go test ./... suite, a copy moved into the library
across disks before this change (its files' times are reset, so it is no
longer grouped by the lock).
2026-11-01 04:36:37 +08:00

178 lines
7.0 KiB
Go

package library
import (
"database/sql"
"io/fs"
"os"
"path/filepath"
"testing"
"time"
_ "modernc.org/sqlite"
)
// writtenAt sets every file under dir as last written at.
func writtenAt(t *testing.T, dir string, at time.Time) {
t.Helper()
err := filepath.WalkDir(dir, func(p string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
return os.Chtimes(p, at, at)
})
if err != nil {
t.Fatal(err)
}
}
// AzheRise on X: a skill installed from a GitHub repository was deleted,
// and another made under the same name in an agent's own skills folder
// was filed under that repository once brought into the library, not
// under This computer. The skills CLI's lock is by name and outlives the
// skill. Only the folder the CLI wrote is that repository's skill: one
// made since is the user's, in ~/.dsh/skills or in ~/.agents/skills,
// brought in now or before. A copy the CLI gave an agent stays grouped,
// edited later too.
func TestDeletedSkillsLockNotInherited(t *testing.T) {
h := sandbox(t)
installed := time.Now().Add(-2 * time.Hour).UTC()
ts := installed.Format(time.RFC3339Nano)
write(t, filepath.Join(h, ".agents/.skill-lock.json"), `{"version":3,"skills":{
"notes":{"source":"someone/skills","sourceType":"github","installedAt":"`+ts+`","updatedAt":"`+ts+`"},
"shared":{"source":"someone/skills","sourceType":"github","installedAt":"`+ts+`","updatedAt":"`+ts+`"},
"copied":{"source":"someone/skills","sourceType":"github","installedAt":"`+ts+`","updatedAt":"`+ts+`"},
"linked":{"source":"someone/skills","sourceType":"github","installedAt":"`+ts+`","updatedAt":"`+ts+`"}}}`)
// the reporter's: a new skill of the deleted one's name, in an
// agent's own folder (DeepSeek Harness's ~/.dsh/skills)
skill(t, filepath.Join(h, ".claude/skills/notes"), "notes", "Mine, made today")
// one made in ~/.agents/skills under such a name
skill(t, filepath.Join(h, ".agents/skills/shared"), "shared", "Mine too")
dirLink(filepath.Join(h, ".agents/skills/shared"), filepath.Join(h, ".claude/skills/shared"))
// the CLI's own: a copy it gave Claude Code (--copy), and one in
// ~/.agents/skills linked from Claude Code, each written as it locked
skill(t, filepath.Join(h, ".claude/skills/copied"), "copied", "The CLI's copy")
writtenAt(t, filepath.Join(h, ".claude/skills/copied"), installed.Add(-2*time.Second))
skill(t, filepath.Join(h, ".agents/skills/linked"), "linked", "The CLI's")
writtenAt(t, filepath.Join(h, ".agents/skills/linked"), installed.Add(-time.Second))
dirLink(filepath.Join(h, ".agents/skills/linked"), filepath.Join(h, ".claude/skills/linked"))
ok(t)(ImportSkills([]string{"notes", "shared", "copied", "linked"}))
want := map[string]string{"notes": "", "shared": "", "copied": "someone/skills", "linked": "someone/skills"}
got := repoIn(t)
for n, r := range want {
if got[n] != r {
t.Errorf("%s: repo %q, want %q", n, got[n], r)
}
}
l := mustLoad(t)
if f := l.skill("notes").From; f != "" {
t.Errorf("notes was given From %q", f)
}
if f := l.skill("copied").From; f != "someone/skills" {
t.Errorf("the CLI's copy wasn't given its From: %q", f)
}
// brought in before: the lock's repository was kept as From for the
// one in ~/.agents/skills, and the moved-in copy had none
l.skill("shared").From = "someone/skills"
l.skill("copied").From = ""
if err := l.save(); err != nil {
t.Fatal(err)
}
got = repoIn(t)
for n, r := range want {
if got[n] != r {
t.Errorf("as brought in before, %s: repo %q, want %q", n, got[n], r)
}
}
// the CLI's copy, edited since: its other files are as the CLI wrote
// them
write(t, filepath.Join(skillDir("copied"), "SKILL.md"), "---\nname: copied\ndescription: edited\n---\n")
if r := repoIn(t)["copied"]; r != "someone/skills" {
t.Errorf("the edited copy: repo %q", r)
}
}
// A lock that doesn't say when it wrote a skill groups by name as
// before: nothing tells the skill apart from another of its name.
func TestUndatedLockStillGroups(t *testing.T) {
h := sandbox(t)
write(t, filepath.Join(h, ".agents/.skill-lock.json"), `{"version":3,"skills":{"notes":{"source":"someone/skills","sourceType":"github"}}}`)
skill(t, filepath.Join(h, ".claude/skills/notes"), "notes", "N")
ok(t)(ImportSkills([]string{"notes"}))
if r := repoIn(t)["notes"]; r != "someone/skills" {
t.Errorf("repo %q", r)
}
}
// A group the user made keeps the skill put in it, not its name: a skill
// removed from the library and another of that name brought in later is
// not in the group.
func TestRemovedSkillLeavesItsGroup(t *testing.T) {
h := sandbox(t)
skill(t, filepath.Join(h, ".claude/skills/notes"), "notes", "Old")
skill(t, filepath.Join(h, ".claude/skills/other"), "other", "Other")
ok(t)(ImportSkills([]string{"notes", "other"}))
ok(t)(GroupSkills("", "Work", []string{"notes", "other"}))
ok(t)(RemoveSkill("notes"))
skill(t, filepath.Join(h, ".claude/skills/notes"), "notes", "New")
ok(t)(ImportSkills([]string{"notes"}))
v, err := Read(nil)
if err != nil {
t.Fatal(err)
}
if len(v.SkillGroups) != 1 || len(v.SkillGroups[0].Skills) != 1 || v.SkillGroups[0].Skills[0] != "other" {
t.Errorf("groups %+v", v.SkillGroups)
}
}
// CC Switch keeps the skills it installs in its own folder, by name, and
// that folder outlives a copy of it deleted from the library: another
// skill of that name brought in later is the user's, not CC Switch's. It
// is neither filed under CC Switch's repository nor updated from there,
// which would put the repository's skill in place of the user's.
func TestCCSwitchNameNotInherited(t *testing.T) {
h := sandbox(t)
ccs := filepath.Join(h, ".cc-switch")
write(t, filepath.Join(ccs, "skills/pdf/SKILL.md"), "---\nname: pdf\ndescription: CC Switch's\n---\n")
write(t, filepath.Join(ccs, "skills/same/SKILL.md"), "---\nname: same\ndescription: CC Switch's\n---\n")
db, err := sql.Open("sqlite", filepath.Join(ccs, "cc-switch.db"))
if err != nil {
t.Fatal(err)
}
for _, q := range []string{
`CREATE TABLE skills (id TEXT PRIMARY KEY, name TEXT NOT NULL, description TEXT, directory TEXT NOT NULL, repo_owner TEXT, repo_name TEXT, repo_branch TEXT DEFAULT 'main')`,
`INSERT INTO skills VALUES ('1','pdf','','pdf','owner','repo','main'), ('2','same','','same','owner','repo','main')`,
} {
if _, err := db.Exec(q); err != nil {
t.Fatal(err)
}
}
db.Close()
// the user's own pdf, and a copy CC Switch gave Claude Code
skill(t, filepath.Join(h, ".claude/skills/pdf"), "pdf", "Mine")
write(t, filepath.Join(h, ".claude/skills/same/SKILL.md"), "---\nname: same\ndescription: CC Switch's\n---\n")
ok(t)(ImportSkills([]string{"pdf", "same"}))
v, err := Read(nil)
if err != nil {
t.Fatal(err)
}
for _, s := range v.Skills {
switch s.Name {
case "pdf":
if s.Origin != "" || s.Repo != "" {
t.Errorf("the user's pdf: origin %q, repo %q", s.Origin, s.Repo)
}
case "same":
if s.Origin == "" || s.Repo != "owner/repo" {
t.Errorf("CC Switch's copy: origin %q, repo %q", s.Origin, s.Repo)
}
}
}
l := mustLoad(t)
if updatable(l.skill("pdf")) {
t.Error("the user's pdf can be updated from CC Switch's repository")
}
}