From 39d2f79ef3e5c46bcd1e7a186b51f2c0e0585bd0 Mon Sep 17 00:00:00 2001 From: iamtoruk Date: Fri, 10 Jul 2026 18:33:54 -0700 Subject: [PATCH] =?UTF-8?q?feat(app):=20Optimize=20section=20=E2=80=94=20W?= =?UTF-8?q?aste/Reverts/Abandoned/Fixes=20tabs=20with=20dollar=20totals?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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). --- app/renderer/App.tsx | 3 + app/renderer/sections/Optimize.test.tsx | 152 +++++++++++++++++++++++ app/renderer/sections/Optimize.tsx | 156 ++++++++++++++++++++++++ 3 files changed, 311 insertions(+) create mode 100644 app/renderer/sections/Optimize.test.tsx create mode 100644 app/renderer/sections/Optimize.tsx diff --git a/app/renderer/App.tsx b/app/renderer/App.tsx index b885633b..362b1f21 100644 --- a/app/renderer/App.tsx +++ b/app/renderer/App.tsx @@ -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() { ) : section === 'spend' ? ( + ) : section === 'optimize' ? ( + ) : ( )} diff --git a/app/renderer/sections/Optimize.test.tsx b/app/renderer/sections/Optimize.test.tsx new file mode 100644 index 00000000..c3ff5307 --- /dev/null +++ b/app/renderer/sections/Optimize.test.tsx @@ -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>(), + getYield: vi.fn<(period: string) => Promise>(), +})) +vi.mock('../lib/ipc', async orig => { + const actual = await orig() + 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() + + 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() + 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() + + 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() + }) +}) diff --git a/app/renderer/sections/Optimize.tsx b/app/renderer/sections/Optimize.tsx new file mode 100644 index 00000000..c997239d --- /dev/null +++ b/app/renderer/sections/Optimize.tsx @@ -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

{children}

+} + +export function Optimize({ period, provider }: { period: Period; provider: string }) { + const overview = usePolled(() => codeburn.getOverview(period, provider), [period, provider]) + const yieldReport = usePolled(() => codeburn.getYield(period), [period]) + const [tab, setTab] = useState('waste') + + if (!overview.data) { + if (overview.error?.kind === 'not-found') { + return ( + +

+ CodeBurn Desktop reads your usage by running the{' '} + codeburn command, but it isn't + on your PATH yet. +

+

+ Install it with npm i -g codeburn, + then reopen this window. +

+
+ ) + } + if (overview.error) { + return ( + +

{overview.error.message}

+
+ ) + } + return ( + + Scanning optimize findings… + + ) + } + + 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 ( + <> + setTab(value as OptimizeTab)} + style={{ alignSelf: 'flex-start' }} + /> + + {tab === 'waste' ? ( + + ) : tab === 'reverts' ? ( + + ) : tab === 'abandoned' ? ( + + ) : ( + + )} + + + ) +} + +function WasteRows({ data }: { data: MenubarPayload }) { + const findings = data.optimize.topFindings + + if (!findings.length) return No waste findings in this range yet. + + return ( + <> + {findings.map((finding, i) => ( +
+ {String(i + 1).padStart(2, '0')} +
+ {finding.title} + {finding.impact} impact +
+ {fmtUsd(finding.savingsUSD)} +
+ ))} + + ) +} + +function YieldRows({ + report, + category, + empty, +}: { + report: ReturnType> + category: SessionYieldJson['category'] + empty: string +}) { + if (!report.data) { + if (report.error) return

{report.error.message}

+ return {report.loading ? 'Loading yield analysis…' : empty} + } + + const rows = report.data.details.filter(row => row.category === category) + if (!rows.length) return {empty} + + return ( + <> + {rows.map((row, i) => ( +
+ {String(i + 1).padStart(2, '0')} +
+ {row.project} + + {row.commitCount.toLocaleString('en-US')} {row.commitCount === 1 ? 'commit' : 'commits'} · {row.sessionId} + +
+ {fmtUsd(row.costUSD)} +
+ ))} + + ) +} + +function FixesRows({ data }: { data: MenubarPayload }) { + const count = data.optimize.findingCount + if (!count) return No fixes in this range yet. + + return ( +
+ {String(count).padStart(2, '0')} +
+ {count.toLocaleString('en-US')} fixes ready + {fmtUsd(data.optimize.savingsUSD)} recoverable waste +
+ {fmtUsd(data.optimize.savingsUSD)} +
+ ) +}