Files
aigc-home/docs/hel-892/zimage-style-document-evidence/verify_sources.py
T
2026-10-31 16:13:50 +08:00

111 lines
4.2 KiB
Python

"Verify exact text, Git/package provenance and source graph without upstream imports."
import hashlib
import json
from build_definition import OUT, REF, build, canonical
from inventory import inventory
def object_hash(kind, raw):
return hashlib.sha1(kind.encode() + b" " + str(len(raw)).encode() + b"\0" + raw).hexdigest()
def main():
block, inv = build(), inventory()
rows = json.loads((REF / "sources.json").read_bytes())["sources"]
checks = []
for e in rows:
raw = (REF / e["local_file"]).read_bytes()
assert hashlib.sha256(raw).hexdigest() == e["sha256"]
root = REF / e["identity_root"]
key = e["identity_key"]
commit = (root / (key + "-commit.txt")).read_bytes()
tree = json.loads((root / (key + "-tree.json")).read_bytes())
assert object_hash("commit", commit) == e["commit"]
assert commit.decode().splitlines()[0] == "tree " + e["tree"] == "tree " + tree["sha"]
assert not tree["truncated"]
assert (
next(t["sha"] for t in tree["tree"] if t["path"] == e["file"])
== e["git_blob_sha1"]
== object_hash("blob", raw)
)
tag = json.loads((root / (key + "-tag.json")).read_bytes())
tag_raw = (root / (key + "-tag.txt")).read_bytes()
assert tag["commit"] == e["commit"]
if tag["object"] == e["commit"]:
assert tag_raw == commit
else:
assert object_hash("tag", tag_raw) == tag["object"]
assert tag_raw.decode().startswith("object " + e["commit"] + "\n")
proof = (
"Rehashed commit/tag/selected blob; complete tree listing, not full tree-object rehash"
)
if not e["license_file"].startswith("未知"):
license_row = next(x for x in rows if x["id"] == e["id"] and x["file"] == "LICENSE")
assert (
hashlib.sha256((REF / e["license_file"]).read_bytes()).hexdigest()
== license_row["sha256"]
)
checks.append(dict(id=e["id"], file=e["file"], sha256=e["sha256"], proof=proof))
assert len(inv["fields"]) == 44 and not inv["notes"]
assert [(s["nodes"], s["edges"]) for s in inv["scopes"]] == [
(15, 17),
(15, 28),
(3, 8),
]
assert sum(f["json_type"] == "list" for f in inv["fields"]) == 1
for a, f in zip(inv["fields"], block["fields"], strict=True):
assert (a["pointer"], a["json_type"], canonical(a["value"])) == (
f["pointer"],
f["json_type"],
canonical(f["value"]),
)
for e in block["sources"]:
raw = (OUT / e["local_file"]).read_bytes()
assert hashlib.sha256(raw).hexdigest() == e["sha256"]
assert 1 <= e["lines"][0] <= e["lines"][1] <= len(raw.decode().splitlines())
from build_definition import PATHS, REPO
graph = json.loads((REPO / PATHS[0]).read_bytes())
checked = 0
for scope in [graph, *graph["definitions"]["subgraphs"]]:
nodes = {n["id"]: n for n in scope["nodes"]}
for edge in scope["links"]:
if isinstance(edge, list):
eid, a, ao, z, zi, _kind = edge
else:
eid, a, ao, z, zi = [
edge[k]
for k in [
"id",
"origin_id",
"origin_slot",
"target_id",
"target_slot",
]
]
if a in nodes:
assert eid in nodes[a]["outputs"][ao]["links"]
else:
assert a == -10 and eid in scope["inputs"][ao]["linkIds"]
if z in nodes:
assert nodes[z]["inputs"][zi]["link"] == eid
else:
assert z == -20 and eid in scope["outputs"][zi]["linkIds"]
checked += 1
assert checked == 53
result = dict(
physical_edges_checked=checked,
sources=checks,
counts=block["counts"],
inventory_matches_definition=True,
no_upstream_execution=True,
)
(OUT / "source-check.json").write_text(json.dumps(result, ensure_ascii=False, indent=2) + "\n")
print("PASS exact Git text and scopes; 44 positions, zero notes")
if __name__ == "__main__":
main()