codeburn/app/renderer/components/SessionDrawer.tsx
Resham Joshi e42f1e5db3
Explain why one session cost what it did (CLI + app session view) (#1645)
* feat(sessions): explain why one session cost what it did

`codeburn sessions --id <id> --why` reads one Claude Code transcript on
demand and returns where the money went: a verdict, findings ranked by
dollars, spend by prompt with helper (sub-agent) cost stacked, and every
prompt's model calls and tool steps with timing, cost parts and step detail.
Money comes from the parser's own dedupe, turn grouping and calculateCost,
so the session total matches its `sessions` row.

The desktop app opens this as a focused session view when a Claude Code
session is clicked. Transcript content is read on demand only: never cached,
never routed through serve's output memo, never synced or sent in telemetry.

* fix(sessions): audit fixes for the session cost view

Recovery after a failed step stays inside its prompt; neutral note when the
view and the row differ (forked/resumed transcripts); starting-context
estimate compares against the cached prefix; local dates with the end date
when the session spans days; idle gaps that end with a helper reporting back
say so; savings are the difference of the shown figures; singular counts;
more secret patterns (sk_live_/sk_test_, aws_secret_access_key, URL
credentials); a note when the prompt count differs from the row's turns.
2026-10-06 11:49:58 +02:00

297 lines
14 KiB
TypeScript

import { useEffect, useMemo, useRef } from 'react'
import { Stat } from './Stat'
import { useEscape } from '../hooks/useEscape'
import { formatCompact, formatCount, formatDayLong, formatDuration, formatUsd, shortenProjectPath } from '../lib/format'
import { DUR, useExitAnimation } from '../lib/motion'
import { codeburn } from '../lib/ipc'
import type { InvestigationFilters } from '../lib/investigation'
import { contributeRow } from '../lib/investigation'
import type { SessionDrillRow } from '../lib/types'
import { Icon } from './icons'
import { t } from '../i18n'
/**
* The drill-through side drawer: a plain-language read of one session, then the
* cost/token figures and every link the report carries (PR URLs). All content
* derives from the already-loaded contributions report, and the heavy
* breakdowns below only render while the drawer is open (lazy by mount, not by
* fetch), so the list behind it stays responsive. Transcript text crosses IPC
* only for the session view (SessionView.tsx): read on demand when that view
* opens, never cached, never synced or sent in telemetry.
*
* A11y contract: role="dialog", Escape closes, focus moves into the panel on
* open and the PARENT returns focus to the control that opened it (the opener
* element is still alive behind the drawer). Tab is trapped inside.
*/
export function SessionDrawer({ row, openKey, filters, medianCost, onClose }: {
row: SessionDrillRow
/** Row identity, so a drawer still exiting on the old row disarms its close
* when the user picks a new one. */
openKey: string
filters: InvestigationFilters
/** Median cost of the sessions the list is currently showing (the searched
* and filtered set). Absent when the population is too small for the
* comparison to mean anything. */
medianCost?: number
onClose: () => void
}) {
const panelRef = useRef<HTMLDivElement>(null)
const { closing, beginExit } = useExitAnimation(onClose, DUR.slow, openKey)
useEscape(true, beginExit)
useEffect(() => {
const panel = panelRef.current
panel?.focus()
const onKeyDown = (event: KeyboardEvent) => {
if (event.key !== 'Tab' || !panel) return
// Keep Tab cycling inside the drawer while it is open.
const focusable = panel.querySelectorAll<HTMLElement>('button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])')
if (focusable.length === 0) return
const first = focusable[0]!
const last = focusable[focusable.length - 1]!
if (event.shiftKey && document.activeElement === first) {
event.preventDefault()
last.focus()
} else if (!event.shiftKey && document.activeElement === last) {
event.preventDefault()
first.focus()
}
}
window.addEventListener('keydown', onKeyDown, true)
return () => window.removeEventListener('keydown', onKeyDown, true)
}, [])
return (
<>
<div className={closing ? 'drawer-scrim closing' : 'drawer-scrim'} aria-hidden="true" onClick={beginExit} />
<aside
ref={panelRef}
className={closing ? 'session-drawer closing' : 'session-drawer'}
role="dialog"
aria-modal="true"
aria-label={t('sessions.drawer.ariaLabel', { title: row.title || shortenProjectPath(row.project) })}
tabIndex={-1}
>
<div className="drawer-head">
<div>
<h3 className="drawer-title">{row.title || shortenProjectPath(row.project)}</h3>
<div className="drawer-sub">
{row.provider} · {shortenProjectPath(row.project)} · <span className="mono">{row.sessionId.slice(0, 18)}</span>
</div>
<div className="drawer-sub">
{formatDayLong(row.startedAt)} → {formatDayLong(row.endedAt)}
{row.durationMs > 0 && <> · {formatDuration(row.durationMs)}</>}
</div>
</div>
<button type="button" className="drawer-close" aria-label={t('sessions.drawer.closeAriaLabel')} onClick={beginExit}><Icon name="x" /></button>
</div>
<SessionDetails row={row} filters={filters} medianCost={medianCost} />
</aside>
</>
)
}
/** The session's cost, tokens, models, categories, branches and PRs, from the
* contributions report. The drawer's body, and the session view's details. */
export function SessionDetails({ row, filters, medianCost }: { row: SessionDrillRow; filters: InvestigationFilters; medianCost?: number }) {
const contribution = useMemo(() => contributeRow(row, filters), [row, filters])
const breakdown = useMemo(() => buildBreakdowns(row), [row])
const cacheTotal = row.inputTokens + row.cacheReadTokens
const cacheHit = cacheTotal > 0 ? Math.round(row.cacheReadTokens / cacheTotal * 100) : 0
const median = medianCost !== undefined && medianCost > 0 ? medianCost : null
const selectedCost = contribution !== null && contribution.cost < row.cost - 1e-9 ? contribution.cost : null
const leadCost = selectedCost ?? row.cost
// Past 100x the multiple says nothing the dollar figure has not already said.
const ratio = median === null || leadCost / median > 100 ? null : leadCost / median
const foldLabel = branchPrLabel(breakdown)
return (
<>
<p className="drawer-lead">
{selectedCost === null ? t('sessions.drawer.leadCost') : t('sessions.drawer.leadCostSelection')}
<b>{formatUsd(leadCost)}</b>
{ratio === null
? '.'
: ratio < 0.1
? t('sessions.drawer.leadFraction')
: <>{t('sessions.drawer.leadRatioPrefix')}<b>{formatRatio(ratio)}{t('sessions.drawer.ratioUnit')}</b>{t('sessions.drawer.leadRatioSuffix')}</>}
</p>
<div className="stats drawer-tiles">
<Stat
label={t('sessions.drawer.statCostLabel')}
value={formatUsd(leadCost)}
delta={selectedCost !== null
? t('sessions.drawer.deltaOfTotal', { total: formatUsd(row.cost) })
: ratio === null
? t('sessions.drawer.deltaFullSession')
: ratio < 0.1
? <span className="down">{t('sessions.drawer.deltaBelowMedian')}</span>
: <span className={ratio >= 1 ? 'up' : 'down'}>{t('sessions.drawer.deltaRatioMedian', { ratio: `${formatRatio(ratio)}${t('sessions.drawer.ratioUnit')}` })}</span>}
/>
<Stat label={t('sessions.drawer.statTurnsLabel')} value={row.turns.toLocaleString()} delta={formatCount(row.calls, 'call')} />
{row.durationMs > 0
? <Stat label={t('sessions.drawer.statDurationLabel')} value={formatDuration(row.durationMs)} delta={t('sessions.drawer.deltaWallClock')} />
: <Stat label={t('sessions.drawer.statCallsLabel')} value={row.calls.toLocaleString()} delta={t('sessions.drawer.deltaApiCalls')} />}
</div>
{row.isSidechain && row.parentSessionId && (
<p className="drawer-note">{t('sessions.drawer.subagentPrefix')}<span className="mono">{row.parentSessionId.slice(0, 18)}</span>.</p>
)}
<DrawerBreakdown label={t('sessions.drawer.modelsLabel')} rows={breakdown.models} />
<DrawerBreakdown label={t('sessions.drawer.categoriesLabel')} rows={breakdown.categories} />
<details className="drawer-fold">
<summary>
{t('sessions.drawer.tokensSummary', {
inTok: formatCompact(row.inputTokens),
outTok: formatCompact(row.outputTokens),
cacheTok: formatCompact(row.cacheWriteTokens),
hitPct: cacheHit,
})}
</summary>
<div className="drawer-fold-body">
<div className="stats">
<Stat label={t('sessions.drawer.statInputLabel')} value={formatCompact(row.inputTokens)} delta={t('sessions.drawer.deltaTokensSent')} />
<Stat label={t('sessions.drawer.statOutputLabel')} value={formatCompact(row.outputTokens)} delta={t('sessions.drawer.deltaTokensGenerated')} />
<Stat label={t('sessions.drawer.statCacheReadLabel')} value={formatCompact(row.cacheReadTokens)} delta={t('sessions.drawer.deltaCacheHit', { percent: cacheHit })} />
<Stat label={t('sessions.drawer.statCacheWriteLabel')} value={formatCompact(row.cacheWriteTokens)} delta={t('sessions.drawer.deltaTokensCached')} />
</div>
</div>
</details>
{foldLabel !== null && (
<details className="drawer-fold">
<summary>{t('sessions.drawer.branchesPrsSummary', { label: foldLabel })}</summary>
<div className="drawer-fold-body">
<DrawerBreakdown label={t('sessions.drawer.branchesLabel')} rows={breakdown.branches} caption={t('sessions.drawer.branchesCaption')} />
{breakdown.days.length > 1 && <DrawerBreakdown label={t('sessions.drawer.daysLabel')} rows={breakdown.days} />}
<DrawerBreakdown label={t('sessions.drawer.prsLabel')} rows={breakdown.prs} caption={t('sessions.drawer.prsCaption')} link />
{breakdown.unattributedPrCost > 0 && (
<p className="drawer-note">{t('sessions.drawer.notTiedToPr', { amount: formatUsd(breakdown.unattributedPrCost) })}</p>
)}
</div>
</details>
)}
<p className="drawer-note">
{row.savingsUSD > 0 ? t('sessions.drawer.savedBaseline', { amount: formatUsd(row.savingsUSD) }) : t('sessions.drawer.savedBaselineNone')}
</p>
</>
)
}
function formatRatio(ratio: number): string {
return (ratio >= 10 ? Math.round(ratio) : Math.round(ratio * 10) / 10).toLocaleString('en-US')
}
function branchPrLabel({ branches, prs }: { branches: BreakdownRow[]; prs: BreakdownRow[] }): string | null {
const parts: string[] = []
// A lone `main` with no PRs is every session's default: nothing to unfold.
if (branches.length > 0 && !(branches.length === 1 && branches[0]!.label === 'main' && prs.length === 0)) {
const named = branches.slice(0, 2).map(entry => entry.label).join(', ')
parts.push(branches.length > 2 ? t('sessions.drawer.branchesMoreList', { named, count: branches.length - 2 }) : named)
}
if (prs.length > 0) parts.push(t(prs.length === 1 ? 'sessions.drawer.prCount.one' : 'sessions.drawer.prCount.other', { count: prs.length }))
return parts.length > 0 ? parts.join(', ') : null
}
type BreakdownRow = { key: string; label: string; cost: number; approx?: boolean; url?: string }
function buildBreakdowns(row: SessionDrillRow): {
models: BreakdownRow[]
categories: BreakdownRow[]
branches: BreakdownRow[]
days: BreakdownRow[]
prs: BreakdownRow[]
unattributedPrCost: number
} {
const models = new Map<string, number>()
const categories = new Map<string, number>()
const branches = new Map<string, number>()
const days = new Map<string, number>()
const prs = new Map<string, { cost: number; approx: boolean }>()
let unattributedPrCost = 0
const segments = row.contributions?.segments ?? []
for (const segment of segments) {
for (const [model, cost] of Object.entries(segment.models)) {
if (cost === 0) continue
models.set(model, (models.get(model) ?? 0) + cost)
}
if (segment.category && segment.cost > 0) categories.set(segment.category, (categories.get(segment.category) ?? 0) + segment.cost)
if (segment.branch && segment.cost > 0) branches.set(segment.branch, (branches.get(segment.branch) ?? 0) + segment.cost)
if (segment.day && segment.cost > 0) days.set(segment.day, (days.get(segment.day) ?? 0) + segment.cost)
if (segment.prs.length === 0) {
unattributedPrCost += segment.cost
} else {
const share = 1 / segment.prs.length
for (const url of segment.prs) {
const entry = prs.get(url) ?? { cost: 0, approx: false }
entry.cost += segment.cost * share
entry.approx = entry.approx || segment.approx === true
prs.set(url, entry)
}
}
}
const toRows = (map: Map<string, number>): BreakdownRow[] =>
[...map.entries()]
.map(([key, cost]) => ({ key, label: key, cost }))
.sort((a, b) => b.cost - a.cost)
return {
models: toRows(models).map(entry => ({ ...entry, label: entry.key === '' ? t('sessions.drawer.unknownModel') : entry.key })),
categories: toRows(categories),
branches: toRows(branches).map(entry => ({ ...entry, label: entry.key })),
days: toRows(days),
prs: [...prs.entries()]
.map(([url, entry]) => ({ key: url, label: prLabel(url), cost: entry.cost, approx: entry.approx || undefined, url }))
.sort((a, b) => b.cost - a.cost),
unattributedPrCost,
}
}
/** Short `owner/repo#123` form for GitHub URLs, else the URL itself — the same
* rule the by-PR report uses for labels. */
function prLabel(url: string): string {
const match = /^https:\/\/github\.com\/([^/]+)\/([^/]+)\/pull\/(\d+)/.exec(url)
return match ? `${match[1]}/${match[2]}#${match[3]}` : url
}
function DrawerBreakdown({ label, rows, caption, link = false }: {
label: string
rows: BreakdownRow[]
caption?: string
link?: boolean
}) {
if (rows.length === 0) return null
const max = rows[0]!.cost
return (
<div className="drawer-breakdown" role="group" aria-label={t('sessions.drawer.breakdownAriaLabel', { label })}>
<div className="drawer-breakdown-head">{label}</div>
{rows.map(entry => (
<div className="drawer-breakdown-row" key={entry.key}>
<span className="drawer-breakdown-label" title={entry.url ?? entry.label}>{entry.label}</span>
<div className="drawer-breakdown-bar" aria-hidden="true"><span style={{ width: `${max > 0 ? entry.cost / max * 100 : 0}%` }} /></div>
{link && entry.url
? (
<a
className="drawer-breakdown-cost drawer-link"
href={entry.url}
onClick={event => {
event.preventDefault()
void codeburn.openExternal(entry.url!)
}}
>
{entry.approx ? '~' : ''}{formatUsd(entry.cost)}
</a>
)
: <span className="drawer-breakdown-cost">{formatUsd(entry.cost)}</span>}
</div>
))}
{caption && <p className="drawer-caption">{caption}</p>}
</div>
)
}