mirror of
https://github.com/AgentSeal/codeburn.git
synced 2026-12-03 03:29:51 +00:00
* fix(optimize): scope transcript-derived findings to the selected provider
scanSessions() always ran discoverAllSessions('claude'), so every finding it
feeds was computed from Claude transcripts regardless of --provider, while the
header (sessions, calls, cost) came from the already-filtered projects. Under
--provider codex the two described different providers, and the Claude-derived
numbers read as the selected provider's.
Skip the scan when the filter excludes Claude, and skip the detectors it feeds
rather than handing them an empty scan: emptiness reads as "never invoked", so
an empty scan turned every skill, agent and command into a reported ghost.
Findings derived from projects (MCP tool coverage, capability reliability,
low-worth sessions, context bloat, outliers, model recommendations) already
filter correctly and still run.
The result cache key now carries the provider, since the provider decides
whether the scan runs at all.
* fix(optimize): thread the provider through the apply, aggregator and TUI scans
The previous commit fixed one of four scanAndDetect callers. The other three
carry a provider filter and dropped it:
- act/optimize-apply.ts: `optimize --apply` branches in main.ts before the
code that threads it, so a Codex-scoped run planned applies off Claude
findings. This is the worst of the three because `unused-skills` is
appliable and its plan moves directories out of ~/.claude/skills.
- usage-aggregator.ts: AggregateOpts.provider was already honoured for the
usage half but not for the optimize half, so the menubar, desktop and web
surfaces carried the same mismatch.
- dashboard.tsx: `p` cycles activeProvider and `o` opens optimize off the
same state, so the TUI could show Claude findings under a Codex view.
activeProvider joins the callback deps; reloadData already clears
optimizeResult on a provider switch, so no extra invalidation is needed.
Covered by a dry-run test on the apply path: under `provider: 'codex'` a fake
home holding an uninvoked skill must plan nothing, and under 'claude' the same
fixture must still plan the archive.
942 lines
47 KiB
TypeScript
942 lines
47 KiB
TypeScript
import { homedir } from 'node:os'
|
|
import { CATEGORY_LABELS, type ProjectSummary, type TaskCategory, type DateRange } from './types.js'
|
|
import { type PeriodData, type ProviderCost, type BreakdownArrays, type MenubarPayload, type ClaudeConfigSelector, buildMenubarPayload } from './menubar-json.js'
|
|
import { parseAllSessions, filterProjectsByName, filterProjectsByDays, filterProjectsByClaudeConfigSource, isSessionHydrationComplete } from './parser.js'
|
|
import { findUnpricedModels, getLocalModelSavingsConfigHash, getPriceOverridesConfigHash, getShortModelName, isExpectedFreeModel } from './models.js'
|
|
import { getAllProviders, safeDiscoverSessions } from './providers/index.js'
|
|
import { claude, getClaudeConfigDirs, getDesktopSessionsDirs } from './providers/claude.js'
|
|
import { stat } from 'node:fs/promises'
|
|
import { aggregateProjectsIntoDays, buildPeriodDataFromDays, dateKeyInTz } from './day-aggregator.js'
|
|
import { aggregateModelEfficiency } from './model-efficiency.js'
|
|
import { aggregateModels } from './models-report.js'
|
|
import { scanUserCorrections, medianTimeToFirstEditMs, aggregateFileChurn, computePricingCoverage } from './workflow-insights.js'
|
|
import { buildPrAttribution, aggregateByBranch } from './sessions-report.js'
|
|
import { scanAndDetect } from './optimize.js'
|
|
import { getDaysInRange, ensureCacheHydrated, emptyCache, BACKFILL_DAYS, toDateString, type DailyCache, type DailyEntry, type ProjectDayStats, type ProviderDaySlice } from './daily-cache.js'
|
|
import { buildGranularHistory } from './granular-history.js'
|
|
|
|
// Row caps for the by-PR / by-branch payload aggregations, ranked by cost.
|
|
const TOP_BRANCHES = 15
|
|
|
|
export function buildPeriodData(label: string, projects: ProjectSummary[]): PeriodData {
|
|
const sessions = projects.flatMap(p => p.sessions)
|
|
const catTotals: Record<string, { turns: number; cost: number; savingsUSD: number; editTurns: number; oneShotTurns: number }> = {}
|
|
const modelTotals: Record<string, { calls: number; cost: number; savingsUSD: number; estimatedCostUSD: number; tokens: number }> = {}
|
|
let inputTokens = 0, outputTokens = 0, cacheReadTokens = 0, cacheWriteTokens = 0
|
|
|
|
for (const sess of sessions) {
|
|
inputTokens += sess.totalInputTokens
|
|
outputTokens += sess.totalOutputTokens
|
|
cacheReadTokens += sess.totalCacheReadTokens
|
|
cacheWriteTokens += sess.totalCacheWriteTokens
|
|
for (const [cat, d] of Object.entries(sess.categoryBreakdown)) {
|
|
if (!catTotals[cat]) catTotals[cat] = { turns: 0, cost: 0, savingsUSD: 0, editTurns: 0, oneShotTurns: 0 }
|
|
catTotals[cat].turns += d.turns
|
|
catTotals[cat].cost += d.costUSD
|
|
catTotals[cat].savingsUSD += d.savingsUSD
|
|
catTotals[cat].editTurns += d.editTurns
|
|
catTotals[cat].oneShotTurns += d.oneShotTurns
|
|
}
|
|
for (const [model, d] of Object.entries(sess.modelBreakdown)) {
|
|
if (!modelTotals[model]) modelTotals[model] = { calls: 0, cost: 0, savingsUSD: 0, estimatedCostUSD: 0, tokens: 0 }
|
|
modelTotals[model].calls += d.calls
|
|
modelTotals[model].cost += d.costUSD
|
|
modelTotals[model].savingsUSD += d.savingsUSD
|
|
modelTotals[model].estimatedCostUSD += d.estimatedCostUSD ?? 0
|
|
modelTotals[model].tokens += d.tokens.inputTokens + d.tokens.outputTokens + d.tokens.cacheReadInputTokens + d.tokens.cacheCreationInputTokens
|
|
}
|
|
}
|
|
|
|
const unpricedModels = findUnpricedModels(Object.entries(modelTotals)
|
|
.map(([model, d]) => ({ model, calls: d.calls, cost: d.cost, tokens: d.tokens })))
|
|
const costBearingCalls = Object.entries(modelTotals)
|
|
.reduce((s, [model, d]) => s + (model === '<synthetic>' || isExpectedFreeModel(model) ? 0 : d.calls), 0)
|
|
const unpricedCalls = unpricedModels.reduce((s, m) => s + m.calls, 0)
|
|
const corrections = scanUserCorrections(projects)
|
|
|
|
return {
|
|
label,
|
|
cost: projects.reduce((s, p) => s + p.totalCostUSD, 0),
|
|
savingsUSD: projects.reduce((s, p) => s + p.totalSavingsUSD, 0),
|
|
estimatedCostUSD: projects.reduce((s, p) => s + (p.totalEstimatedCostUSD ?? 0), 0),
|
|
calls: projects.reduce((s, p) => s + p.totalApiCalls, 0),
|
|
sessions: projects.reduce((s, p) => s + p.sessions.length, 0),
|
|
inputTokens, outputTokens, cacheReadTokens, cacheWriteTokens,
|
|
categories: Object.entries(catTotals)
|
|
.sort(([, a], [, b]) => b.cost - a.cost)
|
|
.map(([cat, d]) => ({ name: CATEGORY_LABELS[cat as TaskCategory] ?? cat, ...d })),
|
|
models: Object.entries(modelTotals)
|
|
.sort(([, a], [, b]) => b.cost - a.cost)
|
|
.map(([name, d]) => ({ name, calls: d.calls, cost: d.cost, savingsUSD: d.savingsUSD, estimatedCostUSD: d.estimatedCostUSD })),
|
|
unpricedModels,
|
|
workflow: {
|
|
corrections: corrections.corrections,
|
|
correctionRate: corrections.correctionRate,
|
|
medianTimeToFirstEditMs: medianTimeToFirstEditMs(projects),
|
|
},
|
|
topReworkedFiles: aggregateFileChurn(projects),
|
|
pricingCoverage: computePricingCoverage(costBearingCalls, unpricedCalls),
|
|
}
|
|
}
|
|
|
|
export function getDailyCacheConfigHash(): string {
|
|
const savingsHash = getLocalModelSavingsConfigHash()
|
|
const overridesHash = getPriceOverridesConfigHash()
|
|
if (!overridesHash) return savingsHash
|
|
return `localModelSavings=${savingsHash}\u0002priceOverrides=${overridesHash}`
|
|
}
|
|
|
|
async function hydrateCache(): Promise<DailyCache> {
|
|
try {
|
|
return await ensureCacheHydrated(
|
|
(range) => parseAllSessions(range, 'all'),
|
|
aggregateProjectsIntoDays,
|
|
getDailyCacheConfigHash(),
|
|
// Never finalize the daily history off a partial (interrupted) session
|
|
// hydration — that is what froze empty older days into the chart.
|
|
isSessionHydrationComplete,
|
|
// On a tz-change re-derive the same parse is re-aggregated under the old
|
|
// tzKey so carried slices can be reduced by the turns that re-bucketed
|
|
// across local midnight (issue #770).
|
|
(projects, tz) => aggregateProjectsIntoDays(projects, (iso) => dateKeyInTz(iso, tz)),
|
|
)
|
|
} catch (err) {
|
|
// Previously swallowed silently, which turned any backfill failure into an
|
|
// empty trend/history with no signal (issue #441). Per-file parse errors no
|
|
// longer reach here (they're isolated in parseProviderSources), so anything
|
|
// that does is exceptional and worth surfacing.
|
|
process.stderr.write(
|
|
`codeburn: daily history backfill failed; the trend chart may be incomplete. ` +
|
|
`${err instanceof Error ? err.message : String(err)}\n`
|
|
)
|
|
return emptyCache()
|
|
}
|
|
}
|
|
|
|
export type PeriodInfo = { range: DateRange; label: string }
|
|
export type AggregateOpts = {
|
|
provider?: string
|
|
project?: string[]
|
|
exclude?: string[]
|
|
daysSelection?: { range: DateRange; label: string; days: Set<string> } | null
|
|
optimize?: boolean
|
|
claudeConfigSourceId?: string | null
|
|
/// Build the granular per-bucket timeline (`history.timeline`). Defaults to
|
|
/// true. The desktop app never renders it, so it passes `--no-timeline` to
|
|
/// skip the buildGranularHistory pass on every menubar poll.
|
|
timeline?: boolean
|
|
}
|
|
|
|
type ConfigOption = { id: string; label: string; path: string }
|
|
|
|
function buildSelector(byId: Map<string, ConfigOption>, selectedId?: string | null): ClaudeConfigSelector | undefined {
|
|
const options = [...byId.values()].sort((a, b) => a.label.localeCompare(b.label))
|
|
if (options.length <= 1) return undefined
|
|
const validSelectedId = selectedId && options.some(option => option.id === selectedId) ? selectedId : null
|
|
return { selectedId: validSelectedId, options }
|
|
}
|
|
|
|
// Complete option list including configs with NO data in the period (so the
|
|
// user can still switch to one to confirm it is $0). Only worth the extra
|
|
// Claude discovery walk when the user actually has multiple config dirs; a
|
|
// single-config user can never have a >1 selector, so skip it and let the
|
|
// project-derived path (which also surfaces a Claude Desktop bucket with data)
|
|
// stand.
|
|
async function claudeConfigSelector(projects: ProjectSummary[], selectedId?: string | null): Promise<ClaudeConfigSelector | undefined> {
|
|
const byId = new Map<string, ConfigOption>()
|
|
for (const session of projects.flatMap(project => project.sessions)) {
|
|
const source = session.source
|
|
if (source?.kind !== 'claude-config' && source?.kind !== 'claude-desktop') continue
|
|
if (!byId.has(source.id)) byId.set(source.id, { id: source.id, label: source.label, path: source.path })
|
|
}
|
|
// The discovery walk lists sources that have no data in the period (so an
|
|
// idle config or Claude Desktop is still selectable). Only worth it when a
|
|
// second source is possible: more than one config dir, or a Claude Desktop
|
|
// sessions dir exists. A plain single-config user skips it entirely.
|
|
const desktopExists = (
|
|
await Promise.all(
|
|
getDesktopSessionsDirs().map(dir => stat(dir).then(s => s.isDirectory()).catch(() => false)),
|
|
)
|
|
).some(Boolean)
|
|
if ((await getClaudeConfigDirs()).length > 1 || desktopExists) {
|
|
for (const source of await claude.discoverSessions()) {
|
|
if ((source.sourceKind !== 'claude-config' && source.sourceKind !== 'claude-desktop') || !source.sourceId || !source.sourceLabel || !source.sourcePath) continue
|
|
if (!byId.has(source.sourceId)) byId.set(source.sourceId, { id: source.sourceId, label: source.sourceLabel, path: source.sourcePath })
|
|
}
|
|
}
|
|
return buildSelector(byId, selectedId)
|
|
}
|
|
|
|
function dailyEntriesToHistory(days: ReturnType<typeof aggregateProjectsIntoDays>): MenubarPayload['history']['daily'] {
|
|
return days.map(d => {
|
|
const topModels = Object.entries(d.models)
|
|
.filter(([name]) => name !== '<synthetic>')
|
|
.sort(([, a], [, b]) => b.cost - a.cost)
|
|
.slice(0, 5)
|
|
.map(([name, m]) => ({
|
|
name,
|
|
cost: m.cost,
|
|
savingsUSD: m.savingsUSD,
|
|
calls: m.calls,
|
|
inputTokens: m.inputTokens,
|
|
outputTokens: m.outputTokens,
|
|
}))
|
|
return {
|
|
date: d.date,
|
|
cost: d.cost,
|
|
savingsUSD: d.savingsUSD,
|
|
calls: d.calls,
|
|
inputTokens: d.inputTokens,
|
|
outputTokens: d.outputTokens,
|
|
cacheReadTokens: d.cacheReadTokens,
|
|
cacheWriteTokens: d.cacheWriteTokens,
|
|
topModels,
|
|
}
|
|
})
|
|
}
|
|
|
|
/// Collapse a day to a single provider's slice, promoting the slice's totals to
|
|
/// the day-level fields buildPeriodDataFromDays reads. A day with no slice for
|
|
/// the provider becomes a zero day (so the date is still present but contributes
|
|
/// nothing). The `carried` flag is inherited so a per-provider total can still
|
|
/// account for expired-source days.
|
|
function sliceDayToProvider(day: DailyEntry, provider: string): DailyEntry {
|
|
const s = Object.hasOwn(day.providers, provider) ? day.providers[provider] : undefined
|
|
if (!s) {
|
|
return {
|
|
date: day.date, cost: 0, savingsUSD: 0, calls: 0, sessions: 0,
|
|
inputTokens: 0, outputTokens: 0, cacheReadTokens: 0, cacheWriteTokens: 0,
|
|
editTurns: 0, oneShotTurns: 0, models: {}, categories: {}, providers: {},
|
|
...(day.carried ? { carried: true as const } : {}),
|
|
}
|
|
}
|
|
return {
|
|
date: day.date,
|
|
cost: s.cost,
|
|
savingsUSD: s.savingsUSD ?? 0,
|
|
calls: s.calls,
|
|
sessions: s.sessions ?? 0,
|
|
inputTokens: s.inputTokens ?? 0,
|
|
outputTokens: s.outputTokens ?? 0,
|
|
cacheReadTokens: s.cacheReadTokens ?? 0,
|
|
cacheWriteTokens: s.cacheWriteTokens ?? 0,
|
|
editTurns: s.editTurns ?? 0,
|
|
oneShotTurns: s.oneShotTurns ?? 0,
|
|
models: s.models ?? {},
|
|
categories: s.categories ?? {},
|
|
providers: { [provider]: s },
|
|
...(s.projects ? { projects: s.projects } : {}),
|
|
...(day.carried ? { carried: true as const } : {}),
|
|
}
|
|
}
|
|
|
|
/// Does a cached day's project entry pass the active name filters? Mirrors
|
|
/// parser.filterProjectsByName exactly — case-insensitive substring match
|
|
/// against the project name OR its filesystem path, include first then exclude —
|
|
/// so a filter selects the same projects whether it is resolved against a fresh
|
|
/// parse or against the day cache. Patterns arrive pre-lowercased. `path` is
|
|
/// absent on entries whose sessions were gone before it could be recorded; the
|
|
/// name is then all there is to match on, as it is for the display layers.
|
|
function dayProjectMatches(name: string, path: string | undefined, include: string[], exclude: string[]): boolean {
|
|
const n = name.toLowerCase()
|
|
const p = (path ?? '').toLowerCase()
|
|
const hit = (pattern: string): boolean => n.includes(pattern) || (p !== '' && p.includes(pattern))
|
|
if (include.length > 0 && !include.some(hit)) return false
|
|
if (exclude.length > 0 && exclude.some(hit)) return false
|
|
return true
|
|
}
|
|
|
|
/// Sum the per-project day stats that pass the filters. `defineProperty` so a
|
|
/// project directory named "__proto__" stays an own key instead of mutating the
|
|
/// prototype link (same reason day-aggregator does it when writing them).
|
|
function sumMatchingProjects(
|
|
projects: Record<string, ProjectDayStats>,
|
|
include: string[],
|
|
exclude: string[],
|
|
): { cost: number; calls: number; savingsUSD: number; sessions: number; projects: Record<string, ProjectDayStats>; matched: number } {
|
|
const out = { cost: 0, calls: 0, savingsUSD: 0, sessions: 0, projects: {} as Record<string, ProjectDayStats>, matched: 0 }
|
|
for (const [name, p] of Object.entries(projects)) {
|
|
if (!dayProjectMatches(name, p.path, include, exclude)) continue
|
|
out.cost += p.cost
|
|
out.calls += p.calls
|
|
out.savingsUSD += p.savingsUSD ?? 0
|
|
out.sessions += p.sessions ?? 0
|
|
out.matched += 1
|
|
Object.defineProperty(out.projects, name, { value: p, enumerable: true, writable: true, configurable: true })
|
|
}
|
|
return out
|
|
}
|
|
|
|
/// Collapse a day to the slice matching the active --project/--exclude filters,
|
|
/// the project-level counterpart of sliceDayToProvider. Without this a
|
|
/// project-filtered headline counted every historical day WHOLE — excluded
|
|
/// projects included — while every detail panel (By Project / By Activity / By
|
|
/// Model, all built from the name-filtered live parse) left them out, so the two
|
|
/// could not be reconciled.
|
|
///
|
|
/// `DailyEntry.projects` (cache v15+) carries cost/calls/savingsUSD/sessions per
|
|
/// project, so those four are recomputed EXACTLY. The day's tokens, models and
|
|
/// categories have no per-project split to slice, so they are dropped here
|
|
/// rather than reported as if they belonged to the surviving projects;
|
|
/// buildDurablePeriod refills them from the project-filtered live parse, which
|
|
/// is exact for every session that still exists.
|
|
///
|
|
/// A day recorded before v15 has no `projects` at all and nothing can
|
|
/// reconstruct one once the sources are gone. Such a day cannot be attributed to
|
|
/// any project, so it contributes nothing to a project-filtered total and its
|
|
/// cost is surfaced as `unattributedCostUSD` instead of being silently folded in
|
|
/// (understating with a stated figure beats overstating with excluded spend).
|
|
function sliceDayToProject(day: DailyEntry, include: string[], exclude: string[]): DailyEntry {
|
|
const zeroDay = (): DailyEntry => ({
|
|
date: day.date, cost: 0, savingsUSD: 0, calls: 0, sessions: 0,
|
|
inputTokens: 0, outputTokens: 0, cacheReadTokens: 0, cacheWriteTokens: 0,
|
|
editTurns: 0, oneShotTurns: 0, models: {}, categories: {}, providers: {},
|
|
...(day.carried ? { carried: true as const } : {}),
|
|
})
|
|
if (!day.projects) return zeroDay()
|
|
const totals = sumMatchingProjects(day.projects, include, exclude)
|
|
if (totals.matched === 0) return zeroDay()
|
|
|
|
// Provider slices carry their own per-project split, so `--provider X` on top
|
|
// of a project filter stays consistent with the day-level slice. A slice
|
|
// adopted from a pre-v15 cache has no split and is dropped for the same
|
|
// reason the day-level one is.
|
|
const providers: Record<string, ProviderDaySlice> = {}
|
|
for (const [name, slice] of Object.entries(day.providers)) {
|
|
if (!slice.projects) continue
|
|
const sliced = sumMatchingProjects(slice.projects, include, exclude)
|
|
if (sliced.matched === 0) continue
|
|
Object.defineProperty(providers, name, {
|
|
value: { cost: sliced.cost, calls: sliced.calls, savingsUSD: sliced.savingsUSD, sessions: sliced.sessions, projects: sliced.projects },
|
|
enumerable: true, writable: true, configurable: true,
|
|
})
|
|
}
|
|
|
|
return {
|
|
date: day.date,
|
|
cost: totals.cost,
|
|
savingsUSD: totals.savingsUSD,
|
|
calls: totals.calls,
|
|
sessions: totals.sessions,
|
|
inputTokens: 0, outputTokens: 0, cacheReadTokens: 0, cacheWriteTokens: 0,
|
|
editTurns: 0, oneShotTurns: 0, models: {}, categories: {},
|
|
providers,
|
|
projects: totals.projects,
|
|
...(day.carried ? { carried: true as const } : {}),
|
|
}
|
|
}
|
|
|
|
/// The durable day set behind a period's headline: historical days from the
|
|
/// carry-forward cache (up to yesterday, INCLUDING days whose session files have
|
|
/// expired) unioned with today parsed live, then narrowed to the requested range
|
|
/// and (when given) the heatmap day selection. Identical construction to the
|
|
/// menubar's all-provider headline — this IS that construction, extracted.
|
|
///
|
|
/// `sliceHistorical` narrows the cache-sourced days only. Today's days come from
|
|
/// a parse the caller already name-filtered, so re-slicing them would be a no-op
|
|
/// at best and could only lose data the filter meant to keep.
|
|
function unionDaysForPeriod(
|
|
cache: DailyCache,
|
|
todayAllDays: DailyEntry[],
|
|
periodInfo: PeriodInfo,
|
|
daysSelection: Set<string> | null,
|
|
sliceHistorical?: (day: DailyEntry) => DailyEntry,
|
|
): DailyEntry[] {
|
|
const now = new Date()
|
|
const yesterdayStr = toDateString(new Date(now.getFullYear(), now.getMonth(), now.getDate() - 1))
|
|
const rangeStartStr = toDateString(periodInfo.range.start)
|
|
const rangeEndStr = toDateString(periodInfo.range.end)
|
|
const historicalRangeEndStr = rangeEndStr < yesterdayStr ? rangeEndStr : yesterdayStr
|
|
const cacheDays = rangeStartStr <= historicalRangeEndStr
|
|
? getDaysInRange(cache, rangeStartStr, historicalRangeEndStr)
|
|
: []
|
|
// Apply the day selection BEFORE slicing so a day the heatmap filtered out
|
|
// never reaches the slicer (which tallies what it could not attribute).
|
|
const selectedCacheDays = daysSelection ? cacheDays.filter(d => daysSelection.has(d.date)) : cacheDays
|
|
const historicalDays = sliceHistorical ? selectedCacheDays.map(d => sliceHistorical(d)) : selectedCacheDays
|
|
const todayInRange = todayAllDays.filter(d => d.date >= rangeStartStr && d.date <= rangeEndStr)
|
|
const unfiltered = [...historicalDays, ...todayInRange].sort((a, b) => a.date.localeCompare(b.date))
|
|
return daysSelection ? unfiltered.filter(d => daysSelection.has(d.date)) : unfiltered
|
|
}
|
|
|
|
/// The single durable-totals builder every CLI/TUI surface and the menubar share.
|
|
/// Headline totals (cost/calls/sessions/tokens/models/categories/savings) come
|
|
/// from the carry-forward daily cache unioned with today's live parse and sliced
|
|
/// to the requested provider, so a period that includes days whose session files
|
|
/// have expired still counts them — the invariant the menubar already relies on.
|
|
/// Detail-only fields that day entries can't carry (estimatedCost, unpriced
|
|
/// models, workflow intelligence, per-session drill-down) are enriched from a
|
|
/// fresh parse of the surviving sessions.
|
|
export type DurablePeriod = {
|
|
/// Durable headline totals for the period.
|
|
data: PeriodData
|
|
/// The exact provider-sliced, day-filtered day set behind `data`. Daily rows
|
|
/// rendered by report/overview come from here so they reconcile to `data`.
|
|
days: DailyEntry[]
|
|
/// Sum of `cost` on `carried` days included in the period (footnote source).
|
|
carriedCostUSD: number
|
|
/// Cost the active --project/--exclude filter had to set aside: cached days
|
|
/// recorded before per-project day stats existed (v15) carry no project split,
|
|
/// so they cannot be attributed to the filtered projects. Always 0 when no
|
|
/// project filter is active. Reported so a filtered total that is short of the
|
|
/// unfiltered one says so instead of just looking wrong.
|
|
unattributedCostUSD: number
|
|
/// Fresh per-period parse (provider + name filtered) for detail views that
|
|
/// still need surviving session files.
|
|
liveProjects: ProjectSummary[]
|
|
/// Hydrated all-provider cache (reused by the menubar's provider list + daily
|
|
/// history sections).
|
|
cache: DailyCache
|
|
/// Today-only slice, all providers, name-filtered (memo seed for the menubar).
|
|
todayAllDays: DailyEntry[]
|
|
/// The scan range the live parse covered (today-only when the period is today).
|
|
scanRange: DateRange
|
|
}
|
|
|
|
export async function buildDurablePeriod(periodInfo: PeriodInfo, opts: AggregateOpts = {}): Promise<DurablePeriod> {
|
|
const pf = opts.provider ?? 'all'
|
|
const daysSelection = opts.daysSelection ?? null
|
|
const fp = (p: ProjectSummary[]) => filterProjectsByName(p, opts.project ?? [], opts.exclude ?? [])
|
|
|
|
const now = new Date()
|
|
const todayStart = new Date(now.getFullYear(), now.getMonth(), now.getDate())
|
|
const todayRange: DateRange = { start: todayStart, end: now }
|
|
const todayStr = toDateString(todayStart)
|
|
const rangeStartStr = toDateString(periodInfo.range.start)
|
|
const rangeEndStr = toDateString(periodInfo.range.end)
|
|
const isTodayOnly = rangeStartStr === todayStr && rangeEndStr === todayStr
|
|
|
|
const cache = await hydrateCache()
|
|
|
|
// Today's live data always comes from an all-provider parse so the union (and
|
|
// any per-provider slice of it) sees every provider's today. `todayAllDays` is
|
|
// the today bucket only — the union filters the historical remainder out of the
|
|
// cache.
|
|
let liveProjects: ProjectSummary[]
|
|
let todayAllDays: DailyEntry[]
|
|
let scanRange: DateRange
|
|
if (pf === 'all') {
|
|
if (isTodayOnly) {
|
|
const raw = fp(await parseAllSessions(todayRange, 'all'))
|
|
liveProjects = raw
|
|
scanRange = todayRange
|
|
todayAllDays = aggregateProjectsIntoDays(raw).filter(d => d.date === todayStr)
|
|
} else {
|
|
const raw = fp(await parseAllSessions(periodInfo.range, 'all'))
|
|
liveProjects = daysSelection ? filterProjectsByDays(raw, daysSelection.days) : raw
|
|
scanRange = periodInfo.range
|
|
// A period that reaches today contains today's turns already, so derive the
|
|
// today slice from the same parse instead of scanning today again. Slice it
|
|
// to today first (filterProjectsByDays re-anchors a midnight-straddling
|
|
// turn to its surviving today calls), so today's category / turn count
|
|
// lands on today rather than staying anchored on the turn's yesterday
|
|
// start. Otherwise the post-midnight half vanishes from By Activity and the
|
|
// JSON daily turn count while the per-call cost/calls still bucket to today.
|
|
todayAllDays = rangeEndStr >= todayStr
|
|
? aggregateProjectsIntoDays(filterProjectsByDays(raw, new Set([todayStr]))).filter(d => d.date === todayStr)
|
|
: aggregateProjectsIntoDays(fp(await parseAllSessions(todayRange, 'all'))).filter(d => d.date === todayStr)
|
|
}
|
|
} else {
|
|
// Provider-filtered: today's all-provider parse feeds the union (sliced
|
|
// below); the provider-scoped parse feeds the detail/enrichment fields.
|
|
todayAllDays = aggregateProjectsIntoDays(fp(await parseAllSessions(todayRange, 'all'))).filter(d => d.date === todayStr)
|
|
const rawProv = fp(await parseAllSessions(isTodayOnly ? todayRange : periodInfo.range, pf))
|
|
liveProjects = daysSelection && !isTodayOnly ? filterProjectsByDays(rawProv, daysSelection.days) : rawProv
|
|
scanRange = isTodayOnly ? todayRange : periodInfo.range
|
|
}
|
|
|
|
// Name filters must reach the cache-sourced days too. Today's parse is already
|
|
// name-filtered above (`fp`), but the historical remainder comes straight out
|
|
// of the day cache, so without this slice a --project/--exclude headline
|
|
// counted every expired-source day whole while the detail panels did not.
|
|
const projectInclude = (opts.project ?? []).map(s => s.toLowerCase())
|
|
const projectExclude = (opts.exclude ?? []).map(s => s.toLowerCase())
|
|
const hasProjectFilter = projectInclude.length > 0 || projectExclude.length > 0
|
|
// What a filtered total cannot claim, and therefore has to leave out: a cached
|
|
// day with no project split at all, or — with a provider filter also active,
|
|
// since the headline then reads that provider's slice — a slice carried from a
|
|
// cache generation that predates per-project splits. Both are stated back to
|
|
// the caller (footnoted by the overview) instead of vanishing from the total.
|
|
const unattributableCost = (day: DailyEntry): number => {
|
|
if (pf === 'all') return day.projects ? 0 : day.cost
|
|
const slice = Object.hasOwn(day.providers, pf) ? day.providers[pf] : undefined
|
|
if (!slice) return 0
|
|
return !day.projects || !slice.projects ? slice.cost : 0
|
|
}
|
|
let unattributedCostUSD = 0
|
|
const sliceHistorical = hasProjectFilter
|
|
? (day: DailyEntry): DailyEntry => {
|
|
unattributedCostUSD += unattributableCost(day)
|
|
return sliceDayToProject(day, projectInclude, projectExclude)
|
|
}
|
|
: undefined
|
|
|
|
const allDays = unionDaysForPeriod(cache, todayAllDays, periodInfo, daysSelection?.days ?? null, sliceHistorical)
|
|
const days = pf === 'all' ? allDays : allDays.map(d => sliceDayToProvider(d, pf))
|
|
const data = buildPeriodDataFromDays(days, periodInfo.label)
|
|
|
|
// Enrich the cache-authoritative headline with fields DailyEntry cannot carry.
|
|
// These are all derivable only from surviving sessions (estimated-cost markers,
|
|
// unpriced-model detection, per-turn workflow intelligence), so they describe
|
|
// the live population, a subset of the carried headline.
|
|
const scanData = buildPeriodData(periodInfo.label, liveProjects)
|
|
data.estimatedCostUSD = scanData.estimatedCostUSD
|
|
data.unpricedModels = scanData.unpricedModels
|
|
data.workflow = scanData.workflow
|
|
data.topReworkedFiles = scanData.topReworkedFiles
|
|
data.pricingCoverage = scanData.pricingCoverage
|
|
// Cache buckets a session on its START day, the scan on any ACTIVE day; both
|
|
// are lower bounds of distinct sessions, so max is the tightest safe bound.
|
|
data.sessions = Math.max(data.sessions, scanData.sessions)
|
|
// Tokens/models/categories have no per-project split in the day cache, so
|
|
// sliceDayToProject drops them (see there). Under a project filter they come
|
|
// from the live parse instead: exact for the filtered projects, bounded by
|
|
// source retention like every other scan-derived field above, and consistent
|
|
// with the By Model / By Activity panels that read the same parse. Cost, calls,
|
|
// sessions and savings stay durable — sliced out of the cache, expired days
|
|
// included.
|
|
if (hasProjectFilter) {
|
|
data.inputTokens = scanData.inputTokens
|
|
data.outputTokens = scanData.outputTokens
|
|
data.cacheReadTokens = scanData.cacheReadTokens
|
|
data.cacheWriteTokens = scanData.cacheWriteTokens
|
|
data.models = scanData.models
|
|
data.categories = scanData.categories
|
|
}
|
|
const estimatedByModel = new Map(
|
|
scanData.models.filter(m => m.estimatedCostUSD != null).map(m => [m.name, m.estimatedCostUSD!]),
|
|
)
|
|
if (estimatedByModel.size > 0) {
|
|
data.models = data.models.map(m =>
|
|
estimatedByModel.has(m.name) ? { ...m, estimatedCostUSD: estimatedByModel.get(m.name) } : m,
|
|
)
|
|
}
|
|
|
|
const carriedCostUSD = days.reduce((s, d) => s + (d.carried ? d.cost : 0), 0)
|
|
return { data, days, carriedCostUSD, unattributedCostUSD, liveProjects, cache, todayAllDays, scanRange }
|
|
}
|
|
|
|
/**
|
|
* Resolved-range aggregation shared by `status --format menubar-json` and the MCP server.
|
|
* Pricing must already be loaded (callers run loadPricing first). When opts.optimize is
|
|
* false, the expensive scanAndDetect pass is skipped (retryTax/routingWaste still computed).
|
|
*/
|
|
export async function buildMenubarPayloadForRange(periodInfo: PeriodInfo, opts: AggregateOpts = {}): Promise<MenubarPayload> {
|
|
const pf = opts.provider ?? 'all'
|
|
const daysSelection = opts.daysSelection ?? null
|
|
const fp = (p: ProjectSummary[]) => filterProjectsByName(p, opts.project ?? [], opts.exclude ?? [])
|
|
|
|
const now = new Date()
|
|
const todayStart = new Date(now.getFullYear(), now.getMonth(), now.getDate())
|
|
const todayRange: DateRange = { start: todayStart, end: now }
|
|
const todayStr = toDateString(todayStart)
|
|
const yesterdayStr = toDateString(new Date(now.getFullYear(), now.getMonth(), now.getDate() - 1))
|
|
const rangeStartStr = toDateString(periodInfo.range.start)
|
|
const rangeEndStr = toDateString(periodInfo.range.end)
|
|
const historicalRangeEndStr = rangeEndStr < yesterdayStr ? rangeEndStr : yesterdayStr
|
|
const isAllProviders = pf === 'all'
|
|
|
|
let todayAllProjects: ProjectSummary[] | null = null
|
|
let todayAllDays: ReturnType<typeof aggregateProjectsIntoDays> | null = null
|
|
|
|
const getTodayAllProjects = async (): Promise<ProjectSummary[]> => {
|
|
if (!todayAllProjects) {
|
|
todayAllProjects = fp(await parseAllSessions(todayRange, 'all'))
|
|
}
|
|
return todayAllProjects
|
|
}
|
|
|
|
const getTodayAllDays = async (): Promise<ReturnType<typeof aggregateProjectsIntoDays>> => {
|
|
if (!todayAllDays) {
|
|
todayAllDays = aggregateProjectsIntoDays(await getTodayAllProjects())
|
|
}
|
|
return todayAllDays
|
|
}
|
|
|
|
// Assigned in every branch below (scoped-valid, or the !effectivelyScoped
|
|
// fallthrough); the `!` tells the compiler what the flag guarantees.
|
|
let currentData!: PeriodData
|
|
let scanProjects!: ProjectSummary[]
|
|
let scanRange!: DateRange
|
|
let cache: DailyCache = emptyCache()
|
|
/// The exact day set behind the all-provider headline (cache-backed
|
|
/// historical days + today's live days, day-filtered). Non-null only on the
|
|
/// unscoped all-provider path; it is the authority the projects view merges
|
|
/// from, so carried days count even after their session files are gone.
|
|
let cacheDaysForPeriod: DailyEntry[] | null = null
|
|
let claudeConfigs: ClaudeConfigSelector | undefined
|
|
const requestedClaudeConfigSourceId = opts.claudeConfigSourceId?.trim() || null
|
|
const isClaudeConfigScoped = requestedClaudeConfigSourceId !== null
|
|
|
|
let effectivelyScoped = false
|
|
if (isClaudeConfigScoped) {
|
|
// A config source scopes Claude usage only, so scan just Claude (main.ts
|
|
// rejects a contradictory non-Claude --provider). This also avoids parsing
|
|
// every other provider's corpus on each scoped refresh.
|
|
const rawProjects = fp(await parseAllSessions(periodInfo.range, 'claude'))
|
|
const fullProjects = daysSelection ? filterProjectsByDays(rawProjects, daysSelection.days) : rawProjects
|
|
claudeConfigs = await claudeConfigSelector(fullProjects, requestedClaudeConfigSourceId)
|
|
const selectedSourceId = claudeConfigs?.selectedId ?? null
|
|
if (selectedSourceId) {
|
|
effectivelyScoped = true
|
|
scanProjects = filterProjectsByClaudeConfigSource(fullProjects, selectedSourceId)
|
|
scanRange = periodInfo.range
|
|
currentData = buildPeriodData(periodInfo.label, scanProjects)
|
|
}
|
|
// A stale/invalid id does NOT validate: fall through to the normal path so
|
|
// an --provider all query returns real all-provider totals instead of the
|
|
// Claude-only scan. claudeConfigs (selectedId null) is kept so the selector
|
|
// still renders.
|
|
}
|
|
if (!effectivelyScoped) {
|
|
// Every non-scoped headline — all-provider AND provider-filtered — is built
|
|
// by the one shared durable-totals builder. It unions the carry-forward
|
|
// cache with today's live parse (slicing to the provider when filtered), so
|
|
// days whose session files have expired still count. The provider list and
|
|
// daily-history sections below reuse its cache + today slice.
|
|
const durable = await buildDurablePeriod(periodInfo, {
|
|
provider: pf,
|
|
project: opts.project,
|
|
exclude: opts.exclude,
|
|
daysSelection,
|
|
})
|
|
currentData = durable.data
|
|
scanProjects = durable.liveProjects
|
|
scanRange = durable.scanRange
|
|
cacheDaysForPeriod = durable.days
|
|
cache = durable.cache
|
|
todayAllDays = durable.todayAllDays
|
|
}
|
|
claudeConfigs = claudeConfigs ?? await claudeConfigSelector(scanProjects, null)
|
|
|
|
// Codex credits for the period. Reuses the models aggregation (folds reasoning
|
|
// into output, keeps non-cached input + cached-read separate) so the figure
|
|
// matches the official credit rates.
|
|
const modelRows = await aggregateModels(scanProjects)
|
|
currentData.codexCredits = modelRows.reduce(
|
|
(sum, r) => sum + (r.provider === 'codex' && r.credits != null ? r.credits : 0),
|
|
0,
|
|
)
|
|
|
|
// PROVIDERS
|
|
// For .all: enumerate every provider with cost across the period (from cache) + installed-but-zero.
|
|
// For specific: just this single provider with its scoped cost.
|
|
const allProviders = await getAllProviders()
|
|
const displayNameByName = new Map(allProviders.map(p => [p.name, p.displayName]))
|
|
const providers: ProviderCost[] = []
|
|
if (isClaudeConfigScoped) {
|
|
const providerTotals: Record<string, number> = {}
|
|
for (const d of aggregateProjectsIntoDays(scanProjects)) {
|
|
for (const [name, p] of Object.entries(d.providers)) {
|
|
providerTotals[name] = (providerTotals[name] ?? 0) + p.cost
|
|
}
|
|
}
|
|
for (const [name, cost] of Object.entries(providerTotals)) {
|
|
providers.push({ name, displayName: displayNameByName.get(name) ?? name, cost })
|
|
}
|
|
if (providers.length === 0 && claudeConfigs?.selectedId) {
|
|
providers.push({ name: 'claude', displayName: displayNameByName.get('claude') ?? 'Claude', cost: 0 })
|
|
}
|
|
} else if (isAllProviders) {
|
|
// Reuse the day set the headline was built from instead of rebuilding one
|
|
// straight out of the cache. The rebuilt version unioned unfiltered historical
|
|
// days with an already-filtered today, so per-provider costs counted every
|
|
// carried day whole while today honoured --project/--exclude, and the provider
|
|
// list could not be reconciled with the By Project panel (#865).
|
|
//
|
|
// durable.days is that same union, already narrowed by range, day selection and
|
|
// the project filter, which is what the comment above the buildDurablePeriod
|
|
// call already promised this section would use. Non-null here: this branch
|
|
// implies !isClaudeConfigScoped, which forces the !effectivelyScoped path that
|
|
// assigns it.
|
|
const allDaysForProviders = cacheDaysForPeriod ?? []
|
|
const providerTotals: Record<string, number> = {}
|
|
for (const d of allDaysForProviders) {
|
|
for (const [name, p] of Object.entries(d.providers)) {
|
|
providerTotals[name] = (providerTotals[name] ?? 0) + p.cost
|
|
}
|
|
}
|
|
for (const [name, cost] of Object.entries(providerTotals)) {
|
|
providers.push({ name, displayName: displayNameByName.get(name) ?? name, cost })
|
|
}
|
|
for (const p of allProviders) {
|
|
if (providers.some(pc => pc.name === p.name)) continue
|
|
const sources = await safeDiscoverSessions(p)
|
|
if (sources.length > 0) providers.push({ name: p.name, displayName: p.displayName, cost: 0 })
|
|
}
|
|
} else {
|
|
providers.push({ name: pf, displayName: displayNameByName.get(pf) ?? pf, cost: currentData.cost })
|
|
}
|
|
|
|
// DAILY HISTORY (last 365 days)
|
|
// Cache stores per-provider cost+calls per day in DailyEntry.providers, so we can derive
|
|
// 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)
|
|
|
|
let dailyHistory
|
|
if (isClaudeConfigScoped && claudeConfigs?.selectedId) {
|
|
const historyRange: DateRange = {
|
|
start: new Date(now.getFullYear(), now.getMonth(), now.getDate() - BACKFILL_DAYS),
|
|
end: now,
|
|
}
|
|
const historyProjects = filterProjectsByClaudeConfigSource(
|
|
fp(await parseAllSessions(historyRange, 'claude')),
|
|
claudeConfigs.selectedId,
|
|
)
|
|
dailyHistory = dailyEntriesToHistory(aggregateProjectsIntoDays(historyProjects))
|
|
} else if (isAllProviders) {
|
|
const todayDays = (await getTodayAllDays()).filter(d => d.date === todayStr)
|
|
const fullHistory = [...allCacheDays, ...todayDays]
|
|
dailyHistory = dailyEntriesToHistory(fullHistory)
|
|
} else {
|
|
const emptyModels = [] as { name: string; cost: number; savingsUSD: number; calls: number; inputTokens: number; outputTokens: number }[]
|
|
const historyFromCache = allCacheDays.map(d => {
|
|
const prov = d.providers[pf] ?? { calls: 0, cost: 0, savingsUSD: 0 }
|
|
return {
|
|
date: d.date,
|
|
cost: prov.cost,
|
|
savingsUSD: prov.savingsUSD,
|
|
calls: prov.calls,
|
|
inputTokens: 0,
|
|
outputTokens: 0,
|
|
cacheReadTokens: 0,
|
|
cacheWriteTokens: 0,
|
|
topModels: emptyModels,
|
|
}
|
|
})
|
|
const todayFromParse = aggregateProjectsIntoDays(scanProjects)
|
|
.filter(d => d.date === todayStr)
|
|
.map(d => {
|
|
const prov = d.providers[pf] ?? { calls: 0, cost: 0, savingsUSD: 0 }
|
|
return {
|
|
date: d.date,
|
|
cost: prov.cost,
|
|
savingsUSD: prov.savingsUSD,
|
|
calls: prov.calls,
|
|
inputTokens: 0,
|
|
outputTokens: 0,
|
|
cacheReadTokens: 0,
|
|
cacheWriteTokens: 0,
|
|
topModels: emptyModels,
|
|
}
|
|
})
|
|
dailyHistory = [...historyFromCache, ...todayFromParse]
|
|
}
|
|
|
|
const home = homedir()
|
|
const friendlyFromPath = (path: string | undefined, fallback: string): string => {
|
|
if (!path) return fallback
|
|
if (path === home || path === home + '/') return 'Home'
|
|
return path.split('/').filter(Boolean).pop() || fallback
|
|
}
|
|
const friendlyProject = (p: ProjectSummary) => friendlyFromPath(p.projectPath || p.project, p.project)
|
|
const sessionDetailsOf = (p: ProjectSummary) => [...p.sessions]
|
|
.sort((a, b) => b.totalCostUSD - a.totalCostUSD)
|
|
.slice(0, 10)
|
|
.map(s => ({
|
|
cost: s.totalCostUSD,
|
|
savingsUSD: s.totalSavingsUSD,
|
|
calls: s.apiCalls,
|
|
inputTokens: s.totalInputTokens,
|
|
outputTokens: s.totalOutputTokens,
|
|
date: s.firstTimestamp?.split('T')[0] ?? '',
|
|
models: Object.entries(s.modelBreakdown)
|
|
.map(([name, m]) => ({ name, cost: m.costUSD, savingsUSD: m.savingsUSD }))
|
|
.sort((a, b) => b.cost - a.cost)
|
|
.slice(0, 3),
|
|
}))
|
|
|
|
if (cacheDaysForPeriod !== null) {
|
|
// Project totals come from the SAME day set as the headline, so carried
|
|
// days count here too. The surviving-session parse contributes only what
|
|
// day entries cannot: the per-session drill-down and a fresher project
|
|
// path. Days recorded before the projects rollup existed have totals but
|
|
// no project split, so this list can sum to less than the headline — an
|
|
// honest gap, not a bug.
|
|
type CachedProjectTotal = { cost: number; savingsUSD: number; sessions: number; path?: string }
|
|
const cachedTotals = new Map<string, CachedProjectTotal>()
|
|
for (const d of cacheDaysForPeriod) {
|
|
for (const [name, p] of Object.entries(d.projects ?? {})) {
|
|
const acc = cachedTotals.get(name) ?? { cost: 0, savingsUSD: 0, sessions: 0 }
|
|
acc.cost += p.cost
|
|
acc.savingsUSD += p.savingsUSD
|
|
acc.sessions += p.sessions
|
|
if (!acc.path && p.path) acc.path = p.path
|
|
cachedTotals.set(name, acc)
|
|
}
|
|
}
|
|
const liveByName = new Map(scanProjects.map(p => [p.project, p]))
|
|
const names = new Set([...cachedTotals.keys(), ...liveByName.keys()])
|
|
currentData.projects = [...names].map(name => {
|
|
const cached = cachedTotals.get(name)
|
|
const live = liveByName.get(name)
|
|
return {
|
|
name: live ? friendlyProject(live) : friendlyFromPath(cached?.path, name),
|
|
cost: cached?.cost ?? live!.totalCostUSD,
|
|
savingsUSD: cached?.savingsUSD ?? live!.totalSavingsUSD,
|
|
// max for the same reason as the headline: start-day bucketing vs
|
|
// active-day counting, both lower bounds of distinct sessions.
|
|
sessions: Math.max(cached?.sessions ?? 0, live?.sessions.length ?? 0),
|
|
...(live ? { sessionDetails: sessionDetailsOf(live) } : {}),
|
|
}
|
|
}).sort((a, b) => b.cost - a.cost)
|
|
} else {
|
|
currentData.projects = scanProjects.map(p => ({
|
|
name: friendlyProject(p),
|
|
cost: p.totalCostUSD,
|
|
savingsUSD: p.totalSavingsUSD,
|
|
sessions: p.sessions.length,
|
|
sessionDetails: sessionDetailsOf(p),
|
|
}))
|
|
}
|
|
|
|
const effMap = aggregateModelEfficiency(scanProjects)
|
|
currentData.modelEfficiency = [...effMap.entries()].map(([name, eff]) => ({
|
|
name,
|
|
costPerEdit: eff.costPerEditUSD,
|
|
oneShotRate: eff.oneShotRate,
|
|
}))
|
|
|
|
const retryTaxByModel = [...effMap.values()]
|
|
.filter(m => m.retries > 0 && m.editTurns > 0)
|
|
.map(m => ({
|
|
name: m.model,
|
|
taxUSD: m.retries * (m.editCostUSD / m.editTurns),
|
|
retries: m.retries,
|
|
retriesPerEdit: m.retriesPerEdit,
|
|
}))
|
|
.sort((a, b) => b.taxUSD - a.taxUSD)
|
|
const retryTax = {
|
|
totalUSD: retryTaxByModel.reduce((s, m) => s + m.taxUSD, 0),
|
|
retries: retryTaxByModel.reduce((s, m) => s + m.retries, 0),
|
|
editTurns: [...effMap.values()].filter(m => m.retries > 0).reduce((s, m) => s + m.editTurns, 0),
|
|
byModel: retryTaxByModel.slice(0, 5),
|
|
}
|
|
|
|
currentData.topSessions = scanProjects.flatMap(p =>
|
|
p.sessions.map(s => ({
|
|
project: friendlyProject(p),
|
|
cost: s.totalCostUSD,
|
|
savingsUSD: s.totalSavingsUSD,
|
|
calls: s.apiCalls,
|
|
date: s.firstTimestamp?.split('T')[0] ?? '',
|
|
}))
|
|
).sort((a, b) => (b.cost + b.savingsUSD) - (a.cost + a.savingsUSD)).slice(0, 5)
|
|
|
|
// PULL REQUESTS + BRANCHES (all-provider path only). Both are session-layer
|
|
// aggregations over the surviving-session parse, so carried history cannot
|
|
// contribute — expected and fine. PR links and per-turn git branches are
|
|
// captured natively by Claude or correlated from any provider's saved session
|
|
// through explicit references, exact launcher prompts, or unambiguous cwds.
|
|
// Set only when non-empty so the payload omits them (and the app renders its
|
|
// quiet empty state) whenever there is nothing to show. Excluded on the
|
|
// Claude-config-scoped path (which replaces scanProjects with one config's
|
|
// sessions) so this stays the genuine unscoped all-provider aggregation.
|
|
if (isAllProviders && !effectivelyScoped) {
|
|
// One pass yields both rows and totals, so they never disagree.
|
|
const { rows: prRows, totals: prTotals } = buildPrAttribution(scanProjects)
|
|
if (prRows.length > 0) {
|
|
currentData.pullRequests = {
|
|
// PRs are user-auditable spend records, so never collapse the tail into
|
|
// an opaque "Other" bucket. The desktop list scrolls with the page.
|
|
rows: prRows,
|
|
distinctCost: prTotals.cost,
|
|
distinctSessions: prTotals.sessions,
|
|
attributedCost: prTotals.attributedCost,
|
|
unattributedCost: prTotals.unattributedCost,
|
|
...(prTotals.subagentSessions > 0 ? { subagentSessions: prTotals.subagentSessions } : {}),
|
|
}
|
|
}
|
|
const branchRows = aggregateByBranch(scanProjects)
|
|
if (branchRows.length > 0) currentData.byBranch = branchRows.slice(0, TOP_BRANCHES)
|
|
}
|
|
|
|
// Routing waste: find cheapest reliable model (≥90% 1-shot, ≥5 edits),
|
|
// then compute how much each pricier model overpaid.
|
|
const reliableModels = [...effMap.values()]
|
|
.filter(m => m.oneShotRate !== null && m.oneShotRate >= 90 && m.editTurns >= 5
|
|
&& (m.costPerEditUSD ?? 0) >= 0.01)
|
|
.sort((a, b) => (a.costPerEditUSD ?? Infinity) - (b.costPerEditUSD ?? Infinity))
|
|
const baseline = reliableModels[0]
|
|
const routingWasteByModel = baseline
|
|
? [...effMap.values()]
|
|
.filter(m => m.model !== baseline.model && m.editTurns > 0 && (m.costPerEditUSD ?? 0) > (baseline.costPerEditUSD ?? 0))
|
|
.map(m => {
|
|
const counterfactual = m.editTurns * (baseline.costPerEditUSD ?? 0)
|
|
return {
|
|
name: m.model,
|
|
costPerEdit: m.costPerEditUSD ?? 0,
|
|
editTurns: m.editTurns,
|
|
actualUSD: m.editCostUSD,
|
|
counterfactualUSD: counterfactual,
|
|
savingsUSD: m.editCostUSD - counterfactual,
|
|
}
|
|
})
|
|
.filter(m => m.savingsUSD > 0)
|
|
.sort((a, b) => b.savingsUSD - a.savingsUSD)
|
|
: []
|
|
const routingWaste = {
|
|
totalSavingsUSD: routingWasteByModel.reduce((s, m) => s + m.savingsUSD, 0),
|
|
baselineModel: baseline?.model ?? '',
|
|
baselineCostPerEdit: baseline?.costPerEditUSD ?? 0,
|
|
byModel: routingWasteByModel.slice(0, 5),
|
|
}
|
|
|
|
const breakdowns: BreakdownArrays = (() => {
|
|
const toolMap: Record<string, number> = {}
|
|
const skillMap: Record<string, { turns: number; cost: number }> = {}
|
|
const subagentMap: Record<string, { calls: number; cost: number }> = {}
|
|
const mcpMap: Record<string, number> = {}
|
|
// Local-model savings rollup: avoided spend (cost forced to $0, baseline
|
|
// recorded) grouped by model and provider. Mirrors the per-call savingsUSD
|
|
// that applyLocalModelSavings stamps in the parser.
|
|
const savingsByModel = new Map<string, { calls: number; actualUSD: number; savingsUSD: number; baselineModel: string; inputTokens: number; outputTokens: number }>()
|
|
const savingsByProvider = new Map<string, { calls: number; savingsUSD: number }>()
|
|
let totalSavings = 0
|
|
let totalSavingsCalls = 0
|
|
for (const p of scanProjects) for (const s of p.sessions) {
|
|
for (const [t, d] of Object.entries(s.toolBreakdown)) { if (!t.startsWith('lang:')) toolMap[t] = (toolMap[t] ?? 0) + d.calls }
|
|
for (const [sk, d] of Object.entries(s.skillBreakdown)) { const e = skillMap[sk] ?? { turns: 0, cost: 0 }; e.turns += d.turns; e.cost += d.costUSD; skillMap[sk] = e }
|
|
for (const [sa, d] of Object.entries(s.subagentBreakdown)) { const e = subagentMap[sa] ?? { calls: 0, cost: 0 }; e.calls += d.calls; e.cost += d.costUSD; subagentMap[sa] = e }
|
|
for (const [m, d] of Object.entries(s.mcpBreakdown)) { mcpMap[m] = (mcpMap[m] ?? 0) + d.calls }
|
|
for (const turn of s.turns) for (const call of turn.assistantCalls) {
|
|
if (!call.savingsUSD || call.savingsUSD <= 0) continue
|
|
totalSavings += call.savingsUSD
|
|
totalSavingsCalls += 1
|
|
const modelKey = getShortModelName(call.model)
|
|
const acc = savingsByModel.get(modelKey) ?? { calls: 0, actualUSD: 0, savingsUSD: 0, baselineModel: call.savingsBaselineModel ?? '', inputTokens: 0, outputTokens: 0 }
|
|
acc.calls += 1
|
|
acc.actualUSD += call.costUSD
|
|
acc.savingsUSD += call.savingsUSD
|
|
acc.baselineModel = acc.baselineModel || (call.savingsBaselineModel ?? '')
|
|
acc.inputTokens += call.usage.inputTokens
|
|
acc.outputTokens += call.usage.outputTokens
|
|
savingsByModel.set(modelKey, acc)
|
|
const provAcc = savingsByProvider.get(call.provider) ?? { calls: 0, savingsUSD: 0 }
|
|
provAcc.calls += 1
|
|
provAcc.savingsUSD += call.savingsUSD
|
|
savingsByProvider.set(call.provider, provAcc)
|
|
}
|
|
}
|
|
const localModelSavings = {
|
|
totalUSD: totalSavings,
|
|
calls: totalSavingsCalls,
|
|
byModel: Array.from(savingsByModel.entries()).sort(([, a], [, b]) => b.savingsUSD - a.savingsUSD).slice(0, 5).map(([name, d]) => ({ name, ...d })),
|
|
byProvider: Array.from(savingsByProvider.entries()).sort(([, a], [, b]) => b.savingsUSD - a.savingsUSD).slice(0, 5).map(([name, d]) => ({ name, ...d })),
|
|
}
|
|
return {
|
|
tools: Object.entries(toolMap).sort(([, a], [, b]) => b - a).slice(0, 10).map(([name, calls]) => ({ name, calls })),
|
|
skills: Object.entries(skillMap).sort(([, a], [, b]) => b.cost - a.cost).slice(0, 10).map(([name, d]) => ({ name, ...d })),
|
|
subagents: Object.entries(subagentMap).sort(([, a], [, b]) => b.cost - a.cost).slice(0, 10).map(([name, d]) => ({ name, ...d })),
|
|
mcpServers: Object.entries(mcpMap).sort(([, a], [, b]) => b - a).slice(0, 10).map(([name, calls]) => ({ name, calls })),
|
|
localModelSavings,
|
|
}
|
|
})()
|
|
|
|
const optimize = opts.optimize === false ? null : await scanAndDetect(scanProjects, scanRange, opts.provider)
|
|
const granularRange = opts.daysSelection?.range ?? scanRange
|
|
const granularHistory = opts.timeline === false ? undefined : buildGranularHistory(scanProjects, granularRange)
|
|
return buildMenubarPayload(currentData, providers, optimize, dailyHistory, retryTax, routingWaste, breakdowns, claudeConfigs, granularHistory)
|
|
}
|