Skip to content

Optimize Codex cost refresh policy for large history windows #1392

@TheAngryPit

Description

@TheAngryPit

Context

While investigating local CodexBar menu/CPU lag, I found a narrower cost-history refresh problem that may be contributing to the broader menu performance reports in #1321, #1360, and #1371.

Local setup/signal:

  • CodexBar 0.32.5 (80)
  • Cost summary enabled
  • Cost history window set to 365 days
  • ~/.codex/sessions aggregate shape: ~13 GB across ~403 JSONL files
  • A sample captured during a high-CPU spike pointed at the background cost path, not the main menu drawing path:
    • UsageStore.refreshTokenUsage
    • CostUsageFetcher.loadTokenSnapshot
    • CostUsageScanner.loadCodexDaily
    • CostUsageScanner.scanCodexFile
    • CostUsageJsonl.scan

This is related to #1016, but I think the shape is different enough to ask separately. #1016 was about raw JSONL scanner throughput / possible Swift vs Rust/SIMD optimization, and was closed as not worth pursuing without a real bottleneck. This issue is not proposing a scanner rewrite. It is about refresh policy for large history windows.

Proposal

For large windows such as 365 days, make the steady-state refresh cheaper by separating:

  • the selected display/history window, e.g. "show me 365 days"
  • from the normal refresh cost, e.g. "only process new or modified local activity"

Possible behavior:

  • Run a full scan when the cost history window is first enabled or expanded.
  • Run a full scan when the Codex sessions root changes.
  • Run a full scan / rebuild when the cache schema, parser source hash, pricing key, or other correctness-critical invalidator changes.
  • During normal background refreshes, use the existing cache and scan only new or modified session files.
  • Detect modified sessions by file metadata/cache state, not just by date, so resumed/forked/older sessions are still handled.
  • Optionally run a periodic full reconciliation, for example weekly, to catch any drift.

This would keep 365-day history useful without making every normal refresh scale like a large history rebuild.

Questions

  • Does this refresh-policy direction fit how you want cost history to behave?
  • Should manual "Refresh Now" force a full reconciliation, or should it also stay delta-based unless a setting/root/cache invalidator changed?
  • If the direction sounds reasonable, can I claim this and open a small focused PR?

Intended PR scope if accepted

  • Keep this to Codex cost-history refresh/cache policy.
  • No Rust/SIMD helper.
  • No broad menu-rendering refactor.
  • No provider/networking changes.
  • Add focused tests for:
    • cached steady-state refresh
    • new session file
    • modified existing session file
    • history-window expansion
    • cache invalidation / pricing or parser-key change

The goal would be a conservative performance fix that preserves correctness and is easy to review.

Suggested labels, if maintainers agree: enhancement, priority:medium, area:performance,
area:usage-accuracy, provider:codex, needs-design.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Normal priority bug or improvement with limited blast radius.clawsweeper:needs-live-reproClawSweeper needs live local, crabbox, or manual validation to confirm this issue.clawsweeper:needs-maintainer-reviewClawSweeper marked this issue as needing maintainer review before automation.clawsweeper:needs-product-decisionClawSweeper marked this issue as needing a product or behavior decision.clawsweeper:no-new-fix-prClawSweeper does not recommend queueing a new automated fix PR for this issue.impact:otherThis issue has meaningful maintainer-visible impact outside the owned taxonomy.issue-rating: 🐚 platinum hermitGood issue quality with a plausible reproduction path needing some confirmation.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions