Files
magpie/internal/gui/update_wait.go
T
yetone 257eab646d update: a click that updates takes the newest the feed has, not the last check's
coeo91 on Discord: "更新行为不对,他会在点击更新按钮后下载上一次或最近一次
获取到的最新版本,而不是实际的最新版本". Clicking Restart to update asked the
feed again, but four paths still acted on what an earlier check had found:

- With Settings' "Download updates automatically" off, the click's check
  found the newer version and kept the staged older one (the hold), so the
  restart went into the older one. The click's check now counts as a click
  (fetch), so the newest is downloaded, then restarted into.
- When the feed took longer than the 10s wait, the route restarted into
  the staged version anyway. It now answers "checking". The header pill
  reads again and restarts once the answer is in; About's Version row
  redraws, and a second click restarts into what the answer found.
- The tray's Restart to update didn't ask the feed at all. It now goes
  through restartAsked: it checks first, or waits for the gateway as the
  page's click does (#577).
- A magpie that can't replace itself opened the release page the last
  check found. When that check is over a minute old, it checks again first.

A current version that is already downloaded is not downloaded again:
the check finds the same version and keeps it. The wait on the feed is
the one the click had already (at most 10s, usually one round trip).

Not changed: usemagpie.ai/api/latest is cached at the edge for 5 minutes
(site/worker.js), so a release less than 5 minutes old may still be
unseen. magpie web's "Open the release page" opens the URL the page
holds, since window.open must run in the click for the popup blocker.

Tests (internal/gui/update_fresh_test.go, a fake feed, no network):
TestRestartClickTakesTheNewest, TestRestartWaitsForTheFeed,
TestTrayRestartTakesTheNewest, TestAvailableOpensTheNewestPage.

Run on mote (macOS 27), at this tree:
- gofmt -l internal/gui: nothing
- go vet ./internal/gui; GOOS=linux go vet -tags nogui; GOOS=windows
  go vet: clean
- go test -tags nogui -count=1 -run 'TakesTheNewest|WaitsForTheFeed|
  NewestPage|Update|Restart' ./internal/gui: ok
- the same five with -race -count=10, with RestartWhenIdle and
  RestartToUpdate: ok

Broken by hand, one at a time, each red in its own test only:
- recheck's fetch = false: TestRestartClickTakesTheNewest, "went into the
  version the last check found: ... Latest:0.1.11"
- the "checking" answer taken out: TestRestartWaitsForTheFeed,
  "restarted before the feed answered: {State:ready ... 0.1.11}"
- restartAsked restarting at once: TestTrayRestartTakesTheNewest, "the
  tray restarted into 0.1.11 with 0.1.12 out"
- the available recheck taken out: TestAvailableOpensTheNewestPage,
  opened [v0.1.11 v0.1.11]

Not tried: a real update from a published release; the tray item on
Windows and Linux (the code is shared, the menu callback runs in its own
goroutine on Wails v3 beta.24).
2026-10-28 23:22:14 +08:00

213 lines
5.7 KiB
Go

