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

+81 -1
View File
@@ -955,6 +955,7 @@ def enrich_brief_with_path_constraints(
_MIN_STAGE_FIT_SEMANTIC = 0.30
_MIN_STAGE_FIT_RELAXED = 0.20
_MIN_TITLE_EQUIV_SEMANTIC = 0.15
_MIN_ROADMAP_FALLBACK_RANK = 0.15
def build_stage_match_brief(
@@ -1260,6 +1261,76 @@ def exercise_passes_path_semantic_gate(
return False
def _pick_roadmap_rank_fallback(
hits: List[Dict[str, Any]],
used_exercise_ids: Set[int],
*,
stage_learning_goal: str,
stage_anti_patterns: Optional[Sequence[str]] = None,
path_primary_topic: Optional[str] = None,
path_technique_excludes: Optional[Sequence[str]] = None,
) -> Optional[Dict[str, Any]]:
"""
Roadmap-Notfall: bester Treffer nach Stufen-Ranking, wenn striktes Gate leer läuft.
Filtert weiterhin Ausschlüsse und Technik-Scope (Kumite etc.), aber ohne
Mindest-Semantik-Schwelle — so finden auch wortnahe Bibliotheks-Übungen den Slot.
"""
stage_goal = (stage_learning_goal or "").strip()
if not stage_goal or not hits:
return None
best: Optional[Dict[str, Any]] = None
best_key: Tuple[float, float] = (-1.0, -1.0)
for hit in hits:
try:
eid = int(hit["id"])
except (TypeError, ValueError, KeyError):
continue
if eid in used_exercise_ids:
continue
title = str(hit.get("title") or "")
summary = str(hit.get("summary") or "")
goal_text = str(hit.get("goal") or hit.get("exercise_goal") or "")
blob = _blob_from_fields(title, summary, goal_text, [])
constraints = parse_stage_goal_constraints(stage_goal, stage_anti_patterns)
if constraints.exclude_phrases and _blob_matches_stage_excludes(
blob, constraints.exclude_phrases
):
continue
title_equiv = exercise_title_equivalent_to_stage_goal(title, stage_goal)
primary = (path_primary_topic or "").strip()
if primary and not title_equiv:
tech_excludes = list(path_technique_excludes or [])
for item in technique_sibling_excludes(primary):
if item not in tech_excludes:
tech_excludes.append(item)
if not exercise_passes_technique_path_scope(
primary_topic=primary,
title=title,
summary=summary,
goal=goal_text,
learning_goal=stage_goal,
sibling_excludes=tech_excludes,
relaxed=True,
):
continue
rank_sem = float(
hit.get("stage_rank_semantic")
or hit.get("stage_semantic_score")
or hit.get("semantic_score")
or 0.0
)
score = float(hit.get("score") or 0.0)
key = (rank_sem, score)
if key > best_key:
best_key = key
best = hit
if best is None or best_key[0] < _MIN_ROADMAP_FALLBACK_RANK:
return None
return best
def pick_best_path_hit(
hits: List[Dict[str, Any]],
used_exercise_ids: Set[int],
@@ -1341,7 +1412,16 @@ def pick_best_path_hit(
if roadmap_stage_match:
chosen = _scan(strict=False)
return chosen
if chosen:
return chosen
return _pick_roadmap_rank_fallback(
hits,
used_exercise_ids,
stage_learning_goal=stage_goal,
stage_anti_patterns=stage_anti_patterns,
path_primary_topic=path_primary_topic,
path_technique_excludes=path_technique_excludes,
)
chosen = _scan(strict=False)
if chosen: