A long or reasoning reply looked stuck on the Routing page. A request still out now shows, on its row and beside the account answering it, the bytes the agent has had, "≈ n tokens" once a rate is learned, and how long ago content last came; after 15s with no content the account's dot stops pulsing. Nothing stays once the request is done. Ported from Canda15's 7f7bdb189 (github.com/Canda15/magpie), the idea and the per-protocol content extractors kept, the hot path and storage reworked: - every write to the agent passes captureResponseWriter.Write, which calls liveRow.observe; the row hangs on the request's inflight (an atomic pointer), not a context value made per request - rates are in memory only (256 pairs); the fork wrote a JSON file under the estimate lock on every finished request - a whole JSON reply is counted in bytes only: the fork held and decoded up to 8 MiB of it at the end; a stream is cut into lines in place, only a line cut by a write's end is copied, a line over 1 MiB passed over, and each event decoded once (the fork ran json.Valid then Unmarshal) - the reply's kind comes from its first byte, not a header read on every write; the keepalive check allocates nothing (the fork split each write) - everything a row counts is under its own lock, and inflight.tr is read under f.mu; the estimate is in float64 (the int64 product overflowed) - the route's Seq is bumped on every write and traceGather batches the wake-ups; the fork's global throttle could drop the last update before a pause - learned only from a streamed reply that did not fail and had the vendor's output tokens - left out: the finished row's "Received" metric and the ended caption (layout restraint; an ended row has its own metrics), a duplicate "Received" i18n key, the receive-monitor doc (the design is in gateway-routing.md now), the fork's xmod and Windows CI commits The Routing plugin and i18n.js are outside TestCoreScriptBudget. Run on mote at this head unless named: gofmt -l; go vet (darwin), GOOS=linux -tags nogui and GOOS=windows go vet ./...; go build ./...; go test -race on the live tests; go test ./internal/gateway ./internal/gui (gateway ok; gui red only on TestCoreScriptBudget, #5139, 1533907 > 1533802 before this change and untouched by it, and TestMainWindowCorners/macOS_26_SDK, #3949); routing-live-bytes.test.cjs and i18n-keys in Chromium and WebKit, en/zh/zh-TW/ja/de, 1100 and 420px (24/24); every routing-* and panel-routing* GUI file with i18n-keys in WSL Linux (Chromium all green; WebKit reds in 12 files, the same class at origin/main7dd2434d3run the same way: 25 there, 28 here, the sets differing by timing samples; routing-live-patch alone red 2 of 3 on main, 1 of 3 here; filed as #4121, #4520, #5156, #2174). Rebased onto38f4f74e6(gateway.go, trace.go and the Routing plugin moved): build, gateway vet, the live tests, routing-live-bytes and i18n-keys run again there, green (24/24). Broken by hand, each red, then put back: - no observe in capture.Write: TestLiveCountsARealStream - commentOnly never true: TestLiveHeartbeatIsBytesNotContent, TestCommentOnlyTellsHeartbeatsApart - learning from failed replies: TestRatesLearnFromRepliesTheVendorFinished - a whole reply read as a stream: TestContentCounterLeavesAWholeReply - no Seq bump in liveBytes: TestLiveWritesReachTheRoute - no quiet class, no per-second repaint: routing-live-bytes 0/20 Not tried: a real vendor's stream in the running app; the ChatGPT backend chain (codex_backend.go) writes past the capture writer and isn't counted; the full Go and GUI suites run after the tag. Co-authored-by: Canda15 <124772373+Canda15@users.noreply.github.com>
128 lines
8.3 KiB
JavaScript
128 lines
8.3 KiB
JavaScript
// Run with Node's test runner and Playwright on the module path; see README.md.
|
|
// A reply in flight shows, live, how much of it has arrived and roughly
|
|
// its tokens (#4951, Canda15): the request's row has the bytes, "≈ n
|
|
// tokens" once a rate is learned, and how long ago content last came,
|
|
// changed in place as the trace says more (the row isn't built again), and
|
|
// the account answering it says the same. Quiet for 15s, the account's dot
|
|
// stops pulsing and "answering…" goes. Once the request is done none of it
|
|
// stays. English, Chinese (simplified and traditional), Japanese and
|
|
// German, at 1100px and 420px; 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 M = "glm-5";
|
|
const order = [{ id: "solo#s1", provider: "solo", name: "Solo", who: "solo-1", kind: "key", model: M }];
|
|
const ago = (s) => new Date(Date.now() - s * 1e3).toISOString();
|
|
// the request: in flight with the live numbers given, or done
|
|
const route = (live, done = false) => ({
|
|
id: 2, seq: 2, time: ago(30), agent: "zcode", model: M, provider: "solo", done, ...(done ? { status: 200, ms: 30000, out: 900 } : {}),
|
|
order, tries: [{ id: "solo#s1", model: M, start: ago(30), done, ...(done ? { status: 200, ms: 30000 } : {}) }],
|
|
...live,
|
|
});
|
|
|
|
function serve(lang, live) {
|
|
return async (request) => {
|
|
const url = new URL(request.request().url());
|
|
const json = (data) => request.fulfill({ json: data });
|
|
if (url.pathname === "/boot.js") return request.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:"${lang}",theme:"light",web:true};` });
|
|
if (url.pathname === "/wails/runtime.js") return request.fulfill({ contentType: "text/javascript", body: "export const Window = {};" });
|
|
if (url.pathname === "/api/state") return json({ agents: [{ id: "zcode", name: "ZCode", path: "/test/config.json", fields: [] }], profiles: [], settings: { lang, theme: "light" } });
|
|
if (url.pathname === "/api/gateway/trace") {
|
|
if (url.searchParams.get("wait")) {
|
|
if (live.queued.length) return json(live.queued.shift());
|
|
live.pending.push(request);
|
|
return;
|
|
}
|
|
return json({ mine: true, now: new Date().toISOString(), seq: 1, totals: { requests: 1, rerouted: 0, errors: 0 }, routes: [route({ bytes: 2048, contentAt: Date.now() - 1000 })] });
|
|
}
|
|
if (url.pathname === "/api/gateway/history") return json({ cut: false, days: [], routes: [] });
|
|
if (url.pathname === "/api/groups") return json({ groups: [], pools: [] });
|
|
if (url.pathname === "/api/providers") return json({ providers: [], excluded: [], gateway: { running: true, window: true } });
|
|
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 request.fulfill({ body: await fs.readFile(file), contentType });
|
|
};
|
|
}
|
|
|
|
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
|
|
for (const lang of ["en", "zh", "zh-TW", "ja", "de"]) {
|
|
for (const width of [1100, 420]) {
|
|
test(`${engine} ${lang} ${width}px: a reply in flight shows what has arrived, and nothing once it is done`, async (t) => {
|
|
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
|
|
t.after(() => browser.close());
|
|
const page = await browser.newPage({ viewport: { width, height: 900 } });
|
|
page.setDefaultTimeout(5000);
|
|
page.setDefaultNavigationTimeout(60000);
|
|
const errors = [];
|
|
page.on("pageerror", (error) => errors.push(error.message));
|
|
const live = { pending: [], queued: [], seq: 1 };
|
|
await page.route("**/*", serve(lang, live));
|
|
const send = (...routes) => {
|
|
const d = { mine: true, now: new Date().toISOString(), seq: ++live.seq, totals: { requests: live.seq }, routes };
|
|
const r = live.pending.shift();
|
|
if (r) r.fulfill({ json: d }); else live.queued.push(d);
|
|
};
|
|
const say = (key, vars = {}) => page.evaluate(([lang, key, vars]) => {
|
|
const s = (lang !== "en" && I18N[lang]?.[key]) || key;
|
|
return s.replace(/\{(\w+)\}/g, (_, k) => vars[k] ?? `{${k}}`);
|
|
}, [lang, key, vars]);
|
|
const cells = () => page.evaluate(() => [...document.querySelectorAll(".rt-req .rt-metric.recv")].map((c) => c.textContent.trim()));
|
|
const account = page.locator(".rt-accts li", { has: page.locator(".who", { hasText: /^solo-1$/ }) });
|
|
|
|
await page.goto("http://magpie.test/?view=routing");
|
|
await page.locator(".rt-req .rt-metric.recv").first().waitFor();
|
|
|
|
// 2 KiB in, content a second ago, no rate learned: no tokens said
|
|
let got = await cells();
|
|
assert.equal(got.length, 2, JSON.stringify(got));
|
|
assert.equal(got[0], "2.0 KiB");
|
|
assert.ok(got[1].includes(await say("{n} s", { n: 1 })) || got[1].includes(await say("{n} s", { n: 2 })), got[1]);
|
|
const answering = await say("answering…");
|
|
await page.waitForFunction(() => /KiB/.test(document.querySelector(".rt-accts li.now em")?.textContent || ""));
|
|
let em = (await account.locator("em").textContent()).trim();
|
|
assert.ok(em.startsWith(answering) && em.includes("2.0 KiB"), em);
|
|
|
|
// more arrives, a rate is learned: the same cells change in place
|
|
await page.evaluate(() => { window.firstCell = document.querySelector(".rt-req .rt-metric.recv"); window.firstRow = document.querySelector(".rt-req"); });
|
|
send(route({ bytes: 3 * 1048576, estTok: 1234, contentAt: Date.now() }));
|
|
await page.waitForFunction(() => document.querySelectorAll(".rt-req .rt-metric.recv").length === 3);
|
|
got = await cells();
|
|
assert.equal(got[0], "3.0 MiB");
|
|
assert.equal(got[1], await say("≈ {n} tokens", { n: "1.2k" }));
|
|
assert.ok(await page.evaluate(() => document.querySelector(".rt-req") === window.firstRow), "the row was built again");
|
|
send(route({ bytes: 3 * 1048576 + 600000, estTok: 1500, contentAt: Date.now() }));
|
|
await page.waitForFunction(() => document.querySelector(".rt-req .rt-metric.recv .v")?.textContent === "3.6 MiB");
|
|
assert.ok(await page.evaluate(() => document.querySelector(".rt-req .rt-metric.recv") === window.firstCell || document.querySelector(".rt-req") === window.firstRow));
|
|
// each cell says what it is when hovered
|
|
assert.equal(await page.locator(".rt-req .rt-metric.recv").first().getAttribute("title"), await say("How much of this reply has reached the client, framing included"));
|
|
// the page body never scrolls sideways
|
|
assert.ok(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth + 1));
|
|
|
|
// quiet for 20s: the dot stops, and the account says the numbers only
|
|
send(route({ bytes: 3 * 1048576 + 600000, estTok: 1500, contentAt: Date.now() - 20000 }));
|
|
await account.and(page.locator("li.quiet")).waitFor();
|
|
assert.equal(await account.locator(".dot").evaluate((d) => getComputedStyle(d).animationName), "none");
|
|
em = (await account.locator("em").textContent()).trim();
|
|
assert.ok(!em.includes(answering) && em.includes("3.6 MiB"), em);
|
|
assert.ok((await cells())[2].includes(await say("{n} s", { n: 20 })) || (await cells())[2].includes(await say("{n} s", { n: 21 })), (await cells())[2]);
|
|
// the page's second moves "ago" on without a trace answer
|
|
const before = (await cells())[2];
|
|
await page.waitForFunction((b) => document.querySelectorAll(".rt-req .rt-metric.recv")[2]?.textContent.trim() !== b, before, { timeout: 4000 });
|
|
|
|
// done: no live cells, nothing quiet, the account answered it
|
|
send(route({ bytes: 3 * 1048576 + 700000 }, true));
|
|
await page.waitForFunction(() => !document.querySelector(".rt-metric.recv"));
|
|
assert.equal(await page.locator(".rt-accts li.quiet").count(), 0);
|
|
em = (await account.locator("em").textContent()).trim();
|
|
assert.ok(!/MiB/.test(em), em);
|
|
assert.deepEqual(errors, []);
|
|
});
|
|
}
|
|
}
|
|
}
|