routing: a pick lets the stage go live when the reader is back to newer requests

未寒 on Discord (#feedback, 10-28): the Routing page's stage stayed on
a 16:36 Mozilla request to qwenwork/flash, "The agent canceled the
request", headed "16:36 的请求是怎么路由的", while the requests list
under it went on with auto/deepseek-v4-flash at 17:08 and 17:09.

Cause: that head is a pick. A click on a list row, the errors stat,
the context card, Usage's openRoute or the tray's ?req= pins the stage
to one request. #1389 lets a pin go only when the request leaves the
60 the page keeps, so at a calm pace a pick holds for hours. A reader
who left for another app or another page and came back finds the
stage on a request they no longer remember picking, and reads it as
stuck.

Fix (plugins/routing/client.js): the reader leaving while a pick holds
the stage (window blur, another page, the window hidden) marks the
newest request then. Back in sight and focused, if a request newer
than the mark has come, the pin is dropped and the stage follows live
(followListed). This is checked in start() when the poll was running,
and in the poll's catch-up pass when it had slept out of sight, so
requests read only on return count too. Back to nothing new, the pick
stays. A reader who never left keeps it as requests come, as before. A
new pick clears the mark. Day mode and a replay are left alone. The
tray panel has no pin, so there is no sibling there.

Test: internal/gui/tests/routing-pin-away.test.cjs. The canceled
request is picked from the list. It holds while requests come, and it
holds after leaving and coming back to nothing new. The reader then
leaves while 8 more come, still within the 60, and comes back: the
head is "How the last request was routed", there is no Back to live,
the first row is pressed and the stage shows the newest model. Three
ways of leaving (another app, another page, the window hidden), en at
1100 and zh at 420, in Chromium and WebKit.

Run on mote at this head (rebased on c5cdb043e; main to 5a85a9836 changed nothing under internal/gui, and the two pin files ran again there):
- routing-pin-away: red on the old code in every case (the head kept
  "How the request at … was routed…Back to live"); green after.
- All routing-*, panel-routing*, i18n-keys, gui-zh-tw, usage-ledger,
  click-scroll and dates-gui-lang GUI files in both engines (132 files):
  1755 of 1793 pass. The 38 reds are in 9 files. Run alone, they are
  red the same way on an unchanged copy of main c5cdb043e (33 there,
  36 here, at mote load 70 to 200; the counts in the rule-editor files
  vary between runs): routing-row-swap #3799, routing-steady #3412,
  routing-story-shrinks #3954, usage-ledger #4078, routing-rule-drag
  #3789, routing-rule-remove #3094/#3096, routing-rules-line #3593,
  and routing-failed-open's en live case, which had no issue and is
  filed as #4105. routing-pin-away and routing-pin-leaves are green.
- go test -tags nogui ./internal/gui/ (TestCoreScriptBudget and the
  asset tests): ok. TestLoopGuardLeavesOrdinaryRepliesAlone and
  TestAntigravityTurnedAwayHintIsTheRoutingPages (they read
  client.js): ok.
- GOOS=linux and GOOS=windows go vet -tags nogui ./internal/gui/...:
  ok. gofmt -l: clean. No budget raised.

Broken by hand:
- Dropping the poll's catch-up check: the another-page and
  window-hidden cases go red (the poll slept while away), and
  another-app stays green.
- Dropping the check in start(): the another-app cases go red in both
  engines.

Not tried: the real Wails window's blur and focus in WKWebView,
WebView2 and WebKitGTK. The test dispatches the window events and fakes
document.hidden. Not tried with the reporter's own gateway either.
This commit is contained in:
yetone
2026-10-29 01:19:25 +08:00
parent 53913be7aa
commit ca221e115e
2 changed files with 183 additions and 1 deletions
+30 -1
View File
@@ -2865,6 +2865,7 @@ function routingPage(part) {
// go up to the stage, which read as the page jumping to its top)
function pick(r) {
pinned = !day && r.id === newest()?.id ? null : r;
awayAt = 0;
if (rp) { rp = null; rbar.hidden = true; }
stopPlays();
cur = r;
@@ -4383,6 +4384,7 @@ function routingPage(part) {
ticking = 0;
seen = false;
endReplay(true);
leftPick();
for (const tr of trips) tr.res();
trips = [];
wake();
@@ -4391,16 +4393,41 @@ function routingPage(part) {
// Visibility events can arrive after the browser has suspended frames, so
// finish hidden work here too. The first visible frame alone owns resume.
function start() {
const waking = !!sightWait; // the poll waited: it reads what came meanwhile
inSightAgain();
countdown();
if (!shown()) { pause(); return; }
backToPick(!waking);
kick();
}
// A pick holds the stage while the reader is with it (and #1389 lets it
// go once it has left the list). Gone from the page — another app,
// another page, the window hidden — and back to requests that came
// meanwhile, the reader comes back to live, not to a pick they may well
// have forgotten: a 16:36 request the agent had canceled held the stage
// at 17:09 while the list under it filled with newer ones (未寒 on
// Discord, 10-28). Back to none, the pick stays.
let awayAt = 0, blurred = false; // the newest request when the reader left a pick; 0 while with it
function leftPick() {
if (pinned && !day && !rp && !awayAt) awayAt = newest()?.id || 1;
}
// backToPick: the reader is back; done, it was the last word on this
// time away (the poll may still be reading what came meanwhile)
function backToPick(done) {
if (!awayAt || blurred || !shown() || !loaded) return;
const was = awayAt;
if (done) awayAt = 0;
if (!pinned || day || rp || !((newest()?.id || 0) > was)) return;
awayAt = 0;
pinned = null;
followListed();
}
// in sight again: the view picked, the window shown, another tab left,
// the window covered and drawing frames once more
new MutationObserver(start).observe($("#view-routing"), { attributes: true, attributeFilter: ["hidden"] });
document.addEventListener("visibilitychange", start);
window.addEventListener("focus", start);
window.addEventListener("blur", () => { blurred = true; leftPick(); });
window.addEventListener("focus", () => { blurred = false; start(); });
// countdowns tick once a second, only while the page is in sight: a
// tick with nothing to draw wakes the web content process all the same
// (#3185), so out of sight there is none until start() sees it again
@@ -4550,6 +4577,8 @@ function routingPage(part) {
cur = newest();
if (cur) { sync(true); say(affWhy(cur, true) || ruleWhy(cur, true) || firstWhy(cur)); }
}
// back in sight to requests that came while away: live again
if (back) backToPick(true);
if (!pinned && !rp) {
cur = newest();
if (cur) sync();
@@ -0,0 +1,153 @@
// Run with Node's test runner and Playwright on the module path; see README.md.
// A request picked on the Routing page holds the stage while the reader
// is with it. Once they had gone (to another app, another page, the window
// hidden) and requests came in meanwhile, it still held it when they came
// back: the stage and "How the request at 16:36 was routed" stayed on a
// Mozilla request the agent had canceled, while the list under it filled
// with newer deepseek requests (未寒 on Discord, 10-28; the picked one was
// still among the 60 the page keeps, so #1389's rule never let it go).
// Coming back to requests that came while away, the stage follows live
// again; coming back to none, the pick stays; a reader still there keeps
// it as requests come. Both engines, en and zh, narrow and wide. No
// backend: the API is faked here.
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 = Date.now();
const at = (id) => new Date(now - (200 - id) * 20e3).toISOString();
const qw = { id: "qwenwork", provider: "qwenwork", name: "QwenWork", kind: "provider", model: "flash" };
const ds = { id: "deepseek", provider: "deepseek", name: "DeepSeek", kind: "provider", model: "deepseek-v4.1-flash" };
const route = (id, w, agent, failed) => ({
id, seq: id, time: at(id), agent, model: `${w.provider}/${w.model}`, provider: w.provider, done: true,
order: [w], tries: [{ id: w.id, model: w.model, start: at(id), done: true, status: failed ? 499 : 200, ms: 900, ...(failed ? { error: "the agent canceled the request" } : {}) }],
status: failed ? 499 : 200, ms: 900, ...(failed ? { error: "the agent canceled the request" } : {}),
});
// a few to qwenwork, the middle one canceled; then deepseek's, few enough
// that the picked one stays among the 60 the page keeps
const first = [route(1, qw, "mozilla"), route(2, qw, "mozilla"), route(3, qw, "mozilla", true), route(4, qw, "mozilla"), route(5, qw, "mozilla")];
const more = (from) => Array.from({ length: 8 }, (_, i) => route(from + i, ds, "codex"));
function serve(lang, feed) {
const state = { agents: [{ id: "codex", name: "Codex", path: "/t/config.toml", 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: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/plugins") return json({ plugins: [] });
if (url.pathname === "/api/gateway/trace") {
const after = Number(url.searchParams.get("after")) || 0;
// a long poll gives up after a second, as the gateway's does after its wait
for (let i = 0; i < 40 && !feed.out.some((b) => b[b.length - 1].seq > after); i++) await new Promise((res) => setTimeout(res, 25));
const b = feed.out.find((b) => b[b.length - 1].seq > after) || [];
return json({ mine: true, now: new Date(now).toISOString(), seq: b.length ? b[b.length - 1].seq : after, totals: { requests: 0, rerouted: 0, errors: 0 }, routes: structuredClone(b) }).catch(() => {});
}
if (url.pathname === "/api/gateway/history") return json({ cut: false, days: [], routes: [] });
if (url.pathname === "/api/groups") return json({ groups: [] });
if (url.pathname === "/api/providers") return json({ providers: [], excluded: [], presets: [], 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 });
};
}
const words = {
en: { picked: "How the request at", last: "How the last request was routed", back: "Back to live" },
zh: { picked: "的请求是怎么路由的", last: "最近一个请求是怎么路由的", back: "回到实时" },
};
// the ways a reader goes from the page, and comes back to it
const ways = {
"another app": {
leave: (page) => page.evaluate(() => window.dispatchEvent(new Event("blur"))),
back: (page) => page.evaluate(() => window.dispatchEvent(new Event("focus"))),
},
"another page": {
leave: (page) => page.locator('#nav button[data-view="agents"]').click(),
back: (page) => page.locator('#nav button[data-view="routing"]').click(),
},
"the window hidden": {
leave: (page) => page.evaluate(() => window.__hide(true)),
back: (page) => page.evaluate(() => window.__hide(false)),
},
};
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
test(engine + ": a picked request lets the stage go live when the reader comes back to newer requests", async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: process.env.PLAYWRIGHT_CHANNEL || "chromium" }));
const errors = [];
t.after(async () => {
if (errors.length) console.log(errors);
await browser.close();
});
for (const [lang, width] of [["en", 1100], ["zh", 420]]) {
for (const [way, how] of Object.entries(ways)) {
await t.test(`${lang} ${width} ${way}`, async () => {
const w = words[lang], feed = { out: [first] };
const ctx = await browser.newContext({ viewport: { width, height: 760 }, reducedMotion: "reduce" });
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();
page.setDefaultTimeout(5000);
page.on("pageerror", (e) => errors.push(e.message));
await page.route("**/*", serve(lang, feed));
await page.goto("http://magpie.test/?view=routing");
const head = page.locator(".rt-log-head");
const headSays = (s, why) => page.waitForFunction((s) => document.querySelector(".rt-log-head")?.textContent.includes(s), s)
.catch(async () => assert.fail(why + ": " + await head.textContent()));
const models = () => page.locator(".rt-accts code.mdl").allTextContents();
await page.locator(".rt-req").nth(4).waitFor();
// the canceled request, picked from the list
await page.locator(".rt-req").nth(2).click();
await headSays(w.picked, "the pick didn't hold the stage");
// a reader still there keeps it as requests come
feed.out.push(more(6));
await page.locator(".rt-req").nth(12).waitFor();
await page.waitForTimeout(300);
assert((await head.textContent()).includes(w.picked), "a reader still there lost the pick: " + await head.textContent());
// gone and back with nothing new: still picked
await how.leave(page);
await page.waitForTimeout(300);
await how.back(page);
await page.waitForTimeout(500);
assert((await head.textContent()).includes(w.picked), "back to nothing new, the pick was lost: " + await head.textContent());
assert.equal(await head.locator("button", { hasText: w.back }).count(), 1);
// gone while more came, still among the 60 kept: back, it is live.
// Out of sight, the poll has given up its wait and sleeps by the
// time they come, so they are read only once the reader is back.
await how.leave(page);
await page.waitForTimeout(way === "another app" ? 200 : 1600);
feed.out.push(more(14));
if (way === "another app") await page.locator(".rt-req").nth(20).waitFor();
else await page.waitForTimeout(400);
await how.back(page);
await headSays(w.last, "back to newer requests, the stage stayed on the picked one");
assert.equal(await head.locator("button", { hasText: w.back }).count(), 0);
await page.locator(".rt-req").nth(20).waitFor();
await page.waitForFunction(() => document.querySelector(".rt-req")?.getAttribute("aria-pressed") === "true");
await page.waitForFunction(() => [...document.querySelectorAll(".rt-accts code.mdl")].every((c) => c.textContent === "deepseek-v4.1-flash"));
assert(!(await models()).includes("flash"));
// nothing runs off a narrow window
assert.deepEqual(await page.evaluate(() => [document.documentElement.scrollWidth <= innerWidth, scrollX]), [true, 0]);
await ctx.close();
});
}
}
assert.deepEqual(errors, []);
});
}