Files
aigc-home/docs/hel-892/zimage-wash-document-evidence/verify_sources.py
T

216 lines
9.0 KiB
Python

"Verify exact text, Git/package provenance and source graph without upstream imports."
import ast
import hashlib
import json
import zlib
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"]
if e["id"] == "litegraph":
root = REF / e["identity_root"]
assert (
json.loads((root / "litegraph__package__package.json").read_bytes())["version"]
== "0.15.15"
)
front = next(
x for x in rows if x["id"] == "frontend" and x["file"] == "package-lock.json"
)
lock = json.loads((REF / front["local_file"]).read_bytes())
assert (
lock["packages"]["node_modules/@comfyorg/litegraph"]["integrity"]
== "sha512-" + e["artifact_sha512_base64"]
)
proof = (
"Pinned frontend1.23.0 lock, exact0.15."
"15 member SHA; whole artifact digest r"
"eused, Git unknown"
)
elif e["id"] == "seedpkg":
root = REF / e["identity_root"]
acquisition = json.loads((root / "seedpkg-acquisition.json").read_bytes())
assert acquisition["archive_sha256"] == e["package_sha256"]
assert not acquisition["complete_archive_retained"]
member = next(
x
for x in json.loads((root / "seedpkg-members.json").read_bytes())
if x["name"] == e["file"]
)
assert member["crc"] == e["member_crc32"] == zlib.crc32(raw)
assert e["file"] in acquisition["selected_members_verified_before_removal"]
assert json.loads((root / "seedpkg-registry.json").read_bytes())["version"] == "2.5.22"
proof = (
"Exact2.5.22 acquisition SHA recorded; retained text SHA/CRC rechecked; "
"full archive removed, Git unknown"
)
else:
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 blo"
"b; complete tree listing, not fu"
"ll tree-object rehash"
)
if not e["license_file"].startswith("未知"):
license_row = next(
x for x in rows if x["id"] == e["id"] and x["local_file"] == e["license_file"]
)
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"]) == 138 and len(inv["notes"]) == 1
assert inv["scopes"][0]["nodes"] == 66 and inv["scopes"][0]["edges"] == 63
assert sum(n["mode"] == 0 for n in inv["nodes"]) == 56
assert sum(n["mode"] == 4 for n in inv["nodes"]) == 10
assert sum(f["json_type"] == "list" for f in inv["fields"]) == 2
assert sum(f["json_type"] == "NoneType" for f in inv["fields"]) == 1
assert {n["node"] for n in inv["empty"] if n["state"] == "empty_list"} == {
311,
322,
617,
294,
233,
}
assert {n["node"] for n in inv["empty"] if n["state"] == "missing"} == {
310,
316,
317,
616,
397,
626,
318,
315,
319,
628,
242,
682,
}
assert [(n["node"], len(n["text"])) for n in inv["notes"]] == [(681, 11)]
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"]),
)
assert [(n["node"], n["pointer"], n["text"]) for n in inv["notes"]] == [
(int(n["node_id"]), n["pointer"], n["text"]) for n in block["comments"]
]
ns = {n["id"]: n for n in inv["nodes"]}
mismatches = []
for eid, a, ao, z, zi, kind in inv["links"]:
src, dst = ns[a]["outputs"][ao], ns[z]["inputs"][zi]
assert eid in src["links"] and dst["link"] == eid
if not (src["type"] == "*" or kind == src["type"]) or not (
dst["type"] == "*" or kind == dst["type"]
):
mismatches.append(
dict(
edge=eid, source_type=src["type"], destination_type=dst["type"], edge_type=kind
)
)
assert ns[143]["outputs"][1]["links"] == []
assert ns[303]["widgets_values"] == ["DepthAnythingPreprocessor", 1024]
assert ns[321]["widgets_values"][6:8] == ["total_pixel(kilo pixel)", 2048]
assert [x.get("link") for x in ns[675]["inputs"][:2]] == [3406, 3407]
assert ns[313]["inputs"][0]["link"] == 3270 and ns[314]["inputs"][0]["link"] == 3130
assert ns[313]["inputs"][3]["link"] == 3408 and ns[314]["inputs"][3]["link"] == 2687
assert ns[313]["inputs"][5]["link"] == 3097 and ns[314]["inputs"][5]["link"] == 3112
assert ns[618]["widgets_values"] == [9528, "fixed", None]
assert "ver" not in ns[679]["properties"] and "cnr_id" not in ns[679]["properties"]
assert not any(n["type"] in ["VAEEncode", "SetLatentNoiseMask"] for n in inv["nodes"])
assert ns[629]["widgets_values"][0] == "seedvr2_ema_3b_fp8_e4m3fn.safetensors"
assert ns[231]["inputs"][1]["link"] == 2067 and ns[230]["widgets_values"] == ["SeedVR 修复"]
assert (
inv["extra"]["frontendVersion"] == "1.23.0"
and inv["extra"]["workflowRendererVersion"] == "LG"
)
output_checks = []
for nid, key in [
(295, "core0375"),
(234, "core0367"),
(322, "core0376"),
(617, "core051"),
(233, "core0359"),
]:
e = next(e for e in rows if e["id"] == key and e["file"] == "nodes.py")
tree = ast.parse((REF / e["local_file"]).read_bytes())
save = next(c for c in tree.body if isinstance(c, ast.ClassDef) and c.name == "SaveImage")
ret = next(
c.value
for c in save.body
if isinstance(c, ast.Assign)
and any(isinstance(t, ast.Name) and t.id == "RETURN_TYPES" for t in c.targets)
)
assert isinstance(ret, ast.Tuple) and ret.elts == []
if ns[nid]["type"] == "PreviewImage":
preview = next(
c for c in tree.body if isinstance(c, ast.ClassDef) and c.name == "PreviewImage"
)
assert [ast.unparse(b) for b in preview.bases] == ["SaveImage"]
assert not any(
isinstance(c, ast.Assign)
and any(isinstance(t, ast.Name) and t.id == "RETURN_TYPES" for t in c.targets)
for c in preview.body
)
output_checks.append(
dict(
node=nid,
saved_widgets=ns[nid]["widgets_values"],
source_outputs=ns[nid]["outputs"],
accurate_return_types=[],
source_id=key,
)
)
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())
result = dict(
sources=checks,
counts=block["counts"],
physical_edges_checked=63,
exact_core_output_checks=output_checks,
original_type_mismatches=mismatches,
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/package text, 138 positions, 1 note, 63 physical edges")
if __name__ == "__main__":
main()