diff --git a/internal/gui/assets/app.js b/internal/gui/assets/app.js index 672df48e9..bbad144ec 100644 --- a/internal/gui/assets/app.js +++ b/internal/gui/assets/app.js @@ -5946,7 +5946,9 @@ async function loadProvidersNow() { if (providersPage(view)) { magpie.call(view, "render", was); backAfterLoad($("#view-" + view)); - } else renderProviders(); + // under Routing: drawn when opened (#4652) + } else if (view === "routing") { providersBehind = true; markNewPartners(); } + else renderProviders(); renderArchive(); providersWhileFetching(); if (view === "providers") loadUpstream(); @@ -6080,11 +6082,11 @@ $("#foldOff").onclick = () => { // One row per provider: logo, name, the agents pointed at it, key status. // Everything else lives in the editor, a dialog over the page. +let providersBehind = false; function renderProviders(quiet) { if (accountArranging || providerArranging) { accountRenderPending = true; return; } - // Rebuilding the list empties the page for a moment, which clamps its - // scroll to the top; put it back so closing the editor leaves the reader - // where they were. + providersBehind = false; + // a rebuild clamps the scroll to the top: put it back const view = $("#view-providers"), top = view.scrollTop; markNewPartners(); keepIcons($("#providers"), $("#offProviders"), $("#addSheet"), $("#excluded")); @@ -23182,6 +23184,7 @@ async function show(v) { if (v !== "providers" && editing !== null) cancelEdit(true); // a plugin's page loads what it shows; back once it has magpie.call(v, "open")?.then?.(back); + if (v === "providers" && providersBehind) renderProviders(); if (v === "providers" || v === "routing" || providersPage(v)) loadProviders().then(back, statusErr); // Routing's accounts show their windows as the Usage page read them // (#1833): what is kept, read in an ordinary way, never an asked read diff --git a/internal/gui/assets/context.js b/internal/gui/assets/context.js index fb94f0418..76a5c8b46 100644 --- a/internal/gui/assets/context.js +++ b/internal/gui/assets/context.js @@ -206,6 +206,7 @@ for (const e of all) { if (!e.target.isConnected) { cardSizes.unobserve(e.target); continue; } const card = e.target, w = e.contentRect.width; + if (!w) continue; // out of sight (#4652) cardWidth[card.dataset.place] = w; // on the next frame: a class changed in the observer's own call // changes the card's height, which WebKit reports as a diff --git a/internal/gui/assets/plugins/routing/client.js b/internal/gui/assets/plugins/routing/client.js index 5cee49902..7774c38fe 100644 --- a/internal/gui/assets/plugins/routing/client.js +++ b/internal/gui/assets/plugins/routing/client.js @@ -269,9 +269,12 @@ function routingPage(part) { // scrolling view back from its end a frame after each scroll there, so // the page couldn't be scrolled to its bottom (#1249). Set on the next // frame: a class changed in the observer's own call changes the box's - // height, which WebKit reports as a ResizeObserver loop (see quotaFit) + // height, which WebKit reports as a ResizeObserver loop (see quotaFit). + // Out of sight a box is 0 wide: it keeps the layout it had, or coming + // back laid out narrow and then, a frame later, wide again (#4652) const byWidth = (node, marks) => new ResizeObserver(([e]) => { const w = e.contentRect.width; + if (!w) return; requestAnimationFrame(() => { for (const [cls, fits] of Object.entries(marks)) node.classList.toggle(cls, fits(w)); }); @@ -309,9 +312,10 @@ function routingPage(part) { }); }; // only a new width can make the numbers wrap or fit: measured on its // height too, the mark it sets made the rows taller and measured them - // again, flipping back and forth (#2524: 8 times in 8s, ~200ms each) + // again, flipping back and forth (#2524: 8 times in 8s, ~200ms each). + // Out of sight (0 wide) is no new width: back, nothing is measured (#4652) let metaW = -1; - new ResizeObserver(() => { const w = colA.clientWidth; if (w !== metaW) { metaW = w; metaFit(); } }).observe(colA); + new ResizeObserver(() => { const w = colA.clientWidth; if (w && w !== metaW) { metaW = w; metaFit(); } }).observe(colA); const path = () => { const p = document.createElementNS(NS, "path"); wires.appendChild(p); return p; }; const setText = (e, s) => { if (e.textContent !== s) e.textContent = s; }; @@ -2286,13 +2290,21 @@ function routingPage(part) { // 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) + // each replaced node alive (#2515, #2513). Back in sight with the stage + // built from what it would be built from now ("back"), it is brought up + // to date as in sight, not taken down and built again: the same agents, + // accounts and wires made anew each time the page was picked (#4652) + let builtFrom = []; function sync(force) { if (!shown()) return; const rs = staged(); if (!rs.length) return; - if (force) return steady(() => rebuild(rs, true)); - rebuild(rs, false); + const from = () => [cur, pinned, day, rp, purpose.join(), document.documentElement.lang, ...rs, + ...rs.flatMap((r) => [...r.order, ...(r.left || [])].map(who))]; + const now = from(), same = now.length === builtFrom.length && now.every((x, i) => x === builtFrom[i]); + if (force === "back" ? !same : force) steady(() => rebuild(rs, true)); + else rebuild(rs, false); + builtFrom = from(); // as built: rebuild takes cur from the routes } function rebuild(rs, force) { // live, the request the card tells of stays (told), not the newest staged @@ -3797,7 +3809,11 @@ function routingPage(part) { // (fitCols). Digits are as wide as each other there (tabular-nums), so a // probe stands for every row of its shape. The rows were a subgrid of // the list once, and Safari 16 crashed sizing them (#3731). - const WIN_MIN = 300; + // + // From 100 rows: at 300, a day of 180 to 298 kept every row in, and + // each pick of the page laid them all out (#4652). The live list keeps + // 60, so it is never windowed. + const WIN_MIN = 100; let listItems = [], winKey = "", winAvg = 0, winSoon = 0, rowsMoved = false; const heights = new Map(); // a request's id, or a session's head → its height const gapAbove = el("div", "rt-gap"), gapBelow = el("div", "rt-gap"); @@ -3923,8 +3939,12 @@ function routingPage(part) { } reqs.classList.add("win"); // out of sight (another view, the window hidden) nothing has a height: - // drawn as last measured, and measured once in sight again + // drawn as last measured, and measured once in sight again. A list + // already windowed keeps the rows it has: drawn for a 0-high sight + // they were fewer, and coming back put the rest in and measured every + // row's numbers again (#4652). In sight, the list's observer draws it const live = !!reqs.offsetParent; + if (!live && winKey && reqs.contains(gapAbove)) return; const listKey = [colA.className, reqs.clientWidth, reqs.style.getPropertyValue("--rt-cols"), document.documentElement.lang, visibleMetrics.join()].join("|"); // other columns (a wider probe come in): the heights measured in the // old ones dropped, their average kept for the rows not drawn again @@ -4730,7 +4750,7 @@ function routingPage(part) { stopPlays(); if (!loaded) return; if (!pinned && !rp) cur = told(); - if (cur) sync(true); + if (cur) sync("back"); else empty(); for (const r of underWay()) play(r.id); renderAll(); @@ -4847,6 +4867,7 @@ function routingPage(part) { subs.clear(); for (const f of folds.values()) f.wire.remove(); folds.clear(); + builtFrom = []; chip.hidden = true; headOf = null; hubText(); @@ -7364,7 +7385,8 @@ function routingPage(part) { })); } // loaded when the view is shown, and again when the window comes back - new MutationObserver(() => { if (!$("#view-routing").hidden) loadGroups(); }).observe($("#view-routing"), { attributes: true, attributeFilter: ["hidden"] }); + // back on the page, the same groups aren't drawn again either (#4652) + new MutationObserver(() => { if (!$("#view-routing").hidden) loadGroups(true); }).observe($("#view-routing"), { attributes: true, attributeFilter: ["hidden"] }); window.addEventListener("focus", () => { if (shown()) loadGroups(true); }); // newGroupWith: a new group's editor, opened with the model in it — a // model of a provider kept for routing groups that no group has, which diff --git a/internal/gui/tests/routing-return-cost.test.cjs b/internal/gui/tests/routing-return-cost.test.cjs new file mode 100644 index 000000000..f1f3e309b --- /dev/null +++ b/internal/gui/tests/routing-return-cost.test.cjs @@ -0,0 +1,160 @@ +// Run with Node's test runner and Playwright on the module path; see README.md. +// Back on Routing from another page, with a day of a few hundred requests +// listed, the page doesn't stall (xisdumpling, #4652: 180–298 requests on +// Windows, the page and the sidebar's highlight stopped for a moment each +// time Routing was picked again), a request picked. What coming back costs is counted, not +// timed: while Routing is out of sight its boxes keep the layout they had +// (a hidden box measures 0 wide, and it took the narrow layout, to go wide +// again a frame after it came back), the stage keeps its nodes when nothing +// on it changed, the routing groups and the hidden Providers page aren't +// drawn again, and the list draws the rows in and near its sight, not all +// of them. In Chromium the layouts are counted too (LayoutCount): 24 each +// time before, 8 after, of which the frame's own and the sidebar's. In +// Chromium and WebKit. +const assert = require("node:assert/strict"); +const { test } = require("node:test"); +const fs = require("node:fs/promises"); +const path = require("node:path"); +const { chromium, webkit } = require("playwright"); + +const assets = path.resolve(__dirname, "../assets"); +const now = Date.now(), iso = (ms) => new Date(ms).toISOString(); +const N = 250; // between the reporter's 180 and 298 +const seat = (n) => ({ id: `acct-${n}`, provider: `prov${n}`, name: `Provider ${n}`, who: `user${n}@example.com`, kind: "account", model: `model-${n}`, known: true, plan: "Pro" }); +const agents = ["codex", "claude"]; +const prompt = (n) => ({ window: 272000, tokens: 90000 + n * 800, counted: true, turns: n % 9, + parts: [{ kind: "system", tokens: 9000, items: [{ name: "prompt", tokens: 9000 }] }, { kind: "chat", tokens: 81000 + n * 800, items: [{ name: "turn", tag: "turns", n: n % 9, tokens: 81000 + n * 800 }] }] }); +function req(id) { + const t0 = now - (N + 10 - id) * 30000, order = [seat(id % 3), seat((id + 1) % 3)]; + return { id, seq: id, time: iso(t0), agent: agents[id % 2], session: "s" + (id % 4), model: order[0].model, provider: order[0].provider, order, + tries: [{ id: order[0].id, model: order[0].model, start: iso(t0), done: true, status: 200, ms: 1000 + (id % 7) * 900, ttft: 300 + id % 5 * 70 }], + done: true, status: 200, ms: 1000 + (id % 7) * 900, ttft: 300 + id % 5 * 70, tokens: 2000 + id * 37, cost: 0.01 * (id % 9), priced: true, + usage: [{ in: 3000 + id, cache_read: 40000 + id * 11, out: 400 + id }], prompt: prompt(id) }; +} +const routes = Array.from({ length: N }, (_, i) => req(i + 1)); +const DAY = new Date(now - 864e5 * 2).toISOString().slice(0, 10); // a day the history keeps, picked +const models = [{ id: "a/sol", name: "sol", providerName: "A", icon: "generic" }, { id: "b/flash", name: "flash", providerName: "B", icon: "generic" }]; +const member = { members: ["a/sol", "b/flash"], ready: true, memberInfo: [{ id: "a/sol", ready: true }, { id: "b/flash", ready: true }], offers: [], shared: [] }; +const groups = { models, pools: [], groups: [{ ...member, id: "hand", name: "Hand", routing: "manual", pick: "a/sol", picked: "a/sol" }, { ...member, id: "auto", name: "Auto", routing: "order" }] }; +const providers = { presets: [], excluded: [], gateway: { running: true, window: true, url: "http://127.0.0.1:3999" }, + providers: Array.from({ length: 6 }, (_, i) => ({ id: "p" + i, name: "Provider " + i, icon: "generic", models: [], agents: [], key: { set: true, masked: "sk-…ab1" + i } })) }; + +function serve(feed) { + const state = { agents: agents.map((id) => ({ id, name: id, path: "/test/" + id, fields: [] })), profiles: [], settings: { lang: "en", theme: "light" } }; + return async (route) => { + const url = new URL(route.request().url()); const json = (d) => route.fulfill({ json: d }).catch(() => {}); + if (url.pathname === "/boot.js") return route.fulfill({ contentType: "text/javascript", body: 'window.bootPrefs = {lang:"en",theme:"light",web:true};' }); + 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/gateway/trace") { + const trace = (rs, seq) => json({ mine: true, now: new Date().toISOString(), seq, totals: { requests: N, rerouted: 0, errors: 0 }, routes: rs }); + if (!url.searchParams.get("wait")) return trace(routes.slice(-3), N); + while (!feed.closed) await new Promise((r) => setTimeout(r, 50)); // the long poll: nothing new + return trace([], N); + } + if (url.pathname === "/api/gateway/history") return json({ cut: false, days: [{ day: DAY, requests: N }], routes: url.searchParams.get("day") === DAY ? routes : [] }); + if (url.pathname === "/api/groups") return json(groups); + if (url.pathname === "/api/providers") return json(providers); + if (url.pathname.startsWith("/api/")) return json({}); + const file = path.join(assets, url.pathname === "/" ? "index.html" : url.pathname); + const ct = { ".html": "text/html", ".js": "text/javascript", ".css": "text/css", ".svg": "image/svg+xml", ".png": "image/png" }[path.extname(file)]; + await route.fulfill({ body: await fs.readFile(file).catch(() => ""), contentType: ct }); + }; +} + +// what the page does from here on, counted +const watch = () => { + const w = window.__w = { flips: [], providers: 0, groups: 0, story: 0 }; + const marks = /\b(max\d+|min\d+|meta-under)\b/g; + const set = (s) => (s || "").match(marks)?.sort().join(" ") || ""; + new MutationObserver((ms) => { for (const m of ms) if (set(m.oldValue) !== set(m.target.getAttribute("class"))) w.flips.push(m.target.className); }) + .observe(document.querySelector("#view-routing"), { subtree: true, attributes: true, attributeFilter: ["class"], attributeOldValue: true }); + new MutationObserver((ms) => { w.providers += ms.length; }).observe(document.querySelector("#view-providers"), { subtree: true, childList: true }); + new MutationObserver((ms) => { w.groups += ms.length; }).observe(document.querySelector("#view-routing .rt-groups"), { subtree: true, childList: true }); + // the picked request's story: its head, its steps and its context card + for (const s of [".rt-log-head", ".rt-steps", ".ctx-card"]) new MutationObserver((ms) => { w.story += ms.length; }).observe(document.querySelector("#view-routing " + s), { subtree: true, childList: true, characterData: true }); + // the stage's nodes as they are now + for (const e of document.querySelectorAll("#view-routing .rt-srcs > *, #view-routing .rt-accts li")) e.__was = true; +}; +const counted = () => { + const stage = [...document.querySelectorAll("#view-routing .rt-srcs > *, #view-routing .rt-accts li")]; + return { ...window.__w, stage: stage.length, stageKept: stage.filter((e) => e.__was).length, + rows: document.querySelectorAll("#view-routing .rt-reqs > .rt-req").length, + providerRows: document.querySelector("#providers").children.length, + nodes: document.querySelectorAll("#view-routing *").length }; +}; + +for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) { + test(`${engine}: back on Routing with ${N} requests listed, the page isn't laid out and drawn again whole`, async (t) => { + const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" })); + t.after(() => browser.close()); + const context = await browser.newContext({ viewport: { width: 1400, height: 900 } }); + const page = await context.newPage(); + page.setDefaultTimeout(10000); + page.setDefaultNavigationTimeout(60000); + const errors = []; + page.on("pageerror", (e) => errors.push(e.message)); + const feed = { closed: false }; + t.after(() => { feed.closed = true; }); + await page.route("**/*", serve(feed)); + const cdp = engine === "chromium" ? await context.newCDPSession(page) : null; + if (cdp) await cdp.send("Performance.enable"); + const layouts = async () => { + if (!cdp) return null; + const { metrics } = await cdp.send("Performance.getMetrics"); + const m = Object.fromEntries(metrics.map((x) => [x.name, x.value])); + return { layouts: m.LayoutCount, styles: m.RecalcStyleCount, ms: m.LayoutDuration * 1000 }; + }; + // settled: no frame of the page's for a while asks for anything new + const settle = () => page.waitForTimeout(1200); + + await page.goto("http://magpie.test/?view=routing"); + await page.locator(".rt-req").first().waitFor(); + await page.locator(".rt-hub").waitFor(); + await page.locator(".rt-days .rt-day:not(.rt-day-more)").nth(1).click(); + await page.waitForFunction((n) => document.querySelectorAll("#view-routing .rt-req").length > 3 && document.querySelector(".rt-day.on")?.textContent.includes(String(n)), N); + // a request picked: its story and its context card are shown too + await page.locator("#view-routing .rt-reqs > .rt-req").nth(2).click(); + await page.locator("#view-routing .ctx-card").waitFor(); + await settle(); + const wide = await page.evaluate(() => document.querySelector("#view-routing .rt-col").className); + assert(!/max760/.test(wide), `the list's column is wide at 1400px: ${wide}`); + + const nav = (v) => page.locator(`#nav button[data-view="${v}"]`).click(); + await nav("providers"); + await page.locator("#view-providers").waitFor(); + await settle(); + await page.evaluate(watch); + const l0 = await layouts(); + await nav("routing"); + await settle(); + const l1 = await layouts(); + const back = await page.evaluate(counted); + + // and out once more, then back, all of it counted from leaving + await page.evaluate(watch); + await nav("providers"); + await settle(); + const away = await page.evaluate(counted); + await page.evaluate(() => { window.__w.groups = 0; window.__w.providers = 0; }); + const l2 = await layouts(); + await nav("routing"); + await settle(); + const l3 = await layouts(); + const again = await page.evaluate(counted); + // the time is logged, not judged: a busy machine's, not the page's + const lay = cdp ? { back: l1.layouts - l0.layouts, styles: l1.styles - l0.styles, again: l3.layouts - l2.layouts, againMs: +(l3.ms - l2.ms).toFixed(1) } : null; + console.log(`${engine} #4652:`, JSON.stringify({ lay, back, away: { flips: away.flips.length, providers: away.providers }, again: { ...again, flips: again.flips.length } })); + + assert.deepEqual(away.flips, [], "out of sight, Routing's boxes keep the layout they had"); + assert.deepEqual(again.flips, [], "back in sight, no box goes narrow and wide again"); + assert.equal(again.providers, 0, "the hidden Providers page isn't drawn again while Routing is shown"); + assert.equal(again.groups, 0, "the routing groups, the same as before, aren't drawn again"); + assert.equal(again.story, 0, "the picked request's story and context card, the same, aren't drawn again"); + assert(again.stage > 0, "the stage has its agents and accounts"); + assert.equal(again.stageKept, again.stage, "the stage keeps its nodes when nothing on it changed"); + assert(again.rows > 0 && again.rows < N / 2, `the list draws the rows in and near its sight, not all ${N}: ${again.rows}`); + if (lay) assert(lay.again <= 12, `coming back lays the page out a few times at most, not once for each box: ${lay.again}`); + assert.deepEqual(errors, []); + }); +}