fix(mastery): stop a review session offering to start the topic it is teaching

Clicking "start review" opened a session whose first reply was a card
offering to start a session on that same topic. Clicking it opened another,
which offered the card again — one conversation per click, all on one topic,
some bound to no topic at all (#1412). Those conversations begin with nothing
to quiz against, so they answer in prose; that is the other half of the same
report (#1411).

The generator is a premise the tool states as fact. `mastery_new_session`
stays mounted inside a mastery session on purpose — sending the learner to a
*different* topic mid-course is a real thing to want — and its hand-off
instruction tells the model not to tutor the topic here, because "the mastery
tutor picks up on the other side". Inside that topic's own session there is no
other side, and nothing in the tool's result said so, so the model kept
proposing the trip it was already on.

The turn knows which topic it is tutoring; the tool did not. Binding it the
way the mastery tools already get `_mastery_path_id` lets the tool answer for
itself: a hand-off aimed at the topic this conversation *is* gets refused, with
the instruction to teach the lesson here. A hand-off to any other topic is
untouched, and so is `mastery_open_session` — returning to an older
conversation on the same topic is not a loop.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Bingxi Zhao (Frank)
2026-09-14 02:02:53 +08:00
parent 6f618f1abf
commit 2ad1d45d18
4 changed files with 103 additions and 0 deletions

View File

@@ -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:

View File

@@ -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"

View File

@@ -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

View File

@@ -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 (