DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.
Python ai-agentsai-tutorclawbotcli-tooldeepresearchinteractive-learninglearningmulti-agent-systemsrag
fix(mastery): refresh lesson intent and preserve source diagnostics (#1902)
Bingxi Zhao (Frank)148bb1ec26e0
diff --git a/deeptutor/learning/lesson_evidence.py b/deeptutor/learning/lesson_evidence.pyindex 4168893e6..599f18101 100644--- a/deeptutor/learning/lesson_evidence.py+++ b/deeptutor/learning/lesson_evidence.py@@ -175,12 +175,14 @@ def cached_lesson()if cached.get("model_fingerprint") != model_key:return None- if focus is not None and cached.get("coverage", {}).get("focus", "") != focus:+ topic = store.get_topic(progress.book_id, progress=progress)+ if cached.get("course_intent_fingerprint") != _fingerprint(_course_intent(topic, progress)):return None- try:- validate_evidence(store, progress, kp_id, cached, allow_partial=True)- except LessonUnavailable:+ if focus is not None and cached.get("coverage", {}).get("focus", "") != focus:return None+ # Keep concrete access/source failures visible to the automatic delivery+ # hook; unavailable evidence is not an unprepared objective (#1902).+ validate_evidence(store, progress, kp_id, cached, allow_partial=True)return cached@@ -190,6 +192,15 @@ def _fingerprint(value: Any) -> str:).hexdigest()+def _course_intent(topic: Any, progress: Any) -> dict[str, str]:+ # These inputs steer selection (#1902), but do not invalidate the factual+ # answer keys of already registered questions when teaching scope changes.+ return {+ "course_goal": str(getattr(getattr(topic, "metadata", None), "goal", ""))[:2000],+ "target_level": str(getattr(progress.learner_profile, "target_level", ""))[:500],+ }++def objective_fingerprint(progress: Any, kp_id: str) -> str:kp, module_id, _ = find_knowledge_point(progress, kp_id)if kp is None:@@ -601,6 +612,8 @@ async def prepare_lesson()return {}obj_key = objective_fingerprint(progress, kp_id)+ course_intent = _course_intent(topic, progress)+ intent_key = _fingerprint(course_intent)if stream is not None:await stream.progress("A lecke forrásait és oldalhivatkozásait ellenőrzöm",@@ -618,11 +631,16 @@ async def prepare_lesson("preparation": PREPARATION_VERSION,})- cached = (- None- if refresh- else await asyncio.to_thread(cached_lesson, store, progress, kp_id, config, focus=focus)- )+ try:+ cached = (+ None+ if refresh+ else await asyncio.to_thread(cached_lesson, store, progress, kp_id, config, focus=focus)+ )+ except LessonUnavailable:+ # An explicit preparation can replace stale evidence. The fresh scope+ # above and all newly selected sources below still require authorization.+ cached = Noneif cached is not None:return cachedif not supports_vision(config.binding, config.model):@@ -635,8 +653,7 @@ async def prepare_lesson("objective": kp.name,"module": module.name,"purpose": module.objective,- "course_goal": str(topic.metadata.goal)[:2000],- "target_level": str(getattr(progress.learner_profile, "target_level", ""))[:500],+ **course_intent,"focus": focus,"learner_request": learner_request,}@@ -1018,12 +1035,15 @@ async def prepare_lesson("covered": [entry["supports"] for entry in verified_sources],}evidence = {- "id": _fingerprint([obj_key, scope_key, model_key, verified_sources, coverage]),+ "id": _fingerprint(+ [obj_key, scope_key, model_key, intent_key, verified_sources, coverage]+ ),"schema_version": SCHEMA,"knowledge_point_id": kp_id,"objective_fingerprint": obj_key,"scope_fingerprint": scope_key,"model_fingerprint": model_key,+ "course_intent_fingerprint": intent_key,"status": "ready","sources": verified_sources,"model_observations": verified_observations,@@ -1036,9 +1056,13 @@ async def prepare_lesson(def commit(tx: Any) -> None:current_topic = store.get_topic(path_id, progress=tx.progress)current_scope, _ = scope_snapshot(current_topic, tx.progress, kp_id)- if objective_fingerprint(tx.progress, kp_id) != obj_key or current_scope != scope_key:+ if (+ objective_fingerprint(tx.progress, kp_id) != obj_key+ or current_scope != scope_key+ or _fingerprint(_course_intent(current_topic, tx.progress)) != intent_key+ ):raise LessonUnavailable(- "The objective or sources changed while preparing the lesson. Retry."+ "The objective, course requirements or sources changed while preparing the lesson. Retry.")tx.progress.lesson_evidence[kp_id] = evidencetx.progress.source_grounded = Truediff --git a/tests/services/rag/test_mastery_source_pages.py b/tests/services/rag/test_mastery_source_pages.pyindex 514a9ec54..a591c0aa8 100644--- a/tests/services/rag/test_mastery_source_pages.py+++ b/tests/services/rag/test_mastery_source_pages.py@@ -14,6 +14,7 @@ import pytestfrom deeptutor.learning import lesson_evidence as lessonsfrom deeptutor.learning.models import (KnowledgePoint,+ LearnerProfile,LearningModule,LearningProgress,PendingQuestion,@@ -481,6 +482,96 @@ def test_selector_uses_the_actual_module_learning_purpose(material, monkeypatch)assert "course_goal" in selector_payload[email protected]("field", ["course_goal", "target_level"])+def test_changed_course_intent_reprepares_without_erasing_answer_evidence(+ material, monkeypatch, field+):+ calls = _install_preparation(material, monkeypatch)+ old = asyncio.run(lessons.prepare_lesson(material.store, "path", "kp", material.config))+ service = LearningService(material.store)+ service.register_question(+ "path", PendingQuestion(question_id="q", knowledge_point_id="kp", expected_answer="lipids")+ )+ if field == "course_goal":+ topic = material.store.get_topic("path")+ topic.metadata.goal = "Explain membrane transport for a graduate course."+ material.store.put_topic(topic.metadata, topic.sources)+ else:+ progress = material.store.load("path")+ progress.learner_profile = LearnerProfile(+ target_level="Graduate mechanisms, not introductory names"+ )+ material.store.save(progress)+ progress = material.store.load("path")+ assert lessons.cached_lesson(material.store, progress, "kp", material.config) is None+ fresh = asyncio.run(lessons.prepare_lesson(material.store, "path", "kp", material.config))+ assert fresh["id"] != old["id"] and len(calls) == 6+ assert calls[4][0][field]+ # Changed teaching intent does not change a previously verified factual key.+ graded, _, _ = service.grade_interaction("path", question_id="q", answer="lipids")+ assert len(graded.quiz_attempts) == 1 and graded.quiz_attempts[0].is_correct+++def test_pacing_feedback_keeps_valid_lesson_without_paid_repreparation(material, monkeypatch):+ calls = _install_preparation(material, monkeypatch)+ old = asyncio.run(lessons.prepare_lesson(material.store, "path", "kp", material.config))+ progress = material.store.load("path")+ progress.learner_profile = LearnerProfile(preferences="Give shorter explanations")+ material.store.save(progress)+ current = asyncio.run(lessons.prepare_lesson(material.store, "path", "kp", material.config))+ assert current["id"] == old["id"] and len(calls) == 3++[email protected]("field", ["course_goal", "target_level"])+def test_course_intent_change_during_preparation_cannot_publish_stale_packet(+ material, monkeypatch, field+):+ _install_preparation(material, monkeypatch)+ ask = lessons._ask++ async def change_during_selection(*args, **kwargs):+ result = await ask(*args, **kwargs)+ if field == "course_goal":+ topic = material.store.get_topic("path")+ topic.metadata.goal = "Changed course requirements"+ material.store.put_topic(topic.metadata, topic.sources)+ else:+ progress = material.store.load("path")+ progress.learner_profile = LearnerProfile(target_level="Changed intended level")+ material.store.save(progress)+ return result++ monkeypatch.setattr(lessons, "_ask", change_during_selection)+ with pytest.raises(lessons.LessonUnavailable, match="changed"):+ asyncio.run(lessons.prepare_lesson(material.store, "path", "kp", material.config))+ assert not material.store.load("path").lesson_evidence+++async def test_cached_lesson_reports_revoked_access_without_repeating_preparation(+ material, monkeypatch+):+ from deeptutor.capabilities.mastery.pipeline import MasteryLoopPipeline+ from deeptutor.core.context import UnifiedContext+ from deeptutor.runtime.stream_bus import StreamBus++ calls = _install_preparation(material, monkeypatch)+ await lessons.prepare_lesson(material.store, "path", "kp", material.config)+ monkeypatch.setattr("deeptutor.learning.storage.LearningStore", lambda: material.store)+ monkeypatch.setattr(+ "deeptutor.multi_user.knowledge_access.resolve_kb_document_path", lambda *_args: None+ )+ pipeline = MasteryLoopPipeline(language="en")+ pipeline.llm_config = material.config+ context = UnifiedContext(user_message="Continue", metadata={"mastery_path_id": "path"})+ messages = [{"role": "user", "content": context.user_message}]+ for _ in range(2):+ assert await pipeline.prepare_model_request(context, StreamBus(), messages) == []+ assert "access was revoked" in messages[-1]["content"]+ assert "not prepared for this request" not in messages[-1]["content"]+ assert pipeline._lesson_pixels is None and len(calls) == 3++@pytest.mark.parametrize("count", [4, 7])def test_broad_objective_can_use_more_pages_but_remains_bounded(material, monkeypatch, count):_install_preparation(material, monkeypatch)