Files
aigc-home/docs/hel-892/portrait-master-document-evidence/verify_sources.py
T
2026-11-01 05:25:54 +08:00

235 lines
9.4 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())
)
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 inv["sha256"] == "4ff20f7350813ec74fc054971b4532200d61661951c103c89f7729f89f7be2ff"
assert len(inv["fields"]) == 69 and len(inv["notes"]) == 1
assert [(s["nodes"], s["edges"], s["named_nodes"]) for s in inv["scopes"]] == [(9, 7, 0)]
assert inv["scopes"][0]["modes"] == [0] * 9 and not inv["empty"]
assert len(inv["definition_boundary"]) == 0
import ast
from build_definition import PATHS, REPO, extract_schema
g = json.loads((REPO / PATHS[0]).read_bytes())
ns = {n["id"]: n for n in g["nodes"]}
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["pointer"])) == canonical(f["value"])
assert not any(k in f for k in ["named_pointer", "named_value"])
assert all("widgets_values_named" not in n for n in ns.values())
for eid, a, ao, z, zi, _ in g["links"]:
assert eid in ns[a]["outputs"][ao]["links"]
assert ns[z]["inputs"][zi]["link"] == eid
ids = {e[0] for e in g["links"]}
declarations = {
lid for n in ns.values() for out in n["outputs"] for lid in out.get("links") or []
}
assert declarations - ids == {2} and ns[51]["outputs"][0]["links"] == [2, 84]
assert [e[:5] for e in g["links"]] == [
[15, 30, 0, 15, 0],
[17, 31, 0, 17, 0],
[84, 51, 0, 83, 0],
[101, 17, 0, 51, 0],
[102, 15, 0, 51, 1],
[105, 79, 0, 51, 2],
[106, 81, 0, 51, 3],
]
assert all(o["links"] is None for n in [79, 81] for o in ns[n]["outputs"][1:])
assert "links" not in ns[51]["outputs"][1] and ns[83]["outputs"][0]["links"] is None
assert ns[30]["outputs"][1].get("links") is None and ns[50]["inputs"] == []
assert ns[51]["widgets_values"] == [4]
assert [len(ns[n]["widgets_values"]) for n in [31, 30, 17, 15, 79, 81, 51, 83]] == [
22,
41,
1,
1,
1,
1,
1,
1,
]
assert len(ns[31]["inputs"]) == 22
note = inv["notes"][0]
assert (
note["characters"],
note["lf_count"],
note["literal_backslash_n"],
note["ends_with_lf"],
) == (28, 0, 0, False)
assert (
hashlib.sha256(note["text"].encode()).hexdigest()
== "0ea9c0d87c55d319fdffe4a3002a1265c9a2e91ec9f476285aa4209e8a6730fb"
)
assert [len(ns[n]["widgets_values"][0]) for n in [17, 15, 79, 81, 83]] == [
106,
650,
362,
307,
307,
]
assert ns[81]["widgets_values"][0] == ns[83]["widgets_values"][0]
assert len(ns[30]["widgets_values"][-1]) == 483
assert (
ns[30]["widgets_values"][1] == 1.7100000000000004
and ns[30]["widgets_values"][26] == 0.04000000000000001
)
assert ns[31]["widgets_values"][18:] == ["", False, 0, "randomize"]
fields = {(f["node_id"], f["field"]): f for f in block["fields"]}
for j in range(18, 22):
f = fields["31", f"unmapped_saved_{j}"]
assert (
f["role"] == "unmapped_saved_candidate"
and "未知" in f["schema_default"]
and "未知" in f["unit"]
)
for key, count in [("portrait", 16), ("portraitzh", 41)]:
e = next(e for e in rows if e["id"] == key and e["file"] == "__init__.py")
tree = ast.parse((REF / e["local_file"]).read_bytes())
c = next(
c
for c in tree.body
if isinstance(c, ast.ClassDef)
and c.name
== ("PortraitMasterStylePose" if key == "portrait" else "PortraitMaster_中文版")
)
schema = extract_schema(c)
assert len([k for k, v in schema.items() if v["group"] == "required"]) == count
if key == "portrait":
assert not any(
k in schema
for k in ["background", "camera_angle", "camera_distance", "camera_lens"]
)
assert all(len(b["candidates"]) == 1 for b in block["node_bindings"])
(OUT / "source-check.json").write_text(
json.dumps(
dict(
files=checks,
positions=69,
standard_notes=1,
named_values_present=False,
scopes=inv["scopes"],
physical_edges=7,
orphan_output_declarations=[2],
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,69 positions/no named,1 Note,7 edges/orphan declaration2")
if __name__ == "__main__":
main()