File size: 960 Bytes
e9462cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from __future__ import annotations

import re
from typing import Optional

from models import SolverResult


FORBIDDEN_PATTERNS = [
    r"\bthe answer is\b",
    r"\bcorrect answer\b",
    r"\bchoice\s+[A-E]\b",
    r"\boption\s+[A-E]\b",
    r"\bit'?s\s+[A-E]\b",
]


SAFE_FALLBACK = (
    "I can help you solve it, but I should not give the final answer directly. "
    "I’ll guide you to the method and the check you need."
)



def contains_forbidden_answering(text: str) -> bool:
    lower = (text or "").lower()
    return any(re.search(pattern, lower) for pattern in FORBIDDEN_PATTERNS)



def enforce_study_guardrails(reply: str, result: Optional[SolverResult] = None) -> str:
    if contains_forbidden_answering(reply):
        if result and result.steps:
            bullet_steps = "\n".join(f"- {step}" for step in result.steps[:4])
            return SAFE_FALLBACK + "\n\nTry this:\n" + bullet_steps
        return SAFE_FALLBACK
    return reply