The Routing page (and the tray panel's Routing tab) read /api/gateway/trace in a loop for as long as magpie ran, in sight or not. The read was already a long-poll, but each change woke it: a request's way is told in a burst (begun, a try sent, answered, its usage), so under traffic every open page was answered 4-8 times a second, hidden pages included, for nothing they drew. Server (internal/gateway/trace.go): once a change wakes a waiting Trace, it gathers for traceGather (250ms) before answering, so a burst is one answer; a read with nothing new still blocks until a change or the 25s wait. Every answer now also lists the ids of all the routes the trace keeps (TraceState.IDs, "ids"), changed or not; OwnerTrace passes them through from another magpie serving the gateway. Client (plugins/routing/client.js): after its first answer, poll() asks only while the page or the panel's Routing tab is in sight (document not hidden, and the view shown or the panel's tab picked). Out of sight it parks; start() (visibilitychange, focus, the view's hidden attribute) and panelRoutingShown resume it. The first answer after a resume is a catch-up: what came meanwhile is listed, and only the requests still under way fly (stage and panel), as #302 asks. A route last heard of as under way that is missing from "ids" (it ended and was trimmed while the page was away) is dropped, so it can't show as live forever. An answer with no "ids" (an older magpie) leaves the routes as before. Measured: - Hidden page, 2s with a request every 80ms (routing-hidden-stage, panel-routing-hidden): before, the trace was read 24-25 times in 2s by each of the main window (another page picked, or the window hidden) and the hidden tray panel; after, 0 times. - A burst of 4 changes 20ms apart (TestTraceWaitHearsABurstOnce): before, read in 4 answers; after, 1. Tests: - TestTraceWaitHearsABurstOnce (gateway, new): a waiting read answers a burst once, with all of it and the route's id in IDs. Broken by hand: no gather -> "a burst of 4 changes was read in 4 answers, want 1"; no ids -> "ids [], want [<id>]". - routing-hidden-stage and panel-routing-hidden now assert the page reads the trace at most once out of sight (was: at least 20), and shows the newest request once back. The mock is a clock-driven gateway (every change since `after`, at once). With the old client.js all 12 went red: "the trace was read 24/25 times out of sight", "... while hidden". - routing-busy and routing-flood's mocks answered one route per read; the gateway answers every change since `after`, so they do too now. routing-purpose-state sent one update while another page was shown and required a new read to start out there; that send no longer waits for one. Run at this commit's tree (go1.26.8 darwin/arm64, sandbox HOME): gofmt -l .: nothing GOOS=linux go vet -tags nogui ./...: ok GOOS=windows go vet -tags nogui ./...: ok go test -tags nogui ./internal/gateway ./internal/gui: ok go test -tags nogui -race -timeout 45m ./internal/gateway ./internal/gui: ok (gateway 1598s, gui 348s) set -o pipefail; go test -tags nogui -timeout 45m ./...: ok, every package (66 ok, 20 with no test files) GUI, playwright 1.62.0, chromium and webkit, every routing*.test.cjs and panel-routing*.test.cjs (97 files, 1204 tests, concurrency 4): 1203 pass, 1 red: routing-rule-remove "chromium en 390: a rule's ... is there to remove it" (routing-rule-remove.test.cjs:158, "1 !== 0", the rule count read right after a keyboard Enter). Its trace mock never answers a wait, so its polling is the same before and after this change; alone it passed 3 runs of 3 (60/60). The whole GUI suite (822 files) was still running when this was written.
121 lines
7.6 KiB
JavaScript
121 lines
7.6 KiB
JavaScript
// Run with Node's test runner and Playwright on the module path; see README.md.
|
|
// The tray panel is hidden most of the time, and its Routing tab is not
|
|
// drawn again for every request while it is (#2515): a hidden page draws no
|
|
// frames, so the infinite animations on each replaced tab (the live dot, a
|
|
// waiting row's arrow) stayed on the document's timeline and held every old
|
|
// tab. On Windows the hidden panel grew to 265k nodes in 11 minutes and GBs
|
|
// over a day. Hidden, the tab is drawn again only when its shape (what the
|
|
// panel's height is fitted to) changes, no request's dot flies, and once
|
|
// the panel shows it is drawn in full, newest request first. Hidden, it
|
|
// doesn't ask for the trace either: shown again, it asks once for all that
|
|
// came meanwhile. The page's hidden state is faked here, 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 route = (id, done) => ({
|
|
id, seq: id, time: new Date().toISOString(), agent: "codex", model: `relay/m-${id}`, provider: "relay", order: [relay],
|
|
tries: [{ id: relay.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) {
|
|
// a request every 80 ms, under way, whether or not the panel asks
|
|
const t0 = Date.now();
|
|
const seqNow = () => 2 + Math.floor((Date.now() - t0) / 80);
|
|
const state = { agents: [{ id: "codex", name: "Codex", path: "/test/config.toml", 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") {
|
|
// as the gateway does: what changed since after, at once when
|
|
// something did, or once the next request comes. Each is a tab drawn
|
|
// again
|
|
const after = Number(url.searchParams.get("after"));
|
|
if (!after) return json({ mine: true, now: new Date().toISOString(), seq: 2, totals: { requests: 2 }, routes: [route(1, true), route(2, true)] });
|
|
while (seqNow() <= after) await new Promise((res) => setTimeout(res, 20));
|
|
const seq = seqNow(), routes = [];
|
|
for (let id = Math.max(after + 1, seq - 9); id <= seq; id++) routes.push(route(id, false));
|
|
return json({ mine: true, now: new Date().toISOString(), seq, totals: { requests: seq }, routes });
|
|
}
|
|
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 });
|
|
};
|
|
}
|
|
|
|
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
|
|
for (const lang of ["en", "zh"]) {
|
|
test(`${engine} ${lang}: the hidden tray panel doesn't draw its Routing tab again for every 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: 360, height: 560 }, reducedMotion: "no-preference" });
|
|
await ctx.addInitScript(() => {
|
|
try { localStorage.setItem("magpie.panelTab", "routing"); } catch {}
|
|
// the panel's window put away, as WebView2 tells a hidden page
|
|
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/?mode=panel");
|
|
await page.locator("#panelRouting .pr-req").first().waitFor();
|
|
assert(await page.locator("#panelRouting").isVisible());
|
|
// shown, the tab keeps up: its newest request is the one heard last
|
|
await page.waitForFunction(() => document.querySelectorAll("#panelRouting .pr-req").length === 6);
|
|
|
|
// hidden: requests keep coming, the tab is (nearly) never drawn again
|
|
const seqAt = await page.evaluate(() => {
|
|
window.__hide(true);
|
|
window.__draws = 0;
|
|
window.__dots = 0;
|
|
// each draw puts up a new head
|
|
new MutationObserver((ms) => {
|
|
for (const m of ms) for (const n of m.addedNodes) {
|
|
if (n.classList?.contains("pr-head")) window.__draws++;
|
|
if (n.classList?.contains("ps-dot")) window.__dots++;
|
|
}
|
|
}).observe(document.querySelector("#panelRouting"), { childList: true, subtree: true });
|
|
return Number(document.querySelector("#panelRouting .pr-req .pr-a .m").textContent.replace(/\D+/g, ""));
|
|
});
|
|
let heard = 0;
|
|
page.on("request", (q) => { if (q.url().includes("/api/gateway/trace")) heard++; });
|
|
// 2 s hidden: 25 requests come, and the panel asks for none
|
|
await page.waitForTimeout(2000);
|
|
assert(heard <= 1, `the trace was read ${heard} times while hidden`);
|
|
const hidden = await page.evaluate(() => ({ draws: window.__draws, dots: window.__dots, shown: Number(document.querySelector("#panelRouting .pr-req .pr-a .m").textContent.replace(/\D+/g, "")) }));
|
|
assert.equal(hidden.draws, 0, `the hidden tab was drawn ${hidden.draws} times over ${heard} requests`);
|
|
assert.equal(hidden.dots, 0, "no request's dot flies in a hidden panel");
|
|
assert.equal(hidden.shown, seqAt, "what the tab shows is left as it was");
|
|
|
|
// shown again: it asks once for what came meanwhile, and draws the
|
|
// newest request first
|
|
await page.evaluate(() => window.__hide(false));
|
|
await page.waitForFunction((at) => Number(document.querySelector("#panelRouting .pr-req .pr-a .m").textContent.replace(/\D+/g, "")) >= at + 20, seqAt);
|
|
assert(heard >= 1, "shown again, the trace is asked for");
|
|
assert.equal(await page.locator("#panelRouting .pr-req").count(), 6);
|
|
assert.deepEqual(errors, []);
|
|
});
|
|
}
|
|
}
|