Files
magpie/internal/library/skill_autoupdate.go
T
yetone 106a24b95e library: skills from GitHub and git update by themselves when switched on
0xYaCo on X: "skill 能自动化升级吗?". magpie already checked for skill
updates by itself (every 12h, as the Skills tab opens), but each update
waited for a click on Update. A new switch, Update by themselves, sits
under the Skills list while a skill comes from GitHub or git
(POST /api/library/skills/auto-update; `magpie library skill auto-update
on|off`; library.json's autoUpdateSkills, this computer's own). It is off
by default. When it is on, KeepSkillsCurrent (a gateway.WhileServing loop)
runs AutoUpdateTick 2 minutes after start and then hourly. Every 12h that
runs CheckSkills, then UpdateSomeSkills on the skills found at "update".
So a skill changed here stays in Unupdated with its Update button, one
removed upstream is only marked, and a replaced edited version goes to the
backups, as with an update by hand. Updated names and a new error are
logged.

Run (WSL2 magpie-desktop, Go 1.26.8; mote was at load 167, mica's disk full),
at ab637d3c4 (this change on 14807bcf2):
- TestAutoUpdateSkills (local git repo fixture, no network) ok.
  Broken by hand (switch and due checks removed): FAIL
  "off: ... Updated:[pdf] ... notes "two"". Restored: ok.
- -race -count=5 TestAutoUpdateSkills|TestInstallFromGitRepo: ok
- gofmt -l: clean; go vet -tags nogui ./... ok; GOOS=windows go vet ./...
  ok; go build -tags nogui, GOOS=windows go build ok
- go test -tags nogui -timeout 45m ./... under a temp HOME: every
  package ok except two reds in untouched packages, each also red alone
  at origin/main 659ca3f02: internal/update
  TestSettleAsksAboutADownloadKeptElsewhere (filed #6648) and
  internal/usage TestRequestStateKeptAcrossACatalogFetchedAgain (#6572).
- GUI: skill-auto-update.test.cjs (en, zh, zh-TW, ja, de; 400px wide;
  Chromium and WebKit), i18n-keys, i18n-en-no-cjk, skill-update-feedback
  and skill-update-edited: 36/36 pass.
Not run: the whole GUI suite had started but not finished at push
(pushed at the coordinator's ask), and nothing was run at the rebased head
e0e8f34a7 (its parents touch lessons/update only). No darwin vet: the
cgo-off cross vet fails in gui's traywindows_darwin_test.go on main too.
Not tried with a real GitHub skill in a live magpie.
2026-11-14 19:09:55 +08:00

114 lines
2.9 KiB
Go

package library
// Skills kept up to date by themselves (0xYaCo on X: "skill 能自动化升级吗?").
// With the switch on (AutoUpdateSkills), the gateway checks the skills
// from GitHub and other git repositories as the Library page does, a
// little after magpie starts and every checkEvery after, and fetches
// again each one the check found changed there. Everything an update by
// hand does holds: a skill changed here is left as it is and keeps its
// Update button (updateSkills refuses it), one removed there is only
// marked, and a replaced version that was changed goes to the backups.
// Off, the default, the page shows what changed and the user updates it.
import (
"context"
"log"
"slices"
"strings"
"sync"
"time"
)
// SetAutoUpdateSkills turns updating the skills by themselves on or off.
func SetAutoUpdateSkills(on bool) (*Result, error) {
return change(func(l *Library) error {
l.AutoUpdateSkills = on
return nil
})
}
const (
// autoUpdateFirst is how long after magpie starts the first look is:
// the start itself has enough to do
autoUpdateFirst = 2 * time.Minute
// autoUpdatePoll is how often the loop asks whether a look is due, so
// switching on is acted on within the hour
autoUpdatePoll = time.Hour
)
// autoUpdate is when the last look by itself was, and its error, said
// once in the log until it changes.
var autoUpdate struct {
sync.Mutex
at time.Time
err string
}
// AutoUpdateTick looks for changed skills and updates them, when the
// switch is on and no look was made for checkEvery. It returns what the
// update did, nil when nothing was due or nothing had changed.
func AutoUpdateTick(now time.Time) (*Result, error) {
mu.Lock()
l, err := load()
mu.Unlock()
if err != nil {
return nil, err
}
if !l.AutoUpdateSkills || !slices.ContainsFunc(l.Skills, func(s *Skill) bool { return s.Source.remote() }) {
return nil, nil
}
autoUpdate.Lock()
due := autoUpdate.at.IsZero() || now.Sub(autoUpdate.at) >= checkEvery
if due {
autoUpdate.at = now
}
autoUpdate.Unlock()
if !due {
return nil, nil
}
checks, err := CheckSkills()
if err != nil {
return nil, err
}
var names []string
for _, c := range checks {
if c.Status == "update" {
names = append(names, c.Name)
}
}
if len(names) == 0 {
return nil, nil
}
return UpdateSomeSkills(names)
}
// KeepSkillsCurrent runs AutoUpdateTick while the gateway serves.
func KeepSkillsCurrent(ctx context.Context) {
t := time.NewTimer(autoUpdateFirst)
defer t.Stop()
for {
select {
case <-ctx.Done():
return
case <-t.C:
}
t.Reset(autoUpdatePoll)
res, err := AutoUpdateTick(time.Now())
autoUpdate.Lock()
msg := ""
if err != nil {
msg = err.Error()
}
if msg != autoUpdate.err {
if msg != "" {
log.Printf("updating skills by themselves: %s", msg)
}
autoUpdate.err = msg
}
autoUpdate.Unlock()
if res != nil && len(res.Updated) > 0 {
log.Printf("skills updated by themselves: %s", strings.Join(res.Updated, ", "))
}
}
}