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.
115 lines
3.7 KiB
Go
115 lines
3.7 KiB
Go
package agent
|
|
|
|
import (
|
|
"bytes"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
const quitUserSettings = "{\n \"theme\": \"dark\",\n \"defaultProvider\": \"xai\",\n \"defaultModel\": \"grok-4.6\"\n}\n"
|
|
const quitUserModels = "{\n \"providers\": {\n \"mine\": {\n \"baseUrl\": \"http://127.0.0.1:9/v1\",\n \"api\": \"openai-completions\",\n \"apiKey\": \"MINE_KEY\",\n \"models\": [{\"id\": \"m1\"}]\n }\n }\n}\n"
|
|
|
|
// quitHome is a sandbox with Prime Agent and OmO, each with the user's own
|
|
// settings.json and models.json.
|
|
func quitHome(t *testing.T) (prime, omoA *Agent, files map[string][]byte) {
|
|
t.Helper()
|
|
home := syncHome(t)
|
|
prime, omoA = primeAgent(home), omo(home)
|
|
files = map[string][]byte{}
|
|
for _, a := range []*Agent{prime, omoA} {
|
|
writeFile(t, a.Path, quitUserSettings)
|
|
writeFile(t, filepath.Join(a.Dir, "models.json"), quitUserModels)
|
|
files[a.Path] = []byte(quitUserSettings)
|
|
files[filepath.Join(a.Dir, "models.json")] = []byte(quitUserModels)
|
|
if err := a.Connect(); err != nil || !a.Wired() {
|
|
t.Fatalf("%s: connect: %v", a.ID, err)
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
func sameBytes(t *testing.T, files map[string][]byte) {
|
|
t.Helper()
|
|
for p, want := range files {
|
|
if b, _ := os.ReadFile(p); !bytes.Equal(b, want) {
|
|
t.Fatalf("%s not as it was:\n%s", p, b)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Quitting with the setting on puts every connected agent's files back as
|
|
// they were before magpie, byte for byte, leaves no disconnected mark, and
|
|
// the next start connects them again.
|
|
func TestRestoreOnQuitAndRewireOnStart(t *testing.T) {
|
|
prime, omoA, files := quitHome(t)
|
|
all := []*Agent{prime, omoA}
|
|
ids, err := RestoreOnQuit(all)
|
|
if err != nil || len(ids) != 2 {
|
|
t.Fatalf("restored %v: %v", ids, err)
|
|
}
|
|
for _, a := range all {
|
|
if a.Wired() || a.Disconnected() {
|
|
t.Fatalf("%s: wired %v, disconnected mark %v", a.ID, a.Wired(), a.Disconnected())
|
|
}
|
|
}
|
|
sameBytes(t, files)
|
|
got, err := RewireOnStart(all)
|
|
if err != nil || len(got) != 2 {
|
|
t.Fatalf("rewired %v: %v", got, err)
|
|
}
|
|
for _, a := range all {
|
|
if !a.Wired() || a.Check() != "" {
|
|
t.Fatalf("%s not connected again:\n%s", a.ID, readFile(a.Path))
|
|
}
|
|
}
|
|
// the record is used once
|
|
if again, _ := RewireOnStart(all); len(again) != 0 {
|
|
t.Fatalf("rewired twice: %v", again)
|
|
}
|
|
}
|
|
|
|
// An agent the user disconnected stays disconnected: a quit doesn't touch
|
|
// it, and a start doesn't connect it, nor one they disconnected after the
|
|
// quit.
|
|
func TestRestoreOnQuitLeavesUserDisconnected(t *testing.T) {
|
|
prime, omoA, files := quitHome(t)
|
|
all := []*Agent{prime, omoA}
|
|
if err := omoA.Disconnect(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ids, err := RestoreOnQuit(all)
|
|
if err != nil || len(ids) != 1 || ids[0] != "prime-agent" {
|
|
t.Fatalf("restored %v: %v", ids, err)
|
|
}
|
|
sameBytes(t, files)
|
|
if got, _ := RewireOnStart(all); len(got) != 1 || got[0] != "prime-agent" {
|
|
t.Fatalf("rewired %v", got)
|
|
}
|
|
if omoA.Wired() || !omoA.Disconnected() {
|
|
t.Fatalf("OmO came back: wired %v, mark %v", omoA.Wired(), omoA.Disconnected())
|
|
}
|
|
// disconnected by hand while magpie was closed
|
|
if _, err := RestoreOnQuit(all); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
stashMark(prime)
|
|
if got, _ := RewireOnStart(all); len(got) != 0 || prime.Wired() {
|
|
t.Fatalf("rewired %v, wired %v", got, prime.Wired())
|
|
}
|
|
}
|
|
|
|
func stashMark(a *Agent) { stash(map[string]string{a.ID + disconnectedKey: "1"}) }
|
|
|
|
// With nothing restored on quit, a start connects nothing: the setting's
|
|
// off behaviour is that a connected agent keeps magpie.
|
|
func TestRewireOnStartWithoutRestore(t *testing.T) {
|
|
prime, omoA, _ := quitHome(t)
|
|
if err := omoA.Disconnect(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got, _ := RewireOnStart([]*Agent{prime, omoA}); len(got) != 0 || omoA.Wired() || !prime.Wired() {
|
|
t.Fatalf("rewired %v", got)
|
|
}
|
|
}
|