Enhance Roadmap Step Handling and Off-Topic Logic
Deploy Development / deploy (push) Successful in 46s
Test Suite / pytest-backend (push) Successful in 52s
Test Suite / lint-backend (push) Successful in 0s
Test Suite / build-frontend (push) Successful in 14s
Test Suite / k6 /health Baseline (push) Successful in 41s
Test Suite / playwright-tests (push) Successful in 1m51s

- Improved off-topic step handling by incorporating roadmap major step indices for better indexing and detection.
- Refactored `collect_gap_fill_specs` to streamline the insertion logic for off-topic steps, ensuring correct placement based on major step indices.
- Introduced `_normalize_roadmap_steps_coverage` function to standardize roadmap steps coverage, enhancing the handling of missing slots.
- Added `prune_stripped_after_rematch` function to clean up stripped off-topic steps after rematching, improving the overall rematching process.
- Updated tests to validate new rematching and off-topic handling features, ensuring robustness against edge cases.
- Incremented application version to reflect these updates.
This commit is contained in:
Lars committed 2026-06-11 10:40:25 +02:00
1 parent 1d94c2ebf1
commit 713a344d17
7 files changed
+269 -53

No files matched your search

+159 -44
View File
@@ -14,7 +14,11 @@ from pydantic import BaseModel, Field
from tenant_context import TenantContext, library_content_visibility_sql
from planning_exercise_profiles import PlanningTargetProfile
from planning_path_qa_pipeline import run_multistage_path_qa
from planning_path_rematch import collect_rematch_slot_indices, rematch_roadmap_slots
from planning_path_rematch import (
collect_rematch_slot_indices,
prune_stripped_after_rematch,
rematch_roadmap_slots,
)
from planning_stage_context import build_contextualized_stage_goal, resolve_path_start_target
from planning_exercise_path_qa import (
apply_llm_path_reorder,
@@ -704,6 +708,129 @@ def _match_roadmap_slot(
return step, None
def _normalize_roadmap_steps_coverage(
steps: List[Dict[str, Any]],
*,
roadmap_ctx: ProgressionRoadmapContext,
max_steps: int,
) -> List[Dict[str, Any]]:
"""Ein Eintrag pro Roadmap-Major-Step — fehlende Slots als leere Platzhalter."""
stage_specs = list(roadmap_ctx.stage_specs or [])[:max_steps]
if not stage_specs:
return steps
major_by_index: Dict[int, MajorStep] = {}
if roadmap_ctx.roadmap:
major_by_index = {m.index: m for m in roadmap_ctx.roadmap.major_steps}
by_major: Dict[int, Dict[str, Any]] = {}
for raw in steps:
step = dict(raw)
midx = step.get("roadmap_major_step_index")
if midx is not None:
by_major[int(midx)] = step
out: List[Dict[str, Any]] = []
for spec in sorted(stage_specs, key=lambda s: s.major_step_index):
midx = int(spec.major_step_index)
if midx in by_major:
out.append(by_major[midx])
continue
major = major_by_index.get(midx)
goal = (spec.learning_goal or "").strip()
out.append(
{
"exercise_id": None,
"variant_id": None,
"title": goal or f"Slot {midx + 1}",
"is_ai_proposal": False,
"roadmap_major_step_index": midx,
"roadmap_phase": major.phase if major else None,
"roadmap_learning_goal": goal or None,
"roadmap_match_source": "stage_spec",
"reasons": [],
}
)
return out
def _maybe_rematch_roadmap_after_strip(
cur,
*,
tenant: TenantContext,
body: ProgressionPathSuggestRequest,
goal_query: str,
max_steps: int,
semantic_brief: PlanningSemanticBrief,
path_target_profile: PlanningTargetProfile,
path_intent: str,
roadmap_ctx: ProgressionRoadmapContext,
steps: List[Dict[str, Any]],
stripped_off_topic: List[Dict[str, Any]],
off_topic_before_strip: List[Dict[str, Any]],
roadmap_unfilled: List[Tuple[int, StageSpecArtifact]],
) -> Tuple[
List[Dict[str, Any]],
List[Dict[str, Any]],
List[Dict[str, Any]],
List[Dict[str, Any]],
int,
List[Tuple[int, StageSpecArtifact]],
]:
rematch_log: List[Dict[str, Any]] = []
rematch_rounds = 0
if not body.auto_rematch_after_qa or not roadmap_ctx.stage_specs:
return steps, rematch_log, stripped_off_topic, [], rematch_rounds, roadmap_unfilled
slot_indices, rematch_reasons = collect_rematch_slot_indices(
stripped_off_topic=stripped_off_topic,
off_topic_steps=off_topic_before_strip if not stripped_off_topic else [],
optimization_hints=[],
stage_specs=roadmap_ctx.stage_specs,
)
if not slot_indices:
return steps, rematch_log, stripped_off_topic, [], rematch_rounds, roadmap_unfilled
steps, rematch_log, rematch_new_unfilled = rematch_roadmap_slots(
cur,
tenant=tenant,
body=body,
goal_query=goal_query,
max_steps=max_steps,
semantic_brief=semantic_brief,
path_target_profile=path_target_profile,
path_intent=path_intent,
roadmap_ctx=roadmap_ctx,
steps=steps,
slot_indices=slot_indices,
rematch_reasons=rematch_reasons,
match_slot_fn=_match_roadmap_slot,
)
rematch_rounds = 1
stripped_off_topic = prune_stripped_after_rematch(stripped_off_topic, rematch_log)
if rematch_new_unfilled:
remapped = {sp.major_step_index for _, sp in rematch_new_unfilled}
roadmap_unfilled = [
item for item in roadmap_unfilled if item[1].major_step_index not in remapped
]
roadmap_unfilled.extend(rematch_new_unfilled)
off_topic_steps = detect_off_topic_steps(
cur,
steps,
brief=semantic_brief,
goal_query=goal_query,
)
return (
steps,
rematch_log,
stripped_off_topic,
off_topic_steps,
rematch_rounds,
roadmap_unfilled,
)
def _build_steps_roadmap_first(
cur,
*,
@@ -906,7 +1033,6 @@ def _run_evaluate_only_path_qa(
brief=semantic_brief,
goal_query=goal_query,
)
steps, stripped_off_topic = strip_off_topic_steps_from_path(steps, off_topic_steps)
llm_gap_specs = parse_llm_suggested_new_exercises(
llm_qa,
brief=semantic_brief,
@@ -918,7 +1044,7 @@ def _run_evaluate_only_path_qa(
gap_specs = collect_gap_fill_specs(
steps=steps,
unfilled_gaps=fresh_large_gaps or unfilled_gaps,
off_topic_steps=off_topic_steps if not stripped_off_topic else [],
off_topic_steps=off_topic_steps,
llm_specs=llm_gap_specs,
brief=semantic_brief,
goal_query=goal_query,
@@ -1374,49 +1500,31 @@ def suggest_progression_path(
roadmap_first=roadmap_first,
)
if (
roadmap_first
and body.auto_rematch_after_qa
and roadmap_ctx is not None
and roadmap_ctx.stage_specs
):
slot_indices, rematch_reasons = collect_rematch_slot_indices(
if roadmap_first and roadmap_ctx is not None:
(
steps,
rematch_log,
stripped_off_topic,
rematch_off_topic,
rematch_rounds,
roadmap_unfilled,
) = _maybe_rematch_roadmap_after_strip(
cur,
tenant=tenant,
body=body,
goal_query=goal_query,
max_steps=max_steps,
semantic_brief=semantic_brief,
path_target_profile=path_target_profile,
path_intent=path_intent,
roadmap_ctx=roadmap_ctx,
steps=steps,
stripped_off_topic=stripped_off_topic,
off_topic_steps=off_topic_before_strip if not stripped_off_topic else [],
optimization_hints=[],
stage_specs=roadmap_ctx.stage_specs,
off_topic_before_strip=off_topic_before_strip,
roadmap_unfilled=roadmap_unfilled,
)
if slot_indices:
steps, rematch_log, rematch_new_unfilled = rematch_roadmap_slots(
cur,
tenant=tenant,
body=body,
goal_query=goal_query,
max_steps=max_steps,
semantic_brief=semantic_brief,
path_target_profile=path_target_profile,
path_intent=path_intent,
roadmap_ctx=roadmap_ctx,
steps=steps,
slot_indices=slot_indices,
rematch_reasons=rematch_reasons,
match_slot_fn=_match_roadmap_slot,
)
rematch_rounds = 1
if rematch_new_unfilled:
remapped = {sp.major_step_index for _, sp in rematch_new_unfilled}
roadmap_unfilled = [
item
for item in roadmap_unfilled
if item[1].major_step_index not in remapped
]
roadmap_unfilled.extend(rematch_new_unfilled)
off_topic_steps = detect_off_topic_steps(
cur,
steps,
brief=semantic_brief,
goal_query=goal_query,
)
if rematch_off_topic:
off_topic_steps = rematch_off_topic
gaps = detect_path_gaps(
cur,
steps,
@@ -1500,6 +1608,13 @@ def suggest_progression_path(
path_qa["rematch_log"] = rematch_log
path_qa["rematch_rounds"] = rematch_rounds
if roadmap_first and roadmap_ctx is not None:
steps = _normalize_roadmap_steps_coverage(
steps,
roadmap_ctx=roadmap_ctx,
max_steps=max_steps,
)
target_profile_summary = path_target_profile.to_summary_dict(cur)
retrieval_parts = ["profile_v1", "full_library", "path_builder", "semantics"]
if roadmap_first: