diff --git a/docs/subsystems/gui-shell.md b/docs/subsystems/gui-shell.md index c9e3df4f6..2aadec2ae 100644 --- a/docs/subsystems/gui-shell.md +++ b/docs/subsystems/gui-shell.md @@ -465,6 +465,14 @@ brings `.rt-gsec` to the top of the view and focuses it; the page's layout is unchanged. `fitGo` gives it only the head's spare room: named, its arrow alone, or left out, so nothing else in the head moves or wraps for it (`routing-groups-jump.test.cjs`). +Live, the Routing page's card (its head, story and context window) tells of +one request, `cur`, chosen by `told()` in `plugins/routing/client.js`: it +stays on the request it tells of until that one is done, then goes to the +newest, except that a newer request routing hasn't picked for yet does not +take it from one that was routed. Requests that come meanwhile still fly on +the stage, without a caption of their own. With several +conversations sending at once it used to follow the newest and jump between +them (qiang2040 on Discord, `routing-concurrent-hold.test.cjs`). The Plugins page updates the npm search-results section when a search answers, leaving suggested plugin cards in place so a click begun before the answer still diff --git a/internal/gui/assets/plugins/routing/client.js b/internal/gui/assets/plugins/routing/client.js index 019eb127b..23d5b8e24 100644 --- a/internal/gui/assets/plugins/routing/client.js +++ b/internal/gui/assets/plugins/routing/client.js @@ -2239,7 +2239,9 @@ function routingPage(part) { rebuild(rs, false); } function rebuild(rs, force) { - cur = src().get((pinned || rs[rs.length - 1]).id) || pinned || rs[rs.length - 1]; + // live, the request the card tells of stays (told), not the newest staged + const stay = pinned || (!rp && !day && cur && src().get(cur.id)) || rs[rs.length - 1]; + cur = src().get(stay.id) || stay; if (force) { for (const row of rows.values()) row.wire.remove(); rows = new Map(); @@ -4127,6 +4129,18 @@ function routingPage(part) { render(); renderLog(); renderHist(); } const newest = () => (day ? past : [...routes.values()]).filter(matchesPurpose).reduce((a, b) => (!a || b.id > a.id ? b : a), null); + // told: the request the live card tells of. Following the newest, with + // several agents or conversations sending at once, the card jumped from + // one to the next on every update, and to "not routed yet" for each new + // one (qiang2040 on Discord: "狂闪"). It stays on the request it tells + // of until that one is done, and a newer one routing hasn't picked for + // yet doesn't take it from one that was routed. + function told() { + const n = newest(), c = cur && !day && routes.get(cur.id); + if (!c || !n || c.id === n.id || !matchesPurpose(c)) return n; + if (!c.done) return c; + return n.done || n.tries.length || !c.tries.length ? n : c; + } // ---------- playing a request ---------- @@ -4177,11 +4191,14 @@ function routingPage(part) { let r = routes.get(id); const g = gen; playing.set(id, g); - cur = r; + // live, it flies, and the card stays on the request it tells of + cur = rp ? r : told() || r; if (!synced) sync(); - say(affWhy(r, true) || ruleWhy(r, true) || firstWhy(r)); - const aside = asides(r).find((s) => s); - if (aside) say(aside, true); + if (cur.id === r.id) { + say(affWhy(r, true) || ruleWhy(r, true) || firstWhy(r)); + const aside = asides(r).find((s) => s); + if (aside) say(aside, true); + } renderAll(); const A = agents.get(r.agent); // an agent past the stage's four (staged) isn't drawn: its request is @@ -4493,7 +4510,7 @@ function routingPage(part) { function resume() { stopPlays(); if (!loaded) return; - if (!pinned && !rp) cur = newest(); + if (!pinned && !rp) cur = told(); if (cur) sync(true); else empty(); for (const r of underWay()) play(r.id); @@ -4728,7 +4745,7 @@ function routingPage(part) { // back in sight to requests that came while away: live again if (back) backToPick(true); if (!pinned && !rp) { - cur = newest(); + cur = told(); if (cur) sync(); else empty(); } diff --git a/internal/gui/tests/routing-concurrent-hold.test.cjs b/internal/gui/tests/routing-concurrent-hold.test.cjs new file mode 100644 index 000000000..2008ba18a --- /dev/null +++ b/internal/gui/tests/routing-concurrent-hold.test.cjs @@ -0,0 +1,142 @@ +// Run with Node's test runner and Playwright on the module path; see README.md. +// qiang2040 on Discord ("狂闪", a screen recording): with several Codex +// conversations sending at once, one on group/glm-5.3-flash-free and one on +// group/deepseek-v4.1-flash, the Routing page's top card jumped between +// them on every trace update, and to "magpie hasn't routed this request +// yet" for each new request before it was routed. The card followed the +// newest request. It now stays on the request it tells of until that one +// is done, and a newer request routing hasn't picked for yet doesn't take +// it from one that was routed. Two conversations' requests interleaved +// here; every frame's head is watched. Every language, both engines. +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 t0 = Date.now(); +const GROUPS = { + glm: { id: "glm-5.3-flash-free", name: "GLM Flash", routing: "", affinity: "", members: ["zhipu/glm-5.3-flash"] }, + ds: { id: "deepseek-v4.1-flash", name: "DeepSeek Flash", routing: "", affinity: "", members: ["deepseek/deepseek-v4.1-flash"] }, +}; +const SEAT = { + glm: { id: "zhipu-key", provider: "zhipu", name: "Zhipu", kind: "key", model: "glm-5.3-flash", known: true }, + ds: { id: "deepseek-key", provider: "deepseek", name: "DeepSeek", kind: "key", model: "deepseek-v4.1-flash", known: true }, +}; +// a request of conversation g as the trace has it at a step: "held" (not +// routed yet), "going" (routed, under way), "done" +let seq = 10; +function req(id, g, step) { + const at = t0 - (200 - id) * 1000, done = step === "done", routed = step !== "held"; + return { + id, seq: ++seq, time: iso(at), agent: "codex", session: "conv-" + g, model: "group/" + GROUPS[g].id, provider: "magpie", group: GROUPS[g], + order: routed ? [SEAT[g]] : [], + tries: routed ? [{ id: SEAT[g].id, model: SEAT[g].model, start: iso(at), done, ...(done ? { status: 200, ms: 3000 } : {}) }] : [], + done, ...(done ? { status: 200, ms: 3000, tokens: 900 } : {}), + }; +} +const first = [req(100, "glm", "done")]; + +function serve(lang, feed) { + const state = { agents: [{ id: "codex", name: "Codex", path: "/test/codex", fields: [] }], profiles: [], settings: { lang, 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:"${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") { + 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 rs = await new Promise((res) => { feed.next = res; }); + return trace(rs); + } + if (url.pathname === "/api/gateway/history") return json({ cut: false, days: [], routes: [] }); + if (url.pathname === "/api/groups") return json({ groups: Object.values(GROUPS), models: [], pools: [] }); + 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)]; + await route.fulfill({ body: await fs.readFile(file).catch(() => ""), contentType }); + }; +} + +const NOT_ROUTED = "magpie hasn't routed this request yet, so who takes it isn't picked yet."; + +for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) { + test(`${engine}: the routing card stays on one request while others come at once`, async (t) => { + const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" })); + t.after(() => browser.close()); + for (const lang of ["en", "zh", "zh-TW", "ja", "de"]) { + await t.test(lang, async () => { + const context = await browser.newContext({ viewport: { width: 1100, height: 900 } }); + t.after(() => context.close()); + const page = await context.newPage(); + page.setDefaultTimeout(5000); + const errors = []; + page.on("pageerror", (e) => errors.push(e.message)); + const feed = { next: null }; + await page.route("**/*", serve(lang, feed)); + await page.goto("http://magpie.test/?view=routing"); + await page.locator(".rt-req").first().waitFor(); + await page.locator(".rt-what b").waitFor(); + const notRouted = await page.evaluate(([l, s]) => (l === "en" ? s : I18N[l]?.[s]), [lang, NOT_ROUTED]); + assert.ok(notRouted, `the not-routed line has its ${lang}`); + + // every frame from here on: whose route the head names, and its line + await page.evaluate(() => { + window.__frames = []; + window.__on = true; + const tick = () => { + window.__frames.push([document.querySelector(".rt-what b")?.textContent || "", document.querySelector(".rt-mode")?.textContent || ""]); + if (window.__on) requestAnimationFrame(tick); + }; + requestAnimationFrame(tick); + }); + const send = async (...rs) => { + for (let i = 0; i < 100 && !feed.next; i++) await page.waitForTimeout(50); + const next = feed.next; + assert(next, "the page asks for the next trace"); + feed.next = null; + next(rs); + await page.waitForTimeout(350); + }; + const head = () => page.locator(".rt-what b").textContent(); + + // conversation glm's request 101 is routed and under way + await send(req(101, "glm", "going")); + assert.equal(await head(), "GLM Flash"); + // conversation ds sends 102: not routed yet, then routed; glm's + // 101 is still under way, and the card stays on it + await send(req(102, "ds", "held")); + await send(req(102, "ds", "going")); + await send(req(101, "glm", "going"), req(102, "ds", "going")); + assert.equal(await head(), "GLM Flash", "the card stays on 101 while it is under way"); + // 101 is done: the card goes on to the newest, ds's 102 + await send(req(101, "glm", "done")); + assert.equal(await head(), "DeepSeek Flash"); + // glm's next, 103, comes in not routed yet: 102 still holds it + await send(req(103, "glm", "held")); + await send(req(102, "ds", "going"), req(103, "glm", "held")); + assert.equal(await head(), "DeepSeek Flash", "the card stays on 102 while it is under way"); + // 102 is done while 103 is still held: a routed one stays rather + // than the card dropping to "not routed yet" + await send(req(102, "ds", "done")); + assert.equal(await head(), "DeepSeek Flash", "a request not routed yet doesn't take the card from one routed"); + // 103 is routed: now it is told + await send(req(103, "glm", "going")); + assert.equal(await head(), "GLM Flash"); + + const frames = await page.evaluate(() => { window.__on = false; return window.__frames; }); + assert(frames.length > 20, `frames were watched: ${frames.length}`); + // the head named each route in one run: no going back and forth + const runs = frames.map((f) => f[0]).filter((x, i, a) => i === 0 || x !== a[i - 1]); + assert.deepEqual(runs, ["GLM Flash", "DeepSeek Flash", "GLM Flash"], `the head switched only when the request it told of was done: ${JSON.stringify(runs)}`); + assert(!frames.some((f) => f[1] === notRouted), "the card never dropped to not routed yet"); + assert.deepEqual(errors, []); + }); + } + }); +}