#!/usr/bin/env python3 """Validate a complete candidate before activation, using only disposable project data.""" import hashlib import json from pathlib import Path import subprocess import sys import tempfile package = Path(sys.argv[1]).resolve() manifest = json.loads((package / "aie-package.json").read_text()) if manifest.get("schemaVersion") != 1 or manifest.get("protocolVersion") != 4: raise SystemExit("Unsupported native package manifest") if manifest.get("localServerProtocol") not in (None, 1): raise SystemExit("Unsupported local server protocol") if manifest.get("localServerEvents") not in (None, "sse"): raise SystemExit("Unsupported local server event transport") expected = manifest["files"] actual = set() for path in package.rglob("*"): if path.is_symlink(): raise SystemExit("Package components must be regular files, not symlinks") if path.is_file() and path.name != "aie-package.json": relative = str(path.relative_to(package)); actual.add(relative) if hashlib.sha256(path.read_bytes()).hexdigest() != expected.get(relative): raise SystemExit("Package integrity mismatch: " + relative) aie_name = "ai-engineering.exe" if "ai-engineering.exe" in actual else "ai-engineering" sqlite_name = "sqlite3.exe" if "sqlite3.exe" in actual else "sqlite3" if actual != set(expected) or not {aie_name, sqlite_name}.issubset(actual): raise SystemExit("Incomplete package components") if not any(name.startswith("libsqlitejdbc.") or name == "sqlitejdbc.dll" for name in actual): raise SystemExit("Missing companion SQLite JDBC library") aie, sqlite = package / aie_name, package / sqlite_name if subprocess.check_output([str(aie), "--version"], text=True).strip() != "ai-engineering " + manifest["aieVersion"]: raise SystemExit("AIE package version mismatch") if subprocess.check_output([str(sqlite), "--version"], text=True).split()[0] != manifest["sqliteVersion"]: raise SystemExit("SQLite package version mismatch") with tempfile.TemporaryDirectory(prefix="aie-package-check-") as temporary: root = Path(temporary) subprocess.run([str(aie), "init", "--name", "Disposable package verification"], cwd=root, check=True, stdout=subprocess.DEVNULL) database = root / ".ai-engineering/aie.db" if not database.is_file(): raise SystemExit("Native AIE did not create its disposable database") result = subprocess.check_output([str(sqlite), "-readonly", str(database), "PRAGMA quick_check;"], text=True).strip() if result != "ok": raise SystemExit("Packaged SQLite cannot read the native AIE database") if manifest.get("localServerProtocol") == 1: try: subprocess.run([str(aie), "start"], cwd=root, check=True, stdout=subprocess.DEVNULL, timeout=20) status = json.loads(subprocess.check_output([str(aie), "server", "status"], cwd=root, text=True, timeout=10)) if status["state"] != "RUNNING" or status["server"]["protocol"] != 1 or status["server"]["build"] != manifest["buildIdentity"]: raise SystemExit("Native server build/protocol mismatch") subprocess.run([str(aie), "work", "create", "Disposable server verification"], cwd=root, check=True, stdout=subprocess.DEVNULL, timeout=20) work = json.loads(subprocess.check_output([str(aie), "work", "show", "WRK-0001", "--format", "json"], cwd=root, text=True, timeout=20)) if work["id"] != "WRK-0001": raise SystemExit("Native client/server roundtrip failed") except BaseException: log = root / ".ai-engineering/.local/server/startup.log" if log.is_file(): print(log.read_text()[-12000:], file=sys.stderr) raise finally: subprocess.run([str(aie), "stop", "--timeout-seconds", "10"], cwd=root, check=True, stdout=subprocess.DEVNULL, timeout=15) if manifest.get("localServerEvents") == "sse": subprocess.run([sys.executable, str(Path(__file__).with_name("native-sse-smoke.py")), "--binary", str(aie)], check=True, timeout=120) print("Native package verified:", package)