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

126 lines
5.4 KiB
Python

"""Verify exact text identities, all physical endpoints and every saved position."""
import hashlib
import json
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"]
blob = hashlib.sha1(b"blob " + str(len(raw)).encode() + b"\0" + raw).hexdigest()
assert blob == e["git_blob_sha1"]
root = REF / e["identity_root"]
key = e["identity_key"]
tree = json.loads((root / (key + "-tree.json")).read_bytes())
assert tree["sha"] == e["tree"] and not tree["truncated"]
assert next(t["sha"] for t in tree["tree"] if t["path"] == e["file"]) == blob
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"]
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
lic = next(x for x in rows if x["id"] == e["id"] and x["local_file"] == e["license_file"])
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="raw commit/tag + tree identity + selected blob; full tree not rehashed",
license_file=e["license_file"],
)
)
inv = inventory()
assert [s["nodes"] for s in inv["scopes"]] == [11, 6]
assert [s["edges"] for s in inv["scopes"]] == [14, 17]
assert len(inv["fields"]) == 43 and not inv["notes"]
assert all(s["named_nodes"] == 0 and set(s["modes"]) == {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"]),
)
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())
endpoints = []
for scope, graph in [("root", g)] + [
(str(i), s) for i, s in enumerate(g["definitions"]["subgraphs"])
]:
ns = {n["id"]: n for n in graph["nodes"]}
ids = []
for original in graph["links"]:
eid, a, ao, z, zi, kind = (
original
if scope == "root"
else [
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"] == "*" or kind in source["type"].split(",")
assert target["type"] == "*" or kind in target["type"].split(",")
endpoints.append(
dict(scope=scope, edge=original, source_backref=True, target_backref=True)
)
assert len(ids) == len(set(ids))
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(x in ids for x in slot.get("links") or [])
if scope != "root":
for b in graph["inputs"] + graph["outputs"]:
assert all(x in ids for x in b["linkIds"])
h = next(n for n in g["nodes"] if n["id"] == 23)
sub = g["definitions"]["subgraphs"][0]
assert len(h["properties"]["proxyWidgets"]) == 10 and len(h["inputs"]) == 8
assert h["widgets_values"] == sub["widgets"] == []
assert sub["outputs"][0]["linkIds"] == [17, 17]
assert sum(e["id"] == 17 for e in sub["links"]) == 1
for outer, boundary in zip(h["inputs"], sub["inputs"], strict=True):
assert (outer["name"], outer["type"]) == (boundary["name"], boundary["type"])
(OUT / "source-check.json").write_text(
json.dumps(
dict(
sources=checks, endpoints=endpoints, inventory_matched=True, upstream_executed=False
),
ensure_ascii=False,
indent=2,
)
+ "\n"
)
print("PASS exact sources, 31 bidirectional physical edges, 43 positions, one definition")
if __name__ == "__main__":
main()