routing: a row made again in the middle of the list is swapped alone, not every row after it moved (#3164)

placeRows walked the wanted rows against the list's children and put
each one out of place in front of the current child. A row going (its
request moved on, so rowOf made it again) stayed where it was as that
current child, so every wanted row after it was put in again in front
of it, one insertBefore each. On a busy gateway the request in flight is
rarely the newest, so each trace update (several a second) moved ~40
rows and laid the list out again. With two groups of hundreds of keys
open, that is the 50ms+ frames WebKit showed at each update.

Rows no longer wanted are taken out first, as replaceKeeping does; the
rows kept are not touched, so WebKit's scroll position under a picked
row (what the walk was for) holds as before.

Run at this head (rows3164 worktree on origin/main 8299409e4):
- routing-row-swap.test.cjs (new): chromium and webkit pass; on the old
  placeRows (origin/main) both fail: {added: 10, removed: 10} where
  {added: 1, removed: 1} is wanted.
- every routing*.test.cjs and panel-routing*.test.cjs (112 files,
  --test-concurrency=6): 1418 tests, 1365 pass, 53 fail. The red files,
  run alone at origin/main 8299409e4 and at this head, fail the same:
  routing-live-patch 4/6 both (filed #3637), routing-intent-entry 4/4
  vs 5/3 (#3408), routing-manual 2/2 both (#3409), routing-pause 0/14
  both (#3410), routing-rule-effort 4/14 both (#3411), routing-rule-remove
  11/9 both (#3094, #3096). routing-keys-fold's one webkit de red in the
  full run passes 32/32 alone at both trees.
- Sandbox (sbh3164: providers of 500 and 300 keys on a fake upstream, a
  request every 0.3s, both groups open, Playwright WebKit, 10s, the
  served client.js swapped old/new three times):
  frames over 50ms 25/10/14 → 3/2/3; frames 344/464/360 → 398/379/403;
  .rt-reqs childList records in 5s 1566/32/740 → 68/32/32.

Not tried: the macOS app's own WKWebView window, a real upstream.
This commit is contained in:
yetone
2026-10-26 01:02:30 +08:00
parent ef3b9f85a4
commit 3173440ca3
2 changed files with 98 additions and 1 deletions
@@ -3066,7 +3066,10 @@ function routingPage(part) {
// place: the list scrolls on its own, and WebKit, a row taken out for a
// moment, would send it back to its top from under the row just picked.
// A list made mostly anew (another day) is put in at once, not row by
// row among the old ones.
// row among the old ones. A row going (one made again, its request having
// moved on) is taken out first: left where it was, every row after it was
// put in again in front of it, ~40 rows moved and laid out on each of the
// updates a busy gateway sends a second (#3164).
function placeRows(els) {
const kids = reqs.children;
if (kids.length === els.length && els.every((e, i) => kids[i] === e)) return;
@@ -3076,6 +3079,8 @@ function routingPage(part) {
for (const e of els) if (e.parentNode === reqs) kept++;
if (kept * 2 < els.length) reqs.replaceChildren(...els);
else {
const want = new Set(els);
for (const e of [...kids]) if (!want.has(e)) e.remove();
let at = reqs.firstElementChild;
for (const e of els) {
if (e === at) { at = at.nextElementSibling; continue; }
@@ -0,0 +1,92 @@
// Run with Node's test runner and Playwright on the module path; see README.md.
// Routing, the request list: a request in the middle of the list that
// finishes has its row made again, and only that row goes in and out. The
// list put every row after it in again in front of the old one, ~40 rows
// moved and laid out on each update a busy gateway sends, and with two
// groups of hundreds of keys open WebKit dropped to a frame a second for a
// moment at each (#3164). In Chromium and WebKit.
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 iso = (ms) => new Date(ms).toISOString();
const seat = { id: "codex@me", provider: "codex", name: "ChatGPT", who: "me@example.com", kind: "account", model: "gpt-6.1-sol", known: true, plan: "Pro" };
// request id, under way or done; the middle one (110) is still under way
function req(id, done, seq = id) {
const t0 = now - (200 - id) * 1000;
return {
id, seq, time: iso(t0), agent: "codex", model: "gpt-6.1-sol", provider: "codex",
order: [seat], tries: [{ id: seat.id, model: seat.model, start: iso(t0), done, ...(done ? { status: 200, ms: 4000 } : {}) }],
done, ...(done ? { status: 200, ms: 4000, tokens: 2000, cost: 0.05, priced: true, usage: [{ in: 3000, cache_read: 8000, out: 400 }] } : {}),
};
}
const first = Array.from({ length: 30 }, (_, i) => req(101 + i, 101 + i !== 110));
function serve(feed) {
const state = { agents: [{ id: "codex", name: "Codex", path: "/test/codex", fields: [] }], profiles: [], settings: { lang: "en", theme: "light" } };
return 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:"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 = (routes) => json({ mine: true, now: new Date().toISOString(), seq: routes.length ? routes[routes.length - 1].seq : Number(url.searchParams.get("after")), totals: { requests: 0, rerouted: 0, errors: 0 }, routes });
if (!url.searchParams.get("wait")) return trace(first);
const r = await new Promise((res) => { feed.next = res; });
return trace([r]);
}
if (url.pathname === "/api/gateway/history") return json({ cut: false, days: [], routes: [] });
if (url.pathname === "/api/groups") return json({ groups: [], models: [], pools: [] });
if (url.pathname === "/api/providers") return json({ providers: [], 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)];
await route.fulfill({ body: await fs.readFile(file).catch(() => ""), contentType });
};
}
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
test(`${engine}: a request done in the middle of the list swaps its own row only`, async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(() => browser.close());
const page = await browser.newPage({ viewport: { width: 1100, height: 900 } });
page.setDefaultTimeout(5000);
const errors = [];
page.on("pageerror", (e) => errors.push(e.message));
const feed = { next: null };
await page.route("**/*", serve(feed));
await page.goto("http://magpie.test/?view=routing");
await page.locator(".rt-req").nth(first.length - 1).waitFor();
await page.waitForTimeout(800);
await page.evaluate(() => {
const list = document.querySelector(".rt-reqs");
window.__was = [...list.querySelectorAll(".rt-req:not(.probe)")];
window.__moved = { added: 0, removed: 0 };
window.__mo = new MutationObserver((ms) => {
for (const m of ms) for (const [k, ns] of [["added", m.addedNodes], ["removed", m.removedNodes]])
for (const n of ns) if (n.classList?.contains("rt-req") && !n.classList.contains("probe")) window.__moved[k]++;
});
window.__mo.observe(list, { childList: true });
});
for (let i = 0; i < 60 && !feed.next; i++) await page.waitForTimeout(50);
assert(feed.next, "the page asks for the next trace");
feed.next(req(110, true, 200));
await page.waitForFunction(() => document.querySelectorAll(".rt-req.ok, .rt-req:not(.wait)").length && ![...document.querySelectorAll(".rt-req:not(.probe)")].every((r) => window.__was.includes(r)));
await page.waitForTimeout(300);
const got = await page.evaluate(() => {
window.__mo.disconnect();
const rows = [...document.querySelectorAll(".rt-reqs .rt-req:not(.probe)")];
return { rows: rows.length, kept: window.__was.filter((r) => rows.includes(r)).length, moved: window.__moved };
});
assert.equal(got.rows, 30);
assert.equal(got.kept, 29, "every other row is the one it was");
assert.deepEqual(got.moved, { added: 1, removed: 1 }, "only the finished request's row goes out and in");
assert.deepEqual(errors, []);
});
}