Files
magpie/internal/library/skillcheck.go
T
yetone 3a80ce7fa0 library: reading the Library page no longer reads every byte of the agents' skills each time; a folder unchanged since the last read isn't opened again (#541)
0day404 (#541, Windows): the Library page spins for minutes each time it
is opened. The page waits on GET /api/library, and library.Read, under
the library lock, compared every skill an agent has of its own with the
same name in each other agent byte by byte (foundSkills -> sameTree ->
sameFile) and hashed every library skill given as a copy (behind ->
libHash -> hashDir). Skills with node_modules or templates, given to
several agents, are hundreds of MB per read; on Windows each file opened
is scanned.

What sameTree and hashFiles found is now kept against a stamp of the
folders: each file's and link's path, type, size and mtime (stamper in
treememo.go). The walk that compares or hashes a folder stamps it as it
goes, before opening a file, so a folder seen for the first time is
walked once, as before; a later read walks it without opening a file, and
while the stamps match, the answer is reused. In foundSkills a skill held
by several agents is walked once per read, not once per pair.

What is never taken as unchanged:
- a folder that can't be walked, or a file in it that can't be read
  (compareTrees and sameFile now say whether they read everything): it is
  unknown, not the same, and compared again next time;
- a folder with a file written in the last 3s (a rewrite within a file
  system's time granularity can keep size and mtime);
- any change of a file's size or mtime, or a file added or removed.
A rewrite that keeps both size and mtime (cp -p of a same-size file) is
not seen, as with rsync's quick check. .git, .DS_Store and magpie's mark
aren't part of a stamp, as they aren't part of the compare. hashFiles'
hashes are byte-for-byte what they were (they are kept in library.json
and each copy's mark).

Measured on the Windows box (Windows 11 x64, test binary, 20 library
skills given as copies to 4 agents and 40 own skills in 3 agents, each
300 files of 8KB under node_modules), library.Read:
  before:  5.5-6.3s every read
  after:   5.0s the first read after start, then 2.0s
(Glance, the next commit, is 80ms.) The Mac was under load average ~118
from other work, so its timings weren't comparable.

Tests:
- TestPageReadDoesntReadUnchangedSkillsAgain: aged folders, the second
  read finds codex's copy the same with its files made unreadable; an
  edit afterwards is read again and found to differ.
- TestJustWrittenSkillIsReadAgain: a folder written a moment ago isn't
  taken from the memo.
- TestUnreadableSkillIsntRememberedAsDifferent (scripts/run.sh made
  unreadable; SKILL.md unreadable leaves the skill unlisted).
- TestEditedLibrarySkillIsHashedAgain: a library skill edited to the same
  size and aged an hour after a read: its copies are behind.
- TestHashUnchangedByTheMemo: hashFiles' value for files, a link,
  .DS_Store and .git; passes on the old code too.
Broken by hand, each goes red: stamp without mtime (PageRead,
EditedLibrarySkill), memo never hit (PageRead), unsure compares kept
(Unreadable), unsettled folders kept (JustWritten).

Run: go test -tags nogui ./internal/library ./internal/gui (macOS); the
library package's tests on the Windows box; gofmt; vet on darwin, linux
and windows.
2026-10-10 19:53:02 +08:00

430 lines
12 KiB
Go

package library
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"io/fs"
"net/http"
"net/url"
"os"
"path/filepath"
"slices"
"sort"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/yetone/magpie/internal/source"
)
// ---- which skills GitHub has changed --------------------------------------
// githubAPI is where GitHub's REST API is; a variable for tests.
var githubAPI = "https://api.github.com"
// SkillCheck is what a check for updates found of one skill from GitHub.
type SkillCheck struct {
Name string `json:"name"`
// Status is current when GitHub has the skill as the library does,
// update when it has changed it since, and unknown when it couldn't be
// told (Error says why)
Status string `json:"status"`
Commit string `json:"commit,omitempty"` // the last commit to touch the skill's folder
Date string `json:"date,omitempty"` // when it was made
Message string `json:"message,omitempty"` // its first line
// URL shows what changed: the commits since the one installed, or the
// folder's history when which that was isn't known
URL string `json:"url,omitempty"`
Error string `json:"error,omitempty"`
// Limited is set when it couldn't be told because GitHub's rate limit
// was used up, for the page to say so in its own words
Limited *Limited `json:"limited,omitempty"`
}
// Limited is GitHub's rate limit used up: until when (RFC 3339, "" when
// GitHub didn't say), and whether the requests carried a GitHub token.
type Limited struct {
Until string `json:"until,omitempty"`
Token bool `json:"token,omitempty"`
}
func (e errLimited) view() *Limited {
l := &Limited{Token: e.token}
if !e.until.IsZero() {
l.Until = e.until.UTC().Format(time.RFC3339)
}
return l
}
// The last check's findings stay for the page until a skill is updated,
// installed again or removed.
var checks = struct {
sync.Mutex
m map[string]SkillCheck
}{m: map[string]SkillCheck{}}
func lastCheck(name string) *SkillCheck {
checks.Lock()
defer checks.Unlock()
c, ok := checks.m[name]
if !ok {
return nil
}
return &c
}
func forgetCheck(name string) {
checks.Lock()
delete(checks.m, name)
checks.Unlock()
}
// checkedAt is when the skills were last checked for updates (unix
// nanoseconds), 0 before the first check since magpie started.
var checkedAt atomic.Int64
// checkEvery is how long the Library page goes before it checks for
// updates by itself, as it opens the skills (#1449).
const checkEvery = 12 * time.Hour
// checkDue is whether the page should check the library's skills from
// GitHub for updates: none checked since magpie started, or not for
// checkEvery.
func checkDue(l *Library) bool {
if !slices.ContainsFunc(l.Skills, func(s *Skill) bool { return s.Source != nil && s.Source.Kind == "github" }) {
return false
}
at := checkedAt.Load()
return at == 0 || time.Since(time.Unix(0, at)) >= checkEvery
}
// commit is one commit as GitHub's API lists it.
type commit struct {
SHA string `json:"sha"`
URL string `json:"html_url"`
Commit struct {
Message string `json:"message"`
Committer struct {
Date string `json:"date"`
} `json:"committer"`
} `json:"commit"`
}
// lastCommit is the last commit to touch a folder of a repository on a ref.
// It asks with the ETag of the answer it had before, if any: GitHub's 304
// for an unchanged one doesn't count against the rate limit.
func lastCommit(src Source) (*commit, error) {
q := url.Values{"per_page": {"1"}}
if src.Path != "" {
q.Set("path", src.Path)
}
if src.Ref != "" {
q.Set("sha", src.Ref)
}
u := githubAPI + "/repos/" + src.Repo + "/commits?" + q.Encode()
body, code, token, err := apiGet(u)
if err != nil {
return nil, err
}
switch {
case code == 401 && token:
_, from := GitHubToken()
where := "the GitHub token in Settings → Network and sharing"
if from != "settings" {
where = from + " (no GitHub token is set in Settings → Network and sharing)"
}
return nil, fmt.Errorf("GitHub refused %s: it may have expired or been revoked", where)
case code == 404, code == 422:
if src.Ref != "" {
return nil, fmt.Errorf("GitHub has no %s at %s any more", src.Repo, src.Ref)
}
return nil, fmt.Errorf("GitHub has no repository %s any more", src.Repo)
case code != 200:
return nil, fmt.Errorf("GitHub answered %d %s", code, http.StatusText(code))
}
var list []commit
if err := json.Unmarshal(body, &list); err != nil {
return nil, fmt.Errorf("GitHub's answer wasn't understood: %w", err)
}
if len(list) == 0 {
return nil, fmt.Errorf("%s has nothing at %s any more", src.Repo, src.Path)
}
return &list[0], nil
}
// apiGet asks GitHub's API for u, with the ETag of the answer it had
// before, if any: GitHub's 304 for an unchanged one doesn't count against
// the rate limit, and is answered as the 200 it had. It says whether the
// request carried a GitHub token, and a rate limit used up is an
// errLimited.
func apiGet(u string) ([]byte, int, bool, error) {
req, _ := http.NewRequest("GET", u, nil)
req.Header.Set("User-Agent", "magpie")
req.Header.Set("Accept", "application/vnd.github+json")
token := withGitHubToken(req)
etags.Lock()
had, cached := etags.m[u]
etags.Unlock()
if cached {
req.Header.Set("If-None-Match", had.etag)
}
c := &http.Client{Timeout: 20 * time.Second}
resp, err := source.Do(c, req)
if err != nil {
return nil, 0, token, fmt.Errorf("couldn't reach GitHub: %w", err)
}
defer resp.Body.Close()
body, _ := io.ReadAll(io.LimitReader(resp.Body, 8<<20))
if e, ok := limitedBy(resp, body, token); ok {
return nil, resp.StatusCode, token, e
}
if resp.StatusCode == 304 && cached {
return had.body, 200, token, nil
}
if et := resp.Header.Get("ETag"); resp.StatusCode == 200 && et != "" {
etags.Lock()
etags.m[u] = etagged{etag: et, body: body}
etags.Unlock()
}
return body, resp.StatusCode, token, nil
}
// hashDir is a hash of a skill's files, their names and what's in them,
// as copyDir copies them.
func hashDir(dir string) string { return hashFiles(dir, false) }
// hashFiles is hashDir, leaving aside the .DS_Store files Finder leaves in
// a folder it shows when noFinder is set. A folder unchanged since it was
// last hashed isn't read again (treememo.go).
func hashFiles(dir string, noFinder bool) string {
return rememberedHash(dir, noFinder)
}
// hashWalk walks dir as hashFiles reads it, stamping each file and link
// it would hash (treememo.go); with read, it also hashes their bytes. ok
// is false when it can't be walked, or a file can't be read.
func hashWalk(dir string, noFinder, read bool) (hash, stamp string, settled, ok bool) {
h := sha256.New()
st := newStamper()
err := filepath.WalkDir(dir, func(p string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
rel, _ := filepath.Rel(dir, p)
rel = filepath.ToSlash(rel)
switch {
case d.IsDir():
if d.Name() == ".git" && p != dir {
return filepath.SkipDir
}
case d.Type()&fs.ModeSymlink != 0:
fi, err := d.Info()
if err != nil {
return err
}
st.add(rel, fi)
if read {
t, _ := os.Readlink(p)
fmt.Fprintf(h, "link %s %s\n", rel, filepath.ToSlash(t))
}
case d.Type().IsRegular() && rel != marker && !(noFinder && d.Name() == ".DS_Store"):
fi, err := d.Info()
if err != nil {
return err
}
st.add(rel, fi)
if !read {
return nil
}
f, err := os.Open(p)
if err != nil {
return err
}
defer f.Close()
fi, _ = f.Stat()
fmt.Fprintf(h, "file %s %d\n", rel, fi.Size())
if _, err := io.Copy(h, f); err != nil {
return err
}
}
return nil
})
if err != nil {
return "", "", false, false
}
stamp, settled = st.done()
if read {
hash = hex.EncodeToString(h.Sum(nil))
}
return hash, stamp, settled, true
}
func firstLine(s string) string {
s, _, _ = strings.Cut(strings.TrimSpace(s), "\n")
s = strings.TrimSpace(s)
if r := []rune(s); len(r) > 120 {
s = string(r[:119]) + "…"
}
return s
}
// CheckSkills asks GitHub, for every skill installed from there, for the
// last commit to touch its folder, and tells whether it's newer than what
// the library has: by the commit a skill is known to be at, or, while
// that isn't known yet, by fetching the repository once and comparing the
// folder's files with the skill's. A skill GitHub couldn't be asked about
// is unknown, and the others are checked all the same; once GitHub limits
// requests, the rest aren't asked about.
func CheckSkills() ([]SkillCheck, error) {
mu.Lock()
l, err := load()
mu.Unlock()
if err != nil {
return nil, err
}
byRepo := map[string][]*Skill{}
for _, s := range l.Skills {
if s.Source != nil && s.Source.Kind == "github" {
byRepo[s.Source.Repo] = append(byRepo[s.Source.Repo], s)
}
}
f := &fetcher{}
defer f.clean()
var (
out []SkillCheck
known = map[string][2]string{} // name: the commit found, and the hash it was found by
omu sync.Mutex
wg sync.WaitGroup
sem = make(chan struct{}, 4)
limited atomic.Pointer[errLimited]
)
for _, skills := range byRepo {
wg.Add(1)
go func() {
defer wg.Done()
sem <- struct{}{}
defer func() { <-sem }()
asked := map[string]*commit{} // a repository's skills in one folder are asked about once
refs := []string{}
for _, s := range skills {
c, sha, hash := checkSkill(s, f, asked, &limited)
omu.Lock()
out = append(out, c)
if sha != "" {
known[s.Name] = [2]string{sha, hash}
}
omu.Unlock()
if !slices.Contains(refs, s.Source.Ref) {
refs = append(refs, s.Source.Ref)
}
}
// and what the repository has added beside them since
for _, ref := range refs {
if found, ok := findNew(l, skills[0].Source.Repo, ref, f, &limited); ok {
newFound.Lock()
newFound.m[repoKey(skills[0].Source.Repo, ref)] = found
newFound.Unlock()
}
}
}()
}
wg.Wait()
sort.Slice(out, func(i, j int) bool { return out[i].Name < out[j].Name })
if len(known) > 0 {
// what the check learnt is kept, for the next to ask GitHub only
// for the last commit; a skill updated meanwhile is left alone
mu.Lock()
if l, err := load(); err == nil {
for _, s := range l.Skills {
k, ok := known[s.Name]
if ok && s.Source != nil && s.Source.Kind == "github" && s.Commit == "" && (s.Hash == "" || s.Hash == k[1]) {
s.Commit, s.Hash = k[0], k[1]
}
}
_ = l.save()
}
mu.Unlock()
}
checks.Lock()
for _, c := range out {
checks.m[c.Name] = c
}
checks.Unlock()
checkedAt.Store(time.Now().UnixNano())
if out == nil {
out = []SkillCheck{}
}
return out, nil
}
// checkSkill checks one skill; when its files were found to be GitHub's,
// it gives back the commit they're at and their hash, for the library to
// keep.
func checkSkill(s *Skill, f *fetcher, asked map[string]*commit, limited *atomic.Pointer[errLimited]) (SkillCheck, string, string) {
src := *s.Source
c := SkillCheck{Name: s.Name, Status: "unknown"}
key := src.Ref + "\x00" + src.Path
last := asked[key]
if last == nil {
if e := limited.Load(); e != nil {
c.Error, c.Limited = e.Error(), e.view()
return c, "", ""
}
var err error
last, err = lastCommit(src)
if err != nil {
var e errLimited
if errors.As(err, &e) {
limited.Store(&e)
c.Limited = e.view()
}
c.Error = err.Error()
return c, "", ""
}
asked[key] = last
}
c.Commit, c.Date, c.Message = last.SHA, last.Commit.Committer.Date, firstLine(last.Commit.Message)
ref := src.Ref
if ref == "" {
ref = "HEAD"
}
history := "https://github.com/" + src.Repo + "/commits/" + ref
if src.Path != "" {
history += "/" + src.Path
}
if s.Commit != "" {
if s.Commit == last.SHA {
c.Status = "current"
return c, "", ""
}
c.Status, c.URL = "update", "https://github.com/"+src.Repo+"/compare/"+s.Commit+"..."+last.SHA
return c, "", ""
}
// which commit it's at isn't known: its files are compared with GitHub's
tmp, err := f.fetch(src)
if err != nil {
c.Error = err.Error()
return c, "", ""
}
from := filepath.Join(tmp, filepath.FromSlash(src.Path))
if _, ok := readMeta(from); !ok {
c.Error = fmt.Sprintf("%s has no SKILL.md at %s any more", src.Repo, src.Path)
return c, "", ""
}
have := s.Hash
if have == "" {
have = hashDir(skillDir(s.Name)) // installed before magpie kept it
}
if theirs := hashDir(from); theirs == "" || theirs != have {
c.Status, c.URL = "update", history
return c, "", ""
}
c.Status = "current"
return c, last.SHA, have
}