Skip to content

perf: cache Codex cost session scans#56

Merged
moose-lab merged 2 commits into
mainfrom
codex/issue-49-cost-cache
Jun 18, 2026
Merged

perf: cache Codex cost session scans#56
moose-lab merged 2 commits into
mainfrom
codex/issue-49-cost-cache

Conversation

@moose-lab

Copy link
Copy Markdown
Owner

Summary

  • add an in-memory Codex cost scan cache keyed by session file path, mtime, and size
  • prune deleted session files from the cache and parse Codex sessions with bounded concurrency
  • add regression coverage for cache hits, modified files, and deleted-file pruning

Fixes #49

Verification

  • TZ=Asia/Shanghai node --test --import tsx --test-reporter spec src/core/tests/cost-tracker.test.ts
  • git diff --check
  • bun run typecheck
  • TZ=Asia/Shanghai bun run test
  • pre-commit quality gate during commit: git diff --cached --check && bun run typecheck && TZ=Asia/Shanghai bun run test

@claude

claude Bot commented Jun 18, 2026

Copy link
Copy Markdown

Claude Code is working…

I'll analyze this and get back to you.

View job run

@claude

claude Bot commented Jun 18, 2026

Copy link
Copy Markdown

Claude finished @moose-lab's task in 2m 17s —— View job


Findings

  • [should-fix] pruneCodexScanCache uses a hardcoded / separator (src/core/cost-tracker.ts:392-393). The prefixes are built with a literal /:

    const prefixes = roots.map((root) => (root.endsWith("/") ? root : `${root}/`));

    path.join produces \-separated paths on Windows, so key.startsWith(prefix) never matches there — deleted files are never pruned and the cache leaks indefinitely. Use path.sep or root + path.sep to be portable. Fix this →

  • [should-fix] mapWithConcurrency propagates worker rejections unchecked (src/core/cost-tracker.ts:409-414). If the fn callback throws rather than returning null, the for(;;) loop re-throws out of the async worker, causing Promise.all(workers) to reject. scanCodexSessionCached currently catches everything so this is safe today, but the helper's contract is fragile — a future caller that lets an error escape would silently drop all in-flight results. A .catch(() => null) guard on the inner await fn(...) would seal this. Fix this →

  • [nit] Stale entries leak when mixing codexSessionsDir values (src/core/cost-tracker.ts:221-224). pruneCodexScanCache only evicts entries under the directories scanned in the current call. If buildCostReport() (default dir) and buildCostReport({ codexSessionsDir: "/custom" }) are interleaved in the same process, cache entries from each call's dir are never pruned by the other. In practice the cache is bounded by the number of real session files, so this won't cause a memory blowup, but it's worth a comment.

  • [nit] Unnecessary module cast in tests (src/core/__tests__/cost-tracker.test.ts:74-77, 103-106, 133-136). clearCostTrackerCache and getCostTrackerCacheStats are regular named exports — they're already in typeof costTracker. The as typeof costTracker & { ... } intersection cast (and the optional-? variant in the first test) is dead code. Import them directly alongside the other named imports to remove the ambiguity.

Verdict: APPROVE

The caching logic is correct for the common case: mtime+size key is sufficient, the pruning correctly ignores files that disappeared between listing and stat (they don't get added to seenFiles so they're evicted), and concurrent workers are safe under JS's single-threaded event loop. The three new tests cover the meaningful branches (hit, miss-on-change, prune-on-delete). The two should-fix items are low-risk edge cases that don't affect typical macOS/Linux usage but are worth addressing before this pattern is copied elsewhere.

@moose-lab moose-lab merged commit 5251feb into main Jun 18, 2026
4 checks passed
@moose-lab moose-lab deleted the codex/issue-49-cost-cache branch June 18, 2026 04:05
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.

perf: Cost & Usage 页在大量历史会话下会卡死 — 成本报表扫描未走增量缓存

1 participant