Files
magpie/internal/gui/quitask.go
T
yetone e8fbee60ea gui: pause the gateway without quitting magpie (me2seeks on X)
Disconnect and quit was the only way to stop the gateway, and it quits
magpie too. Pause does the same without the quit, from the tray's Pause
Gateway (Resume Gateway while paused), Settings › General › Gateway
(On/Paused, the app's only) and the Gateway page's Resume gateway, all
through POST /api/gateway/pause.

- Pause disconnects each connected agent as its row's Disconnect does
  (its own settings put back), keeps their ids in gateway-paused.json in
  the config folder, and stops the listener with requests in flight
  drained. An agent that can't be disconnected keeps the gateway serving,
  those before it connected again, and is named in the status line.
- While paused, serveGatewayLocked serves nothing: not the 15s watch, a
  port change nor a handover; a magpie started again stays paused. A
  pause file that can't be read counts as paused.
- Resume serves again and connects the agents the pause disconnected.
  A port held by something else keeps the pause and says what holds it.
  The Gateway page's Start/Restart, connecting an agent and picking one
  of magpie's models for it resume too.
- quitask's wired list and the pause share wiredOf.
- Strings in en, zh, zh-TW, ja, de. gui-shell.md documents it.

Run (mote was unreachable for the later runs; the Windows box instead):
- mote, before the rebase: go vet ./internal/gui; go test -run
  'TestGatewayPause|TestGatewayResumes|TestTray|TestGatewayFix|TestQuit'
  ./internal/gui: ok.
- at this tree on b46f23ffa: win-run go vet ./internal/gui ok; wsl-run
  go vet -tags nogui ./internal/gui ok; gofmt -l clean.
- win-run go test -count=1 ./internal/gui: the pause and tray tests ok;
  8 reds, each the same on origin/main d4b4a188e run alone on Windows
  and filed (#4115 %AppData% import rows, #5928 ~\ paths, #2107/#3649
  TestGatewayHeldByAContainer).
- wsl-run GUI (chromium + webkit): gateway-pause (5 languages x 1000/420),
  settings-general, gateway-takeover, i18n-keys, i18n-en-no-cjk: 51/52.
  The red is gateway-takeover webkit en's dark-mode case, red 1 of 2 on
  untouched origin/main too: #5942.
- rebased on 401187300: win-run go test -run 'Budget|TestGatewayPause|
  TestGatewayResumes|TestTray|TestQuit' ./internal/gui ok; wsl-run go vet
  -tags nogui ./internal/gui ok; gofmt clean. darwin vet not rerun after
  the rebase (mote refused SSH).

Broken by hand: serveGatewayLocked without its pausedNow check, and the
rollback's reconnect skipped: TestGatewayPauseAndResume ("served while
paused") and TestGatewayPauseRollsBack ("codex not connected again")
went red; green again restored.

Not tried: the real app's tray on a desktop, a real agent's config
round-trip (the Go tests fake the agents; Disconnect/connect are the
rows' own code), an agent wired from the CLI while the app is paused
(it doesn't resume), the whole GUI suite, the full Go suite (started after the tag).
2026-11-10 19:36:24 +08:00

80 lines
2.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package gui
import (
"time"
"github.com/yetone/magpie/internal/agent"
"github.com/yetone/magpie/internal/settings"
)
// Quitting magpie ends the gateway, and every agent connected to it fails
// until magpie is opened again, with nothing to say why: Codex retried
// "unexpected status 502 Bad Gateway: Unknown error" five times and gave
// up, and its user found magpie's line in ~/.codex/config.toml only after
// a long search (Aisiverto on X). So a Quit the user chooses (the tray's
// menu, the panel's Quit, Command-Q on the Mac) while agents are connected
// opens the window on a question first, naming them (quitask.js): Quit,
// Disconnect and quit (each one's own settings put back, as its row's
// Disconnect does), or Cancel, with "Don't ask again" (zhujiu on Discord),
// which Settings › General › Ask before quitting turns back on
// (settings.QuitNoAsk). A quit magpie makes itself (a restart to
// update, Uninstall) and the system's own (logging out, shutting down,
// the Dock's Quit) don't ask.
// quitAgentJSON is an agent a quit would cut off, for the question.
type quitAgentJSON struct {
ID string `json:"id"`
Name string `json:"name"`
Icon string `json:"icon"`
}
// quitAgents lists the agents connected to the gateway this process
// serves: none when another magpie serves it, which goes on serving them.
// A var so tests can say.
var quitAgents = func() []quitAgentJSON {
if served.Load() == nil {
return nil
}
return wiredAgents(agent.Detected())
}
// wiredAgents is the agents of as with magpie in their config.
func wiredAgents(as []*agent.Agent) []quitAgentJSON {
out := []quitAgentJSON{}
for _, a := range wiredOf(as) {
out = append(out, quitAgentJSON{ID: a.ID, Name: a.Name, Icon: a.Icon})
}
return out
}
// quitAsker is a host that asks before a quit the user chose (the app's);
// a browser's page (magpie web) quits at once.
type quitAsker interface {
// AskQuit quits, or first asks in the window when agents are connected
AskQuit()
}
// quitAgain is how long after the question is put a second Quit quits
// without it.
const quitAgain = time.Minute
// quitNow says whether a Quit chosen at now quits without asking: with no
// agent connected (wired), or within quitAgain of the question put at
// asked (unix nanoseconds; 0 when it never was).
func quitNow(asked int64, now time.Time, wired int) bool {
if wired == 0 {
return true
}
return asked != 0 && now.Sub(time.Unix(0, asked)) < quitAgain
}
// quitAtOnce says whether a Quit chosen at now quits without asking: the
// user said not to ask (settings.QuitNoAsk, the question's "Don't ask
// again"), or quitNow. A settings file that can't be read asks.
func quitAtOnce(asked int64, now time.Time) bool {
if s, err := settings.LoadChecked(); err == nil && s.QuitNoAsk {
return true
}
return quitNow(asked, now, len(quitAgents()))
}