Files
aigc-home/docs/hel-892/batch178-aio-protocol/verify.py
T

110 lines
4.6 KiB
Python

"""Check bytes, evidence links, inert annotations, plan and freeze; never execute samples."""
import ast
import base64
import csv
import hashlib
import json
import sys
from acquire import COMMITS, sha
from build import HERE, ROOT, write
def read(name):
return json.loads((HERE / name).read_text())
def main():
baseline = read("baseline.json")
assert all(sha((ROOT / r["path"]).read_bytes()) == r["sha256"] for r in baseline["files"])
proofs = read("engine-sources.json") + read("direct-sources.json")
for proof in proofs:
assert proof["commit"] == COMMITS[proof["version"]]
record = read("network/" + proof["request_key"] + ".json")
body = (HERE / "network" / (proof["request_key"] + ".body")).read_bytes()
assert sha(body) == record["sha256"] and len(body) == record["bytes"]
assert record["requested_url"].endswith("/" + proof["path"] + "?ref=" + proof["commit"])
item = json.loads(body)
raw = (HERE / proof["file"]).read_bytes()
assert base64.b64decode(item["content"]) == raw
assert (
hashlib.sha1(b"blob " + str(len(raw)).encode() + b"\0" + raw).hexdigest()
== item["sha"]
== proof["git_blob_sha1"]
)
assert sha(raw) == proof["sha256"] and proof["retrieved_at"] == record["finished_at"]
ast.parse(raw)
evidence = read("branch-evidence.json")
aio_class = ast.parse(evidence["aux:AIO_Preprocessor"]["original_class"]["text"]).body[0]
assert not aio_class.bases
assert not any(
isinstance(n, ast.Name) and n.id == "OUTPUT_IS_LIST" for n in ast.walk(aio_class)
)
for ct in ("DWPreprocessor", "AnimalPosePreprocessor"):
tree = ast.parse(evidence["aux:" + ct]["original_class"]["text"])
fn = next(
n
for n in tree.body[0].body
if isinstance(n, ast.FunctionDef) and n.name == "estimate_pose"
)
returned = fn.body[-1].value
assert isinstance(returned, ast.Dict)
values = {ast.literal_eval(k): v for k, v in zip(returned.keys, returned.values)}
assert set(values) == {"ui", "result"}
assert isinstance(values["result"], ast.Tuple) and len(values["result"].elts) == 2
matrix = read("protocol-matrix.json")
for row in matrix["branches"]:
fragment = evidence[row["evidence"]]
text = (HERE / fragment["source"]["file"]).read_text()
a, b = fragment["lines"]
assert "\n".join(text.splitlines()[a - 1 : b]) == fragment["text"]
assert sha(fragment["text"].encode()) == fragment["sha256"]
assert all(row["anchor"] in text.splitlines()[i - 1] for i in row["lines"])
assert not row["engine_executed"] and row["target_compatibility"] == "unknown"
with (HERE / "protocol-matrix.csv").open(newline="") as stream:
rows = [{k: json.loads(v) for k, v in row.items()} for row in csv.DictReader(stream)]
assert rows == matrix["branches"] and len(rows) == 26
branches = {r["branch"] for r in rows}
samples = read("inert-samples.json")["samples"]
for sample in samples:
assert sample["symbolic_only"] is True and sample["upstream_invoked"] is False
assert set(sample["observed_branches"]) <= branches
assert sample["result_python_container"] == "tuple"
assert sample["shape"]["result"] == ["<IMAGE_TOKEN>", "<POSE_LIST_TOKEN>"]
plan = read("target-plan.json")
assert plan["executed"] is False and plan["target_requests"] == 0
assert plan["comfyui_base_url"] is None and plan["port"] is None
assert len(plan["facts"][2]["api_classes"]) == 6
for entry in read("reused-source-records.json"):
proof = entry["original_record"]
path = ROOT / "docs/hel-892/batch177-controlnet-provenance" / proof["file"]
assert sha(path.read_bytes()) == proof["sha256"]
for path in HERE.glob("*.py"):
ast.parse(path.read_text())
write(
sys.argv[1] if len(sys.argv) == 2 else "verification.json",
dict(
frozen_files=len(baseline["files"]),
changed_old_files=[],
exceptions=[],
exact_commit_texts=len(proofs),
branch_rows=len(rows),
inert_shape_annotations=len(samples),
simulated_engine_runs=0,
real_engine_tests=0,
target_requests=0,
csv_equal=True,
source_evidence_checked=True,
tools_ast=True,
),
)
print(
len(baseline["files"]),
"old files frozen; 26 branch rows and inert annotations checked; engine tests 0",
)
if __name__ == "__main__":
main()