Skip to content

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

fix(mastery): declare mastery_mode as a rebinding tool

Bingxi Zhao (Frank)8ee2258be438
diff --git a/deeptutor/capabilities/mastery/loop.py b/deeptutor/capabilities/mastery/loop.py
index 987c2a70d..21f37a3a4 100644
--- a/deeptutor/capabilities/mastery/loop.py
+++ b/deeptutor/capabilities/mastery/loop.py
@@ -143,12 +143,20 @@ class MasteryLoopCapability:
name = "mastery"
owned_tools = (*MASTERY_TOOL_NAMES, "read_source")
- # Declared to the dispatcher so a switch that shares a round with a write
- # runs first and the write lands on the path the model switched *to*. Every
- # call in a round is bound before any of them runs, so without this a
- # ``mastery_switch`` + ``mastery_build`` round rebuilt the map of the path
- # the conversation was leaving.
- rebinding_tools = tuple(_PATH_BINDING_TOOLS)
+ # Declared to the dispatcher so a call that repoints the turn runs first
+ # and the rest of the round lands on the new target. Every call in a round
+ # is bound before any of them runs, so without this a ``mastery_switch`` +
+ # ``mastery_build`` round rebuilt the map of the path the conversation was
+ # leaving.
+ #
+ # ``mastery_mode`` belongs here for the same reason and was missing it. It
+ # repoints the turn's mode (``_bind_active_mode``, below), and every
+ # mode-gated tool reads ``_mastery_session_mode`` from the bind — so in the
+ # round the prompt explicitly asks for, "switch to study and get on with
+ # it", ``mastery_quiz`` still saw ``outline`` and was refused. The tutor
+ # had already written the lead-in, the card never appeared, and the turn
+ # carried on: one of the ways a question ends up answered in prose.
+ rebinding_tools = tuple(_PATH_BINDING_TOOLS | {"mastery_mode"})
def is_active(self, context: UnifiedContext) -> bool:
return bool(context.metadata.get("mastery_mode"))
diff --git a/tests/capabilities/test_mastery_capability.py b/tests/capabilities/test_mastery_capability.py
index 2a145f051..9285baa93 100644
--- a/tests/capabilities/test_mastery_capability.py
+++ b/tests/capabilities/test_mastery_capability.py
@@ -527,3 +527,65 @@ async def test_a_switch_and_a_build_in_one_round_land_on_the_switched_path(tmp_p
assert [m.name for m in store.load("path-b").modules] == ["Rebuilt module"]
# The path the turn started on is untouched.
assert [m.name for m in store.load("path-a").modules] == ["Path A"]
+
+
+async def test_a_mode_switch_and_a_quiz_in_one_round_use_the_new_mode(tmp_path, monkeypatch):
+ """The regression behind "it asked a question, then answered it itself".
+
+ ``mastery_mode`` repoints the turn exactly the way ``mastery_switch`` does,
+ but it was not declared to the dispatcher as a rebinding tool — so it ran
+ in the concurrent batch and ``mastery_quiz`` kept the mode bound before the
+ switch. The prompt asks for precisely this round ("switch and get on with
+ it"), and "继续学" at the end of an outline sitting produces it, so the
+ quiz was refused for being in ``outline`` in the same round that left it.
+
+ The tutor had already streamed the lead-in, no card appeared, and the turn
+ carried on — which is one of the ways the question ends up answered in
+ prose next to no card at all.
+ """
+ from deeptutor.runtime.agentic.tool_dispatch import dispatch_tool_calls
+
+ _use_store_root(monkeypatch, tmp_path)
+ LearningStore().save(_built_path("path-a"))
+
+ context = UnifiedContext(
+ user_message="let's start studying",
+ session_id="session-1",
+ metadata={
+ "mastery_mode": True,
+ "mastery_path_id": "path-a",
+ "mastery_session_mode": "outline",
+ "turn_id": "turn-1",
+ },
+ )
+ capability = MasteryLoopCapability()
+ pipeline = AgenticChatPipeline(language="en")
+
+ results = await dispatch_tool_calls(
+ tool_calls=[
+ {
+ "id": "c1",
+ "name": "mastery_quiz",
+ "arguments": json.dumps(
+ {
+ "knowledge_point_id": "path-a-kp1",
+ "question": "What is slope?",
+ "expected_answer": "rise over run",
+ }
+ ),
+ },
+ {"id": "c2", "name": "mastery_mode", "arguments": '{"mode": "study"}'},
+ ],
+ context=context,
+ stream=StreamBus(),
+ source="chat",
+ stage="responding",
+ iteration_index=0,
+ kwarg_augmenter=pipeline._augment_tool_kwargs,
+ rebinding_tools=frozenset(capability.rebinding_tools),
+ )
+
+ quiz = next(message for message in results.tool_messages if message.get("tool_call_id") == "c1")
+ assert "belongs to the" not in str(quiz.get("content", "")), quiz
+ assert context.metadata["mastery_session_mode"] == "study"