Files
aigc-home/docs/hel-892/moge3-document-evidence/verify_sources.py
T
2026-10-31 03:13:25 +08:00

93 lines
3.4 KiB
Python

"""Offline bytes, git provenance, all source positions and boundary endpoints."""
import hashlib
import json
from build_definition import OUT, REF, REPO, build, canonical
from inventory import inventory
def main():
b = build()
seen = set()
for e in b["sources"]:
p = (OUT / e["local_file"]).resolve()
raw = p.read_bytes()
assert hashlib.sha256(raw).hexdigest() == e["sha256"]
assert 1 <= e["lines"][0] <= e["lines"][1] <= len(raw.decode().splitlines())
seen.add(str(p))
if "git_blob_sha1" in e:
assert (
hashlib.sha1(b"blob " + str(len(raw)).encode() + b"\0" + raw).hexdigest()
== e["git_blob_sha1"]
)
t = json.loads((p.parent / (e["id"] + "-tree.json")).read_bytes())
assert t["sha"] == e["commit"] and not t["truncated"]
assert next(x["sha"] for x in t["tree"] if x["path"] == e["file"]) == e["git_blob_sha1"]
for i in json.loads((REF / "identities.json").read_bytes()):
raw = (REF / (i["id"] + "-commit.txt")).read_bytes()
assert (
hashlib.sha1(b"commit " + str(len(raw)).encode() + b"\0" + raw).hexdigest()
== i["commit"]
)
assert raw.decode().splitlines()[0] == "tree " + i["tree"]
assert i["commit"] in (REF / (i["id"] + "-tag.txt")).read_text()
inv = inventory()
assert len(inv["fields"]) == 67 and len(inv["notes"]) == 1
for a, f in zip(inv["fields"], b["fields"], strict=True):
assert (
a["pointer"] == f["pointer"]
and a["scope"] == f["scope"]
and a["json_type"] == f["json_type"]
and canonical(a["value"]) == canonical(f["value"])
)
g = json.loads((REPO / b["source_path"]).read_bytes())
for graph in [g, *g["definitions"]["subgraphs"]]:
ns = {n["id"]: n for n in graph["nodes"]}
for e in graph["links"]:
if isinstance(e, list):
eid, src, slot, dst, inp, _ = e
else:
eid, src, slot, dst, inp = (
e[k]
for k in [
"id",
"origin_id",
"origin_slot",
"target_id",
"target_slot",
]
)
if src != -10:
assert eid in ns[src]["outputs"][slot]["links"]
if dst != -20:
assert ns[dst]["inputs"][inp]["link"] == eid
assert [
n["id"] for s in g["definitions"]["subgraphs"] for n in s["nodes"] if n["mode"] != 0
] == [110]
assert all(n["mode"] == 0 for n in g["nodes"])
assert "widgets_values" not in next(n for n in g["nodes"] if n["id"] == 111)
(OUT / "source-check.json").write_text(
json.dumps(
{
"all_pass": True,
"cited_texts": len(seen),
"citations": len(b["sources"]),
"counts": b["counts"],
"local_file_base": str(OUT.relative_to(REPO)),
"upstream_executed": False,
"exact_frontend": "1.52.7",
"exact_core": "0.36.0",
"grid_core": "0.17.0",
},
ensure_ascii=False,
indent=2,
)
+ "\n"
)
print(("PASS exact provenance, every sco" + "pe/pointer/value and all physica" + "l edges"))
if __name__ == "__main__":
main()