fix(cline): keep a recorded zero cost at zero

A request Cline recorded at cost 0 (a free model) was re-priced from tokens
into a positive charge. Any recorded cost, zero included, is now kept; only a
request with no cost field is priced from its tokens.
This commit is contained in:
Resham Joshi 2026-11-06 05:48:56 -08:00
parent c8dc383d82
commit 66d4b98ceb
2 changed files with 7 additions and 4 deletions

View file

@ -207,9 +207,10 @@ export function createClineParser(source: SessionSource, seenKeys: Set<string>,
const tsDate = entry.ts ? new Date(entry.ts) : null
const timestamp = tsDate && !Number.isNaN(tsDate.getTime()) ? tsDate.toISOString() : ''
// Cline's `cost` is what the request was billed on its real route
// (cache writes and gateway prices included); a token re-price would
// replace it with a list price for a model the task may not even name.
const isReported = typeof cost === 'number' && Number.isFinite(cost) && cost > 0
// (cache writes and gateway prices included, 0 for a free model); a
// token re-price would replace it with a list price for a model the
// task may not even name. Only a missing cost is priced from tokens.
const isReported = typeof cost === 'number' && Number.isFinite(cost) && cost >= 0
const costUSD = isReported ? cost! : calculateCost(model, tokensIn, tokensOut, cacheWrites, cacheReads, 0)
yield {

View file

@ -256,7 +256,7 @@ it('holds the reported-cost provider set to its documented membership', async ()
])
})
it('keeps a Cline request cost cold and warm, and re-prices requests without one', async () => {
it('keeps a Cline request cost, including a recorded zero, cold and warm, and re-prices requests without one', async () => {
const tasks = join(HOME, '.cline', 'data', 'tasks')
const writeTask = async (id: string, usage: Record<string, number>) => {
await mkdir(join(tasks, id), { recursive: true })
@ -269,6 +269,7 @@ it('keeps a Cline request cost cold and warm, and re-prices requests without one
}
await writeTask('reported', { tokensIn: 1000, tokensOut: 200, cost: 0.5 })
await writeTask('unreported', { tokensIn: 1000, tokensOut: 200 })
await writeTask('free', { tokensIn: 1000, tokensOut: 200, cost: 0 })
const { clearSessionCache, parseAllSessions } = await import('../src/parser.js')
clearSessionCache()
@ -276,6 +277,7 @@ it('keeps a Cline request cost cold and warm, and re-prices requests without one
clearSessionCache()
const warm = (await parseAllSessions(undefined, 'cline')).reduce((s, p) => s + p.totalCostUSD, 0)
// The free request's recorded $0 stays $0; only the one without a cost is priced.
const floor = 1000 * 3e-6 + 200 * 15e-6
expect(cold).toBeCloseTo(0.5 + floor, 10)
expect(warm).toBeCloseTo(cold, 10)