routing: Routing groups opened from Routing lead back to it

Since Routing groups became a page of their own, a group chip on a
request (or Routing's "Routing groups ›") switched to the groups page
with nothing leading back. Without the sidebar the only way out was the
tab bar, and the request you came from was gone from view.

The groups toolbar now starts with "‹ Routing" (gBack). It goes back to
Routing through window.show, so a dirty draft is still asked about, and
focus returns to the chip or button the page was opened from. Esc does
the same, except while an editor, Import, Select or a modal is open, or
focus is in a text field. With the sidebar the back control shows only
when the page was opened from Routing (.rt-gfrom); from the sidebar's own
Groups tab the page is top level and has none. No other drill-in page in
app.js binds Cmd-[, so neither does this. The label follows the language
on every draw. No new layout, no accent stripe.

Run (mote, playwright 1.62.0):
- routing-groups-back.test.cjs (new): 30/30, Chromium and WebKit x
  en/zh/zh-TW/ja/de x 1280 window, 640 window, 420 web. Red on
  origin/main's assets (MAGPIE_ROUTING_ASSETS): no visible .rt-gback.
- Broken by hand: Escape handler off -> red (chromium en 1280); the
  side-nav hide rule off -> red (webkit en 1280, "no back from the
  sidebar's own tab").
- Every routing*, sidebar*, group-*, i18n-keys, i18n-en-no-cjk,
  old-webkit and settings-groups file at 2d66e9f23 (main 0fc3455e2 +
  this): 3548 tests, 3526 pass, 22 red. The same files at origin/main
  cdf734757 alone: the same 18 red, all filed: i18n-keys (#5332, #5426,
  #5482), routing-intent-entry 8/8 (#5392), routing-context-fold webkit
  (#5586), routing-row-swap (#3799), routing-story-shrinks webkit
  (#3954). The other 4 (routing-busy chromium zh, routing-many-keys
  chromium en, routing-pace-expiry chromium de, routing-rule-drag webkit
  ja) passed whole, with routing-groups-back, at this commit on
  cdf734757 (70/70); filed #5665, #5664, seen in #4082, #3789.
- Whole GUI suite and ./...: queued on mote, not run at push; results to
  follow.

Reported-by: unscientific9 (Discord)
This commit is contained in:
yetone
2026-11-08 11:04:43 +08:00
parent 29bfd5dc8b
commit 4ab1191bc9
4 changed files with 211 additions and 2 deletions
+11
View File
@@ -689,6 +689,17 @@ The window opened with `view=groups&newgroup=<model>` (`mainView` keeps
header's row has no room for it (at 860px it pushed the logo out,
`brand.test.cjs`), so there app.css hides it, `markNav` lights Routing's
tab for both pages, and Routing's head shows `#rtToGroups` instead.
The groups page leads back up as a Mac app's back button does
(unscientific9 on Discord): its toolbar starts with `‹ Routing`
(`.rt-gback`, `groupsBack`), which shows Routing and gives the request's
group it came from the keyboard again. It shows where the page was opened
from Routing (`openedFrom`: a request's group, `#rtToGroups`; `.rt-gfrom`
on `#view-groups`, cleared once the page is hidden) and always where the
header has no tab of its own for the page; beside the sidebar, opened from
its own tab, it has none. Escape does the same while the button shows and
nothing on the page takes it: not in a field, nor with a group's editor,
Import or Select open, nor under a modal. The app has no ⌘[ anywhere, so
none here (`routing-groups-back.test.cjs`).
The page's New group goes through `newGroup`, which asks before replacing a
dirty group draft; Cancel retains the draft and Discard opens the new editor.
`newGroupWith` applies the same guard when creating a group from a model and
+37 -2
View File
@@ -54,7 +54,7 @@ function routingPage(part) {
if (!box) return;
// the groups' way in where the header's row has no tab for them (markNav)
const toGroups = $("#rtToGroups");
if (toGroups) toGroups.onclick = () => window.show("groups");
if (toGroups) toGroups.onclick = async () => { if (await window.show("groups")) openedFrom(toGroups); };
const NS = "http://www.w3.org/2000/svg";
const still = calm;
const shown = () => !$("#view-routing").hidden && !document.hidden;
@@ -2792,6 +2792,8 @@ function routingPage(part) {
// the page put itself back where the reader last left it
const asked = window.scrollOnPurpose?.(e), opens = gView().hidden;
if (opens && !(await window.show("groups"))) return;
// from Routing: its toolbar leads back to the request (gBack)
if (opens && e.target.closest?.("#view-routing")) openedFrom(e.target.closest(".rt-go"));
if (asked) {
readerScrolls(1000);
row.scrollIntoView({ block: "center", behavior: opens || still() ? "auto" : "smooth" });
@@ -5706,6 +5708,38 @@ function routingPage(part) {
const gsec = el("div", "rt-gsec");
const gHead = el("div", "row-head"), gList = el("div", "list rt-groups");
const pHead = el("div", "row-head"), pList = el("div", "list rt-pools");
// the way back up to Routing, at the toolbar's leading edge as a Mac
// app's back button (unscientific9 on Discord: a request's group opened
// this page and nothing led back). Shown where the page was opened from
// Routing (gFrom: a request's group, Routing's own "Routing groups ›"),
// and always where the header has no tab of its own for it, Routing's
// tab standing for both (markNav); beside the sidebar, opened from its
// own tab, it is a page of its own and has none (routing.css). Escape
// does the same while nothing on the page is open to take it
let gFrom = null;
const gBack = el("button", "text rt-gback");
gBack.type = "button";
gBack.append("‹ ", el("span", "", t("Routing")));
gBack.onclick = () => groupsBack();
gHead.append(gBack);
async function groupsBack() {
const from = gFrom;
if (!(await window.show("routing"))) return;
// the request the reader came from, still on the page, has the keyboard
if (from?.isConnected) from.focus({ preventScroll: true });
}
const openedFrom = (from) => { gFrom = from || true; gView()?.classList.add("rt-gfrom"); };
new MutationObserver(() => {
if (!gView().hidden) return;
gFrom = null;
gView().classList.remove("rt-gfrom");
}).observe(gView(), { attributes: true, attributeFilter: ["hidden"] });
document.addEventListener("keydown", (e) => {
if (e.key !== "Escape" || e.defaultPrevented || e.repeat || !gShown() || !gBack.getClientRects().length) return;
if (gEdit || gImport || gSel || !$("#modal")?.hidden || e.target.closest?.("input, textarea, select, [contenteditable]")) return;
e.preventDefault();
groupsBack();
});
// whether magpie finds groups on its own, by the list it fills (蓝猫 on
// Discord: they could only be removed one at a time)
const gFound = el("div", "rt-gfound");
@@ -6155,7 +6189,8 @@ function routingPage(part) {
newBtn.append(svg(PLUS, 11, 1.8), el("span", "", t("New group")));
newBtn.title = t("Make a routing group of any models you like — agents pick it as one model");
newBtn.onclick = () => newGroup();
const head = [];
gBack.lastChild.textContent = t("Routing"); // in the language now
const head = [gBack];
if (all.length > 1 || gQ.value) {
gQ.placeholder = t("Filter groups and models");
gQ.setAttribute("aria-label", gQ.placeholder);
@@ -282,6 +282,9 @@
.rt-gsec > .row-head:first-child .rt-gfilter { flex: 0 1 300px; width: auto; margin: 0; }
.rt-gsec > .row-head:first-child .rt-gcount { margin-left: 10px; white-space: nowrap; }
.rt-gsec > .row-head:first-child .rt-gfilter + .rt-gcount { margin-left: 10px; }
/* the way back to Routing (gBack): beside the sidebar only when the page was opened from Routing */
.rt-gsec > .row-head:first-child .rt-gback { flex: none; margin-right: 8px; white-space: nowrap; }
body.side-nav #view-groups:not(.rt-gfrom) .rt-gback { display: none; }
/* the settings for every group, a card under the list */
.rt-gset > :is(.rt-gfound, .rt-gnames, .rt-gdef) { padding: 12px 14px; }
.rt-gset > :is(.rt-gnames, .rt-gdef) { border-top: 1px solid var(--line-2); }
@@ -0,0 +1,160 @@
// Run with Node's test runner and Playwright on the module path; see README.md.
// Routing groups opened from Routing lead back to it (unscientific9 on
// Discord, 11-08: a request's group opened the Routing groups page, and
// nothing on it went back). Since the groups got a page of their own
// (80ae904c2) a request's group, and Routing's own "Routing groups ›" where
// the header has no tab for them, open that page; its toolbar now starts
// with "‹ Routing", as a Mac app's back button, and Escape does the same
// while nothing on the page takes it (the filter, an open editor). Back on
// Routing, the request's group has the keyboard again. Beside the sidebar,
// the page opened from its own tab is a page of its own and has none.
// Every language, Chromium and WebKit, wide, a narrow window and a phone's
// width; no backend, the API is faked.
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(process.env.MAGPIE_ROUTING_ASSETS || path.join(__dirname, "../assets"));
const models = [
{ id: "a/m", name: "m", providerName: "A", icon: "generic" },
{ id: "b/m", name: "m", providerName: "B", icon: "generic" },
{ id: "a/n", name: "n", providerName: "A", icon: "generic" },
];
const info = (ids) => ids.map((id) => ({ id, ready: true }));
const groups = [
{ id: "auto-m", name: "Model M", members: ["a/m", "b/m"], auto: true, ready: true, memberInfo: info(["a/m", "b/m"]) },
{ id: "mine", name: "Mine", members: ["a/n", "b/m"], ready: true, memberInfo: info(["a/n", "b/m"]) },
];
const routing = { en: "Routing", zh: "路由", "zh-TW": "路由", ja: "ルーティング", de: "Weiterleitung" };
const now = Date.now();
const routes = Array.from({ length: 30 }, (_, i) => ({
id: "r" + i, seq: i + 1, at: new Date(now - i * 60000).toISOString(), agent: "claude", asked: "group/mine",
model: "a/n", provider: "a", status: 200, ms: 900, ttft: 300, in: 100, out: 20, done: true,
group: { id: "mine", name: "Mine" },
}));
function serve(lang, web) {
const state = { agents: [{ id: "claude", name: "Claude Code", path: "/test/claude", fields: [] }], profiles: [], settings: { lang, theme: "light" } };
return async (r) => {
const url = new URL(r.request().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:${web}};` });
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/plugins") return json({ plugins: [] });
if (url.pathname === "/api/gateway/trace") {
if (url.searchParams.get("wait")) return new Promise(() => {}); // nothing more comes
return json({ mine: true, now: new Date().toISOString(), seq: routes.length, totals: { requests: routes.length, rerouted: 0, errors: 0 }, routes });
}
if (url.pathname === "/api/gateway/history") return json({ cut: false, days: [], routes: [] });
if (url.pathname === "/api/groups") return json({ models, pools: [], deciders: [], found: true, groups });
if (url.pathname === "/api/providers") return json({ providers: [], presets: [], excluded: [], gateway: { running: true, window: true, url: "http://127.0.0.1:3999" } });
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)];
try { await r.fulfill({ body: await fs.readFile(file), contentType }); } catch { await r.fulfill({ status: 404, body: "" }); }
};
}
// the page's boot under the whole suite's load (#3478)
const boot = 15000;
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
for (const lang of Object.keys(routing)) {
for (const { width, web } of [{ width: 1280, web: false }, { width: 640, web: false }, { width: 420, web: true }]) {
test(`${engine} ${lang} ${width}px${web ? " web" : " window"}: Routing groups opened from Routing go back to it`, async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
const page = await (await browser.newContext({ viewport: { width, height: 700 }, reducedMotion: "reduce" })).newPage();
t.after(async () => {
if (process.env.ARTIFACT_DIR) {
await fs.mkdir(process.env.ARTIFACT_DIR, { recursive: true });
await page.screenshot({ path: path.join(process.env.ARTIFACT_DIR, `${engine}-${lang}-${width}${web ? "-web" : ""}-routing-groups-back.png`) });
}
await browser.close();
});
page.setDefaultTimeout(5000);
page.setDefaultNavigationTimeout(60000);
const errors = [];
page.on("pageerror", (e) => errors.push(e.message));
await page.route("**/*", serve(lang, web));
await page.goto("http://magpie.test/?view=routing");
await page.locator(".rt-reqs > *").first().waitFor({ state: "attached", timeout: boot });
await page.locator(".rt-group", { hasText: "Mine" }).waitFor({ state: "attached", timeout: boot });
const routingView = page.locator("#view-routing"), groupsView = page.locator("#view-groups");
const back = groupsView.locator(".rt-gback");
const onRouting = async (why) => {
await routingView.waitFor({ state: "visible" });
assert.equal(await groupsView.isHidden(), true, why);
const on = await page.evaluate(() => [...document.querySelectorAll("#nav button[data-view].on")].map((b) => b.dataset.view));
assert.deepEqual(on, ["routing"], "Routing's tab is lit again");
};
// a request's group opens the groups page on its row
const chip = routingView.locator('.rt-go[data-go="group:mine"]').first();
await chip.waitFor({ timeout: boot });
await chip.click();
await groupsView.waitFor();
await groupsView.locator('.rt-group[data-id="mine"]').waitFor();
// its toolbar starts with the way back, named for Routing
await back.waitFor({ state: "visible" });
assert.equal((await back.textContent()).replace("‹", "").trim(), routing[lang]);
const head = groupsView.locator(".rt-gsec > .row-head").first();
assert.equal(await head.evaluate((h, b) => [...h.children].find((c) => c.getClientRects().length) === b, await back.elementHandle()), true, "first in the toolbar");
const bb = await back.boundingBox();
assert(bb && bb.x >= 0 && bb.x + bb.width <= width && bb.y >= 0 && bb.y + bb.height <= 700, "in sight");
assert.equal(await back.evaluate((e) => { const c = getComputedStyle(e); return c.borderLeftWidth !== c.borderRightWidth || c.borderLeftColor !== c.borderRightColor; }), false, "no left-border accent");
const headOver = await head.evaluate((h) => h.scrollWidth - h.clientWidth);
assert(headOver <= 0, `nothing overflows the toolbar (${headOver}px)`);
const over = await page.evaluate(() => document.documentElement.scrollWidth - innerWidth);
assert(over <= 0, `no sideways scroll (${over}px)`);
// it goes back to Routing, the request's group with the keyboard
await back.click();
await onRouting("back on Routing");
assert.equal(await page.evaluate(() => !!document.activeElement?.matches('#view-routing .rt-go[data-go="group:mine"]')), true, "the request's group has the keyboard");
// Escape goes back too, but not out of the filter or an open editor
await chip.click();
await back.waitFor({ state: "visible" });
const filter = groupsView.locator(".rt-gfilter");
await filter.focus();
await page.keyboard.press("Escape");
assert.equal(await groupsView.isVisible(), true, "Escape in the filter stays");
await groupsView.locator(".rt-gnew").click();
await groupsView.locator(".rt-gedit").first().waitFor();
await groupsView.locator(".rt-gedit button").first().focus();
await page.keyboard.press("Escape");
assert.equal(await groupsView.isVisible(), true, "Escape with an editor open stays");
await groupsView.locator(".rt-gedit").first().evaluate(() => window.magpie.call("routing", "discard"));
await groupsView.locator(".rt-gedit").waitFor({ state: "detached" });
await page.evaluate(() => document.activeElement?.blur());
await page.keyboard.press("Escape");
await onRouting("Escape goes back");
const side = await page.evaluate(() => document.body.classList.contains("side-nav"));
if (side) {
// beside the sidebar, its own tab opens a page of its own: no way
// back, and Escape leaves it where it is
await page.locator('#nav button[data-view="groups"]').click();
await groupsView.waitFor();
await groupsView.locator('.rt-group[data-id="mine"]').waitFor();
assert.equal(await back.isVisible(), false, "no back from the sidebar's own tab");
await page.keyboard.press("Escape");
assert.equal(await groupsView.isVisible(), true, "Escape stays");
} else {
// the header's row has no tab for them: Routing's head opens them,
// and they lead back
await page.locator("#rtToGroups").click();
await back.waitFor({ state: "visible" });
await back.click();
await onRouting("back from Routing's own way in");
}
assert.deepEqual(errors, []);
});
}
}
}