Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

routing: hundreds of keys in a group are read once, not each against all (#3164)

yetonea3232e3e1199
diff --git a/internal/gui/assets/plugins/routing/client.js b/internal/gui/assets/plugins/routing/client.js
index da94a01d3..f43a32399 100644
--- a/internal/gui/assets/plugins/routing/client.js
+++ b/internal/gui/assets/plugins/routing/client.js
@@ -310,7 +310,20 @@ function routingPage(part) {
let skew = 0; // the gateway's clock less this page's
// now is the gateway's time — or, in a replay, the time it is replaying
const now = () => rp ? rp.real : Date.now() + skew;
- const at = (s) => new Date(s).getTime();
+ // the times the trace says, each read once: every route names each of
+ // its keys' rest and try times, and a provider of hundreds of keys
+ // (#3164) had the stage, the story and the lists parse ~1M of them a
+ // second, more than a third of the page's time
+ const atOf = new Map();
+ const at = (s) => {
+ if (typeof s !== "string") return new Date(s).getTime();
+ let v = atOf.get(s);
+ if (v === undefined) {
+ if (atOf.size > 50000) atOf.clear();
+ atOf.set(s, v = new Date(s).getTime());
+ }
+ return v;
+ };
// when each rest was lifted by hand, by the key it rests by (#1869), on
// the gateway's clock: here, or anywhere else the trace tells of
// (d.lifted: the provider's editor's Try now, a key's new value)
@@ -1415,13 +1428,19 @@ function routingPage(part) {
function layout() {
const r = stage.getBoundingClientRect();
if (!r.width) return;
- const b = (e) => { const x = e.getBoundingClientRect(); return { l: x.left - r.left, r: x.right - r.left, t: x.top - r.top, b: x.bottom - r.top, cx: (x.left + x.right) / 2 - r.left, cy: (x.top + x.bottom) / 2 - r.top }; };
- wires.setAttribute("viewBox", `0 0 ${r.width} ${r.height}`);
- sky.setAttribute("viewBox", `0 0 ${r.width} ${r.height}`);
+ // every box is read before any wire is drawn, each once: a wire drawn
+ // between two reads had the next read lay the stage out again, and
+ // hundreds of keys open (#3164) laid it out once a row, seconds a time
+ const boxes = new Map(), sets = [];
+ const b = (e) => {
+ let v = boxes.get(e);
+ if (!v) { const x = e.getBoundingClientRect(); boxes.set(e, v = { l: x.left - r.left, r: x.right - r.left, t: x.top - r.top, b: x.bottom - r.top, cx: (x.left + x.right) / 2 - r.left, cy: (x.top + x.bottom) / 2 - r.top }); }
+ return v;
+ };
const h = b(hub), low = b(srcs).b;
for (const a of agents.values()) {
const s = b(a.node), mx = (s.r + h.l) / 2;
- a.wire.setAttribute("d", h.l > s.r ? `M${s.r} ${s.cy} C${mx} ${s.cy} ${mx} ${h.cy} ${h.l} ${h.cy}` : `M${s.cx} ${s.b} L${h.cx} ${h.t}`);
+ sets.push([a.wire, h.l > s.r ? `M${s.r} ${s.cy} C${mx} ${s.cy} ${mx} ${h.cy} ${h.l} ${h.cy}` : `M${s.cx} ${s.b} L${h.cx} ${h.t}`]);
}
const fromHub = (a) => {
if (a.l > h.r) {
@@ -1440,19 +1459,25 @@ function routingPage(part) {
};
for (const s of subs.values()) {
const up = s.up && subs.get(s.up);
- s.wire.setAttribute("d", up ? fromSub(up, b(s.li)) : fromHub(b(s.li)));
+ sets.push([s.wire, up ? fromSub(up, b(s.li)) : fromHub(b(s.li))]);
}
for (const f of folds.values()) {
- if (!f.li.isConnected) { f.wire.removeAttribute("d"); continue; }
+ if (!f.li.isConnected) { sets.push([f.wire, null]); continue; }
const up = f.up && subs.get(f.up);
- f.wire.setAttribute("d", up ? fromSub(up, b(f.li)) : fromHub(b(f.li)));
+ sets.push([f.wire, up ? fromSub(up, b(f.li)) : fromHub(b(f.li))]);
}
for (const row of rows.values()) {
// a key folded away has no wire: its fold's is the way to it
- if (!row.li.isConnected) { row.wire.removeAttribute("d"); continue; }
+ if (!row.li.isConnected) { sets.push([row.wire, null]); continue; }
const f = row.fold && folds.get(row.fold);
const up = f?.open ? f : row.up && subs.get(row.up), a = b(row.li);
- row.wire.setAttribute("d", up ? fromSub(up, a) : fromHub(a));
+ sets.push([row.wire, up ? fromSub(up, a) : fromHub(a)]);
+ }
+ wires.setAttribute("viewBox", `0 0 ${r.width} ${r.height}`);
+ sky.setAttribute("viewBox", `0 0 ${r.width} ${r.height}`);
+ for (const [p, d] of sets) {
+ if (d === null) { if (p.hasAttribute("d")) p.removeAttribute("d"); }
+ else if (p.getAttribute("d") !== d) p.setAttribute("d", d);
}
holdAll();
}
@@ -1466,9 +1491,25 @@ function routingPage(part) {
if (p?.account && w.kind === "account") {
const user = (w.who || "").toLowerCase();
i = loginsInOrder(p.account, p).findIndex((l) => (l.user || "").toLowerCase() === user);
- } else if (p && w.kind === "key") i = (p.keyList || []).findIndex((k) => w.id === p.id + "#" + k.id);
+ } else if (p && w.kind === "key") i = keyRank(p, w.id);
return i >= 0 ? i : (p ? 1000 : 0) + (w.rank || 0);
}
+ // keyRank is where a key is in its provider's list, by an index made
+ // once for the list: a sort of a route's hundreds of keys asked it for
+ // each comparison, and each looked through every key (#3164). An index
+ // the list has moved under (a drag reorders it in place) is made again.
+ const keyRanks = new WeakMap(); // keyList → { n, at: key id → place }
+ function keyRank(p, id) {
+ const ks = p.keyList || [], pre = p.id + "#";
+ if (!id.startsWith(pre)) return -1;
+ const kid = id.slice(pre.length);
+ let x = keyRanks.get(ks), i = x?.n === ks.length ? x.at.get(kid) : undefined;
+ if (!x || x.n !== ks.length || (i !== undefined && ks[i]?.id !== kid)) {
+ keyRanks.set(ks, x = { n: ks.length, at: new Map(ks.map((k, j) => [k.id, j])) });
+ i = x.at.get(kid);
+ }
+ return i ?? -1;
+ }
// seated: a route's accounts and keys each in a place of its own, not in
// the order routing weighed them this time — the group's members in the
@@ -1490,13 +1531,14 @@ function routingPage(part) {
if (m < 0) m = members.findIndex((x) => x.startsWith(w.provider + "/"));
return [w.fallback ? 1 : 0, m < 0 ? members.length : m, w.name || w.provider, w.aside ? 1 : 0, rankOf(w), w.who || "", w.id];
};
- const cmp = (a, b) => {
- const x = key(a), y = key(b);
+ const cmp = (x, y) => {
for (let i = 0; i < x.length; i++) if (x[i] !== y[i]) return typeof x[i] === "number" ? x[i] - y[i] : String(x[i]).localeCompare(String(y[i]));
return 0;
};
const on = new Set(r.order.map(seat));
- return [...r.order, ...(r.left || []).filter((w) => heldOut(w) && !on.has(seat(w)))].sort(cmp);
+ // each one's key made once, not at each comparison (#3164)
+ return [...r.order, ...(r.left || []).filter((w) => heldOut(w) && !on.has(seat(w)))]
+ .map((w) => [key(w), w]).sort((a, b) => cmp(a[0], b[0])).map(([, w]) => w);
}
// a seat is one of a route's keys or accounts for one model: two of a
@@ -1513,7 +1555,13 @@ function routingPage(part) {
|| r.order.find((x) => x.id === tr.id);
// tryseat is the seat of the row a try went to: its row is keyed by it
const tryseat = (r, tr) => seat(tried(r, tr) || tr);
- const setOf = (r) => r.order.map(seat).sort().join("\n");
+ // made once for each route's list, which a trace update replaces whole
+ const setsOf = new WeakMap();
+ const setOf = (r) => {
+ let s = setsOf.get(r.order);
+ if (s === undefined) setsOf.set(r.order, s = r.order.map(seat).sort().join("\n"));
+ return s;
+ };
// whole fills in the lists a route's JSON can leave null: a route the
// gateway weighed no seats for has "order": null, and the page reads
// r.order, r.tries and r.left as lists everywhere — one null threw in
@@ -1753,16 +1801,17 @@ function routingPage(part) {
const latest = new Map();
for (const r of rs) latest.set(setOf(r), r);
sets = [...sets.filter((k) => latest.has(k)), ...[...latest.keys()].filter((k) => !sets.includes(k))];
- const ids = [], wOf = new Map(), rOf = new Map();
+ // a Set beside the list: hundreds of keys looked each other up (#3164)
+ const ids = [], has = new Set(), wOf = new Map(), rOf = new Map();
for (const k of sets) {
const r = latest.get(k);
- for (const w of seated(r)) if (!ids.includes(seat(w))) ids.push(seat(w));
+ for (const w of seated(r)) { const id = seat(w); if (!has.has(id)) { has.add(id); ids.push(id); } }
}
for (const r of rs) for (const w of [...r.order, ...(r.left || [])]) { wOf.set(seat(w), w); rOf.set(seat(w), r.id); }
// where each was, of those on the stage: a key folded away was nowhere
const before = new Map([...[...rows].map(([id, row]) => [id, row.li]), ...[...folds].map(([k, f]) => ["\u0001" + k, f.li])]
.filter(([, li]) => li.isConnected).map(([id, li]) => [id, li.getBoundingClientRect().top]));
- for (const [id, row] of rows) if (!ids.includes(id)) { row.li.remove(); row.wire.remove(); rows.delete(id); }
+ for (const [id, row] of rows) if (!has.has(id)) { row.li.remove(); row.wire.remove(); rows.delete(id); }
if (list.querySelector(".idle")) list.replaceChildren();
for (const id of ids) {
let row = rows.get(id);
@@ -1805,7 +1854,8 @@ function routingPage(part) {
for (const id of ids) {
const row = rows.get(id), via = row.w.via || [], f = foldOf.get(id);
row.fold = f ? f.key : null;
- row.li.style.setProperty("--depth", via.length + (f ? 1 : 0));
+ const depth = String(via.length + (f ? 1 : 0));
+ if (row.li.style.getPropertyValue("--depth") !== depth) row.li.style.setProperty("--depth", depth);
row.up = via.length ? via.join(">") : null;
row.shut = null;
for (let d = 0; d < via.length; d++) {
@@ -1842,13 +1892,15 @@ function routingPage(part) {
// FLIP: from where each was to its new place
let moved = false;
const placed = [...[...rows].map(([id, row]) => [id, row.li]), ...[...folds].map(([k, f]) => ["\u0001" + k, f.li])].filter(([, li]) => li.isConnected);
- for (const [id, li] of placed) {
- const dy = before.has(id) ? before.get(id) - li.getBoundingClientRect().top : 0;
- if (!dy || still()) continue;
+ // each row's place read before any is moved: a row moved between two
+ // reads laid the stage out again, once a row of hundreds (#3164)
+ const dys = still() ? [] : placed.map(([id, li]) => before.has(id) ? before.get(id) - li.getBoundingClientRect().top : 0);
+ placed.forEach(([, li], i) => {
+ if (!dys[i]) return;
moved = true;
li.style.transition = "none";
- li.style.transform = `translateY(${dy}px)`;
- }
+ li.style.transform = `translateY(${dys[i]}px)`;
+ });
if (moved) {
void list.offsetWidth;
for (const [, li] of placed) { li.style.transition = ""; li.style.transform = ""; }
@@ -1940,17 +1992,18 @@ function routingPage(part) {
for (const r of rs) for (const tr of r.tries) if (!tr.done) { trying.add(tryseat(r, tr)); busy.add(r.agent); }
const onWire = new Set();
for (const f of flying.values()) { onWire.add(f.id); busy.add(f.agent); }
- for (const [id, row] of rows) {
- // each row as the latest staged request that weighed it found it
- // rests by key or account, and by key or account and model for a
- // rest of one model only: the account's other models' rows don't
- // rest with it (#1960)
- const r = src().get(row.rid) || pinned || cur, answered = new Set(), rests = new Map(), gave = new Map(), went = new Map();
+ // what each request found, read once a pass: hundreds of a route's
+ // keys each read their route's whole list again (#3164)
+ const found = new Map();
+ const findings = (r) => {
+ let f = found.get(r);
+ if (f) return f;
+ const answered = new Set(), rests = new Map(), gave = new Map(), went = new Map();
// a rest lifted since it was found (#1869), here or in the
// provider's editor, is over: the row doesn't say it until the next
// request is refused again (zoecats on Discord)
- const restAt = (id, m, rest, found) => {
- if (lifted(rest.key, found)) return;
+ const restAt = (id, m, rest, since) => {
+ if (lifted(rest.key, since)) return;
const rm = restModel(rest, m); rests.set(rm ? id + "\u0000" + rm : id, rest);
};
const modelOf = (tr) => tried(r, tr)?.model || tr.model || "";
@@ -1967,6 +2020,15 @@ function routingPage(part) {
}
// it answered: whatever rest it began in is over
for (const tr of r.tries) if (tr.done && tryOk(tr)) { rests.delete(tr.id); rests.delete(tr.id + "\u0000" + modelOf(tr)); }
+ found.set(r, f = { answered, rests, gave, went });
+ return f;
+ };
+ for (const [id, row] of rows) {
+ // each row as the latest staged request that weighed it found it
+ // rests by key or account, and by key or account and model for a
+ // rest of one model only: the account's other models' rows don't
+ // rest with it (#1960)
+ const r = src().get(row.rid) || pinned || cur, { answered, rests, gave, went } = findings(r);
const w = row.w, rest = rests.get(w.id) || rests.get(w.id + "\u0000" + (w.model || "")), resting = rest && at(rest.until) > n;
let s;
if (w.unlisted) s = unlistedWord(w);
@@ -3196,6 +3258,10 @@ function routingPage(part) {
const n = now();
let prov = "";
const out = [];
+ // each provider's keys and accounts, gathered once: each of hundreds
+ // of keys looked through the whole list for its own (#3164)
+ const kin = new Map(), kinOf = (w) => w.provider + "\u0000" + w.kind;
+ for (const a of list) { const k = kinOf(a.w); kin.has(k) ? kin.get(k).push(a) : kin.set(k, [a]); }
for (const a of list) {
const w = a.w;
if (w.provider !== prov) {
@@ -3216,7 +3282,7 @@ function routingPage(part) {
// up (#1319), opened to each in place; its accounts too, folded
// when LONG or more
if (folding(w)) {
- const ks = list.filter((x) => x.w.provider === w.provider && x.w.kind === w.kind);
+ const ks = kin.get(kinOf(w));
if (ks.length >= FOLD_AT) {
const pk = w.provider + (w.kind === "account" ? "/@" : "/*"), open = isOpen(pk, w.kind === "account" && ks.length < LONG);
if (ks[0] === a) out.push(keysRow(ks, pk, open, n, w.kind));
diff --git a/internal/gui/tests/routing-many-keys.test.cjs b/internal/gui/tests/routing-many-keys.test.cjs
new file mode 100644
index 000000000..baa87db6d
--- /dev/null
+++ b/internal/gui/tests/routing-many-keys.test.cjs
@@ -0,0 +1,134 @@
+// Run with Node's test runner and Playwright on the module path; see README.md.
+// The Routing page stays light with hundreds of keys in a group (#3164:
+// several hundred gpt-5.6-sol keys in one group, "进到这个分组的路由页就基本
+// 没法流畅操作了"). Each request names every key it weighed, and the page
+// read every one of them again and again: a sort of a route's keys looked
+// each key up in its provider's whole list for every comparison, every
+// row of the stage went through its route's whole list each second, the
+// trace's times were parsed again for each, and drawing the wires read
+// each row's box after the wire before it was drawn, so an open fold laid
+// the stage out once a row. Here 500 keys of one provider and 300 of
+// another serve a group while requests come, folded and opened, and what
+// the page does is counted: no key lookup through a whole list, times
+// parsed a bounded number of times, and no box read after a wire drawn
+// in the same pass. English, Chinese and German; 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 iso = (ms) => new Date(ms).toISOString();
+const M = "gpt-5.6-sol";
+const pools = [["solpool", "SolPool", 500], ["solpool2", "SolPool2", 300]];
+const key = (p, i) => `${p}#k${String(i).padStart(3, "0")}`;
+const order = pools.flatMap(([p, name, n]) => Array.from({ length: n }, (_, i) => ({ id: key(p, i), provider: p, name, who: `sk-proj-…${String(i).padStart(4, "0")}`, kind: "key", model: M })));
+const group = { id: "sol", name: "Sol", routing: "", members: pools.map(([p]) => `${p}/${M}`) };
+const providers = pools.map(([p, name, n]) => ({ id: p, name, keyList: Array.from({ length: n }, (_, i) => ({ id: `k${String(i).padStart(3, "0")}` })) }));
+
+// a request to the group, answered by one key after another refused it,
+// or still under way at it
+function req(id, live) {
+ const t0 = Date.now() - 500, a = order[(id * 7) % order.length], b = order[(id * 7 + 1) % order.length];
+ const tries = [
+ { id: b.id, model: M, start: iso(t0), done: true, status: 429, fail: "rate", ms: 100 },
+ { id: a.id, model: M, start: iso(t0 + 100), done: !live, ms: 300, ...(live ? {} : { status: 200 }) },
+ ];
+ return { id, seq: id, time: iso(t0), agent: "codex", model: "sol", provider: a.provider, group, order: order.map((w) => ({ ...w })), tries,
+ done: !live, status: live ? 0 : 200, ms: live ? 0 : 400, tokens: live ? 0 : 1200 };
+}
+
+function serve(lang, feed) {
+ const first = Array.from({ length: 30 }, (_, i) => req(1 + i, false));
+ 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:"${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({ agents: [{ id: "codex", name: "Codex", path: "/test/config.toml", fields: [] }], profiles: [], settings: { lang, theme: "light" } });
+ if (url.pathname === "/api/plugins") return json({ plugins: [] });
+ 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);
+ while (!feed.q.length && !feed.closed) await new Promise((r) => setTimeout(r, 20));
+ return trace(feed.q.splice(0)).catch(() => {});
+ }
+ if (url.pathname === "/api/gateway/history") return json({ cut: false, days: [], routes: [] });
+ if (url.pathname === "/api/groups") return json({ models: [], groups: [], 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), contentType });
+ };
+}
+
+// what the page does, counted: lookups through a list, times parsed, and
+// the times a box read laid the stage out again after a wire was drawn
+const init = () => {
+ const n = window.__n = { find: 0, dates: 0, thrash: 0, boxes: 0 };
+ const find = Array.prototype.findIndex;
+ Array.prototype.findIndex = function (...a) { if (this.length > 100) n.find++; return find.apply(this, a); };
+ const D = Date;
+ window.Date = new Proxy(D, { construct(T, a) { if (typeof a[0] === "string") n.dates++; return Reflect.construct(T, a); } });
+ // a wire drawn leaves the stage to lay out again: the next box read
+ // lays it out then and there
+ let drew = false;
+ const set = Element.prototype.setAttribute;
+ Element.prototype.setAttribute = function (k, v) { if (k === "d" && this instanceof SVGPathElement) drew = true; return set.call(this, k, v); };
+ const box = Element.prototype.getBoundingClientRect;
+ Element.prototype.getBoundingClientRect = function () { n.boxes++; if (drew) n.thrash++; drew = false; return box.call(this); };
+};
+
+for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
+ for (const lang of ["en", "zh", "de"]) {
+ test(`${engine} ${lang}: hundreds of keys in a group are read once, not each against all`, async (t) => {
+ const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
+ const context = await browser.newContext({ viewport: { width: 1200, height: 800 } });
+ await context.addInitScript(init);
+ const page = await context.newPage();
+ page.setDefaultTimeout(15000);
+ const errors = [];
+ page.on("pageerror", (e) => errors.push(e.message));
+ const feed = { q: [], closed: false };
+ await page.route("**/*", serve(lang, feed));
+ t.after(async () => { feed.closed = true; await browser.close(); });
+ await page.goto("http://magpie.test/?view=routing");
+ await page.locator("#view-routing .rt-fold").first().waitFor();
+ await page.waitForTimeout(1500);
+
+ let id = 100;
+ const pump = setInterval(() => { feed.q.push(req(id, true), req(id - 1, false)); id++; }, 300);
+ t.after(() => clearInterval(pump));
+ const look = async () => {
+ const before = await page.evaluate(() => ({ ...window.__n }));
+ await page.waitForTimeout(3000);
+ const after = await page.evaluate(() => ({ ...window.__n }));
+ return Object.fromEntries(Object.keys(after).map((k) => [k, after[k] - before[k]]));
+ };
+ const folded = await look();
+ // both folds opened, each key a row of its own on the stage
+ while (await page.evaluate(() => { const f = document.querySelector("#view-routing .rt-fold:not(.open)"); f?.click(); return !!f; }));
+ await page.waitForFunction(() => document.querySelectorAll("#view-routing .rt-accts > li:not(.rt-fold)").length >= 800);
+ const opened = await look();
+ clearInterval(pump);
+ t.diagnostic(`folded ${JSON.stringify(folded)}, open ${JSON.stringify(opened)}`);
+ for (const [name, n] of [["folded", folded], ["open", opened]]) {
+ // a key's place is looked up in an index of its provider's list:
+ // before, a sort of 800 keys looked through the 500 for each of
+ // ~8000 comparisons, ~50k lookups a second
+ assert(n.find < 50, `${name}: ${n.find} lookups through a list of hundreds in 3s`);
+ // each time the trace says is parsed once: ~1M a second before
+ assert(n.dates < 20000, `${name}: ${n.dates} times parsed in 3s`);
+ // every box is read before a wire is drawn: before, each of the
+ // 800 rows' boxes was read after the wire above it was drawn, so
+ // the stage was laid out again for each (~7000 a second). A few
+ // dozen a second are the stage drawn anew as requests come.
+ assert(n.thrash < 400, `${name}: the stage laid out again ${n.thrash} times for a wire drawn, in 3s`);
+ }
+ assert(opened.boxes > 800, `the open stage was laid out (${opened.boxes} boxes read)`);
+ assert.deepEqual(errors, []);
+ });
+ }
+}