fix(mastery): drop duplicate ask_user options

This commit is contained in:
XZH
2026-09-11 07:15:59 -07:00
parent 0f8759f730
commit f4dbeea205
2 changed files with 53 additions and 5 deletions

View File

@@ -38,8 +38,7 @@ MAX_PLACEHOLDER_CHARS = 120
# Labels the model sometimes adds as its own catch-all option. The card
# already renders a free-form "Other" row whenever ``allow_free_text``
# is on, so a model-supplied duplicate is dropped (exact match only —
# being clever here risks eating legitimate options).
# is on, so a model-supplied duplicate is dropped.
_REDUNDANT_OTHER_LABELS = frozenset({"other", "其他", "其它"})
@@ -236,6 +235,11 @@ def _drop_half_written_options(raw: Any) -> Any:
return {**raw, "options": kept}
def _option_match_key(label: str) -> str:
"""Compare labels like ``mastery_quiz`` compares option bodies."""
return "".join(label.split()).casefold()
def _build_question(raw: Any, idx: int, used_ids: set[str]) -> AskUserQuestion | str:
if not isinstance(raw, dict):
return f"Question #{idx + 1} must be an object."
@@ -267,11 +271,15 @@ def _build_question(raw: Any, idx: int, used_ids: set[str]) -> AskUserQuestion |
continue
# The card auto-renders an "Other" free-text row; drop a
# model-supplied duplicate so the user never sees two.
if allow_free_text and normalised.label.lower() in _REDUNDANT_OTHER_LABELS:
match_key = _option_match_key(normalised.label)
if allow_free_text and match_key in _REDUNDANT_OTHER_LABELS:
continue
if normalised.label in seen_labels:
# Two labels that differ only by whitespace or case render as the
# same choice, so keep the first rather than shipping an
# unanswerable card (#1409).
if match_key in seen_labels:
continue
seen_labels.add(normalised.label)
seen_labels.add(match_key)
cleaned.append(normalised)
if len(cleaned) >= MAX_OPTIONS:
break

View File

@@ -7,6 +7,8 @@ single-question shorthand which auto-wraps into a one-element list.
from __future__ import annotations
import pytest
from deeptutor.tools.ask_user import (
MAX_HEADER_CHARS,
MAX_OPTION_CHARS,
@@ -283,6 +285,44 @@ def test_v3_drops_model_supplied_other_option() -> None:
assert _labels(payload.questions[0]) == ("A", "B")
@pytest.mark.parametrize(
("options", "expected"),
[
(["First answer", " first ANSWER "], ("First answer",)),
(["Straße", "STRASSE"], ("Straße",)),
(
[
{"label": "First answer", "description": "keep"},
{"label": " first\tanswer ", "description": "drop"},
],
("First answer",),
),
],
)
def test_ask_user_drops_duplicate_labels_like_mastery_quiz(options, expected) -> None:
"""A visible duplicate makes an interactive question unanswerable (#1409)."""
payload, err = build_ask_user_payload(
questions=[{"prompt": "Which answer?", "options": options}]
)
assert err is None
assert payload is not None
assert _labels(payload.questions[0]) == expected
def test_ask_user_deduplicates_even_when_free_text_is_disabled() -> None:
payload, _ = build_ask_user_payload(
questions=[
{
"prompt": "Which answer?",
"options": ["Answer", " answer "],
"allow_free_text": False,
}
]
)
assert payload is not None
assert _labels(payload.questions[0]) == ("Answer",)
def test_v3_keeps_other_option_when_free_text_disabled() -> None:
"""Without the automatic free-text row there is no duplicate to drop."""
payload, _ = build_ask_user_payload(