diff --git a/deeptutor/capabilities/mastery/loop.py b/deeptutor/capabilities/mastery/loop.py index 21f37a3a4..dc3f3ff73 100644 --- a/deeptutor/capabilities/mastery/loop.py +++ b/deeptutor/capabilities/mastery/loop.py @@ -23,6 +23,7 @@ from deeptutor.capabilities.mastery.tools import MASTERY_TOOL_NAMES from deeptutor.capabilities.protocol import PromptBlock from deeptutor.core.context import UnifiedContext from deeptutor.services.prompt.lookup import prompt_text as _prompt_text +from deeptutor.tools.mastery_nav import MASTERY_NAV_TOOL_NAMES logger = logging.getLogger(__name__) @@ -232,6 +233,15 @@ class MasteryLoopCapability: # needing to know a turn context exists. updated["_bind_active_path"] = _path_binder(context) return updated + if tool_name in MASTERY_NAV_TOOL_NAMES: + # The navigation tools stay mounted inside a mastery session — + # sending the learner to a *different* topic is a real thing to + # want mid-course. What they could not see is which topic this + # turn is already tutoring, so a hand-off card could point back at + # this very conversation's topic (#1412). + updated = dict(kwargs) + updated["_tutoring_path_id"] = path_id + return updated return kwargs def finish_instruction(self, context: UnifiedContext, final_text: str) -> str | None: diff --git a/deeptutor/learning/tests/test_mastery_navigation.py b/deeptutor/learning/tests/test_mastery_navigation.py index 0d52e1656..3da4d62ba 100644 --- a/deeptutor/learning/tests/test_mastery_navigation.py +++ b/deeptutor/learning/tests/test_mastery_navigation.py @@ -327,3 +327,46 @@ async def test_an_invented_lesson_is_refused_with_the_real_outline(store): assert result.success is False assert "Descriptive statistics" in result.content assert "Sampling distributions" in result.content + + +@pytest.mark.asyncio +async def test_new_session_refuses_to_restart_the_topic_it_is_already_teaching(store): + """The review session's own opening turn used to answer with a start card. + + `mastery_new_session` stays mounted inside a mastery session so the tutor + can send the learner to another topic, but its hand-off instruction says + the mastery tutor picks the topic up "on the other side" — false when this + window already is that side. The model followed it, the learner clicked, + the draft route opened one more conversation on the same topic, and the + banner in the new one offered the same card again (#1412). The churned + conversations carry no study context, which is where the replies with no + question card come from (#1411). + """ + _create_topic() + + result = await MasteryNewSessionTool().execute( + path_id="stats_101", + opening_message="Review lesson 1 with me", + _tutoring_path_id="stats_101", + ) + + assert result.success is False + assert "already the study session" in result.content + + +@pytest.mark.asyncio +async def test_new_session_still_hands_off_to_a_different_topic_mid_course(store): + """Sending the learner to another topic is the tool working as intended.""" + _create_topic() + _create_topic("ml_path", "Machine Learning") + + payload = _payload( + await MasteryNewSessionTool().execute( + path_id="ml_path", + opening_message="Start me on machine learning", + _tutoring_path_id="stats_101", + ) + ) + + assert payload["kind"] == "new" + assert payload["path_id"] == "ml_path" diff --git a/deeptutor/tools/mastery_nav.py b/deeptutor/tools/mastery_nav.py index 6356c0e21..96aa993a0 100644 --- a/deeptutor/tools/mastery_nav.py +++ b/deeptutor/tools/mastery_nav.py @@ -91,6 +91,36 @@ async def _topic_or_error(path_id: str) -> tuple[dict[str, Any] | None, ToolResu return topic, None +def _handoff_lands_where_we_already_are( + topic: dict[str, Any], + tutoring_path_id: Any, +) -> ToolResult | None: + """Refuse a "start a session on this topic" card inside that topic's session. + + ``_HANDOFF_INSTRUCTION`` tells the model not to tutor the topic in this + window, because the mastery tutor picks it up on the other side. Inside a + mastery session that premise is false — this window *is* the topic's study + session — and nothing in the tool's result told the model otherwise, so it + answered the opening turn with a card offering to start the very topic it + was already teaching. Each click opens the draft route and creates one + more conversation (#1412); those conversations begin with no study context + to quiz against, which is where the replies that carry no question card + come from (#1411). + + A hand-off to a *different* topic is the tool working as intended and is + left alone. + """ + current = _text(tutoring_path_id) + if not current or current != _text(topic.get("path_id")): + return None + return _failure( + "This conversation is already the study session for that topic, so a " + "card would only start a second one on it. Teach the lesson here " + "instead. Reserve mastery_new_session for sending the learner to a " + "different topic." + ) + + def _module_or_error( topic: dict[str, Any], module_ref: str ) -> tuple[dict[str, Any] | None, ToolResult | None]: @@ -434,6 +464,9 @@ class MasteryNewSessionTool(_NavTool): topic, error = await _topic_or_error(kwargs.get("path_id", "")) if error is not None or topic is None: return error or _failure("path_id is required.") + looping = _handoff_lands_where_we_already_are(topic, kwargs.get("_tutoring_path_id")) + if looping is not None: + return looping module, module_error = _module_or_error(topic, kwargs.get("module", "")) if module_error is not None: return module_error diff --git a/tests/capabilities/test_mastery_capability.py b/tests/capabilities/test_mastery_capability.py index 9285baa93..1d2a01516 100644 --- a/tests/capabilities/test_mastery_capability.py +++ b/tests/capabilities/test_mastery_capability.py @@ -271,6 +271,23 @@ def test_read_source_is_owned_and_reads_the_topic_index_on_demand(): assert kwargs["source_index"] == {"bk-path-1-ch1": "chapter one text"} +def test_navigation_tools_learn_which_topic_the_turn_is_tutoring(): + """A hand-off card must be able to tell "elsewhere" from "right here". + + The four navigation tools stay mounted inside a mastery session because + sending the learner to another topic mid-course is a real thing to want. + They just had no way to see the topic this conversation is already + teaching, so a card could point back at it (#1412). + """ + context = _context() + + bound = MasteryLoopCapability().augment_kwargs( + "mastery_new_session", {"path_id": "path-1"}, context + ) + + assert bound["_tutoring_path_id"] == "path-1" + + @pytest.mark.asyncio async def test_mastery_sync_carries_provenance_to_question_bank(tmp_path, monkeypatch) -> None: from deeptutor.capabilities.mastery.tools import (