library: no second copy of a skill the user keeps in ~/.agents/skills

math_milian: a skill given to every agent was in ~/.agents/skills and
in ~/.pi/agent/skills, and Pi warned
  "hunk-review" collision: ~/.agents/skills/hunk-review (skipped)
Pi 1.4.0 reads ~/.pi/agent/skills, then ~/.agents/skills
(userAgentsSkillsDir in dist/core/package-manager.js), and drops a
skill read twice only when both paths are one file (realPathSet in
dist/core/skills.js). magpie already left out its own link for an
agent that reads the shared folder when the entry there was the
library's folder, magpie's link, or a holder's (Goose, Kimi Code). It
did not when the entry was the user's own copy of the same files
(another installer's, or copied by hand), so Pi, Codex, Gemini CLI,
OpenCode and the other readers each got a second tree beside it.

inShared now also counts such a copy (sharedCopy: not ours, not the
library's own folder, sameTree with the library's). The reader gets
nothing in its own folder, and the existing unlink loop takes out
magpie's link or marked copy there on the next sync, which only ever
touches entries ours() proves magpie made. The user's copy is never
touched; once it differs from the library's, the library's is given
again. Readers checked in their own code: Pi 1.4.0's loader, Codex
0.165.0's binary (user-agent-skills, .agents/skills), Gemini CLI
0.35.3's bundle (Storage.getUserAgentSkillsDir) and OpenCode 2.0.25's
binary (home/.agents). Amp isn't a skills target of magpie's.

Run:
- mote-run go test -count=1 ./internal/library ./internal/gui
  ./internal/cli: library and cli ok; gui red only on
  TestMainWindowCorners/macOS_26_SDK, #3949 (mote is macOS 27). go vet internal/library (darwin, GOOS=linux
  -tags nogui, GOOS=windows): ok. gofmt -l: clean.
- Real Pi 1.4.0's own loader (DefaultResourceLoader) in a sandbox HOME,
  cwd outside the home: a link in ~/.pi/agent/skills to the library's
  folder beside the user's copy in ~/.agents/skills gives
  'name "hunk-review" collision'; with the link gone (what the sync now
  does) the skill loads from ~/.agents/skills and no diagnostic.
Broken by hand: sharedCopy's return replaced with false,
TestSharedCopyNotGivenTwice red (link and copy: pi's and codex's
second entry stays).
Not tried: the magpie GUI against the reporter's real tree (their
layout is inferred from the warning); a library skill that is itself
a link to the agent's own folder beside such a copy keeps both (that
folder is the library's); a Pi or omp of unknown version is given its
own as before; Windows and WSL not run.

