fix(billing): prefer Goose session cost over token estimate

Billing fell back to inflated token list-price estimates when Finish
frames carried inline accumulatedCost, causing ~70x overcharge vs DeepSeek.
Always fetch session accumulated_cost first; add Aug 4+ compensation script.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
john
2026-08-05 09:52:13 +08:00
parent 1e7004dffe
commit e7f0627dc9
3 changed files with 254 additions and 8 deletions
+26 -1
View File
@@ -23,7 +23,16 @@ test('estimateAccumulatedCostUsd uses DeepSeek-ish defaults', () => {
assert.equal(estimate, 0.281);
});
test('enrichTokenStateForBilling prefers upstream cost over estimate', () => {
test('enrichTokenStateForBilling prefers session cost over Finish inline cost', () => {
const enriched = enrichTokenStateForBilling(
{ accumulatedInputTokens: 1000, accumulatedOutputTokens: 100, accumulatedCost: 0.82 },
{ sessionCost: { accumulated_cost: 0.0067 } },
{ H5_USE_BACKEND_COST: '1' },
);
assert.equal(enriched.accumulatedCost, 0.0067);
});
test('enrichTokenStateForBilling keeps Finish inline cost when session cost missing', () => {
const enriched = enrichTokenStateForBilling(
{ accumulatedInputTokens: 1000, accumulatedOutputTokens: 100, accumulatedCost: 0.05 },
{},
@@ -78,6 +87,22 @@ test('resolveBillingTokenState fetches session before estimating', async () => {
assert.equal(resolved.accumulatedCost, 0.42);
});
test('resolveBillingTokenState replaces inflated Finish cost with session cost', async () => {
const resolved = await resolveBillingTokenState(
{
accumulatedInputTokens: 280030,
accumulatedOutputTokens: 5797,
accumulatedCost: 0.082,
},
{
sessionId: '20260804_19',
fetchSession: async () => ({ accumulated_cost: 0.006738208 }),
},
{ H5_USE_BACKEND_COST: '1' },
);
assert.equal(resolved.accumulatedCost, 0.006738208);
});
test('enriched cost drives 1.2x billing instead of flat fallback', () => {
const previous = { lastInputTokens: 224853, lastOutputTokens: 6685, lastAccumulatedCost: null };
const tokenState = enrichTokenStateForBilling(