Files
magpie/internal/gui/restoreonquit.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

59 lines
1.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 (
"log"
"github.com/yetone/magpie/internal/agent"
"github.com/yetone/magpie/internal/settings"
)
// Settings › General › Put agents back when quitting (tudjam17795 on X:
// after quitting, the harness it was connected to can't go on). Off by
// default, a connected agent keeps magpie as it quits. On, the app's quit
// does what each agent's Disconnect does (agent.RestoreOnQuit: their own
// files back as they were, no Quit question), and the next start connects
// them again (agent.RewireOnStart). Only the agents connected at the quit,
// and not disconnected by the user, are touched.
// restoreOnQuit says whether the quit puts the agents back: the setting,
// and a gateway this process serves (another magpie serving it goes on
// serving its agents). A settings file that can't be read leaves them.
func restoreOnQuit() bool {
s, err := settings.LoadChecked()
return err == nil && s.RestoreOnQuit && servedHere()
}
// hooks, vars for the tests
var (
servedHere = func() bool { return served.Load() != nil }
quitDetect = agent.Detected
quitRestore = agent.RestoreOnQuit
startAgents = agent.All
startRewire = agent.RewireOnStart
)
// restoreAgentsOnQuit is OnShutdown's part of it.
func restoreAgentsOnQuit() {
if !restoreOnQuit() {
return
}
ids, err := quitRestore(quitDetect())
if err != nil {
log.Println("restore agents on quit:", err)
}
if len(ids) > 0 {
log.Printf("quit: %d agents put back as they were", len(ids))
}
}
// rewireAgentsOnStart connects again the agents a quit put back, unless
// the gateway is paused (the pause's own resume connects its agents).
func rewireAgentsOnStart() {
if pausedNow() {
return
}
if ids, _ := startRewire(startAgents()); len(ids) > 0 {
log.Printf("start: %d agents connected again", len(ids))
}
}