Files
aigc-home/docs/hel-892/flux2-restore-document-evidence/verify_sources.py
T

192 lines
8.4 KiB
Python

"Recheck selected bytes, real Git identities, citations and every graph endpoint."
import hashlib
import json
import zlib
from build_definition import OUT, PATHS, REF, REPO, build, canonical
from inventory import inventory
def main():
block = build()
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"]
root = REF / e["identity_root"]
key = e["identity_key"]
if key == "rgthree":
members = json.loads((root / "rgthree-members.json").read_bytes())
info = next(x for x in members if x["name"] == e["file"])
assert zlib.crc32(raw) == info["crc"] == e["member_crc32"]
assert json.loads((root / "rgthree-registry.json").read_bytes())["version"] == e["tag"]
assert e["git_commit_binding"] == "unknown" and e["package_sha256"].startswith(
"unknown"
)
proof = "exact registry version + selected ZIP member CRC/SHA; full package/Git unknown"
else:
blob = hashlib.sha1(b"blob " + str(len(raw)).encode() + b"\0" + raw).hexdigest()
assert blob == e["git_blob_sha1"]
tree = json.loads((root / (key + "-tree.json")).read_bytes())
assert not tree["truncated"]
assert next(t["sha"] for t in tree["tree"] if t["path"] == e["file"]) == blob
if key == "layer":
c = json.loads((REF / "layer-commit-meta.json").read_bytes())
assert c["sha"] == e["commit"] and c["commit"]["tree"]["sha"] == e["tree"]
assert tree["sha"] == e["tree_index_ref"] == e["commit"]
proof = (
"pinned GitHub commit metadata + selected blob"
"; tree index ref is NOT tree object; no full "
"tree rehash"
)
else:
c = (root / (key + "-commit.txt")).read_bytes()
assert (
hashlib.sha1(b"commit " + str(len(c)).encode() + b"\0" + c).hexdigest()
== e["commit"]
)
assert (
c.decode().splitlines()[0] == "tree " + e["tree"] and tree["sha"] == e["tree"]
)
tag = json.loads((root / (key + "-tag.json")).read_bytes())
assert tag["commit"] == e["commit"]
t = (root / (key + "-tag.txt")).read_bytes()
if tag["object"] != e["commit"]:
assert (
hashlib.sha1(b"tag " + str(len(t)).encode() + b"\0" + t).hexdigest()
== tag["object"]
)
assert t.decode().startswith("object " + e["commit"] + "\n")
else:
assert t == c
proof = (
"raw commit/tag hash + real tree ID + selected blob/index; no full tree rehash"
)
lic = next(x for x in rows if x["id"] == e["id"] and x["file"] == "LICENSE")
assert hashlib.sha256((REF / e["license_file"]).read_bytes()).hexdigest() == lic["sha256"]
checks.append(
dict(
id=e["id"],
file=e["file"],
sha256=e["sha256"],
commit=e["commit"],
tree=e["tree"],
proof=proof,
license_file=e["license_file"],
)
)
inv = inventory()
assert [s["nodes"] for s in inv["scopes"]] == [9, 18]
assert [s["edges"] for s in inv["scopes"]] == [9, 32]
assert all(s["named_nodes"] == 0 and set(s["modes"]) == {0} for s in inv["scopes"])
assert len(inv["fields"]) == 34 and sum(f["scope"] == "root" for f in inv["fields"]) == 14
assert sum(f["json_type"] == "list" for f in inv["fields"]) == 1
assert len(inv["notes"]) == 1 and inv["notes"][0]["characters"] == 1396
assert {x["node"]: x["state"] for x in inv["empty"]} == {
143: "missing",
151: "missing",
150: "empty_list",
141: "empty_list",
140: "empty_list",
139: "empty_list",
103: "empty_list",
104: "empty_list",
136: "missing",
135: "missing",
}
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"]),
)
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())
g = json.loads((REPO / PATHS[0]).read_bytes())
sub = g["definitions"]["subgraphs"][0]
endpoints = []
for scope, graph in [("root", g), ("definition", sub)]:
ns = {n["id"]: n for n in graph["nodes"]}
ids = []
for original in graph["links"]:
if scope == "root":
eid, a, ao, z, zi, kind = original
else:
eid, a, ao, z, zi, kind = [
original[k]
for k in ["id", "origin_id", "origin_slot", "target_id", "target_slot", "type"]
]
ids.append(eid)
source = graph["inputs"][ao] if a == -10 else ns[a]["outputs"][ao]
target = graph["outputs"][zi] if z == -20 else ns[z]["inputs"][zi]
assert eid in source.get("linkIds", source.get("links", []))
assert eid in target["linkIds"] if z == -20 else target["link"] == eid
assert source["type"] in [kind, "*"] and target["type"] in [kind, "*"]
endpoints.append(
dict(
scope=scope,
edge=original,
source_backref=True,
target_backref=True,
source_type=source["type"],
target_type=target["type"],
)
)
assert len(ids) == len(set(ids))
# Reverse verification retains duplicate boundary output declarations.
for n in graph["nodes"]:
for slot in n.get("inputs", []):
assert slot.get("link") is None or slot["link"] in ids
for slot in n.get("outputs", []):
assert all(link in ids for link in slot.get("links") or [])
if scope == "definition":
for boundary in graph["inputs"] + graph["outputs"]:
assert all(link in ids for link in boundary["linkIds"])
ns = {n["id"]: n for n in g["nodes"]}
inner = {n["id"]: n for n in sub["nodes"]}
assert len(ns[150]["properties"]["proxyWidgets"]) == 10 and len(ns[150]["inputs"]) == 9
for i, (outer, boundary) in enumerate(zip(ns[150]["inputs"], sub["inputs"], strict=True)):
assert (outer["name"], outer["type"]) == (boundary["name"], boundary["type"])
assert [x["name"] for x in sub["inputs"]][-2:] == ["", "_1"]
assert [x["type"] for x in sub["inputs"]][-2:] == ["*", "*"]
assert ns[150]["widgets_values"] == sub["widgets"] == []
assert sub["outputs"][0]["linkIds"] == [201, 201, 201]
assert sum(e["id"] == 201 for e in sub["links"]) == 1
assert inner[103]["outputs"][1]["links"] == []
assert ns[9]["outputs"][0]["links"] is None and ns[145]["outputs"][0]["links"] is None
assert ns[152]["widgets_values"] == inner[108]["widgets_values"]
assert ns[150]["inputs"][6]["link"] is None
assert g["extra"]["frontendVersion"] == "1.36.13"
result = dict(
all_pass=True,
source_files=len(rows),
citations=len(block["sources"]),
identities=checks,
counts=block["counts"],
physical_endpoint_checks=endpoints,
duplicate_201_declarations_preserved=[201, 201, 201],
physical_201_records=1,
protocol_conflicts_retained=[
"SaveImage source IMAGE vs core0.8.2 empty RETURN_TYPES",
"Comparer source IMAGE vs inherited Preview UI-only response",
],
upstream_executed=False,
prompt_constructed=False,
)
(OUT / "source-check.json").write_text(json.dumps(result, ensure_ascii=False, indent=2) + "\n")
print(
"PASS",
len(rows),
"texts;",
len(block["sources"]),
"citations;34 positions/41 physical edges",
)
if __name__ == "__main__":
main()