Files
magpie/internal/gui/quitask.go
T
yetone 7e3e0fec4f gui: put agents back as they were when magpie quits, connect them again at start (tudjam17795 on X)
New Settings > General switch, "Put agents back when quitting" (zh: 退出时还原 Agent 配置),
default off. With it on, quitting the GUI app runs the same restore Disconnect does
for every agent magpie has connected (*.before-magpie files back byte for byte), and the
next start connects those agents again. Off keeps today's behaviour: a connected agent
keeps magpie as it quits.

- agent.RestoreOnQuit disconnects each wired agent that the user did not disconnect, then
  forgets the <id>.disconnected mark, so the quit leaves no mark; it stashes the list of ids.
  agent.RewireOnStart takes that list once and connects those still unwired and not
  disconnected since. An agent the user disconnected stays disconnected.
- gui: OnShutdown restores only when the setting is on and this process served the
  gateway; start re-wires in the background (not while the gateway is paused). A restore
  on quit also quits at once without the ask.
- settings.RestoreOnQuit, settingsapi key kept unless sent, row in index.html, i18n for
  zh, zh-TW, ja, de, docs in gui-shell.md and agent-wiring.md.
- app.js: the repeated Off/On settings rows go through one helper (segsOO), 422 bytes
  smaller overall, so TestCoreScriptBudget passes without raising the budget.

Run (sandbox HOME, mica because mote was loaded):
- go test internal/agent, internal/gui, internal/settings, internal/settingsapi, internal/cli
  at the rebased head: the new tests and the budget test pass.
- GUI, chromium and webkit, five languages, widths 1100 and 420: restore-on-quit (new),
  quit-ask-setting, quit-ask, settings-general, settings-sections, settings-places,
  i18n-keys, i18n-en-no-cjk, gui-de, gui-ja, gui-zh-tw, and for the rows moved to
  segsOO: reduce-motion, skeleton-bird, lightweight, keep-login, checkin-at,
  update-auto, update-hide, update-restart, update-download, tray-bird, tray-logos,
  tray-cost, wsl-agents-setting, login-allow. All 0 fail.
- gofmt -l clean; go vet darwin, windows, linux -tags nogui clean.

Broken by hand: not forgetting the disconnected mark turns
TestRestoreOnQuitAndRewireOnStart and TestRestoreOnQuitLeavesUserDisconnected red;
dropping the Disconnected() checks turns TestRestoreOnQuitLeavesUserDisconnected red;
dropping the setting gate turns TestRestoreAgentsOnQuit red.

Known reds, not from this change (same on the base 60559ee73): 20 GUI failures in
settings-hints, settings-lines-wrap and settings-search (#6133, #6107, #6156, #6073);
internal/gui TestPanelHiddenByWailsMarksPageOutOfSight killed under load (#6063);
internal/agent TestPiGroupEntryKeepsItsMembers on mica.

Not tried: real agent configs (sandbox HOME only), a real Wails quit on a Mac with real
accounts, Windows via win-run, the Linux WebKitGTK app.
2026-11-12 00:19:34 +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 || s.RestoreOnQuit) {
return true
}
return quitNow(asked, now, len(quitAgents()))
}