"""Leakage guardrails: hard cutoffs and forbidden-artifact checks. Every forecast-path entry point calls these. Violations raise, never warn. """ from __future__ import annotations import re from pathlib import Path import pandas as pd CUTOFF = pd.Timestamp("2018-12-31") FORBIDDEN_PATTERNS = [ r"load_model_from_id", r"ModelID\.", r"OLMOEARTH_V1", r"allenai/OlmoEarth", r"olmoearth-v1-3-embeddings", r"OlmoEarth-v1-", ] # Files allowed to *mention* the forbidden strings (docs / this guard itself). _ALLOWLIST = {"leakage.py", "AGENTS.md"} def assert_dates_le_cutoff(dates, label: str = "input") -> None: """Raise if any date is after 2018-12-31.""" d = pd.to_datetime(pd.Series(list(dates)), errors="coerce", utc=True) d = d.dt.tz_localize(None) if d.isna().any(): raise ValueError(f"{label}: unparseable dates present") if len(d) and d.max() > CUTOFF: raise ValueError( f"{label}: date {d.max().date()} exceeds cutoff {CUTOFF.date()}" ) def assert_no_forbidden_artifacts(code_dir: Path) -> list[Path]: """Scan forecast-path code for released-OlmoEarth usage. Returns scanned files.""" scanned = [] hits = [] for path in sorted(Path(code_dir).rglob("*.py")): if path.name in _ALLOWLIST: continue text = path.read_text(encoding="utf-8", errors="replace") scanned.append(path) for pat in FORBIDDEN_PATTERNS: if re.search(pat, text): hits.append(f"{path}: matches forbidden pattern {pat!r}") if hits: raise RuntimeError("Forbidden pretrained-OlmoEarth usage found:\n" + "\n".join(hits)) return scanned def assert_frame_le_cutoff(df: pd.DataFrame, date_col: str, label: str = "frame") -> None: assert_dates_le_cutoff(df[date_col], label=label)