307 lines
12 KiB
Python
307 lines
12 KiB
Python
"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"]) == 85 and len(inv["notes"]) == 4
|
||
assert [(v["nodes"], v["edges"], v["named_nodes"]) for v in inv["scopes"]] == [
|
||
(23, 21, 19),
|
||
(15, 54, 13),
|
||
]
|
||
assert all(set(v["modes"]) == {0} for v in inv["scopes"])
|
||
assert [(v["node"], v["state"]) for v in inv["empty"]] == [
|
||
(64, "missing"),
|
||
(24, "missing"),
|
||
(36, "missing"),
|
||
(66, "missing"),
|
||
(457, "missing"),
|
||
(480, "empty_list"),
|
||
(485, "missing"),
|
||
]
|
||
assert all(f["json_type"] not in ["list", "dict"] for f in inv["fields"])
|
||
from build_definition import PATHS, REPO
|
||
|
||
raw = (REPO / PATHS[0]).read_bytes()
|
||
g = json.loads(raw)
|
||
d = g["definitions"]["subgraphs"][0]
|
||
assert (
|
||
hashlib.sha256(raw).hexdigest()
|
||
== "248b6c5ae28d5a44035c5ad91bcdfa458360a67598eaa4d195c3fb6853b18a49"
|
||
)
|
||
|
||
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"])
|
||
== canonical(f["value"])
|
||
)
|
||
anomalies = []
|
||
consistent = 0
|
||
for prefix, graph in [("", g), ("/definitions/subgraphs/0", d)]:
|
||
ns = {n["id"]: n for n in graph["nodes"]}
|
||
for i, n in enumerate(graph["nodes"]):
|
||
if n["type"] in ["Note", "MarkdownNote"]:
|
||
continue
|
||
expected = {
|
||
f"{prefix}/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"{prefix}/nodes/{i}/")
|
||
}
|
||
for edge in graph["links"]:
|
||
eid, a, ao, z, zi = (
|
||
edge[:5]
|
||
if isinstance(edge, list)
|
||
else [
|
||
edge[k] for k in ["id", "origin_id", "origin_slot", "target_id", "target_slot"]
|
||
]
|
||
)
|
||
if a >= 0:
|
||
assert eid in ns[a]["outputs"][ao]["links"]
|
||
elif ao >= len(graph["inputs"]):
|
||
anomalies.append(
|
||
dict(
|
||
edge_id=eid,
|
||
origin_id=a,
|
||
origin_slot=ao,
|
||
input_count=len(graph["inputs"]),
|
||
target_id=z,
|
||
target_slot=zi,
|
||
scope=prefix,
|
||
conclusion="原记录越界悬空,不补边界或认证恢复",
|
||
)
|
||
)
|
||
else:
|
||
assert eid in graph["inputs"][ao]["linkIds"]
|
||
if z >= 0:
|
||
assert ns[z]["inputs"][zi]["link"] == eid
|
||
else:
|
||
assert eid in graph["outputs"][zi]["linkIds"]
|
||
if a >= 0 or ao < len(graph["inputs"]):
|
||
consistent += 1
|
||
assert [(x["edge_id"], x["origin_slot"], x["target_id"]) for x in anomalies] == [
|
||
(784, 24, 485),
|
||
(785, 25, 485),
|
||
(786, 26, 485),
|
||
(787, 27, 485),
|
||
]
|
||
assert consistent == 71
|
||
(OUT / "boundary-anomalies.json").write_text(
|
||
json.dumps(
|
||
dict(
|
||
original_records_unchanged=True,
|
||
consistent_edges=consistent,
|
||
anomalies=anomalies,
|
||
repair_or_execution=False,
|
||
),
|
||
ensure_ascii=False,
|
||
indent=2,
|
||
)
|
||
+ "\n"
|
||
)
|
||
ns = {n["id"]: n for n in g["nodes"]}
|
||
ins = {n["id"]: n for n in d["nodes"]}
|
||
assert (
|
||
len(ns[509]["widgets_values"]) == 18
|
||
and len(ns[509]["inputs"]) == len(d["inputs"]) == 24
|
||
and d["widgets"] == []
|
||
)
|
||
assert "proxyWidgets" not in ns[509] and "proxyWidgets" not in d
|
||
assert ns[63]["widgets_values"] == ["16"] and ns[26]["widgets_values"] == [14]
|
||
assert ns[16]["widgets_values"] == ["", "[filename]", "TextBatch", "index", 6]
|
||
assert ns[54]["widgets_values"] == ["5"]
|
||
assert ns[511]["widgets_values"] == ["9:16 (Portrait Widescreen)", 1.5, 8]
|
||
assert ns[510]["widgets_values"] == ["image/Qwen_image_2.1", "png", "8-bit", "sRGB"]
|
||
assert ns[7]["outputs"][1]["links"] in [None, []]
|
||
assert (
|
||
ins[500]["widgets_values_named"]["use_default_template"] is False
|
||
and ins[500]["widgets_values_named"]["thinking"] is True
|
||
)
|
||
assert (
|
||
ins[500]["widgets_values_named"]["sampling_mode"] == "on"
|
||
and type(ins[500]["widgets_values_named"]["sampling_mode.temperature"]) is int
|
||
)
|
||
text = ins[479]["widgets_values"][0]
|
||
assert (len(text), text.count("\n"), text.count("\\n"), text.endswith("\n")) == (
|
||
10494,
|
||
77,
|
||
0,
|
||
False,
|
||
)
|
||
assert (
|
||
hashlib.sha256(text.encode()).hexdigest()
|
||
== "eaff356cd2bdbcfd1aaf20502b5ccd8cbad16fa6e9afe41e3166eeee299c8e59"
|
||
)
|
||
assert [
|
||
(n["node"], n["characters"], n["lf_count"], n["literal_backslash_n"], n["ends_with_lf"])
|
||
for n in inv["notes"]
|
||
] == [
|
||
(55, 12, 0, 0, False),
|
||
(65, 575, 13, 0, False),
|
||
(69, 305, 15, 0, False),
|
||
(513, 42, 2, 0, False),
|
||
]
|
||
active = ns[512]["widgets_values"][0]
|
||
assert active == ns[69]["widgets_values"][0] and active != ns[65]["widgets_values"][0]
|
||
assert (
|
||
hashlib.sha256(active.encode()).hexdigest()
|
||
== "ba3264f2c3be5f7f3b635a8504ca2e650c85ce4fd0b4c80fc8be60d0be20c732"
|
||
)
|
||
bind = {n["node_id"]: n for n in block["node_bindings"]}
|
||
assert all(
|
||
len(n["candidates"]) == 1
|
||
for n in block["node_bindings"]
|
||
if n["node_id"] not in ["509", "45", "47", "66"]
|
||
)
|
||
assert all(bind[n]["source_id"] == "unknown" for n in ["45", "47", "66"])
|
||
assert bind["509"]["source_id"] == "subgraph-definition"
|
||
assert [bind[n]["source_id"] for n in ["26", "24", "64"]] == [
|
||
"easyloop",
|
||
"easybatch",
|
||
"easyend",
|
||
]
|
||
fields = {(f["node_id"], f["field"]): f for f in block["fields"]}
|
||
assert fields["509:514", "resolution"]["schema_default"] == "1024"
|
||
assert fields["509:469", "device"]["schema_default"] == "'auto'"
|
||
assert fields["26", "total"]["schema_default"] == "1"
|
||
(OUT / "source-check.json").write_text(
|
||
json.dumps(
|
||
dict(
|
||
files=checks,
|
||
positions=85,
|
||
standard_notes=4,
|
||
scopes=inv["scopes"],
|
||
source_edge_records=75,
|
||
consistent_edges=71,
|
||
unresolved_boundary_records=4,
|
||
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,85 dual values,4 notes,"
|
||
"71 consistent edges+4 preserved out-of-bounds records"
|
||
)
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main()
|