"""Deterministic answer guards (ADR-010, decisions 2 and 3). Pure functions, no model call. The `finalize` node in app/graph.py runs them; evals/asserts/check.py imports DISCRETION_PATTERNS.""" import functools import re from app.texts import ADDR_REMOVED, LINK_REMOVED def _w(term, suffix=r"\w*"): """Word-start boundary; the default suffix makes the term a prefix ("opportunit" -> "opportunities").""" return r"(?]|https?://|www\.|@", re.I) # cut JSON/XML, links, addresses _CAPS = re.compile(r"(? bool: return not META.search(line) and not THOUGHT.search(line) and all(t in profile for t in _CAPS.findall(line)) def only_fixed(text: str, fixed: dict[str, str]) -> tuple[str, str] | None: """(kind, kept) if the reply contains a fixed sentence, else None. kind = "idk" or "reloc". kept = the fact lines before an idk sentence. A line with a Discretion hit or a table row is dropped alone (a label such as "Notice period:" must not delete the fact above it); any meta, first-person, marker, link or unknown CAPS-token line drops all kept text. Text after the sentence and around a relocation sentence is dropped. Both kinds: idk wins.""" found = sorted((k for k, s in fixed.items() if s in text), key=lambda k: not k.startswith("idk")) if not found: return None kind = found[0][:-3] kept = text.split(fixed[found[0]])[0].strip() if kind == "idk" and len(found) == 1 else "" lines = [ln for ln in kept.splitlines() if re.search(r"\w", ln) and "|" not in ln and not discretion_hit(ln)] profile = _profile() # a lead-in colon on the last line ("so:", "Notice period:") means the fact it introduces is missing ok = all(_plain(ln, profile) for ln in lines) and not (lines and lines[-1].rstrip().endswith(":")) return kind, "\n".join(lines).strip() if ok else "" @functools.cache def _profile() -> str: from app.prompts import ROOT # late import: answer_guards stays importable without the repo files return (ROOT / "profile/profile.md").read_text() def thought_hit(text: str) -> bool: return THOUGHT.search(text) is not None def discretion_hit(text: str) -> bool: """No negation stripping here (fail closed); check.py strips negations for the eval only.""" return _DISCRETION.search(text) is not None def scrub(text: str, lang: str) -> str: """Images first, then URLs, then addresses, so an address inside a URL goes with the URL.""" text = IMAGE.sub("", text) text = URL.sub(LINK_REMOVED[lang], text) return EMAIL.sub(ADDR_REMOVED[lang], text)