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.
227 lines
7.8 KiB
Go
227 lines
7.8 KiB
Go
package gui
|
||
|
||
import (
|
||
"encoding/json"
|
||
"net/http/httptest"
|
||
"testing"
|
||
"time"
|
||
|
||
"github.com/yetone/magpie/internal/agent"
|
||
"github.com/yetone/magpie/internal/settings"
|
||
)
|
||
|
||
// A Quit the user chose while agents go through magpie asks first
|
||
// (Aisiverto on X: Codex's 502s after magpie was quit, with nothing to
|
||
// say why). The window's Quit reaches AskQuit; the question's own Quit
|
||
// (now=1) quits; a browser's page (magpie web) has no window to ask in
|
||
// and quits.
|
||
func TestWindowQuitAsksFirst(t *testing.T) {
|
||
quits := 0
|
||
asked := make(chan struct{}, 4)
|
||
h := webPageHandler(t, askingHost{webHost{quit: func() { quits++ }}, asked}, nil)
|
||
post := func(url string) {
|
||
t.Helper()
|
||
r := httptest.NewRecorder()
|
||
h.ServeHTTP(r, httptest.NewRequest("POST", url, nil))
|
||
if r.Code != 204 {
|
||
t.Fatalf("%s: %d %s", url, r.Code, r.Body)
|
||
}
|
||
}
|
||
|
||
post("/api/window/quit")
|
||
select {
|
||
case <-asked:
|
||
case <-time.After(5 * time.Second):
|
||
t.Fatal("the window's Quit didn't ask (AskQuit never ran)")
|
||
}
|
||
if quits != 0 {
|
||
t.Fatalf("the window's Quit quit %d times without asking", quits)
|
||
}
|
||
|
||
post("/api/window/quit?now=1")
|
||
if quits != 1 {
|
||
t.Fatalf("the question's Quit (now=1) quit %d times, want 1", quits)
|
||
}
|
||
select {
|
||
case <-asked:
|
||
t.Fatal("the question's own Quit asked again")
|
||
default:
|
||
}
|
||
|
||
web := webPageHandler(t, webHost{quit: func() { quits++ }}, nil)
|
||
r := httptest.NewRecorder()
|
||
web.ServeHTTP(r, httptest.NewRequest("POST", "/api/window/quit", nil))
|
||
if quits != 2 {
|
||
t.Fatalf("a browser's Quit quit %d times, want at once", quits-1)
|
||
}
|
||
}
|
||
|
||
// askingHost is a host that asks, as the app's does.
|
||
type askingHost struct {
|
||
webHost
|
||
asked chan struct{}
|
||
}
|
||
|
||
func (h askingHost) AskQuit() { h.asked <- struct{}{} }
|
||
|
||
// The question names the agents magpie is in, and only those.
|
||
func TestQuitListsWiredAgents(t *testing.T) {
|
||
yes := func() bool { return true }
|
||
got := wiredAgents([]*agent.Agent{
|
||
{ID: "codex", Name: "Codex", Icon: "openai", Joined: yes},
|
||
{ID: "claude", Name: "Claude Code", Icon: "claude"},
|
||
{ID: "hermes", Name: "Hermes", Icon: "hermes", Beside: yes},
|
||
})
|
||
want := []quitAgentJSON{{ID: "codex", Name: "Codex", Icon: "openai"}, {ID: "hermes", Name: "Hermes", Icon: "hermes"}}
|
||
if len(got) != len(want) || got[0] != want[0] || got[1] != want[1] {
|
||
t.Fatalf("wired agents = %+v, want %+v", got, want)
|
||
}
|
||
// none is an empty list, which the page reads as "quit at once"
|
||
if b, _ := json.Marshal(wiredAgents(nil)); string(b) != "[]" {
|
||
t.Fatalf("no agents marshal as %s, want []", b)
|
||
}
|
||
}
|
||
|
||
// GET /api/quit is what the question reads.
|
||
func TestQuitAPIListsAgents(t *testing.T) {
|
||
old := quitAgents
|
||
t.Cleanup(func() { quitAgents = old })
|
||
quitAgents = func() []quitAgentJSON { return []quitAgentJSON{{ID: "codex", Name: "Codex", Icon: "openai"}} }
|
||
r := httptest.NewRecorder()
|
||
webPageHandler(t, webHost{}, nil).ServeHTTP(r, httptest.NewRequest("GET", "/api/quit", nil))
|
||
var got struct{ Agents []quitAgentJSON }
|
||
if err := json.Unmarshal(r.Body.Bytes(), &got); err != nil || len(got.Agents) != 1 || got.Agents[0].ID != "codex" {
|
||
t.Fatalf("GET /api/quit = %d %s (%v)", r.Code, r.Body, err)
|
||
}
|
||
}
|
||
|
||
// With no gateway served here (another magpie serves it, and goes on
|
||
// serving the agents) a Quit cuts nobody off.
|
||
func TestQuitAgentsNoneWithoutGateway(t *testing.T) {
|
||
if served.Load() != nil {
|
||
t.Skip("a gateway is served in this test binary")
|
||
}
|
||
if got := quitAgents(); len(got) != 0 {
|
||
t.Fatalf("with no gateway served here, a Quit cuts off %+v", got)
|
||
}
|
||
}
|
||
|
||
// With agents connected the first Quit asks; a second within a minute of
|
||
// the question quits (the user was told, and it is the way out should the
|
||
// window not show the question); later, it asks again.
|
||
func TestQuitNow(t *testing.T) {
|
||
now := time.Date(2026, 11, 1, 12, 0, 0, 0, time.UTC)
|
||
asked := now.Add(-10 * time.Second).UnixNano()
|
||
for _, c := range []struct {
|
||
name string
|
||
asked int64
|
||
wired int
|
||
want bool
|
||
}{
|
||
{"nothing connected", 0, 0, true},
|
||
{"nothing connected, asked before", asked, 0, true},
|
||
{"connected, never asked", 0, 2, false},
|
||
{"connected, asked 10s ago", asked, 1, true},
|
||
{"connected, asked 2m ago", now.Add(-2 * time.Minute).UnixNano(), 1, false},
|
||
} {
|
||
if got := quitNow(c.asked, now, c.wired); got != c.want {
|
||
t.Errorf("%s: quitNow = %v, want %v", c.name, got, c.want)
|
||
}
|
||
}
|
||
}
|
||
|
||
// "Don't ask again" (zhujiu on Discord): the question's Quit sent with
|
||
// noAsk=1 saves settings.QuitNoAsk and quits; from then on a Quit with
|
||
// agents connected quits at once, until Settings › General › Ask before
|
||
// quitting turns the question back on.
|
||
func TestQuitDontAskAgain(t *testing.T) {
|
||
sandboxHome(t)
|
||
old := quitAgents
|
||
t.Cleanup(func() { quitAgents = old })
|
||
quitAgents = func() []quitAgentJSON { return []quitAgentJSON{{ID: "codex", Name: "Codex", Icon: "openai"}} }
|
||
now := time.Now()
|
||
if quitAtOnce(0, now) {
|
||
t.Fatal("with Codex connected, the first Quit quit without asking")
|
||
}
|
||
|
||
quits := 0
|
||
h := webPageHandler(t, askingHost{webHost{quit: func() { quits++ }}, make(chan struct{}, 1)}, nil)
|
||
r := httptest.NewRecorder()
|
||
h.ServeHTTP(r, httptest.NewRequest("POST", "/api/window/quit?now=1&noAsk=1", nil))
|
||
if r.Code != 204 || quits != 1 {
|
||
t.Fatalf("the question's Quit with Don't ask again: %d %s, %d quits", r.Code, r.Body, quits)
|
||
}
|
||
if !settings.Load().QuitNoAsk {
|
||
t.Fatal("Don't ask again wasn't saved")
|
||
}
|
||
if !quitAtOnce(0, now) {
|
||
t.Fatal("after Don't ask again, a Quit with Codex connected still asks")
|
||
}
|
||
|
||
if err := settings.SetQuitNoAsk(false); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if quitAtOnce(0, now) {
|
||
t.Fatal("with Ask before quitting on again, a Quit didn't ask")
|
||
}
|
||
// a plain Quit from the question saves nothing
|
||
h.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest("POST", "/api/window/quit?now=1", nil))
|
||
if settings.Load().QuitNoAsk {
|
||
t.Fatal("the question's Quit without Don't ask again saved it")
|
||
}
|
||
}
|
||
|
||
// tudjam17795 on X: with Settings › General › Put agents back when
|
||
// quitting on, the app's quit restores the connected agents and its next
|
||
// start connects them again; off (the default), a quit leaves them as
|
||
// they are. With it on a Quit needs no question, and a gateway served by
|
||
// another magpie leaves its agents alone.
|
||
func TestRestoreAgentsOnQuit(t *testing.T) {
|
||
sandboxHome(t)
|
||
old := [...]any{servedHere, quitDetect, quitRestore, startAgents, startRewire}
|
||
t.Cleanup(func() {
|
||
servedHere, quitDetect, quitRestore = old[0].(func() bool), old[1].(func() []*agent.Agent), old[2].(func([]*agent.Agent) ([]string, error))
|
||
startAgents, startRewire = old[3].(func() []*agent.Agent), old[4].(func([]*agent.Agent) ([]string, error))
|
||
})
|
||
restored, rewired := 0, 0
|
||
servedHere = func() bool { return true }
|
||
quitDetect = func() []*agent.Agent { return nil }
|
||
quitRestore = func([]*agent.Agent) ([]string, error) { restored++; return nil, nil }
|
||
startAgents = func() []*agent.Agent { return nil }
|
||
startRewire = func([]*agent.Agent) ([]string, error) { rewired++; return nil, nil }
|
||
oldAgents := quitAgents
|
||
t.Cleanup(func() { quitAgents = oldAgents })
|
||
quitAgents = func() []quitAgentJSON { return []quitAgentJSON{{ID: "codex"}} }
|
||
if quitAtOnce(0, time.Now()) {
|
||
t.Fatal("off by default, a Quit with Codex connected quit without asking")
|
||
}
|
||
|
||
restoreAgentsOnQuit()
|
||
if restored != 0 {
|
||
t.Fatal("off by default, a quit put the agents back")
|
||
}
|
||
s := settings.Load()
|
||
s.RestoreOnQuit = true
|
||
if err := settings.Save(s); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
servedHere = func() bool { return false }
|
||
restoreAgentsOnQuit()
|
||
if restored != 0 {
|
||
t.Fatal("another magpie serves the gateway, yet the quit put its agents back")
|
||
}
|
||
servedHere = func() bool { return true }
|
||
restoreAgentsOnQuit()
|
||
if restored != 1 {
|
||
t.Fatalf("with the setting on, restored %d times", restored)
|
||
}
|
||
// nothing to ask: agents are put back
|
||
if !quitAtOnce(0, time.Now()) {
|
||
t.Fatal("with the setting on a Quit asks")
|
||
}
|
||
rewireAgentsOnStart()
|
||
if rewired != 1 {
|
||
t.Fatalf("a start rewired %d times", rewired)
|
||
}
|
||
}
|