# verify_v42_lockdown.py import sys import os import asyncio import re from unittest.mock import MagicMock # Add current directory to path sys.path.append(os.getcwd()) # Mock problematic dependencies before importing orchestrator sys.modules['cv2'] = MagicMock() sys.modules['ocr_strip_engine'] = MagicMock() import math_intent_detector from smart_solver import SmartSolver from strategy_manager import StrategyManager from orchestrator import seal_pedagogical_output async def test_grade7_lockdown(): print("๐Ÿงช [V4.2] Testing Grade 7 Strategy Lockdown...") # 1. Intent Detection problem = "f(x) = 3x + 5. ืคืชื•ืจ ืืช ื”ืžืฉื•ื•ืื” ืขื‘ื•ืจ f(x)=0" grade_num = 7 intent = math_intent_detector.detect_intent(problem, grade_num) contract = math_intent_detector.get_intent_contract(intent, grade_num) print(f"Detected Intent: {intent}") if intent != "SIMPLE_LINEAR_SOLVE": print("โŒ Failure: Grade 7 problem not classified as SIMPLE_LINEAR_SOLVE") return # 2. Output Sealer Test (V4.2.4) print("\n๐Ÿงช [V4.2.4] Testing Global Output Sealer...") leaky_response = { "final_answer": "x = -1.66", "teacher_summary": "ืžืฆืื ื• ืืช ื”ื ื’ื–ืจืช f'(x) ื•ื’ื™ืœื™ื ื• ืฉื–ื” ืงื• ื™ืฉืจ.", "sections": [{ "steps": [{"content_mixed": "ื ื—ืฉื‘ ืืช ื”ื ื’ื–ืจืช f(x) = 3x+5..."}] }] } sealed = seal_pedagogical_output(leaky_response, 7) sealed_str = str(sealed) forbidden_terms = ["ื ื’ื–ืจืช", "f'(x)", "f(x)"] success = True for term in forbidden_terms: if term in sealed_str: print(f"โŒ Failure: Forbidden term '{term}' still present in sealed output!") success = False if success: print("โœ… Success: Global Sealer scrubbed all forbidden Grade 7 concepts.") # 3. StrategyManager Lockdown (Prompt Injection) print("\n๐Ÿงช [V4.2.4] Testing StrategyManager Narrative Contract...") mock_llm = MagicMock() captured_prompt = "" async def mock_generate(payload): nonlocal captured_prompt captured_prompt = payload[0] if isinstance(payload, list) else payload mock_resp = MagicMock() mock_resp.text = '{"steps": [{"step_id": 1, "pedagogical_explanation": "ื”ืกื‘ืจ ืคืฉื•ื˜"}]}' return mock_resp mock_llm.generate_content_async = mock_generate manager = StrategyManager(mock_llm) await manager.solve_with_strategy( problem, {"function_equations": ["3*x + 5"]}, grade="7", intent=intent, intent_contract=contract ) if "### STRICT CONTRACT ###" in captured_prompt and "Narrative Projector" in captured_prompt: print("โœ… Success: StrategyManager injected STRICT CONTRACT into narrative prompt.") else: print("โŒ Failure: STRICT CONTRACT missing from narrative prompt.") def verify_infrastructure(): print("\n๐Ÿงช [V4.2] Verifying Infrastructure Lockdown...") import config expected_bucket = "buddy-math-dev.firebasestorage.app" # Try different config structures bucket = getattr(config, 'STORAGE_BUCKET', None) if not bucket and hasattr(config, 'DevelopmentConfig'): bucket = getattr(config.DevelopmentConfig, 'STORAGE_BUCKET', None) if bucket == expected_bucket: print(f"โœ… Success: Firebase Storage bucket locked to: {expected_bucket}") else: print(f"โŒ Failure: Bucket mismatch! Found: {bucket}") if __name__ == "__main__": asyncio.run(test_grade7_lockdown()) verify_infrastructure() print("\nโœจ V4.2.4 Strategy Lockdown Verification COMPLETE!")