An unreadable subfolder in an agent's copy of a skill hid edits from the timestamp scan, which ignored read and stat errors. So switching copy/link mode, refreshing the skill, or removing a duplicate copy could replace the copy and lose those edits. Shared-folder deduplication could remove SKILL.md before failing on the unreadable subtree. changedAt and copyIsFresh now return the scan error. The copy is kept in place for that sync (Library.blockedSkills) and reported as a Problem naming the path. This covers agent copies, shared-folder deduplication, project copies and Claude Desktop copies. Other agents still sync, and a later sync tries again once the copy can be read. Merged onto main. #1449 added two callers of changedAt in skill_changed.go: - libraryHash doesn't remember a hash for a folder it couldn't wholly read. - editState says unknown for an agent's copy it couldn't wholly read, so an update from GitHub keeps the library's version aside. TestEditStateUnknownForAnUnreadableCopy covers editState. docs/subsystems/library.md keeps both new rules. Run: - go test -tags nogui ./internal/library/ and ./... (see the PR), go vet for darwin, linux with nogui, and windows, and gofmt. - With changedAt ignoring walk errors again, the four ScanError and EditState tests go red. - With editState ignoring the error, the new test goes red (edited true, known true). - Checked with Unix permissions on macOS only, not as root and not on Windows.
127 lines
3.7 KiB
Go
127 lines
3.7 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"
|
|
"slices"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
// EditedError is an update refused because the skill was changed here
|
|
// since it was fetched from GitHub.
|
|
type EditedError struct{ Name string }
|
|
|
|
func (e *EditedError) Error() string {
|
|
return fmt.Sprintf("%s was changed here since it was fetched from GitHub; updating it would replace those changes", e.Name)
|
|
}
|
|
|
|
// 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 == nil || s.Source.Kind != "github" || 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 || !slices.Contains(s.Agents, 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)
|
|
}
|