Files
magpie/internal/gui/gateway_older.go
T
yetone 90c02326bb gui: an app takes the gateway over from its older copy once idle, and says any other version serving it (matrix0487)
matrix0487 on Discord: after an update, pi said 404 'magpie knows no
model "kimi/k3.1-pro" ... add a provider in magpie first'. The app had
written pi a model its older gateway didn't know, and updating the
gateway fixed it. A magpie that finds another one serving the gateway
used to log it and wait for that one to quit (watchGateway took the
port only once nothing answered). Only the Gateway page said so.

Now (otherServes, gateway_older.go):
- An older released copy of the app (it says serves app or tray, and a
  pid) is this app's predecessor. Once its /v1/magpie/concurrency says
  nothing is in flight (gateway.ServedBusy; unknown is never idle),
  the app quits it the way the Gateway page's button does
  (quitMagpieOnPort: a magpie only, by its program) and serves the
  gateway. A stream through it runs to its end first. watchGateway
  looks again every 15s while another serves. At most three times a
  run; a quit that fails is left to the button.
- Any other one (magpie serve or the service, magpie web, a TUI, a
  newer magpie, a build of its own) is left running. /api/state
  carries it as gatewayOther, and the Agents page says it above the
  list with what to do, plus the take-over button for an older one
  (gatewaySkewLine, in plugins/gateway/client.js so app.js stays under
  its budget). A dev build of this magpie says nothing. New strings in
  zh, zh-TW, ja and de.

How the reporter's older gateway outlived the update isn't known: they
never said whether one ran separately. A failed handover leaves the
old app serving for up to 15m, and a magpie serve left running is
another source. Both are covered now: the first is replaced, and the
second is said.

Known gap: a request that starts between the idle read and the quit
gets the older app's short drain. A socket handover between two
running apps (no gap) is not built.

Run on mote at 0f8ea30bb (this change on 2b067434d):
- go vet for internal/gui and internal/gateway, also GOOS=linux
  -tags nogui and GOOS=windows; gofmt -l clean.
