Fix duplicate session billing and align pricing with DeepSeek ×3.

Serialize billSessionUsage with row locks, expose rates on recharge, add compensation script and admin usage views.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
John
2026-06-16 03:10:05 -07:00
parent befeedfd37
commit 03690ee354
14 changed files with 472 additions and 52 deletions
+42 -1
View File
@@ -1,6 +1,10 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { computeDeltaCostCents, normalizeTokenState } from './billing.mjs';
import {
computeDeltaCostCents,
loadBillingConfig,
normalizeTokenState,
} from './billing.mjs';
const config = {
useBackendCost: false,
@@ -10,6 +14,43 @@ const config = {
minBillCents: 1,
};
test('loadBillingConfig defaults to DeepSeek Flash × 3 at USD/CNY 7.2', () => {
const savedInput = process.env.H5_BILL_INPUT_CENTS_PER_1K;
const savedOutput = process.env.H5_BILL_OUTPUT_CENTS_PER_1K;
const savedRate = process.env.H5_USD_CNY_RATE;
delete process.env.H5_BILL_INPUT_CENTS_PER_1K;
delete process.env.H5_BILL_OUTPUT_CENTS_PER_1K;
process.env.H5_USD_CNY_RATE = '7.2';
try {
const billing = loadBillingConfig();
assert.ok(Math.abs(billing.inputCentsPer1k - 0.3024) < 1e-9);
assert.ok(Math.abs(billing.outputCentsPer1k - 0.6048) < 1e-9);
} finally {
if (savedInput == null) delete process.env.H5_BILL_INPUT_CENTS_PER_1K;
else process.env.H5_BILL_INPUT_CENTS_PER_1K = savedInput;
if (savedOutput == null) delete process.env.H5_BILL_OUTPUT_CENTS_PER_1K;
else process.env.H5_BILL_OUTPUT_CENTS_PER_1K = savedOutput;
if (savedRate == null) delete process.env.H5_USD_CNY_RATE;
else process.env.H5_USD_CNY_RATE = savedRate;
}
});
test('computeDeltaCostCents bills DeepSeek Flash × 3 defaults with min charge', () => {
const billing = {
useBackendCost: false,
usdCnyRate: 7.2,
inputCentsPer1k: 0.3024,
outputCentsPer1k: 0.6048,
minBillCents: 1,
};
const previous = { lastInputTokens: 0, lastOutputTokens: 0 };
const current = normalizeTokenState({
accumulatedInputTokens: 1000,
accumulatedOutputTokens: 500,
});
assert.equal(computeDeltaCostCents(previous, current, billing), 1);
});
test('normalizeTokenState reads camelCase fields', () => {
const state = normalizeTokenState({
inputTokens: 10,