Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

update: a click that updates takes the newest the feed has, not the last check's

yetone257eab646dae
diff --git a/internal/gui/app.go b/internal/gui/app.go
index e41970591..6eff87c1e 100644
--- a/internal/gui/app.go
+++ b/internal/gui/app.go
@@ -461,14 +461,7 @@ func Run(version string, showMain bool, link string) error {
}
return false
}
- // with the gateway busy the first click waits for it to be idle,
- // and the item then restarts at once (#577)
- if waiting, _ := updates.waitingFor(); !waiting && !updates.idle() {
- updates.waitIdle(now)
- return
- }
- updates.cancelWait()
- now()
+ updates.restartAsked(now)
})
quit := menu.Add(labels.quit).OnClick(func(*application.Context) { h.app.Quit() })
var ready string // the version waiting for a restart; on the main thread
diff --git a/internal/gui/update.go b/internal/gui/update.go
index 887d9be35..59ef127f1 100644
--- a/internal/gui/update.go
+++ b/internal/gui/update.go
@@ -76,6 +76,7 @@ type updater struct {
// called off, which isn't restarted into by itself again.
autoRestart func() bool
autoSkip string
+ waitAsked bool // the restart waiting was a click's
}
type updateJSON struct {
@@ -241,22 +242,49 @@ func (u *updater) download() bool {
// recheck asks the feed again before a restart into what was downloaded,
// so a newer version out since is the one restarted into. It waits for
-// the feed a little while; a newer version is then downloading.
-func (u *updater) recheck() {
+// the feed a little while (recheckTicks of recheckTick); a newer version
+// is then downloading. asked is a click on Restart to update, which asks
+// for the newest: with Settings' NoAutoDownload on too, a newer version
+// out since is downloaded and restarted into, not the one the last check
+// found (coeo91 on Discord).
+func (u *updater) recheck(asked bool) {
if u.begin() {
- go u.run()
- }
- for range 50 {
u.mu.Lock()
- s := u.state
+ u.fetch = asked
u.mu.Unlock()
- if s != "checking" {
+ go u.run()
+ }
+ for range recheckTicks {
+ if !u.asking() {
return
}
- time.Sleep(200 * time.Millisecond)
+ time.Sleep(recheckTick)
}
}
+// askedOver is whether the feed was last asked over d ago.
+func (u *updater) askedOver(d time.Duration) bool {
+ u.mu.Lock()
+ defer u.mu.Unlock()
+ return time.Since(u.asked) >= d
+}
+
+// recheckTicks of recheckTick is how long recheck waits for the feed;
+// tests shorten it.
+var (
+ recheckTicks = 50
+ recheckTick = 200 * time.Millisecond
+)
+
+// asking is whether the feed is being asked. The state then reads
+// "ready" while an older download waits (jsonIn), which a restart doesn't
+// go into: the answer may name a newer one.
+func (u *updater) asking() bool {
+ u.mu.Lock()
+ defer u.mu.Unlock()
+ return u.state == "checking"
+}
+
// freshAfter is how long the feed's answer stays fresh for the dialog
// that shows the update before it is put in.
const freshAfter = 5 * time.Minute
@@ -268,7 +296,7 @@ func (u *updater) freshen() {
stale := u.state == "ready" && u.staged != "" && time.Since(u.asked) >= freshAfter
u.mu.Unlock()
if stale {
- u.recheck()
+ u.recheck(false) // the dialog asks for nothing to be downloaded
}
}
@@ -641,12 +669,20 @@ func updateRoutes(mux *http.ServeMux, w Windows) {
return true
}
return false
- })
+ }, true)
writeJSON(rw, updates.json())
return
}
updates.cancelWait()
- updates.recheck()
+ updates.recheck(true)
+ if updates.asking() {
+ // the feed hasn't answered yet: the page reads again and
+ // restarts once it has, into the newest
+ j := updates.json()
+ j.State = "checking"
+ writeJSON(rw, j)
+ return
+ }
}
switch j := updates.json(); {
case j.State == "ready":
@@ -660,6 +696,14 @@ func updateRoutes(mux *http.ServeMux, w Windows) {
// NoAutoDownload says (#2296)
updates.download()
case j.State == "available" && j.URL != "":
+ // the newest's page, not the one the last check found: a check
+ // over a minute old is made again first (coeo91 on Discord)
+ if updates.askedOver(time.Minute) {
+ updates.recheck(true)
+ if j = updates.json(); j.URL == "" {
+ break
+ }
+ }
_ = w.OpenURL(j.URL) // nothing waits on the page: the card still shows the update
}
writeJSON(rw, updates.json())
diff --git a/internal/gui/update_fresh_test.go b/internal/gui/update_fresh_test.go
new file mode 100644
index 000000000..984e16dc8
--- /dev/null
+++ b/internal/gui/update_fresh_test.go
@@ -0,0 +1,264 @@
+package gui
+
+import (
+ "crypto/sha256"
+ "encoding/hex"
+ "encoding/json"
+ "net/http"
+ "net/http/httptest"
+ "os"
+ "path/filepath"
+ "strings"
+ "sync"
+ "sync/atomic"
+ "testing"
+ "time"
+
+ "github.com/yetone/magpie/internal/gateway"
+ "github.com/yetone/magpie/internal/settings"
+ "github.com/yetone/magpie/internal/update"
+)
+
+// coeo91 on Discord: "更新行为不对,他会在点击更新按钮后下载上一次或最近一次获取到的
+// 最新版本,而不是实际的最新版本". A click that updates acts on what the feed
+// says at the click, not on what the last check found: these tests run a
+// check that finds 0.1.11, publish 0.1.12, and click.
+
+// freshFeed is a fake update feed naming latest, whose asset counts its
+// downloads. hold, while set, keeps the feed's answers back until it is
+// closed.
+type freshFeed struct {
+ srv *httptest.Server
+ latest atomic.Value
+ downloads atomic.Int32
+ mu sync.Mutex
+ hold chan struct{}
+}
+
+func newFreshFeed(t *testing.T) *freshFeed {
+ t.Helper()
+ h := t.TempDir()
+ t.Setenv("HOME", h)
+ t.Setenv("USERPROFILE", h)
+ t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
+ t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
+ f := &freshFeed{}
+ f.latest.Store("0.1.11")
+ body := []byte("new magpie")
+ sum := sha256.Sum256(body)
+ f.srv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ if r.URL.Path == "/asset" {
+ f.downloads.Add(1)
+ w.Write(body)
+ return
+ }
+ f.mu.Lock()
+ hold := f.hold
+ f.mu.Unlock()
+ if hold != nil {
+ <-hold
+ }
+ v := f.latest.Load().(string)
+ json.NewEncoder(w).Encode(update.Release{Version: v, URL: "https://example.invalid/v" + v, Assets: map[string]update.Asset{
+ update.BinaryAsset(): {URL: "http://" + r.Host + "/asset", Size: int64(len(body)), SHA256: hex.EncodeToString(sum[:])},
+ }})
+ }))
+ t.Cleanup(f.srv.Close)
+ t.Setenv("MAGPIE_UPDATE_FEED", f.srv.URL)
+ old := Version
+ Version = "0.1.10"
+ t.Cleanup(func() { Version = old })
+ return f
+}
+
+// freshUpdater is updates for the test: replacing the test's binary when
+// exe is set, and never restarting (a handover stands in as under way).
+func freshUpdater(t *testing.T, exe bool) *updater {
+ t.Helper()
+ u := &updater{}
+ if exe {
+ p, err := update.Executable()
+ if err != nil {
+ t.Fatal(err)
+ }
+ u.exe = p
+ u.self, _ = os.Stat(p)
+ t.Cleanup(func() { os.Remove(p + ".new") })
+ }
+ old := updates
+ updates = u
+ t.Cleanup(func() { updates = old })
+ if pending.Load() == nil {
+ pending.Store(&handover{})
+ t.Cleanup(func() { pending.Store(nil) })
+ }
+ return u
+}
+
+// settled waits for u's check or download to end.
+func settled(t *testing.T, u *updater) updateJSON {
+ t.Helper()
+ for deadline := time.Now().Add(10 * time.Second); time.Now().Before(deadline); time.Sleep(5 * time.Millisecond) {
+ if !u.asking() {
+ if j := u.json(); j.State != "downloading" {
+ return j
+ }
+ }
+ }
+ t.Fatalf("still %+v", u.json())
+ return updateJSON{}
+}
+
+type openedURLs struct {
+ quitCount
+ mu sync.Mutex
+ urls []string
+}
+
+func (o *openedURLs) OpenURL(u string) error {
+ o.mu.Lock()
+ o.urls = append(o.urls, u)
+ o.mu.Unlock()
+ return nil
+}
+
+func postInstall(t *testing.T, w Windows) updateJSON {
+ t.Helper()
+ mux := http.NewServeMux()
+ updateRoutes(mux, w)
+ rec := httptest.NewRecorder()
+ mux.ServeHTTP(rec, httptest.NewRequest("POST", "/api/update/install", strings.NewReader("{}")))
+ var j updateJSON
+ if rec.Code != 200 || json.Unmarshal(rec.Body.Bytes(), &j) != nil {
+ t.Fatalf("install: %d %s", rec.Code, rec.Body)
+ }
+ return j
+}
+
+// With Settings' Download updates automatically off, 0.1.11 downloaded on
+// a click and 0.1.12 out since: Restart to update downloads 0.1.12 and
+// restarts into it, rather than into 0.1.11.
+func TestRestartClickTakesTheNewest(t *testing.T) {
+ f := newFreshFeed(t)
+ s := settings.Load()
+ s.NoAutoDownload = true
+ if err := settings.Save(s); err != nil {
+ t.Fatal(err)
+ }
+ u := freshUpdater(t, true)
+ u.check()
+ if j := u.json(); j.State != "found" || j.Latest != "0.1.11" {
+ t.Fatalf("the check: %+v", j)
+ }
+ u.download()
+ if j := settled(t, u); j.State != "ready" || j.Latest != "0.1.11" || f.downloads.Load() != 1 {
+ t.Fatalf("Download update: %+v, %d downloads", j, f.downloads.Load())
+ }
+ f.latest.Store("0.1.12")
+ j := postInstall(t, &quitCount{})
+ if j.State == "ready" && j.Latest == "0.1.11" {
+ t.Fatalf("Restart to update went into the version the last check found: %+v", j)
+ }
+ if j := settled(t, u); j.State != "ready" || j.Latest != "0.1.12" || f.downloads.Load() != 2 {
+ t.Fatalf("after the click: %+v, %d downloads", j, f.downloads.Load())
+ }
+ // nothing newer: the click restarts into what is downloaded, not
+ // downloading it again
+ if j := postInstall(t, &quitCount{}); j.State != "ready" || j.Latest != "0.1.12" || f.downloads.Load() != 2 {
+ t.Fatalf("the newest downloaded: %+v, %d downloads", j, f.downloads.Load())
+ }
+}
+
+// A feed slower than the restart's wait for it: the restart doesn't go
+// into the download it has, which the answer may have made old; it says
+// it is checking, and the answer, once in, is downloaded.
+func TestRestartWaitsForTheFeed(t *testing.T) {
+ f := newFreshFeed(t)
+ u := freshUpdater(t, true)
+ oldTicks, oldTick := recheckTicks, recheckTick
+ recheckTicks, recheckTick = 3, time.Millisecond
+ t.Cleanup(func() { recheckTicks, recheckTick = oldTicks, oldTick })
+ u.check()
+ if j := u.json(); j.State != "ready" || j.Latest != "0.1.11" {
+ t.Fatalf("the check: %+v", j)
+ }
+ f.latest.Store("0.1.12")
+ hold := make(chan struct{})
+ f.mu.Lock()
+ f.hold = hold
+ f.mu.Unlock()
+ j := postInstall(t, &quitCount{})
+ f.mu.Lock()
+ f.hold = nil
+ f.mu.Unlock()
+ close(hold)
+ if j.State != "checking" {
+ t.Fatalf("restarted before the feed answered: %+v", j)
+ }
+ if j := settled(t, u); j.State != "ready" || j.Latest != "0.1.12" || f.downloads.Load() != 2 {
+ t.Fatalf("the answer: %+v, %d downloads", j, f.downloads.Load())
+ }
+}
+
+// The tray's Restart to update asks the feed as the page's does: with
+// 0.1.12 out since, it is downloaded and restarted into, once.
+func TestTrayRestartTakesTheNewest(t *testing.T) {
+ busy := &fakeBusy{}
+ u := freshUpdater(t, true)
+ quick(t, busy, u)
+ waitRecheck = (*updater).recheck
+ f := newFreshFeed(t)
+ u.check()
+ if j := u.json(); j.State != "ready" || j.Latest != "0.1.11" {
+ t.Fatalf("the check: %+v", j)
+ }
+ f.latest.Store("0.1.12")
+ into := make(chan string, 4)
+ u.restartAsked(func() bool {
+ u.mu.Lock()
+ into <- u.latest.Version
+ u.mu.Unlock()
+ return true
+ })
+ select {
+ case v := <-into:
+ if v != "0.1.12" {
+ t.Fatalf("the tray restarted into %s with 0.1.12 out", v)
+ }
+ case <-time.After(5 * time.Second):
+ t.Fatalf("never restarted: %+v", u.json())
+ }
+ if n := f.downloads.Load(); n != 2 {
+ t.Fatalf("%d downloads", n)
+ }
+ // the gateway busy: the restart waits, as before (#577)
+ busy.set(gateway.Busy{Requests: 1})
+ u.restartAsked(func() bool { into <- "busy"; return true })
+ if w, _ := u.waitingFor(); !w {
+ t.Fatal("restarted with a request in flight")
+ }
+}
+
+// A magpie that can't replace itself sends a click to the release page:
+// the newest's, not the one an old check found.
+func TestAvailableOpensTheNewestPage(t *testing.T) {
+ f := newFreshFeed(t)
+ u := freshUpdater(t, false)
+ u.check()
+ if j := u.json(); j.State != "available" || j.Latest != "0.1.11" {
+ t.Fatalf("the check: %+v", j)
+ }
+ f.latest.Store("0.1.12")
+ w := &openedURLs{}
+ // a check a moment ago: the page it found is opened, the feed not asked
+ postInstall(t, w)
+ u.mu.Lock()
+ u.asked = time.Now().Add(-2 * time.Minute)
+ u.mu.Unlock()
+ postInstall(t, w)
+ w.mu.Lock()
+ defer w.mu.Unlock()
+ if want := []string{"https://example.invalid/v0.1.11", "https://example.invalid/v0.1.12"}; strings.Join(w.urls, " ") != strings.Join(want, " ") {
+ t.Fatalf("opened %q, want %q", w.urls, want)
+ }
+}
diff --git a/internal/gui/update_notes_test.go b/internal/gui/update_notes_test.go
index c2759c76c..07fb91341 100644
--- a/internal/gui/update_notes_test.go
+++ b/internal/gui/update_notes_test.go
@@ -163,13 +163,13 @@ func TestUpdateRecheckTakesNotesLate(t *testing.T) {
updates.mu.Unlock()
}
hold("")
- updates.recheck()
+ updates.recheck(false)
if j := updates.json(); j.State != "ready" || j.Latest != "0.1.1224" || !strings.Contains(j.Notes, "twenty-four") || j.URL == "" {
t.Fatalf("empty notes not taken from the recheck: %+v", j)
}
hold("- held")
- updates.recheck()
+ updates.recheck(false)
if j := updates.json(); j.Notes != "- held" {
t.Fatalf("held notes replaced: %+v", j)
}
diff --git a/internal/gui/update_test.go b/internal/gui/update_test.go
index dfb04e41f..ea973f764 100644
--- a/internal/gui/update_test.go
+++ b/internal/gui/update_test.go
@@ -63,7 +63,7 @@ func TestUpdateStagedGivesWayToNewer(t *testing.T) {
t.Setenv("MAGPIE_UPDATE_FEED", srv.URL)
// 0.1.13 out: that is the one it restarts into
latest.Store("0.1.13")
- u.recheck()
+ u.recheck(false)
for u.json().State == "downloading" || u.json().State == "checking" {
time.Sleep(10 * time.Millisecond)
}
diff --git a/internal/gui/update_wait.go b/internal/gui/update_wait.go
index 2cf630aea..b8a649093 100644
--- a/internal/gui/update_wait.go
+++ b/internal/gui/update_wait.go
@@ -34,7 +34,8 @@ var gatewayBusy = func() gateway.Busy {
return gateway.Busy{}
}
-// waitRecheck asks the feed again before the restart (updater.recheck).
+// 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.
@@ -46,10 +47,11 @@ func idleAt(b gateway.Busy, now time.Time) bool {
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).
-func (u *updater) waitIdle(restart func() bool) {
+// 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 = restart, false
+ u.whenIdle, u.gaveUp, u.waitAsked = restart, false, asked
if u.waiting {
u.mu.Unlock()
return
@@ -127,8 +129,11 @@ func (u *updater) waitFor(gen int) {
switch u.json().State {
case "ready":
// a newer version out since is downloaded first, and waited for
- waitRecheck(u)
- if u.json().State != "ready" {
+ u.mu.Lock()
+ asked := u.waitAsked
+ u.mu.Unlock()
+ waitRecheck(u, asked)
+ if u.asking() || u.json().State != "ready" {
continue
}
case "checking", "downloading":
@@ -172,7 +177,27 @@ func (u *updater) autoTick(s settings.Settings) {
if j := u.json(); j.State != "ready" || j.Latest == "" || j.Latest == skip {
return
}
- u.waitIdle(restart)
+ 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
diff --git a/internal/gui/update_wait_test.go b/internal/gui/update_wait_test.go
index 5a66626dc..b1ae9a59d 100644
--- a/internal/gui/update_wait_test.go
+++ b/internal/gui/update_wait_test.go
@@ -40,7 +40,7 @@ func quick(t *testing.T, busy *fakeBusy, u *updater) {
t.Setenv("MAGPIE_UPDATE_FEED", "http://127.0.0.1:1/") // never the real one
oldBusy, oldSettle, oldMost, oldTick, oldRecheck := gatewayBusy, idleSettle, waitMost, waitTick, waitRecheck
gatewayBusy, idleSettle, waitMost, waitTick = busy.get, 80*time.Millisecond, time.Minute, 5*time.Millisecond
- waitRecheck = func(*updater) {}
+ waitRecheck = func(*updater, bool) {}
t.Cleanup(func() {
u.cancelWait()
u.loops.Wait()
@@ -121,7 +121,7 @@ func TestRestartWhenIdle(t *testing.T) {
var told atomic.Int32
u.onWait = func() { told.Add(1) }
var restarts atomic.Int32
- u.waitIdle(func() bool { restarts.Add(1); return true })
+ u.waitIdle(func() bool { restarts.Add(1); return true }, true)
time.Sleep(40 * time.Millisecond)
if restarts.Load() != 0 {
t.Fatal("restarted while streaming")
@@ -152,7 +152,7 @@ func TestRestartWhenIdle(t *testing.T) {
u.loops.Wait()
waitMost = 40 * time.Millisecond
busy.set(gateway.Busy{Requests: 1})
- u.waitIdle(func() bool { restarts.Add(1); return true })
+ u.waitIdle(func() bool { restarts.Add(1); return true }, true)
eventually(t, "never gave up", func() bool { return u.json().GaveUp })
if j := u.json(); j.Waiting || restarts.Load() != 1 || j.Busy == nil {
t.Fatalf("gave up: %+v, %d restarts", j, restarts.Load())
@@ -202,7 +202,7 @@ func TestRestartWhenIdleIntoTheNewest(t *testing.T) {
into <- u.latest.Version
u.mu.Unlock()
return true
- })
+ }, false)
latest.Store("0.1.12")
time.Sleep(30 * time.Millisecond)
latest.Store("0.1.13")