- go test -count=1 -timeout 45m ./internal/gui ./internal/gateway:
  gateway ok. gui has two reds, the same on origin/main 0c3163804:
  TestSettingsSaveKeepsWhatItDoesNotSend (#4514) and
  TestMainWindowCorners/macOS_26_SDK (#3949).
- GUI subset, Chromium and WebKit: gateway-*, agent-layout, agents-*,
  getting-started, i18n-keys, gui-ja/de/zh, panel-arrange, panel-fold:
  411 pass. Two reds, the same on origin/main 0c3163804:
  gateway-caller-keys chromium en (#3797) and gui-ja (#4580). Passing: the new gateway-skew.test.cjs (en and zh at 380px, the
  button POSTs take-over and the line goes).
- Real app on mote, sandbox HOMEs, port 3591, update feed pointed
  nowhere:
  - origin/main built as 0.1.2400 ran `magpie app`, and this head
    built as 0.1.2500 ran `magpie app` beside it. The new one logged
    "an older copy of this app ... with nothing in flight; quitting
    it" and served 0.1.2500 on the port 2s later. The old process was
    gone.
  - The same with the old one as `magpie serve`: it was left running,
    and the new one logged that it serves, older.

Red on origin/main: TestAppReplacesItsOlderGatewayOnceIdle ("the
older copy of the app still serves the gateway", 17s).

Again at the pushed head (this change on 6f3dfc282, mote): vet darwin, linux
nogui and windows; TestCoreScriptBudget (the core is 1,533,104 of
1,533,105: main's #4652 took the room, so app.js's hook line lost its
comment, no raise), TestAppReplacesItsOlderGatewayOnceIdle,
TestGatewaySkew*, TestGatewayFixQuitsOnlyAMagpie,
TestGatewayServedByAnOlderMagpie: ok. gateway-skew, i18n-keys, gui-zh,
gui-de: 11 of 11 pass.

Broken by hand, each red:
- quit without waiting for idle: "quit with a request in flight"
- replaceable() false: "still serves the gateway"
- any serves replaceable: TestGatewaySkewOfAnotherIsSaidNotQuit, the
  serve holder quit
- the Agents hook removed: gateway-skew.test.cjs red in both langs

Not tried: the whole GUI suite (mote was running another session's
full suite), a real app with a real agent stream in flight during the
take-over (the Go test holds a stream through a fake), Windows and
Linux apps (the replace path needs handoverHost, set by the app's Run
on every OS, but it was tried on macOS only).
2026-11-01 11:43:56 +08:00

136 lines
5.3 KiB
Go

package gui
import (
"log"
"strings"
"sync/atomic"
"github.com/yetone/magpie/internal/gateway"
"github.com/yetone/magpie/internal/update"
)
// A magpie of another version keeping the gateway's port sends every
// agent's request its own way, while this one writes the agents' settings
// (matrix0487 on Discord: the app wrote pi a model its older gateway
// didn't know, and pi said 404 "add a provider in magpie first"). Before,
// this magpie only logged it and waited for that one to quit, and only
// the Gateway page said so. Now:
//
// - an older copy of the app (it says it serves as "app" or "tray", a
// released version both) is this app's own predecessor: this one
// takes the gateway over from it once it has no request in flight
// (gateway.ServedBusy), quitting it as the Gateway page's button does
// (quitMagpieOnPort: only a magpie of this user's, by its program),
// so no request is cut off; looked at again every gatewayWatch;
// - any other (magpie serve, a service, magpie web, a TUI, a newer
// magpie, a build of its own) is someone's to quit or update: it is
// left running and the Agents page says so, with what to do
// (gatewayOther, in /api/state).
// gatewaySkew is a magpie of another version serving the gateway, as the
// page is told it.
type gatewaySkew struct {
Version string `json:"version"` // the other's
Mine string `json:"mine"` // this magpie's
Serves string `json:"serves,omitempty"`
Pid int `json:"pid,omitempty"`
// Older: it is older than this one; Newer: newer; neither, a build
// the two can't be ordered by (one of its own)
Older bool `json:"older,omitempty"`
Newer bool `json:"newer,omitempty"`
// Replacing: this app takes the gateway over from it once it is idle;
// Busy is what it has in flight meanwhile, -1 when it doesn't say
Replacing bool `json:"replacing,omitempty"`
Busy int `json:"busy,omitempty"`
// Stuck: why quitting it didn't go (gatewayFixJSON's Reason); this
// one stops trying, and the page offers the button instead
Stuck string `json:"stuck,omitempty"`
}
// gatewayOther is the magpie of another version serving the gateway, nil
// while none does (this one serves it, or one of the same version).
var gatewayOther atomic.Pointer[gatewaySkew]
// replacedOlder counts the older apps this one has quit to serve the
// gateway; past replaceMost something starts them again (a login item
// pointing at an old copy), and they are left to the user.
var replacedOlder atomic.Int32
const replaceMost = 3
// bare is a version without the v a tag has.
func bare(v string) string { return strings.TrimPrefix(v, "v") }
// skewOf is how the magpie o serving the gateway differs from this one,
// nil when it doesn't (the same version, or one that doesn't say).
func skewOf(o gateway.Served) *gatewaySkew {
mine := gateway.Version
if o.Version == "" || bare(o.Version) == bare(mine) || !update.Released(mine) {
// a build of this magpie's own (make dev) beside the installed
// one is its developer's to know about
return nil
}
return &gatewaySkew{
Version: bare(o.Version), Mine: bare(mine), Serves: o.Serves, Pid: o.Pid,
Older: update.Newer(mine, o.Version), Newer: update.Newer(o.Version, mine),
}
}
// replaceable is whether this magpie takes the gateway over from o by
// itself: this is the app, and o an older release of it.
func replaceable(o gateway.Served, sk *gatewaySkew) bool {
return handoverHost.Load() && sk != nil && sk.Older && update.Released(o.Version) &&
(o.Serves == "app" || o.Serves == "tray") && o.Pid > 0 &&
replacedOlder.Load() < replaceMost
}
// otherServes, under gatewayMu with this one serving nothing, looks at the
// magpie o that serves the gateway: what the page is told of it, and, for
// an older copy of the app with no request in flight, quits it. True when
// it was quit and the port is free for this one.
func otherServes(o gateway.Served) bool {
sk := skewOf(o)
before := gatewayOther.Load()
if sk == nil {
gatewayOther.Store(nil)
return false
}
if before != nil && before.Stuck != "" && before.Pid == sk.Pid {
sk.Stuck = before.Stuck // tried; the button is the user's now
} else if replaceable(o, sk) {
sk.Replacing = true
n, ok := gateway.ServedBusy()
if !ok {
n = -1
}
sk.Busy = n
if n == 0 {
log.Printf("gateway: magpie %s (pid %d), an older copy of this app, serves %s with nothing in flight; quitting it for this one (%s) to serve", sk.Version, sk.Pid, gateway.URL(), sk.Mine)
out := quitMagpieOnPort(o.Version)
if out.OK {
replacedOlder.Add(1)
gatewayOther.Store(nil)
return true
}
log.Printf("gateway: magpie %s wasn't quit (%s %s); it keeps the gateway", sk.Version, out.Reason, out.Error)
sk.Replacing, sk.Busy, sk.Stuck = false, 0, out.Reason
}
}
if before == nil || before.Version != sk.Version || before.Pid != sk.Pid {
how := "older than this one"
switch {
case sk.Newer:
how = "newer than this one"
case !sk.Older:
how = "a build other than this one"
}
next := "until it quits"
if sk.Replacing {
next = "until this one takes it over, once it has no request in flight"
}
log.Printf("gateway: magpie %s (%s, pid %d) serves %s, %s (%s); agents' requests go through it %s", sk.Version, sk.Serves, sk.Pid, gateway.URL(), how, sk.Mine, next)
}
gatewayOther.Store(sk)
return false
}