fix(ai-budgets): tighten rollover period filter to exclude stale entries
- Previous filter only checked e.recordedAt < currentPeriodStart - Now also checks e.recordedAt >= prevPeriodStart (lower bound) - Prevents entries from periods before the previous one from inflating the spent amount, which would reduce the rollover incorrectly - 12 ai-budgets tests passing
This commit is contained in:
parent
f3a4d915f5
commit
78cb958a6d
@ -330,13 +330,14 @@ export async function aiBudgetRoutes(app: FastifyInstance) {
|
||||
}
|
||||
const prevPeriodStart = toIsoDayBoundary(periodStart(prev, policy.period));
|
||||
|
||||
// Get spend from previous period
|
||||
// Get spend from previous period only (bounded to prev→current)
|
||||
const prevEntries = await repo.listSpendEntries(access.productId, {
|
||||
policyId: id,
|
||||
since: prevPeriodStart,
|
||||
});
|
||||
// Only count entries in previous period (before current period)
|
||||
const prevPeriodEntries = prevEntries.filter(e => e.recordedAt < currentPeriodStart);
|
||||
const prevPeriodEntries = prevEntries.filter(
|
||||
e => e.recordedAt >= prevPeriodStart && e.recordedAt < currentPeriodStart
|
||||
);
|
||||
const spentUsd = prevPeriodEntries.reduce((sum, e) => sum + e.costUsd, 0);
|
||||
const remainingUsd = Math.max(0, policy.budgetUsd - spentUsd);
|
||||
const rolledOverUsd = remainingUsd; // Full rollover — could add a cap later
|
||||
|
||||
Loading…
Reference in New Issue
Block a user