codeburn/tests/optimize-one-hour-cache.test.ts
Resham Joshi b18d42d0b4
feat(optimize): flag one-hour cache writes the next request never needed (#1860)
* fix(optimize): price findings from the calls they point at

Every finding's dollar figure was its token estimate times one blended
rate: 70% of the period's spend over all input-side tokens. That rate is
close to a cache-read price, so it misprices anything that is not a cache
read. Schema findings already weight cache reads at 0.1, so their reads
were discounted twice; findings whose tokens include output were priced
as input.

A finding can now carry savingsUSD, computed from the calls it names.
Every surface reads it through findingUSD: the CLI and TUI optimize
views, the JSON report (and so the desktop Optimize page), the menubar
payload and MCP get_savings, and the --apply list. classTotals sums it,
scaled to the apply-able share for fix findings. Findings without it keep
tokensSaved times the blended rate, and any token whose session or model
has no price still falls back to that rate.

Measured now:
- low-worth-sessions: each session's real cost times its recovery fraction
- retry-heavy-capabilities: the retry turns' real cost times the fraction
- redundant-rereads, build-folder-reads: the reads at the input rate of
  their session's most-called model
- warmup-heavy: the excess cache writes at the session model's write rate
- mcp-low-coverage, mcp-project-scope: each call's charged schema tokens
  at that call model's cache-write or cache-read rate
- unused-mcp, mcp-deferral-off, mcp-alwaysload-hygiene,
  mcp-defer-threshold: the per-session schema at the input rate of the
  sessions charged

Still on the blended rate, because no calls can be named: read-edit-ratio
(reads that never happened), context-heavy-sessions, cost-outliers,
low-cache-hit-sessions, claude-md-too-long, bash-output-cap,
recurring-context and the unused agents, skills and commands.

* feat(optimize): flag one-hour cache writes the next request never needed

A one-hour prompt-cache write is billed at 1.6 times the five-minute
rate and only earns that back when the same model's cache is next used
after a pause of more than five minutes but less than an hour. Reads
refresh either TTL, so a write is judged by the first such pause after
it, not just by the very next call.

The new one-hour-cache-unused nudge walks each session per model in
time order, prices the 60% premium on every one-hour write that never
reached such a pause from the call's own model and tier, and fires at
one dollar or more. Unpriced models and five-minute writes add nothing.

* test(optimize): pin one-hour writes that later hits kept warm

Every cache hit refreshes the TTL, so a one-hour write followed by more
than an hour of steady activity and then a twenty-minute pause still
needed the hour. The detector already judges the length of the first
pause over five minutes, not its distance from the write; this test
pins that. A pause that itself runs past the hour still counts as
wasted, because the one-hour entry expires inside it either way.

Also lists one-hour-cache-unused among the measured findings.
2026-11-05 00:10:23 +01:00

90 lines
3.3 KiB
TypeScript

import { describe, it, expect } from 'vitest'
import { detectOneHourCacheUnused } from '../src/optimize-one-hour-cache.js'
import type { ParsedApiCall, ProjectSummary } from '../src/types.js'
const START = Date.parse('2026-05-01T10:00:00Z')
const MIN = 60_000
function call(atMs: number, oneHour: number, model = 'claude-sonnet-4-6'): ParsedApiCall {
return {
provider: 'claude',
model,
usage: {
inputTokens: 10,
outputTokens: 100,
cacheCreationInputTokens: oneHour,
cacheReadInputTokens: 0,
cachedInputTokens: 0,
reasoningTokens: 0,
webSearchRequests: 0,
},
costUSD: 0,
tools: [],
mcpTools: [],
skills: [],
subagentTypes: [],
hasAgentSpawn: false,
hasPlanMode: false,
speed: 'standard',
timestamp: new Date(START + atMs).toISOString(),
bashCommands: [],
deduplicationKey: `c${atMs}`,
...(oneHour > 0 ? { cacheCreationOneHourTokens: oneHour } : {}),
}
}
function project(calls: ParsedApiCall[]): ProjectSummary {
return {
project: 'app',
sessions: [{ sessionId: 's1', turns: [{ assistantCalls: calls }] }],
} as unknown as ProjectSummary
}
// Five 100k one-hour writes, two minutes apart, then a final call `lastGapMs` after the fifth.
function fiveWrites(lastGapMs: number, model?: string): ProjectSummary {
const calls = Array.from({ length: 5 }, (_, i) => call(i * 2 * MIN, 100_000, model))
calls.push(call(8 * MIN + lastGapMs, 0, model))
return project(calls)
}
describe('detectOneHourCacheUnused', () => {
it('prices the 60% premium on writes the next request reached within five minutes', () => {
const finding = detectOneHourCacheUnused([fiveWrites(2 * MIN)])
expect(finding).not.toBeNull()
expect(finding!.id).toBe('one-hour-cache-unused')
expect(finding!.tokensSaved).toBe(500_000)
// 100,000 x $3.75/M x 0.6 = $0.225 per write, five writes.
expect(finding!.savingsUSD).toBeCloseTo(1.125, 10)
expect(finding!.impact).toBe('low')
expect(finding!.explanation).toContain('5 writes in 1 session (100% of one-hour write tokens)')
expect(finding!.explanation).toContain('usually came 2.0 min later')
})
it('keeps writes whose session paused 20 minutes before the next request', () => {
expect(detectOneHourCacheUnused([fiveWrites(20 * MIN)])).toBeNull()
})
it('keeps writes that hits held warm for over an hour before a 20-minute pause', () => {
const calls = Array.from({ length: 5 }, (_, i) => call(i * 2 * MIN, 100_000))
for (let t = 10 * MIN; t <= 130 * MIN; t += 2 * MIN) calls.push(call(t, 0))
calls.push(call(150 * MIN, 0))
expect(detectOneHourCacheUnused([project(calls)])).toBeNull()
})
it('counts writes whose next pause outlasted the hour too', () => {
expect(detectOneHourCacheUnused([fiveWrites(90 * MIN)])!.savingsUSD).toBeCloseTo(1.125, 10)
})
it('ignores sessions with only five-minute writes', () => {
const calls = Array.from({ length: 6 }, (_, i) => {
const c = call(i * 2 * MIN, 0)
return { ...c, usage: { ...c.usage, cacheCreationInputTokens: 100_000 } }
})
expect(detectOneHourCacheUnused([project(calls)])).toBeNull()
})
it('ignores models with no cache-write price', () => {
expect(detectOneHourCacheUnused([fiveWrites(2 * MIN, 'codeburn-test-unpriced-model')])).toBeNull()
})
})