Files
magpie/internal/library/skills_shared_test.go
T
yetone 1851ead1a7 library: Pi and omp lock ~/.agents/skills from their versions; Put skills in links every skill there
usrname1833 on Discord asked for two things.

Pi reads ~/.agents/skills itself, so a skill kept there is Pi's whatever
is ticked, with the lock as Codex and OpenCode have. Pi started to in
0.54.0, not 0.19 (its CHANGELOG; userAgentsSkillsDir in
package-manager.js), and omp in 11.0.0 (src/discovery/agents.ts; 10.6.2
doesn't). Target.SharedToo says so from the version, and every reader of
readsShared goes through Target.readsSharedDir: Always (the lock),
syncSkills' inShared (no second link) and a handover's readsSharedHere.
OmO's senpi reads it in every npm version (0.74.0 up), so omo joins
readsShared. A Pi or omp of no known version is taken as older, as
before.

The second ask is a switch: Library > Skills > "Put skills in": each
agent's own folder (the default, today's behaviour) or ~/.agents/skills
(Library.SharedSkills; POST /api/library/skills/where {how};
magpie library skill where agents|shared). On, every library skill on
here gets magpie's link in ~/.agents/skills (syncShared, after
sweepShared), so every agent and tool reading that folder has it and
shows it locked. The files stay in the library rather than moving, so
updates, edit tracking and Off here keep working; existing skills just
get links when it is turned on, nothing is moved. A user's own entry by
the same name is never written over. Off takes out magpie's links there
and nothing else, and agents' own links come back. Per computer:
Collect doesn't carry it. docs/subsystems/library.md says all this.

Not covered, filed: on Windows without link rights the shared entry is
a marked copy, and takeEdits doesn't take edits made in it (#4958).

Run on mote rebased on d76c4b23a (which has 8abdf53a8):
gofmt -l clean; go vet GOOS=linux and GOOS=windows on internal/library,
internal/gui, internal/cli ok; go test internal/library, internal/gui,
internal/cli ok. GUI, every skill-*, library-* and i18n-keys file in
chromium and webkit: 1302 pass, 4 fail, all in library-all (webkit) and
library-null-agents (chromium). Both pass alone at this tree (52/52), so
they are load reds, filed as #4967. skill-where.test.cjs (new) covers en,
zh, zh-TW, ja, de at 900px and 400px. Before the rebase, cli's
TestEveryRouteHasACommandOrIsNamed was red on /api/usage/checkin-account
(not this change); it is green at this base.

Rebased twice more on origin/main (cli's settings_parity_test, app.js,
i18n.js, settings): internal/cli and internal/gui go test ok, i18n-keys
and skill-where green at the pushed tree.

Broken by hand, each red, then restored: since() always false
(TestPiReadsSharedSkillsFromItsVersion, TestSkillsWhereShared red);
syncShared step dropped (red); sweepShared's keepsShared keep dropped
(red on the SameFile no-relink check); the holder line in wanted
(TestSkillsWhereSharedWithKimi red); skillWhereBar not appended
(skill-where 0/11 per engine).

Not tried: a real Pi, omp or OmO run (versions read from their npm
source); the setting on the Windows box or in WSL; the whole GUI suite.
2026-11-03 05:43:12 +08:00

248 lines
8.3 KiB
Go

