Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/billing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,21 @@ function nonNegNumStrict(v: unknown): number | undefined | typeof REJECT {

/** Module-level override table — set by loadOptions (startup + every config
* reload path funnels through it) and by nothing else. Keeps deep settle
* sites free of options plumbing, mirroring the registry costCache pattern. */
* sites free of options plumbing, mirroring the registry costCache pattern.
* `overrideCostRows` is the normalized `CostsShape` view of the same table,
* rebuilt ONCE per setter call (#7276): resolveBillingPrice is hot (per-request
* stamping + the pricing UI), and a per-call rebuild both re-converts every
* row and hands scanRegistryCostRow a fresh table identity — which defeats
* costLowerIndex's identity cache (registry.ts) and re-indexes the whole table
* on every lookup, even an exact override hit. Rebuilt unconditionally (even
* for a repeated object) so in-place mutation between sets can't leak stale
* rows; an empty/cleared set yields null so the registry fallback applies. */
let overridePrices: Record<string, BillingPrice> = {};
let overrideCostRows: CostsShape | null = null;

export function setBillingOverrides(prices: Record<string, BillingPrice> | undefined): void {
overridePrices = prices ?? {};
overrideCostRows = overrideCosts();
}

export function billingOverrides(): Record<string, BillingPrice> {
Expand All @@ -130,9 +140,8 @@ function overrideCosts(): CostsShape | null {

export function resolveBillingPrice(model: string | undefined, host?: string): ResolvedBillingPrice | undefined {
if (!model) return undefined;
const costs = overrideCosts();
if (costs !== null) {
const hit = scanRegistryCostRow(costs, model, host, { conflictHint: "billing.prices" });
if (overrideCostRows !== null) {
const hit = scanRegistryCostRow(overrideCostRows, model, host, { conflictHint: "billing.prices" });
if (hit) return finalize(hit.row, "config");
}
const row = peekRegistryCostRow(model, host);
Expand Down
53 changes: 53 additions & 0 deletions tests/billing-prices.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
setBillingOverrides,
stampBillingPrice,
stampedBillingPrice,
type BillingPrice,
} from "../src/billing.ts";
import { _setForTest as setRegistryForTest } from "../src/registry.ts";
import { _resetPluginStateForTest } from "../src/plugin.ts";
Expand Down Expand Up @@ -125,6 +126,58 @@ test("#4899 resolveBillingPrice: overrides win over the registry, matching model
assert.equal(resolveBillingPrice(""), undefined);
});

// ---------------------------------------------------------------------------
// #7276 — the normalized cost table is built once per setter call, not per
// lookup. Replacing / emptying / clearing overrides must immediately expose
// the new prices or the registry fallback (zero optional rates + case-folded
// names included); a reused input object must still refresh on re-set.
// ---------------------------------------------------------------------------

test("#7276 billing override reloads replace and clear normalized price rows", () => {
resetState();
setRegistryForTest({}, { "custom/model": { input: 2, output: 8 } });
setBillingOverrides({ "custom/model": { input: 3, output: 15, cacheRead: 0.3, cacheWrite: 3.75 } });
const initial = resolveBillingPrice("custom/model");
assert.deepEqual(initial, { input: 3, output: 15, cacheRead: 0.3, cacheWrite: 3.75, source: "config" });

// Replacing the table drops the old override and exposes the registry row.
setBillingOverrides({ "custom/other": { input: 4 } });
assert.deepEqual(resolveBillingPrice("custom/model"), { input: 2, output: 8, cacheRead: 2, cacheWrite: 2, source: "registry" });
assert.equal(resolveBillingPrice("custom/other")?.input, 4);
// A previously resolved price object is immutable — reloads never rewrite it.
assert.equal(initial?.input, 3, "reloading leaves previously resolved prices unchanged");

// Emptying the table clears overrides and falls back to the registry.
setBillingOverrides({});
assert.equal(resolveBillingPrice("custom/other"), undefined, "no override, no registry row: unpriced");
assert.equal(resolveBillingPrice("custom/model")?.source, "registry");
// Clearing (undefined) behaves the same as emptying.
setBillingOverrides({ "custom/model": { input: 6 } });
setBillingOverrides(undefined);
assert.equal(resolveBillingPrice("custom/model")?.input, 2, "cleared overrides fall back to the registry");
assert.deepEqual(billingOverrides(), {});
});

test("#7276 billing override reloads refresh a reused table and zero optional rates", () => {
resetState();
const prices: Record<string, BillingPrice> = { "custom/model": { input: 3 } };
setBillingOverrides(prices);
// Case-folded model name hits the override key; absent rates bill at input.
assert.deepEqual(resolveBillingPrice("CUSTOM/MODEL"), { input: 3, output: 3, cacheRead: 3, cacheWrite: 3, source: "config" });

// Re-setting the SAME object must rebuild the table (not reuse a stale
// identity): the mutated row + explicit zero rates are exposed immediately.
prices["custom/model"] = { input: 5, output: 0, cacheRead: 0, cacheWrite: 0 };
setBillingOverrides(prices);
assert.deepEqual(resolveBillingPrice("CUSTOM/MODEL"), { input: 5, output: 0, cacheRead: 0, cacheWrite: 0, source: "config" });
assert.equal(billingOverrides(), prices, "the raw table is the same object the setter was given");

// A further mutation + re-set refreshes again.
prices["custom/model"] = { input: 7 };
setBillingOverrides(prices);
assert.deepEqual(resolveBillingPrice("CUSTOM/MODEL"), { input: 7, output: 7, cacheRead: 7, cacheWrite: 7, source: "config" });
});

// ---------------------------------------------------------------------------
// Stamp + ledger math.
// ---------------------------------------------------------------------------
Expand Down
Loading