library: every skill named in another script is listed, under its folder's name
4db65681c carries this commit's first message by mistake (another session's amend read it from a shared file); it holds none of this change, which is here.
tnt1223 on Discord: Library › Skills listed 16 skills for
https://github.com/asdfgh1445/ctf-super-hub, which has 35 (one folder each
at the top, 35 SKILL.md in its recursive tree, not truncated).
Cause: the listing found all 35, but nameFor made each SKILL.md's `name`
fit for a file name by dropping everything outside ASCII. "CTF•密码学" and
ten more became "CTF", "Strix•RCE 测试" and nine more "Strix", "头脑风暴"
nothing, and unhiddenFirst lists skills of the same name once, so 35
became 16. Not a depth limit, cap, pagination or truncated tree: the probe
downloads the tarball and walks it; a truncated tree and GitHub's rate
limit are already said (skilltree.go, fetch).
Fix: a declared name that would lose a letter or digit (another script)
or comes out empty goes by its folder's name, which a repository keeps
apart. An ASCII name goes as before ("My Skill" is still My-Skill), so a
skill installed from one is still the library's. A skill at a
repository's top now falls back to the repository's name (srcFolder),
not the temporary download folder; candidates takes that name from each
caller (probe, Group again, CC Switch origin, new-skill check).
Tests: TestSkillsNamedInAnotherScriptAllListed probes and installs from a
fake of the repository built from its real tree (35 folders, front matter
names as written at 7715719); TestSkillNameFallsBackToFolderOnlyWhenLettersAreLost
and TestSkillAtTheTopNamedInAnotherScript cover the rule and the top
folder. No network.
Run:
- mote: go vet ./internal/library; GOOS=linux and GOOS=windows go vet
-tags nogui ./internal/library; gofmt -l internal docs (empty);
go test ./internal/library -run 'TestSkillsNamedInAnotherScript|
TestSkillNameFallsBack|TestSkillAtTheTop|TestRepoWithAPlugin|Probe|
CCSwitch|Regroup|NewSkill|From' -count=1: ok.
- wsl-run (magpie-desktop): go test -tags nogui ./internal/library
-timeout 45m -count=1: ok 94.9s.
- Live, sandbox HOME (the package's TestMain/testenv), a throwaway test
calling ProbeSkills("https://github.com/asdfgh1445/ctf-super-hub")
against the real GitHub: 35 candidates, each named by its folder,
nothing left out. Not committed.
Broken by hand: nameFor using the declared name whenever it isn't empty
(the old rule): TestSkillsNamedInAnotherScriptAllListed lists 16 with
names CTF/Strix/..., and the rule test fails on "CTF•密码学" and
"Café notes". Restored, green.
Not tried: the full ./... suite (mote at load ~350 from other runs; only
internal/library changed); the GUI suite (no app.js change); Windows test
run; installing the real repository's skills into a real agent. Not
changed: two different skills declaring the same ASCII name in folders of
different names are still listed once, as copies of one skill are
(unhiddenFirst).
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -37,7 +37,7 @@ func ccSwitchOrigin(s *Skill) (Source, bool) {
|
||||
// origin finds the skill in the repository fetched for it: the folder
|
||||
// named as CC Switch named it, or the skill of that name.
|
||||
func origin(root string, src Source, name string) (string, bool) {
|
||||
for _, c := range candidates(root, "") {
|
||||
for _, c := range candidates(root, "", srcFolder(src)) {
|
||||
if lastPart(c.Path) == src.Path || c.Name == name {
|
||||
return c.Path, true
|
||||
}
|
||||
|
||||
@@ -378,7 +378,7 @@ func RegroupSkills() (int, error) {
|
||||
continue
|
||||
}
|
||||
fetched++
|
||||
noted += noteFrom(src, root, candidates(root, ""))
|
||||
noted += noteFrom(src, root, candidates(root, "", srcFolder(src)))
|
||||
os.RemoveAll(root)
|
||||
}
|
||||
if fetched == 0 && first != nil {
|
||||
|
||||
@@ -0,0 +1,151 @@
|
||||
package library
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// tnt1223 on Discord: asdfgh1445/ctf-super-hub has 35 skills, one folder
|
||||
// each at the top, and the page listed 16. Each SKILL.md names its skill in
|
||||
// Chinese after a "CTF•" or "Strix•", and made fit for a file name the
|
||||
// names fell to "CTF" (11 of them), "Strix" (10) and "" (头脑风暴), which
|
||||
// were listed once each. The folders and names are the repository's own,
|
||||
// at 7715719, quotes as written.
|
||||
var ctfSuperHub = []struct{ dir, name string }{
|
||||
{"brainstorming", `"头脑风暴"`},
|
||||
{"ctf-ai-ml", `"CTF•AI/ML 攻防"`},
|
||||
{"ctf-beginner-hub", `"CTF•新手入口"`},
|
||||
{"ctf-crypto", `"CTF•密码学"`},
|
||||
{"ctf-forensics", `"CTF•数字取证"`},
|
||||
{"ctf-malware", `"CTF•恶意软件分析"`},
|
||||
{"ctf-misc", `"CTF•杂项"`},
|
||||
{"ctf-osint", `"CTF•开源情报"`},
|
||||
{"ctf-pwn", `"CTF•二进制利用"`},
|
||||
{"ctf-reverse", `"CTF•逆向工程"`},
|
||||
{"ctf-super-hub", `"CTF•超级总控"`},
|
||||
{"ctf-web", `"CTF•Web 漏洞"`},
|
||||
{"ctf-writeup", `"CTF•题解写作"`},
|
||||
{"solve-challenge", `"CTF•挑战调度器"`},
|
||||
{"strix-authentication-jwt", `Strix•JWT 身份认证测试`},
|
||||
{"strix-beginner-hub", `Strix•新手入口`},
|
||||
{"strix-broken-function-level-authorization", `Strix•功能级授权缺陷`},
|
||||
{"strix-business-logic", `Strix•业务逻辑漏洞`},
|
||||
{"strix-csrf", `Strix•CSRF 测试`},
|
||||
{"strix-ffuf", `Strix•ffuf 用法`},
|
||||
{"strix-httpx", `Strix•httpx 用法`},
|
||||
{"strix-idor", `Strix•IDOR 测试`},
|
||||
{"strix-information-disclosure", `Strix•信息泄露测试`},
|
||||
{"strix-insecure-file-uploads", `Strix•不安全文件上传`},
|
||||
{"strix-katana", `Strix•Katana 用法`},
|
||||
{"strix-nuclei", `Strix•Nuclei 用法`},
|
||||
{"strix-open-redirect", `Strix•开放重定向`},
|
||||
{"strix-path-traversal-lfi-rfi", `Strix•路径穿越与文件包含`},
|
||||
{"strix-quick", `Strix•快速扫描`},
|
||||
{"strix-rce", `Strix•远程代码执行`},
|
||||
{"strix-sql-injection", `Strix•SQL 注入`},
|
||||
{"strix-sqlmap", `Strix•sqlmap 用法`},
|
||||
{"strix-ssrf", `Strix•SSRF 测试`},
|
||||
{"strix-standard", `Strix•标准扫描`},
|
||||
{"strix-xss", `Strix•XSS 测试`},
|
||||
}
|
||||
|
||||
func TestSkillsNamedInAnotherScriptAllListed(t *testing.T) {
|
||||
sandbox(t)
|
||||
files := map[string]string{
|
||||
"README.md": "# CTF Super Hub\n",
|
||||
"SKILL-INDEX.md": "| skill |\n",
|
||||
"docs/guide.md": "guide\n",
|
||||
"evals/README.md": "evals\n",
|
||||
"scripts/install.sh": "#!/bin/sh\n",
|
||||
".github/workflows/validate.yml": "on: push\n",
|
||||
}
|
||||
var want []string
|
||||
for _, s := range ctfSuperHub {
|
||||
files[s.dir+"/SKILL.md"] = "---\nname: " + s.name + "\ndescription: \"用于 " + s.dir + ";触发名:" + s.dir + "\"\n---\n\n# " + s.dir + "\n"
|
||||
files[s.dir+"/references/notes.md"] = "notes\n"
|
||||
want = append(want, s.dir)
|
||||
}
|
||||
fakeSkillRepo(t, files)
|
||||
|
||||
p, err := ProbeSkills("https://github.com/asdfgh1445/ctf-super-hub")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var paths, names []string
|
||||
for _, c := range p.Candidates {
|
||||
paths, names = append(paths, c.Path), append(names, c.Name)
|
||||
if !nameRe.MatchString(c.Name) {
|
||||
t.Errorf("%s goes by %q, which the library can't hold", c.Path, c.Name)
|
||||
}
|
||||
}
|
||||
slices.Sort(paths)
|
||||
if !slices.Equal(paths, want) {
|
||||
t.Errorf("listed %d skills %v, want all %d", len(paths), paths, len(want))
|
||||
}
|
||||
if !slices.Equal(names, want) { // sorted by name, each its folder's
|
||||
t.Errorf("names %v, want %v", names, want)
|
||||
}
|
||||
|
||||
r, err := InstallSkills("https://github.com/asdfgh1445/ctf-super-hub", []string{"ctf-crypto", "strix-rce", "brainstorming"}, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := slices.Sorted(slices.Values(r.Installed)); !slices.Equal(got, []string{"brainstorming", "ctf-crypto", "strix-rce"}) {
|
||||
t.Errorf("installed %v", r.Installed)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(skillDir("ctf-crypto"), "references", "notes.md")); err != nil {
|
||||
t.Errorf("ctf-crypto's files: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// A name in ASCII goes as it did, its spaces and marks made dashes, so a
|
||||
// skill installed from it before is still the one the library has. A name
|
||||
// in another script with no folder name to fall back on keeps what is left
|
||||
// of it. A skill at the top of a repository goes by the repository's name,
|
||||
// not the temporary folder it was downloaded to.
|
||||
func TestSkillNameFallsBackToFolderOnlyWhenLettersAreLost(t *testing.T) {
|
||||
for _, c := range []struct{ folder, name, want string }{
|
||||
{"my-skill", "My Skill", "My-Skill"},
|
||||
{"pdf", "pdf", "pdf"},
|
||||
{"x", "", "x"},
|
||||
{"ctf-crypto", "CTF•密码学", "ctf-crypto"},
|
||||
{"brainstorming", "头脑风暴", "brainstorming"},
|
||||
{"cafe-notes", "Café notes", "cafe-notes"},
|
||||
{"技能", "头脑风暴", ""},
|
||||
{"技能", "CTF•密码学", "CTF"},
|
||||
} {
|
||||
if got := nameFor(c.folder, meta{Name: c.name}); got != c.want {
|
||||
t.Errorf("nameFor(%s, %q) = %q, want %q", c.folder, c.name, got, c.want)
|
||||
}
|
||||
}
|
||||
for _, c := range []struct {
|
||||
src Source
|
||||
want string
|
||||
}{
|
||||
{Source{Kind: "github", Repo: "asdfgh1445/ctf-super-hub"}, "ctf-super-hub"},
|
||||
{Source{Kind: "git", Repo: "https://gitlab.example.com/group/skills.git"}, "skills"},
|
||||
{Source{Kind: "git", Repo: "git@gitlab.example.com:group/skills.git"}, "skills"},
|
||||
{Source{Kind: "git", Repo: "git@host:skills"}, "skills"},
|
||||
{Source{Kind: "folder", Dir: filepath.FromSlash("/home/me/my-skill")}, "my-skill"},
|
||||
} {
|
||||
if got := srcFolder(c.src); got != c.want {
|
||||
t.Errorf("srcFolder(%+v) = %q, want %q", c.src, got, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A repository that is one skill at its top, named in another script, is
|
||||
// listed under the repository's name.
|
||||
func TestSkillAtTheTopNamedInAnotherScript(t *testing.T) {
|
||||
sandbox(t)
|
||||
fakeSkillRepo(t, map[string]string{"SKILL.md": "---\nname: \"头脑风暴\"\ndescription: d\n---\n"})
|
||||
p, err := ProbeSkills("asdfgh1445/brainstorming")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(p.Candidates) != 1 || p.Candidates[0].Name != "brainstorming" || p.Candidates[0].Path != "" {
|
||||
t.Errorf("candidates %+v, want brainstorming at the top", p.Candidates)
|
||||
}
|
||||
}
|
||||
@@ -285,7 +285,7 @@ func findNew(l *Library, repo, ref string, f *fetcher, limited *atomic.Pointer[e
|
||||
sort.Strings(agents)
|
||||
found = []NewSkill{}
|
||||
from := (&Source{Kind: "github", Repo: repo, Ref: ref, Path: base}).String()
|
||||
for _, c := range candidates(tmp, base) {
|
||||
for _, c := range candidates(tmp, base, srcFolder(Source{Kind: "github", Repo: repo})) {
|
||||
if known[c.Path] || l.skill(c.Name) != nil || checkName("skill", c.Name) != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
+45
-11
@@ -20,6 +20,7 @@ import (
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
"unicode"
|
||||
|
||||
"github.com/yetone/magpie/internal/source"
|
||||
"gopkg.in/yaml.v3"
|
||||
@@ -139,13 +140,37 @@ func parseMeta(b []byte) meta {
|
||||
|
||||
var unsafe = regexp.MustCompile(`[^A-Za-z0-9_-]+`)
|
||||
|
||||
// nameFor is the name a skill found at dir goes by: what its SKILL.md
|
||||
// says, else its folder's, made fit for a file name.
|
||||
func nameFor(dir string, m meta) string {
|
||||
n := m.Name
|
||||
if n == "" {
|
||||
n = filepath.Base(dir)
|
||||
// nameFor is the name a skill in the folder named folder goes by: what its
|
||||
// SKILL.md says, made fit for a file name. A name in another script doesn't
|
||||
// survive that: asdfgh1445/ctf-super-hub's "CTF•密码学" and ten more were
|
||||
// all "CTF", "Strix•RCE 测试" and ten more "Strix", and "头脑风暴" nothing,
|
||||
// so 35 skills were listed as 16 (tnt1223 on Discord). Such a name, or
|
||||
// none, goes by its folder's name instead, which a repository keeps apart;
|
||||
// only a folder whose name is no name either leaves the name as it came
|
||||
// out.
|
||||
func nameFor(folder string, m meta) string {
|
||||
if n := fitName(m.Name); n != "" && !dropsLetters(m.Name) {
|
||||
return n
|
||||
}
|
||||
if n := fitName(folder); n != "" {
|
||||
return n
|
||||
}
|
||||
return fitName(m.Name)
|
||||
}
|
||||
|
||||
// srcFolder is the name of the folder a source's top is: a repository's
|
||||
// own name, not the temporary folder it was downloaded to.
|
||||
func srcFolder(src Source) string {
|
||||
if src.Kind == "folder" {
|
||||
return filepath.Base(src.Dir)
|
||||
}
|
||||
r := strings.TrimSuffix(strings.TrimRight(src.Repo, "/"), ".git")
|
||||
return r[strings.LastIndexAny(r, "/:")+1:]
|
||||
}
|
||||
|
||||
// fitName is a name made fit for a file name: anything but ASCII letters,
|
||||
// digits, _ and - is a -, and it is at most 64 bytes.
|
||||
func fitName(n string) string {
|
||||
n = strings.Trim(unsafe.ReplaceAllString(n, "-"), "-_")
|
||||
if len(n) > 64 {
|
||||
n = n[:64]
|
||||
@@ -153,6 +178,14 @@ func nameFor(dir string, m meta) string {
|
||||
return n
|
||||
}
|
||||
|
||||
// dropsLetters is whether making n fit for a file name loses a letter or
|
||||
// digit of it, not only a space or a mark between words.
|
||||
func dropsLetters(n string) bool {
|
||||
return strings.ContainsFunc(n, func(r rune) bool {
|
||||
return r > unicode.MaxASCII && (unicode.IsLetter(r) || unicode.IsDigit(r))
|
||||
})
|
||||
}
|
||||
|
||||
// ---- linking into agents --------------------------------------------------
|
||||
|
||||
// marker is left in a copy magpie makes where it can't link (Windows
|
||||
@@ -931,17 +964,18 @@ func untar(r io.Reader, dst string) error {
|
||||
// skills folder: some repositories keep their skill only in
|
||||
// .claude/skills (#1964, UFO404). A skill the repository holds twice, in a
|
||||
// hidden folder or a plugin's, is listed once (unhiddenFirst).
|
||||
func candidates(root, sub string) []Candidate {
|
||||
func candidates(root, sub, top string) []Candidate {
|
||||
base := filepath.Join(root, filepath.FromSlash(sub))
|
||||
var out []Candidate
|
||||
var walk func(dir string, depth int)
|
||||
walk = func(dir string, depth int) {
|
||||
if m, ok := readMeta(dir); ok {
|
||||
rel, _ := filepath.Rel(root, dir)
|
||||
folder := filepath.Base(dir)
|
||||
if rel == "." {
|
||||
rel = ""
|
||||
rel, folder = "", top
|
||||
}
|
||||
out = append(out, Candidate{Path: filepath.ToSlash(rel), Name: nameFor(dir, m), Description: m.Description})
|
||||
out = append(out, Candidate{Path: filepath.ToSlash(rel), Name: nameFor(folder, m), Description: m.Description})
|
||||
return
|
||||
}
|
||||
if depth == 0 {
|
||||
@@ -1072,7 +1106,7 @@ func probeSkills(input, name string) (*Probe, error) {
|
||||
return nil, fmt.Errorf("%s isn't a folder", input)
|
||||
}
|
||||
p.Kind, p.root, p.src = "folder", dir, Source{Kind: "folder", Dir: dir}
|
||||
p.Candidates = candidates(dir, "")
|
||||
p.Candidates = candidates(dir, "", filepath.Base(dir))
|
||||
} else if src, ok := githubSource(input); ok || isGit(input) {
|
||||
if !ok {
|
||||
src, _, _ = gitSource(input)
|
||||
@@ -1086,7 +1120,7 @@ func probeSkills(input, name string) (*Probe, error) {
|
||||
os.RemoveAll(tmp)
|
||||
return nil, fmt.Errorf("%s has no folder %s", src.Repo, src.Path)
|
||||
}
|
||||
p.Candidates = candidates(tmp, src.Path)
|
||||
p.Candidates = candidates(tmp, src.Path, srcFolder(src))
|
||||
p.Left = tookLeftOut(tmp, src.Path)
|
||||
key := input
|
||||
if part && name != "" {
|
||||
|
||||
Reference in New Issue
Block a user