DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.
Python ai-agentsai-tutorclawbotcli-tooldeepresearchinteractive-learninglearningmulti-agent-systemsrag
fix(mastery): add explicit question recovery exit
XZHf5e4b7cd4816
diff --git a/deeptutor/capabilities/mastery/capability.py b/deeptutor/capabilities/mastery/capability.pyindex c71af3e04..4c8f12ec5 100644--- a/deeptutor/capabilities/mastery/capability.py+++ b/deeptutor/capabilities/mastery/capability.py@@ -4,10 +4,11 @@ There is no bespoke state machine here anymore. The chat agent loop IS thetutor: this capability only marks the turn as mastery mode and resolves the*initial* active path id, then runs the standard agentic chat pipeline. Thepipeline mounts the mastery tools — the gate tools (``mastery_status`` /-``mastery_quiz`` / ``mastery_grade`` / ``mastery_assess`` / ``mastery_build``)-and the binding tools (``mastery_paths`` / ``mastery_switch`` /-``mastery_leave``), through which the tutor can move the conversation between-paths mid-turn — and injects the tutor playbook; the pure engine in+``mastery_quiz`` / ``mastery_grade`` / ``mastery_skip_question`` /+``mastery_assess`` / ``mastery_build``) and the binding tools+(``mastery_paths`` / ``mastery_switch`` / ``mastery_leave``), through which+the tutor can move the conversation between paths mid-turn — and injects the+tutor playbook; the pure engine in:mod:`deeptutor.learning` owns the hard, per-type mastery gate and thespaced-repetition arithmetic.diff --git a/deeptutor/capabilities/mastery/prompts/en/system.md b/deeptutor/capabilities/mastery/prompts/en/system.mdindex 6b706cdbc..4bf99af20 100644--- a/deeptutor/capabilities/mastery/prompts/en/system.md+++ b/deeptutor/capabilities/mastery/prompts/en/system.md@@ -7,6 +7,7 @@ Then act on the objective:- No objectives yet? Design a path from the learner's materials (use `rag` / `read_source` when materials are attached) and call `mastery_build`. Tag each knowledge point: memory (facts), procedure (step-by-step skills), concept (ideas to understand), design (open-ended judgement).- `probe` (untouched): briefly check whether the learner already knows it before teaching. A test-out is not a silent skip — record its result through the gate (`mastery_assess` for concept / design, `mastery_quiz` + `mastery_grade` for memory / procedure) before advancing. Never move past an objective the engine hasn't marked mastered.- memory / procedure objectives: register the question + its answer with `mastery_quiz`, then ALWAYS present it with the `ask_user` tool so the learner answers on an interactive card — never write the choices as plain numbered text. For multiple choice, pass every full option body to `mastery_quiz.options` in label order (for example `A: ...`, `B: ...`), give the matching `ask_user` options the short labels A / B / C … with those same bodies as their descriptions, and set the correct label as `mastery_quiz`'s `expected_answer`. Never pass bare labels as `mastery_quiz.options`. If `mastery_status.next.pending_question` exists, reuse its question id, prompt, and option labels/bodies exactly; never regenerate or reorder them. If `pending_interaction.status` is `answered`, immediately call `mastery_grade` with its persisted `learner_answer` and `question_id` instead of asking the learner twice. Otherwise present the pending question with `ask_user`. For open questions use `ask_user` free text. When the answer comes back, score it with `mastery_grade`, passing the persisted `question_id`. Keep working the same objective until `mastery_grade` reports `mastered: true`. Always pass `explanation` to `mastery_quiz` (why the expected answer is right): it never appears on the card the learner is answering, but it travels with the attempt into their question bank, where it is the only explanation they will have when reviewing the mistake.+- If the learner explicitly asks to abandon the current question (or it is unrecoverably stuck after retrying an `answered` grade), call `mastery_skip_question`, then continue the same objective with a different `mastery_quiz`. A skipped question gives no mastery credit and must never be used to pass a gate.- concept / design objectives: ask the learner to explain the idea in their own words, judge it, and record the result with `mastery_assess` (`passed: true` only when the explanation truly shows understanding).- `review`: a spaced-repetition item is due — quiz it again to refresh it.- `complete`: congratulate the learner and summarise what they have mastered.diff --git a/deeptutor/capabilities/mastery/prompts/zh/system.md b/deeptutor/capabilities/mastery/prompts/zh/system.mdindex 801c9fcdf..2fb44cda0 100644--- a/deeptutor/capabilities/mastery/prompts/zh/system.md+++ b/deeptutor/capabilities/mastery/prompts/zh/system.md@@ -7,6 +7,7 @@- 还没有任何知识点?根据学习者的材料设计一条路径(材料已挂载时用 `rag` / `read_source`),调用 `mastery_build`。给每个知识点标类型:memory(记忆/事实)、procedure(程序/步骤技能)、concept(概念/需理解)、design(设计/开放判断)。- `probe`(未触碰):先简短探查学习者是否已经会了再教。"测试通过"不等于直接跳过——仍要用门工具记录结果(concept / design 用 `mastery_assess`,memory / procedure 用 `mastery_quiz` + `mastery_grade`)再推进;绝不要越过引擎尚未标记为"已掌握"的知识点。- memory / procedure 类:先用 `mastery_quiz` 登记题目与答案,然后**始终用 `ask_user` 工具**把题目呈现成可点选的卡片让学习者作答——绝不要把选项写成纯文字的 1./2./3.。选择题必须把每个选项的完整正文按标签顺序传入 `mastery_quiz.options`(例如 `A:……`、`B:……`),再给 `ask_user` 使用 A / B / C … 短标签,并把相同正文放进对应 description;正确标签设为 `mastery_quiz` 的 `expected_answer`。绝不能只把 A/B/C/D 裸标签传给 `mastery_quiz.options`。如果 `mastery_status.next.pending_question` 存在,必须原样复用其中的题号、题面和选项标签/正文,绝不重新生成或调整顺序。如果 `pending_interaction.status` 是 `answered`,应立即用其中持久化的 `learner_answer` 和 `question_id` 调用 `mastery_grade`,不要让学习者重复作答;否则再用 `ask_user` 展示待答题目。简答题用 `ask_user` 的自由输入。收到作答后用 `mastery_grade` 批改,并传入持久化的 `question_id`。在 `mastery_grade` 返回 `mastered: true` 之前,持续打磨同一个知识点。每次调用 `mastery_quiz` 都要带上 `explanation`(这个答案为什么对):它不会出现在作答卡片上,但会随作答记录进入学习者的题库,是他们日后复习错题时唯一能看到的解析。+- 如果学习者明确要求放弃当前题目,或重试已提交 (`answered`) 的评分后仍无法恢复,调用 `mastery_skip_question`,然后继续同一知识点并另出一题。被跳过的题目不给任何掌握度,也绝不能用来越过门槛。- concept / design 类:让学习者用自己的话解释该概念,你来判断,并用 `mastery_assess` 记录结果(只有解释确实体现理解时才 `passed: true`)。- `review`:有到期的间隔复习项——再考一次以巩固。- `complete`:祝贺学习者并总结其已掌握的内容。diff --git a/deeptutor/capabilities/mastery/tools.py b/deeptutor/capabilities/mastery/tools.pyindex 2e816e673..00214c052 100644--- a/deeptutor/capabilities/mastery/tools.py+++ b/deeptutor/capabilities/mastery/tools.py@@ -68,6 +68,7 @@ MASTERY_TOOL_NAMES: tuple[str, ...] = ("mastery_status","mastery_quiz","mastery_grade",+ "mastery_skip_question","mastery_assess","mastery_build","mastery_paths",@@ -814,6 +815,51 @@ class MasteryAssessTool(BaseTool):return _json_result(payload, meta_key="mastery_assess")+class MasterySkipQuestionTool(BaseTool):+ """Abandon the open question without inventing a graded result."""++ def get_definition(self) -> ToolDefinition:+ return ToolDefinition(+ name="mastery_skip_question",+ description=(+ "Abandon the currently open mastery question without grading "+ "it. This keeps every attempt and mastery level already earned, "+ "but gives no credit for the abandoned question. Use it only "+ "when the learner explicitly asks to skip this question or the "+ "question is unrecoverably stuck; if mastery_status reports an "+ "answered interaction, retry mastery_grade first."+ ),+ parameters=[],+ )++ async def execute(self, **kwargs: Any) -> ToolResult:+ path_id = _resolve_path_id(kwargs)+ if not path_id:+ return _no_path_result()++ service = _new_service()+ if _load_path(service, path_id) is None:+ return _no_built_path_result("mastery_skip_question")++ interaction = service.store.get_active_interaction(path_id)+ progress, skipped = service.abandon_active_question(path_id)+ payload = {+ "status": "skipped" if skipped else "no_pending_question",+ "skipped": skipped,+ "path_revision": progress.version,+ "question_id": interaction.interaction_id if interaction is not None else "",+ "next": next_objective(progress).to_dict(),+ "instruction": (+ "The question was abandoned without an attempt or mastery credit. "+ "Continue the objective from mastery_status.next and register a "+ "different question with mastery_quiz."+ if skipped+ else "No question was open; follow mastery_status.next."+ ),+ }+ return _json_result(payload, meta_key="mastery_skip_question")++class MasteryBuildTool(BaseTool):"""Create / extend the skill map from objectives the tutor designed."""@@ -1118,6 +1164,7 @@ MASTERY_TOOL_TYPES: tuple[type[BaseTool], ...] = (MasteryStatusTool,MasteryQuizTool,MasteryGradeTool,+ MasterySkipQuestionTool,MasteryAssessTool,MasteryBuildTool,MasteryPathsTool,@@ -1135,6 +1182,7 @@ __all__ = ["MasteryLeaveTool","MasteryPathsTool","MasteryQuizTool",+ "MasterySkipQuestionTool","MasteryStatusTool","MasterySwitchTool",]diff --git a/deeptutor/learning/tests/test_mastery_tools.py b/deeptutor/learning/tests/test_mastery_tools.pyindex dd7e877e9..98ee25ab3 100644--- a/deeptutor/learning/tests/test_mastery_tools.py+++ b/deeptutor/learning/tests/test_mastery_tools.py@@ -19,6 +19,7 @@ from deeptutor.tools.mastery_tool import (MasteryLeaveTool,MasteryPathsTool,MasteryQuizTool,+ MasterySkipQuestionTool,MasteryStatusTool,MasterySwitchTool,)@@ -223,6 +224,80 @@ async def test_grade_without_pending_fails(path_id):assert result.success is False+async def test_skip_question_unblocks_registration_without_credit(path_id):+ await _build_basic(path_id)+ status = json.loads((await MasteryStatusTool().execute(_mastery_path_id=path_id)).content)+ kp_id = status["next"]["knowledge_point_id"]+ first = json.loads(+ (+ await MasteryQuizTool().execute(+ _mastery_path_id=path_id,+ knowledge_point_id=kp_id,+ question="First?",+ expected_answer="right",+ )+ ).content+ )+ from deeptutor.learning.service import LearningService++ LearningService().record_question_answer(+ path_id,+ "wrong",+ interaction_id=first["question_id"],+ )+ before = LearningStore().load(path_id)+ assert before is not None+ mastery_before = before.mastery_levels.get(kp_id, 0.0)++ result = await MasterySkipQuestionTool().execute(_mastery_path_id=path_id)+ skipped = json.loads(result.content)+ progress = LearningStore().load(path_id)+ abandoned = LearningStore().get_interaction(path_id, first["question_id"])++ assert result.success is True+ assert skipped["skipped"] is True+ assert skipped["question_id"] == first["question_id"]+ assert skipped["next"]["action"] != "answer_pending"+ assert progress is not None+ assert progress.pending_question is None+ assert progress.quiz_attempts == []+ assert progress.mastery_levels.get(kp_id, 0.0) == mastery_before+ assert abandoned is not None+ assert abandoned.status == InteractionStatus.ABANDONED+ assert LearningStore().get_active_interaction(path_id) is None++ replacement = json.loads(+ (+ await MasteryQuizTool().execute(+ _mastery_path_id=path_id,+ knowledge_point_id=kp_id,+ question="Replacement?",+ expected_answer="right",+ )+ ).content+ )+ assert replacement["status"] == "registered"+ assert replacement["question_id"] != first["question_id"]+++async def test_skip_question_without_open_question_is_no_op(path_id):+ await _build_basic(path_id)+ before = LearningStore().load(path_id)+ assert before is not None++ result = await MasterySkipQuestionTool().execute(_mastery_path_id=path_id)+ payload = json.loads(result.content)+ after = LearningStore().load(path_id)++ assert result.success is True+ assert payload["skipped"] is False+ assert payload["question_id"] == ""+ assert after is not None+ assert after.version == before.version++@pytest.mark.asyncioasync def test_quiz_unknown_kp_fails(path_id):await _build_basic(path_id)diff --git a/deeptutor/tools/mastery_tool.py b/deeptutor/tools/mastery_tool.pyindex 0333f146c..316b90f4f 100644--- a/deeptutor/tools/mastery_tool.py+++ b/deeptutor/tools/mastery_tool.py@@ -15,6 +15,7 @@ from deeptutor.capabilities.mastery.tools import (MasteryLeaveTool,MasteryPathsTool,MasteryQuizTool,+ MasterySkipQuestionTool,MasteryStatusTool,MasterySwitchTool,)@@ -28,6 +29,7 @@ __all__ = ["MasteryLeaveTool","MasteryPathsTool","MasteryQuizTool",+ "MasterySkipQuestionTool","MasteryStatusTool","MasterySwitchTool",]