"""Safety net: verify every DB literal in a caught row's ORIGINAL query still appears in PROPOSALS.md. Order #s / gift-card IDs / item numbers are unique per row, so whole-file presence effectively proves per-row preservation. Flags drops. Usage: python temp/story_remediation/validate_literals.py """ import json, re from pathlib import Path HERE = Path(__file__).resolve().parent OUT = HERE / "out" done = {l.strip() for l in (OUT / "done_ids.txt").read_text(encoding="utf-8").splitlines() if l.strip()} rows = {json.loads(l)["item_id"]: json.loads(l) for l in (OUT / "caught_rows.jsonl").read_text(encoding="utf-8").splitlines() if l.strip()} md = (HERE / "PROPOSALS.md").read_text(encoding="utf-8") PATS = { "order": r"#W\d+", "giftcard": r"gift_card_\d+", "email": r"[\w.\-]+@[\w.\-]+", "phone": r"\+1-\d{3}-\d{3}-\d{4}", "itemnum": r"\b\d{7,}\b", "quote": r"'[^']{3,}'", "money": r"\$\d+", "zip": r"\b\d{5}\b", "promo": r"\b[A-Z]{3,}\d{1,3}\b", } def literals(text): out = set() for _, p in PATS.items(): for m in re.findall(p, text): out.add(m) return out problems = 0 checked = 0 for iid in done: r = rows.get(iid) if not r: continue checked += 1 src = r["query"] + " " + " ".join(h["content"] for h in r["history"]) for lit in literals(r["query"]): # ignore bare 5-digit ZIPs that are substrings of order numbers etc. if lit not in md: print(f"MISSING literal {lit!r} for {iid}") problems += 1 print(f"\nchecked {checked} done rows | missing literals: {problems}") print("OK - all literals preserved" if problems == 0 else "REVIEW NEEDED")