diff --git a/docs/subsystems/library.md b/docs/subsystems/library.md index 3f1d13c31..813860155 100644 --- a/docs/subsystems/library.md +++ b/docs/subsystems/library.md @@ -115,6 +115,7 @@ Pi's extensions and packages. - A carried library (backup, WebDAV, S3) carries the variables (`Bundle.Env`: name, value, agents ticked; onducoder on X: a skill's key set once, on every computer), never `Applied.Env`. Nil in a bundle from a magpie before they were carried, and `Put` then keeps this computer's. A copy without keys keeps the names and empties every value, whatever the name (`WithoutSecrets`; a variable's name says nothing of its value), and `WithSecrets` fills them from this computer's. `syncEnv` writes nothing for a variable with no value. A backup with keys is sealed under its passphrase, as the servers' tokens are. `TestEnvIsCarried`. - A subagent file is recorded by the hash of what magpie wrote (`Applied.Subagents`). magpie writes over or takes away only a file still exactly as it wrote it; one the user edited is theirs from then on, and the page says so. A file of the user's own by the same name is never written over: the agent gets a problem instead. A file of magpie's the user deleted is written again on the next sync. Cursor's `~/.cursor/agents` is read by the Cursor app only; the cursor-agent CLI reads only a project's `.cursor/agents`. An agent in a WSL distro gets no subagents, nor do agents with no user-level subagent folder (Kimi, Goose, Crush, Qoder, CodeBuddy, iFlow, Kiro). A carried library carries the subagents (`Bundle.Subagents`: name, description, prompt, skills and servers named, agents ticked; xixi_Sean_ on X), never `Applied.Subagents`: each computer writes them into its own agents at the sync `Put` runs and records its own files, so a file the user edited there, or one of theirs by the name, stays theirs as above. `Put` replaces the subagents with the bundle's, so one taken out on one computer goes from the others; a bundle without the field, from a magpie before it, is unknown and keeps the ones here (nil, where Collect always carries a list). Agent ids are carried as they come, one a newer magpie gives subagents to included; a name that isn't one refuses the bundle. An empty list hashes as before the field, so a computer with none sees no change to sync (`TestCarrySubagents`, `TestCarryOldBundleKeepsSubagents`, `TestCarrySubagentKeepsUsersFiles`, `TestCarryNewerSubagents`, `TestBackupFileCarriesSubagents`, `TestSyncSubagents`, `TestNoSubagentsHashAsBefore`). - CC Switch's own skills folder is only ever read. +- A skill linked in from a folder the user keeps (`~/.agents/skills`, the skills CLI's) whose folder is gone, as after `npx skills remove` (#7226), or has no `SKILL.md`, is `Skill.Dangling` (`markDangling` in [`skill_dangling.go`](../../internal/library/skill_dangling.go), each read and sync): marked `Gone` for the page to offer taking it out, and given to no agent, project or shared folder (`gives`); magpie's own links to it are taken away. Only its link is ever removed, never the folder. A link that can't be looked at for another reason than being absent is unknown, and the entry stays. - Reading the page never waits on reading skills through (#541). `Read` compares an agent's own skill with the same name in another agent (`sameTree`) and hashes each library skill given as a copy (`hashFiles`, for `behind`). Both are kept against a stamp of the folders (each file's and link's path, type, size and mtime, `stamper` in [`treememo.go`](../../internal/library/treememo.go)), taken by the same walk that compares or hashes them, before a file is opened; a later read walks the folder without opening a file, and while the stamps match reuses the answer. Only an answer from a whole read of folders untouched for `settleFor` (3s) is kept: a folder that couldn't be walked or read, or was written a moment ago, is read again next time, never taken as unchanged. A rewrite that keeps a file's size and mtime isn't seen. `Glance` (`GET /api/library?glance=1`) is the page without those two, marked `partial`; the page draws it first, then the whole read, which runs `behind` and `foundSkills` after the library's lock is let go, so a change doesn't wait on them either. - A read of the page (`Read`, `Glance`) follows the links on the way to a few hundred paths, most of them many times over: each agent's skills folder for each skill, CC Switch's and the shared folder for each source. While the read holds the lock, `resolve` looks at each entry on the way once (`lookPart` in [`linkmemo.go`](../../internal/library/linkmemo.go)): one `Lstat`, and a `Readlink` for a link. Nothing is kept past the read, so a link made or moved on disk between two visits shows on the second. An entry that couldn't be looked at for another reason than its not being there is unknown and looked at again. One read had taken ~10,000 looks; on oxyuan's Windows machine that was ~1.3s a visit (#1771). - A slash command file is recorded as a subagent's is (`Applied.Commands`, through the shared `syncOwnFiles`): magpie writes over or takes away only a file still exactly as it wrote it, an edited one is the user's from then on, a file of the user's own by the name is never written over (the agent gets a problem), one whose bytes are what magpie would write is taken as magpie's, and one of magpie's the user deleted is written again. A carried library carries the commands (`Bundle.Commands`: name, description, prompt, agents ticked), never `Applied.Commands`; nil keeps this computer's, and an empty list hashes as before the field (`TestCommandsLeaveTheUsersFiles`, `TestCommandsCarried`). diff --git a/internal/library/projects.go b/internal/library/projects.go index 7e0901d45..e8bce269b 100644 --- a/internal/library/projects.go +++ b/internal/library/projects.go @@ -175,7 +175,7 @@ func (l *Library) syncFolder(p *Project, res *Result, hubs map[string]*Server) { // right to make a symlink copies := map[string]bool{} for name, agents := range p.Skills { - if l.skill(name) == nil { + if s := l.skill(name); s == nil || s.Dangling { // none, or its source is gone (skill_dangling.go) continue } for _, id := range agents { diff --git a/internal/library/skill_dangling.go b/internal/library/skill_dangling.go new file mode 100644 index 000000000..a12287222 --- /dev/null +++ b/internal/library/skill_dangling.go @@ -0,0 +1,65 @@ +package library + +// A skill linked in from a folder the user keeps, whose source is gone +// (@Eternalfate__ on X, #7226): "Bring in all" links a skill of the skills +// CLI's ~/.agents/skills into the library instead of moving it, and +// `npx skills remove ` then deletes that folder. The library's link +// leads nowhere, and every agent ticked for it was given a link to that +// link. markDangling marks such a skill Gone (the page offers to take it +// out, RemoveGoneSkills, which only removes the link) and Dangling, which +// `gives` reads, so no sync writes an agent a link to it. Nothing the user +// owns is touched: only magpie's own links are taken away, and a folder +// that can't be read is unknown, not gone. + +import ( + "os" + "path/filepath" + "time" +) + +// sourceGone is whether the library's skill by that name is a link to a +// folder that isn't there any more, or has no SKILL.md left. A skill that +// is a folder of the library's own, or a link that can't be looked at, is +// not: unknown keeps the entry. +func sourceGone(name string) bool { + p := skillDir(name) + if !linked(p) { + return false + } + fi, err := os.Stat(p) + if err != nil { + return absent(err) + } + if !fi.IsDir() { + return false + } + _, err = os.Stat(filepath.Join(p, "SKILL.md")) + return err != nil && absent(err) +} + +// markDangling sets Dangling for each skill linked in whose source is +// gone, marks it Gone, and clears that when it is back. It says whether a +// skill's Gone changed (to be saved). A skill from a repository keeps its +// Gone for skill_gone.go's own check. +func (l *Library) markDangling() bool { + changed := false + now := time.Now().UTC().Format(time.RFC3339) + for _, s := range l.Skills { + s.Dangling = false + if s.Source.remote() { + continue + } + switch { + case sourceGone(s.Name): + s.Dangling = true + if s.Gone == "" { + s.Gone, changed = now, true + } + case s.Gone != "" && linked(skillDir(s.Name)): + if _, err := os.Stat(filepath.Join(skillDir(s.Name), "SKILL.md")); err == nil { + s.Gone, changed = "", true + } + } + } + return changed +} diff --git a/internal/library/skill_dangling_test.go b/internal/library/skill_dangling_test.go new file mode 100644 index 000000000..fd3897eeb --- /dev/null +++ b/internal/library/skill_dangling_test.go @@ -0,0 +1,104 @@ +package library + +import ( + "os" + "path/filepath" + "testing" +) + +// #7226: a skill brought in from ~/.agents/skills is a link into it. After +// `npx skills remove` the folder is gone: the library shows the skill as +// gone, no agent is given a link to it (and magpie's own link there is +// taken away), and the user's own files are untouched. +func TestLinkedInSkillWhoseSourceIsGone(t *testing.T) { + h := sandbox(t) + shared := filepath.Join(h, ".agents/skills") + skill(t, filepath.Join(shared, "grilling"), "grilling", "Grill a plan") + skill(t, filepath.Join(shared, "keeper"), "keeper", "Stays") + os.MkdirAll(filepath.Join(h, ".claude"), 0o755) + if _, err := ImportSkills([]string{"grilling", "keeper"}); err != nil { + t.Fatal(err) + } + if _, err := SkillAgents("grilling", []string{"claude"}); err != nil { + t.Fatal(err) + } + cl := filepath.Join(h, ".claude/skills/grilling") + if _, err := os.Stat(filepath.Join(cl, "SKILL.md")); err != nil { + t.Fatalf("the agent isn't given the skill: %v", err) + } + // npx skills remove grilling + if err := os.RemoveAll(filepath.Join(shared, "grilling")); err != nil { + t.Fatal(err) + } + v, err := Read(nil) + if err != nil { + t.Fatal(err) + } + for _, s := range v.Skills { + switch s.Name { + case "grilling": + if s.Gone == "" { + t.Errorf("a skill whose source is gone isn't said to be: %+v", s) + } + case "keeper": + if s.Gone != "" { + t.Errorf("a skill whose source is there is gone: %+v", s) + } + } + } + // a change syncs every agent: none gets a link to it + if _, err := SkillAgents("grilling", []string{"claude", "codex"}); err != nil { + t.Fatal(err) + } + for _, p := range []string{cl, filepath.Join(h, ".codex/skills/grilling")} { + if _, err := os.Lstat(p); err == nil { + t.Errorf("%s: an agent still has a link to the gone skill", p) + } + } + if _, err := os.Stat(filepath.Join(shared, "keeper/SKILL.md")); err != nil { + t.Errorf("another skill of the user's was touched: %v", err) + } + // the source comes back (an unmounted drive, a reinstall): given again + skill(t, filepath.Join(shared, "grilling"), "grilling", "Grill a plan") + if _, err := SkillAgents("grilling", []string{"claude"}); err != nil { + t.Fatal(err) + } + if _, err := os.Stat(filepath.Join(cl, "SKILL.md")); err != nil { + t.Errorf("not given again once its source is back: %v", err) + } + v, _ = Read(nil) + for _, s := range v.Skills { + if s.Name == "grilling" && s.Gone != "" { + t.Errorf("still gone with its source back: %+v", s) + } + } +} + +// A folder with its SKILL.md taken out, what a skills CLI that empties the +// entry leaves, is as gone. The library's own folder never is. +func TestSourceGoneKinds(t *testing.T) { + h := sandbox(t) + shared := filepath.Join(h, ".agents/skills") + skill(t, filepath.Join(shared, "a"), "a", "A") + skill(t, skillDir("own"), "own", "Own") + if err := dirLink(filepath.Join(shared, "a"), skillDir("a")); err != nil { + t.Fatal(err) + } + if sourceGone("a") || sourceGone("own") { + t.Fatal("a skill that is there is gone") + } + os.Remove(filepath.Join(shared, "a/SKILL.md")) + if !sourceGone("a") { + t.Error("an emptied entry isn't gone") + } + if sourceGone("own") || sourceGone("nothing") { + t.Error("the library's own folder, or no entry, is gone") + } + os.RemoveAll(filepath.Join(shared, "a")) + if !sourceGone("a") { + t.Error("a removed folder isn't gone") + } + if _, err := os.Lstat(skillDir("a")); err != nil { + t.Errorf("the library's link was taken away: %v", err) + } +} diff --git a/internal/library/skill_dangling_windows_test.go b/internal/library/skill_dangling_windows_test.go new file mode 100644 index 000000000..3c89c35b3 --- /dev/null +++ b/internal/library/skill_dangling_windows_test.go @@ -0,0 +1,34 @@ +//go:build windows + +package library + +import ( + "os" + "path/filepath" + "testing" +) + +// #7226 on Windows, where the library's link into ~/.agents/skills is a +// junction: its folder removed, the skill is gone, and taking the junction +// away leaves the user's other folders as they are. +func TestJunctionToARemovedFolderIsGone(t *testing.T) { + h := sandbox(t) + shared := filepath.Join(h, ".agents", "skills") + skill(t, filepath.Join(shared, "pdf"), "pdf", "Read PDFs") + os.MkdirAll(filepath.Dir(skillDir("pdf")), 0o755) + if err := junction(filepath.Join(shared, "pdf"), skillDir("pdf")); err != nil { + t.Fatal(err) + } + if sourceGone("pdf") { + t.Fatal("a junction to a folder that is there is gone") + } + if err := os.RemoveAll(filepath.Join(shared, "pdf")); err != nil { + t.Fatal(err) + } + if !sourceGone("pdf") { + t.Error("a junction to a removed folder isn't gone") + } + if _, err := os.Lstat(skillDir("pdf")); err != nil { + t.Errorf("the junction was taken away: %v", err) + } +} diff --git a/internal/library/skills.go b/internal/library/skills.go index 629e3c638..5246a3e11 100644 --- a/internal/library/skills.go +++ b/internal/library/skills.go @@ -64,6 +64,9 @@ type Skill struct { // Gone is when an update or a check found its source without it: it // was removed there (skill_gone.go). Kept on this computer only. Gone string `json:"gone,omitempty"` + // Dangling is a skill linked in from a folder that is gone + // (skill_dangling.go): given to no agent. Set at each sync and read. + Dangling bool `json:"-"` // OffHere turns the skill off on this computer alone (hanslin1666 on // Discord): no agent here gets it, whatever Agents says. Agents is // what a sync carries and stays as the other computers have it; this @@ -108,7 +111,7 @@ type HomeEntry struct { // gives reports whether the agent id gets the skill on this computer: it // is ticked for it, and the skill isn't turned off here. func (s *Skill) gives(id string) bool { - return s != nil && !s.OffHere && slices.Contains(s.Agents, id) + return s != nil && !s.OffHere && !s.Dangling && slices.Contains(s.Agents, id) } // SkillHere turns a skill on or off on this computer alone. Which agents diff --git a/internal/library/sync.go b/internal/library/sync.go index 8c96c89c3..4641cd48d 100644 --- a/internal/library/sync.go +++ b/internal/library/sync.go @@ -112,7 +112,7 @@ func syncSteps(all, gone []*Target, b *backups, res *Result) []func(*Library) { // a skill whose app moved it is followed first, for every agent to // get the moved one (skill_moved.go) // an edit made in a subagent's own file too (subagent_file.go) - steps := []func(*Library){func(l *Library) { l.followMovedSkills(); l.followSubagentFiles() }} + steps := []func(*Library){func(l *Library) { l.followMovedSkills(); l.markDangling(); l.followSubagentFiles() }} for _, t := range all { steps = append(steps, func(l *Library) { l.syncInstructions(t, all, b, res, true) @@ -640,7 +640,8 @@ func readView(problems []Problem, glance bool) (*View, error) { // a skill whose app moved it to its new version's folder is linked // there (skill_moved.go); what was kept of the links is let go then, // for the rest of the read to see the new one - if l.followMovedSkills() { + moved := l.followMovedSkills() + if l.markDangling() || moved { forget() forget = readParts() if err := l.save(); err != nil {