Files
magpie/internal/agent/quitrestore.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

87 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 agent
import (
"encoding/json"
"fmt"
"log"
"strings"
)
// Restoring on quit (tudjam17795 on X: after magpie quits, the harness
// it was connected to can't go on). Settings › General › Put agents back
// when quitting is off by default: a connected agent keeps magpie as it
// quits, which is what the Quit question names. With it on, RestoreOnQuit
// does what each agent's Disconnect does, the files put back as they were
// before magpie, and RewireOnStart connects them again at the next start.
//
// Only the agents magpie had connected are touched, and only those are
// connected again: one the user disconnected stays so (its disconnectedKey
// mark), and one the user disconnected, or connected, while magpie was
// closed (the CLI) is left as they set it. Unlike a Disconnect, a quit
// leaves no mark, which would stop the start from connecting it again.
// quitRestoredKey is the stash's record of the agents a quit put back, a
// JSON list of ids.
const quitRestoredKey = "quit.restored"
// RestoreOnQuit disconnects each of as that has magpie in its config and
// that the user hasn't disconnected, and records which for RewireOnStart.
// An agent that can't be disconnected is left as it is and named in the
// error; the others go on.
func RestoreOnQuit(as []*Agent) (restored []string, err error) {
var errs []string
for _, a := range as {
if a.Disconnected() || !a.Wired() {
continue
}
if e := a.Disconnect(); e != nil {
errs = append(errs, fmt.Sprintf("%s: %v", a.Name, e))
continue
}
a.forgetDisconnected()
restored = append(restored, a.ID)
b, _ := json.Marshal(restored)
stash(map[string]string{quitRestoredKey: string(b)})
}
if len(errs) > 0 {
err = fmt.Errorf("%s", strings.Join(errs, "; "))
}
return restored, err
}
// RewireOnStart connects again the agents RestoreOnQuit put back, among
// all (a list of All()). One connected again by hand meanwhile, or
// disconnected by the user since, is left as it is. The record is taken
// out first: an agent that can't be connected is said, not tried at every
// start.
func RewireOnStart(all []*Agent) (rewired []string, err error) {
var ids []string
json.Unmarshal([]byte(unstash(quitRestoredKey)), &ids)
var errs []string
for _, id := range ids {
var a *Agent
for _, c := range all {
if c.ID == id {
a = c
break
}
}
if a == nil || a.Wired() || a.Disconnected() {
continue
}
if _, e := a.ConnectHow(); e != nil {
errs = append(errs, fmt.Sprintf("%s: %v", a.Name, e))
continue
}
rewired = append(rewired, id)
}
if len(errs) > 0 {
err = fmt.Errorf("%s", strings.Join(errs, "; "))
log.Println("rewire after quit:", err)
}
return rewired, err
}
// forgetDisconnected takes out the mark Disconnect left.
func (a *Agent) forgetDisconnected() { forget(a.ID + disconnectedKey) }