Like the MCP router, for skills. With the Skills tab's new "Skill router" switch on (off by default), each agent's skills folder gets one skill of magpie's, magpie-skills, in place of a link of each skill ticked for it. Its SKILL.md lists them, each with its description and the path of its SKILL.md, and the agent reads that file when a task fits. An agent then carries one skill's description in its prompt however many the Library gives it. - The entry is a link to <settings dir>/skill-router/<hash of the folder>/, which holds SKILL.md and a hidden .skills/<name> link of each listed skill (hidden, so Codex, OpenCode and Pi don't find a skill twice). Its description stays within 1024 bytes (Codex's limit), cut on a whole character. - Every skill is listed unless it is kept direct (a button on the skill, or `magpie library skill router <name> direct`; carried by a sync), is only for the user to type (disable-model-invocation), is in ~/.agents/skills, is blocked, or the agent has its own by that name. Those keep their own links. - An agent whose folder can't take it keeps every link, and the page says why: given copies, a live agent picking its own, the shared ~/.agents/skills, a Library skill called magpie-skills, or a magpie-skills of the user's (never written over). - Claude Code 2.1.316 asks before reading outside its project, its skills folder too, so magpie adds Read rules for the entry and the folders behind it to permissions.allow, records them, and takes out only its own when they go (as permissions.deny does). - The switch is this computer's alone; `magpie library skill router on|off` sets it from the CLI. - Sessions count a routed read of .skills/<name>/SKILL.md as <name> (Claude Code, Alma, Droid), and calling up magpie-skills itself as no skill, so a use counts once. - docs/subsystems/skill-router.md, with a row in the index. Run at this tree (rebased on origin/main8f74442e4), on mote unless named: - go test -tags nogui ./internal/library ./internal/sessions -count=1: ok - go test -tags nogui ./internal/cli ./internal/gui: ok at bca122bf9; ./internal/gui again at8f74442e4(app.js changed there): ok - node --test of every library-*, *skill*, i18n* and gui-* GUI file (96 files), in the Linux VM (wsl-run): mote's browsers didn't start while another session's simulators ran. 1678 pass, 11 fail atbca122bf9, every red also red on origin/main and filed: library-all WebKit (#4967), skill-where WebKit 400px (#5154), i18n-keys' plugin words (below), and library-own-folder under load, fixed here. The files this change touches, add-partners-shine and i18n-keys again at8f74442e4: 111 pass, i18n-keys the 1 red. - The router's row under the skill list moved four GUI tests' footing, fixed in the tests: remove-all-skills counted it as a skill (now rows of lists other than a hub's, as list-sort does); found-skill-pick-remove and library-pick-remove's reach only wheeled down from above the target, so a Select pushed under the sticky tabs or below the fold was never reached (now toward it, over the list, and the scroll at rest before the click, WebKit's settled a pixel late); library-own-folder's Playwright click scrolled the now taller page under load (now a click where the button is). Each red on the branch before, 22/22 or 14/14 after, twice. - skill-router.test.cjs: 22/22 (chromium and webkit, 1100 and 420px, en zh zh-TW ja de) - go vet -tags nogui, GOOS=linux and GOOS=windows: ok - win-run (xiaomi-gpu) go test ./internal/library: ok but TestGoneAgentListedToTakeOut and TestCheckMixedServerEachSide, red the same at origin/main6b0b7a96eand filed in #4115 (run at that base; nothing in internal/library has changed on main since). The router tests' expected rules were /C:/…; Claude Code's form is //c/… (claudePath), which the code wrote, so the tests now ask claudePath too. - Broken by hand: routable returning true is red in TestSkillRouter (a skill for the user to type is listed); with the routerSkill check out of the sessions reader, TestClaudeRoutedSkillCounted is red. - Real Codex 0.165.0, sandbox HOME, through the live gateway with no sign-in copied: a binary built from this change at6b0b7a96eran `library skill hand-over`, `skill agents secret-word codex` and `skill router on`; `codex exec "What is the project's secret word?"` read magpie-skills/SKILL.md, then .skills/secret-word/SKILL.md at the path given, and answered PELICAN-7319. `router off` put the skill's link back and removed the router's folder. - Real Claude Code 2.1.316, sandbox HOME, through the live gateway on auto/gpt-5.6-luna: that binary wrote the router and two Read rules into settings.json (the user's "model" kept). "Check your skills for one that fits, then greet me." ran Skill magpie-skills, then Read .skills/greet-user/SKILL.md without asking, and answered with the skill's words. Without the rules that Read was refused (tried earlier with hand-written settings). Not tried on the Claude subscription route with this binary: there the live gateway drops a skill's body, which the gateway commit before this one fixes. Known: Alma reads Claude Code's and Codex's folders too, so it may find several magpie-skills (#5722, not tried with a real Alma). i18n-keys is red on 3 keys that aren't this change's, the same on origin/main (#5327, #5332, #5482, #5426).
640 lines
19 KiB
Go
640 lines
19 KiB
Go
package library
|
|
|
|
import (
|
|
"bytes"
|
|
"cmp"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"io/fs"
|
|
"maps"
|
|
"os"
|
|
"path/filepath"
|
|
"slices"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// Bundle is the library as a backup carries it to another computer: the
|
|
// instruction sets and the one on, what each agent gets besides, the MCP
|
|
// servers, the skills with their files and the subagents. What magpie
|
|
// wrote into each agent (Applied, the subagents' files among it) is this
|
|
// computer's own and stays out. It is made the
|
|
// same way every time — no times, everything in order — so two computers
|
|
// with the same library carry the same bundle.
|
|
type Bundle struct {
|
|
Instructions Instructions `json:"instructions"`
|
|
Texts map[string]string `json:"texts,omitempty"` // set → its text
|
|
Extra map[string]string `json:"extra,omitempty"` // agent → what it gets besides
|
|
MCP []*Server `json:"mcp"`
|
|
Skills []*CarriedSkill `json:"skills"`
|
|
Icons map[string]string `json:"icons,omitempty"`
|
|
// Subagents are nil in a bundle from a magpie before they were
|
|
// carried, which knew nothing of this computer's: Put keeps them. Collect
|
|
// always sets it, empty too, so taking the last one out is carried.
|
|
Subagents *[]*Subagent `json:"subagents,omitempty"`
|
|
// Hooks are nil in a bundle from a magpie before they were carried,
|
|
// as Subagents are: Put keeps this computer's. Applied.Hooks, the
|
|
// entries magpie wrote into each agent here, stays out.
|
|
Hooks *[]*Hook `json:"hooks,omitempty"`
|
|
}
|
|
|
|
// CarriedSkill is a skill with its folder's files.
|
|
type CarriedSkill struct {
|
|
Name string `json:"name"`
|
|
Source *Source `json:"source,omitempty"`
|
|
Agents []string `json:"agents"`
|
|
Files map[string][]byte `json:"files"` // by path in the folder, with /
|
|
Exec []string `json:"exec,omitempty"` // the files that run
|
|
Left []string `json:"left,omitempty"` // files too big to carry
|
|
// Called is the name its SKILL.md gives, when that isn't Name
|
|
Called string `json:"called,omitempty"`
|
|
// Note is the user's note on it (Skill.Note)
|
|
Note string `json:"note,omitempty"`
|
|
// Router is Skill.Router: direct, kept out of the skill router
|
|
Router string `json:"router,omitempty"`
|
|
}
|
|
|
|
// A skill's file bigger than maxCarried is left out, and so are the
|
|
// biggest once the skills come to maxCarriedAll. A skill's own files go
|
|
// with it, its scripts and its templates (a PowerPoint template with its
|
|
// pictures is several MB), but a sync reads at most 64 MiB of a backup
|
|
// (davsync), and a skill's bytes are base64 twice in one (the bundle's
|
|
// JSON, then the sealed envelope's): 32 MiB of files is about 57 MiB.
|
|
const (
|
|
maxCarried = 16 << 20
|
|
maxCarriedAll = 32 << 20
|
|
)
|
|
|
|
// Collect is the library as it is now; nil when there is none yet.
|
|
func Collect() (*Bundle, error) {
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
if _, err := os.Stat(path()); errors.Is(err, fs.ErrNotExist) {
|
|
return nil, nil
|
|
} else if err != nil { // there, but unknown: never carried as no library (#2269)
|
|
return nil, err
|
|
}
|
|
l, err := load()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
b := &Bundle{Instructions: l.Instructions, Texts: map[string]string{}, Extra: extras(), MCP: []*Server{}, Skills: []*CarriedSkill{}, Icons: l.Icons}
|
|
b.Instructions.Agents = orNone(b.Instructions.Agents)
|
|
// a set's own file is this computer's: its text goes, where it is
|
|
// doesn't
|
|
b.Instructions.Sets = slices.Clone(b.Instructions.Sets)
|
|
for i := range b.Instructions.Sets {
|
|
b.Instructions.Sets[i].File = ""
|
|
}
|
|
for _, s := range l.sets() {
|
|
if text := setText(l.textPath(s.ID)); text != "" {
|
|
b.Texts[s.ID] = text
|
|
}
|
|
}
|
|
if len(b.Texts) == 0 {
|
|
b.Texts = nil
|
|
}
|
|
if len(b.Extra) == 0 {
|
|
b.Extra = nil
|
|
}
|
|
// a server kept to this computer stays out; another computer's by its
|
|
// name, which a sync brought, goes on as it came (mcp_local.go)
|
|
for _, s := range sorted(l.carriedServers(), func(s *Server) string { return s.Name }) {
|
|
c := *carried(s) // magpie's own by name, each computer's own binary
|
|
c.Agents = orNone(slices.Sorted(slices.Values(s.Agents)))
|
|
b.MCP = append(b.MCP, &c)
|
|
}
|
|
skills := sorted(l.Skills, func(s *Skill) string { return s.Name })
|
|
dirs := []string{}
|
|
for _, s := range skills {
|
|
dirs = append(dirs, realDir(skillDir(s.Name)))
|
|
}
|
|
for i, c := range readSkills(dirs) {
|
|
s := skills[i]
|
|
c.Name, c.Source, c.Agents, c.Called, c.Note, c.Router = s.Name, s.Source, orNone(slices.Sorted(slices.Values(s.Agents))), s.Called, s.Note, s.Router
|
|
b.Skills = append(b.Skills, c)
|
|
}
|
|
subs := []*Subagent{}
|
|
for _, s := range sorted(l.Subagents, func(s *Subagent) string { return s.Name }) {
|
|
c := *s
|
|
c.Skills, c.MCP = slices.Sorted(slices.Values(s.Skills)), slices.Sorted(slices.Values(s.MCP))
|
|
c.Agents = orNone(slices.Sorted(slices.Values(s.Agents)))
|
|
subs = append(subs, &c)
|
|
}
|
|
b.Subagents = &subs
|
|
hooks := []*Hook{}
|
|
for _, h := range sorted(l.Hooks, func(h *Hook) string { return h.Name }) {
|
|
c := *h
|
|
c.Agents = orNone(slices.Sorted(slices.Values(h.Agents)))
|
|
hooks = append(hooks, &c)
|
|
}
|
|
b.Hooks = &hooks
|
|
return b, nil
|
|
}
|
|
|
|
func sorted[T any](xs []T, key func(T) string) []T {
|
|
return slices.SortedFunc(slices.Values(xs), func(a, b T) int { return strings.Compare(key(a), key(b)) })
|
|
}
|
|
|
|
func orNone(xs []string) []string {
|
|
if xs == nil {
|
|
return []string{}
|
|
}
|
|
return xs
|
|
}
|
|
|
|
// readSkills is each skill folder's files. Links in it, .git and what
|
|
// isn't a plain file stay out. What goes is chosen smallest first across
|
|
// every skill, so the text of them all comes before any big file.
|
|
func readSkills(dirs []string) []*CarriedSkill {
|
|
type file struct {
|
|
skill int
|
|
rel, p string
|
|
size int64
|
|
runs bool
|
|
}
|
|
var files []file
|
|
out := make([]*CarriedSkill, len(dirs))
|
|
for i, dir := range dirs {
|
|
out[i] = &CarriedSkill{Files: map[string][]byte{}}
|
|
filepath.WalkDir(dir, func(p string, d fs.DirEntry, err error) error {
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
if d.IsDir() {
|
|
if p != dir && (d.Name() == ".git" || d.Name() == marker) {
|
|
return filepath.SkipDir
|
|
}
|
|
return nil
|
|
}
|
|
if !d.Type().IsRegular() || d.Name() == marker {
|
|
return nil
|
|
}
|
|
rel, err := filepath.Rel(dir, p)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
fi, err := d.Info()
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
files = append(files, file{i, filepath.ToSlash(rel), p, fi.Size(), fi.Mode().Perm()&0o111 != 0})
|
|
return nil
|
|
})
|
|
}
|
|
// smallest first, and the same order for the same files every time
|
|
slices.SortFunc(files, func(a, b file) int {
|
|
return cmp.Or(cmp.Compare(a.size, b.size), cmp.Compare(a.skill, b.skill), strings.Compare(a.rel, b.rel))
|
|
})
|
|
budget := int64(maxCarriedAll)
|
|
for _, f := range files {
|
|
c := out[f.skill]
|
|
if f.size > maxCarried || f.size > budget {
|
|
c.Left = append(c.Left, f.rel)
|
|
continue
|
|
}
|
|
data, err := os.ReadFile(f.p)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
if n := int64(len(data)); n > maxCarried || n > budget { // grew since
|
|
c.Left = append(c.Left, f.rel)
|
|
continue
|
|
}
|
|
budget -= int64(len(data))
|
|
c.Files[f.rel] = data
|
|
if f.runs {
|
|
c.Exec = append(c.Exec, f.rel)
|
|
}
|
|
}
|
|
for _, c := range out {
|
|
slices.Sort(c.Exec)
|
|
slices.Sort(c.Left)
|
|
}
|
|
return out
|
|
}
|
|
|
|
// check refuses a bundle that would write outside the library: a name
|
|
// that isn't one, a file's path that climbs out of its skill.
|
|
func (b *Bundle) check() error {
|
|
for _, s := range b.Instructions.Sets {
|
|
if err := checkName("set", s.ID); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
for id := range b.Texts {
|
|
if id != defaultSet && !slices.ContainsFunc(b.Instructions.Sets, func(s InstrSet) bool { return s.ID == id }) {
|
|
return fmt.Errorf("a text for no set, %q", id)
|
|
}
|
|
}
|
|
for id := range b.Extra {
|
|
if err := checkAgent(id); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
for _, s := range b.MCP {
|
|
if s == nil {
|
|
return fmt.Errorf("an empty server")
|
|
}
|
|
if err := s.check(); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
seen := map[string]bool{}
|
|
for _, s := range b.Skills {
|
|
if s == nil {
|
|
return fmt.Errorf("an empty skill")
|
|
}
|
|
if err := checkName("skill", s.Name); err != nil {
|
|
return err
|
|
}
|
|
if seen[s.Name] {
|
|
return fmt.Errorf("the skill %s twice", s.Name)
|
|
}
|
|
seen[s.Name] = true
|
|
for _, rel := range slices.Concat(slices.Collect(maps.Keys(s.Files)), s.Left) {
|
|
if !localFile(rel) {
|
|
return fmt.Errorf("skill %s: %q is not a file in its folder", s.Name, rel)
|
|
}
|
|
}
|
|
}
|
|
if b.Subagents != nil {
|
|
seen := map[string]bool{}
|
|
for _, s := range *b.Subagents {
|
|
if s == nil {
|
|
return fmt.Errorf("an empty subagent")
|
|
}
|
|
// the name is the file's in every agent's folder; the agents
|
|
// are left as they come, as a skill's are: one a newer magpie
|
|
// gives subagents to is no target here and gets nothing
|
|
if !subagentNameRe.MatchString(s.Name) || slices.Contains(reservedSubagents, s.Name) {
|
|
return fmt.Errorf("the subagent name %q", s.Name)
|
|
}
|
|
if seen[s.Name] {
|
|
return fmt.Errorf("the subagent %s twice", s.Name)
|
|
}
|
|
seen[s.Name] = true
|
|
}
|
|
}
|
|
if b.Hooks != nil {
|
|
seen := map[string]bool{}
|
|
for _, h := range *b.Hooks {
|
|
if h == nil {
|
|
return fmt.Errorf("an empty hook")
|
|
}
|
|
// the event and agents are left as they come: one a newer
|
|
// magpie knows and this one doesn't is a problem on the
|
|
// agent it is given to (syncHooks), not a refused library
|
|
if err := checkName("hook", h.Name); err != nil {
|
|
return err
|
|
}
|
|
if strings.TrimSpace(h.Command) == "" {
|
|
return fmt.Errorf("the hook %s has no command", h.Name)
|
|
}
|
|
if seen[h.Name] {
|
|
return fmt.Errorf("the hook %s twice", h.Name)
|
|
}
|
|
seen[h.Name] = true
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// localFile reports whether rel, with /, names a file inside a folder:
|
|
// not absolute, no .., no \ to be read as a separator elsewhere.
|
|
func localFile(rel string) bool {
|
|
return rel != "" && !strings.ContainsAny(rel, "\\:\x00") && filepath.IsLocal(filepath.FromSlash(rel)) &&
|
|
!slices.Contains(strings.Split(rel, "/"), "..")
|
|
}
|
|
|
|
// WithoutSecrets empties the values of the servers' environment variables
|
|
// and headers that secret says hold one; their names stay, to be filled in.
|
|
func (b *Bundle) WithoutSecrets(secret func(string) bool) {
|
|
for i, s := range b.MCP {
|
|
c := *s
|
|
c.Env, c.Headers = blank(s.Env, secret), blank(s.Headers, secret)
|
|
// an OAuth app's secret is a key too; its client ID isn't
|
|
if s.OAuth != nil && s.OAuth.ClientSecret != "" {
|
|
a := *s.OAuth
|
|
a.ClientSecret = ""
|
|
c.OAuth = &a
|
|
}
|
|
b.MCP[i] = &c
|
|
}
|
|
}
|
|
|
|
func blank(m map[string]string, secret func(string) bool) map[string]string {
|
|
if m == nil {
|
|
return nil
|
|
}
|
|
out := map[string]string{}
|
|
for k, v := range m {
|
|
if secret(k) {
|
|
v = ""
|
|
}
|
|
out[k] = v
|
|
}
|
|
return out
|
|
}
|
|
|
|
// WithSecrets is b with the values WithoutSecrets emptied filled from the
|
|
// servers of the same name in have, so a bundle made without keys doesn't
|
|
// take away the ones kept here.
|
|
func (b *Bundle) WithSecrets(have *Bundle, secret func(string) bool) *Bundle {
|
|
if have == nil {
|
|
return b
|
|
}
|
|
out := *b
|
|
out.MCP = nil
|
|
for _, s := range b.MCP {
|
|
c := *s
|
|
i := slices.IndexFunc(have.MCP, func(h *Server) bool { return h.Name == s.Name })
|
|
if i >= 0 {
|
|
c.Env, c.Headers = filled(s.Env, have.MCP[i].Env, secret), filled(s.Headers, have.MCP[i].Headers, secret)
|
|
// the same app's secret, kept here
|
|
if h := have.MCP[i].OAuth; s.OAuth != nil && s.OAuth.ClientSecret == "" && h != nil && h.ClientID == s.OAuth.ClientID {
|
|
a := *s.OAuth
|
|
a.ClientSecret = h.ClientSecret
|
|
c.OAuth = &a
|
|
}
|
|
}
|
|
out.MCP = append(out.MCP, &c)
|
|
}
|
|
return &out
|
|
}
|
|
|
|
func filled(m, have map[string]string, secret func(string) bool) map[string]string {
|
|
if m == nil {
|
|
return nil
|
|
}
|
|
out := maps.Clone(m)
|
|
for k, v := range m {
|
|
if v == "" && secret(k) && have[k] != "" {
|
|
out[k] = have[k]
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// Put makes the library what b says — its instructions, servers and
|
|
// skills in place of the ones here — and writes it into the agents, which
|
|
// keep whatever of theirs isn't magpie's. The skills are written aside
|
|
// first, and only once all of them are there does any go in; a skill
|
|
// replaced or taken out is kept with the backups, and so is an
|
|
// instructions file that changes. A skill that comes linked to a folder
|
|
// this computer has too is linked to it; else it comes as its files. A
|
|
// skill whose files are as they are here is left alone. The subagents
|
|
// replace the ones here too, unless b is from a magpie that didn't carry
|
|
// them.
|
|
func Put(b *Bundle) (*Result, error) {
|
|
if err := b.check(); err != nil {
|
|
return nil, err
|
|
}
|
|
return change(func(l *Library) error {
|
|
if err := os.MkdirAll(skillsDir(), 0o755); err != nil {
|
|
return err
|
|
}
|
|
stage, err := os.MkdirTemp(Dir(), ".incoming-") // beside skills, to be renamed in
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer os.RemoveAll(stage)
|
|
|
|
keep, link := map[string]bool{}, map[string]string{}
|
|
for _, s := range b.Skills {
|
|
if cur := l.skill(s.Name); cur != nil && sameSource(cur.Source, s.Source) {
|
|
if have := readSkills([]string{realDir(skillDir(s.Name))})[0]; sameFiles(have, s) {
|
|
keep[s.Name] = true
|
|
continue
|
|
}
|
|
}
|
|
if src := s.Source; src != nil && src.Kind == "folder" && src.Dir != "" && isDir(src.Dir) {
|
|
if to, err := os.Readlink(skillDir(s.Name)); err == nil && filepath.Clean(to) == filepath.Clean(src.Dir) {
|
|
keep[s.Name] = true
|
|
} else {
|
|
link[s.Name] = src.Dir
|
|
}
|
|
continue
|
|
}
|
|
if err := writeSkill(filepath.Join(stage, s.Name), s); err != nil {
|
|
return fmt.Errorf("skill %s: %w", s.Name, err)
|
|
}
|
|
if err := keepLeft(filepath.Join(stage, s.Name), realDir(skillDir(s.Name)), s); err != nil {
|
|
return fmt.Errorf("skill %s: %w", s.Name, err)
|
|
}
|
|
}
|
|
|
|
// every skill's files are ready: out with the old ones
|
|
aside := filepath.Join(BackupDir(), time.Now().Format("2006-01-02_15-04-05.000"), "skills")
|
|
out := func(name string) error {
|
|
p := skillDir(name)
|
|
fi, err := os.Lstat(p)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
if linkEntry(fi) {
|
|
return os.Remove(p) // a folder of the user's: only the link goes
|
|
}
|
|
if err := os.MkdirAll(aside, 0o700); err != nil {
|
|
return err
|
|
}
|
|
return move(p, filepath.Join(aside, name))
|
|
}
|
|
for _, s := range l.Skills {
|
|
if !keep[s.Name] {
|
|
if err := out(s.Name); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
skills := []*Skill{}
|
|
for _, s := range b.Skills {
|
|
// turned off here stays off here: that is this computer's own
|
|
off := l.skill(s.Name) != nil && l.skill(s.Name).OffHere
|
|
skills = append(skills, &Skill{Name: s.Name, Source: s.Source, Agents: slices.Clone(orNone(s.Agents)), OffHere: off, Called: s.Called, Note: s.Note, Router: s.Router})
|
|
if keep[s.Name] {
|
|
continue
|
|
}
|
|
if err := out(s.Name); err != nil { // one of no skill's, in the way
|
|
return err
|
|
}
|
|
if dir, ok := link[s.Name]; ok {
|
|
if err := dirLink(dir, skillDir(s.Name)); err != nil {
|
|
if err := copyDir(dir, skillDir(s.Name)); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
continue
|
|
}
|
|
if err := os.Rename(filepath.Join(stage, s.Name), skillDir(s.Name)); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
l.Skills = skills
|
|
|
|
// the instructions: every text here that b hasn't goes, kept first
|
|
kept := newBackups()
|
|
l.kept = kept
|
|
texts := map[string]string{}
|
|
// a set linked here to a file of the user's keeps it: the file is
|
|
// given the text b has for the set, and never emptied for a text
|
|
// b hasn't
|
|
files := map[string]string{}
|
|
for _, s := range l.sets() {
|
|
if s.File != "" {
|
|
files[s.ID] = s.File
|
|
} else {
|
|
texts[setPath(s.ID)] = ""
|
|
}
|
|
}
|
|
for id := range extras() {
|
|
texts[extraPath(id)] = ""
|
|
}
|
|
for id, text := range b.Texts {
|
|
if files[id] == "" {
|
|
texts[setPath(id)] = text
|
|
}
|
|
}
|
|
for id, text := range b.Extra {
|
|
texts[extraPath(id)] = text
|
|
}
|
|
for _, p := range slices.Sorted(maps.Keys(texts)) {
|
|
if readText(p) == strings.TrimSpace(texts[p]) {
|
|
continue
|
|
}
|
|
if err := kept.keep("library", p); err != nil {
|
|
return err
|
|
}
|
|
if err := writeText(p, texts[p]); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
l.Instructions = Instructions{Agents: slices.Sorted(slices.Values(orNone(b.Instructions.Agents))),
|
|
Sets: slices.Clone(b.Instructions.Sets), Active: b.Instructions.Active}
|
|
for i := range l.Instructions.Sets {
|
|
s := &l.Instructions.Sets[i]
|
|
s.File = files[s.ID]
|
|
if text := strings.TrimSpace(b.Texts[s.ID]); s.File != "" && text != "" {
|
|
if err := l.putText(s.File, text); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
|
|
l.putServers(b.MCP)
|
|
l.Icons = maps.Clone(b.Icons)
|
|
// the agents are written on the sync change runs: what magpie
|
|
// wrote here (Applied) decides, file by file, what is its own
|
|
if b.Subagents != nil {
|
|
subs := []*Subagent{}
|
|
for _, s := range *b.Subagents {
|
|
c := *s
|
|
c.Skills, c.MCP, c.Agents = slices.Clone(s.Skills), slices.Clone(s.MCP), slices.Clone(orNone(s.Agents))
|
|
subs = append(subs, &c)
|
|
}
|
|
l.Subagents = subs
|
|
}
|
|
// the same for hooks: Applied.Hooks decides, entry by entry,
|
|
// what magpie takes out of each agent's file here
|
|
if b.Hooks != nil {
|
|
hooks := []*Hook{}
|
|
for _, h := range *b.Hooks {
|
|
c := *h
|
|
c.Agents = slices.Clone(orNone(h.Agents))
|
|
hooks = append(hooks, &c)
|
|
}
|
|
l.Hooks = hooks
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
|
|
func sameSource(a, b *Source) bool {
|
|
return (a == nil) == (b == nil) && (a == nil || *a == *b)
|
|
}
|
|
|
|
// sameFiles reports whether a folder here has what c carries.
|
|
func sameFiles(have, c *CarriedSkill) bool {
|
|
return maps.EqualFunc(have.Files, c.Files, bytes.Equal) && slices.Equal(have.Exec, sorted(c.Exec, func(s string) string { return s }))
|
|
}
|
|
|
|
// writeSkill writes a carried skill's files into a new folder.
|
|
func writeSkill(dir string, s *CarriedSkill) error {
|
|
if err := os.MkdirAll(dir, 0o755); err != nil {
|
|
return err
|
|
}
|
|
for _, rel := range slices.Sorted(maps.Keys(s.Files)) {
|
|
p := filepath.Join(dir, filepath.FromSlash(rel))
|
|
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
|
|
return err
|
|
}
|
|
mode := fs.FileMode(0o644)
|
|
if slices.Contains(s.Exec, rel) {
|
|
mode = 0o755
|
|
}
|
|
if err := os.WriteFile(p, s.Files[rel], mode); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// keepLeft copies into a skill being put (at dir) the files the other
|
|
// computer left out as too big to carry that the skill here has, so it
|
|
// is put with them, not without.
|
|
func keepLeft(dir, here string, s *CarriedSkill) error {
|
|
for _, rel := range s.Left {
|
|
if _, ok := s.Files[rel]; ok {
|
|
continue
|
|
}
|
|
from := filepath.Join(here, filepath.FromSlash(rel))
|
|
fi, err := os.Lstat(from)
|
|
if err != nil || !fi.Mode().IsRegular() {
|
|
continue
|
|
}
|
|
to := filepath.Join(dir, filepath.FromSlash(rel))
|
|
if err := os.MkdirAll(filepath.Dir(to), 0o755); err != nil {
|
|
return err
|
|
}
|
|
in, err := os.Open(from)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
out, err := os.OpenFile(to, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, fi.Mode().Perm())
|
|
if err == nil {
|
|
_, err = io.Copy(out, in)
|
|
err = errors.Join(err, out.Close())
|
|
}
|
|
in.Close()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func isDir(p string) bool {
|
|
fi, err := os.Stat(p)
|
|
return err == nil && fi.IsDir()
|
|
}
|
|
|
|
// Changed is when the library was last changed: library.json, or a file
|
|
// of its own (links not followed).
|
|
func Changed() time.Time {
|
|
var t time.Time
|
|
if fi, err := os.Stat(path()); err == nil {
|
|
t = fi.ModTime()
|
|
}
|
|
filepath.WalkDir(Dir(), func(p string, d fs.DirEntry, err error) error {
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
if fi, err := d.Info(); err == nil && fi.ModTime().After(t) {
|
|
t = fi.ModTime()
|
|
}
|
|
return nil
|
|
})
|
|
return t
|
|
}
|