""" Artifact Validation Gateway — Vitalis FSI Intercepts and validates the structural integrity of ArchitectureArtifacts prior to ledger insertion. Prevents schema drift and ensures downstream CI compatibility. """ from typing import Any, Dict, List from src.cognition._constants import logger from src.cognition.artifact import ArchitectureArtifact class ArtifactValidator: """Enforces absolute adherence to the ArchitectureArtifact schema.""" @staticmethod def validate_artifact(artifact: Any) -> bool: if not isinstance(artifact, dict): logger.error("Validation Failed: Artifact is not a dictionary.") return False required_keys = { "components": list, "boundaries": list, "data_flows": list, "patterns": list, "risks": list, "rationale": str, "constraints": list } for key, expected_type in required_keys.items(): if key not in artifact: logger.error("Validation Failed: Missing critical key '%s'.", key) return False if not isinstance(artifact[key], expected_type): logger.error("Validation Failed: Key '%s' expected %s, got %s.", key, expected_type.__name__, type(artifact[key]).__name__) return False # Deep validation of risk schema for risk in artifact.get("risks", []): if not ArtifactValidator._validate_risk(risk): return False logger.debug("Artifact structure verified successfully.") return True @staticmethod def _validate_risk(risk: Dict[str, Any]) -> bool: required_risk_keys = {"id": str, "description": str, "severity": str, "mitigation": str} for key, expected_type in required_risk_keys.items(): if key not in risk or not isinstance(risk[key], expected_type): logger.error("Validation Failed: Malformed risk object on key '%s'.", key) return False if risk["severity"] not in ["LOW", "MEDIUM", "HIGH", "CRITICAL"]: logger.error("Validation Failed: Invalid risk severity level '%s'.", risk["severity"]) return False return True def validate(self, artifact): """ Validate a generated code artifact. Returns (bool, str) — (passed, reason) """ if artifact is None: return False, "Null artifact" code = getattr(artifact, "code", str(artifact)) if not code or not code.strip(): return False, "Empty code body" # Basic syntax check try: compile(code, "", "exec") return True, "Syntax OK" except SyntaxError as e: return False, f"SyntaxError: {e}" def validate(self, artifact): if artifact is None: return False, "Null artifact" code = getattr(artifact, "code", str(artifact)) if not code or not code.strip(): return False, "Empty code body" try: compile(code, "", "exec") return True, "Syntax OK" except SyntaxError as e: return False, f"SyntaxError: {e}"