Files
magpie/internal/library/skill_handover.go
T
yetone da8039368f library: skills kept in ~/.agents/skills can be handed over to the library, and then turned off per agent
tnt1223 on Discord asked for the skills in ~/.agents/skills to move into
magpie's library folder and be managed there ("统一移入 magpie 的资源库文件
夹,交给 magpie 管理"), since a skill there can't be turned off for one agent
("不能决定某个 skill 是否自由开启"). Codex, Gemini CLI and the other readers
of that folder read it themselves, whatever the library ticks. Goose, Kimi
Code and Cindy use it as their own folder.

HandOverSkills (POST /api/library/skills/hand-over with names) does the
hand-over only when the user asks for it:
- A folder kept in ~/.agents/skills moves into library/skills/<name>
  (moveIn). It is renamed, or, across disks, copied. A copy has to read
  back the same (sameTree) before the original is set aside with the
  backups. On any failure the folder stays where it was and the library's
  link stays as it was.
- An entry there that only links to a folder elsewhere is removed, and
  the library links straight to that folder. The folder itself stays.
- The user's own links to the skill in agents' folders are removed too,
  since they would point at nothing. The library gives those agents its
  own link.
- Every agent here that read the skill there (readsSharedHere, plus the
  agent of each such link) is ticked. So each agent keeps the skill, now
  from the library, until the user unticks it or turns it off here.
- A skill found there that isn't in the library yet is imported first.
- A name that can't be handed over is listed in Result.Unmoved, and the
  other names are handed over all the same.
HandBackSkill moves the folder back and links the library to it again.

"Kept there" is now sharedEntry: the skill's own entry in the shared
folder, by its name, or, for a library link into it under another
folder's name, by that folder's name. Everything that read the old
by-name check follows it:
- SkillView.Always, plus the new Shared and SharedElsewhere fields;
- syncSkills' in-shared check;
- the shared folder's own target, which no longer gets a second link
  beside such an entry. TestSharedUnderAnotherName covers this, and it
  fails without the guard.

Where:
- GUI, Library > Skills: a row kept there has "Manage in magpie"
  ("交给 magpie 管理") in its source line. With more than one such row,
  the skills heading has "Manage {n} in magpie". Each asks first, in the
  page, and says:
  - where the folder goes, or that only the link goes;
  - which agents keep the skill, ticked;
  - how to put it back.
  Cancel sends nothing. The strings are in en, zh, zh-TW, ja and de.
- CLI: `magpie library skill hand-over <name…>|--all` and
  `magpie library skill hand-back <name>`.

Existing users: nothing changes until they click. A library.json gains
no new key.

Run (mote, at 58e926e5d/d4b7c0586 rebased on 3142a5559 unless said):
- gofmt -l: clean.
- go vet on darwin, GOOS=linux -tags nogui and GOOS=windows, for
  library, cli and gui. GOOS=linux and GOOS=windows builds.
