feat(app): Optimize section — Waste/Reverts/Abandoned/Fixes tabs with dollar totals

Segment tabs carry their totals (Waste=optimize.savingsUSD, Reverts/Abandoned from
getYield summary, Fixes=findingCount). Waste findings from optimize.topFindings; reverts
and abandoned lists from yield.details by category. Evidence lines + copy-fix chips are
honestly omitted (not present in MenubarPayload.optimize) — a richer `optimize --format
json` emitter is flagged as a follow-up.

Implemented by Codex gpt-5.5 (high); committed by Fable (git blocked in Codex sandbox).
This commit is contained in:
iamtoruk 2026-07-10 18:33:54 -07:00
parent 5cd5c783a2
commit 39d2f79ef3
3 changed files with 311 additions and 0 deletions

View file

@ -8,6 +8,7 @@ import { Window } from './components/Window'
import { usePolled } from './hooks/usePolled'
import { codeburn } from './lib/ipc'
import { Overview } from './sections/Overview'
import { Optimize } from './sections/Optimize'
import { Spend } from './sections/Spend'
import type { MenubarPayload, Period } from './lib/types'
@ -71,6 +72,8 @@ export function App() {
<Overview period={period} provider={provider} />
) : section === 'spend' ? (
<Spend period={period} provider={provider} />
) : section === 'optimize' ? (
<Optimize period={period} provider={provider} />
) : (
<SectionPlaceholder title={SECTION_TITLES[section]} />
)}

View file

@ -0,0 +1,152 @@
// @vitest-environment jsdom
import { fireEvent, render, screen } from '@testing-library/react'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import type { MenubarPayload, YieldJsonReport } from '../lib/types'
import { Optimize } from './Optimize'
const { getOverview, getYield } = vi.hoisted(() => ({
getOverview: vi.fn<(period: string, provider: string) => Promise<MenubarPayload>>(),
getYield: vi.fn<(period: string) => Promise<YieldJsonReport>>(),
}))
vi.mock('../lib/ipc', async orig => {
const actual = await orig<typeof import('../lib/ipc')>()
return { ...actual, codeburn: { getOverview, getYield } }
})
function makePayload(): MenubarPayload {
return {
generated: '2026-07-10T19:00:00.000Z',
current: {
label: 'Last 30 days',
cost: 612.48,
calls: 1220,
sessions: 88,
oneShotRate: null,
inputTokens: 0,
outputTokens: 0,
cacheReadTokens: 0,
cacheWriteTokens: 0,
cacheHitPercent: 0,
codexCredits: 0,
topActivities: [],
topModels: [],
localModelSavings: { totalUSD: 0, calls: 0, byModel: [], byProvider: [] },
providers: {},
topProjects: [],
modelEfficiency: [],
topSessions: [],
retryTax: { totalUSD: 0, retries: 0, editTurns: 0, byModel: [] },
routingWaste: { totalSavingsUSD: 0, baselineModel: '', baselineCostPerEdit: 0, byModel: [] },
tools: [],
skills: [],
subagents: [],
mcpServers: [],
},
optimize: {
findingCount: 3,
savingsUSD: 94.4,
topFindings: [
{ title: 'Opus is doing your small talk', impact: 'high', savingsUSD: 9.1 },
{ title: 'Cache hit is low in agentseal-dash', impact: 'medium', savingsUSD: 8.7 },
],
},
history: { daily: [] },
}
}
function makeYield(): YieldJsonReport {
return {
period: { label: 'Last 30 days', start: '2026-06-11', end: '2026-07-10' },
summary: {
productive: { costUSD: 440, sessions: 19, costPercent: 72, sessionPercent: 70 },
reverted: { costUSD: 107, sessions: 4, costPercent: 17, sessionPercent: 15 },
abandoned: { costUSD: 65.4, sessions: 3, costPercent: 11, sessionPercent: 15 },
total: { costUSD: 612.4, sessions: 26 },
productiveToRevertedCostRatio: 4.1,
},
details: [
{ sessionId: 'rev-1', project: 'codeburn', category: 'reverted', commitCount: 2, costUSD: 55 },
{ sessionId: 'rev-2', project: 'agentseal-dash', category: 'reverted', commitCount: 1, costUSD: 52 },
{ sessionId: 'abn-1', project: 'sandbox-spike', category: 'abandoned', commitCount: 0, costUSD: 65.4 },
{ sessionId: 'prod-1', project: 'desktop-app', category: 'productive', commitCount: 5, costUSD: 440 },
],
}
}
function emptyPayload(): MenubarPayload {
const payload = makePayload()
payload.optimize = { findingCount: 0, savingsUSD: 0, topFindings: [] }
return payload
}
function emptyYield(): YieldJsonReport {
const report = makeYield()
report.summary.reverted = { costUSD: 0, sessions: 0, costPercent: 0, sessionPercent: 0 }
report.summary.abandoned = { costUSD: 0, sessions: 0, costPercent: 0, sessionPercent: 0 }
report.details = []
return report
}
describe('Optimize', () => {
beforeEach(() => {
getOverview.mockReset()
getYield.mockReset()
})
afterEach(() => {
vi.useRealTimers()
})
it('renders waste findings with savings and segment totals from overview and yield payloads', async () => {
getOverview.mockResolvedValue(makePayload())
getYield.mockResolvedValue(makeYield())
render(<Optimize period="30days" provider="all" />)
expect(await screen.findByText('Opus is doing your small talk')).toBeInTheDocument()
expect(screen.getByText('high impact')).toBeInTheDocument()
expect(screen.getByText('$9.10')).toBeInTheDocument()
expect(screen.getByText('Cache hit is low in agentseal-dash')).toBeInTheDocument()
expect(screen.getByRole('button', { name: 'Waste $94.40' })).toBeInTheDocument()
expect(screen.getByRole('button', { name: 'Reverts $107.00' })).toBeInTheDocument()
expect(screen.getByRole('button', { name: 'Abandoned $65.40' })).toBeInTheDocument()
expect(screen.getByRole('button', { name: 'Fixes 3' })).toBeInTheDocument()
})
it('switches to Reverts and shows only reverted yield details', async () => {
getOverview.mockResolvedValue(makePayload())
getYield.mockResolvedValue(makeYield())
render(<Optimize period="30days" provider="all" />)
expect(await screen.findByText('Opus is doing your small talk')).toBeInTheDocument()
fireEvent.click(screen.getByRole('button', { name: 'Reverts $107.00' }))
expect(screen.getByText('codeburn')).toBeInTheDocument()
expect(screen.getByText('2 commits · rev-1')).toBeInTheDocument()
expect(screen.getByText('$55.00')).toBeInTheDocument()
expect(screen.getByText('agentseal-dash')).toBeInTheDocument()
expect(screen.queryByText('sandbox-spike')).not.toBeInTheDocument()
expect(screen.queryByText('desktop-app')).not.toBeInTheDocument()
})
it('renders honest empty states for missing optimize findings and yield details', async () => {
getOverview.mockResolvedValue(emptyPayload())
getYield.mockResolvedValue(emptyYield())
render(<Optimize period="30days" provider="all" />)
expect(await screen.findByText('No waste findings in this range yet.')).toBeInTheDocument()
expect(screen.getByRole('button', { name: 'Waste $0.00' })).toBeInTheDocument()
expect(screen.getByRole('button', { name: 'Reverts $0.00' })).toBeInTheDocument()
expect(screen.getByRole('button', { name: 'Abandoned $0.00' })).toBeInTheDocument()
expect(screen.getByRole('button', { name: 'Fixes 0' })).toBeInTheDocument()
fireEvent.click(screen.getByRole('button', { name: 'Reverts $0.00' }))
expect(screen.getByText('No reverted sessions in this range yet.')).toBeInTheDocument()
fireEvent.click(screen.getByRole('button', { name: 'Abandoned $0.00' }))
expect(screen.getByText('No abandoned sessions in this range yet.')).toBeInTheDocument()
})
})

View file

@ -0,0 +1,156 @@
import { useState } from 'react'
import { Panel } from '../components/Panel'
import { SegTabs } from '../components/SegTabs'
import { usePolled } from '../hooks/usePolled'
import { codeburn } from '../lib/ipc'
import type { MenubarPayload, Period, SessionYieldJson, YieldJsonReport } from '../lib/types'
type OptimizeTab = 'waste' | 'reverts' | 'abandoned' | 'fixes'
function fmtUsd(n: number): string {
return `$${n.toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`
}
function EmptyNote({ children }: { children: React.ReactNode }) {
return <p style={{ color: 'var(--t3)', margin: 0, fontSize: 12 }}>{children}</p>
}
export function Optimize({ period, provider }: { period: Period; provider: string }) {
const overview = usePolled<MenubarPayload>(() => codeburn.getOverview(period, provider), [period, provider])
const yieldReport = usePolled<YieldJsonReport>(() => codeburn.getYield(period), [period])
const [tab, setTab] = useState<OptimizeTab>('waste')
if (!overview.data) {
if (overview.error?.kind === 'not-found') {
return (
<Panel title="Locate the codeburn CLI">
<p style={{ color: 'var(--t2)', margin: '0 0 6px', fontSize: 12.5 }}>
CodeBurn Desktop reads your usage by running the{' '}
<code style={{ fontFamily: 'var(--mono)', color: 'var(--lav)' }}>codeburn</code> command, but it isn&apos;t
on your PATH yet.
</p>
<p style={{ color: 'var(--t3)', margin: 0, fontSize: 11.5 }}>
Install it with <code style={{ fontFamily: 'var(--mono)', color: 'var(--lav)' }}>npm i -g codeburn</code>,
then reopen this window.
</p>
</Panel>
)
}
if (overview.error) {
return (
<Panel title="Couldn't read optimize">
<p style={{ color: 'var(--red)', margin: 0, fontSize: 12 }}>{overview.error.message}</p>
</Panel>
)
}
return (
<Panel title="Optimize">
<EmptyNote>Scanning optimize findings…</EmptyNote>
</Panel>
)
}
const revertedTotal = yieldReport.data?.summary.reverted.costUSD ?? 0
const abandonedTotal = yieldReport.data?.summary.abandoned.costUSD ?? 0
const options = [
{ value: 'waste', label: `Waste ${fmtUsd(overview.data.optimize.savingsUSD)}` },
{ value: 'reverts', label: `Reverts ${fmtUsd(revertedTotal)}` },
{ value: 'abandoned', label: `Abandoned ${fmtUsd(abandonedTotal)}` },
{ value: 'fixes', label: `Fixes ${overview.data.optimize.findingCount.toLocaleString('en-US')}` },
]
return (
<>
<SegTabs
options={options}
value={tab}
onChange={value => setTab(value as OptimizeTab)}
style={{ alignSelf: 'flex-start' }}
/>
<Panel>
{tab === 'waste' ? (
<WasteRows data={overview.data} />
) : tab === 'reverts' ? (
<YieldRows report={yieldReport} category="reverted" empty="No reverted sessions in this range yet." />
) : tab === 'abandoned' ? (
<YieldRows report={yieldReport} category="abandoned" empty="No abandoned sessions in this range yet." />
) : (
<FixesRows data={overview.data} />
)}
</Panel>
</>
)
}
function WasteRows({ data }: { data: MenubarPayload }) {
const findings = data.optimize.topFindings
if (!findings.length) return <EmptyNote>No waste findings in this range yet.</EmptyNote>
return (
<>
{findings.map((finding, i) => (
<div className="li" style={{ alignItems: 'flex-start' }} key={`${finding.title}-${i}`}>
<span className="no">{String(i + 1).padStart(2, '0')}</span>
<div className="lx">
<b>{finding.title}</b>
<span>{finding.impact} impact</span>
</div>
<span className="val ok">{fmtUsd(finding.savingsUSD)}</span>
</div>
))}
</>
)
}
function YieldRows({
report,
category,
empty,
}: {
report: ReturnType<typeof usePolled<YieldJsonReport>>
category: SessionYieldJson['category']
empty: string
}) {
if (!report.data) {
if (report.error) return <p style={{ color: 'var(--red)', margin: 0, fontSize: 12 }}>{report.error.message}</p>
return <EmptyNote>{report.loading ? 'Loading yield analysis…' : empty}</EmptyNote>
}
const rows = report.data.details.filter(row => row.category === category)
if (!rows.length) return <EmptyNote>{empty}</EmptyNote>
return (
<>
{rows.map((row, i) => (
<div className="li" style={{ alignItems: 'flex-start' }} key={row.sessionId}>
<span className="no">{String(i + 1).padStart(2, '0')}</span>
<div className="lx">
<b>{row.project}</b>
<span>
{row.commitCount.toLocaleString('en-US')} {row.commitCount === 1 ? 'commit' : 'commits'} · {row.sessionId}
</span>
</div>
<span className="val ok">{fmtUsd(row.costUSD)}</span>
</div>
))}
</>
)
}
function FixesRows({ data }: { data: MenubarPayload }) {
const count = data.optimize.findingCount
if (!count) return <EmptyNote>No fixes in this range yet.</EmptyNote>
return (
<div className="li" style={{ alignItems: 'flex-start' }}>
<span className="no">{String(count).padStart(2, '0')}</span>
<div className="lx">
<b>{count.toLocaleString('en-US')} fixes ready</b>
<span>{fmtUsd(data.optimize.savingsUSD)} recoverable waste</span>
</div>
<span className="val ok">{fmtUsd(data.optimize.savingsUSD)}</span>
</div>
)
}