Refactor Planning Exercise Retrieval and Suggestion Logic
Deploy Development / deploy (push) Successful in 39s
Test Suite / pytest-backend (push) Successful in 39s
Test Suite / lint-backend (push) Successful in 1s
Test Suite / build-frontend (push) Successful in 12s
Test Suite / k6 /health Baseline (push) Successful in 33s
Test Suite / playwright-tests (push) Successful in 1m12s

- Updated the planning exercise retrieval process to implement a multistage approach, ranking the entire visible library deterministically against the expectation profile.
- Removed the previous profile OR pool mechanism, simplifying the retrieval logic and ensuring full-text search is only used as a scoring signal.
- Adjusted the `compose_retrieval_phase` function to accommodate the new full library ranking strategy.
- Incremented version to 0.8.177 and updated changelog to reflect these changes in planning exercise capabilities.
This commit is contained in:
Lars committed 2026-05-23 06:35:45 +02:00
1 parent a8633235f2
commit d1d8539b42
6 files changed
+208 -259

No files matched your search

+94 -224
View File
@@ -1,10 +1,9 @@
"""
Mehrstufiges Retrieval für Planungs-Übungssuche (S1b).
Mehrstufiges Retrieval für Planungs-Übungssuche (Phase A).
Stufen:
S1b-0 Kandidaten-Pool (Profil-Signale, Volltext, Progressions-Nachfolger)
S1b-1 Profil-Vorselektion → Top-K vor teurem Hybrid-Score
S1b-2 Hybrid-Score (Volltext, Graph, Skills, Plan, Profil, Wiederholung)
S1b-0 Gesamte sichtbare Bibliothek (Governance + Hard-Filter, kein Profil-OR-Pool)
S1b-1 Deterministischer Hybrid-Score auf allen Kandidaten → sortiert
"""
from __future__ import annotations
@@ -16,8 +15,8 @@ from planning_exercise_profiles import (
score_exercise_against_target,
)
_RAW_POOL_LIMIT = 500
_PROFILE_PRESELECT_LIMIT = 160
_MAX_LIBRARY_ROWS = 8000
_PROFILE_LOAD_BATCH = 400
def _skill_jaccard(a: Set[int], b: Set[int]) -> float:
@@ -28,45 +27,37 @@ def _skill_jaccard(a: Set[int], b: Set[int]) -> float:
return inter / union if union else 0.0
def _top_weight_keys(weights: Mapping[int, float], limit: int) -> List[int]:
if not weights:
return []
return [
int(k)
for k, _ in sorted(weights.items(), key=lambda x: -float(x[1]))[:limit]
if int(k) > 0
]
def _normalize_exercise_kind_filter(exercise_kind_any: Optional[List[str]]) -> List[str]:
out: List[str] = []
if not exercise_kind_any:
return out
for raw in exercise_kind_any:
s = str(raw or "").strip().lower()
if s in ("simple", "combination") and s not in out:
out.append(s)
return out
def _target_profile_signals(target: PlanningTargetProfile) -> Tuple[List[int], List[int], List[int]]:
skill_ids = _top_weight_keys(target.skill_weights, 8)
for sid in _top_weight_keys(target.skill_gap_weights, 6):
if sid not in skill_ids:
skill_ids.append(sid)
focus_ids = _top_weight_keys(target.focus_area_ids, 6)
style_ids = _top_weight_keys(target.style_direction_ids, 4)
return skill_ids[:12], focus_ids, style_ids
def fetch_retrieval_candidate_rows(
def fetch_all_visible_exercise_rows(
cur,
*,
vis_sql: str,
vis_params: Sequence[Any],
query: str,
exercise_kind_any: Optional[List[str]],
target: PlanningTargetProfile,
progression_successor_ids: Set[int],
anchor_skill_ids: Set[int],
raw_pool_limit: int = _RAW_POOL_LIMIT,
max_rows: int = _MAX_LIBRARY_ROWS,
) -> List[Dict[str, Any]]:
"""S1b-0: Profil-geführter Kandidaten-Pool."""
"""
S1b-0: Alle sichtbaren Übungen (ohne Profil-/Volltext-Pool-Vorselektion).
Hard-Filter: Governance, nicht archiviert, optional exercise_kind.
Volltext-Rank nur als Score-Signal in SELECT, nicht als WHERE-Filter.
"""
where = [vis_sql, "COALESCE(e.status, '') <> %s"]
params: List[Any] = []
if query:
ft_select = "ts_rank_cd(e.search_vector, plainto_tsquery('german', %s)) AS ft_rank"
# SELECT-Platzhalter steht im SQL vor WHERE — Query zuerst binden.
params.append(query)
else:
ft_select = "0.0::float AS ft_rank"
@@ -74,56 +65,12 @@ def fetch_retrieval_candidate_rows(
params.extend(vis_params)
params.append("archived")
ek_filtered: List[str] = []
if exercise_kind_any:
for raw in exercise_kind_any:
s = str(raw or "").strip().lower()
if s in ("simple", "combination") and s not in ek_filtered:
ek_filtered.append(s)
ek_filtered = _normalize_exercise_kind_filter(exercise_kind_any)
if ek_filtered:
ph = ",".join(["%s"] * len(ek_filtered))
where.append(f"(LOWER(TRIM(COALESCE(e.exercise_kind::text,''))) IN ({ph}))")
params.extend(ek_filtered)
skill_ids, focus_ids, style_ids = _target_profile_signals(target)
if not skill_ids and anchor_skill_ids:
skill_ids = sorted(anchor_skill_ids)[:10]
profile_clauses: List[str] = []
if skill_ids:
ph = ",".join(["%s"] * len(skill_ids))
profile_clauses.append(
f"EXISTS (SELECT 1 FROM exercise_skills es WHERE es.exercise_id = e.id AND es.skill_id IN ({ph}))"
)
params.extend(skill_ids)
if focus_ids:
ph = ",".join(["%s"] * len(focus_ids))
profile_clauses.append(
f"EXISTS (SELECT 1 FROM exercise_focus_areas efa WHERE efa.exercise_id = e.id AND efa.focus_area_id IN ({ph}))"
)
params.extend(focus_ids)
if style_ids:
ph = ",".join(["%s"] * len(style_ids))
profile_clauses.append(
f"EXISTS (SELECT 1 FROM exercise_style_directions esd WHERE esd.exercise_id = e.id AND esd.style_direction_id IN ({ph}))"
)
params.extend(style_ids)
if progression_successor_ids:
ph = ",".join(["%s"] * len(progression_successor_ids))
profile_clauses.append(f"e.id IN ({ph})")
params.extend(sorted(progression_successor_ids))
if query:
profile_clauses.append("e.search_vector @@ plainto_tsquery('german', %s)")
params.append(query)
use_profile_pool = bool(profile_clauses)
if use_profile_pool:
where.append(f"({' OR '.join(profile_clauses)})")
order_by = "e.updated_at DESC, e.id DESC"
if query:
order_by = "ft_rank DESC NULLS LAST, e.updated_at DESC, e.id DESC"
sql = f"""
SELECT e.id, e.title, e.summary,
(
@@ -136,129 +83,46 @@ def fetch_retrieval_candidate_rows(
{ft_select}
FROM exercises e
WHERE {' AND '.join(where)}
ORDER BY {order_by}
ORDER BY e.id ASC
LIMIT %s
"""
params.append(int(raw_pool_limit))
params.append(int(max_rows))
cur.execute(sql, params)
rows = [dict(r) for r in cur.fetchall()]
if rows or not use_profile_pool:
return rows
return _fetch_broad_fallback_pool(
cur,
vis_sql=vis_sql,
vis_params=vis_params,
query=query,
ek_filtered=ek_filtered,
raw_pool_limit=raw_pool_limit,
)
def _fetch_broad_fallback_pool(
cur,
*,
vis_sql: str,
vis_params: Sequence[Any],
query: str,
ek_filtered: List[str],
raw_pool_limit: int,
) -> List[Dict[str, Any]]:
fallback_where = [vis_sql, "COALESCE(e.status, '') <> %s"]
fallback_params: List[Any] = list(vis_params)
fallback_params.append("archived")
if ek_filtered:
ph = ",".join(["%s"] * len(ek_filtered))
fallback_where.append(f"(LOWER(TRIM(COALESCE(e.exercise_kind::text,''))) IN ({ph}))")
fallback_params.extend(ek_filtered)
if query:
ft_fb = "ts_rank_cd(e.search_vector, plainto_tsquery('german', %s)) AS ft_rank"
fb_order = "ft_rank DESC NULLS LAST, e.updated_at DESC, e.id DESC"
fallback_params.insert(0, query)
else:
ft_fb = "0.0::float AS ft_rank"
fb_order = "e.updated_at DESC, e.id DESC"
fb_sql = f"""
SELECT e.id, e.title, e.summary,
(
SELECT fa.name FROM exercise_focus_areas efa
JOIN focus_areas fa ON fa.id = efa.focus_area_id
WHERE efa.exercise_id = e.id
ORDER BY efa.is_primary DESC NULLS LAST, fa.name ASC
LIMIT 1
) AS primary_focus_name,
{ft_fb}
FROM exercises e
WHERE {' AND '.join(fallback_where)}
ORDER BY {fb_order}
LIMIT %s
"""
fallback_params.append(int(raw_pool_limit))
cur.execute(fb_sql, fallback_params)
return [dict(r) for r in cur.fetchall()]
def profile_preselect_rows(
cur,
rows: Sequence[Dict[str, Any]],
*,
target: PlanningTargetProfile,
intent: str,
progression_successor_ids: Set[int],
query: str,
preselect_limit: int = _PROFILE_PRESELECT_LIMIT,
) -> Tuple[List[Dict[str, Any]], bool]:
"""S1b-1: Profil-Score auf Pool, Top-K für Hybrid."""
if len(rows) <= preselect_limit:
return list(rows), False
cand_ids = [int(r["id"]) for r in rows]
match_profiles = load_exercise_match_profiles_bulk(cur, cand_ids)
scored: List[Tuple[float, Dict[str, Any]]] = []
row_by_id = {int(r["id"]): r for r in rows}
must_keep: Set[int] = set(int(x) for x in progression_successor_ids)
if query:
max_ft = max(float(r.get("ft_rank") or 0.0) for r in rows) or 0.0
if max_ft > 0:
for r in rows:
if float(r.get("ft_rank") or 0.0) / max_ft >= 0.5:
must_keep.add(int(r["id"]))
for eid in cand_ids:
emp = match_profiles.get(eid)
profile_score = 0.0
if emp:
profile_score, _ = score_exercise_against_target(emp, target, intent=intent)
scored.append((profile_score, row_by_id[eid]))
scored.sort(key=lambda x: (-x[0], str(x[1].get("title") or "")))
selected: List[Dict[str, Any]] = []
seen: Set[int] = set()
for _, row in scored:
eid = int(row["id"])
if eid in seen:
continue
seen.add(eid)
selected.append(row)
if len(selected) >= preselect_limit:
break
for eid in must_keep:
if eid in seen:
continue
row = row_by_id.get(eid)
if row:
selected.append(row)
seen.add(eid)
return selected, True
def _load_match_profiles_chunked(cur, exercise_ids: Sequence[int], *, batch: int = _PROFILE_LOAD_BATCH):
ids = sorted({int(x) for x in exercise_ids if int(x) > 0})
if not ids:
return {}
out: Dict[int, Any] = {}
for i in range(0, len(ids), batch):
chunk = ids[i : i + batch]
out.update(load_exercise_match_profiles_bulk(cur, chunk))
return out
def hybrid_score_planning_hits(
def _load_skill_sets_chunked(cur, exercise_ids: Sequence[int], *, batch: int = _PROFILE_LOAD_BATCH) -> Dict[int, Set[int]]:
ids = sorted({int(x) for x in exercise_ids if int(x) > 0})
out: Dict[int, Set[int]] = {eid: set() for eid in ids}
if not ids:
return out
for i in range(0, len(ids), batch):
chunk = ids[i : i + batch]
ph = ",".join(["%s"] * len(chunk))
cur.execute(
f"SELECT exercise_id, skill_id FROM exercise_skills WHERE exercise_id IN ({ph})",
chunk,
)
for row in cur.fetchall():
eid = int(row["exercise_id"])
sid = row.get("skill_id")
if sid is not None:
out.setdefault(eid, set()).add(int(sid))
return out
def rank_visible_library_hits(
cur,
rows: Sequence[Dict[str, Any]],
*,
@@ -268,7 +132,7 @@ def hybrid_score_planning_hits(
target: PlanningTargetProfile,
pack: Mapping[str, Any],
) -> Tuple[List[Dict[str, Any]], Dict[int, Set[int]]]:
"""S1b-2: Hybrid-Score auf vorselektiertem Pool."""
"""S1b-1: Hybrid-Score auf der gesamten sichtbaren Bibliothek."""
planned_set = set(pack.get("planned_exercise_ids") or [])
group_recent_set = set(pack.get("group_recent_exercise_ids") or [])
progression_set = set(pack.get("progression_successor_ids") or [])
@@ -285,24 +149,21 @@ def hybrid_score_planning_hits(
)
last_planned_skills = {int(r["skill_id"]) for r in cur.fetchall() if r.get("skill_id")}
cand_ids = [int(r["id"]) for r in rows]
skills_by_ex: Dict[int, Set[int]] = {cid: set() for cid in cand_ids}
match_profiles = load_exercise_match_profiles_bulk(cur, cand_ids)
if cand_ids:
ph = ",".join(["%s"] * len(cand_ids))
cur.execute(
f"SELECT exercise_id, skill_id FROM exercise_skills WHERE exercise_id IN ({ph})",
cand_ids,
)
for r in cur.fetchall():
skills_by_ex.setdefault(int(r["exercise_id"]), set()).add(int(r["skill_id"]))
max_ft = 0.0
scored_items: List[Dict[str, Any]] = []
cand_rows: List[Dict[str, Any]] = []
for row in rows:
eid = int(row["id"])
if anchor_id and eid == int(anchor_id):
continue
cand_rows.append(row)
cand_ids = [int(r["id"]) for r in cand_rows]
match_profiles = _load_match_profiles_chunked(cur, cand_ids)
skills_by_ex = _load_skill_sets_chunked(cur, cand_ids)
max_ft = 0.0
scored_items: List[Dict[str, Any]] = []
for row in cand_rows:
eid = int(row["id"])
ft = float(row.get("ft_rank") or 0.0)
if ft > max_ft:
max_ft = ft
@@ -397,29 +258,15 @@ def run_multistage_planning_retrieval(
intent_weights: Mapping[str, float],
pack: Mapping[str, Any],
) -> Tuple[List[Dict[str, Any]], Dict[int, Set[int]], bool]:
"""Orchestriert S1b-0 → S1b-1 → S1b-2."""
progression_set = set(pack.get("progression_successor_ids") or [])
anchor_skills = set(pack.get("anchor_skill_ids") or [])
rows = fetch_retrieval_candidate_rows(
"""Orchestriert S1b-0 → S1b-1 (Voll-Library-Ranking)."""
rows = fetch_all_visible_exercise_rows(
cur,
vis_sql=vis_sql,
vis_params=vis_params,
query=query,
exercise_kind_any=exercise_kind_any,
target=target,
progression_successor_ids=progression_set,
anchor_skill_ids=anchor_skills,
)
rows, preselect_applied = profile_preselect_rows(
cur,
rows,
target=target,
intent=intent,
progression_successor_ids=progression_set,
query=query,
)
hits, skills_by_ex = hybrid_score_planning_hits(
hits, skills_by_ex = rank_visible_library_hits(
cur,
rows,
query=query,
@@ -428,12 +275,35 @@ def run_multistage_planning_retrieval(
target=target,
pack=pack,
)
return hits, skills_by_ex, preselect_applied
full_library_ranked = len(rows) > 0
return hits, skills_by_ex, full_library_ranked
# Legacy-Alias für Tests / externe Imports
fetch_retrieval_candidate_rows = fetch_all_visible_exercise_rows
hybrid_score_planning_hits = rank_visible_library_hits
def profile_preselect_rows(
cur,
rows: Sequence[Dict[str, Any]],
*,
target: PlanningTargetProfile,
intent: str,
progression_successor_ids: Set[int],
query: str,
preselect_limit: int = 160,
) -> Tuple[List[Dict[str, Any]], bool]:
"""Deprecated: Phase A rankt die volle Library — keine separate Vorselektion."""
_ = (cur, target, intent, progression_successor_ids, query, preselect_limit)
return list(rows), False
__all__ = [
"fetch_all_visible_exercise_rows",
"fetch_retrieval_candidate_rows",
"hybrid_score_planning_hits",
"profile_preselect_rows",
"rank_visible_library_hits",
"run_multistage_planning_retrieval",
]