Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@
## 2026-07-16 - [O(N*M) Nested Loop Lookups in Grading Engines]
**Learning:** In interactive scenarios (such as Bid Elevator and STR Triage), grading engines frequently iterate over user decisions and match them against scenario properties (like keywords or search terms). Performing `array.find()` inside loop bodies or filter predicates results in costly O(N*M) lookups.
**Action:** Convert arrays to `Map` lookups before entering loops/nested scans. Mapping keys once in O(M) time enables O(1) lookups during execution, transforming the time complexity of the grading logic to O(N + M).

## 2026-07-17 - [O(R) Database Roundtrips in Multi-Rule Evaluation]
**Learning:** Evaluators checking multiple rules (such as checkCriteria in the badge engine) can generate redundant database queries for identical user records or resource aggregates when looping over each rule. These can be optimized with a transient local cache (caching query promises rather than resolved values) during the evaluation lifecycle, safely collapsing database roundtrips from O(R) to O(1) where R is the number of rules.
**Action:** Create a transient `EvaluationCache` to hold and share database query promises across criteria checks during a single `evaluateBadges` call.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,5 @@
"eslint --fix"
]
},
"packageManager": "pnpm@11.13.0+sha512.88d94724d8f2e6c186744a5584c6e59ecac869ec7ba15e9cb4cd628e8dc7066820b2481d8ee3b51ea8da323a7378068aa58c556a3720d32b7c20a051d088363a"
"packageManager": "pnpm@11.11.0"
}
91 changes: 90 additions & 1 deletion src/lib/__tests__/badges.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { db } from '@/lib/db';
import { evaluateBadges } from '@/lib/badges';
import { evaluateBadges, EvaluationCache } from '@/lib/badges';
import { BadgeCriteria } from '@/lib/badges';
import { CourseTier } from '@/lib/enums';

Expand Down Expand Up @@ -92,4 +92,93 @@ describe('badges.ts', () => {
const result = await evaluateBadges('user-1', { trigger: 'login' });
expect(result.awarded).toEqual([]);
});

describe('EvaluationCache and additional branch coverage', () => {
it('caches lesson progress count queries successfully', async () => {
const cache = new EvaluationCache('user-cache-test');
(db.lessonProgress.count as unknown as ReturnType<typeof vi.fn>).mockResolvedValue(5);

const promise1 = cache.getLessonCount();
const promise2 = cache.getLessonCount();

expect(promise1).toBe(promise2);
const res = await promise1;
expect(res).toBe(5);
expect(db.lessonProgress.count).toHaveBeenCalledTimes(1);
});

it('caches tool session count queries with different scopes successfully', async () => {
const cache = new EvaluationCache('user-cache-test');
(db.toolSession.count as unknown as ReturnType<typeof vi.fn>).mockResolvedValue(3);

const promiseAny1 = cache.getToolSessionCount();
const promiseAny2 = cache.getToolSessionCount();
expect(promiseAny1).toBe(promiseAny2);

const promiseSpecific = cache.getToolSessionCount('CAMPAIGN_BUILDER');
expect(promiseSpecific).not.toBe(promiseAny1);

await promiseAny1;
await promiseSpecific;
expect(db.toolSession.count).toHaveBeenCalledTimes(2);
});

it('caches user details queries successfully', async () => {
const cache = new EvaluationCache('user-cache-test');
(db.user.findUnique as unknown as ReturnType<typeof vi.fn>).mockResolvedValue({ streakDays: 5, xp: 500 });

const promise1 = cache.getUser();
const promise2 = cache.getUser();
expect(promise1).toBe(promise2);

const res = await promise1;
expect(res?.xp).toBe(500);
expect(db.user.findUnique).toHaveBeenCalledTimes(1);
});

it('handles module_complete criteria checks correctly', async () => {
(db.badge.findMany as unknown as ReturnType<typeof vi.fn>).mockResolvedValue([
{ id: 'b_mod', title: 'Module Complete', criteria: JSON.stringify({ type: 'module_complete', threshold: 1 }), xpReward: 10, description: '', icon: '', tier: 'BRONZE', isPublished: true, deletedAt: null },
]);
(db.lessonProgress.count as unknown as ReturnType<typeof vi.fn>).mockResolvedValue(1);

const result = await evaluateBadges('user-1', { trigger: 'lesson_complete' });
expect(result.awarded).toHaveLength(1);
});

it('handles quiz_score criteria checks correctly', async () => {
(db.badge.findMany as unknown as ReturnType<typeof vi.fn>).mockResolvedValue([
{ id: 'b_quiz', title: 'Perfect Quiz', criteria: JSON.stringify({ type: 'quiz_score', threshold: 100 }), xpReward: 15, description: '', icon: '', tier: 'BRONZE', isPublished: true, deletedAt: null },
]);

// Failed quiz submit
let result = await evaluateBadges('user-1', { trigger: 'quiz_submit', score: 100, passed: false });
expect(result.awarded).toHaveLength(0);

// Passed but low score
result = await evaluateBadges('user-1', { trigger: 'quiz_submit', score: 80, passed: true });
expect(result.awarded).toHaveLength(0);

// Passed with enough score
result = await evaluateBadges('user-1', { trigger: 'quiz_submit', score: 100, passed: true });
expect(result.awarded).toHaveLength(1);
});

it('handles tool_sessions criteria checks with and without scope correctly', async () => {
(db.badge.findMany as unknown as ReturnType<typeof vi.fn>).mockResolvedValue([
{ id: 'b_tool_any', title: 'Tool Master', criteria: JSON.stringify({ type: 'tool_sessions', threshold: 3 }), xpReward: 20, description: '', icon: '', tier: 'SILVER', isPublished: true, deletedAt: null },
{ id: 'b_tool_builder', title: 'Builder Master', criteria: JSON.stringify({ type: 'tool_sessions', threshold: 2, scope: { toolType: 'CAMPAIGN_BUILDER' } }), xpReward: 25, description: '', icon: '', tier: 'GOLD', isPublished: true, deletedAt: null },
]);

(db.toolSession.count as unknown as ReturnType<typeof vi.fn>).mockImplementation(async (args) => {
if (args?.where?.toolType === 'CAMPAIGN_BUILDER') {
return 2;
}
return 3;
});

const result = await evaluateBadges('user-1', { trigger: 'tool_submit', toolType: 'CAMPAIGN_BUILDER', passed: true });
expect(result.awarded).toHaveLength(2);
});
});
});
80 changes: 61 additions & 19 deletions src/lib/badges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,58 @@ export interface BadgeEvaluationResult {
totalXpGained: number;
}

