Files
magpie/internal/library/borrow_test.go
T
yetone 1c0ecec0f6 library: a magpie in Docker keeps skills and prompts for its clients
fenglian980510 on X: in the Docker gateway the Library had MCP and no
Skills. Since f285426ac a page that shows no agent (no agents found, as
in a container, or gateway mode) drew the MCP tab alone. 5a9b5720d then
let another computer's magpie take a remote's skills and prompts (Use its
skills and prompts, GET /v1/magpie/library), but a Docker magpie had no
way to hold any, and anything it held was ticked for no agent, so Borrow
gave it to none on the client. The borrow change didn't re-derive the
agentless page.

- client.js: with no agent shown, the Library has Instructions, MCP
  servers and Skills (AGENTLESS_TABS), with nothing of agents (no chips,
  agents' own skills, projects or Agents list) and a line saying a
  magpie that adds this one as a provider takes them. It opens on MCP
  unless the reader picked one of the three. Strings in zh, zh-TW, ja, de.
- library: Shared says NoAgents when the page shows none (NoAgentsShown,
  set by gui.gatewayMode). Borrow gives such a new skill or set ticked
  for none to the agents here that take skills or instructions and
  aren't hidden (givenHere); one ticked for agents keeps them.
- docs: docker.html en/zh/ja (zh and ja still said there is no Library
  at all), reference.md, library, gui-shell and mcp-router pages; the
  search index rebuilt.

Rebased on origin/main 5cf1d3846 (runs below at 589e4ba6d; the five
commits since touch none of these files but gui-shell.md, which
rebased clean, and docs-index.mjs writeAll left the index unchanged).
The search index was rebuilt through
docs-index.mjs's writeAll after the rebase). What was run:
- At this head, on Linux (WSL magpie-desktop): gofmt -l (0 files),
  GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./...: ok
  (mote and mica were over their load bars).
- At this head, on WSL: go test -tags nogui ./internal/library
  ./internal/gui ./internal/gateway ./internal/provider
  ./internal/testenv -count=1 -timeout 45m: all ok.
- At this head, on WSL: library-agentless-skills, library-mcp-agentless,
  i18n-keys, gateway-mode*, library-* and i18n-* (both engines, every
  language the files run): 1975 of 2010 ok. The 35 reds are in 11
  tests, all red on main too: library-all, library-commands-import,
  library-group-steady, library-mcp-copy-config, library-mcp-off-fold,
  library-open-again (#7097), library-bili (#6015: 82/2 alone, with and
  without this change), gateway-model-chip-height (#7108: red 2 of 3
  alone at main). library-bili-settings and library-mcp-json pass alone
  at both (22/22, 32/32).
- At 998e40c85 (the same change on origin/main 48fffb60c), on mica
  (load 25-35 on 12 cores): go test ./... -timeout 45m, 100 packages ok
  and 3 red, each also red or passing alone at the base 534b85bf9:
  internal/dirent (4 tests, red alone at both, #7031), internal/gui
  TestGatewayModeListsNoOwnSessions (passes alone 3/3, #7064),
  internal/sessions TestWSLStoppedDistroKept (passes alone, #7071).
  gofmt and darwin/linux/windows vet ok there too.
- At 998e40c85, on WSL: 239 GUI files that reach the Library (library-*,
  i18n-*, gateway-mode*, web-page*, any file naming the Library):
  4530 of 4688 ok. The 24 red files rerun at concurrency 2: 151 red at
  998e40c85, 155 at 48fffb60c, the same files (#7097, with #6990,
  #4566, #3922, #6570, #4716 for segs-press-redraw, old-webkit and
  title-bar-tools).
- Not on macOS this round: mote and mica each had another session's full
  GUI suite running all the while.

Broken by hand:
- Borrow's two NoAgents branches set to false:
  TestBorrowFromAMagpieWithNoAgents red ("a skill ticked for none there
  went to [] here, not [claude gemini]").
- origin/main's client.js and i18n.js: library-agentless-skills and
  library-mcp-agentless red at "the tabs", 44 of 44.

Not tried: a real Docker magpie with a second magpie borrowing from it
across two machines. site/search-index.mjs run as a script hangs on its
circular import of docs-index.mjs (as on main); the index was written
through docs-index.mjs's writeAll.
2026-11-18 21:29:39 +08:00

323 lines
9.9 KiB
Go

package library
import (
"encoding/json"
"os"
"path/filepath"
"runtime"
"slices"
"strings"
"testing"
"github.com/yetone/magpie/internal/settings"
)
// a Library as a remote magpie tells it: two skills and two prompt sets
func remoteLibrary(t *testing.T) *Bundle {
t.Helper()
sandbox(t)
_, err := Put(&Bundle{
Instructions: Instructions{Agents: []string{"claude"}, Sets: []InstrSet{{ID: "work", Name: "Work"}}, Active: "work"},
Texts: map[string]string{"default": "Be brief.", "work": "Reply in the user's language."},
MCP: []*Server{},
Skills: []*CarriedSkill{
{Name: "pdf", Agents: []string{"claude"}, Files: map[string][]byte{"SKILL.md": []byte("---\nname: pdf\ndescription: PDFs\n---\n")}},
{Name: "notes", Agents: []string{"claude"}, Files: map[string][]byte{"SKILL.md": []byte("---\nname: notes\ndescription: Notes\n---\n"), "run.sh": []byte("echo 1\n")}, Exec: []string{"run.sh"}},
},
})
if err != nil {
t.Fatal(err)
}
b, err := Shared()
if err != nil {
t.Fatal(err)
}
// what travels is JSON
j, err := json.Marshal(b)
if err != nil {
t.Fatal(err)
}
var out Bundle
if err := json.Unmarshal(j, &out); err != nil {
t.Fatal(err)
}
return &out
}
func borrowedText(t *testing.T, name string) string {
t.Helper()
b, err := os.ReadFile(filepath.Join(skillDir(name), "SKILL.md"))
if err != nil {
return ""
}
return string(b)
}
func names(l *Library) []string {
var out []string
for _, s := range l.Skills {
out = append(out, s.Name)
}
slices.Sort(out)
return out
}
// KoiLx0 on X: a client uses the remote's skills and prompts, and what
// the user has stays theirs.
func TestBorrowRemoteLibrary(t *testing.T) {
remote := remoteLibrary(t)
if remote.MCP == nil || len(remote.MCP) != 0 {
t.Fatalf("the servers stay on their computer: %v", remote.MCP)
}
// the client: a user's own skill of the same name, and their own prompt
h := sandbox(t)
skill(t, filepath.Join(h, "mine/pdf"), "pdf", "My PDFs")
if _, err := InstallSkills(filepath.Join(h, "mine"), []string{"pdf"}, []string{"claude"}); err != nil {
t.Fatal(err)
}
mine := borrowedText(t, "pdf")
if mine == "" {
t.Fatal("the user's skill wasn't installed")
}
// switched off: nothing is taken
if _, err := Borrow("office", "Office", remote); err == nil {
t.Fatal("a Library was taken with the switch off")
}
if _, err := SetBorrow("office", true); err != nil {
t.Fatal(err)
}
r, err := Borrow("office", "Office", remote)
if err != nil {
t.Fatal(err)
}
if !slices.Contains(r.Added, "skill:notes") || !slices.Contains(r.Kept, "skill:pdf") {
t.Fatalf("added %v, kept %v", r.Added, r.Kept)
}
if got := borrowedText(t, "pdf"); got != mine {
t.Fatalf("the user's own pdf was written over: %q", got)
}
if borrowedText(t, "notes") == "" {
t.Fatal("the remote's skill didn't arrive")
}
if fi, err := os.Stat(filepath.Join(skillDir("notes"), "run.sh")); err != nil || fi.Mode().Perm()&0o100 == 0 && runtime.GOOS != "windows" {
t.Fatalf("the script lost its mode: %v %v", fi, err)
}
l := mustLoad(t)
if i := slices.IndexFunc(l.Skills, func(s *Skill) bool { return s.Name == "notes" }); i < 0 || !slices.Contains(l.Skills[i].Agents, "claude") {
t.Fatalf("not given to the agents the remote gives it to: %+v", l.Skills)
}
// the prompts come as sets of their own, and the agents read the first
// since they read nothing
sid := setID("office", "work")
if !l.hasSet(sid) || !l.hasSet(setID("office", "default")) {
t.Fatalf("sets %+v", l.Instructions.Sets)
}
if l.active() != setID("office", "default") && l.active() != sid {
t.Fatalf("active %q", l.active())
}
if setText(l.textPath(sid)) != "Reply in the user's language." {
t.Fatalf("text %q", setText(l.textPath(sid)))
}
// a borrowed set is not passed on by Shared
back, err := Shared()
if err != nil {
t.Fatal(err)
}
for _, s := range back.Skills {
if s.Name == "notes" {
t.Fatal("a borrowed skill was told on")
}
}
for _, s := range back.Instructions.Sets {
if strings.HasPrefix(s.ID, borrowedPrefix) {
t.Fatal("a borrowed set was told on")
}
}
// the remote changes a skill and drops one; the user edited neither
remote.Skills = slices.DeleteFunc(remote.Skills, func(s *CarriedSkill) bool { return s.Name == "pdf" })
for _, s := range remote.Skills {
s.Files["SKILL.md"] = []byte("---\nname: notes\ndescription: Notes v2\n---\n")
}
if _, err := Borrow("office", "Office", remote); err != nil {
t.Fatal(err)
}
if !strings.Contains(borrowedText(t, "notes"), "v2") {
t.Fatalf("not updated: %q", borrowedText(t, "notes"))
}
// the user edits it: from then on it is theirs
edited := "---\nname: notes\ndescription: My notes\n---\n"
if err := os.WriteFile(filepath.Join(skillDir("notes"), "SKILL.md"), []byte(edited), 0o644); err != nil {
t.Fatal(err)
}
for _, s := range remote.Skills {
s.Files["SKILL.md"] = []byte("---\nname: notes\ndescription: Notes v3\n---\n")
}
r, err = Borrow("office", "Office", remote)
if err != nil {
t.Fatal(err)
}
if borrowedText(t, "notes") != edited || !slices.Contains(r.Kept, "skill:notes") {
t.Fatalf("the user's edit was written over: %q %v", borrowedText(t, "notes"), r.Kept)
}
// the remote drops it: the user's edit stays
remote.Skills = nil
if _, err := Borrow("office", "Office", remote); err != nil {
t.Fatal(err)
}
if borrowedText(t, "notes") != edited {
t.Fatal("a skill the user changed was taken out")
}
// the user's own pdf and prompt are never touched by turning it off
if _, err := SetBorrow("office", false); err != nil {
t.Fatal(err)
}
l = mustLoad(t)
if borrowedText(t, "pdf") != mine || !slices.Contains(names(l), "pdf") || !slices.Contains(names(l), "notes") {
t.Fatalf("turning it off took the user's: %v", names(l))
}
if l.hasSet(sid) {
t.Fatalf("its sets stayed: %+v", l.Instructions.Sets)
}
}
// A skill taken and not changed goes when the remote drops it or the
// switch goes off; one the user removed is not brought back.
func TestBorrowedSkillGoesWithTheRemote(t *testing.T) {
remote := remoteLibrary(t)
sandbox(t)
if _, err := SetBorrow("office", true); err != nil {
t.Fatal(err)
}
if _, err := Borrow("office", "Office", remote); err != nil {
t.Fatal(err)
}
if got := names(mustLoad(t)); !slices.Equal(got, []string{"notes", "pdf"}) {
t.Fatalf("%v", got)
}
if _, err := RemoveSkill("pdf"); err != nil {
t.Fatal(err)
}
if _, err := Borrow("office", "Office", remote); err != nil {
t.Fatal(err)
}
if got := names(mustLoad(t)); !slices.Equal(got, []string{"notes"}) {
t.Fatalf("a skill the user removed came back: %v", got)
}
if _, err := SetBorrow("office", false); err != nil {
t.Fatal(err)
}
if got := names(mustLoad(t)); len(got) != 0 {
t.Fatalf("left after switching off: %v", got)
}
}
// fenglian980510 on X: a magpie in Docker shows no agent, so its skills and
// prompts are ticked for none. A magpie that uses its Library gives them
// to its own agents, as it would a new one; one the remote ticked keeps
// its ticks.
func TestBorrowFromAMagpieWithNoAgents(t *testing.T) {
sandbox(t)
NoAgentsShown = func() bool { return true }
t.Cleanup(func() { NoAgentsShown = nil })
if _, err := Put(&Bundle{
Instructions: Instructions{Agents: []string{}},
Texts: map[string]string{"default": "Be brief."},
MCP: []*Server{},
Skills: []*CarriedSkill{
{Name: "pdf", Agents: []string{}, Files: map[string][]byte{"SKILL.md": []byte("---\nname: pdf\ndescription: PDFs\n---\n")}},
{Name: "notes", Agents: []string{"codex"}, Files: map[string][]byte{"SKILL.md": []byte("---\nname: notes\ndescription: Notes\n---\n")}},
},
}); err != nil {
t.Fatal(err)
}
b, err := Shared()
if err != nil {
t.Fatal(err)
}
j, err := json.Marshal(b)
if err != nil {
t.Fatal(err)
}
var remote Bundle
if err := json.Unmarshal(j, &remote); err != nil {
t.Fatal(err)
}
if !remote.NoAgents {
t.Fatal("a magpie showing no agent didn't say so")
}
NoAgentsShown = nil
// the client: its agents are found, one of them hidden on its Agents page
sandbox(t)
here := []string{"claude", "gemini"}
was := givenHere
givenHere = func(string) []string { return here }
t.Cleanup(func() { givenHere = was })
if _, err := SetBorrow("nas", true); err != nil {
t.Fatal(err)
}
if _, err := Borrow("nas", "NAS", &remote); err != nil {
t.Fatal(err)
}
l := mustLoad(t)
agents := map[string][]string{}
for _, s := range l.Skills {
agents[s.Name] = s.Agents
}
if !slices.Equal(agents["pdf"], here) {
t.Fatalf("a skill ticked for none there went to %v here, not %v", agents["pdf"], here)
}
if !slices.Equal(agents["notes"], []string{"codex"}) {
t.Fatalf("a skill the remote ticked lost its ticks: %v", agents["notes"])
}
if !slices.Equal(l.Instructions.Agents, here) {
t.Fatalf("the prompts went to %v, not %v", l.Instructions.Agents, here)
}
}
// A magpie with agents here says nothing of the kind: what it ticked for
// none stays with none.
func TestBorrowFromAMagpieWithAgentsKeepsNone(t *testing.T) {
remote := remoteLibrary(t)
if remote.NoAgents {
t.Fatal("a magpie with agents said it shows none")
}
remote.Skills[0].Agents = []string{}
sandbox(t)
if _, err := SetBorrow("office", true); err != nil {
t.Fatal(err)
}
if _, err := Borrow("office", "Office", remote); err != nil {
t.Fatal(err)
}
for _, s := range mustLoad(t).Skills {
if s.Name == remote.Skills[0].Name && len(s.Agents) != 0 {
t.Fatalf("ticked for none there, given to %v here", s.Agents)
}
}
}
// givenHere is the agents found here with a skills folder, less those the
// Agents page hides.
func TestGivenHereLeavesOutHiddenAgents(t *testing.T) {
sandbox(t)
all := givenHere("skills")
if !slices.Contains(all, "gemini") {
t.Fatalf("gemini, found by its binary, isn't one: %v", all)
}
s := settings.Load()
s.AgentsHidden = []string{"gemini"}
if err := settings.Save(s); err != nil {
t.Fatal(err)
}
if got := givenHere("skills"); slices.Contains(got, "gemini") {
t.Fatalf("a hidden agent is given new skills: %v", got)
}
}