package gui
import (
"time"
"github.com/yetone/magpie/internal/gateway"
"github.com/yetone/magpie/internal/settings"
)
// A restart to update ends the gateway this process serves, and with it
// whatever the agents have in flight through it (#577: an agent's reply
// streaming, a Claude Code turn waiting on its caller's tool results). On
// a Mac or Linux it hands the gateway over instead (handover.go), and only
// the Claude subscription's turns would be cut short (restartBusy). So a
// restart asked for while those are busy waits until they are idle:
// nothing in flight, no turn waiting on tools, and nothing ended in the
// last idleSettle — the gap of a tool chain, an agent running a tool
// between one request and the next. It waits waitMost at most; then the
// update stays downloaded, put in on quitting or at the next restart, and
// a newer version out meanwhile is the one restarted into. "Restart now"
// restarts at once, whatever is in flight.
var (
idleSettle = 10 * time.Second
waitMost = time.Hour
waitTick = time.Second
)
// gatewayBusy is what the gateway this process serves has in flight;
// nothing when another magpie serves it, which a restart here leaves be.
var gatewayBusy = func() gateway.Busy {
if gw := served.Load(); gw != nil {
return gw.Busy()
}
return gateway.Busy{}
}
// waitRecheck asks the feed again before the restart (updater.recheck),
// asked when a click armed the wait.
var waitRecheck = (*updater).recheck
// idleAt is whether b leaves nothing for a restart at now to cut short.
func idleAt(b gateway.Busy, now time.Time) bool {
return !b.Any() && now.Sub(b.Last) >= idleSettle
}
// idle is whether the gateway is idle now.
func (u *updater) idle() bool { return idleAt(restartBusy(), time.Now()) }
// waitIdle has restart run once the gateway is idle; a wait under way
// takes the newer restart (the window as it is now). asked is a click's
// (Restart to update), not Settings' AutoRestart's.
func (u *updater) waitIdle(restart func() bool, asked bool) {
u.mu.Lock()
u.whenIdle, u.gaveUp, u.waitAsked = restart, false, asked
if u.waiting {
u.mu.Unlock()
return
}
u.waiting, u.waitFrom = true, time.Now()
u.waitGen++
gen := u.waitGen
u.mu.Unlock()
u.told()
u.loops.Add(1)
go func() {
defer u.loops.Done()
u.waitFor(gen)
}()
}
// cancelWait stops a restart waiting.
func (u *updater) cancelWait() {
u.mu.Lock()
was := u.waiting
u.waiting, u.whenIdle = false, nil
u.waitGen++
u.mu.Unlock()
if was {
u.told()
}
}
// waitingFor is whether a restart waits, and on what.
func (u *updater) waitingFor() (bool, gateway.Busy) {
u.mu.Lock()
w := u.waiting
u.mu.Unlock()
if !w {
return false, gateway.Busy{}
}
return true, restartBusy()
}
func (u *updater) told() {
u.mu.Lock()
f := u.onWait
u.mu.Unlock()
if f != nil {
go f() // off the caller's thread: the tray's relabel runs on the main one
}
}
// waitFor restarts once the gateway is idle and the version downloaded is
// the latest, unless the wait is called off (gen no longer its) or runs out.
func (u *updater) waitFor(gen int) {
var last gateway.Busy
for {
time.Sleep(waitTick)
u.mu.Lock()
if !u.waiting || u.waitGen != gen {
u.mu.Unlock()
return
}
if time.Since(u.waitFrom) >= waitMost {
u.waiting, u.gaveUp, u.whenIdle = false, true, nil
u.mu.Unlock()
u.told()
return
}
u.mu.Unlock()
b := restartBusy()
if b.Requests != last.Requests || b.Tools != last.Tools {
last = b
u.told()
}
if !idleAt(b, time.Now()) {
continue
}
switch u.json().State {
case "ready":
// a newer version out since is downloaded first, and waited for
u.mu.Lock()
asked := u.waitAsked
u.mu.Unlock()
waitRecheck(u, asked)
if u.asking() || u.json().State != "ready" {
continue
}
case "checking", "downloading":
continue
default:
// nothing left to restart into (a newer one failed to download)
u.cancelWait()
return
}
u.mu.Lock()
if !u.waiting || u.waitGen != gen {
u.mu.Unlock()
return
}
restart := u.whenIdle
u.waiting, u.whenIdle = false, nil
u.mu.Unlock()
u.told()
if restart != nil {
restart()
}
return
}
}
// autoTick, with Settings' AutoRestart on (and automatic updates), has a
// downloaded update restarted into once the gateway is idle (#2096). The
// wait is waitIdle's, so a restart never cuts a request short; one that
// ran out (gaveUp) is waited for again on the next tick, and one the user
// called off isn't, for that version.
func (u *updater) autoTick(s settings.Settings) {
if s.NoAutoUpdate || !s.AutoRestart {
return
}
u.mu.Lock()
restart, waiting, skip := u.autoRestart, u.waiting, u.autoSkip
u.mu.Unlock()
if restart == nil || waiting {
return
}
if j := u.json(); j.State != "ready" || j.Latest == "" || j.Latest == skip {
return
}
u.waitIdle(restart, false)
}
// restartAsked is the tray's Restart to update, restart doing it. With the
// gateway busy it waits for it to be idle (#577), and a click while it
// waits restarts at once. The feed is asked first, as the page's restart
// asks it (coeo91 on Discord: the tray restarted into the version the
// last check found): a newer version out since is downloaded, and the
// restart waits for it.
func (u *updater) restartAsked(restart func() bool) {
if waiting, _ := u.waitingFor(); !waiting && !u.idle() {
u.waitIdle(restart, true)
return
}
u.cancelWait()
u.recheck(true)
if u.asking() || u.json().State != "ready" {
u.waitIdle(restart, true)
return
}
restart()
}
// callOff is the user's Cancel of a restart waiting: the wait stops, and
// AutoRestart leaves the version it was for alone.
func (u *updater) callOff() {
u.mu.Lock()
if u.waiting && u.latest != nil {
u.autoSkip = u.latest.Version
}
u.mu.Unlock()
u.cancelWait()
}