Files
magpie/internal/library/skill_how.go
T
yetone 88b65d7584 library: the skills sheet says why an agent gets copies (4kT1FohCTP97031 on X)
The report (X, tweet 2123056163587780891): "Claude Code can only copy
skills, never link them; every other agent can use paths." Claude Code
already takes links: real claude 2.1.327 in a sandbox HOME and
CLAUDE_CONFIG_DIR loads a symlinked skill folder and a symlinked
SKILL.md, and reads the files beside it. The row that said 只能用副本
with no reason and no switch was Claude Desktop's (Cowork), which
really does skip a linked skill (Claude Desktop 2.7032; see
desktop_skills.go), so it keeps copies.

- AgentView gains CopyWhy and CopyWith (copyWhy in skill_how.go):
  desktop, liveagent, wsl (a WSL agent on a Windows drive), junction
  (agy/antigravity/crush on Windows), and shared, for an agent that reads
  the same folder as one given copies. A shared agent was shown as
  "Gets links" while syncSkills gave it copies (l.copies with sharers);
  its How is now copy and the row names whose copies they are.
- The How sheet's row says the reason ("Only ever takes copies: Cowork
  skips a linked skill"), in en, zh, zh-TW, ja and de. Without a known
  reason it still says "Only ever takes copies".
- docs/subsystems/library.md says so.

