mirror of
https://github.com/AgentSeal/codeburn.git
synced 2026-08-21 06:24:32 +00:00
The daily-cache re-derivation test seeded v18, a version that only ever existed as an unreleased draft of this change. Seed the shipped v17 so the test models the 17 -> 19 upgrade path users actually hit, and rename it: the bump re-derives every day for every provider, not just Grok, because the daily cache has no per-provider invalidation. The Grok day stays as the fixture since Grok is what the bump exists to correct. The changelog entry now says outright that Grok totals change materially on upgrade (150K -> 96.3M cache-read tokens on a 568-session corpus), that a turn without a turn_completed record inside an otherwise-covered session is dropped rather than estimated, and that the one-time daily re-derivation reads the warm session cache and keeps the superseded file. The context-bloat denominator fix moves to Fixed and names the providers it corrects. docs/providers/grok.md gets the same undercount warning in the token model and a matching entry under Quirks.
108 lines
3.2 KiB
TypeScript
108 lines
3.2 KiB
TypeScript
import { describe, it, expect, beforeEach, afterEach } from 'vitest'
|
|
import { mkdir, readFile, rm, writeFile } from 'fs/promises'
|
|
import { join } from 'path'
|
|
import { tmpdir } from 'os'
|
|
|
|
import {
|
|
currentTzKey,
|
|
ensureCacheHydrated,
|
|
toDateString,
|
|
type DailyEntry,
|
|
} from '../src/daily-cache.js'
|
|
|
|
// The last SHIPPED daily-cache version before the Grok accounting change, so
|
|
// this models the real 17 -> 19 upgrade path users hit. (v18 existed only as an
|
|
// unreleased draft.) Anything below MIN_SUPPORTED_VERSION is untrusted, which
|
|
// is what makes the re-derivation global rather than Grok-scoped.
|
|
const PRE_FIX_DAILY_VERSION = 17
|
|
const cacheRoot = join(tmpdir(), `codeburn-daily-rederive-${process.pid}-${Date.now()}`)
|
|
|
|
function day(date: string, cost: number): DailyEntry {
|
|
return {
|
|
date,
|
|
cost,
|
|
savingsUSD: 0,
|
|
calls: 1,
|
|
sessions: 1,
|
|
inputTokens: 100,
|
|
outputTokens: 20,
|
|
cacheReadTokens: 30,
|
|
cacheWriteTokens: 0,
|
|
editTurns: 0,
|
|
oneShotTurns: 0,
|
|
models: {
|
|
'Grok Build': {
|
|
calls: 1,
|
|
cost,
|
|
savingsUSD: 0,
|
|
inputTokens: 100,
|
|
outputTokens: 20,
|
|
cacheReadTokens: 30,
|
|
cacheWriteTokens: 0,
|
|
},
|
|
},
|
|
categories: {},
|
|
providers: {
|
|
grok: {
|
|
calls: 1,
|
|
cost,
|
|
savingsUSD: 0,
|
|
sessions: 1,
|
|
inputTokens: 100,
|
|
outputTokens: 20,
|
|
cacheReadTokens: 30,
|
|
cacheWriteTokens: 0,
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
beforeEach(async () => {
|
|
process.env['CODEBURN_CACHE_DIR'] = cacheRoot
|
|
await rm(cacheRoot, { recursive: true, force: true })
|
|
await mkdir(cacheRoot, { recursive: true })
|
|
})
|
|
|
|
afterEach(async () => {
|
|
await rm(cacheRoot, { recursive: true, force: true })
|
|
})
|
|
|
|
// Raising MIN_SUPPORTED_VERSION re-derives EVERY day from EVERY provider, not
|
|
// only Grok - the daily cache has no per-provider invalidation. A Grok day is
|
|
// used here because Grok is the provider whose totals the bump exists to
|
|
// correct; the mechanism under test is version-wide.
|
|
describe('daily-cache re-derivation on a DAILY_CACHE_VERSION bump', () => {
|
|
it('re-derives a day from a below-minimum v17 cache while preserving the old file', async () => {
|
|
const date = toDateString(new Date(Date.now() - 7 * 24 * 60 * 60 * 1000))
|
|
const yesterday = toDateString(new Date(Date.now() - 24 * 60 * 60 * 1000))
|
|
const oldPath = join(cacheRoot, `daily-cache.v${PRE_FIX_DAILY_VERSION}.json`)
|
|
const oldCache = {
|
|
version: PRE_FIX_DAILY_VERSION,
|
|
savingsConfigHash: 'cfg',
|
|
tzKey: currentTzKey(),
|
|
lastComputedDate: yesterday,
|
|
days: [day(date, 99)],
|
|
complete: true,
|
|
watermarkTrusted: true,
|
|
}
|
|
await writeFile(oldPath, JSON.stringify(oldCache))
|
|
|
|
let parseCount = 0
|
|
const corrected = day(date, 2)
|
|
const hydrated = await ensureCacheHydrated(
|
|
async () => {
|
|
parseCount++
|
|
return []
|
|
},
|
|
() => [corrected],
|
|
'cfg',
|
|
() => true,
|
|
)
|
|
|
|
const refreshedDay = hydrated.days.find(entry => entry.date === date)
|
|
expect(parseCount).toBe(1)
|
|
expect(refreshedDay?.providers.grok?.cost).toBe(2)
|
|
expect(refreshedDay?.cost).toBe(2)
|
|
expect(JSON.parse(await readFile(oldPath, 'utf8'))).toEqual(oldCache)
|
|
})
|
|
})
|