Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- ai_question_manager.py +108 -37
- deterministic_question_executor.py +36 -11
ai_question_manager.py
CHANGED
|
@@ -55,8 +55,8 @@ from recipe_compiler import (
|
|
| 55 |
|
| 56 |
_INTERACTION_ENGINE = get_interaction_engine()
|
| 57 |
|
| 58 |
-
VERSION = "
|
| 59 |
-
POLICY_VERSION = "
|
| 60 |
|
| 61 |
SUBJECTS_REGISTRY_FILE = (
|
| 62 |
os.getenv("SUBJECTS_REGISTRY_FILE", "subjects_registry.json").strip()
|
|
@@ -93,27 +93,36 @@ ORAL_RESPONSE_MODES = {"oral", "oral_response", "spoken", "spoken_response", "sp
|
|
| 93 |
EXTERNAL_RESPONSE_MODES = {"drawing_response", "whiteboard_response", "whiteboard", "graph_interaction", "interactive_game", "tool_state"}
|
| 94 |
|
| 95 |
|
| 96 |
-
AGENT_VERSION = "
|
| 97 |
AGENT_MAX_ATTEMPTS = max(1, min(2, int(os.getenv("QUESTION_AGENT_MAX_ATTEMPTS", "1") or 1)))
|
| 98 |
AGENT_MEMORY_MAX_TASKS = max(8, min(128, int(os.getenv("QUESTION_AGENT_MEMORY_MAX_TASKS", "32") or 32)))
|
| 99 |
AGENT_MEMORY_MAX_EVENTS = max(8, min(64, int(os.getenv("QUESTION_AGENT_MEMORY_MAX_EVENTS", "20") or 20)))
|
| 100 |
-
AGENT_MAX_TOOL_ROUNDS =
|
|
|
|
| 101 |
|
| 102 |
# Fixed prompt/policy. The model is not allowed to expand its own scope.
|
| 103 |
QUESTION_AGENT_SYSTEM_PROMPT = """
|
| 104 |
-
أنت
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
لا
|
| 111 |
-
|
| 112 |
-
لا ت
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
""".strip()
|
| 118 |
|
| 119 |
QUESTION_AGENT_TOOLS = [
|
|
@@ -685,10 +694,10 @@ class AIQuestionManager:
|
|
| 685 |
}
|
| 686 |
|
| 687 |
teacher_memory_path = _text(
|
| 688 |
-
cfg.get("
|
| 689 |
-
or cfg.get("
|
| 690 |
-
or cfg.get("
|
| 691 |
-
or f"diagnostics/{subject_id}/{subject_id}
|
| 692 |
)
|
| 693 |
teacher_memory_doc = self._load_recipe_document_by_path(
|
| 694 |
teacher_memory_path, repo_key=repo_key
|
|
@@ -721,7 +730,7 @@ class AIQuestionManager:
|
|
| 721 |
f"GLOBAL={bool(self.global_recipe_document)} "
|
| 722 |
f"SUBJECT_DOCS={len(self.subject_recipe_documents)} "
|
| 723 |
f"SUBJECT_POLICIES={len(self.subject_policy_documents)} "
|
| 724 |
-
f"
|
| 725 |
f"POLICY_COMPILER={SUBJECT_POLICY_COMPILER_VERSION} "
|
| 726 |
"AUTHORITY_ENVELOPE=contract_once+recipe_once "
|
| 727 |
f"RECIPES_INDEXED={len(self.recipes_by_id)} VERSION={VERSION}"
|
|
@@ -734,7 +743,7 @@ class AIQuestionManager:
|
|
| 734 |
"templates_loaded": len(self.templates_by_id) or sum(len(v) for v in self.templates_by_skill.values()),
|
| 735 |
"recipes_loaded": len(self.recipes_by_id),
|
| 736 |
"subject_policies_loaded": len(self.subject_policy_documents),
|
| 737 |
-
"
|
| 738 |
"subject_policy_compiler": SUBJECT_POLICY_COMPILER_VERSION,
|
| 739 |
"canonical_recipe_subjects": subject_recipe_loads,
|
| 740 |
"version": VERSION,
|
|
@@ -1749,7 +1758,7 @@ class AIQuestionManager:
|
|
| 1749 |
if text and text not in excluded:
|
| 1750 |
excluded.append(text)
|
| 1751 |
|
| 1752 |
-
role = _text(_as_dict(teacher_memory.get("family")).get("role")) or "م
|
| 1753 |
target_instruction = (
|
| 1754 |
f"أنشئ audio_text عربيًا فصيحًا طبيعيًا من {preferred_count} كلمات بالضبط."
|
| 1755 |
if preferred_count > 0
|
|
@@ -1758,7 +1767,7 @@ class AIQuestionManager:
|
|
| 1758 |
|
| 1759 |
return {
|
| 1760 |
"enabled": enabled,
|
| 1761 |
-
"task_version": "
|
| 1762 |
"subject": subject_id,
|
| 1763 |
"role": role,
|
| 1764 |
"skill_id": _text(contract.get("skill_id")),
|
|
@@ -2000,7 +2009,7 @@ class AIQuestionManager:
|
|
| 2000 |
if isinstance(semantic_micro_task, dict) else {}
|
| 2001 |
),
|
| 2002 |
"instruction": (
|
| 2003 |
-
"نفّذ semantic_micro_task أولًا كم
|
| 2004 |
"ثم سلّم الحقول المسموحة فقط عبر submit_question_candidate. "
|
| 2005 |
"لا تطلب أدوات قراءة أخرى."
|
| 2006 |
),
|
|
@@ -2186,18 +2195,40 @@ class AIQuestionManager:
|
|
| 2186 |
self,
|
| 2187 |
task_key: str,
|
| 2188 |
tool_results: Dict[str, Any],
|
|
|
|
| 2189 |
) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2190 |
manifest = self.agent_tool_manifest()
|
| 2191 |
memory = self._agent_memory_snapshot(task_key)
|
| 2192 |
compact_results = self._compact_agent_tool_results(tool_results)
|
| 2193 |
return (
|
| 2194 |
QUESTION_AGENT_SYSTEM_PROMPT
|
| 2195 |
-
+ "\n\nصلاحيات الأدوات المسموحة
|
| 2196 |
+ json.dumps(manifest, ensure_ascii=False, separators=(",", ":"))
|
| 2197 |
-
+ "\n\nنتائج الأدوات المقروءة
|
| 2198 |
-
+ json.dumps(compact_results, ensure_ascii=False, default=str, separators=(",", ":"))[:
|
| 2199 |
-
+ "\n\nذاكرة العمل
|
| 2200 |
-
+ json.dumps(memory, ensure_ascii=False, default=str, separators=(",", ":"))[:
|
| 2201 |
)
|
| 2202 |
|
| 2203 |
def _build_native_repair_packet(
|
|
@@ -2452,10 +2483,32 @@ class AIQuestionManager:
|
|
| 2452 |
"""
|
| 2453 |
request = copy.deepcopy(single_request)
|
| 2454 |
messages: List[Dict[str, Any]] = []
|
| 2455 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2456 |
submission_attempts = 0
|
| 2457 |
last_rejected_submission_digest = ""
|
| 2458 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2459 |
for model_turn in range(AGENT_MAX_TOOL_ROUNDS):
|
| 2460 |
structured = request.get("structured_generation_payload")
|
| 2461 |
if not isinstance(structured, dict):
|
|
@@ -2600,9 +2653,9 @@ class AIQuestionManager:
|
|
| 2600 |
print(
|
| 2601 |
"[AI QUESTION SUBMIT VALIDATOR] "
|
| 2602 |
f"SKILL={contract.get('skill_id','')} PHASE={phase} VALID=False "
|
| 2603 |
-
f"ATTEMPT={submission_attempts}/
|
| 2604 |
)
|
| 2605 |
-
if submission_attempts >=
|
| 2606 |
return {
|
| 2607 |
"success": False,
|
| 2608 |
"ready": False,
|
|
@@ -4214,7 +4267,25 @@ class AIQuestionManager:
|
|
| 4214 |
for agent_attempt in range(1, AGENT_MAX_ATTEMPTS + 1):
|
| 4215 |
memory["attempts"] = int(memory.get("attempts", 0) or 0) + 1
|
| 4216 |
single_request = self._make_single_request(single_contract, template, sequence=sequence)
|
| 4217 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4218 |
policy_constraints = [
|
| 4219 |
_text(x)
|
| 4220 |
for x in _as_list(recipe.get("subject_policy_qwen_constraints"))
|
|
@@ -4774,8 +4845,8 @@ class AIQuestionManager:
|
|
| 4774 |
"subject_policy_pre_generation_alignment": True,
|
| 4775 |
"subject_policy_compiler": SUBJECT_POLICY_COMPILER_VERSION,
|
| 4776 |
"question_form_authority_envelope": "contract_once+recipe_once",
|
| 4777 |
-
"
|
| 4778 |
-
"
|
| 4779 |
"duplicate_effective_recipe_prompt_blocks": False,
|
| 4780 |
"contract_visual_semantic_only": True,
|
| 4781 |
"written_word_labels_model_writable": False,
|
|
|
|
| 55 |
|
| 56 |
_INTERACTION_ENGINE = get_interaction_engine()
|
| 57 |
|
| 58 |
+
VERSION = "ai_question_manager_v32_question_author_agent_direct_semantic_submit"
|
| 59 |
+
POLICY_VERSION = "ai_question_manager_policy_v32_question_author_direct_semantic_recovery"
|
| 60 |
|
| 61 |
SUBJECTS_REGISTRY_FILE = (
|
| 62 |
os.getenv("SUBJECTS_REGISTRY_FILE", "subjects_registry.json").strip()
|
|
|
|
| 93 |
EXTERNAL_RESPONSE_MODES = {"drawing_response", "whiteboard_response", "whiteboard", "graph_interaction", "interactive_game", "tool_state"}
|
| 94 |
|
| 95 |
|
| 96 |
+
AGENT_VERSION = "question_agent_v27_assessment_author_direct_semantic_submit"
|
| 97 |
AGENT_MAX_ATTEMPTS = max(1, min(2, int(os.getenv("QUESTION_AGENT_MAX_ATTEMPTS", "1") or 1)))
|
| 98 |
AGENT_MEMORY_MAX_TASKS = max(8, min(128, int(os.getenv("QUESTION_AGENT_MEMORY_MAX_TASKS", "32") or 32)))
|
| 99 |
AGENT_MEMORY_MAX_EVENTS = max(8, min(64, int(os.getenv("QUESTION_AGENT_MEMORY_MAX_EVENTS", "20") or 20)))
|
| 100 |
+
AGENT_MAX_TOOL_ROUNDS = 4 # general: context + up to 3 submits; semantic microtask starts directly at submit
|
| 101 |
+
AGENT_MAX_SUBMISSIONS = 3
|
| 102 |
|
| 103 |
# Fixed prompt/policy. The model is not allowed to expand its own scope.
|
| 104 |
QUESTION_AGENT_SYSTEM_PROMPT = """
|
| 105 |
+
أنت Question Authoring Agent داخل منصة Divid Teacher.
|
| 106 |
+
مهمتك: تأليف المحتوى المسموح لسؤال تشخيصي معتمد وتسليمه وفق Schema.
|
| 107 |
+
|
| 108 |
+
حدودك:
|
| 109 |
+
- Diagnostic Contract هو الحقيقة التشخيصية ولا تغيّر skill_id أو template_id أو Q-Matrix أو diagnostic condition أو scoring.
|
| 110 |
+
- Recipe تحدد شكل السؤال والوسائط والقيود.
|
| 111 |
+
- النظام يملك الميكانيك والإجابة والحفظ والتقدم عندما تكون System-owned.
|
| 112 |
+
- أنت تملك فقط model_writable_fields.
|
| 113 |
+
- لا تحفظ في البنك ولا تمنح إتقانًا ولا تغيّر تقدم الطالب.
|
| 114 |
+
|
| 115 |
+
طريقة العمل:
|
| 116 |
+
1) افهم construct المطلوب قبل الكتابة.
|
| 117 |
+
2) ركز على جودة المحتوى، لا على إرضاء Validator شكليًا فقط.
|
| 118 |
+
3) في المهمة اللغوية القصيرة كوّن داخليًا عدة مرشحين.
|
| 119 |
+
4) استبعد الغريب والمبهم والمكرر والمخالف للعمر أو modality.
|
| 120 |
+
5) نفّذ self-check قبل submit.
|
| 121 |
+
6) عند الرفض أصلح السبب مع الحفاظ على نفس العقد ولا تكرر القيمة المرفوضة.
|
| 122 |
+
|
| 123 |
+
لا تكشف correct_answer في نص السؤال.
|
| 124 |
+
إذا كان السؤال صوتيًا فـ audio_text هو المثير فقط ولا تحوّل الاس��ماع إلى قراءة.
|
| 125 |
+
أخرج عبر الأداة المطلوبة فقط؛ لا Markdown ولا شرح خارجي.
|
| 126 |
""".strip()
|
| 127 |
|
| 128 |
QUESTION_AGENT_TOOLS = [
|
|
|
|
| 694 |
}
|
| 695 |
|
| 696 |
teacher_memory_path = _text(
|
| 697 |
+
cfg.get("question_generation_memory_path")
|
| 698 |
+
or cfg.get("question_author_memory_path")
|
| 699 |
+
or cfg.get("subject_question_memory_path")
|
| 700 |
+
or f"diagnostics/{subject_id}/{subject_id}_question_generation_memory.json"
|
| 701 |
)
|
| 702 |
teacher_memory_doc = self._load_recipe_document_by_path(
|
| 703 |
teacher_memory_path, repo_key=repo_key
|
|
|
|
| 730 |
f"GLOBAL={bool(self.global_recipe_document)} "
|
| 731 |
f"SUBJECT_DOCS={len(self.subject_recipe_documents)} "
|
| 732 |
f"SUBJECT_POLICIES={len(self.subject_policy_documents)} "
|
| 733 |
+
f"QUESTION_MEMORIES={len(self.subject_teacher_memories)} "
|
| 734 |
f"POLICY_COMPILER={SUBJECT_POLICY_COMPILER_VERSION} "
|
| 735 |
"AUTHORITY_ENVELOPE=contract_once+recipe_once "
|
| 736 |
f"RECIPES_INDEXED={len(self.recipes_by_id)} VERSION={VERSION}"
|
|
|
|
| 743 |
"templates_loaded": len(self.templates_by_id) or sum(len(v) for v in self.templates_by_skill.values()),
|
| 744 |
"recipes_loaded": len(self.recipes_by_id),
|
| 745 |
"subject_policies_loaded": len(self.subject_policy_documents),
|
| 746 |
+
"question_memories_loaded": len(self.subject_teacher_memories),
|
| 747 |
"subject_policy_compiler": SUBJECT_POLICY_COMPILER_VERSION,
|
| 748 |
"canonical_recipe_subjects": subject_recipe_loads,
|
| 749 |
"version": VERSION,
|
|
|
|
| 1758 |
if text and text not in excluded:
|
| 1759 |
excluded.append(text)
|
| 1760 |
|
| 1761 |
+
role = _text(_as_dict(teacher_memory.get("family")).get("role")) or "مؤلف أسئلة تقييم عربية"
|
| 1762 |
target_instruction = (
|
| 1763 |
f"أنشئ audio_text عربيًا فصيحًا طبيعيًا من {preferred_count} كلمات بالضبط."
|
| 1764 |
if preferred_count > 0
|
|
|
|
| 1767 |
|
| 1768 |
return {
|
| 1769 |
"enabled": enabled,
|
| 1770 |
+
"task_version": "arabic_question_author_microtask_v2",
|
| 1771 |
"subject": subject_id,
|
| 1772 |
"role": role,
|
| 1773 |
"skill_id": _text(contract.get("skill_id")),
|
|
|
|
| 2009 |
if isinstance(semantic_micro_task, dict) else {}
|
| 2010 |
),
|
| 2011 |
"instruction": (
|
| 2012 |
+
"نفّذ semantic_micro_task أولًا كمؤلف سؤال تقييم عربي، ثم نفّذ self_check داخليًا، "
|
| 2013 |
"ثم سلّم الحقول المسموحة فقط عبر submit_question_candidate. "
|
| 2014 |
"لا تطلب أدوات قراءة أخرى."
|
| 2015 |
),
|
|
|
|
| 2195 |
self,
|
| 2196 |
task_key: str,
|
| 2197 |
tool_results: Dict[str, Any],
|
| 2198 |
+
semantic_micro_task: Optional[Dict[str, Any]] = None,
|
| 2199 |
) -> str:
|
| 2200 |
+
micro = _as_dict(semantic_micro_task)
|
| 2201 |
+
if micro.get("enabled") is True:
|
| 2202 |
+
author_memory = _as_dict(micro.get("teacher_memory"))
|
| 2203 |
+
family_memory = _as_dict(author_memory.get("family"))
|
| 2204 |
+
wc = _as_dict(micro.get("word_count"))
|
| 2205 |
+
exact = int(wc.get("preferred_exact") or 0)
|
| 2206 |
+
core_rules = [_text(x) for x in _as_list(author_memory.get("core_rules")) if _text(x)][:8]
|
| 2207 |
+
family_rules = [_text(x) for x in _as_list(family_memory.get("rules")) if _text(x)][:6]
|
| 2208 |
+
excluded = [_text(x) for x in _as_list(micro.get("excluded_stimuli")) if _text(x)][:10]
|
| 2209 |
+
return (
|
| 2210 |
+
"أنت وكيل تأليف محتوى سؤال تقييم عربي، ولست مدرسًا علاجيًا.\n"
|
| 2211 |
+
"نطاقك هنا لغوي فقط؛ النظام يملك العقد والميكانيك والتصحيح.\n"
|
| 2212 |
+
f"المهمة: {_text(micro.get('task_instruction'))}\n"
|
| 2213 |
+
+ (f"TARGET_WORD_COUNT={exact} EXACT=True.\n" if exact else "")
|
| 2214 |
+
+ "دورة التأليف الداخلية: افهم الهدف؛ كوّن 3 مرشحين؛ استبعد غير الطبيعي "
|
| 2215 |
+
"والمكرر؛ عدّ الكلمات؛ اختر أفضل مرشح؛ ثم submit.\n"
|
| 2216 |
+
+ "القواعد: " + " | ".join(core_rules + family_rules) + "\n"
|
| 2217 |
+
+ ("المثيرات الممنوعة من التكرار: " + " | ".join(excluded) + "\n" if excluded else "")
|
| 2218 |
+
+ "لا تخترع كلمة. لا تكتب شرحًا. أرسل الحقول المسموحة فقط."
|
| 2219 |
+
)
|
| 2220 |
+
|
| 2221 |
manifest = self.agent_tool_manifest()
|
| 2222 |
memory = self._agent_memory_snapshot(task_key)
|
| 2223 |
compact_results = self._compact_agent_tool_results(tool_results)
|
| 2224 |
return (
|
| 2225 |
QUESTION_AGENT_SYSTEM_PROMPT
|
| 2226 |
+
+ "\n\nصلاحيات الأدوات المسموحة:\n"
|
| 2227 |
+ json.dumps(manifest, ensure_ascii=False, separators=(",", ":"))
|
| 2228 |
+
+ "\n\nنتائج الأدوات المقروءة:\n"
|
| 2229 |
+
+ json.dumps(compact_results, ensure_ascii=False, default=str, separators=(",", ":"))[:2400]
|
| 2230 |
+
+ "\n\nذاكرة العمل:\n"
|
| 2231 |
+
+ json.dumps(memory, ensure_ascii=False, default=str, separators=(",", ":"))[:900]
|
| 2232 |
)
|
| 2233 |
|
| 2234 |
def _build_native_repair_packet(
|
|
|
|
| 2483 |
"""
|
| 2484 |
request = copy.deepcopy(single_request)
|
| 2485 |
messages: List[Dict[str, Any]] = []
|
| 2486 |
+
_initial_structured = (
|
| 2487 |
+
request.get("structured_generation_payload")
|
| 2488 |
+
if isinstance(request.get("structured_generation_payload"), dict)
|
| 2489 |
+
else {}
|
| 2490 |
+
)
|
| 2491 |
+
_initial_micro = (
|
| 2492 |
+
_initial_structured.get("semantic_micro_task")
|
| 2493 |
+
if isinstance(_initial_structured.get("semantic_micro_task"), dict)
|
| 2494 |
+
else self._semantic_micro_task(
|
| 2495 |
+
contract=contract,
|
| 2496 |
+
template=template,
|
| 2497 |
+
recipe=recipe,
|
| 2498 |
+
)
|
| 2499 |
+
)
|
| 2500 |
+
micro_direct_submit = bool(_initial_micro.get("enabled") is True)
|
| 2501 |
+
context_seen = micro_direct_submit
|
| 2502 |
submission_attempts = 0
|
| 2503 |
last_rejected_submission_digest = ""
|
| 2504 |
|
| 2505 |
+
if micro_direct_submit:
|
| 2506 |
+
print(
|
| 2507 |
+
"[AI QUESTION AUTHOR AGENT] "
|
| 2508 |
+
f"SKILL={contract.get('skill_id','')} DIRECT_SUBMIT=True "
|
| 2509 |
+
"MODEL_CONTEXT_TOOL_SKIPPED=True MANAGER_CONTEXT_AUTHORITATIVE=True"
|
| 2510 |
+
)
|
| 2511 |
+
|
| 2512 |
for model_turn in range(AGENT_MAX_TOOL_ROUNDS):
|
| 2513 |
structured = request.get("structured_generation_payload")
|
| 2514 |
if not isinstance(structured, dict):
|
|
|
|
| 2653 |
print(
|
| 2654 |
"[AI QUESTION SUBMIT VALIDATOR] "
|
| 2655 |
f"SKILL={contract.get('skill_id','')} PHASE={phase} VALID=False "
|
| 2656 |
+
f"ATTEMPT={submission_attempts}/{AGENT_MAX_SUBMISSIONS} ERRORS={precheck.get('errors',[])}"
|
| 2657 |
)
|
| 2658 |
+
if submission_attempts >= AGENT_MAX_SUBMISSIONS or model_turn + 1 >= AGENT_MAX_TOOL_ROUNDS:
|
| 2659 |
return {
|
| 2660 |
"success": False,
|
| 2661 |
"ready": False,
|
|
|
|
| 4267 |
for agent_attempt in range(1, AGENT_MAX_ATTEMPTS + 1):
|
| 4268 |
memory["attempts"] = int(memory.get("attempts", 0) or 0) + 1
|
| 4269 |
single_request = self._make_single_request(single_contract, template, sequence=sequence)
|
| 4270 |
+
_prompt_structured = (
|
| 4271 |
+
single_request.get("structured_generation_payload")
|
| 4272 |
+
if isinstance(single_request.get("structured_generation_payload"), dict)
|
| 4273 |
+
else {}
|
| 4274 |
+
)
|
| 4275 |
+
_prompt_micro = (
|
| 4276 |
+
_prompt_structured.get("semantic_micro_task")
|
| 4277 |
+
if isinstance(_prompt_structured.get("semantic_micro_task"), dict)
|
| 4278 |
+
else self._semantic_micro_task(
|
| 4279 |
+
contract=single_contract,
|
| 4280 |
+
template=template,
|
| 4281 |
+
recipe=recipe,
|
| 4282 |
+
)
|
| 4283 |
+
)
|
| 4284 |
+
agent_prompt = self._agent_system_prompt(
|
| 4285 |
+
task_key,
|
| 4286 |
+
tool_results,
|
| 4287 |
+
semantic_micro_task=_prompt_micro,
|
| 4288 |
+
)
|
| 4289 |
policy_constraints = [
|
| 4290 |
_text(x)
|
| 4291 |
for x in _as_list(recipe.get("subject_policy_qwen_constraints"))
|
|
|
|
| 4845 |
"subject_policy_pre_generation_alignment": True,
|
| 4846 |
"subject_policy_compiler": SUBJECT_POLICY_COMPILER_VERSION,
|
| 4847 |
"question_form_authority_envelope": "contract_once+recipe_once",
|
| 4848 |
+
"question_generation_memory_enabled": True,
|
| 4849 |
+
"direct_question_authoring_microtask": True,
|
| 4850 |
"duplicate_effective_recipe_prompt_blocks": False,
|
| 4851 |
"contract_visual_semantic_only": True,
|
| 4852 |
"written_word_labels_model_writable": False,
|
deterministic_question_executor.py
CHANGED
|
@@ -27,7 +27,7 @@ from question_interaction_contract import (
|
|
| 27 |
canonicalize_question,
|
| 28 |
)
|
| 29 |
|
| 30 |
-
VERSION = "
|
| 31 |
|
| 32 |
|
| 33 |
def _text(value: Any) -> str:
|
|
@@ -481,9 +481,8 @@ def _compile_declared_visual_word_mapping(
|
|
| 481 |
|
| 482 |
source = {
|
| 483 |
"id": "stimulus_audio",
|
| 484 |
-
"label":
|
| 485 |
-
"label_ar":
|
| 486 |
-
"visual_token": unit,
|
| 487 |
"semantic_value": "audio_stimulus",
|
| 488 |
"value": "audio_stimulus",
|
| 489 |
"audio_asset_role": "prompt_audio",
|
|
@@ -499,9 +498,8 @@ def _compile_declared_visual_word_mapping(
|
|
| 499 |
"id": target_id,
|
| 500 |
"label": pattern,
|
| 501 |
"label_ar": pattern,
|
| 502 |
-
"
|
| 503 |
-
"
|
| 504 |
-
"value": f"visual_pattern_{index + 1}",
|
| 505 |
})
|
| 506 |
|
| 507 |
# Stable deterministic shuffle for presentation while preserving target IDs.
|
|
@@ -509,6 +507,19 @@ def _compile_declared_visual_word_mapping(
|
|
| 509 |
targets = _stable_shuffle(targets, seed)
|
| 510 |
|
| 511 |
mapping = {"stimulus_audio": correct_target_id}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 512 |
out["interaction_payload"] = {
|
| 513 |
"left_items": [copy.deepcopy(source)],
|
| 514 |
"drag_items": [copy.deepcopy(source)],
|
|
@@ -516,6 +527,9 @@ def _compile_declared_visual_word_mapping(
|
|
| 516 |
"drop_zones": copy.deepcopy(targets),
|
| 517 |
"mapping_answer": copy.deepcopy(mapping),
|
| 518 |
"correct_mapping": copy.deepcopy(mapping),
|
|
|
|
|
|
|
|
|
|
| 519 |
}
|
| 520 |
out["correct_mapping"] = copy.deepcopy(mapping)
|
| 521 |
out["correct_answer"] = copy.deepcopy(mapping)
|
|
@@ -529,9 +543,10 @@ def _compile_declared_visual_word_mapping(
|
|
| 529 |
out["options"] = []
|
| 530 |
out["choices"] = []
|
| 531 |
|
| 532 |
-
#
|
| 533 |
-
|
| 534 |
-
|
|
|
|
| 535 |
value = record.get(key)
|
| 536 |
if not _token_only_visual(value, allowed):
|
| 537 |
errors.append(f"visual_token_contract_violation:{key}")
|
|
@@ -572,8 +587,16 @@ def _validate_declared_visual_surface(
|
|
| 572 |
visible_records.append(row)
|
| 573 |
|
| 574 |
for row in visible_records:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 575 |
for key in ("label", "label_ar", "text", "visual_token"):
|
| 576 |
-
if key
|
|
|
|
|
|
|
|
|
|
|
|
|
| 577 |
errors.append(f"visual_surface_forbidden_text:{key}")
|
| 578 |
|
| 579 |
for key in ("options", "choices"):
|
|
@@ -872,4 +895,6 @@ def summary() -> Dict[str, Any]:
|
|
| 872 |
"zero_semantic_guess": True,
|
| 873 |
"declared_visual_modality_guard": True,
|
| 874 |
"written_word_label_leakage_forbidden": True,
|
|
|
|
|
|
|
| 875 |
}
|
|
|
|
| 27 |
canonicalize_question,
|
| 28 |
)
|
| 29 |
|
| 30 |
+
VERSION = "deterministic_question_executor_v3_student_clear_visual_segmentation"
|
| 31 |
|
| 32 |
|
| 33 |
def _text(value: Any) -> str:
|
|
|
|
| 481 |
|
| 482 |
source = {
|
| 483 |
"id": "stimulus_audio",
|
| 484 |
+
"label": "🔊 الجملة المسموعة",
|
| 485 |
+
"label_ar": "🔊 الجملة المسموعة",
|
|
|
|
| 486 |
"semantic_value": "audio_stimulus",
|
| 487 |
"value": "audio_stimulus",
|
| 488 |
"audio_asset_role": "prompt_audio",
|
|
|
|
| 498 |
"id": target_id,
|
| 499 |
"label": pattern,
|
| 500 |
"label_ar": pattern,
|
| 501 |
+
"semantic_value": target_id,
|
| 502 |
+
"value": target_id,
|
|
|
|
| 503 |
})
|
| 504 |
|
| 505 |
# Stable deterministic shuffle for presentation while preserving target IDs.
|
|
|
|
| 507 |
targets = _stable_shuffle(targets, seed)
|
| 508 |
|
| 509 |
mapping = {"stimulus_audio": correct_target_id}
|
| 510 |
+
|
| 511 |
+
response_kind = _lower(response.get("kind"))
|
| 512 |
+
if response_kind == "visual_word_segmentation_mapping":
|
| 513 |
+
instruction = "استمع ثم اختر شكل فصل الكلمات."
|
| 514 |
+
elif response_kind == "visual_word_boundary_mapping":
|
| 515 |
+
instruction = "استمع ثم اختر شكل حدود الكلمات."
|
| 516 |
+
else:
|
| 517 |
+
instruction = "استمع ثم اختر التمثيل الصحيح للكلمات."
|
| 518 |
+
|
| 519 |
+
out["question"] = instruction
|
| 520 |
+
out["prompt"] = instruction
|
| 521 |
+
out["text"] = instruction
|
| 522 |
+
|
| 523 |
out["interaction_payload"] = {
|
| 524 |
"left_items": [copy.deepcopy(source)],
|
| 525 |
"drag_items": [copy.deepcopy(source)],
|
|
|
|
| 527 |
"drop_zones": copy.deepcopy(targets),
|
| 528 |
"mapping_answer": copy.deepcopy(mapping),
|
| 529 |
"correct_mapping": copy.deepcopy(mapping),
|
| 530 |
+
"student_help": "اسحب الجملة المسموعة إلى شكل فصل الكلمات.",
|
| 531 |
+
"student_legend": "● كلمة، | فاصل",
|
| 532 |
+
"internal_labels_visible": False,
|
| 533 |
}
|
| 534 |
out["correct_mapping"] = copy.deepcopy(mapping)
|
| 535 |
out["correct_answer"] = copy.deepcopy(mapping)
|
|
|
|
| 543 |
out["options"] = []
|
| 544 |
out["choices"] = []
|
| 545 |
|
| 546 |
+
# Only target answers must be token-only. The neutral source descriptor
|
| 547 |
+
# does not reveal the hidden audio transcript.
|
| 548 |
+
for record in targets:
|
| 549 |
+
for key in ("label", "label_ar"):
|
| 550 |
value = record.get(key)
|
| 551 |
if not _token_only_visual(value, allowed):
|
| 552 |
errors.append(f"visual_token_contract_violation:{key}")
|
|
|
|
| 587 |
visible_records.append(row)
|
| 588 |
|
| 589 |
for row in visible_records:
|
| 590 |
+
is_audio_source = bool(
|
| 591 |
+
_text(row.get("audio_asset_role")) == "prompt_audio"
|
| 592 |
+
or _text(row.get("id")) in {"stimulus_audio", "stimulus_media"}
|
| 593 |
+
)
|
| 594 |
for key in ("label", "label_ar", "text", "visual_token"):
|
| 595 |
+
if key not in row:
|
| 596 |
+
continue
|
| 597 |
+
if is_audio_source and key in {"label", "label_ar", "text"}:
|
| 598 |
+
continue
|
| 599 |
+
if not _token_only_visual(row.get(key), allowed):
|
| 600 |
errors.append(f"visual_surface_forbidden_text:{key}")
|
| 601 |
|
| 602 |
for key in ("options", "choices"):
|
|
|
|
| 895 |
"zero_semantic_guess": True,
|
| 896 |
"declared_visual_modality_guard": True,
|
| 897 |
"written_word_label_leakage_forbidden": True,
|
| 898 |
+
"student_facing_internal_ids_forbidden": True,
|
| 899 |
+
"clear_audio_source_descriptor": True,
|
| 900 |
}
|