Removing a skill always moved its folder to ~/.magpie/backups/<time>/, and backups that hold skills are never pruned, so they piled up. The sheet that asks first now has one tick under what it says, "Keep a copy in magpie's backups", ticked unless the reader unticked it last time (localStorage magpie.libSkillBackup). Unticked, the sheet says the folder is deleted and Remove posts noBackup: true. The default is unchanged. What is deleted is only magpie's own folder for the skill, or an agent's own skill folder. Never deleted, either way: - a folder a library skill links to (~/.agents/skills or anywhere else): only the link goes, and there's no tick on its sheet; - an entry in ~/.agents/skills, which other tools read too: it still goes to the backups (inSharedDir), and the sheet has no tick for it; - a skill brought in from the agents: it still goes back to them; - a link inside a skill's folder: RemoveAll takes the link, not its target. Every path that removes skills: - GUI: one skill, Remove all / Remove n, Remove gone, an agent's own skill and several of them (skills/remove, remove-all, remove-gone, remove-found, remove-found-some take noBackup); - CLI: magpie library skill rm <name> --no-backup, and --no-backup on rm --all and rm --gone, whose prompt says so; - TUI: d asks, then D deletes with no backup (d d still keeps a copy); the first d's flash says so, on rows with a folder to delete. library: RemoveSkillsHow, RemoveFoundSkillsHow, RemoveGoneSkillsHow (keep bool); RemoveSkill(s) and RemoveFoundSkill(s) keep a copy as before. The sheets with the tick say they hold nothing to lose (modal.dirty false): toggling it made Cancel ask to discard changes. A page reload with such a sheet open still reads the form as changed (beforeunload reads the core's modalFormDirty); not changed here. Existing backups stay where they are, in ~/.magpie/backups/<time>/skills and <time>/<agent>/skills. Nothing clears them; the Library's Backups button opens the folder. docs/subsystems/library.md says so. Run (mote, this tree at base 9494a38b4; rebased onto848e3b1b2, whose two commits since touch only internal/gateway): - new tests: TestRemoveSkillWithoutBackup, TestRemoveFoundSkillWithout- Backup (library), TestLibrarySkillRmNoBackup (cli), TestLibraryDeleteWithoutBackup (tui): ok. - broken by hand: with keep forced true in RemoveSkillsHow and RemoveFoundSkillsHow, all four are red (folders found in the backups); with the inSharedDir guard taken out, TestRemoveFoundSkillWithoutBackup is red (the shared entry deleted, not backed up). - go test ./internal/library ./internal/cli ./internal/tui ./internal/gui -timeout 45m: library, cli, tui ok. gui: TestBuiltinsTranslated (#7257) and TestSettingsSaveKeepsWhatItDoesNotSend (#7240) red, the same at origin/main9494a38b4without this change. - gofmt -l: nothing. GOOS=linux go vet -tags nogui and GOOS=windows go vet of those four packages: ok. - GUI, chromium and webkit: skill-remove-nobackup (new: en, zh, zh-TW, ja, de at 980 and 440px; tick, its text, the choice remembered, the post, no tick for linked or shared skills, nothing sideways), i18n-keys, i18n-en-no-cjk and the 27 files that remove skills or read these sheets' lines. Red: the new file's Cancel (the dirty-form ask above, fixed and green after), and api-key-theme (#6444), found-skill-pick-remove at 420px (#5013), saved-signedout-remove (#6666): those 18 the same at origin/main9494a38b4. Not run: the full go test ./... and the whole GUI suite. mote and mica were both taken by other sessions' full suites for over an hour. No guard test outside the packages run reads the files changed (grep for docs/subsystems/library.md and plugins/library). Not tried in the real app or on Windows or Linux; a Windows junction to a skill folder goes through the same linked() branch as on macOS.
160 lines
4.5 KiB
Go
160 lines
4.5 KiB
Go
package library
|
|
|
|
// A skill its source no longer has (スライム on Discord): the repository
|
|
// it came from was read, and its folder isn't there any more. An update
|
|
// then has nothing to fetch, which is no failure of the update: the skill
|
|
// is marked removed from its source (Skill.Gone), for the page to say so
|
|
// and offer to take out every such skill of a source at once. Nothing is
|
|
// taken out by itself, as the user may keep using it. Only a source read
|
|
// whole tells: a fetch that failed, or a skill left out of a fetch as too
|
|
// big, says nothing of whether it is still there.
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io/fs"
|
|
"os"
|
|
"path/filepath"
|
|
"slices"
|
|
"syscall"
|
|
"time"
|
|
)
|
|
|
|
// GoneError is a skill its source was read without: it was removed there.
|
|
type GoneError struct {
|
|
Name string
|
|
Source string // the repository
|
|
}
|
|
|
|
func (e *GoneError) Error() string {
|
|
return fmt.Sprintf("%s has no %s any more: it was removed from there", e.Source, e.Name)
|
|
}
|
|
|
|
// goneFrom is whether a fetched source folder, tmp, lacks the skill at
|
|
// path: neither the folder nor its SKILL.md is there. A skill the fetch
|
|
// left out as too big is there, only not fetched; a folder that can't be
|
|
// read is unknown, not gone.
|
|
func goneFrom(tmp, path string) bool {
|
|
if leftOutAt(tmp, path) {
|
|
return false
|
|
}
|
|
dir := filepath.Join(tmp, filepath.FromSlash(path))
|
|
fi, err := os.Stat(dir)
|
|
if err != nil {
|
|
return absent(err)
|
|
}
|
|
if !fi.IsDir() {
|
|
return true // a file stands where the skill's folder was
|
|
}
|
|
_, err = os.Stat(filepath.Join(dir, "SKILL.md"))
|
|
return err != nil && absent(err)
|
|
}
|
|
|
|
// absent is whether err says a path isn't there, rather than that it
|
|
// couldn't be read.
|
|
func absent(err error) bool {
|
|
return errors.Is(err, fs.ErrNotExist) || errors.Is(err, syscall.ENOTDIR)
|
|
}
|
|
|
|
// leftOutAt is whether the fetch into dir left out the skill at path, or
|
|
// one inside it, as too big; unlike tookLeftOut it forgets nothing.
|
|
func leftOutAt(dir, path string) bool {
|
|
leftOut.Lock()
|
|
defer leftOut.Unlock()
|
|
for _, l := range leftOut.m[dir] {
|
|
if path == "" || l.Path == path || len(l.Path) > len(path) && l.Path[:len(path)+1] == path+"/" {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// markGone records which skills their sources were found without, and
|
|
// which were found there again.
|
|
func markGone(gone, there []string) {
|
|
if len(gone) == 0 && len(there) == 0 {
|
|
return
|
|
}
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
l, err := load()
|
|
if err != nil {
|
|
return
|
|
}
|
|
if l.setGone(gone, there) {
|
|
_ = l.save()
|
|
}
|
|
}
|
|
|
|
// setGone is markGone on a library already loaded; it says whether
|
|
// anything changed.
|
|
func (l *Library) setGone(gone, there []string) bool {
|
|
now := time.Now().UTC().Format(time.RFC3339)
|
|
changed := false
|
|
for _, s := range l.Skills {
|
|
switch {
|
|
case slices.Contains(gone, s.Name) && s.Gone == "":
|
|
s.Gone, changed = now, true
|
|
case slices.Contains(there, s.Name) && s.Gone != "":
|
|
s.Gone, changed = "", true
|
|
}
|
|
}
|
|
return changed
|
|
}
|
|
|
|
// goneCheck is the check a skill found removed from its source keeps
|
|
// till it is updated, installed again or removed.
|
|
func goneCheck(e *GoneError) SkillCheck {
|
|
return SkillCheck{Name: e.Name, Status: "gone", Error: e.Error()}
|
|
}
|
|
|
|
// RemoveGoneSkills takes out of the library, as RemoveSkills does, the
|
|
// skills named that their sources no longer have; with none named, every
|
|
// such skill. One found in its source again since is left, and said in
|
|
// Unremoved.
|
|
func RemoveGoneSkills(names []string) (*Result, error) {
|
|
return RemoveGoneSkillsHow(names, true)
|
|
}
|
|
|
|
// RemoveGoneSkillsHow is RemoveGoneSkills, with keep false deleting their
|
|
// folders rather than moving them to the backups (RemoveSkillsHow).
|
|
func RemoveGoneSkillsHow(names []string, keep bool) (*Result, error) {
|
|
mu.Lock()
|
|
l, err := load()
|
|
mu.Unlock()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var take []string
|
|
var left []Problem
|
|
if names == nil {
|
|
for _, s := range l.Skills {
|
|
if s.Gone != "" {
|
|
take = append(take, s.Name)
|
|
}
|
|
}
|
|
} else {
|
|
for _, name := range slices.Compact(slices.Sorted(slices.Values(names))) {
|
|
switch s := l.skill(name); {
|
|
case s == nil:
|
|
left = append(left, Problem{What: "skill:" + name, Error: "no skill called " + name})
|
|
case s.Gone == "":
|
|
left = append(left, Problem{What: "skill:" + name, Error: name + " is in its source again; it's kept"})
|
|
default:
|
|
take = append(take, name)
|
|
}
|
|
}
|
|
}
|
|
if len(take) == 0 {
|
|
if len(left) > 0 {
|
|
return nil, errors.New(left[0].Error)
|
|
}
|
|
return nil, fmt.Errorf("no skill is removed from its source")
|
|
}
|
|
res, err := RemoveSkillsHow(take, keep)
|
|
if res != nil {
|
|
res.Unremoved = append(res.Unremoved, left...)
|
|
}
|
|
return res, err
|
|
}
|