#2096 asks for a setting that installs and restarts whenever an update is there. Settings > About gets "Restart automatically" (Off/On), off by default: a restart ends the window, and whatever an agent sends during it waits for the gateway to come back. When on, and Automatic updates is on, the updater's minute loop sees a "ready" update and starts the same idle wait the Install-when-idle button uses (waitIdle: 10s with nothing in flight, at most 1h). Cancelling that wait from the GUI skips that version, so the next one is restarted into again. The app (window or tray) and `magpie web` are covered; serve and daemon have no in-process updater and are not. On the Mac, a restart the app made by itself reopens to the menu bar icon only (`open … --args tray`) when no window was shown, rather than popping a window up unasked (update.Relaunch takes a window flag; relaunchOpen builds the command). With Automatic updates off the row is dimmed and reads "While automatic updates are on", like Check every. Run: - go test: internal/gui (-tags nogui, full package, 273s), internal/update, internal/settings, internal/cli — ok, with set -o pipefail. - New: TestAutoRestartWaitsForIdle, TestAutoRestartNeedsAReadyUpdate, TestAutoRestartCalledOff, TestRelaunchOpen. - Broken by hand: without autoTick, WaitsForIdle and CalledOff fail; without the autoSkip assignment, CalledOff fails; restarting without the idle wait fails WaitsForIdle and CalledOff; without prefsKeep's autoRestart, update-restart.test.cjs fails 11 of 11. - gofmt -l clean; go vet and go build for darwin, GOOS=linux -tags nogui and GOOS=windows on internal/gui, update, settings, cli. - GUI: new update-restart.test.cjs, chromium and webkit, 1100 and 420, en/zh/zh-TW/ja/de (22 pass, twice); update-*, i18n-keys and settings* in both engines: 253 pass, 0 fail. - Found on the way, filed as #2134: in WebKit at 420, turning Automatic updates off scrolls a view at its top by ~2px (the Version row's sub grows). The test checks the clicked control holds still instead. Not tried: a real update or relaunch (no real install was touched), the tray-only reopen on a real bundle, Windows/Linux restarts by hand.
186 lines
4.7 KiB
Go
186 lines
4.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). So a
|
|
// restart asked for while the gateway is busy waits until it is 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).
|
|
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(gatewayBusy(), 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).
|
|
func (u *updater) waitIdle(restart func() bool) {
|
|
u.mu.Lock()
|
|
u.whenIdle, u.gaveUp = restart, false
|
|
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, gatewayBusy()
|
|
}
|
|
|
|
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 := gatewayBusy()
|
|
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
|
|
waitRecheck(u)
|
|
if 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)
|
|
}
|
|
|
|
// 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()
|
|
}
|