import os import yaml import re import hashlib from datetime import datetime from src.logger import log_event from src.gemini_utils import normalize_url, clean_toc_text from src.config import INVENTORY_PATH from src.inventory_manager import load_inventory try: from yaml import CSafeLoader as Loader except ImportError: from yaml import SafeLoader as Loader V1_DIR = "docs" V2_DIR = "v2-docs" SPECIAL_ASSETS_PATH = "data/special_assets.yaml" CURATION_SOURCES_PATH = "data/curation_sources.yaml" WORKFLOW_PATH = ".github/workflows/01.1.agentic_cron.yml" class SafetyGuard: def __init__(self): self.errors = [] self.warnings = [] self.inventory = self._load_inventory() def _load_inventory(self): return load_inventory() def _load_exempt_files(self): try: with open("data/link_rules.yaml", "r", encoding="utf-8") as f: rules = yaml.load(f, Loader=Loader) return rules.get("hierarchy_rules", {}).get("toc_exempt_files", []) except Exception as e: log_event(f"[WARN] load exempt files from link_rules.yaml: {str(e)[:100]}") return [] def validate_data_integrity(self, old_inventory: dict): """Mandate 1: InformaciΓ³n Preservation.""" for url, old_meta in old_inventory.items(): new_meta = self.inventory.get(url) if not new_meta: continue if old_meta.get("stars", 0) > new_meta.get("stars", 0): self.errors.append(f"β **Star Loss**: `{url}` ({old_meta['stars']} -> {new_meta['stars']})") if old_meta.get("description") and not new_meta.get("description"): self.errors.append(f"β **V1 Desc Loss**: `{url}`") def validate_semantic_interlinking(self): """Mandate 5: Semantic Interlinking validation.""" for url, meta in self.inventory.items(): related = meta.get("related_categories", []) for rel_cat in related: path = os.path.join(V1_DIR, f"{rel_cat}.md") if os.path.exists(path): content = open(path, "r").read() if url not in content: self.warnings.append(f"π **Interlink Missing**: `{meta.get('title', url)}` in `{rel_cat}.md` (See also)") def validate_special_assets_completeness(self): """Mandate 27: Special Assets Exhaustive Inclusion.""" if not os.path.exists(SPECIAL_ASSETS_PATH): return try: with open(SPECIAL_ASSETS_PATH, "r", encoding="utf-8") as f: special = yaml.load(f, Loader=Loader).get("special_assets", []) except Exception as e: log_event(f"[WARN] load special_assets.yaml for validation: {str(e)[:100]}") return for sa in special: if "Include 100%" in sa.get("v2_rule", "") or "Exhaustive" in sa.get("v2_rule", ""): file_name = sa["file"] v1_path = os.path.join(V1_DIR, file_name) if os.path.exists(v1_path): with open(v1_path, "r") as f: v1_links = re.findall(r'\[.*?\]\((https?://.*?)\)', f.read()) for link in v1_links: nu = normalize_url(link) if nu in self.inventory and self.inventory[nu].get("status") == "online": if not self.inventory[nu].get("is_special"): self.errors.append(f"π **VIP Flag Missing**: `{link}` from `{file_name}` is not marked as Special") def validate_mvq_compliance(self): """Mandate 3 & 16: Minimum Viable Quality (MVQ).""" for url, meta in self.inventory.items(): if "github.com" in url and meta.get("v2_locations"): pushed = meta.get("gh_pushed", "") # Skip entries with no usable push date. Enrichment stores "N/A" # (and historically None/empty) when GitHub data is unavailable; # these are not ISO dates and must not reach fromisoformat(). if not pushed or str(pushed).strip().upper() in ("N/A", "NONE"): continue try: last_date = datetime.fromisoformat(str(pushed).replace("Z", "+00:00")) inactive_years = (datetime.now(last_date.tzinfo) - last_date).days / 365 # gh_stars may be missing; fall back to V1 stars (Γ100 heuristic). # Coalesce None so a stored null never reaches the multiplication. stars = meta.get("gh_stars") if stars is None: stars = (meta.get("stars") or 0) * 100 if inactive_years > 4 and stars < 30: self.warnings.append(f"ποΈ **MVQ Violation**: Stale repo `{url}` (>4yrs) in V2 with low impact") except Exception as e: log_event(f"[WARN] MVQ compliance check for {url}: {str(e)[:100]}") def validate_linguistic_tagging(self): """Mandate 10: Explicit Language Tagging.""" if not os.path.exists(V2_DIR): return for file in os.listdir(V2_DIR): if file.endswith(".md"): with open(os.path.join(V2_DIR, file), "r") as f: content = f.read() for url, meta in self.inventory.items(): # language may be stored as null; coalesce to avoid None.lower() # aborting the entire audit. lang = meta.get("language") or "English" if lang.lower() != "english" and url in content: tag = f"[{lang.upper()} CONTENT]" if tag not in content: self.warnings.append(f"π **Missing Lang Tag**: `{meta.get('title', url)}` in `{file}` needs `{tag}`") def validate_platinum_schema(self): """Mandate 22: Platinum Metadata Lifecycle.""" required = ["content_hash", "health_score", "hierarchy", "v1_locations"] new_count = 0 for url, meta in self.inventory.items(): if url.startswith("INTRO:"): continue missing = [f for f in required if f not in meta] if missing: new_count += 1 if new_count < 10: self.warnings.append(f"𧬠**Schema Incomplete**: `{url}` missing {missing}") def has_valid_toc(self, content: str) -> bool: """Checks if content has a valid Table of Contents.""" if "## Table of Contents" in content: return True toc_pattern = r'^\d+\.\s+\[.*?\]\(#.*?\)' matches = re.findall(toc_pattern, content, re.MULTILINE) return len(matches) >= 3 def validate_structural_standards(self): """Mandate 30: Universal Title and TOC Standards.""" exempt_files = self._load_exempt_files() for root, dirs, files in os.walk(V1_DIR): for file in files: if file.endswith(".md") and file not in exempt_files: path = os.path.join(root, file) with open(path, "r") as f: content = f.read() # 1. No Emojis or & in titles (H2-H6) titles = re.findall(r'^#{2,6}\s+(.*)', content, re.M) for t in titles: if "&" in t: self.errors.append(f"π« **Standard Violation**: Ampersand `&` found in title `{t}` in `{file}`. Use 'and'.") # Basic emoji detection (not exhaustive but covers most common ones in project) if any(char in t for char in "π§ βοΈππ‘οΈπ§ͺππ΅οΈβ¨β οΈπ΄π‘β π"): self.errors.append(f"π« **Standard Violation**: Emoji found in title `{t}` in `{file}`.") # 2. HTML block rendering check (Mandate 19) if "