diff --git a/internal/gui/assets/context.js b/internal/gui/assets/context.js index 422077872..bc3c9bff1 100644 --- a/internal/gui/assets/context.js +++ b/internal/gui/assets/context.js @@ -1308,12 +1308,7 @@ ? chart(a.compact.curve || [], r.current, r.best, show, timed) : chart([{ value: "5m", cost: r.short }, { value: "1h", cost: r.hour }], r.current, r.best, show); if (c) row.append(c); - // the leanest band: the windows within 3% of the cheapest on the curve, - // the share tune counts as costing the same. Timed, the advice keeps a - // window that waits no longer than now, so a window the reader picked - // past the band (yapechan on Discord: a larger one) comes back as the - // advice and isn't the leanest; it says so, names the band, and offers - // the band's largest window, the one of them that compacts least + // the leanest band: within 3% of the cheapest (yapechan on Discord) const curve = (a.compact && a.compact.curve) || []; const fewest = curve.length ? Math.min(...curve.map((p) => p.cost)) : 0; const band = curve.filter((p) => p.cost <= fewest * 1.03); @@ -1404,10 +1399,7 @@ // an hour no Undo puts back, too few calls to weigh (linkaww) if (a.few && ttl && cur === "1h" && !held && !set.ours) use("5m", yours); if (lean && !held) use(lean.value, yours || t("Fewer tokens, but more compactions to wait on")); - // a window of the reader's own (#1860), from the same replay: each - // value's tokens against the setting now, which the reader picks away - // from (hayden4608 on Discord: against the leanest, every line read as - // more), and compactions, both estimates + // a window of the reader's own (#1860), against the setting now (hayden4608) const pts = (a.compact && a.compact.curve) || []; if (pts.length > 1 && !held) { const other = el("button", "text ctx-tune-pick", t("Other window…")); @@ -1498,6 +1490,10 @@ pane.append(grid); } + // what this page can draw: another version's reply can hold other rows + // (Linka on Discord: a page from before few rows threw on one, "r.cost") + const obj = (r) => !!r && typeof r == "object", objs = (l) => Array.isArray(l) ? l.filter(obj) : []; + async function loadTune(force) { const read = ++tuneRead, days = ctxDays; if (tuneData && tuneData.days !== +days) tuneData = null; @@ -1505,7 +1501,7 @@ if (read !== tuneRead) return; const json = JSON.stringify(data); if (!force && tuneData && json === tuneJSON) return; - tuneData = { days: +data.days || +days, agents: (data.agents || []).map((a) => ({ ...a, advice: a.advice || [] })) }, tuneJSON = json; + tuneData = { days: +data.days || +days, agents: objs(data?.agents).map((a) => ({ ...a, advice: objs(a.advice).filter((r) => obj(r.compact) || obj(r.ttl) || r.few).map((r) => ({ ...r, setting: obj(r.setting) ? r.setting : {} })) })) }, tuneJSON = json; renderContext(); } diff --git a/internal/gui/tests/context-tune-shapes.test.cjs b/internal/gui/tests/context-tune-shapes.test.cjs new file mode 100644 index 000000000..29217a8ce --- /dev/null +++ b/internal/gui/tests/context-tune-shapes.test.cjs @@ -0,0 +1,113 @@ +// Run with Node's test runner and Playwright on the module path; see README.md. +// Usage › Context's Tuned for you draws what /api/tune answers, and the +// magpie answering can be of another version than the page (Linka on +// Discord, a Docker magpie: "undefined is not an object (evaluating +// 'r.cost')"). A row put in with too few calls has no replay (few, no +// compact or ttl), and a row this page doesn't know, a null or one with +// no setting, is left out: the page draws the rest and throws nothing, in +// Chromium and WebKit, in English and Chinese, at 900 and 420 wide. +// MAGPIE_ASSETS runs it against another version's assets. +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(process.env.MAGPIE_ASSETS || path.resolve(__dirname, "../assets")); +const facts = { calls: 4100, streams: 60, compacts: 0, peak: 166000, after: 57000, growth: 1000, rework: 4700, quick: 0.95, pause: 0.045, away: 0.005, hour: 0.9 }; +// as magpie sends them since v0.1.3120: put in, too few calls, no replay +const fewRows = { knob: "cache_ttl", setting: { value: "1h", source: "settings", ours: true }, few: true }; +const codex = { agent: "codex", calls: 900, advice: [ + { knob: "compact", setting: {}, compact: { current: 200000, best: 200000, cost: 2e8, low: 2e8, curve: [{ value: 100000, cost: 3e8 }, { value: 200000, cost: 2e8 }], facts } }, +] }; +const cases = { + // the reply the report's page met + "too few calls": { days: 7, agents: [{ agent: "claude", calls: 3, advice: [fewRows, { knob: "compact", model: "claude-haiku-4-5", setting: {}, few: true }] }, codex] }, + // rows and agents this page doesn't know: left out + "a shape not known": { days: 7, agents: [ + { agent: "claude", calls: 3, advice: [ + fewRows, + { knob: "subagent_cache_ttl", setting: { value: "5m" } }, + { knob: "compact", model: "claude-opus-5-5", compact: null, ttl: null }, + null, + "cache_ttl", + // too few calls, its setting not said + { knob: "compact", model: "claude-haiku-4-5", few: true }, + ] }, + codex, + null, + ] }, +}; + +function serve(lang, tune) { + const state = { agents: [{ id: "claude", name: "Claude Code", path: "/test/claude", fields: [] }, { id: "codex", name: "Codex", path: "/test/codex", fields: [] }], profiles: [], settings: { lang, theme: "light" } }; + return async (r) => { + const url = new URL(r.request().url()); + const json = (data) => r.fulfill({ json: data }); + if (url.pathname === "/boot.js") return r.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:"${lang}",theme:"light",web:true};` }); + if (url.pathname === "/wails/runtime.js") return r.fulfill({ contentType: "text/javascript", body: "export const Window = {};" }); + if (url.pathname === "/api/state") return json(state); + if (url.pathname === "/api/tune") return json(tune); + if (url.pathname === "/api/context") return json({ days: 7, agents: [], sessions: [] }); + if (url.pathname === "/api/gateway/trace") { + if (url.searchParams.get("wait")) return new Promise(() => {}); + return json({ mine: true, now: new Date().toISOString(), seq: 0, totals: { requests: 0, rerouted: 0, errors: 0 }, routes: [] }); + } + 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 === "/api/usage/quotas") return json([]); + if (url.pathname === "/api/groups") return json({ groups: [], models: [] }); + if (url.pathname === "/api/plugins") return json({ plugins: [] }); + 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 r.fulfill({ body: await fs.readFile(file).catch(() => ""), contentType, status: 200 }); + }; +} + +const few = { en: "Not enough calls in this range to tune on", zh: "这段时间的调用太少,还算不出建议" }; +for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) { + for (const [name, tune] of Object.entries(cases)) test(`${engine}, ${name}: Tuned for you draws the rows it knows`, async (t) => { + const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" })); + t.after(() => browser.close()); + for (const lang of Object.keys(few)) for (const [width, height] of [[900, 760], [420, 760]]) { + await t.test(`${lang}, ${width}x${height}`, async () => { + const ctx = await browser.newContext({ viewport: { width, height }, reducedMotion: "reduce" }); + await ctx.addInitScript(() => { + try { localStorage.setItem("magpie.usageTab", "context"); } catch {} + // every line the status pill says, which an error leaves after 8s + window.__said = []; + new MutationObserver(() => { const s = document.querySelector("#status"); if (s?.textContent) window.__said.push(s.textContent); }) + .observe(document, { subtree: true, childList: true, characterData: true }); + }); + const page = await ctx.newPage(); + page.setDefaultTimeout(5000); + page.setDefaultNavigationTimeout(60000); + const errors = []; + page.on("pageerror", (e) => errors.push(e.message)); + await page.route("**/*", serve(lang, tune)); + await page.goto("http://magpie.test/?view=usage"); + await page.locator(".ctx-tunes").waitFor({ timeout: 10000 }).catch(() => {}); + assert.deepEqual(errors, []); + // the load's failure is said in the status line, as the report shows it + const said = await page.evaluate(() => [document.body.innerText, ...window.__said].join("\n")); + assert.doesNotMatch(said, /is not an object|Cannot read properties|TypeError|is not iterable/); + const cards = page.locator(".ctx-tune"); + assert.equal(await cards.count(), 2); + const claude = cards.nth(0).locator(".ctx-tune-row"); + assert.equal(await claude.count(), 2, "the two few rows, nothing for the rows not known"); + for (const i of [0, 1]) { + assert.equal(await claude.nth(i).locator(".ctx-tune-why").innerText(), few[lang]); + assert.doesNotMatch(await claude.nth(i).innerText(), /NaN|undefined|null/); + } + assert.equal(await claude.nth(0).locator("button").count(), 1, "Undo"); + assert.equal(await cards.nth(1).locator(".ctx-tune-row").count(), 1); + const over = await page.evaluate(() => { + const view = document.querySelector("#view-usage"); + return view.scrollWidth > view.clientWidth + 1 ? view.scrollWidth + ">" + view.clientWidth : ""; + }); + assert.equal(over, ""); + await ctx.close(); + }); + } + }); +}