diff --git a/internal/library/skill_version_test.go b/internal/library/skill_version_test.go new file mode 100644 index 000000000..7dfa74e2d --- /dev/null +++ b/internal/library/skill_version_test.go @@ -0,0 +1,101 @@ +package library + +import ( + "os" + "path/filepath" + "testing" +) + +// egoLite lays out iomect's ego lite (#2635) under h at version v: the +// framework's Versions/ holding the skill, Versions/Current, and the +// app's own links in ~/.local/share/ego, which it points at the new +// version when it updates. +func egoLite(t *testing.T, h, v string) (versions string) { + t.Helper() + versions = filepath.Join(h, "Applications/ego lite.app/Contents/Frameworks/ego Framework.framework/Versions") + skill(t, filepath.Join(versions, v, "Resources/ego-skills/ego-browser"), "ego-browser", "ego "+v) + share := filepath.Join(h, ".local/share/ego") + if err := os.MkdirAll(share, 0o755); err != nil { + t.Fatal(err) + } + for link, to := range map[string]string{ + filepath.Join(versions, "Current"): v, + filepath.Join(share, "active_version_dir"): filepath.Join(versions, v), + filepath.Join(share, "ego-skills"): filepath.Join(share, "active_version_dir/Resources/ego-skills/ego-browser"), + } { + os.Remove(link) + if err := os.Symlink(to, link); err != nil { + t.Skip("no symlinks here:", err) + } + } + return versions +} + +// A skill an app keeps in a versioned folder, brought in, is the app's +// current one after the app updates (iomect, #2635): ~/.agents/skills/ +// ego-browser -> ~/.local/share/ego/ego-skills -> active_version_dir/... -> +// Versions/0.5.1.13, and the update points active_version_dir at 0.6.0.12. +func TestImportedSkillFollowsAppUpdate(t *testing.T) { + h := sandbox(t) + egoLite(t, h, "0.5.1.13") + shared := filepath.Join(h, ".agents/skills") + if err := os.MkdirAll(shared, 0o755); err != nil { + t.Fatal(err) + } + if err := os.Symlink(filepath.Join(h, ".local/share/ego/ego-skills"), filepath.Join(shared, "ego-browser")); err != nil { + t.Fatal(err) + } + if _, err := ImportSkill("ego-browser"); err != nil { + t.Fatal(err) + } + egoLite(t, h, "0.6.0.12") + if m, ok := readMeta(skillDir("ego-browser")); !ok || m.Description != "ego 0.6.0.12" { + to, _ := os.Readlink(skillDir("ego-browser")) + t.Errorf("after the update the library's ego-browser is %q (links to %s), want 0.6.0.12's", m.Description, to) + } + v, _ := Read(nil) + if len(v.Skills) != 1 || v.Skills[0].Missing || len(v.FoundSkills) != 0 { + t.Errorf("skills %+v, found %+v", v.Skills, v.FoundSkills) + } +} + +// An agent's link straight into Versions/ is brought in through +// Versions/Current, which an update moves on. +func TestImportedSkillThroughVersionsCurrent(t *testing.T) { + h := sandbox(t) + versions := egoLite(t, h, "0.5.1.13") + shared := filepath.Join(h, ".agents/skills") + if err := os.MkdirAll(shared, 0o755); err != nil { + t.Fatal(err) + } + if err := os.Symlink(filepath.Join(versions, "0.5.1.13/Resources/ego-skills/ego-browser"), filepath.Join(shared, "ego-browser")); err != nil { + t.Fatal(err) + } + if _, err := ImportSkill("ego-browser"); err != nil { + t.Fatal(err) + } + egoLite(t, h, "0.6.0.12") + if m, ok := readMeta(skillDir("ego-browser")); !ok || m.Description != "ego 0.6.0.12" { + t.Errorf("after the update the library's ego-browser is %q, want 0.6.0.12's", m.Description) + } +} + +// A link to a folder that isn't versioned is linked to where it is, as +// before. +func TestImportedSkillPlainLink(t *testing.T) { + h := sandbox(t) + src := filepath.Join(h, "code/notes") + skill(t, src, "notes", "Mine") + if err := os.MkdirAll(filepath.Join(h, ".claude/skills"), 0o755); err != nil { + t.Fatal(err) + } + if err := os.Symlink(src, filepath.Join(h, ".claude/skills/notes")); err != nil { + t.Skip("no symlinks here:", err) + } + if _, err := ImportSkill("notes"); err != nil { + t.Fatal(err) + } + if to, err := os.Readlink(skillDir("notes")); err != nil || realDir(to) != realDir(src) { + t.Errorf("library's notes links to %q (%v), want %s", to, err, src) + } +} diff --git a/internal/library/skills.go b/internal/library/skills.go index c5ac808ec..c5a42def5 100644 --- a/internal/library/skills.go +++ b/internal/library/skills.go @@ -1798,6 +1798,65 @@ func foundSkills(l *Library) []FoundSkill { return res } +// linkVia is what the library links to for a skill found at the entry at, +// whose folder is really at real: the first link on the way from at that +// is outside every skills folder (the agents', the shared one and the +// library's), which the app that made it keeps pointing at its current +// folder, as ego lite's ~/.local/share/ego/ego-skills does. A folder in a +// bundle's Versions/ is named through Versions/Current when Current is +// that very version. It is real when nothing on the way is any steadier, +// or when the way doesn't end at real. +func linkVia(at, real string) string { + var homes []string + for _, d := range []string{skillsDir(), sharedSkillsDir()} { + homes = append(homes, realBelow(d)) + } + for _, t := range Targets() { + if t.Skills != "" { + homes = append(homes, realBelow(t.Skills)) + } + } + inHome := func(p string) bool { + up := realBelow(filepath.Dir(p)) + return slices.ContainsFunc(homes, func(h string) bool { return within(up, h) }) + } + cur := filepath.Clean(at) + for i := 0; inHome(cur); i++ { + to, ok := linkTarget(cur) + if !ok || i > 40 { + cur = real + break + } + if !filepath.IsAbs(to) { + to = filepath.Join(filepath.Dir(cur), to) + } + cur = filepath.Clean(to) + } + cur = currentVersion(cur) + if realDir(cur) != real { + return real + } + return cur +} + +// currentVersion is p with a Versions/ part of it named Versions/Current, +// when that link is there and points at : a macOS framework's versions, +// of which an update leaves the old one behind. +func currentVersion(p string) string { + parts := strings.Split(p, string(filepath.Separator)) + for i := len(parts) - 2; i > 0; i-- { + if parts[i] != "Versions" || parts[i+1] == "Current" { + continue + } + vs := strings.Join(parts[:i+1], string(filepath.Separator)) + cur := filepath.Join(vs, "Current") + if realDir(cur) == realDir(filepath.Join(vs, parts[i+1])) && linked(cur) { + return filepath.Join(append([]string{cur}, parts[i+2:]...)...) + } + } + return p +} + // ImportSkill takes a skill the agents have of their own into the library: // a folder is moved in, a link to a folder elsewhere is linked to from the // library, and the agents that had it get the library's from then on. @@ -1858,9 +1917,15 @@ func importSkill(l *Library, found []FoundSkill, name string) error { src = nil } case f.Link != "" || shared: - if err := dirLink(f.real, skillDir(name)); err != nil { + // linked through the way the agent's own link goes, not to where + // it ends today: an app that keeps its skills in a versioned + // folder points its own link at the new one when it updates + // (iomect's ego lite, #2635), and the library follows + via := linkVia(f.at, f.real) + if err := dirLink(via, skillDir(name)); err != nil { return err } + src.Dir = via default: // a move that copied it all but couldn't take the whole folder away // (a file in it held open) has brought it in all the same: what is