diff --git a/docs/subsystems/library.md b/docs/subsystems/library.md index 748b970fe..37cc05c79 100644 --- a/docs/subsystems/library.md +++ b/docs/subsystems/library.md @@ -84,6 +84,7 @@ Pi's extensions and packages. - Discover's featured list is the same with nothing set up: one of magpie's own whose model or search isn't there yet, or which Settings turned off, carries `unready` (`gateway.OwnUnready`, set by the GUI's `/api/library/market/servers`), shown as a Not set up yet badge on its card and the full reason in its sheet. It can still be added, since it answers once that is set up, and until then its tools answer with the same reason, so no agent is given a tool that fails without a word. - A carried library (`Collect`) has every file of each skill, its templates and other binaries included, up to `maxCarried` (16 MiB) a file and `maxCarriedAll` (32 MiB) in all, chosen smallest first across the skills (`readSkills`), so every skill's text goes before any big file. The total keeps a sealed backup under the 64 MiB a sync reads: the files are base64 twice in it. What is left out is named in `Left`, and `Put` keeps this computer's copy of such a file in the skill it puts (`keepLeft`). [`carry.go`](../../internal/library/carry.go) - A skill can be turned off on this computer alone (`OffHere`, `SkillHere`; the row's power button, `magpie library skill off-here ` and `on-here`; hanslin1666 on Discord). No agent here gets it, whatever its `Agents` say (`gives`, which every reader of `Agents` for giving goes through: `syncSkills`, `desktop_skills.go`, `skill_changed.go`, `skill_edits.go`, `behind`, `Targets`). `Agents` stays what a sync carries, and the page leaves its chips as they are, faded. `Collect` never carries `OffHere`, so turning it off changes no part a sync uploads, and `Put` keeps this computer's `OffHere` for a skill it puts, so a sync from a computer that has it on, or gives it to more agents, never turns it on here. A library.json from before has no `offHere` and every skill stays on. Not covered: a project's skills; a library skill kept in `~/.agents/skills`, which agents read in place, until it is handed over (below); and a skill another computer removes, which takes the flag with it. [`skills.go`](../../internal/library/skills.go), [`carry.go`](../../internal/library/carry.go) +- A skill's group (its repository, `tracer.repoOf`) is traced from files that are one computer's own: the skills CLI's lock, a git checkout, CC Switch's record. `Collect` carries the result as `CarriedSkill.From` for a skill with no source of its own (ConatusMoon2222 on X: skills grouped by library on one computer sat in "this computer" on the next after a sync, and Group again had nothing to start from), and `Put` writes it as `Skill.From`. A bundle from before has none: `Put` then keeps the `From` this computer found for the same skill (a Group again, or an earlier sync), and `Skill.Gone` for one with the same source, which are this computer's. Group again fetches each GitHub repository some skill is traced to, so skills synced before still need one skill of the pack that carries its source (or an updated computer's next sync); a pack with no skill traced to it here has nothing to start from. Not covered: `Put` still writes a skill with a bundle's source, so a copy Group again bound to its repository here is unbound by a sync from a computer that has it unbound (`TestSyncedSkillKeepsItsRepo`, `TestSyncKeepsRepoFoundHere`, `TestRegroupAfterOldSync`). [`carry.go`](../../internal/library/carry.go), [`skill_from.go`](../../internal/library/skill_from.go) - An MCP server can be kept to this computer (`Server.Local`; the editor's "Sync to your other computers" switch, off for a local one (it said "This computer only", which bin.90 on Discord read as sharing), the row tagged "Not synced", `magpie library mcp add --local`, `magpie library mcp local on|off`; huhuy72219405 on X). `Collect` never carries it (`carriedServers`), so a sync or a backup file never takes it to another computer, and turning a shared server local takes it out of what is carried: the other computers drop it at their next sync. `Put` (sync, join, Restore, Undo, a backup file) keeps every local server here, with its agents, and clears `Local` on what comes in (`putServers`). Another computer's server by the name of a local one doesn't take its place: it goes to `Library.Away`, given to no agent, not in the router, not checked, and carried on as it came, so a push from here never takes it from the computers that have it. `settleAway` runs on every change: while the name is local here the away copy stays; once no server of that name is left here (removed or renamed) it comes in as a sync would bring it; once a shared server has the name (local turned off) this computer's is the one carried. The router serves local servers like any other, since it runs on this computer. A carried subagent names its servers, a local one too; a computer without that server leaves it out of the agents' files, as any server the library hasn't. The TUI keeps the flag on edit and marks the row. (`TestLocalServerNotCarried`, `TestPutKeepsLocalServer`, `TestPutSameNameAsLocal`, `TestRestoreKeepsLocalServer`, `TestLocalServerThroughSync`, `mcp-local-server.test.cjs`) [`mcp_local.go`](../../internal/library/mcp_local.go), [`carry.go`](../../internal/library/carry.go) - The CLI sets a server's headers, env and startup timeout (#6111, Abandon332 on X): `magpie library mcp add header=K=V env=K=V timeout=SECS agents=…`, `mcp set header=K=V|header-=K|env=K=V|env-=K|timeout=SECS` (the rest of the server kept) and `mcp show ` (JSON). `SplitServerWords` takes those words out of the line before `--`; words after `--` are the program's own, and a word that looks like a setting magpie doesn't know (`headers=`, `envs=`) is refused rather than passed on as an argument. `header=` on a command or `env=` on a URL is an error. (`TestSplitServerWords`, `TestLibraryMCPHeadersEnvTimeout`) [`mcp_options.go`](../../internal/library/mcp_options.go) - A skill brought in from a link into a folder an app keeps per version (ego lite's framework `Versions/`, #2635) is linked through the app's own steady link, or through `Versions/Current` when Current is that version (`linkVia`, `currentVersion`; `TestImportedSkillFollowsAppUpdate`, `TestImportedSkillThroughVersionsCurrent`). One brought in before that links straight to `Versions/`, which the app's update deletes (tjhhhh on Discord). Every read of the page (looking at each link once, `readParts`, and starting that over when a link was mended) and the first step of every sync mend such links (`followMovedSkills`): a library skill that is a link, whose target isn't there, and whose first missing part is named like a version (`versionName`) is linked to the same place under that folder's `Current`, `current` or `latest` link, else under the highest version beside it that has a `SKILL.md` there (`movedVersion`, `versionLess`); one whose target is in `Versions/` while the bundle's `Versions/Current` is a link to another version with the same skill is linked through Current (`laterCurrent`). `Source.Dir` follows. Only the library's link is replaced (`os.Remove` of the link, then `dirLink`; put back as it was when the new link can't be made). A folder of the library's own, a link to a folder the user deleted that isn't a version (shown missing as before) and the app's old version folder are left as they are (`TestOldImportFollowsDeletedVersion`, `TestOldImportFollowsVersionsCurrent`, `TestOldImportFollowsHighestVersion`, `TestMovedSkillsLeaveTheRest`). The app's own steady link can be left naming the version its update deleted (ego's `active_version_dir` still at 0.6.2.5 with only 0.6.2.7 on disk, tjhhhh again): the app's links are its own and never written to. When the library's link dangles and its target isn't in a version folder, the links on the way are followed as far as they are there (`linkedEnd`) and `movedVersion` is asked of where they end; the library links to the folder found, and keeps the steady link as `Source.Via`. Once the steady link leads to a `SKILL.md` again, the library links through it once more and `Via` is cleared (`TestSteadyLinkLeftOnDeletedVersion`, `TestSteadyLinkLeftOnDeletedVersionNoOther`). [`skill_moved.go`](../../internal/library/skill_moved.go) diff --git a/internal/library/carry.go b/internal/library/carry.go index 24ad5a937..6e9952c6b 100644 --- a/internal/library/carry.go +++ b/internal/library/carry.go @@ -57,6 +57,13 @@ type CarriedSkill struct { Note string `json:"note,omitempty"` // Router is Skill.Router: direct, kept out of the skill router Router string `json:"router,omitempty"` + // From is the repository the skill was traced to on the computer that + // made the bundle (Skill.From, skill_from.go), when it isn't a source + // the skill carries itself: the lock, a checkout, CC Switch's record or + // an install-again there. What traced it is that computer's own files, + // so without this the other computer groups the skill under "this + // computer". Bundles from before this have none. + From string `json:"from,omitempty"` } // A skill's file bigger than maxCarried is left out, and so are the @@ -114,8 +121,12 @@ func Collect() (*Bundle, error) { for _, s := range skills { dirs = append(dirs, realDir(skillDir(s.Name))) } + tr := &tracer{} for i, c := range readSkills(dirs) { s := skills[i] + if s.Source == nil || s.Source.Kind == "folder" { + c.From = tr.repoOf(s) + } 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) } @@ -515,7 +526,18 @@ func Put(b *Bundle) (*Result, error) { 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}) + n := &Skill{Name: s.Name, Source: s.Source, Agents: slices.Clone(orNone(s.Agents)), OffHere: off, Called: s.Called, Note: s.Note, Router: s.Router} + // where it came from: the other computer's trace, else what + // this one has found itself (Group again) for the same skill, + // which a bundle from before From was carried says nothing of + n.From = s.From + if cur := l.skill(s.Name); cur != nil && sameSource(cur.Source, s.Source) { + if n.From == "" { + n.From = cur.From + } + n.Gone = cur.Gone // kept on this computer only + } + skills = append(skills, n) if keep[s.Name] { continue } diff --git a/internal/library/carry_from_test.go b/internal/library/carry_from_test.go new file mode 100644 index 000000000..1af2525fa --- /dev/null +++ b/internal/library/carry_from_test.go @@ -0,0 +1,127 @@ +package library + +import ( + "encoding/json" + "net/http" + "net/http/httptest" + "path/filepath" + "strings" + "testing" +) + +// ConatusMoon2222 on X: skills grouped by their library on one computer +// land in "this computer" on another after a sync, and Group again doesn't +// bring them back. What traced them (the skills CLI's lock, a checkout) is +// the first computer's own files, and the bundle carried neither it nor +// the From a trace made, while Put also wrote every skill new and lost +// what Group again had found here. + +// onA makes a library on one computer whose skills are traced by the +// skills CLI's lock, and gives its bundle. +func onA(t *testing.T) *Bundle { + t.Helper() + h := sandbox(t) + skill(t, filepath.Join(h, ".agents/skills/locked"), "locked", "Locked") + write(t, filepath.Join(h, ".agents/.skill-lock.json"), `{"version":3,"skills":{"locked":{"source":"other/locked","sourceType":"github"}}}`) + dirLink(filepath.Join(h, ".agents/skills/locked"), filepath.Join(h, ".claude/skills/locked")) + skill(t, filepath.Join(h, ".claude/skills/mine"), "mine", "Mine") + ok(t)(ImportSkills([]string{"locked", "mine"})) + if r := repoIn(t)["locked"]; r != "other/locked" { + t.Fatalf("A doesn't group locked: %q", r) + } + b, err := Collect() + if err != nil { + t.Fatal(err) + } + return b +} + +func TestSyncedSkillKeepsItsRepo(t *testing.T) { + b := onA(t) + sandbox(t) // the other computer: no lock, no checkout + ok(t)(Put(b)) + got := repoIn(t) + if got["locked"] != "other/locked" || got["mine"] != "" { + t.Errorf("on B: %v", got) + } + // and it goes on to a third computer from B, whose files trace nothing + b2, err := Collect() + if err != nil { + t.Fatal(err) + } + sandbox(t) + ok(t)(Put(b2)) + if r := repoIn(t)["locked"]; r != "other/locked" { + t.Errorf("on C: %q", r) + } +} + +// A bundle from before From was carried says nothing of it: what this +// computer found for the skill (Group again) stays through the sync. +func TestSyncKeepsRepoFoundHere(t *testing.T) { + b := onA(t) + // as a magpie before this made it: the same JSON without the field + raw, err := json.Marshal(b) + if err != nil { + t.Fatal(err) + } + raw = []byte(strings.ReplaceAll(string(raw), `"from":"other/locked",`, "")) + b = &Bundle{} + if err := json.Unmarshal(raw, b); err != nil { + t.Fatal(err) + } + sandbox(t) + ok(t)(Put(b)) + l := mustLoad(t) + l.skill("locked").From = "other/locked" // Group again, here + if err := l.save(); err != nil { + t.Fatal(err) + } + ok(t)(Put(b)) + if r := repoIn(t)["locked"]; r != "other/locked" { + t.Errorf("after the next sync: %q", r) + } +} + +// Synced before From was carried: a skill of the pack that came with its +// source still names the repository, and Group again groups the rest. +func TestRegroupAfterOldSync(t *testing.T) { + h := sandbox(t) + pack := map[string]string{ + "skills/a/SKILL.md": "---\nname: a\ndescription: A\n---\nA\n", + "skills/b/SKILL.md": "---\nname: b\ndescription: B\n---\nB\n", + } + tar := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if strings.TrimPrefix(r.URL.Path, "/tar/") != "someone/pack" { + http.NotFound(w, r) + return + } + w.Write(tarball(t, pack)) + })) + t.Cleanup(tar.Close) + oldT := tarballURL + tarballURL = func(repo, ref string) string { return tar.URL + "/tar/" + repo } + t.Cleanup(func() { tarballURL = oldT }) + + write(t, filepath.Join(h, "x/a/SKILL.md"), pack["skills/a/SKILL.md"]) + write(t, filepath.Join(h, "x/b/SKILL.md"), pack["skills/b/SKILL.md"]) + b := &Bundle{Skills: []*CarriedSkill{ + {Name: "a", Source: &Source{Kind: "github", Repo: "someone/pack", Path: "skills/a"}, Agents: []string{}, Files: map[string][]byte{"SKILL.md": []byte(pack["skills/a/SKILL.md"])}}, + {Name: "b", Agents: []string{}, Files: map[string][]byte{"SKILL.md": []byte(pack["skills/b/SKILL.md"]), "mine.txt": []byte("x")}}, // a file of its own: grouped, not bound + }} + ok(t)(Put(b)) + if r := repoIn(t)["b"]; r != "" { + t.Fatalf("b before: %q", r) + } + if n, err := RegroupSkills(); err != nil || n != 1 { + t.Fatalf("regroup: %d %v", n, err) + } + if r := repoIn(t)["b"]; r != "someone/pack" { + t.Errorf("b after: %q", r) + } + // and the next sync of the same old bundle doesn't undo it + ok(t)(Put(b)) + if r := repoIn(t)["b"]; r != "someone/pack" { + t.Errorf("b after a sync: %q", r) + } +}