- go test -tags nogui ./... -timeout 45m, at the pre-rebase base
  a14f5c9c9: 76 packages ok. The one red was internal/gui's
  TestCoreScriptBudget (1531446 > 1530861), which is red on main since
  a4e929212 (#4133). This change adds no core script.
- Windows (win-run, xiaomi-gpu): TestHandOver*, TestSharedUnderAnotherName
  and TestSharedAgentsSkills pass. TestHandOverFailureKeepsOriginal
  skips there, because a read-only folder can still be written.
- GUI: skill-handover.test.cjs in Chromium and WebKit, at 860 and 400px,
  in five languages, plus the heading's all-at-once case. It ran with the
  library skill files and i18n-keys: 84/84.
- Whole GUI suite, on the Windows box (mote was running another
  session's full suite). Nine reds, all in pages this change doesn't
  touch:
  - webkit: agents' sign-ins read, or not at all;
  - disconnect previews use the full body width (chromium and webkit,
    en and zh);
  - webkit zh: an agent's model list;
  - webkit en/ja 1100: an agent's own settings are in its opened row;
  - webkit: a token on a new custom provider with no Balance URL.
  These are NOT yet compared with main. The local disk filled up before
  the run's summary could be read.

- At the head rebased on 4d9058a98 (WSL magpie-desktop): go vet
  -tags nogui on library and cli; TestHandOver*,
  TestSharedUnderAnotherName, TestSharedAgentsSkills and
  TestLibrarySkillHandOver pass; skill-handover and i18n-keys pass 28/28.
  i18n-keys was red there first: the hand-over tooltip "Move these {n}
  out of ~/.agents/skills…" said the usage plugin's word "{n} out".
  It now reads "Move these {n} from ~/.agents/skills…".

Broken by hand:
- Without the ticking, the HandOver tests fail.
- Without syncSkills' guard, TestSharedUnderAnotherName fails ("goose
  was given it twice").
(At 400px the first layout squeezed the path to nothing. The button
now shrinks first, and the test checks the path's width. That check
wasn't run against the old CSS.)

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.
- The skills CLI's lock (npx skills update) may put the entry back.
- No hand-back button in the GUI; the sheet names the CLI command.
- Tried in a sandbox HOME only, never on a real ~/.agents.
2026-10-29 15:52:42 +08:00

295 lines
9.2 KiB
Go

package library
import (
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"slices"
"time"
)
// HandOverSkills gives skills kept in ~/.agents/skills to the library to
// manage (tnt1223 on Discord, "统一移入 magpie 的资源库文件夹,交给 magpie
// 管理"). Several agents read that folder themselves, so a skill there is
// on for each of them whatever the library ticks, and turning it off for
// one, or on this computer alone (OffHere), can't work. Handed over:
// - a folder kept in ~/.agents/skills moves into the library's own folder
// (renamed, or copied, checked the same and only then set aside with
// the backups), and its entry there is gone;
// - an entry there that only links to a folder elsewhere is taken away,
// and the library links to that folder past it;
// - the user's own links to it in the agents' folders, which would point
// at nothing, are taken away too.
//
// Every agent that read it there, or through such a link, is ticked for
// it, so each goes on having it, now from the library, till it's unticked.
// A skill not in the library yet (found in ~/.agents/skills) is brought in
// first. One that can't be handed over stays as it was, in Unmoved, and
// the others are handed over all the same. HandBackSkill undoes it.
func HandOverSkills(names []string) (*Result, error) {
if len(names) == 0 {
return nil, fmt.Errorf("no skills to hand over")
}
names = slices.Compact(slices.Sorted(slices.Values(names)))
var failed, partly []Problem
res, err := change(func(l *Library) error {
var found []FoundSkill
ts := Targets()
for _, name := range names {
left, err := handOver(l, &found, ts, name)
if err != nil {
failed = append(failed, Problem{What: "skill:" + name, Error: err.Error()})
} else if left != "" {
partly = append(partly, Problem{Agent: "agents", What: "skill:" + name, Error: left})
}
}
if len(failed) == len(names) {
return errors.New(failed[0].Error)
}
return nil
})
if res != nil {
res.Unmoved = failed
res.Problems = append(res.Problems, partly...)
}
return res, err
}
// SharedSkills are the names of the library's skills kept in
// ~/.agents/skills, which HandOverSkills can move into the library.
func SharedSkills() ([]string, error) {
mu.Lock()
defer mu.Unlock()
l, err := load()
if err != nil {
return nil, err
}
var out []string
for _, s := range l.Skills {
if sharedHas(s.Name) {
out = append(out, s.Name)
}
}
return out, nil
}
// readsSharedHere are the agents here that read ~/.agents/skills: those
// that read it besides their own folder, and those whose folder it is.
func readsSharedHere(ts []*Target) []string {
sh := realDir(sharedSkillsDir())
var out []string
for _, t := range ts {
if t.Skills != "" && !t.Gone && t.Desktop == nil && (slices.Contains(readsShared, t.Agent.ID) || realDir(t.Skills) == sh) {
out = append(out, t.Agent.ID)
}
}
return out
}
// handOver hands one skill over (HandOverSkills). left says what of the
// original couldn't be taken away after a whole copy was made.
func handOver(l *Library, found *[]FoundSkill, ts []*Target, name string) (left string, err error) {
sh := realDir(sharedSkillsDir())
if l.skill(name) == nil {
if *found == nil {
*found = foundSkills(l)
}
i := slices.IndexFunc(*found, func(f FoundSkill) bool { return f.Name == name })
if i < 0 || (*found)[i].Shared == "" && !within((*found)[i].real, sh) {
return "", fmt.Errorf("no skill called %s in %s", name, sharedSkillsDir())
}
if err := importSkill(l, *found, name); err != nil {
return "", err
}
}
s := l.skill(name)
entry := sharedEntry(name)
if entry == "" {
return "", fmt.Errorf("%s isn't kept in %s", name, sharedSkillsDir())
}
lib := skillDir(name)
r := realDir(lib)
agents := readsSharedHere(ts)
// the user's own links to it in the agents' folders: they'd point at
// nothing once it moves, and the library gives the agent its own
var strays []string
for _, t := range ts {
if t.Skills == "" || t.Desktop != nil || realDir(t.Skills) == sh {
continue
}
q := filepath.Join(t.Skills, name)
if linked(q) && !ours(q, name) && realDir(q) == r {
strays = append(strays, q)
agents = append(agents, t.Agent.ID)
}
}
if within(r, sh) {
// the folder itself is in the shared one: it moves in
if !linked(lib) {
return "", fmt.Errorf("%s is in the library's folder and in %s at once", name, sharedSkillsDir())
}
if left, err = moveIn(r, lib); err != nil {
return "", err
}
if e := filepath.Clean(entry); e != r && linked(e) {
// a link in the shared folder to it there, by its name
if err := os.Remove(e); err != nil && !errors.Is(err, fs.ErrNotExist) {
left = err.Error()
}
}
s.Source = nil
} else {
// the entry only links to a folder elsewhere (or to the library's
// own): the library links past it, and only the link goes
if !linked(entry) {
return "", fmt.Errorf("%s in %s is a folder of its own", name, sharedSkillsDir())
}
if linked(lib) {
via := linkVia(lib, r)
old, _ := os.Readlink(lib)
if filepath.Clean(old) != filepath.Clean(via) {
if err := os.Remove(lib); err != nil {
return "", err
}
if err := dirLink(via, lib); err != nil {
if rerr := dirLink(old, lib); rerr != nil {
return "", fmt.Errorf("%w; and the library's link to %s couldn't be made again: %v", err, old, rerr)
}
return "", err
}
}
s.Source = &Source{Kind: "folder", Dir: via}
}
if err := os.Remove(entry); err != nil {
return "", err
}
}
for _, q := range strays {
os.Remove(q) // a link: what it pointed at is the library's now
}
s.Agents = slices.Compact(slices.Sorted(slices.Values(append(s.Agents, agents...))))
return left, nil
}
// moveIn puts the folder from in the place of the library's link lib to
// it: renamed, or where that can't be (another disk), copied, checked to
// hold the very same files, and only then set aside with the backups. On
// a failure the folder stays where it was and lib links to it as before.
// left is what of from couldn't be taken away after its whole copy was
// in (a file in it held open, on Windows).
func moveIn(from, lib string) (left string, err error) {
tmp := filepath.Join(filepath.Dir(lib), "."+filepath.Base(lib)+".magpie-in-"+time.Now().Format("20060102150405.000"))
renamed := os.Rename(from, tmp) == nil
if !renamed {
if err := copyDir(from, tmp); err != nil {
os.RemoveAll(tmp)
return "", err
}
// a copy that can't be read back the same is no copy: the
// original stays where it is
if !sameTree(from, tmp) {
os.RemoveAll(tmp)
return "", fmt.Errorf("the copy of %s didn't read back the same; it stays where it is", from)
}
}
back := func() {
if renamed {
os.Rename(tmp, from)
} else {
os.RemoveAll(tmp)
}
}
old, err := os.Readlink(lib)
if err != nil {
back()
return "", err
}
if err := os.Remove(lib); err != nil {
back()
return "", err
}
if err := os.Rename(tmp, lib); err != nil {
dirLink(old, lib)
back()
return "", err
}
if renamed {
return "", nil
}
if _, err := setAside("agents", from); err != nil {
var lb *leftBehind
if errors.As(err, &lb) {
// whole in the library and in the backups; what is left of
// it where it was is said
return fmt.Sprintf("moved into the library, but part of %s couldn't be taken away: %v", from, err), nil
}
// nothing of it moved: as it was before
os.RemoveAll(lib)
dirLink(old, lib)
return "", err
}
return "", nil
}
// HandBackSkill puts a skill handed over (HandOverSkills) back into
// ~/.agents/skills: its folder moves there, and the library links to it,
// as it did before. Which agents it's ticked for stays as it is; those
// that read ~/.agents/skills have it again whatever is ticked.
func HandBackSkill(name string) (*Result, error) {
return change(func(l *Library) error {
s := l.skill(name)
if s == nil {
return fmt.Errorf("no skill called %s", name)
}
lib := skillDir(name)
if linked(lib) {
return fmt.Errorf("%s isn't kept in the library's folder: it links to %s", name, realDir(lib))
}
if fi, err := os.Stat(lib); err != nil || !fi.IsDir() {
return fmt.Errorf("%s has no folder in the library: %v", name, err)
}
p := filepath.Join(sharedSkillsDir(), name)
if _, err := os.Lstat(p); err == nil {
if !ours(p, name) {
return fmt.Errorf("%s has a %s of its own", sharedSkillsDir(), name)
}
// magpie's own link there (for Goose, Kimi Code or Cindy)
if err := unlink(p); err != nil {
return err
}
} else if !errors.Is(err, fs.ErrNotExist) {
return err
}
if err := os.MkdirAll(sharedSkillsDir(), 0o755); err != nil {
return err
}
if err := os.Rename(lib, p); err != nil {
if err := copyDir(lib, p); err != nil {
os.RemoveAll(p)
return err
}
if !sameTree(lib, p) {
os.RemoveAll(p)
return fmt.Errorf("the copy of %s didn't read back the same; it stays in the library", name)
}
if _, err := setAside("library", lib); err != nil {
var lb *leftBehind
if !errors.As(err, &lb) {
os.RemoveAll(p)
}
return err
}
}
if err := dirLink(p, lib); err != nil {
if rerr := move(p, lib); rerr != nil {
return fmt.Errorf("%w; the skill is at %s", err, p)
}
return err
}
s.Source = &Source{Kind: "folder", Dir: p}
return nil
})
}