Skip to content

Fix duplicated Codex token counting in forked sessions#627

Open
sszzz830 wants to merge 2 commits intosteipete:mainfrom
sszzz830:main
Open

Fix duplicated Codex token counting in forked sessions#627
sszzz830 wants to merge 2 commits intosteipete:mainfrom
sszzz830:main

Conversation

@sszzz830
Copy link
Copy Markdown

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 cumulative token_count history. The existing scanner treated the forked session as fully independent and counted that inherited prefix again, which inflated token and cost totals.

What changed

  • Read session_meta.forked_from_id from Codex session logs
  • Track raw token_count entries per session during scanning
  • When a child session is forked from a parent, compare the child's raw prefix against the parent's existing prefix
  • Skip only the inherited prefix that exactly matches the parent history
  • Continue counting all new usage generated after the fork in both parent and child sessions
  • Bump the Codex cost usage cache artifact version from v2 to v3 so previously overcounted cached results are invalidated

Why this approach

This is intentionally strict and narrow:

  • It only applies to explicit Codex forks with forked_from_id
  • It does not dedupe unrelated sessions that merely happen to have similar totals
  • It preserves post-fork usage correctly for both branches
  • It avoids over-deduping by only removing the exact inherited prefix

Validation

Validated against forked-session scenarios to confirm that:

  • inherited parent usage is not counted twice
  • new child usage after the fork is still counted
  • new parent usage after the fork is still counted
  • incremental rescans still work correctly
  • the Codex cache now rolls to codex-v3.json

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment on lines +650 to +653
let parsed = Self.parseCodexFile(
fileURL: fileURL,
range: range,
parentUsageEntries: parentUsageEntries)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant