Skip to content

perf(usage): skip unchanged cache rewrites - #16220

Closed
sanshengai wants to merge 1 commit into
stablyai:mainfrom
sanshengai:perf/skip-unchanged-usage-cache-writes
Closed

sanshengai wants to merge 1 commit into
stablyai:mainfrom
sanshengai:perf/skip-unchanged-usage-cache-writes

Conversation

@sanshengai

Copy link
Copy Markdown

Summary

  • let each built-in usage scanner report whether any per-source projection was reparsed, added, removed, or reclaimed
  • skip the durable cache snapshot when all source projections were reused unchanged
  • still persist worktree-attribution changes and recovery from a previous scan error
  • keep providers/plugins that do not supply the new optional signal on the conservative existing write path

Why

Usage refreshes run on a five-minute freshness window. Even when every transcript/database is unchanged and the scanners only reuse cached projections, the lifecycle currently serializes and atomically rewrites the entire provider JSON. On the measured profile that means up to 61.8 MiB across Codex + Claude per no-op refresh.

After this change, a no-op refresh updates its in-memory completion time but performs zero durable file opens or renames. A real source/worktree/error-state change still uses the existing durable snapshot writer.

Related performance umbrella: #16211
Complementary startup-memory change: #16219

This PR is independently mergeable against main; it does not require #16219.

Compatibility

  • sourceProjectionChanged is optional in the shared provider contract
  • missing signal means "write", preserving current behavior for plugins
  • source-count differences and any parsed/reclaimed source report a change
  • worktree fingerprint changes override a false source signal
  • the first successful refresh after an error persists the cleared error

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, 107 tests passed
  • lifecycle test asserts an unchanged refresh performs zero file opens and zero renames
  • provider tests cover changed -> stable -> changed transitions for Codex, Claude, and OpenCode
  • pnpm exec oxlint ...changed TypeScript files...
  • 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 source deletion/reclaim, worktree-attribution invalidation, plugin compatibility, error recovery, and durable-write semantics.

@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: 82c1cc8d-b83d-4cd1-956a-3a2455715f12

📥 Commits

Reviewing files that changed from the base of the PR and between 4ee41fe and 51b4657.

📒 Files selected for processing (12)
  • src/main/claude-usage/scanner-scan.test.ts
  • src/main/claude-usage/scanner.ts
  • src/main/claude-usage/store.test.ts
  • src/main/codex-usage/scanner-paths.test.ts
  • src/main/codex-usage/scanner.ts
  • src/main/codex-usage/store-test-harness.ts
  • src/main/opencode-usage/scanner.test.ts
  • src/main/opencode-usage/scanner.ts
  • src/main/opencode-usage/store.test.ts
  • src/main/usage/usage-provider-contract.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; 6 remain after this review.


📝 Walkthrough

Walkthrough

Claude, Codex, and OpenCode scanners now return sourceProjectionChanged. Shared scan result types and test fixtures include this field. The usage provider applies scan results to state on every refresh, but persists successful results only when the source projection or worktree fingerprint changes, or when a previous scan error is cleared. Tests cover changed projections, cached rescans, and unchanged refreshes.

Merge Risk: ⚪ Minimal · up to 51b46

The change skips durable cache rewrites only when usage data is unchanged while preserving writes for source, worktree, plugin, and error-state changes; no actionable merge-blocking risk remains after 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 skipping unchanged usage cache rewrites, which is the primary change.
Description check ✅ Passed The description clearly explains the change, rationale, compatibility behavior, linked issues, validation, and AI assistance.
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.

@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