Files
aigc-home/docs/hel-892/qwen21-bfs-document-evidence/verify_sources.py
T

231 lines
10 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 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 "package_record" in e:
package = json.loads((REF / e["package_record"]).read_bytes())
reg = json.loads((REF / e["registry_record"]).read_bytes())
member = next(x for x in package["members"] if x["path"] == e["file"])
assert member["saved"] and member["sha256"] == e["sha256"]
assert reg["version"] == e["tag"] == "1.0.0"
assert package["sha256"] == e["artifact_sha256"]
proof = (
"复用已审准确发布成员SHA/registry/历史整包摘要记录;本轮未获取整包,不证明Git绑定"
)
elif e["id"] == "llama-unavailable":
proof = "准确20571提交不可得的有界调查原记录,仅缺口,不作为schema实现"
elif "member_crc32" in e:
root = REF / e["identity_root"]
m = json.loads((root / (e["id"] + "-registry.json")).read_bytes())
members = json.loads((root / (e["id"] + "-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))
assert len(inv["fields"]) == 77 and len(inv["notes"]) == 4
assert [(v["nodes"], v["edges"], v["named_nodes"]) for v in inv["scopes"]] == [(36, 33, 34)]
assert inv["scopes"][0]["modes"] == [0] * 36
assert [(v["node"], v["state"]) for v in inv["empty"]] == [
(472, "empty_list"),
(487, "missing"),
(500, "missing"),
]
assert sum(f["json_type"] == "list" for f in inv["fields"]) == 1
assert sum(f["json_type"] == "NoneType" for f in inv["fields"]) == 1
assert sum(f["json_type"] not in ["list", "dict", "NoneType"] for f in inv["fields"]) == 75
from build_definition import PATHS, REPO
raw = (REPO / PATHS[0]).read_bytes()
g = json.loads(raw)
assert (
hashlib.sha256(raw).hexdigest()
== "6a9da127ee2bccafeff7499ab3d62f349aec1eff4265cb211bdec578dba1fe7b"
)
assert not g.get("definitions") and not block["boundaries"]
def pointer(obj, p):
for bit in p.strip("/").split("/"):
bit = bit.replace("~1", "/").replace("~0", "~")
obj = obj[int(bit)] if isinstance(obj, list) else obj[bit]
return obj
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 canonical(pointer(g, f["named_pointer"])) == canonical(f["named_value"])
if f["node_id"] != "480":
assert canonical(f["named_value"]) == canonical(f["value"])
ns = {n["id"]: n for n in g["nodes"]}
for i, n in enumerate(g["nodes"]):
if n["type"] in ["Note", "MarkdownNote"]:
continue
expected = {
f"/nodes/{i}/widgets_values_named/" + k.replace("~", "~0").replace("/", "~1")
for k in n.get("widgets_values_named", {})
}
assert expected == {
f["named_pointer"] for f in block["fields"] if f["pointer"].startswith(f"/nodes/{i}/")
}
for eid, a, ao, z, zi, _ in g["links"]:
assert eid in ns[a]["outputs"][ao]["links"] and ns[z]["inputs"][zi]["link"] == eid
assert ns[13]["widgets_values"] == ["9:16 (Portrait Widescreen)", 2, 32]
assert type(ns[13]["widgets_values"][1]) is int
assert (
ns[477]["widgets_values"]
== ns[478]["widgets_values"]
== [512, 512, "lanczos", "resize", "0, 0, 0", "center", 32, "cpu"]
)
assert ns[486]["inputs"][0]["link"] == 820 and ns[486]["inputs"][1]["link"] == 821
assert ns[490]["inputs"][3]["link"] == 788 and ns[496]["outputs"][2]["links"] == []
assert ns[496]["widgets_values"][0] == ns[499]["widgets_values"][7]
t = ns[496]["widgets_values"][0]
assert (len(t), t.count("\n"), t.count("\\n"), t.endswith("\n")) == (367, 0, 0, False)
assert (
ns[498]["widgets_values"] == [3, "right", True, None]
and ns[498]["widgets_values_named"]["Update inputs"] is None
)
assert ns[461]["widgets_values"] == ["image/Qwen_image_2.1", "png", "8-bit", "sRGB"]
assert ns[480]["inputs"][0]["link"] == 809 and ns[480]["inputs"][1]["link"] == 744
history = ns[480]["widgets_values"][0]
named = ns[480]["widgets_values_named"]["rgthree_comparer"]
assert len(history) == len(named["images"]) == 2
for a, b in zip(history, named["images"], strict=True):
assert b["img"] == {} and {k: v for k, v in b.items() if k != "img"} == a
assert [a["name"] for a in history] == ["A", "B"]
assert [
(n["node"], n["characters"], n["lf_count"], n["literal_backslash_n"], n["ends_with_lf"])
for n in inv["notes"]
] == [
(463, 1248, 22, 0, False),
(476, 2610, 55, 0, True),
(516, 109, 2, 0, False),
(517, 111, 0, 0, False),
]
bind = {n["node_id"]: n for n in block["node_bindings"]}
unknown = [str(n["id"]) for n in g["nodes"] if n["type"] in ["SetNode", "GetNode"]]
assert len(unknown) == 11 and all(bind[n]["source_id"] == "unknown" for n in unknown)
assert all(
len(n["candidates"]) == 1 for n in block["node_bindings"] if n["node_id"] not in unknown
)
assert all(bind[n]["source_id"] == "core0371" for n in ["13", "472", "496", "495", "490"])
fields = {(f["node_id"], f["field"]): f for f in block["fields"]}
assert (
fields["496", "resolution"]["schema_default"] == "1024"
and fields["495", "device"]["schema_default"] == "'auto'"
)
sampler = next(e for e in rows if e["id"] == "core0371" and e["file"] == "comfy/samplers.py")
assert '"deis_2m"' not in (REF / sampler["local_file"]).read_text()
assert '"deis"' in (REF / sampler["local_file"]).read_text()
(OUT / "source-check.json").write_text(
json.dumps(
dict(
files=checks,
positions=77,
scalar_positions=75,
history_positions=1,
null_positions=1,
standard_notes=4,
scopes=inv["scopes"],
source_edges_verified=33,
unknown_alias_nodes=unknown,
deis_2m_in_core_sampler_enum=False,
upstream_executed=False,
),
ensure_ascii=False,
indent=2,
)
+ "\n"
)
(OUT / "node-version-bindings.json").write_text(
json.dumps(block["node_bindings"], ensure_ascii=False, indent=2) + "\n"
)
print(
"PASS exact sources,77 dual positions/75 scalars+history+null,"
"4 notes,33 edges; aliases unknown"
)
if __name__ == "__main__":
main()