Commit Graph
5 Commits
Author SHA1 Message Date
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
yetone d307aa6485 gui: /api/state is written one agent at a time, for the page and the kept sum at once
Each poll of the Agents page encoded the whole state (1.3 MB with real
accounts) into json's buffer, and the GC between polls empties the pool
that buffer comes from, so it grew to the state's size again each poll:
48 MB of what twenty rounds allocated (v0.1.2477 build, real-account
sandbox), and 68 MB at v0.1.2463.

writeStateJSON encodes the state without its agents twice (as sent, and
as keepState keeps it, without the settings), and each agent once,
straight to the response and to keepState's sha256, through one encoder.
keepState is split so the handler hands it that sum (keepStateSummed).
When the splice can't be made (no agents, or a field put before
"agents"), it writes and sums the state whole, as before.

Measured on this Mac with the real-account sandbox (abr.sh, 20 rounds of
drift + state + providers):
- v0.1.2477: writeJSON under /api/state 48 MB, 511 MB in all.
- this change: writeStateJSON 15 MB, 477 MB in all. The /api/state
  bytes are identical to v0.1.2477's (cmp).

Run on mote (mote-run, Go 1.26.8):
- gofmt clean; go vet ok, also GOOS=windows.
- TestStateWrittenAsEncoded (same bytes as Encode, same sum as keepState,
  for a large state, an empty agent list and none) and
  TestStateWriteGrowsNoBufferItsSize, with TestKeptState* and TestLast*:
  ok 3 of 3.
- Broken by hand (the whole-state path always taken): red, "writing a
  1614586-byte state allocated 4207139 bytes, want under half its size".
- Not run before this push: the full internal/gui package; it runs next.
2026-11-01 05:12:20 +08:00
yetone 6a23d9d755 gui: the kept state and providers are marshaled only when they changed
keepState and keepProviders ran json.Marshal on every /api/state and
/api/providers poll to compare the answer with the one kept on disk.
They now keep a sha256 of what was written and hash the new answer
into it with an Encoder; only a changed answer is marshaled and
written. json.Marshal there was 38.6 MB (keepState 25.9 MB,
keepProviders 12.7 MB) of what twenty window polls allocated with
real accounts (m2463 profile).

Test: TestKeptStateMarshaledOnlyWhenChanged. Twenty identical keeps
marshal nothing, nor does a keep after the file is read again; a
changed state is marshaled once and read back.

Run on mote, at this tree on dd7442f28:
- gofmt, go vet (darwin, windows, linux): ok
- the new test alone 3 times: ok
- broken by hand (the sum check made never true): red, "20 polls of
  the same state and list marshaled them 20 times, want 0"
- go test ./internal/gui: every test ok except the known
  TestSettingsSaveKeepsWhatItDoesNotSend (#4514) and
  TestMainWindowCorners (#3949)

Not run: an A/B with the real-account sandbox for this change alone;
it goes with the next batch's profile.
2026-11-01 04:46:38 +08:00
yetone 6a900e0d04 gui: a poll of the Agents page no longer drops every cached model list
/api/state keeps the state it built in agents-state.json, written only
when the bytes change. They changed on every poll: a stale copy's start
is now less how long it has run, two clock reads apart, so it moved by
microseconds each time (on a real config, only
/agents[1]/staleCopies[i]/since differed between two polls). The file
was written through edit.WriteAtomic, whose write calls
filememo.Forget(). That bumps provider.StampNow, so every 15s poll threw
away each agent's model list and picker (magpieModels, viaMagpie) and
every provider's kept catalog, and the next look built them anew.

- StaleCopies' since is truncated to the second (the check against
  magpie's change still uses the exact time).
- keepState writes through steady.WriteFile (temp, flush, rename), which
  leaves filememo alone: nothing a request reads is built from this file.

Not changed: on Linux and WSL the process list gives a run time in whole
seconds (ps etime), so a copy's since can still move by a second between
polls there; macOS reads the exact start from sysctl.

Run on mote at 7428f3229 + this change:
- go test ./internal/agent ./internal/gui (-count=1 -timeout 45m):
  agent ok (280.8s). gui FAIL in two tests already red on main and
  filed: TestSettingsSaveKeepsWhatItDoesNotSend (#4514) and
  TestMainWindowCorners/macOS_26_SDK on macOS 27 (#3949).
- node --test agent-stale-copies.test.cjs: 0 failed.
- go vet (darwin), GOOS=windows go vet of both packages, GOOS=linux go
  vet ./internal/agent, gofmt -l: clean. A Linux vet of internal/gui
  needs gtk4 and webkitgtk-6.0, which neither mote's cross build nor the
  WSL box has.

Broken by hand: without the truncation TestStaleCopySinceSameEachLook
is red (since 08:20:11.5 then 08:20:11.500001); with filememo.Forget()
after the write TestKeptStateLeavesFilememo is red.
2026-11-01 01:28:58 +08:00
yetone c97b4247a7 gui: the Agents page draws the agents magpie read last at once after a start or a light-mode reopen, and the fresh read replaces them (#1519, fatkun)
After a start, or after light mode let the window go, the Agents page
waited for state(): every agent's config read again, and with WSL agents
on each distro's through \\wsl.localhost. Measured on the Windows test box
(realistic config, one running distro with 9 agents):

  /api/state, WSL agents on:  456-620 ms per read (WSL rows 20-160 ms each)
  /api/state, WSL agents off: 117-253 ms
  /api/providers:             83-125 ms, already loaded on its own
                              (show() at start), not behind the state

Now GET /api/state keeps what it built (memory, and
<cache>/magpie/agents-state.json for the next start, written only when it
changed). GET /api/state?last=1 answers with it at once, marked last, with
the settings as they are now (the kept copy leaves the settings out: they
hold the GitHub token and the LAN key) and only for the version that kept
it. The page's first load asks for both, draws the last-known rows if they
come first, and the fresh read replaces them: an agent leaves the list
only once that read says it is gone. A change made on the early rows
answers with a newer state, which stands over the fresh read asked before
it. With nothing kept the page waits as before.

Windows box, new binary, cold start: /api/state?last=1 7-25 ms (436 KB,
all 20 agents incl. the WSL ones), fresh /api/state 456-550 ms beside it.

Run: TestLastKnownState (fails on the old handler: ?last=1 gave the full
fresh state); agents-last-known.test.cjs in Chromium and WebKit (the
at-once case fails on the old app.js); the 21 GUI tests that count or
stub /api/state; go test ./internal/gui; gofmt; vet darwin, linux,
windows; real page in Chromium against the built binary (second start
drawn from agents-state.json).

Not changed: the ~1.15 s of start-up moves before the window on Windows,
and state()'s own cost.
2026-10-11 01:49:47 +08:00