Implement Learning Goal Candidate Retrieval and Roadmap Fallback Logic
Deploy Development / deploy (push) Successful in 43s
Test Suite / pytest-backend (push) Successful in 43s
Test Suite / lint-backend (push) Successful in 0s
Test Suite / build-frontend (push) Successful in 13s
Test Suite / k6 /health Baseline (push) Successful in 34s
Test Suite / playwright-tests (push) Successful in 1m22s

- Added `_safe_tsquery_fragment` to sanitize learning goal input for SQL queries, improving query safety.
- Introduced `_fetch_learning_goal_library_candidate_ids` to retrieve exercise IDs matching learning goals, enhancing exercise relevance in roadmap suggestions.
- Enhanced `_match_roadmap_slot` to utilize learning goal candidates, improving the accuracy of supplemental exercise selection.
- Implemented `_pick_roadmap_rank_fallback` to provide a fallback mechanism for selecting the best exercise when strict matching fails, ensuring better exercise retrieval.
- Updated tests to validate the new learning goal retrieval and fallback logic, ensuring robustness in exercise selection processes.
This commit is contained in:
Lars committed 2026-06-11 12:54:07 +02:00
1 parent b2fbf6b4af
commit 6d130a7e09
5 files changed
+227 -5

No files matched your search

+104 -2
View File
@@ -347,6 +347,87 @@ def _graph_visibility_context(
)
def _safe_tsquery_fragment(text: str) -> str:
import re
cleaned = re.sub(r"[^\w\säöüßÄÖÜ]", " ", text or "", flags=re.UNICODE)
words = [w for w in cleaned.split() if len(w) >= 2][:10]
return " ".join(words) if words else (text or "")[:60].strip()
def _fetch_learning_goal_library_candidate_ids(
cur,
*,
tenant: TenantContext,
progression_graph_id: Optional[int],
learning_goal: str,
limit: int = 24,
) -> List[int]:
"""Sichtbare Übungen, deren Titel/Volltext zum Stufen-Lernziel passt."""
lg = (learning_goal or "").strip()
if len(lg) < 3:
return []
vis_sql, vis_params = _planning_visibility_sql(cur, tenant, progression_graph_id)
tsq = _safe_tsquery_fragment(lg)
like_pat = f"%{lg[:100].lower()}%"
try:
cur.execute(
f"""
SELECT e.id
FROM exercises e
WHERE ({vis_sql})
AND COALESCE(e.status, '') <> %s
AND (
lower(trim(e.title)) = lower(trim(%s))
OR lower(e.title) LIKE %s
OR (%s <> '' AND e.search_vector @@ plainto_tsquery('german', %s))
)
ORDER BY
CASE WHEN lower(trim(e.title)) = lower(trim(%s)) THEN 0 ELSE 1 END,
CASE WHEN %s <> '' THEN ts_rank_cd(e.search_vector, plainto_tsquery('german', %s)) ELSE 0 END DESC,
e.id ASC
LIMIT %s
""",
[
*vis_params,
"archived",
lg,
like_pat,
tsq,
tsq,
lg,
tsq,
tsq,
int(limit),
],
)
except Exception:
cur.execute(
f"""
SELECT e.id
FROM exercises e
WHERE ({vis_sql})
AND COALESCE(e.status, '') <> %s
AND (
lower(trim(e.title)) = lower(trim(%s))
OR lower(e.title) LIKE %s
)
ORDER BY CASE WHEN lower(trim(e.title)) = lower(trim(%s)) THEN 0 ELSE 1 END, e.id ASC
LIMIT %s
""",
[*vis_params, "archived", lg, like_pat, lg, int(limit)],
)
out: List[int] = []
for row in cur.fetchall() or []:
try:
eid = int(row.get("id") or 0)
except (TypeError, ValueError):
continue
if eid > 0:
out.append(eid)
return out
def _load_supplemental_exercise_rows(
cur,
*,
@@ -1000,10 +1081,31 @@ def _match_roadmap_slot(
step_kind = resolve_step_exercise_kind_filter(stage_spec, body.exercise_kind_any)
supplemental_ids = _supplemental_exercise_ids_from_body(cur, body)
lg_candidates = _fetch_learning_goal_library_candidate_ids(
cur,
tenant=tenant,
progression_graph_id=body.progression_graph_id,
learning_goal=stage_goal,
)
supplemental_ids = list(
dict.fromkeys(
int(x)
for x in [
*supplemental_ids,
*lg_candidates,
slot_priority_exercise_id,
]
if x is not None and int(x) > 0
)
)
priority_ids = list(
dict.fromkeys(
x
for x in [slot_priority_exercise_id, *(body.retrieval_boost_exercise_ids or [])]
int(x)
for x in [
slot_priority_exercise_id,
*(body.retrieval_boost_exercise_ids or []),
*lg_candidates[:8],
]
if x is not None and int(x) > 0
)
)