Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

routing: out of sight, the page builds nothing for each request (#2515, #2513)

yetonee8ab1333be2a
diff --git a/internal/gui/assets/plugins/routing/client.js b/internal/gui/assets/plugins/routing/client.js
index adfe76fe6..b65c058ae 100644
--- a/internal/gui/assets/plugins/routing/client.js
+++ b/internal/gui/assets/plugins/routing/client.js
@@ -1533,7 +1533,13 @@ function routingPage(part) {
// the accounts they weighed, each route's in the order it weighed them —
// a new order of the same accounts moves them to their places, so it
// shows. Accounts and agents stay where they are while they stay.
+ // Out of sight none of the stage is drawn: resume() draws it as it is
+ // when the page comes back. Built while hidden, every trace update put
+ // new agents, rows and wires in, and their entrance and live animations,
+ // held by a timeline that doesn't run while the window is hidden, kept
+ // each replaced node alive (#2515, #2513)
function sync(force) {
+ if (!shown()) return;
const rs = staged();
if (!rs.length) return;
if (force) return steady(() => rebuild(rs, true));
@@ -2580,6 +2586,7 @@ function routingPage(part) {
return els;
}
function renderHist() {
+ if (!shown()) return; // drawn by resume()'s renderAll, as the stage
const rs = listed();
renderStats(rs);
const all = allListed();
@@ -3596,6 +3603,7 @@ function routingPage(part) {
}
function empty() {
+ if (!shown()) return; // resume() empties it in sight, as sync
offline("");
what.replaceChildren(el("b", "", t(purpose.length || day ? "No requests match these filters." : "Waiting for a request")));
mode.textContent = t("Send one from any agent routed through magpie and it plays here as it happens: who routing put first and why, each try, and what each answered.");
diff --git a/internal/gui/tests/routing-hidden-stage.test.cjs b/internal/gui/tests/routing-hidden-stage.test.cjs
new file mode 100644
index 000000000..a68d6e18c
--- /dev/null
+++ b/internal/gui/tests/routing-hidden-stage.test.cjs
@@ -0,0 +1,127 @@
+// Run with Node's test runner and Playwright on the module path; see README.md.
+// The Routing page out of sight — another page picked, or the window hidden
+// — builds nothing for the requests that come meanwhile (#2515, #2513). Each
+// trace update put up the stage again (its agents, rows and wires) and the
+// request list, out of sight on every page and in the tray panel. In a
+// hidden window no frame is drawn, so the entrance and live animations on
+// the nodes it replaced stayed on the document's timeline and held each one:
+// WebView2 grew to GBs over a day. Out of sight the page is left as it was,
+// and back in sight it is drawn at once with the newest request. The
+// window's hidden state is faked, as headless pages are never hidden.
+const assert = require("node:assert/strict");
+const fs = require("node:fs/promises");
+const path = require("node:path");
+const { test } = require("node:test");
+const { chromium, webkit } = require("playwright");
+
+const assets = path.resolve(__dirname, "../assets");
+const relay = { id: "relay:team", provider: "relay", name: "Relay", kind: "key", who: "team", model: "gpt-6-sol" };
+const other = { id: "other:solo", provider: "other", name: "Other", kind: "key", who: "solo", model: "gpt-6-sol" };
+// agents and accounts take turns, so each update changes the stage
+const route = (id, done) => ({
+ id, seq: id, time: new Date().toISOString(), agent: id % 2 ? "codex" : "claude", model: `relay/m-${id}`, provider: id % 3 ? "relay" : "other",
+ order: id % 3 ? [relay, other] : [other, relay],
+ tries: [{ id: (id % 3 ? relay : other).id, model: "gpt-6-sol", start: new Date().toISOString(), done, status: done ? 200 : 0, ms: done ? 900 : 0 }],
+ done, status: done ? 200 : 0, ms: done ? 900 : 0,
+});
+
+function serve(lang) {
+ const state = {
+ agents: [{ id: "codex", name: "Codex", path: "/test/config.toml", fields: [] }, { id: "claude", name: "Claude Code", path: "/test/settings.json", fields: [] }],
+ profiles: [], settings: { lang, theme: "light" },
+ };
+ return async (r) => {
+ const req = r.request(), url = new URL(req.url());
+ const json = (data) => r.fulfill({ json: data });
+ if (url.pathname === "/boot.js") return r.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:"${lang}",theme:"light",web:true};` });
+ if (url.pathname === "/wails/runtime.js") return r.fulfill({ contentType: "text/javascript", body: "export const Window = {};" });
+ if (url.pathname === "/api/state") return json(state);
+ if (url.pathname === "/api/gateway/trace") {
+ // a request every 80 ms, and the one before it done
+ const after = Number(url.searchParams.get("after"));
+ const now = new Date().toISOString();
+ if (!after) return json({ mine: true, now, seq: 2, totals: { requests: 2 }, routes: [route(1, true), route(2, true)] });
+ await new Promise((res) => setTimeout(res, 80));
+ return json({ mine: true, now, seq: after + 1, totals: { requests: after + 1 }, routes: [route(after, true), route(after + 1, false)] });
+ }
+ if (url.pathname === "/api/gateway/history") return json({ cut: false, days: [], routes: [] });
+ if (url.pathname === "/api/usage" && url.searchParams.get("period") === "today") return json({ calls: 2, input: 1000, output: 200, series: [] });
+ if (url.pathname === "/api/usage/quotas") return json([]);
+ if (url.pathname === "/api/groups") return json({ groups: [] });
+ if (url.pathname === "/api/providers") return json({ providers: [], gateway: { running: true, window: true } });
+ if (url.pathname.startsWith("/api/window/")) return r.fulfill({ status: 204 });
+ if (url.pathname.startsWith("/api/")) return json({});
+ const file = path.join(assets, url.pathname === "/" ? "index.html" : url.pathname);
+ const contentType = { ".html": "text/html", ".js": "text/javascript", ".css": "text/css", ".svg": "image/svg+xml", ".png": "image/png" }[path.extname(file)];
+ await r.fulfill({ body: await fs.readFile(file), contentType });
+ };
+}
+
+// the newest request the list shows
+const newestListed = () => Math.max(0, ...[...document.querySelectorAll("#view-routing .rt-req .m")].map((m) => Number(m.textContent.replace(/\D+/g, "")) || 0));
+
+const ways = {
+ "another page picked": {
+ hide: (page) => page.locator('#nav button[data-view="agents"]').click(),
+ show: (page) => page.locator('#nav button[data-view="routing"]').click(),
+ },
+ "the window hidden": {
+ hide: (page) => page.evaluate(() => window.__hide(true)),
+ show: (page) => page.evaluate(() => window.__hide(false)),
+ },
+};
+
+for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
+ for (const lang of ["en", "zh"]) {
+ for (const [way, how] of Object.entries(ways)) {
+ test(`${engine} ${lang}: out of sight (${way}), the Routing page builds nothing for each request`, async (t) => {
+ const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
+ t.after(() => browser.close());
+ const ctx = await browser.newContext({ viewport: { width: 1200, height: 800 }, reducedMotion: "no-preference" });
+ await ctx.addInitScript(() => {
+ window.__hid = false;
+ Object.defineProperty(Document.prototype, "hidden", { configurable: true, get: () => window.__hid });
+ Object.defineProperty(Document.prototype, "visibilityState", { configurable: true, get: () => (window.__hid ? "hidden" : "visible") });
+ window.__hide = (h) => { window.__hid = h; document.dispatchEvent(new Event("visibilitychange")); };
+ });
+ const page = await ctx.newPage();
+ const errors = [];
+ page.on("pageerror", (e) => errors.push(e.message));
+ page.setDefaultTimeout(5000);
+ await page.route("**/*", serve(lang));
+ await page.goto("http://magpie.test/?view=routing");
+ await page.locator("#view-routing .rt-srcs .rt-node").first().waitFor();
+ // in sight, the list keeps up
+ await page.waitForFunction(`(${newestListed})() >= 6`);
+
+ await how.hide(page);
+ // a request flying as the page went is let finish its step first
+ await page.waitForTimeout(300);
+ const seqAt = await page.evaluate(() => {
+ window.__added = {};
+ new MutationObserver((ms) => {
+ for (const m of ms) for (const n of m.addedNodes) {
+ if (n.nodeType !== 1) continue;
+ const k = n.tagName.toLowerCase() + (n.classList.length ? "." + n.classList[0] : "");
+ window.__added[k] = (window.__added[k] || 0) + 1;
+ }
+ }).observe(document.querySelector("#view-routing"), { childList: true, subtree: true });
+ return Math.max(0, ...[...document.querySelectorAll("#view-routing .rt-req .m")].map((m) => Number(m.textContent.replace(/\D+/g, "")) || 0));
+ });
+ let heard = 0;
+ page.on("request", (q) => { if (q.url().includes("/api/gateway/trace")) heard++; });
+ for (let i = 0; i < 50 && heard < 20; i++) await page.waitForTimeout(100);
+ assert(heard >= 20, `the trace was read ${heard} times out of sight`);
+ // a stage's node, a row, a wire, a list's request: none of them put up
+ const added = await page.evaluate(() => window.__added);
+ assert.deepEqual(added, {}, `out of sight over ${heard} requests, the page added ${JSON.stringify(added)}`);
+
+ // back in sight: drawn at once, with the newest request
+ await how.show(page);
+ await page.waitForFunction(`(${newestListed})() >= ${seqAt + 20}`);
+ assert(await page.locator("#view-routing .rt-srcs .rt-node").first().isVisible());
+ assert.deepEqual(errors, []);
+ });
+ }
+ }
+}
diff --git a/internal/gui/tests/routing-idle-frame.test.cjs b/internal/gui/tests/routing-idle-frame.test.cjs
index f7dda0035..b2e64ac9a 100644
--- a/internal/gui/tests/routing-idle-frame.test.cjs
+++ b/internal/gui/tests/routing-idle-frame.test.cjs
@@ -208,7 +208,10 @@ async function routingPage(t, engine, lang, feed, view = "routing") {
page.on("pageerror", e => errors.push(e.message));
await page.route("**/*", serve(lang, feed));
await page.goto(`http://magpie.test/?view=${view}`);
- await page.waitForFunction(() => document.querySelectorAll(".rt-req").length > 0);
+ // out of sight the list isn't drawn (routing-hidden-stage.test.cjs): the
+ // trace is in once the live dot says a request is under way
+ if (view === "routing") await page.waitForFunction(() => document.querySelectorAll(".rt-req").length > 0);
+ else await page.waitForFunction(() => document.querySelector(".rt-livedot.on"));
return { page, errors };
}