package library
import (
"os"
"path/filepath"
"slices"
"strings"
"testing"
"github.com/yetone/magpie/internal/agent"
)
func setOmpVersion(t *testing.T, v string) {
t.Helper()
was := ompVersion
ompVersion = func(*agent.Agent) string { return v }
t.Cleanup(func() { ompVersion = was })
}
func alwaysOf(t *testing.T, name string) []string {
t.Helper()
v, err := Read(nil)
if err != nil {
t.Fatal(err)
}
for _, s := range v.Skills {
if s.Name == name {
return s.Always
}
}
t.Fatalf("no skill %s in the view", name)
return nil
}
// usrname1833: Pi reads ~/.agents/skills itself, from 0.54.0 (its
// CHANGELOG, package-manager.js userAgentsSkillsDir), omp from 11.0.0
// (discovery/agents.ts) and OmO's senpi in every version on npm. A skill
// kept there is theirs whatever is ticked (the lock), and no second link
// is made in their own folders; an older or unknown one is as before.
func TestPiReadsSharedSkillsFromItsVersion(t *testing.T) {
h := sandbox(t)
os.RemoveAll(filepath.Join(h, ".config/goose"))
os.Remove(filepath.Join(h, "bin/goose"))
os.Remove(filepath.Join(h, "bin/goose.exe"))
write(t, filepath.Join(h, ".omp/agent/mcp.json"), "{}")
holdCLI(t, h, "omp")
write(t, filepath.Join(h, ".omo/agent/settings.json"), "{}")
holdCLI(t, h, "omo")
shared := filepath.Join(h, ".agents/skills")
skill(t, filepath.Join(shared, "orch"), "orch", "Orchestrate")
own := map[string]string{
"pi": filepath.Join(h, ".pi/agent/skills/orch"),
"omp": filepath.Join(h, ".omp/agent/skills/orch"),
"omo": filepath.Join(h, ".omo/agent/skills/orch"),
}
setPiVersion(t, "0.53.0")
setOmpVersion(t, "10.6.2")
ok(t)(ImportSkill("orch"))
ok(t)(SkillAgents("orch", []string{"claude", "pi", "omp", "omo"}))
for _, id := range []string{"pi", "omp"} {
if !ours(own[id], "orch") {
t.Errorf("%s before it read ~/.agents/skills hasn't its own link", id)
}
}
if _, err := os.Lstat(own["omo"]); !os.IsNotExist(err) {
t.Errorf("omo reads ~/.agents/skills, yet got a second link: %v", err)
}
a := alwaysOf(t, "orch")
if slices.Contains(a, "pi") || slices.Contains(a, "omp") || !slices.Contains(a, "omo") || !slices.Contains(a, "codex") {
t.Errorf("always before: %v", a)
}
setPiVersion(t, "0.54.0")
setOmpVersion(t, "11.0.0")
ok(t)(Sync())
for _, id := range []string{"pi", "omp"} {
if _, err := os.Lstat(own[id]); !os.IsNotExist(err) {
t.Errorf("%s reads ~/.agents/skills now, yet keeps a second link: %v", id, err)
}
}
a = alwaysOf(t, "orch")
for _, id := range []string{"pi", "omp", "omo", "codex"} {
if !slices.Contains(a, id) {
t.Errorf("%s isn't said to have it always: %v", id, a)
}
}
if slices.Contains(a, "claude") {
t.Errorf("claude reads no ~/.agents/skills: %v", a)
}
// a version not known is taken as an older one, as before
setPiVersion(t, "")
ok(t)(Sync())
if !ours(own["pi"], "orch") || slices.Contains(alwaysOf(t, "orch"), "pi") {
t.Error("a pi of no known version isn't as before")
}
}
// usrname1833: the library's skills can go into ~/.agents/skills too, as
// magpie's links to the library's folders, so every agent and tool that
// reads that folder has every skill. Off (the default) is as before;
// turning it off takes out magpie's links there and nothing else.
func TestSkillsWhereShared(t *testing.T) {
h := sandbox(t)
os.RemoveAll(filepath.Join(h, ".config/goose"))
os.Remove(filepath.Join(h, "bin/goose"))
os.Remove(filepath.Join(h, "bin/goose.exe"))
setPiVersion(t, "0.70.0")
src := filepath.Join(h, "src/skills")
skill(t, filepath.Join(src, "pdf"), "pdf", "Read PDFs")
skill(t, filepath.Join(src, "mine"), "mine", "Mine")
shared := filepath.Join(h, ".agents/skills")
// the user's own skills there: one magpie has no skill by, and one by
// a name the library has, with other files
skill(t, filepath.Join(shared, "theirs"), "theirs", "Theirs")
write(t, filepath.Join(shared, "mine", "SKILL.md"), "---\nname: mine\ndescription: Not the library's\n---\n")
pdf := filepath.Join(shared, "pdf")
pi, cl := filepath.Join(h, ".pi/agent/skills/pdf"), filepath.Join(h, ".claude/skills/pdf")
ok(t)(InstallSkills(src, []string{"pdf"}, []string{"claude", "pi"}))
if _, err := os.Lstat(pdf); !os.IsNotExist(err) {
t.Fatalf("the default put pdf in ~/.agents/skills: %v", err)
}
if !ours(pi, "pdf") {
t.Fatal("pi hasn't pdf")
}
if v, _ := Read(nil); v.SharedSkills || v.SharedDir != shared {
t.Errorf("view: shared %v at %q", v.SharedSkills, v.SharedDir)
}
if _, err := SetSkillsWhere("everywhere"); err == nil {
t.Error("a place it doesn't know was taken")
}
ok(t)(SetSkillsWhere(SkillsInShared))
if !ours(pdf, "pdf") || !linked(pdf) {
t.Fatal("pdf isn't magpie's link in ~/.agents/skills")
}
// a sync leaves the link as it is, not taken out and made again,
// which a reader of the folder would find missing in between
was, _ := os.Lstat(pdf)
ok(t)(Sync())
if now, err := os.Lstat(pdf); err != nil || !os.SameFile(was, now) {
t.Errorf("a sync made the link again: %v", err)
}
// pi reads that folder: no second link; claude doesn't: its stays
if _, err := os.Lstat(pi); !os.IsNotExist(err) {
t.Errorf("pi keeps a second link: %v", err)
}
if !ours(cl, "pdf") {
t.Error("claude lost its link")
}
a := alwaysOf(t, "pdf")
for _, id := range []string{"pi", "codex", "gemini"} {
if !slices.Contains(a, id) {
t.Errorf("%s isn't said to have it always: %v", id, a)
}
}
if slices.Contains(a, "claude") {
t.Errorf("claude: %v", a)
}
if v, _ := Read(nil); !v.SharedSkills {
t.Error("the view doesn't say skills go to ~/.agents/skills")
}
// the user's own are as they were
if _, err := os.Stat(filepath.Join(shared, "theirs", "SKILL.md")); err != nil {
t.Error("the user's own skill went")
}
// a library skill by a name the user has there with other files:
// theirs stays, and the agents ticked for the library's get it in
// their own folders
ok(t)(InstallSkills(src, []string{"mine"}, []string{"claude", "pi"}))
if !ours(filepath.Join(h, ".pi/agent/skills/mine"), "mine") {
t.Error("pi hasn't the library's mine")
}
if ours(filepath.Join(shared, "mine"), "mine") || !strings.Contains(read(t, filepath.Join(shared, "mine", "SKILL.md")), "Not the library's") {
t.Error("the user's own mine was written over")
}
// off on this computer: out of ~/.agents/skills, and pi's own is back
ok(t)(SkillHere("pdf", false))
if _, err := os.Lstat(pdf); !os.IsNotExist(err) {
t.Errorf("a skill off here stays in ~/.agents/skills: %v", err)
}
ok(t)(SkillHere("pdf", true))
if !ours(pdf, "pdf") {
t.Error("on again, pdf isn't back")
}
// a skill unticked everywhere is still in the shared folder
ok(t)(SkillAgents("pdf", nil))
if !ours(pdf, "pdf") {
t.Error("unticked, pdf left ~/.agents/skills")
}
// back to each agent's folder: magpie's links there go, nothing else
ok(t)(SkillAgents("pdf", []string{"claude", "pi"}))
r, err := SetSkillsWhere(SkillsInAgents)
if err != nil {
t.Fatal(err)
}
if len(r.Problems) != 0 {
t.Errorf("problems: %+v", r.Problems)
}
if _, err := os.Lstat(pdf); !os.IsNotExist(err) {
t.Errorf("pdf stays in ~/.agents/skills: %v", err)
}
if !ours(pi, "pdf") {
t.Error("pi hasn't its own link back")
}
if slices.Contains(alwaysOf(t, "pdf"), "pi") {
t.Error("pi still said to have it always")
}
for _, n := range []string{"theirs", "mine"} {
if _, err := os.Stat(filepath.Join(shared, n, "SKILL.md")); err != nil {
t.Errorf("the user's own %s went", n)
}
}
if _, err := os.Stat(filepath.Join(skillDir("pdf"), "SKILL.md")); err != nil {
t.Error("the library's pdf went")
}
}
// A skill ticked for Kimi Code, whose folder ~/.agents/skills is, and then
// unticked for it stays there while the setting keeps it there.
func TestSkillsWhereSharedWithKimi(t *testing.T) {
h := sandbox(t)
write(t, filepath.Join(h, ".kimi/config.toml"), "")
src := filepath.Join(h, "src/skills")
skill(t, filepath.Join(src, "pdf"), "pdf", "Read PDFs")
pdf := filepath.Join(h, ".agents/skills/pdf")
ok(t)(InstallSkills(src, []string{"pdf"}, []string{"kimi"}))
ok(t)(SetSkillsWhere(SkillsInShared))
ok(t)(SkillAgents("pdf", []string{"claude"}))
if !ours(pdf, "pdf") {
t.Fatal("unticked for Kimi Code, pdf left ~/.agents/skills")
}
ok(t)(Sync())
if !ours(pdf, "pdf") {
t.Fatal("a second sync took pdf out")
}
ok(t)(SetSkillsWhere(SkillsInAgents))
if _, err := os.Lstat(pdf); !os.IsNotExist(err) {
t.Errorf("pdf stays: %v", err)
}
}