Repository navigation
fix(codex-usage): deduplicate fork-copied token events across timestamp rewrites - #19152
MeroZemory wants to merge 3 commits into
Conversation
…mp rewrites Codex rewrites copied token_count timestamps to the fork creation time when sessions are forked, causing timestamp-based deduplication keys to treat copied turns as new unique usage events. Drop the raw timestamp from buildCodexUsageEventKey and key deduplication on monotonic token usage tuples. Bump CODEX_USAGE_SCHEMA_VERSION to 6 to invalidate cached v5 entries. Fixes stablyai#19139
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Advanced Run ID: 📒 Files selected for processing (5)
🚧 Files skipped from review as they are similar to previous changes (2)
Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review. 📝 WalkthroughWalkthroughThe Codex usage schema version increases from 5 to 6. The parser now tracks root thread and turn identity. Usage event keys use this identity and token usage tuples instead of raw timestamps. Scanner tests cover fork deduplication and unrelated sessions. Persistence tests update schema version expectations. Priority: ⬆️ High — Impact reflects high issue severity. Severity of issue fixed: High Merge Risk: ⚪ Minimal · up to Codex usage aggregation now avoids re-counting copied fork history while preserving separate usage for unrelated sessions, and cached aggregates are rebuilt under the updated schema. 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Team
Run ID: 441d4ab5-2e36-4ed7-9ec1-bedc0cc7765a
📒 Files selected for processing (5)
src/main/codex-usage/codex-usage-provider.tssrc/main/codex-usage/codex-usage-record-parser.tssrc/main/codex-usage/codex-usage-token-delta.tssrc/main/codex-usage/scanner-paths.test.tssrc/main/codex-usage/store-persistence.test.ts
Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.
There was a problem hiding this comment.
Important
The dedup key now carries no identity component at all, so identical usage tuples from unrelated sessions will collide and silently undercount — the inverse of the overcount this PR fixes. Details inline.
Reviewed changes — this PR reworks Codex usage-event deduplication so fork-copied records survive timestamp rewrites, plus a schema bump and a regression test.
- Key change —
buildCodexUsageEventKeydrops the raw timestamp and keys only on thetotalUsage/lastUsagetuples. - Schema bump —
CODEX_USAGE_SCHEMA_VERSION5 → 6 invalidates cached v5 aggregates so history re-aggregates. - Regression test —
scanner-paths.test.tsasserts fork-copied events with rewritten timestamps dedupe to 22 total tokens; the assertion is exact and would fail under the v5 key.
DeepSeek Pro (free via Pullfrog for OSS) | 𝕏
There was a problem hiding this comment.
ℹ️ No critical issues — the prior identity concern is addressed; one minor naming nit inline.
Reviewed changes — this follow-up threads session and turn identity into the Codex usage-event dedup key so unrelated sessions no longer collide, plus regression coverage.
- Resolved
rootThreadId— the parser now derives a fork-stable id (session_id→forked_from_id→sessionId) and carries it on the parse context. - Added turn identity —
currentTurnIdfromturn_contextisolates independent fork turns that produce identical usage tuples. - Added last-only occurrence identity — records without cumulative totals key on
[sessionId, timestamp, usageEventIndex]so equal successive deltas stay countable. - Regression tests — new
codex-usage-record-identity.test.tsplus threescanner-paths.test.tscases assert unrelated-session and independent-turn isolation with exact token totals.
DeepSeek Pro (free via Pullfrog for OSS) | 𝕏
|
|
||
| export function buildCodexUsageEventKey( | ||
| timestamp: string, | ||
| rootThreadId: string | null | undefined, |
There was a problem hiding this comment.
The first parameter is named rootThreadId, but the parser now passes a JSON.stringify-ed array of [rootThreadId, currentTurnId, last-only occurrence] (see codex-usage-record-parser.ts:163), not a raw thread id. Rename it (e.g. identity) so the signature no longer misstates what the key is built from.
|
Thanks @MeroZemory — your diagnosis was right (Codex rewrites copied |
Codex usage no longer double-counts forked sessions: forks copy the parent's token_count events with fresh timestamps, so cumulative events are now keyed on turn id + totals (timestamp fallback when no turn id); the turn id is kept in the incremental-scan checkpoint and the usage cache is rebuilt once. Fixes #19139 Supersedes #19152 Co-authored-by: Jio Kim <26668612+MeroZemory@users.noreply.github.com>

ELI5
Forking a Codex conversation can copy its token events with new timestamps. Count copied history once while keeping unrelated conversations and independent turns separate.
What Changed
Use the recorded shared root and turn identity with the cumulative/last usage tuples, without the rewritten event timestamp. Fall back to an explicit parent ID or the session's own ID for older logs. Last-only records receive an occurrence identity so equal successive deltas remain countable. Invalidate the old usage cache (schema 6).
Why
The original tuple-only fix could undercount independent sessions. This follow-up addresses that review finding and adds scanner and parser regression coverage. Logs with neither stable lineage nor turn identity cannot prove that two histories are copies; legacy histories without that evidence may remain counted separately.
Linked Issue
Fixes #19139
Visual Proof
N/A — this changes usage event ownership and cache invalidation, not rendered layout. Synthetic scanner fixtures assert exact totals before and after fork copying.
Testing
AI Disclosure
Developed and reviewed with Codex (GPT-6).
Review
Follow-up isolates unrelated roots, legacy session IDs, independent turns, and repeated last-only events. Cache ownership/deletion regression tests also pass.
Agent skill upstream boundary