Files
magpie/internal/library/skill_beside.go
T
yetone 20482d71ff library: a skill by a name the library has from elsewhere goes in beside it
mora1n on Discord: anthropics/skills' skill-creator and openai/skills'
are two skills by one name, and only the first could be added; the
second was skipped as "another skill called skill-creator, left as it
is". The library keyed being had by name in every place that asked.

Now being had is decided by source (hadSkill): the same source under any
name here, or a skill with no source (taken in from an agent's folder)
by that name with the very same files. Another skill from GitHub or a
git repository whose name is taken goes in beside it as
<name>-<owner> (skill-creator-openai), then owner-repo, repo, a number,
at most 64 bytes (freeSkillName in skill_beside.go). The library's copy
of its SKILL.md gets that name in the front matter's top-level name:
line (nameSkill), so no agent sees two skills by one name; Skill.Called
keeps the name it came with, and the carried bundle carries it. Its hash
is of the renamed copy, and checks (GitHub and git) and updates compare
what they fetch renamed the same way (fetchedHash), so it doesn't read
as edited and an update keeps the name.

The class: probe (Candidate.Have now means this skill; Candidate.As is
the name it has or would get), installFrom (listed and unlisted folder
by that name), the new-skills check (no longer drops a skill because
its name is taken), the market's had check (by name only for a skill
with no repository: none or a folder), carry, the TUI's probe list and
the GUI's probe list ("adds as {name}" with why in its title, "In the
library as {name}"; zh, zh-TW, ja, de; the tag is cut short in its row).

Old data: nothing is renamed. A skill already in the library keeps its
name, folder, SKILL.md and agent links; library.json without "called"
reads as before. Only a skill added from now on can get another name.
Not changed: a folder of the user's own is linked as it is and its
SKILL.md can't be named apart, so one by a taken name is still skipped,
now saying to rename it in its SKILL.md; and an agent's own skills
folder still holds one skill per name (an agent's own skill by the
library's name is settled by Use library / Keep agent's as before).

Run (mote, at c6fd4b37c on origin/main 6a900e0d0, then rebased on
0fe9f97c1, which touches only claudetui, gui and provider):
- gofmt -l, go vet ./..., GOOS=linux go vet -tags nogui ./...,
  GOOS=windows go vet ./..., go build ./..., go build -tags nogui:
  clean.
- go test -tags nogui -count=1 -timeout 45m ./...: 82 packages ok,
  internal/gui red only in TestSettingsSaveKeepsWhatItDoesNotSend
  (SearchAll, #4514, open on main, not this change).
- After the rebase: internal/library and internal/tui ok.
- GUI, both engines: skill-install-beside (new), every skill-*,
  library-*, found-skill-*, remove-found-*, mcp-*, i18n-keys, gui-ja,
  gui-de, gui-zh-tw, font-cascade, inner-scroll-contain, win-fonts:
  1427 tests, 1423 pass. The 4 reds fail the same at origin/main
  3baf3b6df run alone: gui-ja (#4580), i18n-keys "Unmarked" (#4524),
  library-row-remove chromium (#3099).

Red on the old code: the anthropics-then-openai install against
origin/main code fails with "the library already has another skill
called skill-creator, left as it is". Broken by hand: fetchedHash made
to hash the fetched files unrenamed turns
TestSameNameFromAnotherSourceGoesInBeside red (hash mismatch); the
.lib-pick > .lib-tag rule removed turns skill-install-beside red ("the
tag stays in its row") in both widths.

Not tried: real anthropics/skills and openai/skills installs in a
sandbox HOME (the tests serve both repositories' layouts as tarballs;
the fixtures put both at skills/skill-creator. The real openai/skills
keeps it under skills/.system, which a probe of the repository's top
doesn't walk into; that is how probing has been and isn't changed
here); Windows and Linux runs of the GUI.
2026-11-01 02:02:24 +08:00

188 lines
5.3 KiB
Go

package library
import (
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
)
// 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
}
return nil
}
// 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)
}