fix(billing): read Goose accumulated_cost from PostgreSQL fallback

Goosed GET /sessions/:id returns accumulated_cost null while memind_sessions
PG has the real upstream cost. Fall back to PG before token estimate billing.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
john
2026-08-05 15:48:39 +08:00
parent e2014e05e6
commit 8c1ae7550d
5 changed files with 166 additions and 24 deletions
+17
View File
@@ -103,6 +103,23 @@ test('resolveBillingTokenState replaces inflated Finish cost with session cost',
assert.equal(resolved.accumulatedCost, 0.006738208);
});
test('resolveBillingTokenState uses PG cost when goosed session API omits accumulated_cost', async () => {
const resolved = await resolveBillingTokenState(
{
accumulatedInputTokens: 280030,
accumulatedOutputTokens: 5797,
accumulatedCost: 0.082,
},
{
sessionId: '20260804_19',
fetchSession: async () => ({ id: '20260804_19', accumulated_cost: null }),
fetchSessionCostFromPg: async () => 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(