library: a project's choices exported from the CLI and applied at another folder (#7278)

jinpwng asked to carry one project's Library choices, with what they
need, to a new path (a new checkout, another machine) from the CLI.

- `magpie library project export <dir> [file|-]` writes ExportProject's
  JSON (kind magpie-project, version 1): the project's skills, servers,
  subagents, hooks and plugins by agent, its Claude Code plugins, Pi
  packages, servers and skills turned off, and its instructions, plus
  need: each hook's and subagent's definition, each skill's and plugin's
  remote source; a plugin's own hooks and subagents go by their plugin.
  No ownership record (Placed, HookRecs, AgentFiles, PiRecs, ClaudeWrote,
  InstrFiles), key, sign-in or other project goes in; a server goes by
  name only, since its settings can hold a key. The file is 0600.
- `apply <file|-> <dir>` (also `import`) makes the folder a project,
  saves a hook or subagent the library here lacks from the export (for no
  agent of its own), and gives each choice through the setters the page
  uses: the agents the folder has for it kept, the export's added, so a
  second apply changes nothing and the folder's own entries stay. A Claude
  plugin or Pi package the folder has already is left as it is;
  instructions only when it has none. What this library lacks (a skill,
  server or plugin) is named with its source and exits 1; nothing is
  fetched.

