usage: the tags over the Context curve's dots never print over each other either (#1549)

The previous commit keeps the axis labels apart. The tags over the dots
had the same fault: a setting one step from the advice printed its
"+3%" on "Leanest", and a setting one step from the costliest value
printed "+147%" on "+150%". The tags now go by the same rule, "Leanest"
first, then the setting now, then the costliest: one within 16% of the
axis of a tag that matters more and at about its height (20 of the
plot's 100 units) keeps its dot and tooltip but not its tag.

Run: context-tune's overlap test gains a row whose setting sits one step
from the advice, checks the tags' boxes as well as the axis labels', and
runs in German ("Sparsamste", the widest "Leanest") as well as English,
at 360 and 1100px. With the previous commit's context.js it fails in
chromium and webkit ('en 360px: tag "+150%" overlaps "+147%"'); with
this one it passes. The axis test alone failed on main before the
previous commit ('360px: "160K" overlaps "170K"'). context-tune,
compact-at and context-window: 80/80. 56 context/usage/language GUI
files in both engines: 652/654; the two reds are usage-ledger's route
export (passes 3 of 3 alone, filed as #1876) and, in an earlier run,
usage-sum-whole at 780px in German (#1864, noted there); neither reads
context.js.

Co-authored-by: ump45nose <52391318+ump45nose@users.noreply.github.com>
This commit is contained in:
yetone
2026-10-13 06:18:27 +08:00
co-authored by ump45nose
parent 9f54b6428e
commit f2c30a8859
2 changed files with 41 additions and 10 deletions
+18 -4
View File
@@ -975,15 +975,29 @@
: t("{value}: {tokens} more tokens over the range (+{pct})", { value: show(p.value), tokens: fmtK(extra(p)), pct: pct(extra(p) / low) });
plot.append(dot);
};
if (worst !== now && worst !== top) at(worst, "worst", "+" + pct(extra(worst) / low));
at(top, "best", t("Leanest"));
if (now && now !== top) at(now, "now", "+" + pct(extra(now) / low));
const axis = el("div", "ctx-tc-x");
// the advice's value first, then the setting now, then the range's two
// ends: a value that sits too close to one before it is left off, so two
// labels are never drawn over each other (#1549, Ti800: 170K, the
// leanest, one step from the axis's 160K, read as "1170K")
const along = (p) => pts.indexOf(p) / (pts.length - 1);
// the tags over the dots the same way: "Leanest" first, then the setting
// now, then the costliest; one beside a tag that matters more and at
// about its height keeps its dot and tooltip but not its tag, so a
// setting one step from the advice doesn't print its "+3%" over
// "Leanest" (#1549)
const tagged = [];
const tag = (p, label) => {
if (tagged.some((k) => Math.abs(along(k) - along(p)) < TICK_GAP && Math.abs(Y(k) - Y(p)) < 20)) return "";
tagged.push(p);
return label;
};
const bestTag = tag(top, t("Leanest"));
const nowTag = now && now !== top ? tag(now, "+" + pct(extra(now) / low)) : "";
const worstTag = worst !== now && worst !== top ? tag(worst, "+" + pct(extra(worst) / low)) : "";
if (worst !== now && worst !== top) at(worst, "worst", worstTag);
at(top, "best", bestTag);
if (now && now !== top) at(now, "now", nowTag);
const axis = el("div", "ctx-tc-x");
const kept = [];
for (const p of new Set([top, now, pts[0], pts[pts.length - 1]].filter(Boolean))) {
if (kept.every((k) => Math.abs(along(k) - along(p)) >= TICK_GAP)) kept.push(p);
+23 -6
View File
@@ -283,7 +283,7 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium",
// drawn over each other (#1549, Ti800: the leanest 170K one step from the
// axis's 160K read as "1170K"), at a phone's width and a desk's
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
test(`${engine}: the curve's axis labels never overlap`, async (t) => {
test(`${engine}: the curve's axis labels and tags never overlap`, async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(() => browser.close());
const close = {
@@ -293,19 +293,24 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium",
compact: { model: "claude-opus-5-5", current: 400000, best: 170000, cost: 1.9e9, low: 1.46e9, curve: curve(160000, 1000000, 170000), facts } },
{ knob: "compact", model: "claude-sonnet-5", setting: {},
compact: { model: "claude-sonnet-5", current: 990000, best: 500000, cost: 1.9e9, low: 1.46e9, curve: curve(160000, 1000000, 500000), facts } },
// the setting now one step from the advice: its "+3%" tag would sit
// on "Leanest"
{ knob: "compact", model: "claude-haiku-4-5", setting: {},
compact: { model: "claude-haiku-4-5", current: 180000, best: 170000, cost: 1.9e9, low: 1.46e9, curve: curve(160000, 1000000, 170000), facts } },
] }],
};
for (const [width, height] of [[360, 760], [1100, 760]]) {
// German's "Sparsamste" is the widest "Leanest"
for (const lang of ["en", "de"]) for (const [width, height] of [[360, 760], [1100, 760]]) {
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);
const errors = [];
page.on("pageerror", (e) => errors.push(e.message));
const base = serve("en", []);
const base = serve(lang, []);
await page.route("**/*", (r) => new URL(r.request().url()).pathname === "/api/tune" ? r.fulfill({ json: close }) : base(r));
await page.goto("http://magpie.test/?view=usage");
await page.locator(".ctx-tune-chart").nth(1).waitFor();
await page.locator(".ctx-tune-chart").nth(2).waitFor();
const axes = await page.locator(".ctx-tc-x").evaluateAll((all) => all.map((a) => [...a.children].map((s) => {
const r = s.getBoundingClientRect();
return { text: s.textContent, cls: s.className, left: r.left, right: r.right };
@@ -316,9 +321,21 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium",
assert.ok(a.right <= b.left || b.right <= a.left, `${width}px: "${a.text}" (${Math.round(a.left)}–${Math.round(a.right)}) overlaps "${b.text}" (${Math.round(b.left)}–${Math.round(b.right)})`);
}
}
// nor the tags over the dots, and "Leanest" always keeps its own
const tags = await page.locator(".ctx-tune-chart").evaluateAll((all) => all.map((c) => [...c.querySelectorAll(".ctx-tc-tag")].map((s) => {
const r = s.getBoundingClientRect();
return { text: s.textContent, left: r.left, right: r.right, top: r.top, bottom: r.bottom };
})));
for (const list of tags) {
assert.ok(list.some((s) => s.text === { en: "Leanest", de: "Sparsamste" }[lang]), `${lang} ${width}px: no "Leanest" tag`);
for (let i = 0; i < list.length; i++) for (let j = i + 1; j < list.length; j++) {
const [a, b] = [list[i], list[j]];
assert.ok(a.right <= b.left || b.right <= a.left || a.bottom <= b.top || b.bottom <= a.top, `${lang} ${width}px: tag "${a.text}" overlaps "${b.text}"`);
}
}
// the advice's value and the one set now keep their labels
assert.deepEqual(axes[0].map((s) => s.text), ["170K", "400K", "1M"], width + "px");
assert.deepEqual(axes[1].map((s) => s.text), ["160K", "500K", "990K"], width + "px");
assert.deepEqual(axes[0].map((s) => s.text), ["170K", "400K", "1M"], lang + " " + width + "px");
assert.deepEqual(axes[1].map((s) => s.text), ["160K", "500K", "990K"], lang + " " + width + "px");
assert.deepEqual(errors, []);
await ctx.close();
}