From 7db3446a094359231ba3894d95f332e4f352bbd3 Mon Sep 17 00:00:00 2001 From: Sai Shankar Date: Sun, 8 Mar 2026 21:00:46 +0530 Subject: [PATCH] fix(costs): align byAgent run filter to startedAt, tighten providerTabItems memo deps, stabilize byProject row keys --- server/src/services/costs.ts | 4 ++-- ui/src/pages/Costs.tsx | 15 +++++++++------ 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/server/src/services/costs.ts b/server/src/services/costs.ts index ea5fa8a8..4874fb84 100644 --- a/server/src/services/costs.ts +++ b/server/src/services/costs.ts @@ -121,8 +121,8 @@ export function costService(db: Db) { .orderBy(desc(sql`coalesce(sum(${costEvents.costCents}), 0)::int`)); const runConditions: ReturnType[] = [eq(heartbeatRuns.companyId, companyId)]; - if (range?.from) runConditions.push(gte(heartbeatRuns.finishedAt, range.from)); - if (range?.to) runConditions.push(lte(heartbeatRuns.finishedAt, range.to)); + if (range?.from) runConditions.push(gte(heartbeatRuns.startedAt, range.from)); + if (range?.to) runConditions.push(lte(heartbeatRuns.startedAt, range.to)); const runRows = await db .select({ diff --git a/ui/src/pages/Costs.tsx b/ui/src/pages/Costs.tsx index e63acc01..2b390a7d 100644 --- a/ui/src/pages/Costs.tsx +++ b/ui/src/pages/Costs.tsx @@ -259,11 +259,14 @@ export function Costs() { // totals are derived from byProvider (already memoized on providerData) so this memo // only rebuilds when the underlying data actually changes, not on every query refetch. ---------- const providerTabItems = useMemo(() => { - const allTokens = providers.reduce( + // derive provider keys inline so this memo only rebuilds when byProvider changes, + // not on the extra tick caused by the derived `providers` memo also changing. + const providerKeys = Array.from(byProvider.keys()); + const allTokens = providerKeys.reduce( (s, p) => s + (byProvider.get(p)?.reduce((a, r) => a + r.inputTokens + r.outputTokens, 0) ?? 0), 0, ); - const allCents = providers.reduce( + const allCents = providerKeys.reduce( (s, p) => s + (byProvider.get(p)?.reduce((a, r) => a + r.costCents, 0) ?? 0), 0, ); @@ -273,7 +276,7 @@ export function Costs() { label: ( All providers - {providers.length > 0 && ( + {providerKeys.length > 0 && ( <> {formatTokens(allTokens)} @@ -286,12 +289,12 @@ export function Costs() { ), }, - ...providers.map((p) => ({ + ...providerKeys.map((p) => ({ value: p, label: , })), ]; - }, [providers, byProvider]); + }, [byProvider]); // ---------- guard ---------- @@ -483,7 +486,7 @@ export function Costs() {
{spendData.byProject.map((row, i) => (