Count Codex token_usage_record rows and skip repeated token totals - #39
Merged
Merged
Conversation
Repeated token_count snapshots and the post-compaction phantom were added as new usage. token_usage_record lines, including compaction inference, were dropped because usage sits past the header window. Historical sessions keep their stored totals until those files are rescanned. The Codex scan-cache parser revision is unchanged. Co-authored-by: Dmitry Starkov <21260939+starkdmi@users.noreply.github.com>
…count Records are keyed by the same session rule as token_count, so a fork's copied parent records still pair. A compacted line ends the pairing window, pairing compares normalized counts, and a paired token_count's quota sample links to a record emitted outside any turn. A counter reset without last_token_usage counts the new total, standalone records get their own event kind, and the archive quota scan follows session_meta.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Behaviour
Codex rollout parsing now matches CLI 0.147–0.155 usage lines.
token_countsnapshots no longer inflate totals. Whentotal_token_usageis present and equals the previous total in the same file, that line contributes no usage. A second copy for anotherlimit_id(for examplepremium) is the same case. The line still produces a quota observation, with no usage sample. A missingtotal_token_usagestill useslast_token_usage. A smaller total (fork or resume reset) still counts as new usage.token_countaftercompactedis not billed. The compaction fixture repeatstotal_tokens11081643 with a differentlast_token_usage(total_tokens17983). That 17983 is no longer added. Advancing last-usage totals in that fixture sum to 390224.token_usage_recordis a usage source, including compaction. The line is classified before the headless-usage fallback, including when"usage"sits past the 256-byte header. Before a file’s first record, usage still comes fromtoken_count. From the first record on, onlypayload.usageon records is counted; latertoken_countlines supply quota and context only. Compaction inference that never appears on atoken_countis therefore included.turn_token_usageandthread_token_usageare not summed.cache_write_input_tokens/cacheWriteInputTokensare an inclusive subset of input, clamped the same way as cached input.turn_id,root_turn_id, andresponse_id. For a sub-agent file,session_id(the parent) androot_turn_idare available so a later change can roll cost up to the parent turn. No store columns were added.Historical data
Stored history is corrected only when those rollout files are rescanned.
CODEX_SCAN_CACHE_PARSER_REVISIONis unchanged, so this does not force a full-history resync.Tests
Synthetic fixtures under
tests/fixtures/codex/usage-record/cover a mixed-era file (legacytoken_count, then records, including a compaction record) and a sub-agent file.cargo fmt --check,cargo clippy --workspace --all-targets -- -D warnings, andcargo test --workspacepassed.