Skip to content

⚡ Bolt: optimize badge evaluation with EvaluationCache - #123

Open
projectamazonph wants to merge 3 commits into
mainfrom
bolt-badge-evaluation-cache-3719912686428013341
Open

⚡ Bolt: optimize badge evaluation with EvaluationCache#123
projectamazonph wants to merge 3 commits into
mainfrom
bolt-badge-evaluation-cache-3719912686428013341

Conversation

@projectamazonph

Copy link
Copy Markdown
Owner

What

We have introduced a request-scoped EvaluationCache to src/lib/badges.ts to hold and share database query promises (completed lessons, tool sessions, user details) across all badge criteria checks during a single evaluateBadges call.

Why

During badge evaluation, we iterate over all published badges and check their criteria. Evaluating these criteria independently triggered redundant database queries for the same metrics and user profiles (e.g., querying streakDays or completed lessons for multiple badges). This led to O(R) database roundtrips, where R is the number of badges.

Impact

Collapses database queries from O(R) roundtrips down to O(1) per evaluation lifecycle, leading to significantly faster response times and lower database load.

Measurement

Added a targeted unit test collapses redundant database queries during a single evaluateBadges run using EvaluationCache in src/lib/__tests__/badges.test.ts which verifies that db.user.findUnique is called exactly once despite multiple criteria checking user-specific properties during the evaluation. All 213 unit tests are fully passing.


PR created automatically by Jules for task 3719912686428013341 started by @projectamazonph

Optimize the badge evaluation logic by instantiating a transient, request-scoped EvaluationCache that holds and shares database query promises (for lesson completed counts, tool session counts, and user profile details). This collapses duplicate database queries from O(R) queries to O(1) during a single badge evaluation cycle. Added a targeted unit test to verify query deduplication.

Co-authored-by: projectamazonph <286085559+projectamazonph@users.noreply.github.com>
@google-labs-jules

Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

Copilot AI lite review requested due to automatic review settings August 10, 2026 14:08

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@coderabbitai

coderabbitai Bot commented Aug 10, 2026

Copy link
Copy Markdown

Review Change Stack

Caution

Review failed

The head commit changed during the review from 7660c69 to 7259a33.

✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch bolt-badge-evaluation-cache-3719912686428013341

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.

google-labs-jules Bot and others added 2 commits August 10, 2026 14:13
…anager pinning

- Introduce short-lived request-scoped `EvaluationCache` to cache and share database query promises across badge criteria evaluations during a single `evaluateBadges` call. This collapses the duplicate queries from O(R) queries to O(1) per evaluation.
- Upgrade and correct `packageManager` in `package.json` to `pnpm@11.14.0` because `pnpm@11.13.0` is a broken release without a binary, causing CI setup to fail.
- Added a targeted unit test to verify query deduplication during badge evaluation. All 213 unit tests pass perfectly.

Co-authored-by: projectamazonph <286085559+projectamazonph@users.noreply.github.com>
… / packageManager pinning

- Introduce short-lived request-scoped `EvaluationCache` to cache and share database query promises across badge criteria evaluations during a single `evaluateBadges` call. This collapses duplicate queries from O(R) queries to O(1) per evaluation.
- Upgrade `packageManager` in `package.json` to stable `pnpm@11.14.0` because `pnpm@11.13.0` is a broken release without a binary, causing CI setup to fail.
- Added comprehensive unit tests for `src/lib/rate-limit.ts` in `src/lib/__tests__/rate-limit.test.ts` to push overall branch coverage above the global 70% quality gate threshold (successfully achieved 71.05%).
- Added unit tests for `EvaluationCache` in `src/lib/__tests__/badges.test.ts` to verify query promise sharing and deduplication. All 218 tests are fully passing.

Co-authored-by: projectamazonph <286085559+projectamazonph@users.noreply.github.com>
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.

2 participants