Files
magpie/internal/library/skill_moved_test.go
T
yetone 557f51dbf0 library: a skill linked into an app version the update deleted follows it to the current one (tjhhhh on Discord)
tjhhhh: ego lite keeps its skills in its framework's Versions/<v>. After
the app updated, the skill was in the new version's folder, the old folder
was gone, and magpie still linked the old one. dccfefdf7 (#2635) linked new
imports through the app's own steady link (~/.local/share/ego/ego-skills, or
Versions/Current), but a skill brought in before then links straight to the
resolved Versions/<old> path. tjhhhh's case is that old link: an import
made since dccfefdf7 already follows the update
(TestImportedSkillFollowsAppUpdate).

followMovedSkills (skill_moved.go) runs on every read of the Library page,
under the read's link memo, which is restarted when a link is mended. It
also runs as the first step of every sync. For each library skill that is
a link:
- Target gone, and its first missing part named like a version: link the
  same place under the folder's Current/current/latest link. With no such
  link, use the highest version beside it that has a SKILL.md there
  (1.10 after 1.9).
- Target in Versions/<v> while the bundle's Versions/Current links to
  another version with the same skill: link it through Current.
Source.Dir follows. Only the link is replaced (os.Remove of the link, then
dirLink, and the old link is put back if the new one can't be made). A
folder of the library's own, a link the user deleted that isn't a version
(still shown missing), and the app's old version folder are left alone.
docs/subsystems/library.md says so.

Run (all on mote via mote-run):
- New tests on ego lite's real layout (egoLite: framework Versions/<v>,
  Current, ~/.local/share/ego links): TestOldImportFollowsDeletedVersion
  (0.5.1.13 deleted, then 0.6.0.12, then the next update to 0.7.0.1 by
  itself, plus the agents' link through the library),
  TestOldImportFollowsVersionsCurrent (old kept, through Sync()),
  TestOldImportFollowsHighestVersion, TestMovedSkillsLeaveTheRest and
  TestVersionLess. All pass, as do dccfefdf7's three tests.
- Broken by hand: with followMovedSkills returning false at once, the
  three OldImport tests are red.
- go vet -tags nogui ./..., GOOS=linux go vet -tags nogui ./... and
  GOOS=windows go vet ./...: clean. gofmt -l: nothing.
- Full go test -tags nogui -count=1 -timeout 45m ./... in a sandbox HOME:
  80 ok, 3 FAIL.
  - internal/library TestReadLooksAtEachFolderOnce was mine: the mend ran
    before the read's memo, so each skill link was looked at twice. Fixed
    (the memo starts first and restarts after a mend). The whole
    internal/library package then passed (75s).
  - internal/gui TestSettingsSaveKeepsWhatItDoesNotSend is red on
    origin/main 3ecc02b78 and ccc791231 alike (#4514, SearchAll).
  - internal/cli TestAgentFieldNameShowsIt failed once under load. It
    passes alone and in a whole internal/cli run, and its message was lost
    to my grep of the log (#4583).
  The full ./... was not run again after the memo fix.
- At the pushed head, rebased on a51776f9c (which changed internal/library
  too): go vet and the whole internal/library package ok (95s).

Not tried: tjhhhh's own machine and ego lite's real update (the layout is
the #2635 fixture, built from iomect's report). Windows junctions to a
deleted version (dirLink is the same call ImportSkill uses). An app that
renames the folder above the version is not followed: it stays missing.
2026-10-31 22:01:45 +08:00

182 lines
6.2 KiB
Go

package library
import (
"os"
"path/filepath"
"strings"
"testing"
)
// oldImport brings the skill in at the agent's entry at as the library
// did before dccfefdf7: linked to where it really was, Versions/<v> and all.
func oldImport(t *testing.T, name, at string) string {
t.Helper()
if _, err := ImportSkill(name); err != nil {
t.Fatal(err)
}
real := realDir(at)
if err := os.Remove(skillDir(name)); err != nil {
t.Fatal(err)
}
if err := os.Symlink(real, skillDir(name)); err != nil {
t.Fatal(err)
}
if _, err := change(func(l *Library) error { l.skill(name).Source.Dir = real; return nil }); err != nil {
t.Fatal(err)
}
return real
}
func agentLink(t *testing.T, h, name, to string) string {
t.Helper()
shared := filepath.Join(h, ".agents/skills")
if err := os.MkdirAll(shared, 0o755); err != nil {
t.Fatal(err)
}
at := filepath.Join(shared, name)
if err := os.Symlink(to, at); err != nil {
t.Skip("no symlinks here:", err)
}
return at
}
// tjhhhh on Discord: ego lite brought in before dccfefdf7 links into
// Versions/0.5.1.13; the update to 0.6.0.12 deletes that folder. Reading
// the library links it through Versions/Current, where the skill is now,
// and the agents have the new one through the library.
func TestOldImportFollowsDeletedVersion(t *testing.T) {
h := sandbox(t)
versions := egoLite(t, h, "0.5.1.13")
at := agentLink(t, h, "ego-browser", filepath.Join(h, ".local/share/ego/ego-skills"))
old := oldImport(t, "ego-browser", at)
if !strings.Contains(old, "0.5.1.13") {
t.Fatalf("the old import links to %s", old)
}
egoLite(t, h, "0.6.0.12")
if err := os.RemoveAll(filepath.Join(versions, "0.5.1.13")); err != nil {
t.Fatal(err)
}
if _, ok := readMeta(skillDir("ego-browser")); ok {
t.Fatal("the old link still reads: the fixture didn't break it")
}
v, err := Read(nil)
if err != nil {
t.Fatal(err)
}
if len(v.Skills) != 1 || v.Skills[0].Missing {
t.Fatalf("skills %+v", v.Skills)
}
if m, ok := readMeta(skillDir("ego-browser")); !ok || m.Description != "ego 0.6.0.12" {
t.Errorf("the library's ego-browser is %q, want 0.6.0.12's", m.Description)
}
want := filepath.Join(realDir(versions), "Current/Resources/ego-skills/ego-browser")
if to, _ := os.Readlink(skillDir("ego-browser")); filepath.Clean(to) != want {
t.Errorf("links to %s, want %s, which the next update moves on too", to, want)
}
l, _ := load()
if d := l.skill("ego-browser").Source.Dir; d != want {
t.Errorf("source %s, want %s", d, want)
}
if m, ok := readMeta(at); !ok || m.Description != "ego 0.6.0.12" {
t.Errorf("the agents' ego-browser is %q, want 0.6.0.12's", m.Description)
}
// and it then follows the next update by itself
egoLite(t, h, "0.7.0.1")
if err := os.RemoveAll(filepath.Join(versions, "0.6.0.12")); err != nil {
t.Fatal(err)
}
if m, ok := readMeta(skillDir("ego-browser")); !ok || m.Description != "ego 0.7.0.1" {
t.Errorf("after the next update it is %q", m.Description)
}
}
// An update that leaves the old version behind: the library follows
// Versions/Current, now the new one, on a sync too.
func TestOldImportFollowsVersionsCurrent(t *testing.T) {
h := sandbox(t)
versions := egoLite(t, h, "0.5.1.13")
at := agentLink(t, h, "ego-browser", filepath.Join(versions, "0.5.1.13/Resources/ego-skills/ego-browser"))
oldImport(t, "ego-browser", at)
egoLite(t, h, "0.6.0.12")
if m, _ := readMeta(skillDir("ego-browser")); m.Description != "ego 0.5.1.13" {
t.Fatalf("before a sync the library's is %q", m.Description)
}
if _, err := Sync(); err != nil {
t.Fatal(err)
}
if m, ok := readMeta(skillDir("ego-browser")); !ok || m.Description != "ego 0.6.0.12" {
t.Errorf("the library's ego-browser is %q, want 0.6.0.12's", m.Description)
}
// the old version is the app's to delete, not the library's
if _, err := os.Stat(filepath.Join(versions, "0.5.1.13/Resources/ego-skills/ego-browser/SKILL.md")); err != nil {
t.Errorf("the old version's folder: %v", err)
}
}
// An app with no Current link: the highest version beside the gone one
// that has the same skill, by number (1.10 after 1.9).
func TestOldImportFollowsHighestVersion(t *testing.T) {
h := sandbox(t)
vs := filepath.Join(h, ".tool/versions")
skill(t, filepath.Join(vs, "1.2.0/skills/review"), "review", "1.2.0")
at := agentLink(t, h, "review", filepath.Join(vs, "1.2.0/skills/review"))
oldImport(t, "review", at)
skill(t, filepath.Join(vs, "1.9.0/skills/review"), "review", "1.9.0")
skill(t, filepath.Join(vs, "1.10.0/skills/review"), "review", "1.10.0")
skill(t, filepath.Join(vs, "2.0.0/other"), "other", "2.0.0") // no review here
if err := os.RemoveAll(filepath.Join(vs, "1.2.0")); err != nil {
t.Fatal(err)
}
if _, err := Read(nil); err != nil {
t.Fatal(err)
}
if m, ok := readMeta(skillDir("review")); !ok || m.Description != "1.10.0" {
t.Errorf("the library's review is %q, want 1.10.0's", m.Description)
}
}
// What isn't a version moved on from is left as it is: a folder of the
// library's own, and a link to a folder the user deleted, which shows as
// missing rather than being pointed at something else.
func TestMovedSkillsLeaveTheRest(t *testing.T) {
h := sandbox(t)
src := filepath.Join(h, "code/notes")
skill(t, src, "notes", "Mine")
agentLink(t, h, "notes", src)
if _, err := ImportSkill("notes"); err != nil {
t.Fatal(err)
}
skill(t, filepath.Join(h, "code/notes-2"), "notes", "Another")
own := filepath.Join(h, ".claude/skills/own")
skill(t, own, "own", "Moved in")
if _, err := ImportSkill("own"); err != nil {
t.Fatal(err)
}
if err := os.RemoveAll(src); err != nil {
t.Fatal(err)
}
v, err := Read(nil)
if err != nil {
t.Fatal(err)
}
if to, err := os.Readlink(skillDir("notes")); err != nil || realBelow(to) != realBelow(src) {
t.Errorf("notes links to %q (%v), want %s as it was", to, err, src)
}
for _, s := range v.Skills {
if s.Name == "notes" && !s.Missing {
t.Errorf("notes, deleted by the user, isn't shown missing")
}
}
if fi, err := os.Lstat(skillDir("own")); err != nil || !fi.IsDir() {
t.Errorf("the library's own folder: %v %v", fi, err)
}
}
func TestVersionLess(t *testing.T) {
for _, c := range [][2]string{{"1.9.0", "1.10.0"}, {"0.5.1.13", "0.6.0.12"}, {"v1.2", "v1.2.1"}, {"2.0.0-beta", "2.0.1"}} {
if !versionLess(c[0], c[1]) || versionLess(c[1], c[0]) {
t.Errorf("%s < %s", c[0], c[1])
}
}
}