Skip to content

fix(server): include Codex archived_sessions in usage scan - #11896

Open
bulbulogludemir wants to merge 1 commit into
pingdotgg:mainfrom
bulbulogludemir:fix/codex-archived-sessions-usage
Open

bulbulogludemir wants to merge 1 commit into
pingdotgg:mainfrom
bulbulogludemir:fix/codex-archived-sessions-usage

Conversation

@bulbulogludemir

@bulbulogludemir bulbulogludemir commented Sep 15, 2026

Copy link
Copy Markdown

Codex moves rolled-out sessions to archived_sessions, but the usage scanner only reads sessions/, so any archived history silently contributes zero cost to the Usage page.

This change:

  • resolves archived_sessions alongside sessions for every Codex home (deduped per directory, so aliased/shadow homes still scan once),
  • gives Codex usage events a stable dedupeKey (session, timestamp, model, token totals) so a rollout present in both directories is charged exactly once; genuinely different sessions/events keep distinct keys,
  • bumps USAGE_SCAN_CACHE_VERSION 3 → 4 because v3 Codex records carry null keys and would double-count archived copies.

Tests:

  • usageTranscripts.test.ts: identical events parsed from two copies share one dedupeKey.
  • UsageService.test.ts: an identical archived rollout copy is scanned (sources 4 → 5, files 4 → 5) with totals unchanged.
  • usageScanCache.test.ts: previous-version rejection now targets v3.
  • apps/server typecheck passes; usage test files pass (usageTranscripts/usageScanCache/usageAggregation green, modified UsageService account test green — 2 pre-existing it.live timeouts in that file also fail on clean main in this environment).

Summary by CodeRabbit

  • Bug Fixes
    • Usage reporting now includes archived Codex sessions, improving completeness when sessions are moved or copied.
    • Archived and active copies of the same session are counted only once, preventing duplicate usage charges.
    • Usage scan data is refreshed safely to account for updated duplicate-detection behavior.
  • Tests
    • Added coverage confirming consistent identification of copied Codex rollout records and correct usage totals.

Codex moves rolled-out sessions to archived_sessions, but the usage
scanner only read sessions/, so archived history never contributed
cost. Scan both directories and give Codex events a stable dedupeKey
so copies across the two are charged once. Bump the scan cache
version since v3 Codex records have null keys.
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, add credits to your account and enable them for code reviews in your settings.

@github-actions github-actions Bot added vouch:unvouched PR author is not yet trusted in the VOUCHED list. size:M 30-99 changed lines (additions + deletions). labels Sep 15, 2026
@macroscopeapp

macroscopeapp Bot commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

Approvability

Verdict: Not approved

Macroscope's review found this PR not approvable — The PR changes production usage metering by adding archived Codex sessions to priced summaries and changing cross-file duplicate charging behavior. It is narrowly scoped and tested, but metering changes require human review.

You can add or adjust custom eligibility rules. Learn more.

@coderabbitai

coderabbitai Bot commented Sep 15, 2026

Copy link
Copy Markdown

Review Change StackReview Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Advanced

Run ID: 5b4e44c6-fa7a-4896-892b-99dbb8e2d108

📥 Commits

Reviewing files that changed from the base of the PR and between bf3be75 and 846d0d0.

📒 Files selected for processing (6)
  • apps/server/src/usage/UsageService.test.ts
  • apps/server/src/usage/UsageService.ts
  • apps/server/src/usage/usageScanCache.test.ts
  • apps/server/src/usage/usageScanCache.ts
  • apps/server/src/usage/usageTranscripts.test.ts
  • apps/server/src/usage/usageTranscripts.ts

Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.


📝 Walkthrough

Walkthrough

Codex usage scanning now includes archived_sessions. Codex records with session IDs receive stable dedupe keys. The scan-cache format version increases, and tests cover duplicate rollout files and stale cache entries.

Changes

Codex usage deduplication

Layer / File(s) Summary
Stable Codex record identity
apps/server/src/usage/usageTranscripts.ts, apps/server/src/usage/usageTranscripts.test.ts
parseCodexLine generates a composite dedupeKey for records with session IDs. Tests verify identical events produce equal non-null keys.
Archived session scanning
apps/server/src/usage/UsageService.ts, apps/server/src/usage/UsageService.test.ts
resolveTranscriptDirs scans Codex archived_sessions. Tests verify both rollout copies are scanned while output tokens remain charged once.
Scan-cache version invalidation
apps/server/src/usage/usageScanCache.ts, apps/server/src/usage/usageScanCache.test.ts
The cache format version increases from 3 to 4. Tests reject documents using the previous version.

Priority: ⬇️ Low

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Suggested reviewers: t3dotgg

Merge Risk: ⚪ Minimal · up to 846d0

The archived-session scan, deduplication, and cache invalidation changes have no identified merge-blocking risk.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the primary change: including Codex archived_sessions in the server usage scan.
Description check ✅ Passed The description clearly explains what changed, why it changed, the deduplication approach, cache-version impact, and test coverage. It does not use the template headings or include the checklist, but …
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 6 files.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands.

@bulbulogludemir

Copy link
Copy Markdown
Author

Ready for human review (per the Macroscope approability note — this touches usage metering, so auto-approval is off by policy).

Verification summary:

  • New behavior covered by tests: identical Codex rollout copies across sessions/ and archived_sessions/ are scanned but charged once; archive-only sessions now contribute usage; moving a rollout into archives keeps totals stable.
  • apps/server typecheck passes; new/updated tests in usageTranscripts, usageScanCache and the modified UsageService account test pass. (2 pre-existing it.live timeouts in UsageService.test.ts also fail on clean main in this environment, unrelated to this change.)
  • Also validated the parser/aggregator against ~2.5k real Codex transcript files (sessions + archives): 0 read errors.

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

Labels

size:M 30-99 changed lines (additions + deletions). vouch:unvouched PR author is not yet trusted in the VOUCHED list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant