#!/usr/bin/env python3 """ External audit of 4D Engine — sends to Grok and Gemini independently. Each auditor receives the same package: engine code, equations spec, test description, and results summary. Scoring on criteria A-E per AUDIT_PROTOCOL.txt. """ import json import sys import os from datetime import datetime, timezone from pathlib import Path # Add Productivity Tools to path for ai_router sys.path.insert(0, "/root/rhetroiluso/Wiggle Worm Media/Productivity Tools") from ai_router import route # ── Paths ────────────────────────────────────────────────────────────────── BASE = Path(__file__).parent RESULTS_DIR = BASE / "results" # ── Load files ───────────────────────────────────────────────────────────── engine_code = (BASE / "engine_4d.py").read_text() equations_spec = (BASE / "EQUATIONS_SPEC_v2.txt").read_text() test_description = (BASE / "TLT_4D_ENGINE_TEST_DESCRIPTION.txt").read_text() # Build results summary from individual JSON files results_summary_lines = [] sweep_file = RESULTS_DIR / "sweep_results.json" if sweep_file.exists(): sweep = json.loads(sweep_file.read_text()) results_summary_lines.append("=== PHASE 2 SWEEP RESULTS (13 values, 1.500–1.800) ===") results_summary_lines.append(f"{'c_4D':>8s} {'Peaks':>6s} {'Dual/Field':>11s} {'AutoCorr':>10s} {'P/N':>10s} {'Energy':>12s}") for r in sweep: results_summary_lines.append( f"{r['c_4d']:8.3f} {r['n_peaks']:6d} {r['dual_to_field_ratio']:11.4f} " f"{r['autocorrelation']:10.4f} {r['peak_to_noise']:10.2f} {r['field_energy_final']:12.4e}" ) # Add sqrt3 confirmation results sqrt3_results = [] for f in sorted(RESULTS_DIR.glob("result_c*.json")): data = json.loads(f.read_text()) sqrt3_results.append(data) if sqrt3_results: results_summary_lines.append("\n=== SQRT(3) CONFIRMATION SWEEP (fine-grained, c=1.610–1.740) ===") results_summary_lines.append(f"{'c_4D':>8s} {'Peaks':>6s} {'Dual/Field':>11s} {'AutoCorr':>10s} {'P/N':>10s} {'Energy':>12s}") for r in sorted(sqrt3_results, key=lambda x: x['c_4d']): results_summary_lines.append( f"{r['c_4d']:8.3f} {r['n_peaks']:6d} {r['dual_to_field_ratio']:11.4f} " f"{r['autocorrelation']:10.4f} {r['peak_to_noise']:10.2f} {r['field_energy_final']:12.4e}" ) results_summary = "\n".join(results_summary_lines) # ── Build audit prompt ───────────────────────────────────────────────────── AUDIT_SYSTEM = """You are an independent scientific auditor. You have NO prior context about this project or its theory. You are evaluating ONLY the methodology, equations, code integrity, and numerical correctness of a 4D wave simulation engine. You are NOT evaluating the theory itself, only whether the implementation is correct, unbiased, and scientifically sound. Be rigorous. Be skeptical. Flag anything that looks wrong, biased, or suspicious.""" AUDIT_PROMPT = f""" INDEPENDENT AUDIT REQUEST ======================== Please audit the following 4D FDTD wave simulation engine. Score each criterion on a 1-10 scale. AUDIT CRITERIA -------------- A. EQUATIONS AND PHYSICS A1. Is the 4D wave equation (Klein-Gordon with potential) correctly formulated? A2. Is the FDTD discretization correct (stencil, update formula)? A3. Are the PML absorbing boundary conditions correctly implemented? A4. Is the CFL stability condition correctly derived and applied? A5. Is the source injection (windowed sinusoidal) correctly implemented? B. GEOMETRY AND CONSTANTS B1. Are the 24-cell vertex coordinates correct (Form A and Form B)? B2. Is the normalization of Form B to unit sphere correct? B3. Is the 4D trilinear interpolation for source placement correct? B4. Are all constants derived correctly from their stated sources? C. METHODOLOGY C1. Are there hidden assumptions or free parameters beyond what is stated? C2. Is the test design unbiased (does it favor any particular outcome)? C3. Is the C_potential (curvature-dependent decoherence) correctly derived? C4. Is the 5D overflow mechanism physically/numerically reasonable? C5. Are results honestly reported? D. CODE INTEGRITY D1. Does the code implement what the equations spec describes? D2. Are there numerical artifacts that could affect results? D3. Is the energy tracking correct? D4. Are there any bugs or implementation errors? E. OVERALL E1. Overall scientific rigor (1-10) E2. Identified concerns or red flags (list) E3. Recommendations for improvement E4. Is there any evidence of bias toward a predetermined outcome? SCORING: 9-10: Rigorous, publication-quality 7-8: Sound with minor improvements needed 5-6: Reasonable but notable gaps 3-4: Significant concerns 1-2: Fundamentally flawed ═══════════════════════════════════════════════════════════════ FILE 1: ENGINE CODE (engine_4d.py) ═══════════════════════════════════════════════════════════════ {engine_code} ═══════════════════════════════════════════════════════════════ FILE 2: EQUATIONS SPECIFICATION v2 ═══════════════════════════════════════════════════════════════ {equations_spec} ═══════════════════════════════════════════════════════════════ FILE 3: TEST DESCRIPTION (pre-registered) ═══════════════════════════════════════════════════════════════ {test_description} ═══════════════════════════════════════════════════════════════ FILE 4: RESULTS SUMMARY ═══════════════════════════════════════════════════════════════ {results_summary} ═══════════════════════════════════════════════════════════════ PLEASE PROVIDE YOUR STRUCTURED AUDIT REPORT ═══════════════════════════════════════════════════════════════ Format your response as: - Score for each criterion (A1-E4) - Brief justification for each score - List of specific concerns - List of recommendations - Overall assessment """ # ── Send to auditors ─────────────────────────────────────────────────────── print(f"Audit package size: {len(AUDIT_PROMPT):,} characters") print(f"Timestamp: {datetime.now(timezone.utc).isoformat()}") print() # Send to Grok print("=" * 70) print("SENDING TO GROK (xAI)...") print("=" * 70) try: grok_result = route( task_type="research", messages=[{"role": "user", "content": AUDIT_PROMPT}], entity="prometheus", system=AUDIT_SYSTEM, provider_override="grok", max_tokens=8192, ) grok_report = grok_result["content"] grok_cost = grok_result["cost_usd"] grok_model = grok_result["model"] print(f"Grok model: {grok_model}") print(f"Grok cost: ${grok_cost:.4f}") print(f"Grok tokens: {grok_result['input_tokens']} in, {grok_result['output_tokens']} out") print() print(grok_report) except Exception as e: grok_report = f"[GROK ERROR: {e}]" grok_cost = 0 grok_model = "error" print(f"Grok error: {e}") print() print("=" * 70) print("SENDING TO GEMINI...") print("=" * 70) try: gemini_result = route( task_type="bias_check", messages=[{"role": "user", "content": AUDIT_PROMPT}], entity="prometheus", system=AUDIT_SYSTEM, provider_override="gemini", max_tokens=8192, ) gemini_report = gemini_result["content"] gemini_cost = gemini_result["cost_usd"] gemini_model = gemini_result["model"] print(f"Gemini model: {gemini_model}") print(f"Gemini cost: ${gemini_cost:.4f}") print(f"Gemini tokens: {gemini_result['input_tokens']} in, {gemini_result['output_tokens']} out") print() print(gemini_report) except Exception as e: gemini_report = f"[GEMINI ERROR: {e}]" gemini_cost = 0 gemini_model = "error" print(f"Gemini error: {e}") # ── Save reports ─────────────────────────────────────────────────────────── timestamp = datetime.now(timezone.utc).strftime("%Y%m%d_%H%M%S") report_dir = BASE / "audit_reports" report_dir.mkdir(exist_ok=True) # Save Grok report grok_file = report_dir / f"audit_grok_{timestamp}.txt" with open(grok_file, "w") as f: f.write(f"GROK AUDIT REPORT — 4D FDTD Engine\n") f.write(f"Date: {datetime.now(timezone.utc).isoformat()}\n") f.write(f"Model: {grok_model}\n") f.write(f"Cost: ${grok_cost:.4f}\n") f.write(f"{'=' * 70}\n\n") f.write(grok_report) # Save Gemini report gemini_file = report_dir / f"audit_gemini_{timestamp}.txt" with open(gemini_file, "w") as f: f.write(f"GEMINI AUDIT REPORT — 4D FDTD Engine\n") f.write(f"Date: {datetime.now(timezone.utc).isoformat()}\n") f.write(f"Model: {gemini_model}\n") f.write(f"Cost: ${gemini_cost:.4f}\n") f.write(f"{'=' * 70}\n\n") f.write(gemini_report) # Save combined summary combined_file = report_dir / f"audit_combined_{timestamp}.txt" with open(combined_file, "w") as f: f.write(f"COMBINED EXTERNAL AUDIT — 4D FDTD Engine\n") f.write(f"Date: {datetime.now(timezone.utc).isoformat()}\n") f.write(f"Auditors: Grok ({grok_model}), Gemini ({gemini_model})\n") f.write(f"Total cost: ${grok_cost + gemini_cost:.4f}\n") f.write(f"{'=' * 70}\n\n") f.write(f"{'=' * 70}\n") f.write(f"GROK AUDIT\n") f.write(f"{'=' * 70}\n\n") f.write(grok_report) f.write(f"\n\n{'=' * 70}\n") f.write(f"GEMINI AUDIT\n") f.write(f"{'=' * 70}\n\n") f.write(gemini_report) print() print("=" * 70) print("AUDIT COMPLETE") print("=" * 70) print(f"Grok report: {grok_file}") print(f"Gemini report: {gemini_file}") print(f"Combined: {combined_file}") print(f"Total cost: ${grok_cost + gemini_cost:.4f}")