/**
* Transient cache class to hold database query promises during badge evaluation.
* This collapses database roundtrips from O(R) where R is the number of badge rules
* down to O(1) by sharing the single query promise for lesson progress count,
* tool sessions count (optionally by tool type), and user profile (streak, xp).
*/
export class EvaluationCache {
private userId: string;
private lessonCountPromise: Promise<number> | null = null;
private toolCountPromises: Map<string, Promise<number>> = new Map();
private userPromise: Promise<{ streakDays: number; xp: number } | null> | null = null;

constructor(userId: string) {
this.userId = userId;
}

getLessonCount(): Promise<number> {
if (!this.lessonCountPromise) {
this.lessonCountPromise = db.lessonProgress.count({
where: { userId: this.userId, status: 'COMPLETED' },
});
}
return this.lessonCountPromise;
}

getToolSessionCount(toolType?: string): Promise<number> {
const key = toolType || '__any__';
let promise = this.toolCountPromises.get(key);
if (!promise) {
promise = db.toolSession.count({
where: {
userId: this.userId,
status: 'GRADED',
...(toolType ? { toolType } : {}),
},
});
this.toolCountPromises.set(key, promise);
}
return promise;
}

getUser(): Promise<{ streakDays: number; xp: number } | null> {
if (!this.userPromise) {
this.userPromise = db.user.findUnique({
where: { id: this.userId },
select: { streakDays: true, xp: true },
}) as Promise<{ streakDays: number; xp: number } | null>;
}
return this.userPromise;
}
}

/**
* Evaluate all badges for a user against the current database state. Award any
* newly-earned ones. Idempotent — re-running with no new events returns
Expand Down Expand Up @@ -95,6 +147,9 @@ export async function evaluateBadges(
xpReward: number;
}> = [];

// Instantiating transient cache for O(1) DB query sharing
const cache = new EvaluationCache(userId);

for (const badge of published) {
if (alreadyAwardedSet.has(badge.id)) continue;

Expand All @@ -106,7 +161,7 @@ export async function evaluateBadges(
continue;
}

const qualifies = await checkCriteria(userId, criteria, event);
const qualifies = await checkCriteria(userId, criteria, event, cache);
if (qualifies) earnedNow.push(badge);
}

Expand Down Expand Up @@ -146,12 +201,11 @@ async function checkCriteria(
userId: string,
criteria: BadgeCriteria,
event: BadgeTrigger,
cache: EvaluationCache,
): Promise<boolean> {
switch (criteria.type) {
case 'module_complete': {
const completedCount = await db.lessonProgress.count({
where: { userId, status: 'COMPLETED' },
});
const completedCount = await cache.getLessonCount();
// Treat each completed lesson as progress toward module_complete; the
// seed threshold is 1 so this triggers after the first lesson.
return completedCount >= criteria.threshold;
Expand All @@ -165,30 +219,18 @@ async function checkCriteria(

case 'tool_sessions': {
const scopeToolType = criteria.scope?.toolType;
const count = await db.toolSession.count({
where: {
userId,
status: 'GRADED',
...(scopeToolType ? { toolType: scopeToolType } : {}),
},
});
const count = await cache.getToolSessionCount(scopeToolType);
return count >= criteria.threshold;
}

case 'streak_days': {
const user = await db.user.findUnique({
where: { id: userId },
select: { streakDays: true },
});
const user = await cache.getUser();
if (!user) return false;
return user.streakDays >= criteria.threshold;
}

case 'xp_threshold': {
const user = await db.user.findUnique({
where: { id: userId },
select: { xp: true },
});
const user = await cache.getUser();
if (!user) return false;
return user.xp >= criteria.threshold;
}
Expand Down