"""Integrity checks for agent-runtime-comparison.md. Usage: python3 -I validate.py <report.md>"""
import re, sys
t = open(sys.argv[1]).read()
ORDER = ["codex","goose","hermes-agent","letta-code","nullclaw","openclaw","opencode","pi","software-agent-sdk","spore"]
PINS = {"openai/codex":"065e18b04021008e2e7d504db8dc50275f589f73","aaif-goose/goose":"c546eaa1cd02696e4cd628e203df51429e158175",
"NousResearch/hermes-agent":"3ef8bf4f68c91dce2870eba5625426b32ac6ba58","letta-ai/letta-code":"52a3bb3bc1999a2fd0f06b1dc2e4b0d94a925c21",
"nullclaw/nullclaw":"5f1cade05c19b6ecdfb9565ae3305e892e484da9","openclaw/openclaw":"ac0771450bce15e44c3d4e3dda76ccf38f8b6c61",
"anomalyco/opencode":"2204311ec6e966db8d67988ab3638ad95a811a46","earendil-works/pi":"2126569de40d5595dd3f41f7af126001f04c1de0",
"OpenHands/software-agent-sdk":"cf8158bc25f5552c50ad131a5f0ce2dbc297789b","RHIZOGRIP/spore":"4a9db7982fda3ae2f30a817fa0037c92e246f97f"}
REQUIRED = ["Capability interface","Durable evidence","Specialist definition","Confinement","Embedding interfaces","Service dependencies"]
ok = True
for m in re.finditer(r'((?:^\|.*\n)+)', t, re.M):
lines = m.group(1).splitlines(); hdr = lines[0]
if any(h in hdr for h in REQUIRED):
rows = [l.split('|')[1].strip() for l in lines[2:]]
empty = [r for l, r in zip(lines[2:], rows) if any(not c.strip() for c in l.split('|')[2:-1])]
print(("OK " if rows == ORDER and not empty else "FAIL"), hdr[:80]); ok &= rows == ORDER and not empty
ex = t.split('## Executive assessment (≤300 words)')[1].split('\n## ')[0]
n = len(ex.split()); print("exec words", n); ok &= n <= 300
refs = set(re.findall(r'\[([MAPECXIO]\d+)\]', t)); defs = set(re.findall(r'^(?:- )?\[([MAPECXIO]\d+)\]', t, re.M))
print("undefined refs", sorted(refs - defs)); ok &= not (refs - defs)
bad = [(r, s) for r, s in re.findall(r'https://github.com/([^/]+/[^/]+)/blob/([0-9a-f]{40})/', t) if PINS.get(r) != s]
print("links not pinned to study revisions", bad[:10]); ok &= not bad
sys.exit(0 if ok else 1)