JonahFu asked on X (tweet 2123236872940073039) for each subagent's local path in Library > Subagents, to edit it with an LLM, or to keep a subagent in a git repository. Each subagent of the library (not a plugin's) is now kept on this computer in <library>/subagents/<name>.md as well, in Claude Code's shape (name, description, model, skills, mcpServers, the prompt), or in a file of the user's own set under the editor's Advanced (folded unless one is set), in a repository say: - FileHash is what the file said when magpie last wrote or took it in. An edit there (by hand, by an agent, a git pull) is taken in at the next sync and before a change made on the page, and every ticked agent gets it. While magpie runs, KeepLinked's loop syncs when a file's hash differs, once per edit. - An edit that can't be read as a subagent (no front matter, another name, no prompt) is left as it is, the agents keep what it said before, and the editor says why. magpie writes a file only when it is missing, or still as magpie left it; never the user's edit. - A file of the user's own that is there already is what the subagent says (a bad one refuses the save). It must be an absolute .md path, not a folder, not inside the library, not one another subagent uses, and not a file magpie writes for an agent. - Rename and remove take the library's own file only while it is as magpie wrote it; the user's own file always stays. - File and FileHash are this computer's: Collect leaves them out, Put keeps this computer's. The editor shows the path (click reveals it, Copy copies it), a line that edits there reach the agents, and the error when one wasn't taken in. i18n in zh, zh-TW, ja and de. docs/subsystems/library.md: a row for the subagent's own file and its tests. Run at the tree before2051dfc23(a Pi market change in pimarket.go and plugin/market.go only) came onto origin/main: - mote-run: gofmt -l none; GOOS=linux and GOOS=windows go vet -tags nogui ./... and go vet ./...: ok. go test -timeout 45m ./...: every package ok, internal/library 504s, except known reds not touched here: internal/cli TestHealthcheckFindsTheRunningGatewayWithoutMagpieAddr, TestUpdateRestartsTheGatewayItFindsWithoutMagpieAddr and TestUpdateFindsAMagpieThatWroteNoRecord (#6471, #6223: a real gateway on mote's 127.0.0.1:3425), internal/gateway TestOpenCodeGoOwnFreeModelGetsNothingOfOpenCodes (#7117). mote and mica were both over their cores; the suite went to mote. - mica-run: node --test library-subagents, library-rules, library-commands, i18n-keys, i18n-en-no-cjk (Chromium and WebKit, 420 and 1100 wide, en zh zh-TW ja de): 44 of 44. Before that, every library-*, lib-*, nav-* and i18n* file (95 files, 2071 tests): red only in library-commands (its tab-order assert, fixed in the previous commit) and library-open-again's 1100px cases, red the same alone at base14b6b9555(#6751). - Red on the old code: TestSubagentFileFollowed and TestSubagentFileUnreadable went red with the follow step skipped and the hash check dropped; library-subagents.test.cjs is red against the previous commit's client.js. - Real use: a sandbox HOME magpie on mica (fake claude on PATH) wrote ~/.config/magpie/library/subagents/reviewer.md; an edit there with a new description and prompt reached ~/.claude/agents/reviewer.md and library.json on the next `library sync`; an edit with no front matter was left as written and Claude Code's file kept the old text. - After rebasing on2051dfc23, mica-run: go vet ./internal/library ./internal/plugin and go test -timeout 45m ./internal/library: ok. Not tried: a real Claude Code or other agent editing the file, a real git pull into a repository file while magpie runs (KeepLinked's loop is covered by TestSubagentOwnFile only), Windows and Linux.
203 lines
7.9 KiB
Go
203 lines
7.9 KiB
Go
package library
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func subagentViewOf(t *testing.T, name string) SubagentView {
|
|
t.Helper()
|
|
v, err := Read(nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, s := range v.Subagents {
|
|
if s.Name == name {
|
|
return s
|
|
}
|
|
}
|
|
t.Fatalf("no %s in the view", name)
|
|
return SubagentView{}
|
|
}
|
|
|
|
// Each subagent is kept in a file of its own on this computer: an edit
|
|
// made there, by hand or by an agent, reaches the library and every agent
|
|
// ticked for it; one made on the page is written there.
|
|
func TestSubagentFileFollowed(t *testing.T) {
|
|
h := sandbox(t)
|
|
claude := filepath.Join(h, ".claude/agents/reviewer.md")
|
|
s := Subagent{Name: "reviewer", Description: "Reviews a diff.", Prompt: "You review code.", Agents: []string{"claude"}}
|
|
ok(t)(SaveSubagent("", s))
|
|
f := filepath.Join(Dir(), "subagents/reviewer.md")
|
|
if got := read(t, f); got != "---\nname: \"reviewer\"\ndescription: \"Reviews a diff.\"\n---\n\nYou review code.\n" {
|
|
t.Fatalf("its file: %q", got)
|
|
}
|
|
if v := subagentViewOf(t, "reviewer"); v.Path != f || v.FileError != "" || v.FileHash != "" {
|
|
t.Errorf("view: path %q error %q hash %q", v.Path, v.FileError, v.FileHash)
|
|
}
|
|
|
|
// edited there, as an agent would: the library and Claude Code follow
|
|
write(t, f, "---\nname: reviewer\ndescription: Reviews a diff for bugs.\nmodel: gpt-5.5\nskills: tdd, lint\n---\n\nYou review code.\n\nReport bugs only.\n")
|
|
ok(t)(Sync())
|
|
l, _ := load()
|
|
got := l.subagent("reviewer")
|
|
if got.Description != "Reviews a diff for bugs." || got.Prompt != "You review code.\n\nReport bugs only." || got.Model != "gpt-5.5" || strings.Join(got.Skills, ",") != "lint,tdd" {
|
|
t.Errorf("the library after the edit: %+v", got)
|
|
}
|
|
if c := read(t, claude); !strings.Contains(c, "Report bugs only.") || !strings.Contains(c, "Reviews a diff for bugs.") {
|
|
t.Errorf("claude's after the edit: %q", c)
|
|
}
|
|
// the user's own words stay as they wrote them
|
|
if !strings.Contains(read(t, f), "skills: tdd, lint") {
|
|
t.Error("the edited file was written over")
|
|
}
|
|
|
|
// a change on the page is written there
|
|
s.Prompt = "Be brief."
|
|
ok(t)(SaveSubagent("reviewer", s))
|
|
if got := read(t, f); !strings.HasSuffix(got, "---\n\nBe brief.\n") {
|
|
t.Errorf("its file after a save: %q", got)
|
|
}
|
|
|
|
// renamed: the old file goes with the old name
|
|
s.Name = "checker"
|
|
ok(t)(SaveSubagent("reviewer", s))
|
|
if _, err := os.Stat(f); !os.IsNotExist(err) {
|
|
t.Errorf("reviewer.md after the rename: %v", err)
|
|
}
|
|
g := filepath.Join(Dir(), "subagents/checker.md")
|
|
if !strings.Contains(read(t, g), "name: \"checker\"") {
|
|
t.Errorf("checker.md: %q", read(t, g))
|
|
}
|
|
ok(t)(RemoveSubagents([]string{"checker"}))
|
|
if _, err := os.Stat(g); !os.IsNotExist(err) {
|
|
t.Errorf("checker.md after remove: %v", err)
|
|
}
|
|
}
|
|
|
|
// An edit that can't be read as a subagent is left as it is, the agents
|
|
// keep what it said before, and the page says why; once it reads again,
|
|
// it is taken in.
|
|
func TestSubagentFileUnreadable(t *testing.T) {
|
|
h := sandbox(t)
|
|
claude := filepath.Join(h, ".claude/agents/reviewer.md")
|
|
ok(t)(SaveSubagent("", Subagent{Name: "reviewer", Description: "Reviews.", Prompt: "Review.", Agents: []string{"claude"}}))
|
|
f := filepath.Join(Dir(), "subagents/reviewer.md")
|
|
before := read(t, claude)
|
|
for _, bad := range []string{
|
|
"Just a prompt, no front matter.\n",
|
|
"---\nname: other\ndescription: x\n---\n\nhi\n",
|
|
"---\ndescription: Reviews.\n---\n\n \n",
|
|
} {
|
|
write(t, f, bad)
|
|
ok(t)(Sync())
|
|
if read(t, f) != bad {
|
|
t.Errorf("%q was written over", bad)
|
|
}
|
|
if read(t, claude) != before {
|
|
t.Errorf("claude's changed on %q", bad)
|
|
}
|
|
if v := subagentViewOf(t, "reviewer"); !strings.Contains(v.FileError, f) || v.Prompt != "Review." {
|
|
t.Errorf("view on %q: %q %q", bad, v.FileError, v.Prompt)
|
|
}
|
|
// a change on the page meanwhile leaves the user's text too
|
|
ok(t)(SubagentAgentsSet("reviewer", []string{"claude"}))
|
|
if read(t, f) != bad {
|
|
t.Errorf("%q was written over by a change", bad)
|
|
}
|
|
}
|
|
write(t, f, "---\ndescription: Reviews.\n---\n\nReview twice.\n")
|
|
ok(t)(Sync())
|
|
if !strings.Contains(read(t, claude), "Review twice.") || subagentViewOf(t, "reviewer").FileError != "" {
|
|
t.Errorf("not taken once fixed: %q", read(t, claude))
|
|
}
|
|
// a file the user deleted comes back
|
|
os.Remove(f)
|
|
ok(t)(Sync())
|
|
if !strings.Contains(read(t, f), "Review twice.") {
|
|
t.Errorf("not written again: %q", read(t, f))
|
|
}
|
|
// one edited and not taken in stays when the subagent goes
|
|
write(t, f, "mine")
|
|
ok(t)(RemoveSubagents([]string{"reviewer"}))
|
|
if read(t, f) != "mine" {
|
|
t.Error("an edited file went with the subagent")
|
|
}
|
|
}
|
|
|
|
// Kept in a file of the user's own, in a repository say: one there
|
|
// already is what the subagent says; one not there yet is written. It
|
|
// stays when the subagent goes.
|
|
func TestSubagentOwnFile(t *testing.T) {
|
|
h := sandbox(t)
|
|
claude := filepath.Join(h, ".claude/agents/reviewer.md")
|
|
mine := filepath.Join(h, "repo/agents/reviewer.md")
|
|
write(t, mine, "---\nname: reviewer\ndescription: From the repo.\n---\n\nThe repo's prompt.\n")
|
|
s := Subagent{Name: "reviewer", Description: "Typed.", Prompt: "Typed.", Agents: []string{"claude"}, File: "~/repo/agents/reviewer.md"}
|
|
ok(t)(SaveSubagent("", s))
|
|
if c := read(t, claude); !strings.Contains(c, "The repo's prompt.") || !strings.Contains(c, "From the repo.") {
|
|
t.Errorf("claude's: %q", c)
|
|
}
|
|
if _, err := os.Stat(filepath.Join(Dir(), "subagents/reviewer.md")); !os.IsNotExist(err) {
|
|
t.Errorf("the library's own file too: %v", err)
|
|
}
|
|
if v := subagentViewOf(t, "reviewer"); v.Path != mine || v.File != mine {
|
|
t.Errorf("view: %q %q", v.Path, v.File)
|
|
}
|
|
// a git pull, as keepSubagentFiles sees it while magpie runs
|
|
write(t, mine, "---\nname: reviewer\ndescription: From the repo.\n---\n\nPulled.\n")
|
|
keepSubagentFiles()
|
|
if !strings.Contains(read(t, claude), "Pulled.") {
|
|
t.Errorf("claude's after a pull: %q", read(t, claude))
|
|
}
|
|
// one not there yet is written
|
|
other := filepath.Join(h, "repo/agents/new.md")
|
|
ok(t)(SaveSubagent("", Subagent{Name: "new", Description: "New.", Prompt: "New.", Agents: []string{}, File: other}))
|
|
if !strings.Contains(read(t, other), "New.") {
|
|
t.Errorf("new.md: %q", read(t, other))
|
|
}
|
|
ok(t)(RemoveSubagents([]string{"reviewer", "new"}))
|
|
if !strings.Contains(read(t, mine), "Pulled.") || read(t, other) == "" {
|
|
t.Error("removing took the user's own files")
|
|
}
|
|
|
|
for _, bad := range []string{"repo/x.md", filepath.Join(h, "repo/x.txt"), filepath.Join(Dir(), "x.md"), filepath.Join(h, "repo")} {
|
|
if _, err := SaveSubagent("", Subagent{Name: "bad", Description: "x", Prompt: "x", File: bad}); err == nil {
|
|
t.Errorf("kept in %s", bad)
|
|
}
|
|
}
|
|
ok(t)(SaveSubagent("", Subagent{Name: "given", Description: "x", Prompt: "x", Agents: []string{"claude"}}))
|
|
if _, err := SaveSubagent("", Subagent{Name: "bad", Description: "x", Prompt: "x", File: filepath.Join(h, ".claude/agents/given.md")}); err == nil {
|
|
t.Error("kept in a file magpie writes for Claude Code")
|
|
}
|
|
write(t, filepath.Join(h, "repo/broken.md"), "no front matter")
|
|
if _, err := SaveSubagent("", Subagent{Name: "broken", Description: "x", Prompt: "x", File: filepath.Join(h, "repo/broken.md")}); err == nil {
|
|
t.Error("kept in a file that isn't a subagent")
|
|
}
|
|
}
|
|
|
|
// The file is this computer's: a bundle carries none, and Put keeps the
|
|
// file each subagent here is kept in.
|
|
func TestSubagentFileNotCarried(t *testing.T) {
|
|
h := sandbox(t)
|
|
mine := filepath.Join(h, "repo/reviewer.md")
|
|
ok(t)(SaveSubagent("", Subagent{Name: "reviewer", Description: "Reviews.", Prompt: "Review.", Agents: []string{}, File: mine}))
|
|
b, err := Collect()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if c := (*b.Subagents)[0]; c.File != "" || c.FileHash != "" {
|
|
t.Fatalf("carried: %q %q", c.File, c.FileHash)
|
|
}
|
|
(*b.Subagents)[0].Prompt = "From the other computer."
|
|
if _, err := Put(b); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
l, _ := load()
|
|
if s := l.subagent("reviewer"); s.File != mine || !strings.Contains(read(t, mine), "From the other computer.") {
|
|
t.Errorf("after put: %+v, file %q", s, read(t, mine))
|
|
}
|
|
}
|