#!/usr/bin/env python3 """ verify_ene_schema.py — Verifies the presence and structure of Braid Eigensolid Compressor tables in the ENE substrate schema and writes a validation receipt. """ import os import json import re import hashlib from pathlib import Path SCHEMA_FILE = Path(__file__).parent / "ene_substrate_schema.sql" RECEIPT_FILE = Path(__file__).parent.parent.parent / "shared-data" / "data" / "stack_solidification" / "ene_schema_validation_receipt.json" REQUIRED_TABLES = [ "ene.packages", "ene.relations", "ene.receipts", "ene.scars", "ene.vectors", "ene.ingest_events", "ene.sessions", "ene.routes", "ene.nspace_kv", "ene.gossip_surface_nodes", "ene.gossip_surface_edges", "ene.dsp_nodes", "ene.prover_state", "ene.prover_instances", "ene.sidon_labels", "ene.crossing_weights", "ene.eigensolid_snapshots", "ene.braid_strands" ] def verify_schema(): if not SCHEMA_FILE.exists(): print(f"Error: Schema file not found at {SCHEMA_FILE}") return False content = SCHEMA_FILE.read_text() # Calculate sha256 of the schema file schema_sha256 = hashlib.sha256(content.encode('utf-8')).hexdigest() # Perform syntax checks: count open/close parenthesis open_paren = content.count('(') close_paren = content.count(')') if open_paren != close_paren: print(f"Warning: Parenthesis imbalance! '(' = {open_paren}, ')' = {close_paren}") # Check for presence of required tables missing_tables = [] found_tables = [] for table in REQUIRED_TABLES: # Match "CREATE TABLE IF NOT EXISTS table_name" or "CREATE TABLE table_name" pattern = rf"CREATE\s+TABLE\s+(?:IF\s+NOT\s+EXISTS\s+)?{re.escape(table)}\b" if re.search(pattern, content, re.IGNORECASE): found_tables.append(table) else: missing_tables.append(table) # Check for alterations in ene.receipts has_theorem_id = "theorem_id" in content and "ene.receipts" in content has_dispatch_path = "dispatch_path" in content and "ene.receipts" in content # Check constraint check has_no_floats_check = "weight_raw" in content and "REAL" not in content.split("crossing_weights")[1].split(");")[0] status = "valid" errors = [] if missing_tables: status = "invalid" errors.append(f"Missing tables: {', '.join(missing_tables)}") if not has_theorem_id: status = "invalid" errors.append("Missing theorem_id column alteration in ene.receipts") if not has_dispatch_path: status = "invalid" errors.append("Missing dispatch_path column alteration in ene.receipts") if not has_no_floats_check: status = "invalid" errors.append("Float found or incorrect integer constraints in crossing_weights table") receipt = { "schema": "ene_schema_validation_receipt_v1", "schema_file_path": str(SCHEMA_FILE.relative_to(SCHEMA_FILE.parent.parent.parent)), "schema_sha256": schema_sha256, "status": status, "found_tables": found_tables, "missing_tables": missing_tables, "alterations_verified": { "theorem_id": has_theorem_id, "dispatch_path": has_dispatch_path }, "errors": errors } # Write the receipt RECEIPT_FILE.parent.mkdir(parents=True, exist_ok=True) RECEIPT_FILE.write_text(json.dumps(receipt, indent=2)) print(f"Validation complete. Status: {status}. Receipt written to {RECEIPT_FILE}") return status == "valid" if __name__ == "__main__": import sys success = verify_schema() sys.exit(0 if success else 1)