fix: harden canonical_answer_text and align decision-core signatures to spec

This commit is contained in:
2026-07-14 15:55:36 -04:00
parent c109f2257a
commit 24ed7ca322
2 changed files with 39 additions and 35 deletions
+18 -18
View File
@@ -7,26 +7,20 @@ from app.question_gen.adversarial_filter import (
judge_flip,
question_hash,
)
from core.types import GeneratedQuestion
def _q(qid="q1", options=("A. 蒸", "B. 炒", "C. 煮", "D. 炸"), answer="A"):
return GeneratedQuestion(
question_id=qid, video_id="v1", task_type="Action Recognition",
question="?", options=options, answer=answer,
source_nodes=("n1",), difficulty="hard",
sub_pattern="temporal_reasoning_failure",
)
_OPTIONS = ("A. 蒸", "B. 炒", "C. 煮", "D. 炸")
def test_question_hash_stable_and_payload_sensitive():
h1 = question_hash(_q())
h2 = question_hash(_q())
h1 = question_hash("?", _OPTIONS, "A")
h2 = question_hash("?", _OPTIONS, "A")
assert h1 == h2
h3 = question_hash(_q(answer="B")) # answer 变 → hash 变
h3 = question_hash("?", _OPTIONS, "B") # answer 变 → hash 变
assert h1 != h3
h4 = question_hash(_q(options=("A. 蒸", "B. 炒", "C. 煮", "D. 烤"))) # option 变 → 变
h4 = question_hash("?", ("A. 蒸", "B. 炒", "C. 煮", "D. 烤"), "A") # option 变 → 变
assert h1 != h4
h5 = question_hash("!", _OPTIONS, "A") # question 变 → 变
assert h1 != h5
def test_agent_config_fingerprint_changes_with_inputs():
@@ -37,14 +31,20 @@ def test_agent_config_fingerprint_changes_with_inputs():
def test_canonical_answer_text_maps_letter_to_option_text():
assert canonical_answer_text(_q(), "C") == ""
assert canonical_answer_text(_q(), "c") == ""
assert canonical_answer_text(_OPTIONS, "C") == ""
assert canonical_answer_text(_OPTIONS, "c") == ""
def test_canonical_answer_text_invalid_returns_none():
assert canonical_answer_text(_q(), "Z") is None
assert canonical_answer_text(_q(), "") is None
assert canonical_answer_text(_q(), None) is None
assert canonical_answer_text(_OPTIONS, "Z") is None
assert canonical_answer_text(_OPTIONS, "") is None
assert canonical_answer_text(_OPTIONS, None) is None
def test_canonical_answer_text_invalid_inputs_return_none():
for bad in ["", "AB", "E", "1", " ", "AA"]:
assert canonical_answer_text(_OPTIONS, bad) is None, bad
assert canonical_answer_text(_OPTIONS, "b") == "" # 合法仍工作(去前缀、大小写不敏感)
def test_judge_flip_different_answers_passed():