Run (mote): go test ./internal/library -run 'TestProjectExportApply'
-count=1: ok; the full suite is in the push record.
Real CLI (built binary, two sandbox HOMEs with stub claude and pi on
PATH): in A, project add, hook bark and subagent checker for claude, Pi
npm:pi-demo, export; A deleted; in B (its own "echo mine" Stop hook in
.claude/settings.local.json) apply saved bark and checker and printed
"has what the export has", a second import printed the same and added
nothing; B's settings.local.json held SessionStart "echo bark" beside
"echo mine", .claude/agents/checker.md was written and .pi/settings.json
listed npm:pi-demo.
Broken by hand: with the hook save in ApplyProject skipped,
TestProjectExportApply is red.
Not tried: the reporter's own projects; a Windows path in an export.
This commit is contained in:
yetone
2026-11-20 12:06:44 +08:00
parent bbac3b5a60
commit 06db81530e
5 changed files with 540 additions and 1 deletions
File diff suppressed because one or more lines are too long
+8
View File
@@ -47,6 +47,10 @@ const libraryProjectUsage = `magpie library project [--json] the projects (
magpie's two marker lines, into the project file each agent reads: by default for
the agents the shared instructions are on for, else every agent here that reads one
magpie library project instructions <dir> agents <a,b…|none> the agents whose project file they're written into
magpie library project export <dir> [file] what the project has, and the hooks, subagents and sources that
takes, as JSON (to the file, or printed): no key, sign-in or other project
magpie library project apply <file|-> <dir> give a folder (made a project) what an export has: added to what
it has already, so again changes nothing; what this library lacks is named
<dir> may be relative, or "." for the folder you're in. A skill that couldn't be
placed (a folder there that isn't magpie's is left as it is) is named, and the
command exits with status 1.
@@ -158,6 +162,10 @@ func libraryProjectCmd(args []string) error {
return projectPiCmd(rest, usage)
case "instructions", "instr":
return projectInstrCmd(rest, usage)
case "export":
return projectExportCmd(rest, usage)
case "apply", "import":
return projectApplyCmd(rest, usage)
}
return fmt.Errorf("no library project command %q\n %s", args[0], libraryProjectUsage)
}
+89
View File
@@ -0,0 +1,89 @@
package cli
import (
"encoding/json"
"fmt"
"io"
"os"
"strings"
"github.com/yetone/magpie/internal/library"
)
// projectExportCmd is magpie library project export <dir> [file] (#7278).
func projectExportCmd(rest []string, usage func() error) error {
if len(rest) < 1 || len(rest) > 2 || strings.HasPrefix(rest[0], "-") {
return usage()
}
dir, err := projectDir(rest[0])
if err != nil {
return err
}
e, err := library.ExportProject(dir)
if err != nil {
return err
}
b, err := json.MarshalIndent(e, "", " ")
if err != nil {
return err
}
b = append(b, '\n')
if len(rest) == 1 || rest[1] == "-" {
_, err = os.Stdout.Write(b)
return err
}
if err := os.WriteFile(rest[1], b, 0o600); err != nil {
return err
}
fmt.Println(green.Render("✓"), dir, "→", rest[1])
return nil
}
// projectApplyCmd is magpie library project apply <file|-> <dir>.
func projectApplyCmd(rest []string, usage func() error) error {
if len(rest) != 2 || rest[0] != "-" && strings.HasPrefix(rest[0], "-") || strings.HasPrefix(rest[1], "-") {
return usage()
}
var (
b []byte
err error
)
if rest[0] == "-" {
b, err = io.ReadAll(os.Stdin)
} else {
b, err = os.ReadFile(rest[0])
}
if err != nil {
return err
}
var e library.ProjectExport
if err := json.Unmarshal(b, &e); err != nil {
return fmt.Errorf("%s: %w", rest[0], err)
}
dir, err := library.ProjectPath(rest[1])
if err != nil {
return err
}
out, err := library.ApplyProject(dir, &e)
if err != nil {
return err
}
var problems []library.Problem
for _, r := range out.Res {
problems = append(problems, r.Problems...)
}
for _, s := range out.Saved {
fmt.Println(green.Render("+"), s, muted.Render("saved in the library, for no agent of its own"))
}
if err := projectOutcome(dir, nil, &library.Result{Problems: problems}); err != nil {
return err
}
for _, m := range out.Missing {
fmt.Println(amber.Render("!"), m)
}
if len(out.Missing) > 0 {
return fmt.Errorf("%d of the export's choices left out of %s", len(out.Missing), dir)
}
fmt.Println(green.Render("✓"), dir, "has what the export has")
return nil
}
+333
View File
@@ -0,0 +1,333 @@
package library
import (
"errors"
"fmt"
"maps"
"path/filepath"
"slices"
"strings"
)
// A project's choices carried to another folder, on this computer or
// another (#7278): magpie library project export writes them, apply gives
// them to a folder named there. Only what the project wants travels: the
// library's skills, servers, subagents, hooks and plugins it has and for
// which agents, the Claude Code plugins and Pi packages it turns on, its
// own instructions. What magpie placed and wrote in the first folder (its
// ownership records) stays behind: the second folder's are made there, by
// the same writers the Library page uses. No key, sign-in or other project
// goes with it.
//
// What the choices need from the library goes along too, as far as it can
// without a secret: a hook's and a subagent's definition, a skill's and a
// plugin's source, a server's name only (its settings can hold a key). A
// hook or subagent the other library lacks is saved there from that, given
// to no agent but the project's; anything else it lacks is named, with its
// source, and left out.
// ProjectExportKind is what an export file says it is.
const ProjectExportKind = "magpie-project"
type ProjectExport struct {
Kind string `json:"kind"`
Version int `json:"version"`
// Dir is the folder it was taken from, for the reader alone: apply
// gives the choices to the folder it's told
Dir string `json:"dir,omitempty"`
Copy bool `json:"copy,omitempty"`
Skills map[string][]string `json:"skills,omitempty"`
Servers map[string][]string `json:"servers,omitempty"`
Subagents map[string][]string `json:"subagents,omitempty"`
Hooks map[string][]string `json:"hooks,omitempty"`
Plugins map[string][]string `json:"plugins,omitempty"`
ClaudePlugins []string `json:"claudePlugins,omitempty"`
Pi map[string]string `json:"pi,omitempty"`
ServersOff map[string][]string `json:"serversOff,omitempty"`
SkillsOff map[string][]string `json:"skillsOff,omitempty"`
Instr string `json:"instr,omitempty"`
InstrAgents []string `json:"instrAgents,omitempty"`
Need ExportNeeds `json:"need"`
}
// ExportNeeds are the library's things an export's choices name.
type ExportNeeds struct {
// Skills and Plugins are their sources, "" for one kept only here
Skills map[string]string `json:"skills,omitempty"`
Plugins map[string]string `json:"plugins,omitempty"`
// Bundles are the plugins the hooks and subagents named came with,
// which bring them
Hooks []ExportHook `json:"hooks,omitempty"`
Subagents []ExportSubagent `json:"subagents,omitempty"`
}
// ExportHook is a hook as an export carries it: a plugin's names its
// plugin, which brings it, and nothing else.
type ExportHook struct {
Name string `json:"name"`
Bundle string `json:"bundle,omitempty"`
Description string `json:"description,omitempty"`
Event string `json:"event,omitempty"`
Matcher string `json:"matcher,omitempty"`
Command string `json:"command,omitempty"`
Timeout int `json:"timeout,omitempty"`
}
// ExportSubagent is a subagent as an export carries it.
type ExportSubagent struct {
Name string `json:"name"`
Bundle string `json:"bundle,omitempty"`
Description string `json:"description,omitempty"`
Prompt string `json:"prompt,omitempty"`
Model string `json:"model,omitempty"`
Skills []string `json:"skills,omitempty"`
MCP []string `json:"mcp,omitempty"`
}
// ExportProject is a project's choices and what they need of the library.
func ExportProject(dir string) (*ProjectExport, error) {
l, err := loadLocked()
if err != nil {
return nil, err
}
l.followSubagentFiles()
p := l.project(dir)
if p == nil {
return nil, fmt.Errorf("no project %s", dir)
}
e := &ProjectExport{
Kind: ProjectExportKind, Version: 1, Dir: p.Dir, Copy: p.Copy,
Skills: maps.Clone(p.Skills), Servers: maps.Clone(p.Servers),
Subagents: maps.Clone(p.Subagents), Hooks: maps.Clone(p.Hooks),
Plugins: maps.Clone(p.Plugins), ClaudePlugins: slices.Clone(p.ClaudePlugins),
Pi: maps.Clone(p.Pi), ServersOff: maps.Clone(p.ServersOff), SkillsOff: maps.Clone(p.SkillsOff),
Instr: p.Instr, InstrAgents: slices.Clone(p.InstrAgents),
}
for _, n := range slices.Sorted(maps.Keys(p.Skills)) {
if e.Need.Skills == nil {
e.Need.Skills = map[string]string{}
}
e.Need.Skills[n] = ""
if s := l.skill(n); s != nil && s.Source.remote() {
e.Need.Skills[n] = s.Source.String()
}
}
for _, n := range slices.Sorted(maps.Keys(p.Plugins)) {
if e.Need.Plugins == nil {
e.Need.Plugins = map[string]string{}
}
e.Need.Plugins[n] = ""
if pl := l.plugin(n); pl != nil && !isLocalPath(pl.Source) {
e.Need.Plugins[n] = pl.Source
}
}
for _, n := range slices.Sorted(maps.Keys(p.Hooks)) {
h := l.hook(n)
if h == nil {
continue
}
x := ExportHook{Name: h.Name, Bundle: h.Bundle}
if h.Bundle == "" {
x.Description, x.Event, x.Matcher, x.Command, x.Timeout = h.Description, h.Event, h.Matcher, h.Command, h.Timeout
}
e.Need.Hooks = append(e.Need.Hooks, x)
}
for _, n := range slices.Sorted(maps.Keys(p.Subagents)) {
s := l.subagent(n)
if s == nil {
continue
}
x := ExportSubagent{Name: s.Name, Bundle: s.Bundle}
if s.Bundle == "" {
x.Description, x.Prompt, x.Model = s.Description, s.Prompt, s.Model
x.Skills, x.MCP = slices.Clone(s.Skills), slices.Clone(s.MCP)
}
e.Need.Subagents = append(e.Need.Subagents, x)
}
return e, nil
}
// isLocalPath is whether a source names a folder on this computer, which
// another can't reach.
func isLocalPath(s string) bool {
return s == "" || filepath.IsAbs(s) || strings.HasPrefix(s, ".") || strings.HasPrefix(s, "~") ||
len(s) > 1 && s[1] == ':' // C:\…
}
// ProjectApplied is what ApplyProject did: Saved the hooks and subagents it put
// in the library from the export, Missing what it left out and why, Res
// each write's outcome.
type ProjectApplied struct {
Saved []string
Missing []string
Res []*Result
}
// ApplyProject gives a folder the choices of an export, adding it as a
// project first when it isn't one. A choice the folder has already keeps
// its agents and gains the export's; nothing it has is taken away, so
// applying again changes nothing.
func ApplyProject(dir string, e *ProjectExport) (*ProjectApplied, error) {
if e == nil || e.Kind != ProjectExportKind {
return nil, errors.New("not a project export (magpie library project export writes one)")
}
if e.Version != 1 {
return nil, fmt.Errorf("a project export of version %d, newer than this magpie reads", e.Version)
}
out := &ProjectApplied{}
add := func(res *Result, err error, what string) {
if res != nil {
out.Res = append(out.Res, res)
}
if err != nil {
out.Missing = append(out.Missing, what+": "+err.Error())
}
}
if at, err := ProjectAt(dir); err != nil {
return nil, err
} else if at == "" {
res, err := AddProject(dir)
if err != nil {
return nil, err
}
out.Res = append(out.Res, res)
}
l, err := loadLocked()
if err != nil {
return nil, err
}
// the hooks and subagents the export defines and this library lacks
for _, h := range e.Need.Hooks {
if l.hook(h.Name) != nil || h.Bundle != "" || h.Command == "" {
continue
}
res, err := SaveHook("", Hook{Name: h.Name, Description: h.Description, Event: h.Event, Matcher: h.Matcher, Command: h.Command, Timeout: h.Timeout})
add(res, err, "hook "+h.Name)
if err == nil {
out.Saved = append(out.Saved, "hook "+h.Name)
}
}
for _, s := range e.Need.Subagents {
if l.subagent(s.Name) != nil || s.Bundle != "" || s.Prompt == "" {
continue
}
res, err := SaveSubagent("", Subagent{Name: s.Name, Description: s.Description, Prompt: s.Prompt, Model: s.Model, Skills: s.Skills, MCP: s.MCP, Agents: []string{}})
add(res, err, "subagent "+s.Name)
if err == nil {
out.Saved = append(out.Saved, "subagent "+s.Name)
}
}
if l, err = loadLocked(); err != nil {
return nil, err
}
p := l.project(dir)
if p == nil {
return nil, fmt.Errorf("no project %s", dir)
}
// the export's agents, and those the folder has the thing for already
both := func(have map[string][]string, name string, want []string) []string {
a := slices.Clone(have[name])
for _, id := range want {
a = set(a, id, true)
}
return a
}
lacks := func(kind, name, source string) {
m := fmt.Sprintf("%s %s: not in this library", kind, name)
if source != "" {
m += " (install it from " + source + ")"
}
out.Missing = append(out.Missing, m)
}
if e.Copy && !p.Copy {
res, err := ProjectCopy(dir, true)
add(res, err, "copies")
}
for _, n := range slices.Sorted(maps.Keys(e.Skills)) {
if l.skill(n) == nil {
lacks("skill", n, e.Need.Skills[n])
continue
}
res, err := ProjectSkills(dir, []string{n}, both(p.Skills, n, e.Skills[n]))
add(res, err, "skill "+n)
}
for _, n := range slices.Sorted(maps.Keys(e.Servers)) {
if l.server(n) == nil {
lacks("server", n, "")
continue
}
res, err := ProjectServer(dir, n, both(p.Servers, n, e.Servers[n]))
add(res, err, "server "+n)
}
for _, n := range slices.Sorted(maps.Keys(e.Plugins)) {
if l.plugin(n) == nil {
lacks("plugin", n, e.Need.Plugins[n])
continue
}
res, err := ProjectPlugin(dir, n, both(p.Plugins, n, e.Plugins[n]))
add(res, err, "plugin "+n)
}
bundle := func(kind, n string) string {
for _, h := range e.Need.Hooks {
if kind == "hook" && h.Name == n && h.Bundle != "" {
return " (it comes with the plugin " + h.Bundle + ")"
}
}
for _, s := range e.Need.Subagents {
if kind == "subagent" && s.Name == n && s.Bundle != "" {
return " (it comes with the plugin " + s.Bundle + ")"
}
}
return ""
}
for _, n := range slices.Sorted(maps.Keys(e.Subagents)) {
if l.subagent(n) == nil {
out.Missing = append(out.Missing, "subagent "+n+": not in this library"+bundle("subagent", n))
continue
}
res, err := ProjectSubagents(dir, []string{n}, both(p.Subagents, n, e.Subagents[n]))
add(res, err, "subagent "+n)
}
for _, n := range slices.Sorted(maps.Keys(e.Hooks)) {
if l.hook(n) == nil {
out.Missing = append(out.Missing, "hook "+n+": not in this library"+bundle("hook", n))
continue
}
res, err := ProjectHooks(dir, []string{n}, both(p.Hooks, n, e.Hooks[n]))
add(res, err, "hook "+n)
}
for _, id := range e.ClaudePlugins {
if slices.Contains(p.ClaudePlugins, id) {
continue
}
res, err := ProjectClaudePlugin(dir, id, true)
add(res, err, "Claude Code plugin "+id)
}
for _, src := range slices.Sorted(maps.Keys(e.Pi)) {
if _, ok := p.Pi[src]; ok {
continue // the folder's own say stays
}
res, err := ProjectPi(dir, src, e.Pi[src])
add(res, err, "Pi package "+src)
}
for _, n := range slices.Sorted(maps.Keys(e.ServersOff)) {
if l.server(n) == nil {
continue // nothing here to turn off
}
res, err := ProjectServerOff(dir, n, both(p.ServersOff, n, e.ServersOff[n]))
add(res, err, "server "+n+" off")
}
for _, n := range slices.Sorted(maps.Keys(e.SkillsOff)) {
if l.skill(n) == nil {
continue
}
res, err := ProjectSkillsOff(dir, []string{n}, both(p.SkillsOff, n, e.SkillsOff[n]))
add(res, err, "skill "+n+" off")
}
if e.Instr != "" && p.Instr == "" {
text, agents := e.Instr, slices.Clone(e.InstrAgents)
res, err := ProjectInstructions(dir, ProjectInstr{Text: &text, Agents: &agents})
add(res, err, "instructions")
}
return out, nil
}
+109
View File
@@ -0,0 +1,109 @@
package library
import (
"encoding/json"
"os"
"path/filepath"
"slices"
"strings"
"testing"
)
// A project's choices taken to another folder with a library of its own
// (#7278): its hooks and subagents are saved there from the export, given
// to the new folder by the same writers, beside what the folder has; what
// the other library lacks is named, and applying again changes nothing.
// None of the first folder's ownership records goes along.
func TestProjectExportApply(t *testing.T) {
_, a, _ := pluginProjects(t)
ok(t)(SaveHook("", Hook{Name: "bark", Event: "session-start", Command: "say bark", Agents: []string{}}))
s := reviewer
s.Name, s.Agents = "checker", []string{}
ok(t)(SaveSubagent("", s))
ok(t)(ProjectHooks(a, []string{"bark"}, []string{"claude"}))
ok(t)(ProjectSubagents(a, []string{s.Name}, []string{"claude"}))
ok(t)(ProjectPlugin(a, "acme-kit", []string{"claude"}))
ok(t)(ProjectPi(a, "npm:pi-demo", "on"))
e, err := ExportProject(a)
if err != nil {
t.Fatal(err)
}
b, err := json.Marshal(e)
if err != nil {
t.Fatal(err)
}
for _, own := range []string{"placed", "hookRecs", "agentFiles", "piRecs", "made", "wrote"} {
if strings.Contains(string(b), `"`+own+`"`) {
t.Errorf("the export carries %s: %s", own, b)
}
}
if !strings.Contains(string(b), "say bark") || !strings.Contains(string(b), "You review code.") {
t.Errorf("the hook's and subagent's definitions didn't go: %s", b)
}
// another computer: a library of its own, a folder of its own with
// the user's hook in it, and none of the first folder
h := sandbox(t)
pluginAgents(t, h)
var back ProjectExport
if err := json.Unmarshal(b, &back); err != nil {
t.Fatal(err)
}
c := filepath.Join(t.TempDir(), "c")
gitInit(t, c)
f := filepath.Join(c, ".claude/settings.local.json")
write(t, f, `{"hooks": {"Stop": [{"hooks": [{"type": "command", "command": "echo mine"}]}]}}`)
if err := os.RemoveAll(a); err != nil {
t.Fatal(err)
}
out, err := ApplyProject(c, &back)
if err != nil {
t.Fatal(err)
}
if !slices.Equal(out.Saved, []string{"hook bark", "subagent " + s.Name}) {
t.Errorf("saved %q", out.Saved)
}
if len(out.Missing) != 1 || !strings.HasPrefix(out.Missing[0], "plugin acme-kit") {
t.Errorf("missing %q", out.Missing)
}
for _, r := range out.Res {
if len(r.Problems) > 0 {
t.Errorf("problems: %+v", r.Problems)
}
}
l := loadT(t)
if l.hook("bark") == nil || len(l.hook("bark").Agents) != 0 {
t.Errorf("hook: %+v", l.hook("bark"))
}
if p := l.project(c); p == nil || !slices.Equal(p.Hooks["bark"], []string{"claude"}) || !slices.Equal(p.Subagents[s.Name], []string{"claude"}) {
t.Fatalf("project: %+v", p)
}
got := read(t, f)
if !strings.Contains(got, "say bark") || !strings.Contains(got, "echo mine") {
t.Errorf("settings.local.json:\n%s", got)
}
there(t, filepath.Join(c, ".claude/agents", s.Name+".md"))
if pi := read(t, filepath.Join(c, ".pi/settings.json")); strings.Count(pi, "npm:pi-demo") != 1 {
t.Errorf(".pi/settings.json:\n%s", pi)
}
// again: nothing more
out, err = ApplyProject(c, &back)
if err != nil {
t.Fatal(err)
}
if len(out.Saved) != 0 || len(out.Missing) != 1 {
t.Errorf("again: saved %q, missing %q", out.Saved, out.Missing)
}
if n := strings.Count(read(t, f), "say bark"); n != 1 || !strings.Contains(read(t, f), "echo mine") {
t.Errorf("again, %d entries:\n%s", n, read(t, f))
}
if pi := read(t, filepath.Join(c, ".pi/settings.json")); strings.Count(pi, "npm:pi-demo") != 1 {
t.Errorf("again, .pi/settings.json:\n%s", pi)
}
if _, err := ApplyProject(c, &ProjectExport{Kind: "other"}); err == nil {
t.Error("a file that isn't an export was applied")
}
}