"""cross-examine-patch.py — Emergency-sync Secondary Brain gate. Loads Lighthouse CI output and the Chrome DevTools MCP report and cross- examines them (primary/secondary cohesion). Fails the gate unless both agree. Usage: python scripts/cross-examine-patch.py \ --preview --lighthouse --secondary """ import argparse import json import pathlib import sys parser = argparse.ArgumentParser() parser.add_argument("--preview") parser.add_argument("--lighthouse") parser.add_argument("--secondary") args = parser.parse_args() print(f"Cross-examining patch at {args.preview}") failures = [] warnings = [] # ---- Lighthouse CI manifest ------------------------------------------------ lh_path = pathlib.Path(args.lighthouse) if args.lighthouse else None if lh_path and lh_path.exists(): manifest = json.loads(lh_path.read_text()) for run in manifest if isinstance(manifest, list) else [manifest]: if run.get("categories", {}).get("performance", {}).get("score", 0) < 0.9: failures.append(f"Lighthouse performance < 0.9: {run.get('categories', {}).get('performance', {}).get('score')}") if run.get("categories", {}).get("accessibility", {}).get("score", 0) < 1.0: failures.append("Lighthouse accessibility < 1.0") else: warnings.append("No Lighthouse manifest found — relying on MCP secondary report") # ---- Chrome DevTools MCP secondary report ---------------------------------- secondary_path = pathlib.Path(args.secondary) if args.secondary else None if secondary_path and secondary_path.exists(): data = json.loads(secondary_path.read_text()) if data.get("verdict") != "pass": failures.append(f"Secondary brain verdict: {data.get('verdict')} -> {data.get('reasoning')}") else: failures.append("Secondary brain report missing — gate cannot pass") for w in warnings: print(f"WARN: {w}") if failures: print("FAIL:") for f in failures: print(f" - {f}") sys.exit(1) print("PASS: Both primary and secondary brain agree — cohesive unit verified")