| |
| |
|
|
| import re |
| import logging |
|
|
| logger = logging.getLogger(__name__) |
|
|
| |
| FORBIDDEN_CONCEPTS = [ |
| r"נגזרת", r"נגזור", r"גזירה", r"קיצון", r"מקסימום", r"מינימום", |
| r"אינטגרל", r"אינטגרציה", r"הפרש", r"מערכת", r"נעלמים", r"שני נעלמים", |
| r"פיתגורס", r"שורשים", r"מכנה משותף" |
| ] |
|
|
| |
| FORBIDDEN_SYMBOLS = [ |
| r"'", r"\\'", r"integral", r"\\int", r"\\Sigma", r"\\Delta" |
| ] |
|
|
| def scan_explanation(text: str, proof_graph) -> tuple[bool, str]: |
| """ |
| Scans explanation text for concepts NOT present in the ProofGraph. |
| Returns (is_safe, violation_reason). |
| """ |
| if not text: |
| return True, "" |
|
|
| |
| for pattern in FORBIDDEN_CONCEPTS: |
| if re.search(pattern, text): |
| |
| |
| is_in_proof = False |
| if proof_graph: |
| for step in proof_graph.steps: |
| if step.operator_used and pattern in step.logic_description: |
| is_in_proof = True |
| break |
| |
| if not is_in_proof: |
| logger.warning(f"🛡️ [FIREWALL] Semantic violation detected: '{pattern}'") |
| return False, f"Concept '{pattern}' found in text but not in ProofGraph." |
|
|
| |
| for symbol in FORBIDDEN_SYMBOLS: |
| if symbol in text: |
| |
| is_in_proof = False |
| if proof_graph: |
| for step in proof_graph.steps: |
| if symbol in str(step.math_content): |
| is_in_proof = True |
| break |
| |
| if not is_in_proof: |
| logger.warning(f"🛡️ [FIREWALL] Symbol violation detected: '{symbol}'") |
| return False, f"Symbol '{symbol}' found in text but not in ProofGraph." |
|
|
| return True, "" |
|
|