visamzh on X: with the desktop app and magpie web both open, an update
from either side left the other still offering it ("↑ 更新") until it was
quit by hand.
Cause: the app and magpie web are two processes over one program, each
with its own updater (state, download, latest) in memory. One restarting
to update puts the new binary in; the other is told nothing. Its
replaced() (the binary on disk is no longer the one it started from) was
read only on a click of its own Update or on magpie update's gateway ask
(restartBeside), which reaches only the process serving the gateway. And
a browser's page reloaded (backAsNew) only after a restart it asked for
itself, so magpie web's tab kept the old page and its pill even once
the process under it was new.
Fix:
- followReplaced (update_beside.go): every updater stats its binary each
5 s and, once another magpie replaced it, restarts into it through
restartBeside, once: at once when idle, else after the gateway's
requests (waitIdle), as for magpie update's ask. Not for what this
process put in itself (installed, set by install, cleared by
wontStart), not twice (followed), not for a build that isn't a release
(make dev, go build rebuild in place). It stops with backendCtx, so a
copy left draining after a handover doesn't look.
- waitFor (update_wait.go): a wait for the gateway restarts into what
another magpie put in whatever the feed says now; before, a feed out
of reach or a "latest" answer ended the wait with nothing restarted.
- boot.js names the version that served the page (bootPrefs.version);
app.js's servedAnew reloads a browser's page (web) when /api/update's
current names another, on the reads it already made (load, focus, back
into view) and once a minute while shown. The app's own windows come
back with their new process and are left alone.
- docs/subsystems/gui-shell.md describes it.
Run (mote, through mote-run, at 1e1068c86 + this change unless said):
- gofmt -l internal/gui: nothing.
- go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet
./..., go build, GOOS=linux go build -tags nogui, GOOS=windows go
build: all ok.
- go test -tags nogui -race -count=5 -run
'Follow|AnotherMagpies|OwnUpdate|OwnBuild|UpdateClicked|Wait|Beside|
Restart|Install|WhatsNew|Quiet' ./internal/gui/: ok (102.9s).
- go test -tags nogui -timeout 45m ./internal/gui/... ./internal/cli/...
./internal/update/... . : gui, update, update/launcher and magpie ok;
internal/cli red in TestEveryRouteHasACommandOrIsNamed (GET
/api/context/reply has no command), red the same on origin/main
51fa4b7eb alone, filed as #5026 (from 0872bc2a0).
- node --test internal/gui/tests/update-served-anew.test.cjs: 34/34
(Chromium and WebKit; en, zh, zh-TW, ja, de; 1100 and 420 wide; the
minute poll under page.clock; the app's window not reloaded).
Broken by hand, each red, then put back:
- followReplaced never restarting: TestAnotherMagpiesUpdateRestartsThisOne
and TestAnotherMagpiesUpdateWaitsForTheGatewayThenRestarts red.
- the installed guard dropped: TestOwnUpdateIsNotFollowedAgain and
TestUpdateClickedOverAnothersIsNotFollowedAgain red (a second restart).
- waitFor's "in" case off: TestAnotherMagpiesUpdateWaitsForTheGateway
ThenRestarts red (the wait ends with nothing restarted).
- boot.js without the version: TestBootNamesTheVersionThatServedThePage
red.
- servedAnew not reloading: 15 of 16 subtests red in Chromium.
- the minute poll removed: the 5 "left on screen" subtests red in WebKit.
A first cut also refused a second install() once one was in; that made
TestRestartBySelfComesBackQuietly and TestWhatsNewQuietAfterRestartTo
Update red (they restart one updater several times), so the flag only
keeps followReplaced off.
Not tried: a real pair of the app and magpie web on two release builds
with one replacing the binary (only temp files and fakes); Windows
(InstallBinaryAside starts the staged file there, so the own-install
test is skipped and the click test covers the flag) and Linux runs;
the whole GUI suite: mote was never free (two other sessions' full GUI suites, load ~400), so it runs on the Linux box after this lands.
219 lines
5.9 KiB
Go
219 lines
5.9 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
|
|
}
|
|
u.mu.Lock()
|
|
// another magpie put the update in (restartBeside): that is what
|
|
// the restart goes into, whatever the feed answers now
|
|
in := u.replaced()
|
|
u.mu.Unlock()
|
|
switch st := u.json().State; {
|
|
case in:
|
|
case st == "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 st == "checking", st == "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()
|
|
}
|