diff --git a/CHANGELOG.md b/CHANGELOG.md index b741c511..034eb6a6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,6 +40,7 @@ ### Fixed - **Mixed-version installs no longer thrash the Codex / Cursor / Antigravity result caches.** Daily and session caches already own a version-suffixed file so an old desktop binary and a newer CLI cannot clobber each other. The three per-provider result caches still used one unsuffixed filename with an internal version field, so a v10 and a v11 binary rewrote the same `codex-results.json` (and the Cursor / Antigravity siblings) on every run and each re-parsed its whole corpus. They now write `*-results.v.json` the same way the daily cache does. The unsuffixed file is left for older binaries; a matching-version copy is adopted once and never overwritten. (#1082) - **Codex spend no longer counts reasoning tokens twice, and cache writes are priced only where OpenAI actually charges for them.** OpenAI bills reasoning tokens as *part of* `output_tokens`, not on top of it — on a 1,396-rollout corpus all 134,316 events carrying a total satisfy `input + output == total` — but CodeBurn added `reasoning_output_tokens` to output when pricing a Codex call and again in the models, audit and per-model displays. Every Codex number was therefore too high: on that corpus **cost by $166.03 (3.5%)** and **displayed Output tokens by 34.6%** ($4,713.12 -> $4,547.09; 22.6M -> 16.8M output tokens). The raw `reasoningTokens` figure is unchanged and still reported on its own; only the double-count is gone. Both places that price a Codex call — the parser and the cache-rehydration re-price — now go through one shared `billableOutputTokens` helper, so a cold run and a warm run can never disagree. Separately, Codex's `cache_write_input_tokens` (new in codex PR #33454) was never read and cache-creation tokens were hardcoded to 0; they are now carved out of the uncached-input bucket and clamped so they can never exceed it. That carve-out happens **only on models whose pricing source publishes a real cache-write rate** — gpt-5.6 and its terra/sol/luna variants charge 1.25x input for a cache write, everything before it charges nothing extra — because CodeBurn fabricates a 1.25x rate when a source omits one, and charging that would have invented a surcharge on gpt-5.5, gpt-5.4, gpt-5.3-codex and gpt-5. On models without an explicit rate the tokens stay in the plain input bucket and the price is unchanged to the cent. The field is new enough that today's impact is $0 on that corpus. Codex sessions re-parse once and the daily cache re-derives once off the warm session cache (a global re-derivation of every day and every provider, since it has no per-provider invalidation); no other provider's numbers move. Days whose Codex transcripts have since aged out are held by the same never-lose guard #1040 relies on: a re-derivation that finds fewer calls than the settled baseline keeps the older, pre-fix (double-counted) total rather than truncating it, so those days do not pick up the repricing until their sources are re-derived with equal or greater evidence. Long-context pricing tiers from the same report are tracked separately in #1076 and the missing `gpt-5.6-codex` snapshot rows in #1077. Thanks @chr-evensen. (#1075) +- **Codex Tok/s no longer counts reasoning tokens twice.** #1075 fixed the same double-count for cost, but `activeGeneratedTokens`/`taskGeneratedTokens` in the Codex parser and `generatedTokens` in the `codex-tps` live-throughput reader still summed `outputTokens + reasoningTokens`, so the displayed Tok/s (and the dashboard's per-model `Tok/s` column) read high for every reasoning-heavy call — display only, no cost impact. All three sites now go through the same `billableOutputTokens('codex', …)` helper #1075 introduced, so the throughput numerator can never drift from the billed one. On a real 51,753-call Codex corpus, cost and every token count are unchanged to the last digit; Tok/s drops **20-42% depending on how reasoning-heavy the model is**: GPT-5.5 37.8 -> 28.1 tok/s (-25.7%), GPT-5.6 Sol 43.2 -> 33.8 (-21.6%), GPT-5.6 Luna 53.5 -> 30.9 (-42.3%), GPT-5.4 68.0 -> 43.6 (-35.9%). `activeGeneratedTokens`/`activeDurationMs`/`toolWaitMs` are stored verbatim in both the Codex result cache and the session cache rather than re-derived on read, so neither self-heals: Codex sessions re-parse once. (#1079) - **Codex calls attributed from session metadata no longer carry a stale model.** The Buffer fast path scanned `session_meta` for the first `"model"` string anywhere in the payload, so a nested `base_instructions.provenance.model` was read as if it were `payload.model` — and since the model is last-writer-wins state, that wrong value was credited to every call before the rollout's first `turn_context` and to every call after any mid-file `session_meta` (29 of 1380 rollouts on one real corpus carry a late `session_meta`, and 57 record usage before any `turn_context`). Direct payload fields are now read depth-aware, which is what the non-fast `JSON.parse` path always did. Codex sessions re-parse once (~9s on a 4 GB rollout corpus) and the daily cache re-derives once off the warm session cache, a global re-derivation of every day and every provider since it has no per-provider invalidation; it moves per-model attribution, and clears any rollup an earlier parse change had left stale. Days whose transcripts have partly aged out are held by the never-lose guard: on a real 110-day cache no day lost value and none disappeared — 100 days came back identical and 9 grok days rose by $19.80 in total. Thanks @timdp. (#1040) - **Codex `session_meta` cwd / session id / originator follow the same depth-1 window as `model`.** #1040 fixed nested `provenance.model`; the compact Buffer path still took the first `cwd`, `session_id`, `originator`, `name`, `forked_from_id` or `model_provider` anywhere in the payload, so a `dynamic_tools[].name` (or any same-named nested key) could steal the top-level field. Those strings now use the existing payload-depth-1 scan. Function-call `name` on other event types is unchanged. Codex sessions re-parse once. (#1045) - **Plan rows for sticker-price presets read as a budget instead of live provider quota.** There is no Grok quota endpoint, so a SuperGrok row was parsed API-equivalent spend divided by the plan's sticker price on a monthly reset — but the TUI labelled that math "plan" and "reset", which next to a client showing xAI's real weekly window read as CodeBurn being wrong. The bars and the arithmetic are unchanged; the words are not. Both the dashboard and the desktop app now say the number is an API-equivalent monthly budget and not a live provider window, in the same wording on both surfaces, and for every preset rather than as a SuperGrok special case. The window is anniversary-based (`plan.resetDay`, settable with `codeburn plan set --reset-day`), so it is called a budget reset rather than a calendar one. The row was also shortened to fit 80 columns: at that width the percentage and the projected month were being truncated away, including on custom plans, whose label carries the provider. diff --git a/src/codex-cache.ts b/src/codex-cache.ts index 70cc8e2b..a8e93d9e 100644 --- a/src/codex-cache.ts +++ b/src/codex-cache.ts @@ -27,7 +27,14 @@ import type { ParsedProviderCall } from './providers/types.js' // output, and cache_write_input_tokens is carved out of the input bucket. This // file stores each call's costUSD and token buckets verbatim, so entries // written by v10 carry the old (overstated) cost and must be re-derived. -export const CODEX_CACHE_VERSION = 11 +// v13: codex throughput fix (#1079) - activeGeneratedTokens was summing +// output + reasoning, the same double-count Fix A removed from cost. This +// file stores activeGeneratedTokens/activeDurationMs/toolWaitMs verbatim (not +// re-derived on read), so v11 entries carry the overstated numerator and must +// re-parse. Not 12: v12 is claimed by feat/core-extraction's own port of this +// throughput feature (PR #1086), so reusing it would let two incompatible +// schemas share a filename. +export const CODEX_CACHE_VERSION = 13 export const CODEX_LEGACY_CACHE_FILE = 'codex-results.json' export function codexCacheFileName(version = CODEX_CACHE_VERSION): string { return `codex-results.v${version}.json` diff --git a/src/codex-throughput.ts b/src/codex-throughput.ts index 4796206d..f4b7ca95 100644 --- a/src/codex-throughput.ts +++ b/src/codex-throughput.ts @@ -1,6 +1,8 @@ import { open, stat } from 'node:fs/promises' import { StringDecoder } from 'node:string_decoder' +import { billableOutputTokens } from './models.js' + export type CodexThroughputPoint = { timestamp: string model?: string @@ -400,7 +402,9 @@ export class CodexThroughputReader { state.previousOutput = total?.output_tokens ?? state.previousOutput state.previousReasoning = total?.reasoning_output_tokens ?? state.previousReasoning } - const generatedTokens = outputTokens + reasoningTokens + // Reasoning is already inside output_tokens (#1075/#1078); same numerator + // as the cost path so live Tok/s can't drift from billed tokens (#1079). + const generatedTokens = billableOutputTokens('codex', outputTokens, reasoningTokens) if (generatedTokens <= 0) return const timestampMs = Date.parse(entry.timestamp) if (!Number.isFinite(timestampMs)) return diff --git a/src/parser.ts b/src/parser.ts index 43137d74..071bde68 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -1768,7 +1768,7 @@ function buildSessionSummary( modelBreakdown[modelKey].tokens.reasoningTokens += call.usage.reasoningTokens if (call.activeDurationMs !== undefined) { modelBreakdown[modelKey].activeDurationMs = (modelBreakdown[modelKey].activeDurationMs ?? 0) + call.activeDurationMs - modelBreakdown[modelKey].activeGeneratedTokens = (modelBreakdown[modelKey].activeGeneratedTokens ?? 0) + (call.activeGeneratedTokens ?? call.usage.outputTokens + call.usage.reasoningTokens) + modelBreakdown[modelKey].activeGeneratedTokens = (modelBreakdown[modelKey].activeGeneratedTokens ?? 0) + (call.activeGeneratedTokens ?? billableOutputTokens(call.provider, call.usage.outputTokens, call.usage.reasoningTokens)) modelBreakdown[modelKey].toolWaitMs = (modelBreakdown[modelKey].toolWaitMs ?? 0) + (call.toolWaitMs ?? 0) } diff --git a/src/providers/codex.ts b/src/providers/codex.ts index 7693c7fa..8c735b7e 100644 --- a/src/providers/codex.ts +++ b/src/providers/codex.ts @@ -879,7 +879,10 @@ function createParser(source: SessionSource, seenKeys: Set, capture?: { const activeMs = durationMs - toolWaitMs if (activeMs <= 0) continue for (const call of pendingTaskCalls) { - const generated = call.outputTokens + call.reasoningTokens + // Reasoning is already inside output_tokens (#1075/#1078); the + // throughput numerator must agree with the cost numerator or + // Tok/s reads high for reasoning-heavy calls (#1079). + const generated = billableOutputTokens('codex', call.outputTokens, call.reasoningTokens) if (generated <= 0) continue call.activeGeneratedTokens = generated call.activeDurationMs = activeMs * (generated / taskGeneratedTokens) @@ -1140,7 +1143,7 @@ function createParser(source: SessionSource, seenKeys: Set, capture?: { ...(pendingLocRemoved ? { locRemoved: pendingLocRemoved } : {}), ...(pendingEditFailed ? { editFailed: pendingEditFailed } : {}), }) - taskGeneratedTokens += outputTokens + reasoningTokens + taskGeneratedTokens += billableOutputTokens('codex', outputTokens, reasoningTokens) pendingTools = [] pendingToolSequence = [] diff --git a/src/session-cache.ts b/src/session-cache.ts index 3a5f9751..91929290 100644 --- a/src/session-cache.ts +++ b/src/session-cache.ts @@ -286,7 +286,11 @@ export const PROVIDER_PARSE_VERSIONS: Record = { // models with an explicit cache-write rate. The bucket move does NOT self-heal // on read (cached entries store the buckets, not the raw event), so cached // sessions must re-parse. - codex: 'mcp-attribution-v5-est-cost-active-timing-mcp-wait-rich-capture-v1-cross-provider-pr-v1-session-meta-model-v1-session-meta-fields-v1-codex-pricing-v1', + // codex-tps-v1 (#1079): activeGeneratedTokens summed output + reasoning, the + // same double-count codex-pricing-v1 removed from cost. Cached entries store + // activeGeneratedTokens/activeDurationMs/toolWaitMs verbatim (cachedCallToApiCall + // passes them through without recomputing), so this does NOT self-heal either. + codex: 'mcp-attribution-v5-est-cost-active-timing-mcp-wait-rich-capture-v1-cross-provider-pr-v1-session-meta-model-v1-session-meta-fields-v1-codex-pricing-v1-codex-tps-v1', cursor: 'composer-anchored-crediting-v1-est-cost', 'cursor-agent': 'workspaceless-transcript-v1', // source-provenance-v1 (#944): CLI sessions were misread as VS Code diff --git a/tests/codex-throughput.test.ts b/tests/codex-throughput.test.ts index 1e0fd4da..fa4fb2b5 100644 --- a/tests/codex-throughput.test.ts +++ b/tests/codex-throughput.test.ts @@ -21,8 +21,38 @@ describe('Codex throughput prototype', () => { const points = await readCodexThroughput(path) expect(points).toHaveLength(2) - expect(points[1]).toMatchObject({ generatedTokens: 50, elapsedSeconds: 5, generatedTokensPerSecond: 10, activeDurationSeconds: 7, activeGeneratedTokensPerSecond: 21.428571428571427, toolWaitSeconds: 3, model: 'gpt-5.6-sol' }) - expect(renderCodexThroughput(points, path)).toContain('21.4 generated tokens/sec') + // Reasoning is a subset of output_tokens (#1075/#1078), not additive: the + // checkpoints report output 80/40 and reasoning 20/10, so the generated + // numerator is output alone (80, then 40), matching the cost path. + expect(points[1]).toMatchObject({ generatedTokens: 40, elapsedSeconds: 5, generatedTokensPerSecond: 8, activeDurationSeconds: 7, activeGeneratedTokensPerSecond: (80 + 40) / 7, toolWaitSeconds: 3, model: 'gpt-5.6-sol' }) + expect(renderCodexThroughput(points, path)).toContain('17.1 generated tokens/sec') + }) + + it('REGRESSION (#1079): does not add reasoning tokens on top of output for Tok/s', async () => { + // Reasoning tokens are a SUBSET of output_tokens (#1075/#1078), not a + // separate bucket. A single checkpoint reporting output=60, reasoning=40 + // must drive Tok/s off 60, not 100 -- summing them would double-count 40 + // tokens that are already inside the 60. If this ever reverts to + // `outputTokens + reasoningTokens`, activeGeneratedTokensPerSecond becomes + // 10 (100 tokens / 10s) instead of 6 (60 tokens / 10s). + const dir = await mkdtemp(join(tmpdir(), 'codeburn-tps-regression-')) + const path = join(dir, 'rollout.jsonl') + await writeFile(path, [ + JSON.stringify({ type: 'session_meta', timestamp: '2026-07-25T00:00:00.000Z', payload: { model: 'gpt-5.5' } }), + JSON.stringify({ type: 'event_msg', timestamp: '2026-07-25T00:00:00.000Z', payload: { type: 'task_started' } }), + JSON.stringify({ type: 'event_msg', timestamp: '2026-07-25T00:00:10.000Z', payload: { type: 'token_count', info: { last_token_usage: { output_tokens: 60, reasoning_output_tokens: 40 }, total_token_usage: { total_tokens: 100, output_tokens: 60, reasoning_output_tokens: 40 } } } }), + JSON.stringify({ type: 'event_msg', timestamp: '2026-07-25T00:00:10.000Z', payload: { type: 'task_complete', duration_ms: 10000 } }), + ].join('\n')) + + const points = await readCodexThroughput(path) + expect(points).toHaveLength(1) + expect(points[0]).toMatchObject({ + outputTokens: 60, + reasoningTokens: 40, + generatedTokens: 60, + taskGeneratedTokens: 60, + activeGeneratedTokensPerSecond: 6, + }) }) it('parses only appended complete lines while watching a growing rollout', async () => { @@ -36,7 +66,9 @@ describe('Codex throughput prototype', () => { await appendFile(path, first.slice(40) + '\n' + second + '\n') const points = await reader.update(path) expect(points).toHaveLength(2) - expect(points[1]).toMatchObject({ generatedTokens: 5, generatedTokensPerSecond: 5 }) + // second checkpoint: output 4 + reasoning 1 -> billable numerator is 4 + // (reasoning already inside output_tokens), not the additive 5. + expect(points[1]).toMatchObject({ generatedTokens: 4, generatedTokensPerSecond: 4 }) }) it('ignores replayed pre-fork checkpoints before estimating new work', async () => { diff --git a/tests/providers/codex.test.ts b/tests/providers/codex.test.ts index 66613174..37325fa2 100644 --- a/tests/providers/codex.test.ts +++ b/tests/providers/codex.test.ts @@ -722,7 +722,9 @@ describe('codex provider - JSONL parsing', () => { reasoningTokens: 20, tools: ['Bash'], activeDurationMs: 7000, - activeGeneratedTokens: 120, + // Reasoning (20) is a subset of output_tokens (100), not additive + // (#1075/#1078/#1079): the billable/throughput numerator is 100, not 120. + activeGeneratedTokens: 100, toolWaitMs: 3000, }) })