Routing's request list showed "缓存 100%" for Codex requests of 77.5K
tokens that read 77,200 of them from the cache (99.6%), beside rows at
99%, and the route line said "请求缓存命中率 99%". Every hit rate was
Math.round(100 * h) + "%", so anything from 99.5% up read 100%.
One formatter now says every hit rate: hitPct(h, d) in app.js, whole
percents (d decimals) unless rounding would say 100% or 0% of a prompt
not all or none cached, then one or two decimals (99.6%, 99.95%; never
past 99.99% or under 0.01%). Its callers: Routing's request rows, their
route line and a session's Avg. cache (plugins/routing/client.js); the
tray panel's Cache hit rate and the ranking rows' hit rate (app.js); the
Usage page's tiles, agent rows, Requests table, request detail and its
KPI strip, which keeps its one decimal (plugins/usage/page.js); the
context card's Prompt cache and the session traits Cache-friendly,
Cache misses and Cache hit (context.js). The TUI's and CLI's sessions
line, which floored the integer, use usage.HitPct, the same rule in Go;
it rounds now, so the TUI test's 67% (25.2K of 37.3K, 67.6%) reads 68%.
No new strings; app.js pays its bytes with six ledger comments cut, so
the core scripts are 15 bytes smaller than at its parent, no raise. Main
is over the core budget already (1,534,476 > 1,533,802 at the parent,
#5918); this commit doesn't add to it.
Run on mote:
- new internal/gui/tests/cache-hit-pct.test.cjs (Routing rows 99.6% /
100% / 99%, the route line, hitPct at its edges) in chromium and
webkit, en and zh: green; with the escalation line removed by hand,
red (99.99% where 99.6% is wanted).
- cache-hit-pct, panel-usage, context-window, i18n-keys, usage-ledger,
tray-totals, ctx-route, ledger-cache-hit, usage-hit-rate,
routing-request-metrics (both engines; ledger-cache-unknown and
ledger-cache-tokens in an earlier run): 186 pass, 6 red, all in
usage-ledger's 'newest live route needs no return-to-live button',
red the same on origin/main 00e8242e8 (#5048, #4078).
- go test ./internal/usage ./internal/cli ./internal/tui: ok, with
TestHitPctNeverRoundsToAllOrNone and a 99.6% case in
TestHitRateCountsCacheWrites (red on the old floor: 99%).
- go test ./internal/gui at the first base: TestCoreScriptBudget ok; one red,
TestGatewayModeStatsNameTheGatewaySessions, run at 00:25 local (#5690,
the first hour of a local day).
- gofmt; go vet darwin, GOOS=linux -tags nogui, GOOS=windows: clean.
At the pushed head (rebased, comments cut): cache-hit-pct and
routing-request-metrics again, both engines, 8 of 8.
Not tried: the real app's window, or GUI files beyond those above (a
65-file run waited on mote's suite lock behind four full suites).
72 lines
4.7 KiB
JavaScript
72 lines
4.7 KiB
JavaScript
// A cache hit rate never reads 100% unless every prompt token came from the
|
|
// cache (ZhangXiaof48473 on X: Codex gpt-6.1-sol requests of 77.5K tokens
|
|
// read "缓存 100%" on Routing's request list beside "缓存 99%"). Where
|
|
// rounding to a whole percent would say 100% or 0% of a prompt not all or
|
|
// none cached, the rate shows a decimal or two: 99.6%. One formatter
|
|
// (hitPct) says every hit rate. 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 = new Date();
|
|
const seat = { id: "codex", provider: "codex", name: "Codex", who: "test@example.com", kind: "account", model: "gpt-6.1-sol" };
|
|
function req(id, input, read) {
|
|
const time = new Date(now.getTime() - (20 - id) * 60e3).toISOString();
|
|
return { id, seq: id, time, agent: "codex", session: "cache-pct", model: "codex/gpt-6.1-sol", provider: "codex",
|
|
order: [seat], tries: [{ id: seat.id, model: seat.model, start: time, done: true, status: 200, ms: 11000, ttft: 7000 }],
|
|
done: true, status: 200, ms: 11000, ttft: 7000, tokens: input + read + 400, out: 400,
|
|
usage: [{ provider: seat.provider, model: seat.model, in: input, out: 400, cache_read: read, cache_write: 0 }], priced: true, cost: 0.01 };
|
|
}
|
|
// newest first: 77,200 of 77,500 read (99.6%), all read, 77,035 of 77,500 (99.4%)
|
|
const fixtures = [req(3, 300, 77200), req(2, 0, 77500), req(1, 465, 77035)];
|
|
|
|
function serve(lang) {
|
|
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", fields: [] }], profiles: [], settings: { lang, theme: "light" } });
|
|
if (url.pathname === "/api/gateway/trace") {
|
|
if (url.searchParams.has("wait")) return new Promise(() => {});
|
|
return json({ mine: true, now: now.toISOString(), seq: 3, totals: { requests: fixtures.length, rerouted: 0, errors: 0 }, routes: fixtures });
|
|
}
|
|
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: [], 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)];
|
|
try { await route.fulfill({ body: await fs.readFile(file), contentType }); } catch { await route.fulfill({ status: 404 }); }
|
|
};
|
|
}
|
|
|
|
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
|
|
for (const lang of ["en", "zh"]) {
|
|
test(`${engine} ${lang}: a cache hit rate reads 100% only when all of the prompt was cached`, async (t) => {
|
|
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
|
|
t.after(() => browser.close());
|
|
const page = await browser.newPage({ viewport: { width: 1280, height: 900 }, reducedMotion: "reduce" });
|
|
page.setDefaultTimeout(5000);
|
|
page.setDefaultNavigationTimeout(60000);
|
|
const errors = [];
|
|
page.on("pageerror", (e) => errors.push(e.message));
|
|
await page.route("**/*", serve(lang));
|
|
await page.goto("http://magpie.test/?view=routing");
|
|
const rows = page.locator(".rt-req");
|
|
await rows.nth(fixtures.length - 1).waitFor();
|
|
assert.deepEqual(await rows.locator(".cache-hit .v").allTextContents(), ["99.6%", "100%", "99%"]);
|
|
await rows.nth(0).click();
|
|
assert.match(await page.locator(".rt-steps").textContent(), lang === "zh" ? /请求缓存命中率 99\.6%/ : /request cache hit rate 99\.6%/);
|
|
// every caller's shared formatter, at its edges
|
|
const got = await page.evaluate(() => [[1], [0], [0.6], [0.994], [0.9995], [0.99999], [0.003], [0.0004], [1e-7], [0.873, 1], [0.99996, 1], [1, 1]]
|
|
.map(([h, d]) => hitPct(h, d)));
|
|
assert.deepEqual(got, ["100%", "0%", "60%", "99%", "99.95%", "99.99%", "0.3%", "0.04%", "0.01%", "87.3%", "99.99%", "100.0%"]);
|
|
assert.deepEqual(errors, []);
|
|
});
|
|
}
|
|
}
|