Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

feat: persist Feynman unevaluated explanations in LearningProgress

unknown00338e79b434
diff --git a/deeptutor/capabilities/guided_learning.py b/deeptutor/capabilities/guided_learning.py
index a88d45859..41e0b9333 100644
--- a/deeptutor/capabilities/guided_learning.py
+++ b/deeptutor/capabilities/guided_learning.py
@@ -615,6 +615,9 @@ class GuidedLearningCapability(BaseCapability):
progress, "feynman_check", stream,
)
if response is None:
+ # Persist user explanation so it is not lost on reconnection
+ if kp_id and user_explanation:
+ progress.feynman_explanations[kp_id] = user_explanation
result = {"passed": False, "feedback": "未评估(评估服务暂时不可用)", "gap": ""}
else:
result = self._safe_json_parse(response, default={"passed": False, "feedback": "", "gap": ""})
@@ -624,6 +627,7 @@ class GuidedLearningCapability(BaseCapability):
is_passed = passed is True or str(passed).lower() in ("true", "1", "yes")
if is_passed:
progress.feynman_retries[kp_id] = 0
+ progress.feynman_explanations.pop(kp_id, None)
if kp_id:
progress.mastery_levels[kp_id] = max(progress.mastery_levels.get(kp_id, 0.0), 0.6)
self._advance_after_kp(progress, kps)
diff --git a/deeptutor/learning/models.py b/deeptutor/learning/models.py
index 08c77d688..4245f1cdf 100644
--- a/deeptutor/learning/models.py
+++ b/deeptutor/learning/models.py
@@ -177,6 +177,7 @@ class LearningProgress(BaseModel):
review_queue: list[ReviewTask] = Field(default_factory=list)
module_stage: dict[str, LearningStage] = Field(default_factory=dict)
feynman_retries: dict[str, int] = Field(default_factory=dict)
+ feynman_explanations: dict[str, str] = Field(default_factory=dict)
stage_failure_counts: dict[str, int] = Field(default_factory=dict)
stage_failure_notes: dict[str, str] = Field(default_factory=dict)
learning_mode: Literal["mastery", "exam"] = "mastery"
diff --git a/deeptutor/learning/tests/test_timeout_degradation.py b/deeptutor/learning/tests/test_timeout_degradation.py
index 541ad34d0..cd379eb1a 100644
--- a/deeptutor/learning/tests/test_timeout_degradation.py
+++ b/deeptutor/learning/tests/test_timeout_degradation.py
@@ -314,3 +314,44 @@ async def test_cumulative_failure_below_threshold_retries():
assert cap._call_llm.call_count >= 1
# Failure count should have increased
assert progress.stage_failure_counts["explain"] > 2
+
+
+# ── Feynman explanation persistence ───────────────────────────────────
+
+
+async def test_feynman_failure_persists_explanation():
+ """When LLM fails, user explanation should be saved to feynman_explanations."""
+ cap = _make_capability()
+ cap._call_llm = AsyncMock(side_effect=RuntimeError("LLM unavailable"))
+ progress = _make_progress_with_module()
+ progress.current_stage = LearningStage.FEYNMAN_CHECK
+ stream = FakeStream()
+ stream.inputs = ["My explanation of the concept"]
+
+ await cap._run_feynman_check(progress, None, stream)
+
+ # User explanation should be persisted
+ assert progress.feynman_explanations.get("kp1") == "My explanation of the concept"
+ # Should show "未评估"
+ content_texts = [t for _, t in stream.events if _ == "content"]
+ assert any("未评估" in t for t in content_texts)
+
+
+async def test_feynman_success_clears_explanation():
+ """When LLM evaluation succeeds, feynman_explanations entry should be cleared."""
+ cap = _make_capability()
+ import json as _json
+ success_result = _json.dumps({"passed": True, "feedback": "很好", "gap": ""})
+ cap._call_llm = AsyncMock(return_value=success_result)
+ progress = _make_progress_with_module()
+ progress.current_stage = LearningStage.FEYNMAN_CHECK
+ progress.feynman_explanations["kp1"] = "previous unevaluated explanation"
+ stream = FakeStream()
+ stream.inputs = ["My explanation"]
+
+ await cap._run_feynman_check(progress, None, stream)
+
+ # Should have cleared the previous unevaluated explanation
+ assert "kp1" not in progress.feynman_explanations