Files
magpie/internal/library/skill_how.go
T
yetone fe05f0a721 library: a skill can be turned off on this computer alone, and a sync neither carries that nor turns it on
hanslin1666 on Discord (#general 1565016936344191096) wanted a library
skill off on one computer only ("只在这台电脑上关掉"). The only way to do
that was to untick its agents, and the ticks are what a sync carries, so
every other computer lost it too. A sync from a computer that had it on
also gave it back here.

Skill.OffHere (library.json "offHere", omitempty) turns a skill off on
this computer. No agent here gets it, whatever Agents says. Every reader
that gives a skill to an agent now asks gives():
- syncSkills, including the shared ~/.agents/skills check;
- desktop_skills.go;
- skill_changed.go;
- skill_edits.go;
- behind;
- Targets.
Agents stays what a sync carries, and the page shows those chips faded.

Collect never carries OffHere, so turning a skill off changes no part a
sync uploads. Put keeps this computer's OffHere for a skill it puts, so
a sync from a computer that has it on (or that gives it to more agents)
leaves it off here.

Where to turn it off:
- GUI: a power button on the skill row (aria-pressed). When off, an
  "Off on this computer" tag sits on its own line under the name; it
  says why and turns the skill on when clicked.
- CLI: `magpie library skill off-here|on-here <name>`.
- TUI: the row says the skill is off.
The strings are in en, zh, zh-TW, ja and de.

Existing users: nothing changes. An older library.json has no offHere,
so every skill stays on, and a skill that was never turned off writes no
key (TestSkillOffHereOldLibraryIsOn).

Not covered:
- a project's skills;
- a library skill whose own folder is inside ~/.agents/skills, which
  agents read in place;
- a skill another computer removes (the flag goes with it).
The skill's files in the library folder still update while it is off.

Docs: docs/subsystems/library.md and sync-backup.md.

Run at the parent aeacc6358 (rebased on origin/main), on mote:
- gofmt -l: clean.
- go vet ./...; GOOS=linux go vet -tags nogui ./...; GOOS=windows go
  vet ./...; go build ./...: ok.
- go test -tags nogui -timeout 45m ./internal/library ./internal/davsync
  ./internal/cli ./internal/tui ./internal/gui ./internal/backup: all ok.
- GUI, Chromium and WebKit:
  - skill-off-here: 22/22 (en, zh, zh-TW, ja, de at 860 and 400px).
  - i18n-keys, gui-ja, gui-de, gui-zh-tw, library-items, library-mcp-hub,
    library-all and library-toggle-status: green. The first try timed out
    in browserType.launch (mote at load ~60); run again one file at a
    time, they passed.
  - library-row-remove: Chromium red in the hooks part, 2 or 3 of 11
    each run. It is red the same way on origin/main aeacc6358, 3 of 3
    runs there too (#3099); this change doesn't touch hook rows.
- Before the rebase, at e5e2aa58e plus this change: the 98 GUI files
  that reach the library page (1350 tests). 68 reds, nearly all
  browserType.launch timeouts under load; run again one file at a time,
  all passed except mobile-web's "desktop matches origin/main"
  subtests. Those need .git, which mote-run doesn't copy.

Then rebased onto ec3dad75a, which changes no library file. At this
pushed head: go build ./..., go vet of library and gui, the gui Budget
tests, and i18n-keys plus skill-off-here in both engines (26/26), all
ok.

Broken by hand (restored afterwards):
- Put dropping OffHere:
  - TestSkillOffHereStaysHere:96 failed: "a sync from a computer with it
    on gave pdf to an agent here again".
  - TestSkillOffHereThroughSync:108 failed: "a sync from A, with pdf on,
    gave it to an agent on B again".
- gives() ignoring OffHere: TestSkillOffHereStaysHere:74 failed: "off
  here, claude still has pdf".
- The GUI test fails on the old code, which has no button.

Not tried:
- no real WebDAV or GitHub backend (the sync test uses the fake S3);
- no real app run with real agents;
- no Windows or Linux test runs (only GOOS=windows and GOOS=linux vet);
- not the whole GUI suite, only the 98 files that reach the library page,
  the i18n checks and gui-ja, gui-de and gui-zh-tw.
2026-10-29 04:23:33 +08:00

147 lines
4.3 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 })
}
// 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 {
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
}