gui: the tray panel fits its tallest tab whole, and Routing's first open holds its height

CyberCaius (Discord, Windows): the tray panel jitters or shows a
scrollbar, and today's call count is slow on the first open.

- fit() measured the sections alone, so the view's padding, the gap
  under the tabs and what sits around a section were left out: 10px
  short on Routing, 31px with 8 agents. It now lays out every section
  at once (no data-ptab for the measure), sums each tab's sections by
  their margin box, skips what is display:none, and adds what lies
  outside the view. It never shows one tab after another: hiding the
  open tab ends its running transitions (panel-fold's unroll,
  tray-cell-click's refresh).
- Routing's placeholder was three short bars, and the panel grew when
  the trace landed. It is now a hidden copy of the stage and six
  request rows, so it is as tall as what comes.
- Today's line (usage?period=today) is asked for when the page is
  built, not after the first trace.

Test: internal/gui/tests/panel-fit-tabs.test.cjs, chromium and webkit
x en, zh, zh-TW, ja, de. 8 and 2 agents: no tab overflows the fitted
panel. Trace held: today's 42 shows before it, the fit is the same
after it, Routing doesn't overflow. Scrolled by wheel in a 220px
panel: fit(40); fit() leaves scrollTop where it was, with no scroll
events.

Run at tree 53ae48f72 (on origin/main 3b5a2f612): go test -tags nogui
-timeout 45m ./internal/usage ./internal/gui ok (TestCoreScriptBudget
included, no budget raised; app.js 1,317,465 of 1,317,486). GUI files
panel*, tray*, i18n-keys, routing-sessions (27 files, chromium and
webkit): 336 pass, 0 fail. gofmt clean. Earlier, at 7571ebc93: full
GUI suite 1095 pass; the 4 reds are two tests red on main too (#3497,
removed by hand gives "changed height 630 !== 561".
This commit is contained in:
yetone
2026-10-24 23:49:08 +08:00
parent 7034378669
commit 2a8a177466
6 changed files with 206 additions and 15 deletions
+2 -1
View File
@@ -4073,7 +4073,8 @@ body.window .row.agent.gone > .ag-conn { margin-left: auto; }
.guide-more { color: var(--muted); font-size: 12px; }
body.panel .guide { padding: 10px 12px 4px; border-radius: 10px; }
body.panel .guide-step { gap: 10px; padding: 8px 0; }
body.panel:not([data-ptab="agents"]) #agentsLead { display: none; }
/* no tab is fit()'s measure, every section shown */
body.panel[data-ptab]:not([data-ptab="agents"]) #agentsLead { display: none; }
/* the first line keeps its place opened: a line of its own height, the
row not centring what it holds; no gap between its lines, which the
opened part couldn't fold away: it starts where the shut row ended */
+12 -6
View File
@@ -3979,16 +3979,22 @@ if (mode === "window") {
// extra is room about to be taken (or given back), e.g. by agents unrolling;
// glide moves the panel's edge there over time instead of at once.
// The panel is as tall as its tallest tab, not the one showing, so it keeps
// its height as the tabs change and a shorter tab leaves room below (#124):
// every tab is laid out at once for the measure, before anything is drawn.
// its height as the tabs change and a shorter tab leaves room below (#124).
// Each tab counts its view's padding and margins too (sections alone left
// a scrollbar), all laid out at once: hiding the open tab ends its motion.
function fit(extra = 0, glide) {
if (mode !== "panel") return;
const body = document.body, tab = body.dataset.ptab;
const body = document.body, view = $("#view-agents"), tab = body.dataset.ptab, vs = getComputedStyle(view);
delete body.dataset.ptab;
// extra is only ever the agents' (a row opening, the scroll unrolling)
const tallest = Math.max($("#agents").offsetHeight + extra, $("#panelQuota").offsetHeight, $("#panelRouting").offsetHeight, $("#panelUsage").offsetHeight);
const need = { agents: extra, usage: 0, stats: 0, routing: 0 };
for (const c of view.children) {
const cs = getComputedStyle(c), px = cs.display === "none" ? 0 : c.offsetHeight + parseFloat(cs.marginTop) + parseFloat(cs.marginBottom);
const only = { agentsLead: "agents", agents: "agents", panelQuota: "usage", panelUsage: "stats", panelRouting: "routing" }[c.id];
for (const t in need) if (!only || only === t) need[t] += px;
}
let h = body.clientHeight - view.offsetHeight + parseFloat(vs.paddingTop) + parseFloat(vs.paddingBottom) + Math.max(...Object.values(need));
if (tab) body.dataset.ptab = tab;
const h = $(".top").offsetHeight + $("#ptabs").offsetHeight + tallest + $(".foot").offsetHeight + 4;
h = Math.ceil(h);
if (h === fit.last) return;
fit.last = h;
const still = !glide || calm();
+26 -1
View File
@@ -6458,7 +6458,7 @@ function routingPage(part) {
if (picks) out.push(picks);
if (!offMsg && loaded && rs.length) out.push(pStage);
if (offMsg) out.push(el("p", "pr-none", t(offMsg)));
else if (!loaded) for (let i = 0; i < 3; i++) out.push(el("span", "skeleton pr-sk-row"));
else if (!loaded) out.push(...pGhost());
else if (!rs.length) {
const p = el("div", "pr-none");
p.append(el("b", "", t("No request yet")), t("Every request an agent sends to magpie shows up here, routed for real."));
@@ -6474,6 +6474,28 @@ function routingPage(part) {
pWake();
fit();
}
// pGhost holds the tab's place while the trace is on its way: the stage
// and P_ROWS requests, laid out as they will be and drawn as skeletons,
// so the panel, fitted to its tallest tab, doesn't grow when they come
// (CyberCaius on Discord). A shorter skeleton had the panel grow on the
// first open, and a scrollbar show till it did.
function pGhost() {
const stage = el("div", "skeleton pr-ghost");
stage.append(el("div", "pr-stage"));
const list = el("div", "pr-list");
for (let i = 0; i < P_ROWS; i++) {
const row = el("span", "pr-req");
const asked = el("span", "pr-a");
asked.append(el("span", "pr-who", "\u00a0"), el("code", "m", "\u00a0"));
const line = el("span", "pr-line");
line.append(el("span", "pr-to", "\u00a0"), el("span", "meta", "\u00a0"));
row.append(asked, el("span", "at", "\u00a0"), line);
list.append(row);
}
const rows = el("div", "skeleton pr-ghost");
rows.append(list);
return [stage, rows];
}
// a Manual group's model, switched here as its card's radios do (#2489,
// Jerell.OvO on Discord): CC Switch's one provider on at a time, from
// the tray. A row a group of two or more that is on, its picked
@@ -6582,5 +6604,8 @@ function routingPage(part) {
new ResizeObserver(fitSoon).observe(box);
words();
start();
// the tray panel's line of today's totals is asked for at once, not
// after the trace's first answer, which it doesn't need (CyberCaius)
if (pBox) loadToday();
poll();
}
@@ -821,7 +821,9 @@ button.rt-session { cursor: pointer; }
.pr-today b { color: var(--fg); font-weight: 600; }
.pr-head .text { flex: none; font-size: 12px; }
.skeleton.pr-sk { display: inline-block; width: 150px; height: 9px; border-radius: 4px; vertical-align: middle; }
.skeleton.pr-sk-row { height: 40px; border-radius: 11px; }
/* the stage and the requests to come, their own size, as skeletons */
.skeleton.pr-ghost { border-radius: 11px; }
.pr-ghost > * { visibility: hidden; }
/* a Manual group's model, picked from the tray (#2489) */
.pr-picks { display: flex; flex-direction: column; padding: 4px; border-radius: 11px; background: var(--card); box-shadow: var(--card-edge); }
.pr-pick { display: flex; align-items: center; gap: 10px; min-height: 30px; padding: 2px 4px 2px 8px; font-size: 12px; }
+154
View File
@@ -0,0 +1,154 @@
// Run with Node's test runner and Playwright on the module path; see README.md.
// The tray panel's height (fit) is its tallest tab's, every part of it
// counted: the header, the tabs and the gap under them, the view's padding,
// what sits around a tab's section (the Get started card, the fold under
// the agents) and the foot. Summing the sections alone left the view short
// by 10px on Routing and 31px on 8 agents, so a scrollbar showed
// (CyberCaius on Discord, on Windows). And on the first open, while the
// trace is on its way, the Routing tab's skeleton is as tall as what comes,
// so the panel doesn't grow when it lands; today's line is asked for at
// once, not after the trace.
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 now = new Date();
const at = (m) => new Date(now - m * 60e3).toISOString();
const relay = { id: "relay:team", provider: "relay", name: "Relay", kind: "key", who: "team", model: "gpt-6-sol" };
const routes = [1, 2, 3, 4, 5, 6, 7, 8].map((i) => ({
id: 100 + i, seq: 100 + i, time: at(i), agent: "codex", model: "relay/gpt-6-sol", provider: "relay", order: [relay],
tries: [{ id: relay.id, model: "gpt-6-sol", start: at(i), done: true, status: 200, ms: 300 }], done: true, status: 200, ms: 300, tokens: 1000,
}));
const ids = ["codex", "claude", "gemini", "opencode", "kimi", "qwen", "crush", "goose"];
// held: promises the trace's and today's first answers wait for
function serve(page, lang, agents, held = {}) {
const state = { agents: ids.slice(0, agents).map((id) => ({ id, name: id, path: "/test/" + id, fields: [] })), profiles: [], settings: { lang, theme: "light" } };
const fits = [];
const handler = async (route) => {
const url = new URL(route.request().url());
const json = (data) => route.fulfill({ json: data });
if (url.pathname === "/boot.js") return route.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:"${lang}",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") {
if (url.searchParams.get("wait")) await new Promise((r) => setTimeout(r, 20e3));
else await held.trace;
return json({ mine: true, now: now.toISOString(), seq: 108, totals: { requests: 8, rerouted: 0, errors: 0 }, routes });
}
if (url.pathname === "/api/usage" && url.searchParams.get("period") === "today") {
await held.today;
return json({ calls: 42, input: 1000000, output: 234000, cache_read: 0, cache_write: 0, reasoning: 0, cost: 0, series: [] });
}
if (url.pathname === "/api/window/fit") {
// the window takes the height asked for, as the tray's does
const h = Number(url.searchParams.get("h"));
fits.push(h);
await page.setViewportSize({ width: 380, height: h }).catch(() => {});
return route.fulfill({ status: 204 });
}
if (url.pathname === "/api/gateway/history") return json({ cut: false, days: [], routes: [] });
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/")) 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 route.fulfill({ body: await fs.readFile(file), contentType });
};
return { handler, fits };
}
// the view's overflow on each tab, after the panel was fitted
async function overflows(page) {
const out = {};
for (const tab of ["agents", "usage", "stats", "routing"]) {
const b = page.locator(`#ptabs [data-ptab="${tab}"]`);
if (!(await b.isVisible())) continue;
await b.click();
await page.waitForTimeout(150);
out[tab] = await page.evaluate(() => { const v = document.querySelector("#view-agents"); return v.scrollHeight - v.clientHeight; });
}
return out;
}
// a fit that has settled: no new height asked for a while
async function settled(page, fits) {
let n = -1;
while (n !== fits.length) { n = fits.length; await page.waitForTimeout(400); }
return fits[fits.length - 1];
}
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
for (const lang of ["en", "zh", "zh-TW", "ja", "de"]) {
test(`${engine} ${lang}: the tray panel fits every tab whole`, async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(() => browser.close());
const errors = [];
const open = async (agents, held) => {
const page = await (await browser.newContext({ viewport: { width: 380, height: 200 }, reducedMotion: "reduce" })).newPage();
page.setDefaultTimeout(8000);
page.on("pageerror", (e) => errors.push(e.message));
const s = serve(page, lang, agents, held);
await page.route("**/*", s.handler);
await page.goto("http://magpie.test/?mode=panel");
return { page, fits: s.fits };
};
// eight agents, and Routing with its stage and six requests: no tab
// scrolls in the height the panel asked for
for (const agents of [8, 2]) {
const { page, fits } = await open(agents);
await page.locator("#panelRouting .pr-req").first().waitFor({ state: "attached" });
await page.locator("#agents .row.agent").nth(agents - 1).waitFor();
await settled(page, fits);
const over = await overflows(page);
for (const [tab, px] of Object.entries(over)) assert(px <= 0, `${agents} agents: the ${tab} tab overflows the fitted panel by ${px}px (${JSON.stringify(over)}, fits ${fits.join(" ")})`);
await page.context().close();
}
// the first open: the trace held back, today's line comes without it,
// and Routing's skeleton holds the height its requests take
let release;
const trace = new Promise((r) => { release = r; });
const { page, fits } = await open(2, { trace });
await page.locator("#agents .row.agent").nth(1).waitFor();
await page.waitForFunction(() => /42/.test(document.querySelector("#panelRouting .pr-today")?.textContent || ""), null, { timeout: 3000 })
.catch(() => assert.fail("today's line waited for the trace: " + JSON.stringify(fits)));
const before = await settled(page, fits);
release();
await page.locator("#panelRouting .pr-req").first().waitFor({ state: "attached" });
const after = await settled(page, fits);
assert.equal(after, before, `the panel changed height when the trace landed: ${fits.join(" ")}`);
const over = await overflows(page);
assert(over.routing <= 0, `Routing overflows by ${over.routing}px`);
// a fit measured with the view scrolled leaves it where it is: a
// panel kept shorter than its agents (a small screen) scrolled down
await page.route("**/api/window/fit*", (route) => route.fulfill({ status: 204 }));
await page.locator('#ptabs [data-ptab="agents"]').click();
await page.setViewportSize({ width: 380, height: 220 });
// scrolled as a reader does, which the page's scroll guard lets be
const box = await page.evaluate(() => { const r = document.querySelector("#view-agents").getBoundingClientRect(); return { x: r.x + r.width / 2, y: r.y + Math.min(r.height / 2, 40) }; });
await page.mouse.move(box.x, box.y);
await page.mouse.wheel(0, 400);
await page.waitForTimeout(400);
const kept = await page.evaluate(async () => {
const v = document.querySelector("#view-agents");
const top = v.scrollTop;
let moved = 0;
v.addEventListener("scroll", () => moved++);
fit(40);
fit();
await new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(r)));
return { top, now: v.scrollTop, moved };
});
assert(kept.top > 0, "the agents scroll in a short panel");
assert.deepEqual({ now: kept.now, moved: kept.moved }, { now: kept.top, moved: 0 }, "measuring the tabs scrolled the view");
assert.deepEqual(errors, []);
});
}
}
+9 -6
View File
@@ -196,15 +196,18 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium",
assert(tip.includes("Claude") && tip.includes("Relay"), tip);
await p.mouse.move(2, 2);
// Native fit includes the Usage tab, and all controls fit without scrolling.
const height = await p.evaluate(() => {
const body = document.body, tab = body.dataset.ptab;
delete body.dataset.ptab;
const h = document.querySelector('.top').offsetHeight + document.querySelector('#ptabs').offsetHeight + Math.max(...['agents','panelQuota','panelRouting','panelUsage'].map(id => document.getElementById(id).offsetHeight)) + document.querySelector('.foot').offsetHeight + 4;
// Native fit includes the Usage tab, and all controls fit without
// scrolling: each tab's whole layout (the view's padding, the header,
// tabs and foot), not its section's height alone
const need = await p.evaluate(() => {
const body = document.body, v = document.querySelector("#view-agents"), tab = body.dataset.ptab, out = {};
for (const t of ["agents", "usage", "stats", "routing"]) { body.dataset.ptab = t; out[t] = Math.ceil(body.clientHeight - v.offsetHeight + v.scrollHeight); }
body.dataset.ptab = tab;
return h;
return out;
});
const height = Math.max(...Object.values(need));
for (let i=0;i<40 && fitted.at(-1) !== height;i++) await p.waitForTimeout(25);
assert(fitted.at(-1) >= need.usage, `the panel fits the Usage tab: ${fitted.at(-1)} < ${need.usage}`);
assert.equal(fitted.at(-1),height,"the panel fits its tallest tab including Usage");
const scroll = () => p.locator("#view-agents").evaluate(v => v.scrollTop);
const before = await scroll();