fix(usage-aggregator): slice the 365-day history by the project filter

The headline is sliced to the requested projects (buildDurablePeriod runs
every historical cache day through sliceDayToProject) and today's days come
from the name-filtered parse, but `history.daily` was read straight out of
the day cache. Every surface built on it showed every project: the heatmap,
the streak, "N active days", month-to-date, the projected month, the daily
spend chart, the Spend punchcard and the daily-budget check.

On this machine, `status --format menubar-json --project=<one project>`
returned a $7.43 headline over a $2,993.61 month-to-date, and 137 of the
138 days in history.daily were byte-identical to the unfiltered run.

Same slice the headline already uses, so a cache day with no project split
contributes nothing here rather than everything, exactly as it does there.
This commit is contained in:
iamtoruk 2026-09-12 05:50:40 -07:00
parent 435a5c8d95
commit caae3df5cf
2 changed files with 130 additions and 1 deletions

View file

@ -1477,7 +1477,16 @@ export async function buildMenubarPayloadForRange(periodInfo: PeriodInfo, opts:
// a provider-filtered history without re-parsing. Tokens aren't broken down per provider
// in the cache, so the filtered view shows zero tokens (heatmap/trend still works on cost).
const historyStartStr = toDateString(new Date(now.getFullYear(), now.getMonth(), now.getDate() - BACKFILL_DAYS))
const allCacheDays = getDaysInRange(cache, historyStartStr, yesterdayStr)
// Under a project filter the headline is sliced by buildDurablePeriod while
// today's days come from the name-filtered parse, so without this the heatmap,
// streak and month-to-date beside that headline were every project's spend for
// all 364 days before today. Same slice the headline uses, same cost: a cache
// day with no project split contributes nothing rather than everything.
const rawCacheDays = getDaysInRange(cache, historyStartStr, yesterdayStr)
const historyFilter = makeProjectFilter(opts.project, opts.exclude)
const allCacheDays = (opts.project?.length ?? 0) > 0 || (opts.exclude?.length ?? 0) > 0
? rawCacheDays.map(day => sliceDayToProject(day, historyFilter))
: rawCacheDays
let dailyHistory
if (isClaudeConfigScoped && claudeConfigs?.selectedId) {