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
@@ -13,6 +13,7 @@ import path from 'node:path';
import { fileURLToPath } from 'node:url';
import mysql from 'mysql2/promise';
import pg from 'pg';
import { resolveGooseSessionPgUrl } from '../goose-session-cost.mjs';
const root = path.join(path.dirname(fileURLToPath(import.meta.url)), '..');
@@ -33,9 +34,14 @@ loadEnvFile(path.join(root, '.env'));
const apply = process.argv.includes('--apply');
const sinceArg = process.argv.find((a) => a.startsWith('--since='));
const sinceDate = sinceArg ? sinceArg.slice('--since='.length) : '2026-08-04';
const startMs = new Date(`${sinceDate}T00:00:00+08:00`).getTime();
const DEDupe_NOTE_PREFIX = `补偿:Token估价超扣(${sinceDate}起)`;
const sinceRaw = sinceArg ? sinceArg.slice('--since='.length) : '2026-08-04';
const startMs = sinceRaw.includes('T')
? new Date(sinceRaw).getTime()
: new Date(`${sinceRaw}T00:00:00+08:00`).getTime();
const sinceLabel = sinceRaw.includes('T')
? sinceRaw.replace('T', ' ').replace('+08:00', ' CST')
: `${sinceRaw} 00:00 CST`;
const DEDupe_NOTE_PREFIX = `补偿:Token估价超扣(${sinceLabel}起)`;
function loadBillingConfig() {
const marginMultiplier = Number(process.env.H5_MARGIN_MULTIPLIER ?? 1);
@@ -56,12 +62,7 @@ function correctTotalCents(gooseCostUsd, config) {
}
function resolvePgUrl() {
if (process.env.GOOSE_SESSION_DB_URL) return process.env.GOOSE_SESSION_DB_URL;
const host = process.env.GOOSE_SESSION_PG_HOST ?? '127.0.0.1';
const port = process.env.GOOSE_SESSION_PG_PORT ?? '5432';
const db = process.env.GOOSE_SESSION_PG_DATABASE ?? 'memind_sessions';
const user = process.env.GOOSE_SESSION_PG_USER ?? 'john';
return `postgresql://${user}@${host}:${port}/${db}`;
return resolveGooseSessionPgUrl(process.env);
}
async function main() {
@@ -102,7 +103,7 @@ async function main() {
const sessionIds = [...bySession.keys()];
if (sessionIds.length === 0) {
console.log('No usage records since', sinceDate);
console.log('No usage records since', sinceLabel);
return;
}
@@ -150,7 +151,7 @@ async function main() {
const totalRefund = users.reduce((sum, user) => sum + user.refundCents, 0);
console.log('Billing config:', config);
console.log('Since:', sinceDate, `(${startMs})`);
console.log('Since:', sinceLabel, `(${startMs})`);
console.log('Affected users:', users.length);
console.log('Total refund:', `¥${(totalRefund / 100).toFixed(2)}`, `(${totalRefund} cents)`);
console.log('');