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.
140 lines
4.0 KiB
Go
140 lines
4.0 KiB
Go
package library
|
|
|
|
// A skill from GitHub can be changed on this machine after it was
|
|
// installed: the user edits it in the library's folder, an agent it is
|
|
// linked into edits it there, or an agent's copy is edited and taken in
|
|
// (skill_edits.go). An update from GitHub takes its files in place of the
|
|
// library's, so it would undo such a change without a word (#1449). So
|
|
// the library tells a changed skill by the hash of its files as fetched
|
|
// (Skill.Hash): the page says it was changed here, an update of it alone
|
|
// asks first, an update of many leaves it as it is, and the version an
|
|
// update replaces is kept with the backups unless it is known to be
|
|
// GitHub's own.
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
// EditedError is an update refused because the skill was changed here
|
|
// since it was fetched from GitHub, or From, the git repository it came
|
|
// from.
|
|
type EditedError struct{ Name, From string }
|
|
|
|
func (e *EditedError) Error() string {
|
|
from := e.From
|
|
if from == "" {
|
|
from = "GitHub"
|
|
}
|
|
return fmt.Sprintf("%s was changed here since it was fetched from %s; updating it would replace those changes", e.Name, from)
|
|
}
|
|
|
|
// editedError is the refusal of an update of s, naming where it came from.
|
|
func editedError(s *Skill) *EditedError {
|
|
e := &EditedError{Name: s.Name}
|
|
if s.Source != nil && s.Source.Kind == "git" {
|
|
e.From = s.Source.Repo
|
|
}
|
|
return e
|
|
}
|
|
|
|
// edits remembers, for each skill's library folder, its hash as of when
|
|
// anything in it last changed, so a page read doesn't read every skill's
|
|
// files again.
|
|
var edits = struct {
|
|
sync.Mutex
|
|
m map[string]editMemo
|
|
}{m: map[string]editMemo{}}
|
|
|
|
type editMemo struct {
|
|
at time.Time
|
|
hash string
|
|
}
|
|
|
|
// libraryHash is the hash of the library's folder of a skill, leaving
|
|
// aside what Finder keeps in it: "" when it can't be read.
|
|
func libraryHash(name string) string {
|
|
dir := skillDir(name)
|
|
// a folder not wholly read has no time to remember its hash by
|
|
at, err := changedAt(dir)
|
|
if err != nil {
|
|
at = time.Time{}
|
|
}
|
|
edits.Lock()
|
|
m, ok := edits.m[dir]
|
|
edits.Unlock()
|
|
if ok && !at.IsZero() && m.at.Equal(at) {
|
|
return m.hash
|
|
}
|
|
h := hashFiles(dir, true)
|
|
if h != "" && !at.IsZero() {
|
|
edits.Lock()
|
|
edits.m[dir] = editMemo{at: at, hash: h}
|
|
edits.Unlock()
|
|
}
|
|
return h
|
|
}
|
|
|
|
// editState tells whether a skill from GitHub was changed here since it
|
|
// was fetched: its library folder no longer holds the files fetched, or
|
|
// an agent's copy of it was edited since magpie made it and isn't taken
|
|
// in yet. known is false when that can't be told: magpie didn't keep the
|
|
// hash of what it fetched (installed before it did), or the folder can't
|
|
// be read.
|
|
func (l *Library) editState(s *Skill, targets []*Target) (edited, known bool) {
|
|
if !s.Source.remote() || s.Hash == "" {
|
|
return false, false
|
|
}
|
|
lib := skillDir(s.Name)
|
|
if linked(lib) {
|
|
return false, false
|
|
}
|
|
h := libraryHash(s.Name)
|
|
if h == "" {
|
|
return false, false
|
|
}
|
|
// the fetched files may hold a .DS_Store of their own
|
|
if h != s.Hash && hashDir(lib) != s.Hash {
|
|
return true, true
|
|
}
|
|
for _, t := range targets {
|
|
if t.Skills == "" || t.Desktop != nil || !s.gives(t.Agent.ID) {
|
|
continue
|
|
}
|
|
p := filepath.Join(t.Skills, s.Name)
|
|
if !ours(p, s.Name) || linked(p) {
|
|
continue
|
|
}
|
|
made, ok := copiedAt(p)
|
|
if !ok {
|
|
continue
|
|
}
|
|
// a copy not wholly read can't be told unchanged; the sync keeps
|
|
// it in place, and an update keeps the library's version aside
|
|
at, err := changedAt(p)
|
|
if err != nil {
|
|
return false, false
|
|
}
|
|
if !at.After(made) {
|
|
continue
|
|
}
|
|
if !sameTree(p, lib) {
|
|
return true, true
|
|
}
|
|
}
|
|
return false, true
|
|
}
|
|
|
|
// keepReplaced moves the library's version of a skill an update replaces
|
|
// to the backups, where it is found as an edit taken over is.
|
|
func keepReplaced(name, old string) (string, error) {
|
|
dst := filepath.Join(BackupDir(), time.Now().Format("2006-01-02_15-04-05.000"), "library", "skills", name)
|
|
if err := os.MkdirAll(filepath.Dir(dst), 0o700); err != nil {
|
|
return "", err
|
|
}
|
|
return dst, move(old, dst)
|
|
}
|