diff --git a/docs/subsystems/gui-shell.md b/docs/subsystems/gui-shell.md index 8180d397d..71edee0f7 100644 --- a/docs/subsystems/gui-shell.md +++ b/docs/subsystems/gui-shell.md @@ -899,9 +899,14 @@ that one keeps serving the agents and the Quit goes on). stop while magpie is closed, a note that closing the window keeps magpie running (the menu bar on a Mac, the system tray elsewhere), and Disconnect and quit, Cancel, and Quit (focused). Quit posts `window/quit?now=1`. - Disconnect and quit disconnects each agent in turn, as its row's - Disconnect does, and a failure leaves magpie open, named in the status - line. With nothing connected any more it quits without drawing. + Disconnect and quit posts `quit/disconnect` (`quitDisconnect`), which + puts every connected agent back as Put agents back when quitting does + (`agent.RestoreOnQuit`, no disconnected mark, recorded in + `quit.restored`), so the next start connects them again + (`RewireOnStart`), whatever that setting says. Each row's Disconnect, + which it used, left a mark that kept every switch off at every start + after (jtbbtnt54249 on X). A failure leaves magpie open, named in the + status line. With nothing connected any more it quits without drawing. - A second Quit within a minute of the question (`quitNow`, `quitAgain`) quits, so a window that can't show the question doesn't trap the user. - Don't ask again (zhujiu on Discord), a tick above the buttons, unticked diff --git a/internal/gui/api.go b/internal/gui/api.go index 4e407aa99..5f4148fea 100644 --- a/internal/gui/api.go +++ b/internal/gui/api.go @@ -3212,6 +3212,15 @@ func Handler(w Windows, gw *gateway.Server) http.Handler { mux.HandleFunc("GET /api/quit", func(rw http.ResponseWriter, r *http.Request) { writeJSON(rw, map[string]any{"agents": quitAgents()}) }) + // the question's Disconnect and quit: the agents put back as Put + // agents back when quitting puts them, for this quit (quitDisconnect) + mux.HandleFunc("POST /api/quit/disconnect", func(rw http.ResponseWriter, r *http.Request) { + if err := quitDisconnect(); err != nil { + fail(rw, err) + return + } + writeJSON(rw, state()) + }) mux.HandleFunc("POST /api/window/{action}", func(rw http.ResponseWriter, r *http.Request) { switch r.PathValue("action") { case "hide": diff --git a/internal/gui/assets/i18n.js b/internal/gui/assets/i18n.js index 9e947c9bc..f17bc567a 100644 --- a/internal/gui/assets/i18n.js +++ b/internal/gui/assets/i18n.js @@ -898,7 +898,7 @@ const I18N = { "While agents go through magpie, Quit names them and asks first": "有 Agent 通过 magpie 连接时,退出前先列出它们并询问", "Don't ask again": "不再提示", "Settings › General › Ask before quitting turns it back on": "可在 设置 › 常规 › 退出前询问 里重新打开", - "Puts back each agent's own settings first, so they work without magpie": "先恢复每个 Agent 自己的设置,让它们不经过 magpie 也能使用", + "Puts back each agent's own settings first, so they work without magpie; magpie connects them again when it opens": "先恢复每个 Agent 自己的设置,让它们不经过 magpie 也能使用;magpie 再打开时会重新接入", "magpie takes out everything it wrote into {agent}'s config — its endpoint, key, models and effort — and puts back the settings {agent} had before. magpie's providers and accounts stay as they are.": "移除 magpie 写入 {agent} 配置的所有内容(接口地址、密钥、模型和推理强度),还原 {agent} 之前的设置。magpie 的供应商和账号不受影响。", "Disconnect": "断开", "Not connected · {agent} uses its own settings": "未接入 · 用 {agent} 自己的设置", @@ -6231,7 +6231,7 @@ const I18N = { "While agents go through magpie, Quit names them and asks first": "有 Agent 透過 magpie 連線時,退出前先列出它們並詢問", "Don't ask again": "不再提示", "Settings › General › Ask before quitting turns it back on": "可在 設定 › 常規 › 退出前詢問 裡重新開啟", - "Puts back each agent's own settings first, so they work without magpie": "先還原每個 Agent 自己的設定,讓它們不經過 magpie 也能使用", + "Puts back each agent's own settings first, so they work without magpie; magpie connects them again when it opens": "先還原每個 Agent 自己的設定,讓它們不經過 magpie 也能使用;magpie 再開啟時會重新接入", "magpie takes out everything it wrote into {agent}'s config — its endpoint, key, models and effort — and puts back the settings {agent} had before. magpie's providers and accounts stay as they are.": "移除 magpie 寫入 {agent} 設定的所有內容(介面位址、金鑰、模型和推理強度),還原 {agent} 之前的設定。magpie 的供應商和帳號不受影響。", "Disconnect": "斷開", "Not connected · {agent} uses its own settings": "未接入 · 用 {agent} 自己的設定", @@ -11528,7 +11528,7 @@ const I18N = { "While agents go through magpie, Quit names them and asks first": "エージェントが magpie を経由しているとき、終了前にそれらを示して確認します", "Don't ask again": "今後は確認しない", "Settings › General › Ask before quitting turns it back on": "設定 › 一般 › 終了前に確認 でもう一度オンにできます", - "Puts back each agent's own settings first, so they work without magpie": "まず各エージェント自身の設定に戻し、magpie なしでも動作するようにします", + "Puts back each agent's own settings first, so they work without magpie; magpie connects them again when it opens": "まず各エージェント自身の設定に戻し、magpie なしでも動作するようにします。magpie を次に開くと再び接続します", "magpie takes out everything it wrote into {agent}'s config — its endpoint, key, models and effort — and puts back the settings {agent} had before. magpie's providers and accounts stay as they are.": "magpie は {agent} の設定に書き込んだ内容(エンドポイント、キー、モデル、推論レベル)をすべて削除し、{agent} を以前の設定に戻します。magpie のプロバイダとアカウントはそのままです。", "Disconnect": "切断", "Not connected · {agent} uses its own settings": "未接続 · {agent} は独自の設定を使用", @@ -16843,7 +16843,7 @@ const I18N = { "While agents go through magpie, Quit names them and asks first": "Solange Agenten über magpie laufen, nennt Beenden sie und fragt zuerst nach", "Don't ask again": "Nicht mehr fragen", "Settings › General › Ask before quitting turns it back on": "Einstellungen › Allgemein › Vor dem Beenden fragen schaltet es wieder ein", - "Puts back each agent's own settings first, so they work without magpie": "Stellt zuerst die eigenen Einstellungen jedes Agenten wieder her, damit er ohne magpie funktioniert", + "Puts back each agent's own settings first, so they work without magpie; magpie connects them again when it opens": "Stellt zuerst die eigenen Einstellungen jedes Agenten wieder her, damit er ohne magpie funktioniert; beim nächsten Öffnen verbindet magpie ihn wieder", "magpie takes out everything it wrote into {agent}'s config — its endpoint, key, models and effort — and puts back the settings {agent} had before. magpie's providers and accounts stay as they are.": "magpie entfernt alles, was es in die Konfiguration von {agent} geschrieben hat — Endpunkt, Schlüssel, Modelle und Reasoning-Effort — und stellt die vorherigen Einstellungen von {agent} wieder her. Anbieter und Konten in magpie bleiben unverändert.", "Disconnect": "Trennen", "Not connected · {agent} uses its own settings": "Nicht verbunden · {agent} nutzt eigene Einstellungen", diff --git a/internal/gui/assets/quitask.js b/internal/gui/assets/quitask.js index 3bbbb2389..d5b2cddee 100644 --- a/internal/gui/assets/quitask.js +++ b/internal/gui/assets/quitask.js @@ -2,8 +2,8 @@ // magpie's gateway (quitask.go; Aisiverto on X: Codex failed with 502s // after magpie was quit, and nothing said why). It names the agents that // stop working, says that closing the window keeps magpie running, and -// offers Quit, Disconnect and quit (each agent's own settings put back, -// as its row's Disconnect does), or Cancel, with Don't ask again above +// offers Quit, Disconnect and quit (each agent's own settings put back +// until magpie opens again, which connects them again), or Cancel, with Don't ask again above // them (zhujiu on Discord), which Settings › General › Ask before quitting // turns back on. app.js imports this when the // question is put, so it weighs nothing on the core. @@ -41,7 +41,7 @@ window.askQuitSheet = async function () { const bar = el("div", "bar"); const quit = el("button", "text primary", t("Quit")); const off = el("button", "text", t("Disconnect and quit")); - off.title = t("Puts back each agent's own settings first, so they work without magpie"); + off.title = t("Puts back each agent's own settings first, so they work without magpie; magpie connects them again when it opens"); const cancel = el("button", "text", t("Cancel")); const busy = (b, on) => { for (const x of [quit, off, cancel, box]) x.disabled = on; @@ -52,19 +52,18 @@ window.askQuitSheet = async function () { busy(quit, true); try { await bye(); } catch (err) { busy(quit, false); statusErr(err); } }; - // each agent put back as its row's Disconnect puts it back; one that - // can't be leaves magpie open, with the rest still listed + // each agent put back as Put agents back when quitting puts it back, so + // the next start connects it again (a row's Disconnect kept it off for + // good: jtbbtnt54249 on X); one that can't be leaves magpie open off.onclick = async (e) => { e.stopPropagation(); busy(off, true); - for (const a of agents || []) { - try { - state = await api("agents/disconnect/" + a.id, {}); - } catch (err) { - busy(off, false); - status(a.name + ": " + t(err.message), "err"); - return; - } + try { + state = await api("quit/disconnect", {}); + } catch (err) { + busy(off, false); + statusErr(err); + return; } try { await bye(); } catch (err) { busy(off, false); statusErr(err); } }; diff --git a/internal/gui/quitask_test.go b/internal/gui/quitask_test.go index b463f85cc..e81daa794 100644 --- a/internal/gui/quitask_test.go +++ b/internal/gui/quitask_test.go @@ -2,7 +2,9 @@ package gui import ( "encoding/json" + "errors" "net/http/httptest" + "strings" "testing" "time" @@ -224,3 +226,43 @@ func TestRestoreAgentsOnQuit(t *testing.T) { t.Fatalf("a start rewired %d times", rewired) } } + +// jtbbtnt54249 on X: after Disconnect and quit, every agent's switch was +// off when magpie opened again. The question's Disconnect and quit now +// puts the agents back as Put agents back when quitting does, setting on +// or off (the next start connects them again: agent.RewireOnStart), not +// with each row's Disconnect, whose mark kept them off for good. One that +// can't be put back answers an error and nothing quits. +func TestQuitDisconnectComesBackOnStart(t *testing.T) { + sandboxHome(t) + old := [...]any{quitDetect, quitRestore} + t.Cleanup(func() { + quitDetect, quitRestore = old[0].(func() []*agent.Agent), old[1].(func([]*agent.Agent) ([]string, error)) + }) + detected := []*agent.Agent{{ID: "codex", Name: "Codex"}, {ID: "claude", Name: "Claude Code"}} + quitDetect = func() []*agent.Agent { return detected } + var got [][]*agent.Agent + var fails error + quitRestore = func(as []*agent.Agent) ([]string, error) { got = append(got, as); return []string{"codex"}, fails } + 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/quit/disconnect", nil)) + if r.Code != 200 || len(got) != 1 || len(got[0]) != 2 { + t.Fatalf("Disconnect and quit: %d %s, put back %d times", r.Code, r.Body, len(got)) + } + if settings.Load().RestoreOnQuit { + t.Fatal("it turned Put agents back when quitting on") + } + if quits != 0 { + t.Fatal("it quit by itself: the page quits after it") + } + + fails = errors.New("Claude Code: config is locked") + r = httptest.NewRecorder() + h.ServeHTTP(r, httptest.NewRequest("POST", "/api/quit/disconnect", nil)) + if r.Code < 400 || !strings.Contains(r.Body.String(), "Claude Code: config is locked") { + t.Fatalf("a failed put back: %d %s", r.Code, r.Body) + } +} diff --git a/internal/gui/restoreonquit.go b/internal/gui/restoreonquit.go index dcee69aab..97fe53a6e 100644 --- a/internal/gui/restoreonquit.go +++ b/internal/gui/restoreonquit.go @@ -46,6 +46,22 @@ func restoreAgentsOnQuit() { } } +// quitDisconnect is the quit question's Disconnect and quit (quitask.js): +// each connected agent put back as RestoreOnQuit puts it, so the next +// start connects it again. It disconnected each one as its row's +// Disconnect does, whose mark keeps an agent off at every start after: +// a user who chose it so their agents work while magpie is closed found +// every switch off when magpie opened again, and turned each back on by +// hand (jtbbtnt54249 on X, "退出有时候又要重新开启"). An agent that can't +// be put back is named in the error, and magpie stays open. +func quitDisconnect() error { + ids, err := quitRestore(quitDetect()) + if len(ids) > 0 { + log.Printf("quit: %d agents put back as they were, for the next start", len(ids)) + } + return err +} + // rewireAgentsOnStart connects again the agents a quit put back, unless // the gateway is paused (the pause's own resume connects its agents). func rewireAgentsOnStart() { diff --git a/internal/gui/tests/quit-ask.test.cjs b/internal/gui/tests/quit-ask.test.cjs index 469a59b84..afc55f199 100644 --- a/internal/gui/tests/quit-ask.test.cjs +++ b/internal/gui/tests/quit-ask.test.cjs @@ -7,8 +7,10 @@ // says that closing the window keeps magpie running (the menu bar on a // Mac, the system tray elsewhere), and offers Quit, Disconnect and quit, // and Cancel. Cancel posts nothing; Quit posts window/quit?now=1, which -// quits without asking again; Disconnect and quit disconnects each agent -// in turn and then quits, and a disconnect that fails keeps magpie open. +// quits without asking again; Disconnect and quit posts quit/disconnect, +// which puts each agent back for the next start to connect again (a row's +// Disconnect each kept them off at every start after: jtbbtnt54249 on X), +// and then quits; one that fails keeps magpie open. // With nothing connected any more it quits at once. Don't ask again // (zhujiu on Discord), above the buttons and unticked, goes with the Quit // chosen as noAsk=1 (the server saves settings.QuitNoAsk), and its tooltip @@ -32,7 +34,7 @@ const keys = [ "Closing the window keeps magpie running in the menu bar.", "Closing the window keeps magpie running in the system tray.", "Disconnect and quit", - "Puts back each agent's own settings first, so they work without magpie", + "Puts back each agent's own settings first, so they work without magpie; magpie connects them again when it opens", "Quit", "Cancel", "Don't ask again", @@ -51,9 +53,10 @@ function server(lang, posts, o) { if (url.pathname === "/wails/runtime.js") return route.fulfill({ contentType: "text/javascript", body: "export const Window = {};" }); if (url.pathname === "/api/state") return json(state); if (url.pathname === "/api/quit") return json({ agents: o.agents }); - if (req.method() === "POST" && url.pathname.startsWith("/api/agents/disconnect/")) { + if (req.method() === "POST" && url.pathname.startsWith("/api/agents/disconnect/")) { posts.push(url.pathname); return json(state); } + if (req.method() === "POST" && url.pathname === "/api/quit/disconnect") { posts.push(url.pathname); - if (o.fail === url.pathname.split("/").pop()) return json({ error: "config is locked" }, 500); + if (o.fail) return json({ error: o.fail + ": config is locked" }, 500); return json(state); } if (req.method() === "POST" && url.pathname === "/api/window/quit") { posts.push(url.pathname + url.search); return json({}); } @@ -138,8 +141,10 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", const [box, bar] = await Promise.all([ask.boundingBox(), ask.locator(".bar").boundingBox()]); assert.ok(bar.x >= box.x - 1 && bar.x + bar.width <= box.x + box.width + 1, "the bar fits a narrow window"); await ask.locator(".bar button", { hasText: await tr("Disconnect and quit") }).click(); - await until(() => posts.length >= 3); - assert.deepEqual(posts, ["/api/agents/disconnect/codex", "/api/agents/disconnect/claude", "/api/window/quit?now=1"]); + await until(() => posts.length >= 2); + assert.deepEqual(posts, ["/api/quit/disconnect", "/api/window/quit?now=1"], "put back for the next start, not each row's Disconnect"); + assert.equal(await ask.locator(".bar button", { hasText: await tr("Disconnect and quit") }).getAttribute("title"), + await tr("Puts back each agent's own settings first, so they work without magpie; magpie connects them again when it opens")); }); test(`${engine} ${lang}: Don't ask again goes with the Quit, narrow too`, async (t) => { @@ -177,18 +182,18 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", await ask.locator("label.quit-never").click(); await ask.locator(".bar button", { hasText: await tr("Disconnect and quit") }).click(); await until(() => posts.length >= 2); - assert.deepEqual(posts, ["/api/agents/disconnect/codex", "/api/window/quit?now=1&noAsk=1"]); + assert.deepEqual(posts, ["/api/quit/disconnect", "/api/window/quit?now=1&noAsk=1"]); }); test(`${engine} ${lang}: a disconnect that fails keeps magpie open`, async (t) => { - const { page, posts, tr } = await open(t, { agents: two, fail: "codex" }); + const { page, posts, tr } = await open(t, { agents: two, fail: "Codex" }); const ask = page.locator(".editor.quit-ask"); await ask.waitFor(); const off = ask.locator(".bar button", { hasText: await tr("Disconnect and quit") }); await off.click(); await page.locator("#status.err").waitFor(); assert.match(await page.locator("#status").textContent(), /^Codex: /); - assert.deepEqual(posts, ["/api/agents/disconnect/codex"], "nothing after the failed one, no quit"); + assert.deepEqual(posts, ["/api/quit/disconnect"], "no quit"); assert.equal(await ask.count(), 1, "the sheet stays"); assert.ok(await off.isEnabled(), "and can be tried again"); });