Files
magpie/internal/library/skill_beside.go
T
yetone ca64168d3e library: a skill beside itself folds back on read; GitHub by git address is one repo
Refeccd on X said eee217712 (v0.1.4213) didn't fix "skills from a grouped
git repository get the repository's suffix". Reproduced in a sandbox HOME
three ways the fix left open:

- Copies an earlier magpie had already suffixed (blog-team beside blog)
  were folded only by foldBeside in a change. Upgrading and opening the
  Library changed nothing; Read, CheckSkills and Read again all kept them.
  readView now folds them too, when any skill has Called and a remote
  source, saves and syncs the agents in the same read, so a folded copy's
  links don't point at a removed folder. Sync problems join the view's.
- A GitHub repository added as owner/repo and then by its git address
  (git@github.com:owner/repo.git) was two sources: sameRepo and
  fromSameSource compared Kind first, so every skill went in again as
  name-owner. Both now match github and git sources through remoteRepo.
  foldBeside folds a copy from the same folder of the same repository
  (before only one whose target was Gone and at another path).
- gitRepoOf looked at a skill's folder and three above it, while a probe
  finds skills four folders into a repository: a checkout's
  skills/team/writing/poem wasn't traced, and an edited poem went in as
  poem-sub. It now looks at the folder and four above it.

Existing suffixed copies: folded on the next read of the Library page
(or next change); the copy's files, renamed, replace the skill's, its
agents are added, and both old folders go to the backups.

Tests (skill_group_suffix_test.go), each broken by hand:
- TestSkillBesideItselfFoldsOnRead: red with the readView fold removed.
- TestGitHubRepoByGitAddressIsHad: red with sameRepo kept same-kind.
- TestGitHubCopyByGitAddressFoldsBack: red with each of the above and
  with foldBeside's old condition.
- TestCheckoutSkillFourDeepIsTraced: red with gitRepoOf at 4.

Docs: docs/subsystems/library.md's "Skills installed together" no longer
says nothing is renamed after the fact; it describes sameRepo, movedIn,
besideName, followsHere's depth and foldBeside on read.

Run on mica at 168d6fb13 (on origin/main ab7cb5537): gofmt -l internal
clean; go vet ./..., GOOS=windows go vet ./..., GOOS=linux go vet -tags
nogui ./... ok; go test -timeout 45m ./... ok, 101 packages, no FAIL
(internal/library 298s). An earlier full run at 1bfdeab52 was red in two
of these new tests only: other tests mark owner/repo tooBig for the whole
run, so the fixture now uses its own repository (refeccd/grouped-skills).
No GUI file changed and no GUI test was run: the change is Go only.

Not tried: a real GitLab or intranet repository, and the reporter's own
layout (the reply gave no detail); a real GitHub repo by SSH address
(the test maps it to a local repository through git insteadOf).
2026-11-15 07:04:53 +08:00

304 lines
9.2 KiB
Go

