diff --git a/internal/gui/assets/context.js b/internal/gui/assets/context.js index 0dae6dc9d..0b250de6d 100644 --- a/internal/gui/assets/context.js +++ b/internal/gui/assets/context.js @@ -1054,6 +1054,17 @@ ? 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 + 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); + const lean = same && timed && band.length && r.cost > fewest * 1.03 ? band[band.length - 1] : null; + const bandName = lean ? (band[0] === lean ? show(lean.value) : show(band[0].value) + "–" + show(lean.value)) : ""; const why = el("p", "ctx-tune-why"); const f = r.facts || {}; @@ -1080,15 +1091,16 @@ row.append(why); // a window advised is weighed by tokens alone: what a compaction loses of // the task isn't measured, and a smaller one compacts more often (#1854) - if (a.compact && !same) row.append(el("p", "ctx-tune-caveat", timed + if (a.compact && (!same || lean)) row.append(el("p", "ctx-tune-caveat", timed ? t("Tokens and waiting only: it doesn't measure how well the agent keeps the task after a compaction. The lower the window, the more often it compacts.") : t("A token estimate only: it doesn't measure how well the agent keeps the task after a compaction. The lower the window, the more often it compacts."))); const foot = el("div", "ctx-tune-foot"); - const verdict = same || applied + const verdict = !lean && (same || applied) ? el("span", "ctx-chip good", "") : el("span", "ctx-chip info", ""); - verdict.append(el("i"), same ? t("Already the leanest for how you work") : applied ? t("In use") : t("{pct} fewer tokens · {tokens} over the range", { pct: pct(saves), tokens: fmtK(Math.round(r.cost - r.low)) })); + verdict.append(el("i"), lean ? t("Not the leanest: {range} spends the fewest tokens", { range: bandName }) + : same ? t("Already the leanest for how you work") : applied ? t("In use") : t("{pct} fewer tokens · {tokens} over the range", { pct: pct(saves), tokens: fmtK(Math.round(r.cost - r.low)) })); foot.append(verdict, el("span", "grow")); const note = el("div", "ctx-tune-src"); title.after(note); @@ -1125,6 +1137,16 @@ go.onclick = () => send("apply", best); foot.append(go); } + if (lean && !set.locked) { + const go = el("button", "text primary", t("Use {value}", { value: show(lean.value) })); + go.type = "button"; + go.disabled = tuneBusy === key; + go.dataset.tt = set.value && !set.ours + ? t("Replaces your {value}; Undo puts it back", { value: show(set.value) }) + : t("Fewer tokens, but more compactions to wait on"); + go.onclick = () => send("apply", lean.value); + foot.append(go); + } // a window of the reader's own (#1860): one who would spend some tokens // for fewer compactions picks it from the same replay the advice comes // from, each value with what it spends past the leanest and how often diff --git a/internal/gui/assets/i18n.js b/internal/gui/assets/i18n.js index 4356f73fa..2788cdbc0 100644 --- a/internal/gui/assets/i18n.js +++ b/internal/gui/assets/i18n.js @@ -120,6 +120,8 @@ const I18N = { "Your conversations grow {growth} a call, and none was compacted in the range.": "你的对话每次调用增长 {growth},这段时间没有一次压缩。", "{quick} of your calls came within 5 minutes of the one before, {pause} after a pause of 5 to 60 minutes.": "你 {quick} 的调用距上一次不到 5 分钟,{pause} 在停顿 5 到 60 分钟之后。", "Already the leanest for how you work": "已是最适合你用法的最省配置", + "Not the leanest: {range} spends the fewest tokens": "当前不是最省:{range} 最省 Token", + "Fewer tokens, but more compactions to wait on": "Token 更少,但要多等几次压缩", "{pct} fewer tokens · {tokens} over the range": "少 {pct} Token · 这段时间省 {tokens}", "{name} is set and comes first": "已设置 {name},它优先", "Set by magpie": "由 magpie 设置", @@ -3821,6 +3823,8 @@ const I18N = { "Your conversations grow {growth} a call, and none was compacted in the range.": "你的對話每次呼叫增長 {growth},這段時間沒有一次壓縮。", "{quick} of your calls came within 5 minutes of the one before, {pause} after a pause of 5 to 60 minutes.": "你 {quick} 的呼叫距上一次不到 5 分鐘,{pause} 在停頓 5 到 60 分鐘之後。", "Already the leanest for how you work": "已是最適合你用法的最省配置", + "Not the leanest: {range} spends the fewest tokens": "目前不是最省:{range} 最省 Token", + "Fewer tokens, but more compactions to wait on": "Token 更少,但要多等幾次壓縮", "{pct} fewer tokens · {tokens} over the range": "少 {pct} Token · 這段時間省 {tokens}", "{name} is set and comes first": "已設定 {name},它優先", "Set by magpie": "由 magpie 設定", @@ -7497,6 +7501,8 @@ const I18N = { "Your conversations grow {growth} a call, and none was compacted in the range.": "会話は 1 回の呼び出しごとに {growth} 増え、この期間にコンパクトはありませんでした。", "{quick} of your calls came within 5 minutes of the one before, {pause} after a pause of 5 to 60 minutes.": "呼び出しの {quick} は前回から 5 分以内、{pause} は 5〜60 分の間を置いた後でした。", "Already the leanest for how you work": "すでにあなたの使い方で最も省トークン", + "Not the leanest: {range} spends the fewest tokens": "最少ではありません:{range} でトークンが最も少なくなります", + "Fewer tokens, but more compactions to wait on": "トークンは減りますが、コンパクトの待ちが増えます", "{pct} fewer tokens · {tokens} over the range": "トークン {pct} 減 · この期間で {tokens}", "{name} is set and comes first": "{name} が設定されていて優先されます", "Set by magpie": "magpie が設定", @@ -11180,6 +11186,8 @@ const I18N = { "Your conversations grow {growth} a call, and none was compacted in the range.": "Deine Unterhaltungen wachsen um {growth} pro Aufruf, und im Zeitraum wurde keine kompaktiert.", "{quick} of your calls came within 5 minutes of the one before, {pause} after a pause of 5 to 60 minutes.": "{quick} deiner Aufrufe kamen innerhalb von 5 Minuten nach dem vorigen, {pause} nach einer Pause von 5 bis 60 Minuten.", "Already the leanest for how you work": "Schon am sparsamsten für deine Arbeitsweise", + "Not the leanest: {range} spends the fewest tokens": "Nicht am sparsamsten: {range} verbraucht die wenigsten Tokens", + "Fewer tokens, but more compactions to wait on": "Weniger Tokens, aber mehr Kompaktierungen, auf die du wartest", "{pct} fewer tokens · {tokens} over the range": "{pct} weniger Tokens · {tokens} im Zeitraum", "{name} is set and comes first": "{name} ist gesetzt und hat Vorrang", "Set by magpie": "Von magpie gesetzt", diff --git a/internal/gui/tests/context-tune.test.cjs b/internal/gui/tests/context-tune.test.cjs index ba2018742..9598f38a4 100644 --- a/internal/gui/tests/context-tune.test.cjs +++ b/internal/gui/tests/context-tune.test.cjs @@ -444,3 +444,98 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", assert.deepEqual(errors, []); }); } + +// A window the reader picked past the leanest band (yapechan on Discord: +// "Other window…" to a larger one). With compactions timed, tune's advice +// never waits longer than the window now, so the picked window comes back +// as its own advice; the row said "Already the leanest" at +52% tokens. +// It now says it isn't, names the band (the windows within 3% of the +// cheapest), and offers the band's largest window, which compacts least of +// them; a pick inside the band still says it is the leanest and offers +// nothing. In both engines, every language, a phone's width and a desk's. +const bandCurve = () => { + const pts = []; + for (let v = 100000; v <= 500000; v += 10000) { + const compacts = Math.round(9e6 / v); + pts.push({ value: v, cost: 1e9 + Math.max(0, Math.abs(v - 250000) - 20000) * 4000, compacts, wait: compacts * 61 }); + } + return pts; +}; +const bandTune = (value) => { + const pts = bandCurve(), at = pts.find((p) => p.value === value); + return { + days: 7, + agents: [{ agent: "claude", calls: 4300, advice: [ + { knob: "compact", model: "claude-opus-5-5", setting: { value: String(value), source: "model", ours: true }, + compact: { model: "claude-opus-5-5", current: value, window: 1000000, best: value, cost: at.cost, low: at.cost, curve: pts, wait: at.wait, wait_best: at.wait, cheapest: 250000, wait_cheapest: 2196, facts: { ...facts, took: 61, timed: 9 } } }, + ] }], + }; +}; +const notLeanest = { + en: "Not the leanest: 230K–270K spends the fewest tokens", + zh: "当前不是最省:230K–270K 最省 Token", + "zh-TW": "目前不是最省:230K–270K 最省 Token", + ja: "最少ではありません:230K–270K でトークンが最も少なくなります", + de: "Nicht am sparsamsten: 230K–270K verbraucht die wenigsten Tokens", +}; +const leanest = { en: "Already the leanest for how you work", zh: "已是最适合你用法的最省配置", "zh-TW": "已是最適合你用法的最省配置", ja: "すでにあなたの使い方で最も省トークン", de: "Schon am sparsamsten für deine Arbeitsweise" }; +const use270 = { en: "Use 270K", zh: "使用 270K", "zh-TW": "使用 270K", ja: "270K にする", de: "270K verwenden" }; +for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) { + test(`${engine}: a window picked past the leanest band isn't called the leanest`, async (t) => { + const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" })); + const errors = []; + t.after(() => browser.close()); + for (const lang of Object.keys(heading)) for (const [width, height] of [[360, 760], [1100, 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 {} }); + const page = await ctx.newPage(); + page.setDefaultTimeout(5000); + page.on("pageerror", (e) => errors.push(e.message)); + const posts = []; + let value = 400000; + const base = serve(lang, posts); + await page.route("**/*", (r) => { + const p = new URL(r.request().url()).pathname; + if (p === "/api/tune") return r.fulfill({ json: bandTune(value) }); + if (p === "/api/tune/apply") { + const b = JSON.parse(r.request().postData()); + posts.push(b); + value = +b.value; + return r.fulfill({ json: { value: b.value, source: "model", ours: true } }); + } + return base(r); + }); + await page.goto("http://magpie.test/?view=usage"); + const row = page.locator(".ctx-tune-row").nth(0); + await row.locator(".ctx-tune-pick").waitFor(); + const chip = row.locator(".ctx-tune-foot .ctx-chip"); + assert.equal(await chip.innerText(), notLeanest[lang]); + assert.equal(await chip.getAttribute("class"), "ctx-chip info"); + const use = row.locator(".ctx-tune-foot button.primary"); + assert.equal(await use.count(), 1); + assert.equal(await use.innerText(), use270[lang]); + const over = await page.evaluate(() => { + const view = document.querySelector("#view-usage"); + const out = view.scrollWidth > view.clientWidth + 1 ? ["page " + view.scrollWidth + ">" + view.clientWidth] : []; + const b = document.querySelector(".ctx-tune").getBoundingClientRect(); + for (const e of document.querySelectorAll(".ctx-tune-foot *")) { + const r = e.getBoundingClientRect(); + if (r.width && (r.right > b.right + 1 || r.left < b.left - 1)) out.push(e.className + " " + Math.round(r.left) + "–" + Math.round(r.right)); + } + return out; + }); + assert.deepEqual(over, []); + await use.evaluate((b) => b.click()); + await row.locator(".ctx-tune-foot .ctx-chip.good").waitFor(); + assert.deepEqual(posts.map((p) => [p.agent, p.knob, p.model, p.value]), [["claude", "compact", "claude-opus-5-5", "270000"]]); + // inside the band: the leanest, and no button to change it + const row2 = page.locator(".ctx-tune-row").nth(0); + assert.equal(await row2.locator(".ctx-tune-foot .ctx-chip").innerText(), leanest[lang]); + assert.equal(await row2.locator(".ctx-tune-foot button.primary").count(), 0); + await ctx.close(); + }); + } + assert.deepEqual(errors, []); + }); +}