Skip to content
Open
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
16 changes: 12 additions & 4 deletions server/modules/ai/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,20 @@ class AiService {
const user = await User.findById(userId).select("name email dashboardSummary").lean();
if (!user) throw new ApiError(401, "User not found.");

// 2. Return cached summary if it exists
if (user.dashboardSummary && user.dashboardSummary.content) {
return user.dashboardSummary.content;
// 2. Return cached summary if it's still fresh. Without a TTL the summary
// was returned forever — nothing (not a CF sync, not new contests)
// regenerated it, so the "growth insight" could permanently contradict
// the user's current stats.
const SUMMARY_MAX_AGE_MS = 24 * 60 * 60 * 1000; // 24 hours
const cached = user.dashboardSummary;
const summaryAge = cached?.lastGeneratedAt
? Date.now() - new Date(cached.lastGeneratedAt).getTime()
: Infinity;
if (cached && cached.content && summaryAge < SUMMARY_MAX_AGE_MS) {
return cached.content;
}

console.log(`[AI] ▶ No cached summary found for ${user.name}. Generating new summary...`);
console.log(`[AI] ▶ No fresh cached summary for ${user.name}. Generating new summary...`);

// 3. Fetch data for generation
const [cfProfile, recentContests] = await Promise.all([
Expand Down
Loading