import hashlib
import json
import os
import subprocess
import sys
HERE = os.path.dirname(os.path.abspath(__file__))
WORKSPACE = os.path.abspath(os.path.join(HERE, "..", "..", "..", "..", ".."))
ORACLE = os.path.join(
WORKSPACE, "compatibility", "oracle", "node_modules", "overpy", "cli.js"
)
PROBES_JSON = os.path.join(HERE, "probes.json")
LANGUAGE = "en-US"
def sha256_file(path):
digest = hashlib.sha256()
with open(path, "rb") as handle:
for chunk in iter(lambda: handle.read(65536), b""):
digest.update(chunk)
return digest.hexdigest()
def run_probe(probe):
name = probe["id"]
source_path = os.path.join(HERE, probe["source"])
if not os.path.isfile(source_path):
return (name, f"missing probe source {probe['source']}")
if sha256_file(source_path) != probe["sha256"]:
return (name, f"probe source hash mismatch for {probe['source']}")
output_path = source_path + ".ws"
try:
result = subprocess.run(
[
"node", ORACLE, "compile",
"-i", source_path, "-l", LANGUAGE, "-o", output_path,
],
capture_output=True,
text=True,
timeout=120,
)
except FileNotFoundError:
return (name, f"node or the pinned oracle is unavailable at {ORACLE}")
except subprocess.TimeoutExpired:
return (name, "oracle timed out")
emitted = ""
if os.path.exists(output_path):
with open(output_path, "rb") as handle:
emitted = handle.read()
os.unlink(output_path)
expected = probe["expect"]
if expected == "success":
if result.returncode != 0:
stderr = (result.stderr or "").strip().replace("\n", " | ")
return (name, f"expected success, oracle rejected: {stderr[:200]}")
emitted_sha = hashlib.sha256(emitted).hexdigest()
if emitted_sha != probe["outputSha256"]:
return (
name,
f"emission hash mismatch: expected {probe['outputSha256']}, "
f"got {emitted_sha}",
)
else:
if result.returncode == 0:
return (name, "expected failure, oracle accepted")
fragment = probe.get("diagnosticContains")
if fragment and fragment not in (result.stderr or ""):
stderr = (result.stderr or "").strip().replace("\n", " | ")
return (
name,
f"diagnostic category mismatch: missing "
f"{fragment!r} in: {stderr[:200]}",
)
return (name, None)
def main():
if not os.path.isfile(ORACLE):
print(
f"oracle not installed at {ORACLE}; run "
f"`pnpm install --dir compatibility/oracle` first",
file=sys.stderr,
)
return 2
with open(PROBES_JSON, encoding="utf-8") as handle:
data = json.load(handle)
if data.get("schemaVersion") != 1:
print("unsupported probes schemaVersion", file=sys.stderr)
return 2
failures = []
for probe in data["probes"]:
name, error = run_probe(probe)
if error is None:
print(f"OK {name}")
else:
print(f"FAIL {name}: {error}")
failures.append(f"{name}: {error}")
if failures:
print(f"\n{len(failures)} probe(s) failed against the pinned oracle")
return 1
print(f"\nall {len(data['probes'])} probes match the pinned oracle evidence")
return 0
if __name__ == "__main__":
sys.exit(main())