diff --git a/src/web/client.ts b/src/web/client.ts index a3feaca1e2..ee36194dd5 100644 --- a/src/web/client.ts +++ b/src/web/client.ts @@ -1056,7 +1056,7 @@ function changelogCatOf(rawLine) { // every priced session resolves identical rates; mixed fleets keep // plain ≈$ amounts (never a misleading single rate). const r6 = (x) => String(Math.round(x * 1e6) / 1e6); - const mra = o.moneyRates ? { w: r6(o.moneyRates.w), r: r6(o.moneyRates.r), q: r6(o.moneyRates.q) } : null; + const mra = o.moneyRates ? { w: r6(o.moneyRates.w), r: r6(o.moneyRates.r), q: r6(o.moneyRates.q), ...(o.moneyRates.i != null ? { i: r6(o.moneyRates.i) } : {}) } : null; $("st-gross-sub").textContent = t("ov.gross_note") + ((o.savedEstimated || 0) > 0 ? " · " + t("ov.saved_from_legacy", { n: fmtW(o.savedEstimated) }) : "") + (o.savedUsdGrossTotal ? " · " + (mra ? t("ov.gross_formula", { f: o.foldsTotal, g: fmtW(o.grossSavedTotal), r: mra.r, n: fmtUsd(o.savedUsdGrossTotal) }) : t("ov.money_eq", { n: fmtUsd(o.savedUsdGrossTotal) })) @@ -1110,7 +1110,7 @@ function changelogCatOf(rawLine) { const f = (v) => fmtW(v) + (mi > 0 ? " (−" + ((v / mi) * 100).toFixed(1) + "%)" : ""); hs.textContent = missSum > 0 ? t("ov.miss_split", { n: f(o.missNewTotal || 0), c: f(o.missCompTotal || 0), x: f(o.missTtlTotal || 0) }) : t("common.none"); if (o.missSplitUsdTotal) - hs.textContent += " · " + t("ov.miss_split_usd", { n: fmtUsd(o.missSplitUsdTotal.n), c: fmtUsd(o.missSplitUsdTotal.c), x: fmtUsd(o.missSplitUsdTotal.x) }) + (mra ? " " + t("ov.miss_rates", mra) : ""); + hs.textContent += " · " + t("ov.miss_split_usd", { n: fmtUsd(o.missSplitUsdTotal.n), c: fmtUsd(o.missSplitUsdTotal.c), x: fmtUsd(o.missSplitUsdTotal.x) }) + (mra && mra.i != null ? " " + t("ov.miss_rates", mra) : ""); } $("st-input").textContent = o.inputTokens ? fmtW(o.inputTokens) : t("common.none"); $("st-cached").textContent = o.cachedTokens ? fmtW(o.cachedTokens) : t("common.none"); @@ -1119,7 +1119,7 @@ function changelogCatOf(rawLine) { // reconcile with the 花费 card (i + r + w + o == costUsdTotal), so // every displayed token figure has a money face next to it. const cu = o.costUsdSplitTotal; - $("st-input-sub").textContent = cu ? t("ov.tier_unhit", { n: fmtUsd(cu.i) }) + (mra ? " · " + t("ov.at_input", { v: mra.w }) : "") : ""; + $("st-input-sub").textContent = cu ? t("ov.tier_unhit", { n: fmtUsd(cu.i) }) + (mra && mra.i != null ? " · " + t("ov.at_input", { v: mra.i }) : "") : ""; $("st-cached-sub").textContent = cu ? t("ov.money_eq", { n: fmtUsd(cu.r) }) + (mra ? " · " + t("ov.at_read", { v: mra.r }) : "") : ""; $("st-output-sub").textContent = cu ? t("ov.money_eq", { n: fmtUsd(cu.o) }) + (mra ? " · " + t("ov.at_output", { v: mra.q }) : "") : ""; const pb = $("protocol-body"); @@ -2726,9 +2726,9 @@ if (d.keepalive) kv(parts, t("det.keepalive"), d.keepalive.tierLabel + " · " + // #4899 v5: per-session rate args — show THE price inside each formula, // same rule as the overview (only when one price actually applies). const d6 = (x) => String(Math.round(x * 1e6) / 1e6); - const drates = d.rates ? { w: d6(d.rates.w), r: d6(d.rates.r), q: d6(d.rates.q) } : null; + const drates = d.rates ? { w: d6(d.rates.w), r: d6(d.rates.r), q: d6(d.rates.q), ...(d.rates.i != null ? { i: d6(d.rates.i) } : {}) } : null; const inSub = (d.costUsd ? t("ov.tier_unhit", { n: fmtUsd(d.costUsd.input) }) : "") - + (drates ? " · " + t("ov.at_input", { v: drates.w }) : "") + + (drates && drates.i != null ? " · " + t("ov.at_input", { v: drates.i }) : "") + (d.payback != null && d.savedUsdGross != null ? " · " + t("det.in_counterfactual", { n: fmtUsd(d.savedUsdGross) }) : ""); mini(parts, t("ov.input_tokens"), d.inputTokens ? fmtW(d.inputTokens) : null, false, inSub); mini(parts, t("ov.cached_tokens"), d.cachedTokens ? fmtW(d.cachedTokens) : null, false, @@ -2756,7 +2756,7 @@ if (d.keepalive) kv(parts, t("det.keepalive"), d.keepalive.tierLabel + " · " + // #4899: $ face under the missed-tokens split (current price table); tokens // only when no price resolves. const missLine = missArgs - ? t("det.miss_split_line", missArgs) + (d.missSplitUsd ? "
≈ " + escapeHtml(t("ov.miss_split_usd", { n: fmtUsd(d.missSplitUsd.n), c: fmtUsd(d.missSplitUsd.c), x: fmtUsd(d.missSplitUsd.x) })) + (drates ? " " + escapeHtml(t("ov.miss_rates", drates)) : "") : "") + ? t("det.miss_split_line", missArgs) + (d.missSplitUsd ? "
≈ " + escapeHtml(t("ov.miss_split_usd", { n: fmtUsd(d.missSplitUsd.n), c: fmtUsd(d.missSplitUsd.c), x: fmtUsd(d.missSplitUsd.x) })) + (drates && drates.i != null ? " " + escapeHtml(t("ov.miss_rates", drates)) : "") : "") : ""; mini(parts, t("det.hit_pct"), d.cacheHitPct == null ? null : d.cacheHitPct.toFixed(1) + "%", false, hitSub, missLine); mini(parts, t("ov.output_tokens"), d.outputTokens ? fmtW(d.outputTokens) : null, false, diff --git a/src/web/i18n.ts b/src/web/i18n.ts index 49c3cc65ac..25a4318204 100644 --- a/src/web/i18n.ts +++ b/src/web/i18n.ts @@ -102,8 +102,8 @@ export const MESSAGES: Record> = { "ov.tier_unhit": "其中未命中部分折合约 {n}", "ov.gross_formula": "共 {f} 次折叠,Σ(单次省 token × 其后请求数) = {g};按缓存读价 ${r}/Mtok 计折合约 {n}", "ov.net_rates": "(重建按缓存写价−读价差 ${w}−${r}、摘要按输出价 ${q}、抵扣按读价 ${r},$/Mtok)", - "ov.payback_rates": "(单价 $/Mtok:重建按 输入−读 ${w}−${r} · 摘要按输出 ${q} · 抵扣按读 ${r})", - "ov.miss_rates": "(新增内容@输入价 ${w} · 重建/回补@读价 ${r}/Mtok)", + "ov.payback_rates": "(单价 $/Mtok:重建按 写价−读价 ${w}−${r} · 摘要按输出 ${q} · 抵扣按读 ${r})", + "ov.miss_rates": "(新增内容@输入价 ${i} · 重建/回补@读价 ${r}/Mtok)", "ov.at_input": "输入价 ${v}/Mtok", "ov.at_read": "缓存读价 ${v}/Mtok", "ov.at_output": "输出价 ${v}/Mtok", @@ -772,9 +772,9 @@ export const MESSAGES: Record> = { "ov.payback_wait": "not yet (≈{k} more turns)", "ov.tier_unhit": "unhit portion worth ≈ {n}", "ov.gross_formula": "{f} folds in total, Σ(tokens saved per fold × later requests) = {g}; at cache-read ${r}/Mt that is ≈ {n}", - "ov.net_rates": "(replays at cache-write−read delta ${w}−${r}, summaries at output ${q}, credit at read ${r}; $/Mt)", - "ov.payback_rates": "(rates $/Mt: replay at input−read ${w}−${r} · summary at output ${q} · credit at read ${r})", - "ov.miss_rates": "(new content @ input ${w} · rebuild/repay @ read ${r}, $/Mt)", + "ov.net_rates": "(replays at write−read delta ${w}−${r}, summaries at output ${q}, credit at read ${r}; $/Mt)", + "ov.payback_rates": "(rates $/Mt: replay at write−read ${w}−${r} · summary at output ${q} · credit at read ${r})", + "ov.miss_rates": "(new content @ input ${i} · rebuild/repay @ read ${r}, $/Mt)", "ov.at_input": "input ${v}/Mt", "ov.at_read": "cache read ${v}/Mt", "ov.at_output": "output ${v}/Mt", diff --git a/src/web/sessions-data.ts b/src/web/sessions-data.ts index 276775a1c0..3fa2d5636a 100644 --- a/src/web/sessions-data.ts +++ b/src/web/sessions-data.ts @@ -147,8 +147,11 @@ interface WebSessionSummary { /** #4899 v4: the exact tier rates (USD/Mtok) this session's dollar figures * were computed with — live price tiers when resolvable, else the stamped * profile. Exposed so panel formula lines can print the prices themselves. - * Absent when no price resolves (tokens-only session). */ - rates?: { w: number; r: number; q: number }; + * Absent when no price resolves (tokens-only session). #4956: `w` follows + * the kernel fold contract (the cache-WRITE rate); `i` carries the INPUT + * rate separately for the spend/miss faces that bill new content at input + * (absent on stamped-only sessions, where no input-priced face exists). */ + rates?: { w: number; r: number; q: number; i?: number }; /** Σ T — measured compression re-pay tokens. */ repayCost?: number; /** Σ σ — summary generation cost (output tokens). */ @@ -205,8 +208,10 @@ interface WebOverview { foldsTotal: number; /** #4899 v4: single rate triple (USD/Mtok) when every priced session * resolves identical rates — lets formula lines name THE price. Absent - * when several models bill differently (totals stay per-session then). */ - moneyRates?: { w: number; r: number; q: number }; + * when several models bill differently (totals stay per-session then). + * #4956: `i` (input rate) is present only while every input-priced face + * in the fleet agrees on one value. */ + moneyRates?: { w: number; r: number; q: number; i?: number }; /** #4899: Σ money-mirrored net savings (only priced sessions contribute). */ savedUsdTotal: number; /** #4899 v2: Σ money-mirrored GROSS savings (avoided turns @ read rate). @@ -664,12 +669,16 @@ function livePriceForSummary(s: SummarySource): LivePrice | undefined { } /** Absolute $/Mtok profile {w,r,q} for the fold economics mirror, newest - * source first: (1) the live-resolved tier prices (input / cacheRead / + * source first: (1) the live-resolved tier prices (cacheWrite / cacheRead / * output) — always fresh, so price edits propagate; (2) a stamped ABSOLUTE * profile (model no longer in any price table); (3) legacy dimensionless * ratio profiles anchored against the stamped billing price (kernel defaults * 1/0.1/4 fill partial profiles, as before). Nothing usable → undefined: - * tokens only, never fabricated dollars. */ + * tokens only, never fabricated dollars. #4956: w is the cache-WRITE rate on + * every branch (kernel fold contract: (w−r)·T re-uploads at the write + * premium) — previously the live branch fed it the INPUT rate, overstating + * savings ~28% for write-premium providers and jumping when a model left the + * table (the stamped branch already carried the write rate). */ function savedUsdProfileForSummary(s: SummarySource, live?: LivePrice): { w: number; r: number; q: number } | undefined { if (live !== undefined) return { w: live.cacheWrite, r: live.cacheRead, q: live.output }; const o = s.metadata["cachePriceProfile"]; @@ -913,13 +922,13 @@ function summaryOf(s: SummarySource, live: boolean): WebSessionSummary { ? { n: ((agg?.agg?.nc ?? 0) * livePrice.input) / 1e6, c: ((agg?.agg?.cr ?? 0) * livePrice.cacheRead) / 1e6, x: ((agg?.agg?.tr ?? 0) * livePrice.cacheRead) / 1e6 } : undefined; // #4899 v4: publish the unit rates this session's dollars were computed - // with so the panel can print them INSIDE the formulas (fold math uses - // usdProf; spend/miss mirrors use livePrice — same tiers whenever both). + // with so the panel can print them INSIDE the formulas. #4956: the fold + // formula's w is the cache-WRITE rate; the input rate rides along as `i` + // for the spend/miss faces that bill new content at input (live-only — + // stamped sessions have no input-priced face, so no i). const rates = usdProf !== undefined - ? { w: usdProf.w, r: usdProf.r, q: usdProf.q } - : livePrice !== undefined - ? { w: livePrice.cacheWrite, r: livePrice.cacheRead, q: livePrice.output } - : undefined; + ? { w: usdProf.w, r: usdProf.r, q: usdProf.q, ...(livePrice !== undefined ? { i: livePrice.input } : {}) } + : undefined; // Untitled sessions: fall back to the first compression block's topic/summary lead. let firstBlockHint = ""; const fb = s.state.blocks.find((b) => b.topic || b.summary); @@ -1135,7 +1144,7 @@ export async function buildOverview(): Promise { let costUsdTotal = 0, savedUsdTotal = 0, savedUsdGrossTotal = 0, hasMoneyData = false; let cuI = 0, cuR = 0, cuW = 0, cuO = 0; // #4899 v4: fold count + fleet-wide rate uniformity (see WebOverview.rates). - let foldsTotal = 0, mrSeen = false, mrMulti = false, mW = 0, mR = 0, mQ = 0; + let foldsTotal = 0, mrSeen = false, mrMulti = false, mW = 0, mR = 0, mQ = 0, mISeen = false, mIMulti = false, mI = 0; let missUsdN = 0, missUsdC = 0, missUsdX = 0, hasMissUsd = false; let coverageLostFoldTotal = 0, coverageLostFrozenTotal = 0; let pbSpent = 0, pbSaved = 0, pbTurns = 0, hasPayback = false; @@ -1189,6 +1198,12 @@ export async function buildOverview(): Promise { mR = s.rates.r; mQ = s.rates.q; } else if (s.rates.w !== mW || s.rates.r !== mR || s.rates.q !== mQ) mrMulti = true; + // #4956: i is optional (stamped-only sessions carry none) — only + // present values constrain fleet-wide uniformity. + const si = s.rates.i; + if (si !== undefined) { + if (!mISeen) { mISeen = true; mI = si; } else if (si !== mI) mIMulti = true; + } } if (s.savedUsd !== undefined) { hasMoneyData = true; @@ -1245,7 +1260,7 @@ export async function buildOverview(): Promise { savedUsdTotal, savedUsdGrossTotal, foldsTotal, - ...(mrSeen && !mrMulti ? { moneyRates: { w: mW, r: mR, q: mQ } } : {}), + ...(mrSeen && !mrMulti ? { moneyRates: { w: mW, r: mR, q: mQ, ...(mISeen && !mIMulti ? { i: mI } : {}) } } : {}), ...(hasPayback ? { paybackUsdTotal: { spent: Math.round(pbSpent * 1e4) / 1e4, saved: Math.round(pbSaved * 1e4) / 1e4, turns: pbTurns } } : {}), diff --git a/tests/billing-web.test.ts b/tests/billing-web.test.ts index 3c9a05a01b..a9b3707b15 100644 --- a/tests/billing-web.test.ts +++ b/tests/billing-web.test.ts @@ -238,12 +238,12 @@ test("#4899 web summaries: sessions carry costUsd/savedUsd/model; overview total assert.equal(row1!.model, "claude-bill", "model id surfaced for the pricing editor"); assert.ok(Math.abs(row1!.costUsd!.total - (0.003 + 0.00012 + 0.000375 + 0.03)) < 1e-12, "spend total sums the ledger"); // #4899 follow-up (live pricing): the panel prices folds at the CURRENT - // price table — live registry tiers {input:3, cacheRead:0.3, - // cacheWrite:3.75, output:15} override the stamped {w:3.75,…} profile. - // Saved mirrors the kernel fold formula: (S−σ)·ra·r − [(w−r)·T + q·σ − r·S], - // w = the CACHE-WRITE tier (#4956: fold replay re-uploads at the write - // premium — same contract as kernel/src/cache-report.ts/acp_cache), - // rounded to 4 decimals at the summary boundary (display precision). + // price table — live registry tiers {input:3, cacheWrite:3.75, cacheRead:0.3, + // output:15} override the stamped {w:3.75,…} profile. Saved mirrors the + // kernel fold formula: (S−σ)·ra·r − [(w−r)·T + q·σ − r·S], rounded to 4 + // decimals at the summary boundary (display precision). #4956: w is the + // cache-WRITE tier (kernel contract), so the replay term is (3.75−0.3)·T, + // not (3−0.3)·T. const perTurn = (50000 - 5000) * 10 * 0.3; // 135000 const oneTime = (3.75 - 0.3) * 3000 + 15 * 5000 - 0.3 * 50000; // 70350 const expectSaved = Math.round(((perTurn - oneTime) / 1e6) * 1e4) / 1e4; // 0.0647 @@ -254,7 +254,7 @@ test("#4899 web summaries: sessions carry costUsd/savedUsd/model; overview total const pb1 = row1!.payback; assert.ok(pb1 !== undefined, "priced fold session exposes payback"); const r4 = (x: number) => Math.round(x * 1e4) / 1e4; - const expectReplay1 = r4(((3.75 - 0.3) * 3000) / 1e6); + const expectReplay1 = r4(((3.75 - 0.3) * 3000) / 1e6); // #4956: write-tier replay const expectSigma1 = r4((15 * 5000) / 1e6); const expectCredit1 = r4((0.3 * 50000) / 1e6); const expectSpent1 = r4(expectReplay1 + expectSigma1 - expectCredit1); @@ -289,9 +289,13 @@ test("#4899 web summaries: sessions carry costUsd/savedUsd/model; overview total const pb5 = row5!.payback; assert.ok(pb5 !== undefined, "anchored session exposes payback too"); // v5: each priced session carries the exact tier rates its faces used - // (live table {w:3.75, r:0.3, q:15} beats the stamp; anchored ratio keeps - // q=12). #4956: w is the cacheWrite tier, NOT input — positive pin. - assert.deepEqual(row1!.rates, { w: 3.75, r: 0.3, q: 15 }, "live tier rates exposed"); + // (live table {w:3.75,r:0.3,q:15} beats the stamp; anchored ratio keeps q=12). + // #4956 gate, positive leg (#3617 both directions): the live path prices + // the fold replay at the cache-WRITE tier — this fixture row carries + // cache_write 3.75 ≠ input 3, so w must be 3.75 and the input rate must + // ride along separately as i. The negative leg (row WITHOUT cache_write ⇒ + // w falls back to input) is pinned in the legacy suite's re-price row. + assert.deepEqual(row1!.rates, { w: 3.75, r: 0.3, q: 15, i: 3 }, "live tier rates exposed (write-tier w, separate input i)"); assert.ok(row5!.rates && Math.abs(row5!.rates!.w - 3) < 1e-9 && Math.abs(row5!.rates!.r - 0.3) < 1e-9 && Math.abs(row5!.rates!.q - 12) < 1e-9, `anchored ratio rates (kernel 0.1×input → r) ${JSON.stringify(row5!.rates)}`); const expectSpent5 = r4(r4(((3 - 0.3) * 3000) / 1e6) + r4((12 * 5000) / 1e6) - expectCredit1); @@ -353,8 +357,7 @@ test("#4899 legacy sessions: $ faces derive LIVE from the current price table (n assert.equal(row!.costUsd?.estimated, true, "live-derived spend is flagged estimated"); assert.ok(row!.costUsd && Math.abs(row!.costUsd.total - 1.8) < 1e-12, `spend ${row!.costUsd?.total}`); // Folds priced at the same live tiers: perTurn=(45000)·10·0.3=135000, - // oneTime=(3.75−0.3)·3000+15·5000−0.3·50000=70350 → 0.0647 (w = cacheWrite, - // #4956). + // oneTime=(3.75−0.3)·3000+15·5000−0.3·50000=70350 → 0.0647 (#4956 write-tier w). assert.ok(row!.savedUsd !== undefined && Math.abs(row!.savedUsd! - 0.0647) < 1e-9, `savedUsd ${row!.savedUsd}`); assert.ok(Math.abs((row!.savedUsdGross ?? 0) - 0.135) < 1e-9, `savedUsdGross ${row!.savedUsdGross}`); // #4899 v3: legacy sessions get the same payback view; output splits into @@ -371,7 +374,7 @@ test("#4899 legacy sessions: $ faces derive LIVE from the current price table (n assert.ok(cul && Math.abs(cul.i - 1.5) < 1e-9 && Math.abs(cul.r - 0.15) < 1e-9 && cul.w === 0 && Math.abs(cul.o - 0.15) < 1e-9, `legacy split ${JSON.stringify(cul)}`); assert.ok(ov.paybackUsdTotal && Math.abs(ov.paybackUsdTotal.turns - 10) < 1e-9 && ov.paybackUsdTotal.saved === r4(0.135 * 1), `legacy fleet payback turns=${ov.paybackUsdTotal?.turns} saved=${ov.paybackUsdTotal?.saved}`); // v5: a single-tier fleet prints its exact rates inside the formula lines. - assert.deepEqual(ov.moneyRates, { w: 3.75, r: 0.3, q: 15 }, "single-rate fleet exposes the triple"); + assert.deepEqual(ov.moneyRates, { w: 3.75, r: 0.3, q: 15, i: 3 }, "single-rate fleet exposes the triple + input leg"); assert.equal(ov.foldsTotal, 1); // Editing the price table re-prices immediately (nothing was persisted). @@ -379,12 +382,13 @@ test("#4899 legacy sessions: $ faces derive LIVE from the current price table (n const list2 = await buildSessionList(); const row2 = list2.find((s) => s.id === "web-legacy-1"); assert.ok(row2!.costUsd && Math.abs(row2!.costUsd.total - (3.0 + 0.15 + 0.15)) < 1e-12, `re-priced ${row2!.costUsd?.total}`); - // oneTime=(6−0.3)·3000+15·5000−0.3·50000=77100 → (135000−77100)/1e6 - // (#4956 negative direction: the override row carries NO cacheWrite, so - // w follows the cache_write ?? input fallback and stays at 6). + // oneTime=(6−0.3)·3000+15·5000−0.3·50000=77100 → (135000−77100)/1e6. assert.ok(row2!.savedUsd !== undefined && Math.abs(row2!.savedUsd! - 0.0579) < 1e-9, `re-priced savedUsd ${row2!.savedUsd}`); // v5: the exposed rates follow the edit too — nothing frozen at stamp time. - assert.deepEqual(row2!.rates, { w: 6, r: 0.3, q: 15 }, `re-priced rates ${JSON.stringify(row2!.rates)}`); + // #4956 gate, negative leg (#3617): this override row carries NO cache_write, + // so the write tier falls back to input — w must be 6 (== input), proving + // the positive leg above reads the cache_write tier and not a hardcoded one. + assert.deepEqual(row2!.rates, { w: 6, r: 0.3, q: 15, i: 6 }, `re-priced rates ${JSON.stringify(row2!.rates)}`); }); test("#4899 v3 payback: a fold whose one-time cost outruns its avoidance reads NOT-yet-paid-back", async () => { @@ -414,6 +418,7 @@ test("#4899 v3 payback: a fold whose one-time cost outruns its avoidance reads N assert.ok(ov.paybackUsdTotal !== undefined && ov.paybackUsdTotal.saved - ov.paybackUsdTotal.spent < 0, "fleet line shows a negative net while no session has paid back"); // v5: single-session fleet → fold count + rate triple both printable. + // (T=0 fold: the write-tier w never enters the arithmetic — replay term is zero.) assert.equal(ov.foldsTotal, 1); - assert.deepEqual(ov.moneyRates, { w: 3.75, r: 0.3, q: 15 }); + assert.deepEqual(ov.moneyRates, { w: 3.75, r: 0.3, q: 15, i: 3 }); });