Skip to content

perf(usage): lazy-load source cache ledgers - #16219

Closed
sanshengai wants to merge 1 commit into
stablyai:mainfrom
sanshengai:perf/lazy-load-usage-cache-dedupe
Closed

sanshengai wants to merge 1 commit into
stablyai:mainfrom
sanshengai:perf/lazy-load-usage-cache-dedupe

Conversation

@sanshengai

Copy link
Copy Markdown

Summary

  • split each provider's cold source/ownership ledger into a schema- and worktree-bound sidecar cache
  • keep only hot scan state, sessions, and daily aggregates in the startup cache
  • load the cold ledger only when a stale/forced scan actually needs incremental history, then release it after persistence
  • migrate legacy combined caches atomically without losing dedupe ownership; mismatched/corrupt sidecars safely fall back to a rebuild

Why

The usage stores are constructed unconditionally during app startup. Their previous synchronous JSON.parse(readFileSync(...)) loaded every processed-file ownership key even when the usage UI was never opened. On a real active Orca profile:

provider current startup JSON partitioned hot JSON
Codex 41.40 MiB 1.47 MiB
Claude 20.40 MiB 0.78 MiB

The first upgraded launch still performs a one-time legacy parse so it can migrate safely. Subsequent launches read only the small hot cache unless a scan runs.

Related performance umbrella: #16211

Durability and compatibility

  • the cold sidecar includes both schemaVersion and worktreeFingerprint
  • cold state is committed before the matching hot projection
  • a crash or mismatch causes a full source rescan, not stale attribution
  • existing atomic durable writers, generation vetoes, temp cleanup, and quit flushing cover both files
  • persisted report/API shapes remain unchanged (processedFiles / processedDatabases stays an empty array in the hot file)

Validation

  • pnpm exec vitest run --config config/vitest.config.ts src/main/usage src/main/codex-usage src/main/claude-usage src/main/opencode-usage — 21 files, 108 tests passed
  • pnpm exec oxlint src/main/usage/usage-provider-source-cache.ts src/main/usage/usage-provider-store-lifecycle.ts src/main/usage/usage-provider-store-lifecycle.test.ts
  • pnpm run typecheck:node
  • pnpm run check:max-lines-ratchet
  • git diff --check

pnpm test itself cannot load the optional windows-native-registry native module under the local Node 24.19.0 environment, so the same repository Vitest config was invoked directly.

AI assistance

This contribution was developed with AI assistance and manually reviewed for migration ordering, schema/fingerprint invalidation, generation races, failure fallback, and quit flushing.

@coderabbitai

coderabbitai Bot commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: db7629d3-1c46-4f8d-a45c-6bd37b69b0e3

📥 Commits

Reviewing files that changed from the base of the PR and between 4ee41fe and 47cd996.

📒 Files selected for processing (3)
  • src/main/usage/usage-provider-source-cache.ts
  • src/main/usage/usage-provider-store-lifecycle.test.ts
  • src/main/usage/usage-provider-store-lifecycle.ts

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


📝 Walkthrough

Walkthrough

The usage provider now stores source data in a separate cache file. Cache loading validates schema version, worktree fingerprint, source key, and source-array shape. Legacy inline source data migrates asynchronously to the partitioned cache. Hot-state snapshots exclude source data. Scans load prior sources through resident, legacy, or partitioned state and persist scan results separately. Tests cover lazy loading, migration, two-file persistence, and empty processedSources projections.

Merge Risk: ⚪ Minimal · up to 47cd9

The change lazy-loads cold usage ledgers while preserving cache migration and persistence behavior; no actionable merge-blocking risk remains beyond normal checks and review.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: lazy-loading usage source cache ledgers.
Description check ✅ Passed The description explains the change, motivation, compatibility behavior, linked issue, validation results, and AI assistance in sufficient detail.
Docstring Coverage ✅ Passed Docstring check was indeterminate for this PR — some files could not be analyzed in time. Not blocking.
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.

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.

❤️ Share

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

Warning

⚠️ This pull request shows signs of AI-generated slop (defensive_cruft). It has been flagged by CodeRabbit slop detection and should be reviewed carefully.

@sanshengai

Copy link
Copy Markdown
Author

Closing as stale. main has moved on substantially since this branch was opened on Aug 24, and I am no longer following it. Happy to reopen a rebased, single-change PR if maintainers still want the fix.

@sanshengai sanshengai closed this Sep 11, 2026
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