Not changed: agy/crush on Windows take copies even where a symlink
could be made (#5107, #6610 decided that); not part of this report.

Tests:
- TestSkillHowSaysWhyCopies: Claude Code link, Claude Desktop
  desktop, Codex reading Claude Code's folder as copies -> Claude Code
  how copy, shared, ["Codex"], a copy on disk, and links again after.
  Red on the old behaviour (fields kept, copyWhy and the How override
  dropped): "Claude Desktop: why """ and "Claude Code: how "link"".
- skill-how.test.cjs: the desktop row's reason, a shared row, a bare
  must-copy row, and a new all-languages test (en zh zh-TW ja de) at a
  phone width. Red on origin/main's plugins/library client.js and
  i18n.js in every language (expected the reason, got the bare words).

Run:
- gofmt -l clean. go vet internal/library and (-tags nogui)
  internal/gui on Linux, GOOS=windows, GOOS=darwin (wsl-run): ok.
- go test ./internal/library -count=1 at this head (rebased on
  origin/main 1e2287758, on mote): ok.
  go test -tags nogui -run 'Budget|I18n|Doc' ./internal/gui: ok.
- GUI at this head on mote, chromium and webkit: library-wsl-asleep +
  skill-how 53/53 each. On magpie-desktop at load ~78 (another session's
  full suite and GUI suite) many files timed out together; skill-how and
  skill-name-many-agents alone there: 15/15.
- Before the rebase on magpie-desktop, both engines: skill-how,
  library-wsl-asleep, i18n-keys, i18n-en-no-cjk, library-desktop-skills,
  library-desktop-twin, skill-router, library-items pass. click-scroll,
  library-all and skill-where (webkit) red, and the same red on a clean
  origin/main 454f9d023 alone: #4385, #4967, #5154 (commented).

Not run before the push: the full go test ./... and the whole GUI
suite (the coordinator asked to ship on the targeted runs); they run
after on a free machine.
2026-11-19 00:13:47 +08:00

201 lines
6.2 KiB
Go

package library
// How an agent is given its skills (#896): a link to the library's folder,
// which an update reaches at once, or a copy of its own, which a tool that
// doesn't follow links (or a sync, a backup, an archive) takes as a folder
// like any other. The library has one way for every agent, and an agent
// can have its own. A copy carries magpie's mark (marker), so it is known
// for magpie's own and is all a change of way or a Remove ever takes away;
// a folder of the user's by that name is never touched. A copy is made
// again on each sync once the library's skill differs from it: the page
// says which copies differ until then (SkillView.Behind), with a button
// that syncs. A copy edited since it was made goes to the library first,
// when its edit is the newest (takeEdits).
import (
"errors"
"fmt"
"os"
"path/filepath"
"slices"
)
// The ways an agent can be given its skills.
const (
HowLink = "link"
HowCopy = "copy"
)
// copies is whether the agent of t gets copies of its skills: one that
// can only take copies (in a WSL distro), else its own way, else the
// library's. Agents reading the very same folder (sharers) get copies when
// any of them is to, so that they don't undo each other's.
func (l *Library) copies(t *Target, sharers []string) bool {
if t.Copy {
return true
}
return slices.ContainsFunc(append([]string{t.Agent.ID}, sharers...), func(id string) bool { return l.howOf(id) == HowCopy })
}
// Why an agent is given copies of its skills while links are its way
// (AgentView.CopyWhy), so the page can say so rather than leave a
// copy-only row unexplained (4kT1FohCTP97031 on X).
const (
// Claude Desktop's Cowork leaves out a skill whose entry is a link: it
// lstats each one and skips a symbolic link (Claude 2.7032 app.asar),
// and it runs a skill in its own sandbox, which can't read past the
// plugin's folder (desktop_skills.go)
copyDesktop = "desktop"
// K-brain refuses a link anywhere in a skill's path (liveagent.go)
copyLiveAgent = "liveagent"
// an agent in WSL whose skills folder, or the distro's store, is on a
// Windows drive, where the distro can't make a link Windows follows
// (wslStored)
copyWSL = "wsl"
// on Windows, an agent that skips a skill behind a junction
// (junctionBlind)
copyJunction = "junction"
// an agent reading the very same folder as one given copies: they
// get copies together (copies), CopyWith names the others
copyShared = "shared"
)
// copyWhy is why the agent of t gets copies of its skills without its
// own way or the library's asking for them, "" when it doesn't; sharers
// are the other agents reading the very same folder, and with the names
// of those given copies, for copyShared.
func (l *Library) copyWhy(t *Target, sharers []*Target) (why string, with []string) {
switch {
case t.Desktop != nil:
return copyDesktop, nil
case t.Copy && t.Agent.ID == "liveagent":
return copyLiveAgent, nil
case t.Copy && t.Agent.WSL != "":
return copyWSL, nil
case t.Copy && skipsJunctions(t.Agent.ID):
return copyJunction, nil
case t.Copy:
return "", nil
}
if l.howOf(t.Agent.ID) == HowCopy {
return "", nil
}
for _, o := range sharers {
if l.howOf(o.Agent.ID) == HowCopy {
with = append(with, o.Agent.Name)
}
}
if len(with) > 0 {
return copyShared, with
}
return "", nil
}
// howOf is the way the agent is given its skills: its own, else the
// library's.
func (l *Library) howOf(id string) string {
if h := l.SkillHow[id]; h == HowLink || h == HowCopy {
return h
}
if l.CopySkills {
return HowCopy
}
return HowLink
}
// SetSkillHow sets the way the library gives its skills to every agent
// (agent "") or to one ("" for how the library does it); the agents' links
// and copies are made again the new way.
func SetSkillHow(agent, how string) (*Result, error) {
if how != "" && how != HowLink && how != HowCopy {
return nil, fmt.Errorf("skills are given as %s or %s, not %q", HowLink, HowCopy, how)
}
return change(func(l *Library) error {
if agent == "" {
if how == "" {
return fmt.Errorf("the library gives skills as %s or %s", HowLink, HowCopy)
}
l.CopySkills = how == HowCopy
return nil
}
if err := checkAgent(agent); err != nil {
return err
}
if how == "" {
delete(l.SkillHow, agent)
if len(l.SkillHow) == 0 {
l.SkillHow = nil
}
return nil
}
if l.SkillHow == nil {
l.SkillHow = map[string]string{}
}
l.SkillHow[agent] = how
return nil
})
}
// relink puts a link to the library's skill at p in place of magpie's copy
// there, not ok when no link can be made (Windows without the right to),
// which leaves the copy as it is. A copy that differs from the library's
// skill (edited in the agent) is kept aside rather than lost.
func relink(agent, p, name string) (bool, error) {
current, err := copyIsFresh(p, hashDir(realDir(skillDir(name))), false)
if err != nil {
return false, err
}
next := filepath.Join(filepath.Dir(p), "."+name+".magpie-link")
os.Remove(next)
if err := dirLink(skillDir(name), next); err != nil {
return false, nil
}
if current {
old := oldPath(p, name)
if err = os.Rename(p, old); err == nil {
// best effort: what's left is tried again on the next sync
defer os.RemoveAll(old)
}
} else {
_, err = setAside(agent, p)
}
if err != nil {
os.Remove(next)
return false, err
}
if err := os.Rename(next, p); err != nil {
os.Remove(next)
return false, errors.Join(err, link(p, name))
}
return true, nil
}
// behind are, for each skill, the agents given it as a copy (of their own
// choosing, on this machine) whose copy differs from the library's skill.
func (l *Library) behind(targets []*Target) map[string][]string {
out := map[string][]string{}
for _, t := range targets {
if t.Skills == "" || t.Desktop != nil || t.Agent.WSL != "" || l.howOf(t.Agent.ID) != HowCopy && !skipsJunctions(t.Agent.ID) {
continue
}
for _, s := range l.Skills {
if !s.gives(t.Agent.ID) {
continue
}
p := filepath.Join(t.Skills, s.Name)
if !ours(p, s.Name) || linked(p) {
continue
}
h := l.libHash(s.Name)
if h == "" {
continue
}
current, _ := copyIsFresh(p, h, false)
if !current && !slices.Contains(out[s.Name], t.Agent.ID) {
out[s.Name] = append(out[s.Name], t.Agent.ID)
}
}
}
return out
}