package library
import (
"errors"
"fmt"
"io/fs"
"os"
slash "path" // a repository's paths, always with /
"path/filepath"
"slices"
"strings"
"time"
)
// Two skills from different places can go by one name: anthropics/skills'
// skill-creator and openai/skills' are not the same skill (mora1n on
// Discord). The library keeps each under a name of its own, since that
// name is its folder here and in every agent's skills folder: the first
// keeps the name its SKILL.md gives, and one installed after it goes in
// beside it under that name with its source's owner added
// (skill-creator-openai). Called keeps the name its SKILL.md gave.
// hadSkill is the library's skill that is the one at from already: one
// installed from that very place, whatever its name here, or one by its
// name with no source to tell it by (taken in from an agent's folder) and
// the very same files.
func (l *Library) hadSkill(src Source, c Candidate, from string) *Skill {
for _, s := range l.Skills {
if fromSameSource(s, src, c.Path, from) {
return s
}
}
if s := l.skill(c.Name); s != nil && s.Source == nil && sameTree(skillDir(s.Name), from) {
return s
}
if s := l.skill(c.Name); s != nil && movedIn(s, src, c, from) {
return s
}
return nil
}
// movedIn is whether the library's skill s, by the name of the one at
// from, is that skill moved to another folder of its repository: the
// repository put its skills in groups since (skills/pdf to
// skills/documents/pdf), and its old folder isn't there any more. Taken
// for another skill, it went in beside itself under its owner's name
// (Refeccd on X).
func movedIn(s *Skill, src Source, c Candidate, from string) bool {
if !sameRepo(s.Source, src) || s.Called != "" || s.Source.Path == c.Path {
return false
}
// only a folder the fetch had: one fetched as far as src.Path holds
// nothing of the rest of the repository
if src.Path != "" && s.Source.Path != src.Path && !strings.HasPrefix(s.Source.Path, src.Path+"/") {
return false
}
return goneFrom(repoRoot(from, c.Path), s.Source.Path)
}
// besideName is the name the skill at path of src goes in beside the
// library's skill called name by. One from another folder of the same
// repository takes that folder's name, which tells the two apart, where
// the owner both have would not (skills/writing/blog beside
// skills/coding/blog is blog-writing); one from elsewhere takes its
// source's (freeSkillName).
func (l *Library) besideName(name string, src Source, path string) string {
if s := l.skill(name); s != nil && sameRepo(s.Source, src) {
if dir := slash.Dir(path); dir != "." && dir != "" {
if n := withSuffix(name, slash.Base(dir)); n != "" && !l.skillNameTaken(n) {
return n
}
}
}
return l.freeSkillName(name, src)
}
// skillNameTaken is whether the library has a skill called name, or its
// folder has something by that name. One that can't be looked at is
// taken: what is there is unknown.
func (l *Library) skillNameTaken(name string) bool {
if l.skill(name) != nil {
return true
}
_, err := os.Lstat(skillDir(name))
return !errors.Is(err, fs.ErrNotExist)
}
// freeSkillName is the name a skill called name from src goes by in the
// library: name itself while nothing has it, else name with the source's
// owner, then its repository's (or folder's) name, then a number added.
func (l *Library) freeSkillName(name string, src Source) string {
if !l.skillNameTaken(name) {
return name
}
owner, repo := sourceParts(src)
var tries []string
if owner != "" {
tries = append(tries, owner, owner+"-"+repo)
}
if repo != "" {
tries = append(tries, repo)
}
for _, s := range tries {
if n := withSuffix(name, s); n != "" && n != name && !l.skillNameTaken(n) {
return n
}
}
base := owner
if base == "" {
base = repo
}
for i := 2; ; i++ {
suffix := fmt.Sprint(i)
if base != "" {
suffix = base + "-" + suffix
}
if n := withSuffix(name, suffix); n != "" && !l.skillNameTaken(n) {
return n
}
}
}
// sourceParts is a source's owner and repository: openai and skills for
// openai/skills or https://gitlab.com/openai/skills.git, and for a folder
// the folder it is in and its own name.
func sourceParts(src Source) (owner, repo string) {
var parts []string
if src.Kind == "folder" {
d := filepath.Clean(src.Dir)
parts = []string{filepath.Base(filepath.Dir(d)), filepath.Base(d)}
} else {
r := strings.TrimSuffix(strings.TrimRight(src.Repo, "/"), ".git")
parts = strings.FieldsFunc(r, func(c rune) bool { return c == '/' || c == ':' })
}
switch len(parts) {
case 0:
return "", ""
case 1:
return "", fitName(parts[0])
}
return fitName(parts[len(parts)-2]), fitName(parts[len(parts)-1])
}
// withSuffix is name-suffix, name cut short so that the whole is a name
// the library takes (nameRe): at most 64 bytes.
func withSuffix(name, suffix string) string {
suffix = fitName(suffix)
if suffix == "" {
return ""
}
if max := 64 - len(suffix) - 1; len(name) > max {
if max < 1 {
return ""
}
name = strings.TrimRight(name[:max], "-_")
}
n := name + "-" + suffix
if !nameRe.MatchString(n) {
return ""
}
return n
}
// nameSkill sets the name a skill's SKILL.md gives in its front matter to
// name, the folder's: an agent knows a skill by that name, and two by one
// name in an agent are one shadowing the other. Nothing else in it
// changes. A SKILL.md that gives no name already goes by its folder's,
// and one that is a link isn't written through.
func nameSkill(dir, name string) error {
p := filepath.Join(dir, "SKILL.md")
fi, err := os.Lstat(p)
if err != nil || !fi.Mode().IsRegular() {
return err
}
b, err := os.ReadFile(p)
if err != nil {
return err
}
if out, ok := renamedMeta(string(b), name); ok {
return os.WriteFile(p, []byte(out), fi.Mode().Perm())
}
return nil
}
// renamedMeta is a SKILL.md with its front matter's top-level name set to
// name, its line endings kept; false when it gives no name to set.
func renamedMeta(s, name string) (string, bool) {
lines := strings.SplitAfter(s, "\n")
if len(lines) == 0 || strings.TrimRight(lines[0], "\r\n") != "---" {
return s, false
}
for i := 1; i < len(lines); i++ {
line := strings.TrimRight(lines[i], "\r\n")
if line == "---" {
break
}
if strings.HasPrefix(line, "name:") {
if strings.TrimSpace(strings.Trim(strings.TrimSpace(line[len("name:"):]), `"'`)) == name {
return s, false
}
lines[i] = "name: " + name + lines[i][len(line):]
return strings.Join(lines, ""), true
}
}
return s, false
}
// fetchedHash is the hash a skill's files fetched at from have once in
// the library: for one kept under another name than its SKILL.md gives
// (Called), as named there. One that can't be told is "".
func (s *Skill) fetchedHash(from string) string {
if s.Called == "" {
return hashDir(from)
}
tmp, err := os.MkdirTemp("", "magpie-skill-named-")
if err != nil {
return ""
}
defer os.RemoveAll(tmp)
dir := filepath.Join(tmp, s.Name)
if err := copyDir(from, dir); err != nil {
return ""
}
if err := nameSkill(dir, s.Name); err != nil {
return ""
}
return hashDir(dir)
}
// foldBeside puts back together a skill that went in beside itself
// (Refeccd on X): installed from a repository the library's skill by its
// name was traced to, or moved into a group of, it was taken for another
// skill and kept as name-owner. The skill by the name it was called keeps
// its name and takes the copy's files, source and agents, its own files
// going to the backups when they differ (linkSkill); the copy, by then
// the same, goes to the backups too. A skill kept beside one from
// elsewhere, which was right, stays as it is. It gives the names it put
// back together.
func (l *Library) foldBeside() []string {
var folded []string
tr := &tracer{}
for _, s := range slices.Clone(l.Skills) {
if s.Called == "" || s.Called == s.Name || !s.Source.remote() {
continue
}
t := l.skill(s.Called)
if t == nil || t.Called != "" {
continue
}
switch {
case !t.Source.remote():
// the library's own, traced to that repository
if r := tr.repoOf(t); r == "" || !tracedTo(r, *s.Source) {
continue
}
case sameRepo(t.Source, *s.Source):
// the same folder of that repository (added once as owner/repo
// and once by its git address), or another folder of it, which
// a check found it no longer has: moved into a group there
if t.Source.Path != s.Source.Path && t.Gone == "" {
continue
}
default:
continue
}
if err := l.foldInto(t, s); err != nil {
continue
}
folded = append(folded, t.Name)
}
return folded
}
// foldInto is foldBeside for t, the skill by the name s was called.
func (l *Library) foldInto(t, s *Skill) error {
// s's files, named as t is
tmp := skillDir("." + t.Name + ".fold")
os.RemoveAll(tmp)
defer os.RemoveAll(tmp)
if err := copyDir(skillDir(s.Name), tmp); err != nil {
return err
}
if err := nameSkill(tmp, t.Name); err != nil {
return err
}
src, commit := *s.Source, s.Commit
if _, err := l.linkSkill(t, src, src.Path, tmp, s.Agents); err != nil {
return err
}
if t.Commit == "" {
t.Commit = commit
}
t.Gone = ""
for _, p := range l.Projects {
if agents, ok := p.Skills[s.Name]; ok {
if p.Skills[t.Name] == nil {
p.Skills[t.Name] = agents
}
delete(p.Skills, s.Name)
}
}
return removeSkill(l, s.Name, time.Now().Format("2006-01-02_15-04-05.000"))
}