Fix discovery scoring: cap trending, prevent score inflation, add freshness

- Cap trending base_score at 18.0 (was unbounded — a viral channel could
  score 240+ vs search's 15, making everything else invisible)
- Cap all discovery scores at 50.0 globally so no single signal dominates
- Fix score accumulation: cap accumulated total at 50.0 (was unbounded
  across repeated runs, cementing high-score channels in top positions forever)
- Expire unseen queue entries older than 14 days at start of each run
- Add ±8 score perturbation to discovery list endpoint (was pure score DESC,
  identical every visit until dismissed)
- Add score perturbation to discovery_videos ORDER BY too
- Fix SQL injection in update_category_clusters (category strings were
  interpolated directly into query; now use parameterized queries per category)
- Raise category signal score from 3.0 → 5.0 to compensate for trending cap

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-27 01:37:09 +02:00
parent b6a47249d0
commit 592194f2ca
2 changed files with 47 additions and 20 deletions

View File

@@ -1,4 +1,5 @@
import json
import random
from typing import Optional
from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException
@@ -81,6 +82,9 @@ def list_discovery(
pass
rows = [r for r in rows if neg_hit.get(r["channel_id"], 0) < 3]
# Add score perturbation so the list doesn't look identical every visit.
# ±8 jitter keeps relative ranking meaningful while surfacing different channels.
rows = sorted(rows, key=lambda r: r["score"] + random.uniform(-8, 8), reverse=True)
rows = rows[:limit]
items = []
for row in rows:
@@ -210,7 +214,7 @@ def discovery_videos(
)
)
WHERE rn <= 2
ORDER BY score DESC, rn ASC, RANDOM()
ORDER BY (score + (RANDOM() * 10 - 5)) DESC, rn ASC
LIMIT :limit OFFSET :offset
"""),
{"user_id": current_user.id, "limit": limit, "offset": offset},