mirror of
https://github.com/AgentSeal/codeburn.git
synced 2026-11-29 10:43:47 +00:00
fix(codex): detect a counter restart from the total, not from any single field
Any one cumulative field that fell was read as a restart, so a corrected component counted its whole running value again (output 800 -> 790 under a rising total emitted 790 output). Decide the restart on the total falling, or every reported field falling when no previous total exists; otherwise a falling field is a correction and adds nothing.
This commit is contained in:
parent
fd8c228e36
commit
63de339cdc
2 changed files with 31 additions and 3 deletions
|
|
@ -1589,6 +1589,7 @@ function createParser(source: SessionSource, seenKeys: Set<string>, capture?: {
|
|||
if (infoIdentity === prevInfoIdentity) continue
|
||||
prevInfoIdentity = infoIdentity
|
||||
if (reportsCumulative && prevCumulativeTotal !== null && cumulativeTotal === prevCumulativeTotal) continue
|
||||
const prevTotal = prevCumulativeTotal
|
||||
prevCumulativeTotal = reportsCumulative ? cumulativeTotal : null
|
||||
|
||||
const last = info.last_token_usage
|
||||
|
|
@ -1607,9 +1608,22 @@ function createParser(source: SessionSource, seenKeys: Set<string>, capture?: {
|
|||
} else if (cumulativeTotal > 0) {
|
||||
const total = info.total_token_usage
|
||||
if (!total) continue
|
||||
// An omitted counter is unchanged. One below its previous value
|
||||
// restarted (resume, compaction): it holds the usage since then.
|
||||
const since = (now: number | undefined, prev: number) => now === undefined ? 0 : now >= prev ? now - prev : now
|
||||
// A total below the previous one (or, without a previous total,
|
||||
// every reported field falling) is a counter restart (resume,
|
||||
// compaction): each field holds the usage since then. Otherwise a
|
||||
// single falling field is a correction and adds nothing. An
|
||||
// omitted field is unchanged.
|
||||
const counters: Array<[number | undefined, number]> = [
|
||||
[total.input_tokens, prevInput], [total.cached_input_tokens, prevCached],
|
||||
[total.cache_write_input_tokens, prevCacheWrite], [total.output_tokens, prevOutput],
|
||||
[total.reasoning_output_tokens, prevReasoning],
|
||||
]
|
||||
const present = counters.filter(([now]) => now !== undefined) as Array<[number, number]>
|
||||
const restart = prevTotal !== null
|
||||
? cumulativeTotal < prevTotal
|
||||
: present.length > 0 && present.every(([now, prev]) => now < prev)
|
||||
const since = (now: number | undefined, prev: number) =>
|
||||
now === undefined ? 0 : restart ? now : Math.max(0, now - prev)
|
||||
inputTokens = since(total.input_tokens, prevInput)
|
||||
cachedInputTokens = since(total.cached_input_tokens, prevCached)
|
||||
cacheWriteTokens = since(total.cache_write_input_tokens, prevCacheWrite)
|
||||
|
|
|
|||
|
|
@ -857,6 +857,20 @@ describe('codex provider - JSONL parsing', () => {
|
|||
expect(calls.map(c => [c.inputTokens, c.outputTokens])).toEqual([[8_000, 800], [3_000, 300], [3_000, 300]])
|
||||
})
|
||||
|
||||
it('reads one falling field under a rising total as a correction, not a restart', async () => {
|
||||
const filePath = await writeSession(tmpDir, '2026-04-14', 'rollout-cumulative-correction.jsonl', [
|
||||
sessionMeta({ session_id: 'sess-correction', model: 'gpt-5.5' }),
|
||||
tokenCount({ timestamp: '2026-04-14T10:00:10Z', total: { input: 8_000, output: 800 } }),
|
||||
tokenCount({ timestamp: '2026-04-14T10:00:20Z', total: { input: 9_000, output: 790, total: 9_790 } }),
|
||||
])
|
||||
|
||||
const provider = createCodexProvider(tmpDir)
|
||||
const calls: ParsedProviderCall[] = []
|
||||
for await (const call of provider.createSessionParser({ path: filePath, project: 'test', provider: 'codex' }, new Set()).parse()) calls.push(call)
|
||||
|
||||
expect(calls.map(c => [c.inputTokens, c.outputTokens])).toEqual([[8_000, 800], [1_000, 0]])
|
||||
})
|
||||
|
||||
it('reads a cumulative field the record omits as unchanged', async () => {
|
||||
const total = (timestamp: string, usage: Record<string, number>) => JSON.stringify({
|
||||
type: 'event_msg', timestamp, payload: { type: 'token_count', info: { total_token_usage: usage } },
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue