Fix duplicated Codex token counting in forked sessions#627
Fix duplicated Codex token counting in forked sessions#627sszzz830 wants to merge 2 commits intosteipete:mainfrom
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: cf87e675d4
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| let parsed = Self.parseCodexFile( | ||
| fileURL: fileURL, | ||
| range: range, | ||
| parentUsageEntries: parentUsageEntries) |
There was a problem hiding this comment.
Invalidate fork-deduped child cache when parent disappears
This code computes child usage with parentUsageEntries and then caches the deduped days, but it does not persist any dependency on the parent session/file. On later refreshes, unchanged child files take the mtime/size fast path and are not reparsed, so if the parent file is removed from the scan set, the end-of-scan cleanup subtracts the parent totals while the child still excludes the inherited prefix, causing an undercount until the child changes or a force rescan is run.
Useful? React with 👍 / 👎.
Summary
This fixes double counting for Codex token usage when a session is forked.
Forked Codex sessions get a new
session_id, but they inherit the parent session's cumulativetoken_counthistory. The existing scanner treated the forked session as fully independent and counted that inherited prefix again, which inflated token and cost totals.What changed
session_meta.forked_from_idfrom Codex session logstoken_countentries per session during scanningv2tov3so previously overcounted cached results are invalidatedWhy this approach
This is intentionally strict and narrow:
forked_from_idValidation
Validated against forked-session scenarios to confirm that:
codex-v3.json