Skip to content

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

fix: reject duplicate mastery quiz options (#956)

pederbe1e99da1a93d3
diff --git a/deeptutor/capabilities/mastery/tools.py b/deeptutor/capabilities/mastery/tools.py
index 94344ece8..30d44eb75 100644
--- a/deeptutor/capabilities/mastery/tools.py
+++ b/deeptutor/capabilities/mastery/tools.py
@@ -167,7 +167,12 @@ def _normalize_quiz_contract(
"the labels A/B/C/D. Retry mastery_quiz with the exact option "
"descriptions you will show through ask_user."
)
-
+ normalized_bodies = {" ".join(body.split()).casefold() for body in choice_options.values()}
+ if len(normalized_bodies) != len(choice_options):
+ raise ValueError(
+ "Choice option bodies must be unique; retry mastery_quiz with "
+ "distinct answer text for every option."
+ )
resolved_expected = resolve_answer(expected_answer, choice_options)
if not resolved_expected:
raise ValueError(
diff --git a/deeptutor/learning/pending.py b/deeptutor/learning/pending.py
index 0c22547fb..e27f3e59f 100644
--- a/deeptutor/learning/pending.py
+++ b/deeptutor/learning/pending.py
@@ -17,7 +17,7 @@ if TYPE_CHECKING:
from deeptutor.learning.models import PendingQuestion
-OPTION_PREFIX_RE = re.compile(r"^\s*([A-Z])\s*[.::、))-]\s*(.+)$", re.IGNORECASE)
+OPTION_PREFIX_RE = re.compile(r"^\s*([A-Z])\s*[.::、))-]\s*(.+)$", re.IGNORECASE | re.DOTALL)
def parse_options(options: list[str]) -> dict[str, str]:
diff --git a/deeptutor/learning/tests/test_mastery_choices.py b/deeptutor/learning/tests/test_mastery_choices.py
index fda8e3b09..c4fb0f194 100644
--- a/deeptutor/learning/tests/test_mastery_choices.py
+++ b/deeptutor/learning/tests/test_mastery_choices.py
@@ -29,6 +29,13 @@ def test_parse_options_reads_labelled_bodies():
}
+def test_parse_options_reads_multiline_bodies():
+ assert parse_options(["A: first\nanswer", "B: second answer"]) == {
+ "A": "first\nanswer",
+ "B": "second answer",
+ }
+
+
def test_parse_options_keeps_bare_labels_for_legacy_data():
assert parse_options(["A", "B", "C", "D"]) == {"A": "A", "B": "B", "C": "C", "D": "D"}
diff --git a/deeptutor/learning/tests/test_mastery_tools.py b/deeptutor/learning/tests/test_mastery_tools.py
index 8d024a9ee..3ed3e87d1 100644
--- a/deeptutor/learning/tests/test_mastery_tools.py
+++ b/deeptutor/learning/tests/test_mastery_tools.py
@@ -354,6 +354,10 @@ async def test_explicit_non_choice_rejects_options(path_id, question_type):
[
("A: first, B: second", "must be an array"),
(["A: first", "A: second", "B: third"], "labels must be unique"),
+ (["A: repeated answer", "B: repeated answer"], "bodies must be unique"),
+ (["A: Repeated answer", "B: repeated answer"], "bodies must be unique"),
+ (["A: repeated\nanswer", "B: repeated answer"], "bodies must be unique"),
+ (["A: Straße", "B: STRASSE"], "bodies must be unique"),
(["A: first", ""], "non-empty strings"),
],
)
@@ -373,6 +377,7 @@ async def test_choice_quiz_rejects_malformed_options(path_id, options, error):
assert result.success is False
assert error in result.content
+ assert LearningStore().load(path_id).pending_question is None
@pytest.mark.asyncio