Files
aigc-home/docs/hel-892/zimage-upscale-document-evidence/verify_sources.py
T
2026-10-31 17:14:58 +08:00

170 lines
7.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"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 "member_crc32" in e:
root = REF / e["identity_root"]
m = json.loads((root / (e["identity_key"] + "-registry.json")).read_bytes())
members = json.loads((root / (e["identity_key"] + "-members.json")).read_bytes())
member = next(x for x in members if x["name"] == e["file"])
assert m["version"] == e["tag"]
assert member["crc"] == e["member_crc32"] == zlib.crc32(raw)
assert e["package_sha256"].startswith("unknown")
proof = "准确发布成员SHA/CRC;Range选定文本,完整包SHA与Git绑定未知"
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 (
(REF / x["local_file"]).resolve() == (REF / e["license_file"]).resolve()
or 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))
from build_definition import PATHS, REPO
graph = json.loads((REPO / PATHS[0]).read_bytes())
assert len(inv["fields"]) == 25 and len(inv["notes"]) == 6
assert [(s["nodes"], s["edges"]) for s in inv["scopes"]] == [(9, 4), (14, 20)]
assert all(m == 0 for s in inv["scopes"] for m in s["modes"])
assert all(s["named_nodes"] == 0 for s in inv["scopes"])
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["pointer"], n["text"]) for n in inv["notes"]] == [
(n["pointer"], n["text"]) for n in block["comments"]
]
checked = 0
mismatches = []
for scope in [graph, *graph["definitions"]["subgraphs"]]:
ns = {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, kind = [
edge[k]
for k in ["id", "origin_id", "origin_slot", "target_id", "target_slot", "type"]
]
if a in ns:
assert eid in ns[a]["outputs"][ao]["links"]
else:
assert a == -10 and eid in scope["inputs"][ao]["linkIds"]
if z in ns:
assert ns[z]["inputs"][zi]["link"] == eid
else:
assert z == -20 and eid in scope["outputs"][zi]["linkIds"]
checked += 1
assert checked == 24
assert graph["definitions"]["subgraphs"][0]["outputs"][0]["linkIds"] == [97, 97]
assert sum(e.get("id") == 97 for e in graph["definitions"]["subgraphs"][0]["links"]) == 1
assert inv["extra"]["frontendVersion"] == "1.41.13"
e = next(e for e in rows if e["id"] == "core0211" 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 ast.literal_eval(ret) == ()
output_checks = [
dict(
node=143,
accurate_return_types=[],
source_outputs=next(n for n in graph["nodes"] if n["id"] == 143)["outputs"],
)
]
assert output_checks[0]["source_outputs"] == []
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=24,
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, 25 positions, 6 notes, 24 physical edges")
if __name__ == "__main__":
main()