Enhance Stage Specification Refinement and Rematch Logic
Deploy Development / deploy (push) Successful in 46s
Test Suite / pytest-backend (push) Successful in 44s
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

- Updated `max_rematch_rounds` in `ProgressionPathSuggestRequest` to allow for a maximum of 3 rounds, improving flexibility in rematch processes.
- Introduced `_track_rejected` function to track rejected exercises by major step index, enhancing the rematch logic to account for previously rejected exercises.
- Enhanced `_run_roadmap_rematch_loop` to utilize the new rejection tracking, ensuring better handling of off-topic steps during rematching.
- Improved `detect_off_topic_steps` to incorporate refined scoring and reasoning for stage fit, enhancing the accuracy of off-topic detection.
- Updated `refine_stage_spec_artifact` to merge stage exclusion phrases more effectively, improving the clarity of anti-pattern handling.
- Bumped version to 0.8.228 to reflect the new features and improvements.
This commit is contained in:
Lars committed 2026-06-11 22:11:31 +02:00
1 parent f36a747efa
commit a49987408b
9 files changed
+269 -74

No files matched your search

+108 -20
View File
@@ -813,6 +813,70 @@ def _expand_stage_exclude_phrase(phrase: str) -> List[str]:
return out[:12]
def is_trainer_stage_anti_marker(raw: str) -> bool:
"""Trainer-/QS-Marker — nicht als Negationsphrase parsen."""
norm = _normalize_phrase(str(raw or ""))
if not norm:
return False
stripped = re.sub(r"[„“\"'«»]", "", norm)
stripped = re.sub(r"\s+", " ", stripped).strip()
if stripped.startswith("keine übung wie") or stripped.startswith("keine uebung wie"):
return True
return stripped.startswith("qs-hinweis")
def merge_stage_exclude_phrases(
learning_goal: str,
anti_patterns: Optional[Sequence[str]] = None,
) -> List[str]:
"""
Ausschlussphrasen für Stufen-Gates — Negationen nur aus dem Lernziel expandieren,
explizite anti_patterns unverändert (ohne Trainer-Marker erneut zu parsen).
"""
lg = (learning_goal or "").strip()
exclude: List[str] = []
if len(lg) >= 3:
for item in parse_stage_goal_constraints(lg).exclude_phrases:
if item and item not in exclude:
exclude.append(item)
markers: List[str] = []
for raw in anti_patterns or []:
s = str(raw or "").strip()
if not s:
continue
if is_trainer_stage_anti_marker(s):
if s not in markers:
markers.append(s[:200])
continue
norm = _normalize_phrase(s)
if norm and norm not in exclude:
exclude.append(norm)
for marker in markers:
if marker not in exclude:
exclude.append(marker)
return exclude[:16]
def stage_focus_phrases_from_learning_goal(learning_goal: str) -> List[str]:
"""Mehrwort-Schwerpunkte aus Stufen-Lernziel für Fit-Scoring."""
lg = (learning_goal or "").strip()
if len(lg) < 3:
return []
tokens = _significant_stage_tokens(lg, strip_negated=True)
phrases: List[str] = []
for tok in tokens:
if len(tok) >= 5 and tok not in phrases:
phrases.append(tok)
for i in range(len(tokens) - 1):
pair = f"{tokens[i]} {tokens[i + 1]}"
if len(pair) >= 8 and pair not in phrases:
phrases.append(pair)
norm_lg = _normalize_phrase(lg)
if len(norm_lg) >= 8 and norm_lg not in phrases:
phrases.insert(0, norm_lg[:120])
return phrases[:8]
def _significant_stage_tokens(learning_goal: str, *, strip_negated: bool = True) -> List[str]:
"""Wörter aus Stufen-Lernziel für Text-Match (ohne Füllwörter, ohne Negationssegmente)."""
text = _normalize_phrase(learning_goal)
@@ -850,9 +914,11 @@ def parse_stage_goal_constraints(
exclude.extend(_expand_stage_exclude_phrase(chunk))
for raw in anti_patterns or []:
if is_trainer_stage_anti_marker(str(raw or "")):
continue
s = _normalize_phrase(str(raw or ""))
if s:
exclude.extend(_expand_stage_exclude_phrase(s))
if s and s not in exclude:
exclude.append(s)
positive = _significant_stage_tokens(lg, strip_negated=True)
focus_hits = [t for t in positive if t in _STAGE_FOCUS_TOKENS]
@@ -997,7 +1063,7 @@ def build_stage_match_brief(
for expanded in _expand_stage_exclude_phrase(str(raw or "")):
if expanded and expanded not in merged_anti:
merged_anti.append(expanded)
constraints = parse_stage_goal_constraints(lg, merged_anti)
constraints = parse_stage_goal_constraints(lg)
must: List[str] = []
norm_lg = _normalize_phrase(lg)
if primary_path and primary_path not in must:
@@ -1031,11 +1097,13 @@ def build_stage_match_brief(
if ph:
arc.append(ph)
exclude_phrases = merge_stage_exclude_phrases(lg, merged_anti)
return PlanningSemanticBrief(
primary_topic="",
topic_type="focus",
must_phrases=must[:12],
exclude_phrases=list(constraints.exclude_phrases)[:12],
exclude_phrases=exclude_phrases[:12],
development_arc=arc[:4],
retrieval_query=" ".join(p for p in retrieval_parts if p)[:500],
semantic_strength=0.78,
@@ -1062,19 +1130,36 @@ def score_exercise_stage_fit(
step_phase=step_phase,
)
blob = _blob_from_fields(title, summary, goal, variant_names or [])
focus_tokens = [
t
for t in (stage_brief.must_phrases or [])
if t and " " not in t and len(t) >= 4
][:6]
if focus_tokens:
hits = sum(1 for t in focus_tokens if _phrase_in_blob(t, blob))
ratio = hits / len(focus_tokens)
bonus = 0.28 * ratio
lg_hint = ""
for part in (stage_brief.retrieval_query or "").split("|"):
part = part.strip()
if part.lower().startswith("lernziel:"):
lg_hint = part.split(":", 1)[-1].strip()
break
if not lg_hint:
for mp in stage_brief.must_phrases or []:
if mp and len(_normalize_phrase(mp)) >= 8:
lg_hint = mp
break
focus_phrases = stage_focus_phrases_from_learning_goal(lg_hint) if lg_hint else []
if not focus_phrases:
focus_phrases = [
t
for t in (stage_brief.must_phrases or [])
if t and len(_normalize_phrase(t)) >= 5
][:6]
if focus_phrases:
hits = sum(1 for p in focus_phrases if _phrase_in_blob(p, blob))
ratio = hits / len(focus_phrases)
bonus = 0.32 * ratio
if bonus > 0:
score = min(1.0, score + bonus)
if hits >= max(1, len(focus_tokens) // 2):
if hits >= max(1, len(focus_phrases) // 2):
reasons = ["Stufen-Schwerpunkte im Übungstext", *reasons]
learning_goal_for_equiv = lg_hint or (stage_brief.must_phrases[0] if stage_brief.must_phrases else "")
if learning_goal_for_equiv and exercise_title_equivalent_to_stage_goal(title, learning_goal_for_equiv):
score = max(score, 0.42)
reasons = ["Titel entspricht Stufen-Lernziel", *reasons]
return max(0.0, min(1.0, round(score, 4))), reasons[:4]
@@ -1099,11 +1184,13 @@ def exercise_passes_stage_fit(
return True
blob = _blob_from_fields(title, summary, goal, [])
constraints = parse_stage_goal_constraints(lg, anti_patterns)
if constraints.exclude_phrases and _blob_matches_stage_excludes(blob, constraints.exclude_phrases):
exclude_phrases = merge_stage_exclude_phrases(lg, anti_patterns)
if exclude_phrases and _blob_matches_stage_excludes(blob, exclude_phrases):
return False
title_equiv = exercise_title_equivalent_to_stage_goal(title, learning_goal or lg)
if title_equiv:
return True
primary_path = (path_primary_topic or "").strip()
if not primary_path and lg:
@@ -1293,10 +1380,8 @@ def _pick_roadmap_rank_fallback(
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
):
exclude_phrases = merge_stage_exclude_phrases(stage_goal, stage_anti_patterns)
if exclude_phrases and _blob_matches_stage_excludes(blob, exclude_phrases):
continue
title_equiv = exercise_title_equivalent_to_stage_goal(title, stage_goal)
primary = (path_primary_topic or "").strip()
@@ -1465,8 +1550,11 @@ __all__ = [
"resolve_path_primary_topic",
"resolve_path_anti_patterns",
"exercise_passes_stage_learning_goal_gate",
"is_trainer_stage_anti_marker",
"merge_semantic_brief_llm",
"merge_stage_exclude_phrases",
"parse_stage_goal_constraints",
"stage_focus_phrases_from_learning_goal",
"pick_best_path_hit",
"exercise_passes_technique_path_scope",
"score_exercise_stage_fit",