Skip to content

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

fix(mastery): keep unposted choice prompts from ending turns (#1508)

Bingxi Zhao (Frank)2344f4555aca
diff --git a/deeptutor/capabilities/mastery/loop.py b/deeptutor/capabilities/mastery/loop.py
index b57df2586..faee3f6cf 100644
--- a/deeptutor/capabilities/mastery/loop.py
+++ b/deeptutor/capabilities/mastery/loop.py
@@ -74,6 +74,15 @@ _PROMISE_TAIL_CHARS = 160
# more reliable than recognising the wording, which varies every turn.
_DANGLING_LEAD_IN_CHARS = frozenset("::")
+# A bare instruction to pick from unseen choices is also a promise of a card.
+# #1508 ended with "四个选项,选一个。" after describing the objective, but
+# never called mastery_quiz, so no choices were available to the learner.
+_UNPOSTED_CHOICE_LEAD_IN_RE = re.compile(
+ r"(?:[两三四五六七八九十]|\d+)个选项[,,、:\s]*(?:请|你)?(?:选|挑)(?:一|1)个[。!?.!?]?\s*$|"
+ r"\b(?:three|four|\d+) (?:choices|options)[,:;\s]+(?:choose|pick|select) one[.!?]?\s*$",
+ re.IGNORECASE,
+)
+
# Delivery claims can precede a long explanation, unlike the vague question
# announcements above. Check clauses across the reply, but require both a
# concrete card/question and a delivery verb rather than the word "card" alone.
@@ -326,7 +335,12 @@ class MasteryLoopCapability:
"fail rather than report successful completion."
)
if not state.get("card_posted") and (
- _announces_an_unposed_question(final_text) or _claims_card_delivery(final_text)
+ _announces_an_unposed_question(final_text)
+ or _claims_card_delivery(final_text)
+ or (
+ not state.get("quiz_awaiting_grade")
+ and _UNPOSTED_CHOICE_LEAD_IN_RE.search(final_text)
+ )
):
# Argument binding sets quiz_awaiting_grade even when registration
# fails. Only the tool's success callback marks a card as posted.
diff --git a/tests/capabilities/test_mastery_capability.py b/tests/capabilities/test_mastery_capability.py
index 819811352..3c731f37d 100644
--- a/tests/capabilities/test_mastery_capability.py
+++ b/tests/capabilities/test_mastery_capability.py
@@ -137,6 +137,28 @@ def test_announced_but_unposed_question_is_redirected():
assert "SAME round" in instruction
+ "reply",
+ [
+ "所以这条 KP 我要考的不是「README 该有哪些章节」——那部分你已经做对了。"
+ "是排序和取舍:给那个不跑项目的读者,第一样该补什么。四个选项,选一个。",
+ "Four options, choose one.",
+ ],
+)
+def test_unposted_choice_lead_in_requires_a_card(reply: str) -> None:
+ """#1508: naming unseen choices cannot finish a mastery turn successfully."""
+ instruction = MasteryLoopCapability().finish_instruction(_context(), reply)
+ assert instruction is not None
+ assert "mastery_quiz" in instruction
+
+
+def test_existing_question_can_remind_learner_to_choose() -> None:
+ """A card posted in an earlier turn is still available to answer."""
+ context = _context()
+ context.extension("mastery")["quiz_awaiting_grade"] = True
+ assert MasteryLoopCapability().finish_instruction(context, "四个选项,选一个。") is None
+
+
@pytest.mark.parametrize(
"reply",
[