Files
aigc-home/docs/hel-892/flux2-lineart-document-evidence/verify_sources.py
T

76 lines
2.7 KiB
Python

"Rehash every necessary text and audit bidirectional endpoints without repair."
import hashlib
import json
from build_definition import OUT, REF, build, canonical
from inventory import inventory
def main():
b = build()
inv = inventory()
rows = json.loads((REF / "sources.json").read_bytes())["sources"]
for e in rows:
data = (REF / e["local_file"]).read_bytes()
assert hashlib.sha256(data).hexdigest() == e["sha256"]
if e.get("git_blob_sha1"):
assert (
hashlib.sha1(b"blob " + str(len(data)).encode() + b"\0" + data).hexdigest()
== e["git_blob_sha1"]
)
commit = REF / (e["id"] + "-commit.json")
tree = REF / (e["id"] + "-tree.json")
if (
commit.exists()
and tree.exists()
and e["commit"] == json.loads(commit.read_bytes())["sha"]
):
c = json.loads(commit.read_bytes())
t = json.loads(tree.read_bytes())
assert e["tree"] == c["commit"]["tree"]["sha"]
obj = next(x for x in t["tree"] if x["path"] == e["file"])
assert obj["sha"] == e["git_blob_sha1"]
for a, f in zip(inv["fields"], b["fields"], strict=True):
assert (
a["pointer"] == f["pointer"]
and a["json_type"] == f["json_type"]
and canonical(a["value"]) == canonical(f["value"])
)
assert f["evidence"], (f["node_id"], f["field"])
assert len(inv["fields"]) == 96 and len(inv["nodes"]) == 49 and len(inv["edges"]) == 73
conflicts = [
e for e in inv["edges"] if e["source_backref"] is False or e["target_backref"] is False
]
assert [e["edge"][0] for e in conflicts] == [328, 329]
assert [e["target_actual_link_inputs"] for e in conflicts] == [
[(7, "width")],
[(8, "height")],
]
assert not any(n["named_present"] for n in inv["nodes"])
assert [(n["node"], n["mode"]) for n in inv["nodes"] if n["mode"] != 0] == [(189, 4)]
for e in b["sources"]:
data = (OUT / e["local_file"]).read_bytes()
assert hashlib.sha256(data).hexdigest() == e["sha256"]
assert 1 <= e["lines"][0] <= e["lines"][1] <= len(data.decode().splitlines())
result = dict(
all_pass=True,
source_files=len(rows),
citations=len(b["sources"]),
counts=b["counts"],
endpoint_conflicts=conflicts,
upstream_executed=False,
)
(OUT / "source-check.json").write_text(json.dumps(result, ensure_ascii=False, indent=2) + "\n")
print(
"PASS",
len(rows),
"text hashes,",
len(b["sources"]),
("citations, 96 positions; two source conflicts retained"),
)
if __name__ == "__main__":
main()