Reported-by: math_milian
This commit is contained in:
yetone
2026-11-06 11:18:51 +08:00
parent 7301c070f0
commit d487aaab79
3 changed files with 113 additions and 4 deletions
+2 -2
View File
@@ -74,8 +74,8 @@ agents RTK, a CLI that cuts shell output down before the model reads it.
- 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 <name> 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)
- A skill brought in from a link into a folder an app keeps per version (ego lite's framework `Versions/<v>`, #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/<old>`, 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/<v>` 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`). [`skill_moved.go`](../../internal/library/skill_moved.go)
- A skill kept in `~/.agents/skills` is handed over to the library on the user's click (`HandOverSkills`, `POST /api/library/skills/hand-over` with `names`; the row's Manage in magpie in its source line, Manage {n} in magpie in the skills' heading, each asking first; `magpie library skill hand-over <name…>|--all`; tnt1223 on Discord). "Kept there" is `sharedEntry`: the entry in that folder that is the library skill's own folder, by its name or, for a library link into it under another folder's name, by that folder's (`SkillView.Shared`; `SharedElsewhere` when the entry only links to a folder kept elsewhere). Every reader of `sharedHas`/`sharedEntry` follows: `Always`, `syncSkills`' `inShared`, and the shared folder's own target (Goose, Kimi Code, Cindy), which is given no second link beside such an entry (`TestSharedUnderAnotherName`). Handed over, a folder in `~/.agents/skills` moves into the library's skills folder (`moveIn`: renamed, or copied, read back the same with `sameTree` and only then set aside with the backups; a failure leaves it where it was and the library's link as it was, `TestHandOverFailureKeepsOriginal`), and an entry that only links elsewhere is taken away while the library links past it to that folder. The user's own links to it in agents' folders, which would point at nothing, go too. Every agent here that read it (`readsSharedHere`, and the agent of each such link) is ticked, so each keeps it, now from the library, till it is unticked or turned off here. A skill found there and not in the library yet is brought in first. One that can't be handed over is left as it was, in `Result.Unmoved`, and the others are handed over all the same. `HandBackSkill` (`magpie library skill hand-back <name>`, CLI only; the sheet names it) moves the folder back and links the library to it, its ticks as they are. Not covered: another computer whose skill is still in its own `~/.agents/skills` gets the library's copy from a sync beside its own, until it is handed over there too; and the skills CLI's lock (`npx skills update`) may put the entry back (`TestHandOverSharedFolder`, `TestHandOverSharedLink`, `TestHandOverFound`, `TestHandOverWithGoose`, `TestLibrarySkillHandOver`, `skill-handover.test.cjs`). [`skill_handover.go`](../../internal/library/skill_handover.go)
- The agents that read `~/.agents/skills` themselves are `readsShared` (Codex, Gemini CLI, OpenCode, DevEco Code, Crush, dsh, Command Code, Devin, Droid, Cline, Grok Build, Grok App, fx, Alma, Kimi Code, ZCode and OmO) and, from the version that started to, `Target.SharedToo`: Pi from 0.54.0 (its CHANGELOG; `userAgentsSkillsDir` in package-manager.js) and omp from 11.0.0 (`discovery/agents.ts`; 10.6.2 doesn't). OmO runs on senpi, whose oldest version on npm (0.74.0) already reads it. A Pi or omp whose version isn't known is taken as older, as before. Every reader goes through `Target.readsSharedDir`: `Always` (the row's lock), `syncSkills`' `inShared` (no second link in its own folder) and `readsSharedHere` for a handover (usrname1833 on Discord; `TestPiReadsSharedSkillsFromItsVersion`).
- Put skills in (`Library.SharedSkills`; `SetSkillsWhere`, `POST /api/library/skills/where` with `how` `agents` or `shared`; `magpie library skill where agents|shared`; the Skills tab's bar under How, `skillWhereBar` in plugins/library/client.js; usrname1833 on Discord). Off, the default and what a library.json from before reads as, each agent gets the skills ticked for it in its own folder. On, every library skill on here (`keepsShared`: not `OffHere`, its folder there) also gets magpie's link in `~/.agents/skills` (`syncShared`, run after `sweepShared`), so every agent and tool that reads that folder has it, ticked or not, and is in its `Always`. The files stay in the library, so updates, checks, edit tracking and Off here work as before, and nothing is moved when it is turned on: existing skills just get links. An entry of the user's own by a skill's name is left alone, silently, and the agents ticked for the library's skill still get it in their own folders. A sync leaves an existing link as it is. A skill given to the folder's own agent (Kimi Code, Goose, Cindy) is that agent's sync's. Turned off, `sweepShared` takes out magpie's links there and nothing else, and each agent's own links come back. It is this computer's: `Collect` never carries it (`TestSkillsWhereShared`, `TestSkillsWhereSharedWithKimi`, `skill-where.test.cjs`). Not covered: on Windows without the right to make links the entry is a marked copy; a stale one is set aside and copied again, but `takeEdits` doesn't take edits made in that copy into the library, so they go to the backups when the setting is turned off or the skill changes (#4958). [`skills_shared.go`](../../internal/library/skills_shared.go)
- The agents that read `~/.agents/skills` themselves are `readsShared` (Codex, Gemini CLI, OpenCode, DevEco Code, Crush, dsh, Command Code, Devin, Droid, Cline, Grok Build, Grok App, fx, Alma, Kimi Code, ZCode and OmO) and, from the version that started to, `Target.SharedToo`: Pi from 0.54.0 (its CHANGELOG; `userAgentsSkillsDir` in package-manager.js) and omp from 11.0.0 (`discovery/agents.ts`; 10.6.2 doesn't). OmO runs on senpi, whose oldest version on npm (0.74.0) already reads it. A Pi or omp whose version isn't known is taken as older, as before. Every reader goes through `Target.readsSharedDir`: `Always` (the row's lock), `syncSkills`' `inShared` (no second link in its own folder) and `readsSharedHere` for a handover (usrname1833 on Discord; `TestPiReadsSharedSkillsFromItsVersion`). A user's own copy of the library skill's very files in `~/.agents/skills` (another installer's, or copied by hand: not magpie's, not the library's own folder) counts too (`sharedCopy`, by `sameTree`): such a reader gets nothing in its own folder, and magpie's link or marked copy there goes on the next sync, since Pi drops a skill read twice only when both paths are one file (`realPathSet` in dist/core/skills.js) and warned of a collision for the two trees. The user's copy is never touched; once it differs from the library's, the library's is given again (math_milian on Discord; `TestSharedCopyNotGivenTwice`). Not covered: a library skill that is a link to the agent's own folder (brought in from there) beside such a copy keeps both, since that folder is the library's; and a Pi or omp of unknown version is given its own as before.
- Put skills in (`Library.SharedSkills`; `SetSkillsWhere`, `POST /api/library/skills/where` with `how` `agents` or `shared`; `magpie library skill where agents|shared`; the Skills tab's bar under How, `skillWhereBar` in plugins/library/client.js; usrname1833 on Discord). Off, the default and what a library.json from before reads as, each agent gets the skills ticked for it in its own folder. On, every library skill on here (`keepsShared`: not `OffHere`, its folder there) also gets magpie's link in `~/.agents/skills` (`syncShared`, run after `sweepShared`), so every agent and tool that reads that folder has it, ticked or not, and is in its `Always`. The files stay in the library, so updates, checks, edit tracking and Off here work as before, and nothing is moved when it is turned on: existing skills just get links. An entry of the user's own by a skill's name is left alone, silently, and the agents ticked for the library's skill still get it in their own folders, unless it holds the library's very files and they read the folder themselves (`sharedCopy`, above). A sync leaves an existing link as it is. A skill given to the folder's own agent (Kimi Code, Goose, Cindy) is that agent's sync's. Turned off, `sweepShared` takes out magpie's links there and nothing else, and each agent's own links come back. It is this computer's: `Collect` never carries it (`TestSkillsWhereShared`, `TestSkillsWhereSharedWithKimi`, `skill-where.test.cjs`). Not covered: on Windows without the right to make links the entry is a marked copy; a stale one is set aside and copied again, but `takeEdits` doesn't take edits made in that copy into the library, so they go to the backups when the setting is turned off or the skill changes (#4958). [`skills_shared.go`](../../internal/library/skills_shared.go)
- A skill's files are read and edited in the page (refeccd on Discord: skills handed over from `~/.agents/skills` had nowhere else to be edited). `GET /api/library/skill?name=&file=` answers `ReadSkillFile`: the file (`SKILL.md` when none is named), its text, the skill's folder and `files`, its text files (`skillTextFiles`: UTF-8 with no NUL, up to 1 MB each and 200 in all, `SKILL.md` first, leaving out links, hidden folders, `skipInSkill`'s entries and what installing writes, `fromInstall`). `SaveSkillFile` (`POST /api/library/skills/save` with `name`, `file`, `text` and `was`) goes through `change`: it writes only a regular file already in the skill's folder, never through a link or out of the folder (`skillFilePath`), refuses when the file no longer holds `was`, the text the page opened, keeps a file's CRLF line ends, and writes with `edit.WriteAtomic`. It writes the library's folder, so a linked agent reads it at once and the sync that follows copies it again into each copy; a copy edited in an agent before it is older than the save, so `takeEdits` keeps that edit aside with the backups. A skill from GitHub or git saved here reads as changed here (`editState`), so an update asks before it replaces the edit. The page (`viewSkill`, `rendered` in plugins/library/client.js) draws a Markdown file with the Plugins page's renderer (`window.mdRender`, `markdown` in plugins.js) and its front matter as rows, any other file as its text; Edit swaps in a text box, ⌘S or Save sends it, and the skill's other files are the app's menu under its name (`TestSavedSkillFileReachesEveryAgent`, `TestSkillFileSaveRefusesAChangedFile`, `TestSkillFileStaysInTheSkill`, `TestSkillFileKeepsLineEnds`, `skill-edit.test.cjs`). Not covered: the TUI and CLI have no editor; a new file can't be made, and a file can't be renamed or removed, here. [`skill_file.go`](../../internal/library/skill_file.go)
- WorkBuddy's servers go in `$WORKBUDDY_CONFIG_DIR/mcp.json` (default `~/.workbuddy`; WorkBuddy AI, the international build, keeps its own in `~/.workbuddy-ai`, #1494), and CodeBuddy Code's go in the first of `.mcp.json`, `mcp.json` and `~/.codebuddy.json` that exists, as `resolveMcpFilePath` reads them (#1266). WorkBuddy connects a server only after the user has switched it on once in its own MCP settings. It keeps that approval in `mcp-approvals.json`, keyed by the command, args and env names or the URL's origin, so changing those asks again. magpie never writes the approval, and the servers page says so, by the build's name, when a server is given to WorkBuddy or WorkBuddy AI.
- Alma's servers go in `~/.config/alma/mcp.json` on every system (its home, not its data folder), as its own MCP settings write them: `command`/`args`/`env`, or `url`/`headers` with `transport: "sse"` only for SSE (#1292). Alma reads the file as it starts and when its MCP settings refresh, so a server given to it while it runs reaches it after one of those.
+26 -2
View File
@@ -581,6 +581,21 @@ func sharedEntry(name string) string {
return ""
}
// sharedCopy is whether p, the entry in ~/.agents/skills by a library
// skill's name, is the user's own copy of the library's very files: not
// one magpie made (ours), not the library's own folder (sharedEntry), and
// holding what the library's holds.
func sharedCopy(p, name string) bool {
if _, err := os.Lstat(p); err != nil || ours(p, name) {
return false
}
lib := skillDir(name)
if _, err := os.Stat(lib); err != nil || realDir(p) == realDir(lib) {
return false
}
return sameTree(p, lib)
}
// within is whether p is dir or inside it.
func within(p, dir string) bool {
rel, err := filepath.Rel(dir, p)
@@ -633,9 +648,18 @@ func (l *Library) syncSkills(t *Target, res *Result, all []*Target) {
return true
}
p := filepath.Join(sharedSkillsDir(), s.Name)
return slices.ContainsFunc(all, func(o *Target) bool {
if slices.ContainsFunc(all, func(o *Target) bool {
return o.Skills != "" && realDir(o.Skills) == shared && s.gives(o.Agent.ID)
}) && (ours(p, s.Name) || realDir(p) == realDir(skillDir(s.Name)))
}) && (ours(p, s.Name) || realDir(p) == realDir(skillDir(s.Name))) {
return true
}
// the user's own copy of the very same files kept in the shared
// folder (another installer's, or one copied by hand): the agent
// finds it there, and a link or copy of the library's in its own
// folder too is a second tree by that name. pi loads both and
// warns of a collision, since it drops a skill read twice only
// when both paths lead to one file (realpath, dist/core/skills.js)
return sharedCopy(p, s.Name)
}
var mine []string
for _, name := range a.Skills {
@@ -0,0 +1,85 @@
package library
import (
"os"
"path/filepath"
"testing"
)
// math_milian: a skill given to every agent was in ~/.agents/skills (the
// user's own copy) and in ~/.pi/agent/skills (magpie's link to the
// library's own copy of the same files). Pi reads both folders and drops
// a skill read twice only when both paths are one file (realpath,
// dist/core/skills.js), so it warned "hunk-review" collision. An agent
// that reads ~/.agents/skills itself gets no second tree there: magpie's
// link or copy in its own folder goes, the user's copy stays as it is.
func TestSharedCopyNotGivenTwice(t *testing.T) {
for _, how := range []string{HowLink, HowCopy} {
t.Run(how, func(t *testing.T) {
h := sandbox(t)
// no agent whose folder ~/.agents/skills is (Goose)
os.RemoveAll(filepath.Join(h, ".config/goose"))
os.Remove(filepath.Join(h, "bin/goose"))
os.Remove(filepath.Join(h, "bin/goose.exe"))
setPiVersion(t, "1.4.0")
src := filepath.Join(h, "src/skills")
skill(t, filepath.Join(src, "hunk-review"), "hunk-review", "Review hunks")
pi := filepath.Join(h, ".pi/agent/skills/hunk-review")
cx := filepath.Join(h, ".codex/skills/hunk-review")
cl := filepath.Join(h, ".claude/skills/hunk-review")
ok(t)(SetSkillHow("", how))
ok(t)(InstallSkills(src, []string{"hunk-review"}, []string{"claude", "codex", "pi"}))
for _, p := range []string{pi, cx, cl} {
if !ours(p, "hunk-review") {
t.Fatalf("%s isn't given it before the shared copy", p)
}
}
// the user's own copy of the same files in ~/.agents/skills
shared := filepath.Join(h, ".agents/skills/hunk-review")
skill(t, shared, "hunk-review", "Review hunks")
ok(t)(Sync())
for _, p := range []string{pi, cx} {
if _, err := os.Lstat(p); !os.IsNotExist(err) {
t.Errorf("%s still has a second copy: %v", p, err)
}
}
if !ours(cl, "hunk-review") {
t.Error("claude reads no ~/.agents/skills, yet lost it")
}
if ours(shared, "hunk-review") || !exists(filepath.Join(shared, "SKILL.md")) || !exists(filepath.Join(shared, "scripts/run.sh")) {
t.Error("the user's copy in ~/.agents/skills was touched")
}
// the user's copy changes: it is another skill by that name,
// and the library's is given again
write(t, filepath.Join(shared, "SKILL.md"), "---\nname: hunk-review\ndescription: Mine\n---\n")
ok(t)(Sync())
for _, p := range []string{pi, cx} {
if !ours(p, "hunk-review") {
t.Errorf("%s hasn't the library's again", p)
}
}
if read(t, filepath.Join(shared, "SKILL.md")) != "---\nname: hunk-review\ndescription: Mine\n---\n" {
t.Error("the user's changed copy was touched")
}
})
}
}
// Pi before 0.54.0 reads its own folder only: it keeps its link.
func TestSharedCopyOldPiKeepsItsOwn(t *testing.T) {
h := sandbox(t)
os.RemoveAll(filepath.Join(h, ".config/goose"))
os.Remove(filepath.Join(h, "bin/goose"))
os.Remove(filepath.Join(h, "bin/goose.exe"))
setPiVersion(t, "0.53.0")
src := filepath.Join(h, "src/skills")
skill(t, filepath.Join(src, "hunk-review"), "hunk-review", "Review hunks")
skill(t, filepath.Join(h, ".agents/skills/hunk-review"), "hunk-review", "Review hunks")
ok(t)(InstallSkills(src, []string{"hunk-review"}, []string{"pi"}))
if !ours(filepath.Join(h, ".pi/agent/skills/hunk-review"), "hunk-review") {
t.Error("pi 0.53 lost its own link")
}
}