Files
aigc-home/docs/hel-892/animate2-loop-document-evidence/verify_sources.py
T

349 lines
14 KiB
Python

"""Independent graph/value/date/identity/proxy audit; AST/text only, never upstream imports."""
import ast
import collections
import hashlib
import json
import re
import zlib
from datetime import datetime
from build_definition import OUT, PATHS, REF, REPO, build, canonical
from inventory import inventory
from source_history import validate_sources
def sha(raw):
return hashlib.sha256(raw).hexdigest()
def write(name, value):
(OUT / name).write_text(json.dumps(value, ensure_ascii=False, indent=2) + "\n")
def moment(value):
if value is None or value.startswith(("未知", "unknown")):
return None
result = datetime.fromisoformat(value.replace("Z", "+00:00"))
assert result.utcoffset() is not None, value
return result
def main():
raw = (REPO / PATHS[0]).read_bytes()
assert sha(raw) == "22b7853e22629822dd354b0b768e2961a53b4c2f480cfaceeaea9dc51c0f0d0b"
g = json.loads(raw)
b, inv = build(), inventory()
assert len(g["nodes"]) == 35 and len(g["links"]) == 41
assert collections.Counter(n["mode"] for n in g["nodes"]) == {0: 33, 4: 2}
assert {n["id"] for n in g["nodes"] if n["mode"] == 4} == {291, 292}
assert [len(s["nodes"]) for s in g["definitions"]["subgraphs"]] == [28, 13, 28, 5]
assert [len(s["links"]) for s in g["definitions"]["subgraphs"]] == [69, 26, 69, 16]
assert len(inv["fields"]) == len(b["fields"]) == 230
assert collections.Counter(f["json_type"] for f in inv["fields"]) == {
"str": 110,
"int": 96,
"bool": 23,
"dict": 1,
}
assert all(s["named_nodes"] == 0 for s in inv["scopes"])
for old, current in zip(inv["fields"], b["fields"], strict=True):
assert old["pointer"] == current["pointer"]
assert canonical(old["value"]) == canonical(current["value"])
assert old["json_type"] == current["json_type"] == type(current["value"]).__name__
assert current["named_state"] == "missing"
assert "named_pointer" not in current and "named_value" not in current
expected_notes = {
574: (3397, 49, False),
575: (2606, 57, True),
539: (311, 0, False),
587: (30, 2, False),
540: (196, 10, False),
}
notes = []
for n in inv["notes"]:
text = n["text"]
assert (len(text), text.count("\n"), text.endswith("\n")) == expected_notes[n["node"]]
assert "\r" not in text
assert text == next(x["text"] for x in b["comments"] if x["node_id"] == str(n["node"]))
notes.append(
dict(
node=str(n["node"]),
characters=len(text),
LF=text.count("\n"),
CR=0,
tail_LF=text.endswith("\n"),
sha256=sha(text.encode()),
)
)
rows = json.loads((REF / "sources.json").read_bytes())["sources"]
validate_sources(rows)
times, identities = [], []
trees = {}
for i, e in enumerate(rows):
data = (REF / e["local_file"]).read_bytes()
assert sha(data) == e["sha256"]
acquired, verified = moment(e.get("acquisition_time")), moment(e.get("reverified_at"))
assert verified is not None
current_fetch = moment(e.get("current_fetch_at"))
if current_fetch is not None:
assert current_fetch <= verified, (i, e["file"], "current fetch after verification")
if acquired is not None:
assert acquired <= verified, (i, e["file"])
times.append(
dict(
index=i,
first=e.get("acquisition_time", "未知:原记录缺首次取得时间"),
reverified=e["reverified_at"],
current_fetch=e.get("current_fetch_at", "未知:未重取或无记录"),
comparable=acquired is not None,
)
)
blob = e.get("git_blob_sha1", "")
if re.fullmatch("[0-9a-f]{40}", blob):
assert (
hashlib.sha1(b"blob " + str(len(data)).encode() + b"\0" + data).hexdigest() == blob
)
root = (REF / e["identity_root"]).resolve()
key = e.get("identity_key", e["id"])
tree = json.loads((root / (key + "-tree.json")).read_bytes())
assert tree["sha"] == e["tree"]
assert next(x["sha"] for x in tree["tree"] if x["path"] == e["file"]) == blob
cb = (root / (key + "-commit.txt")).read_bytes()
assert (
hashlib.sha1(b"commit " + str(len(cb)).encode() + b"\0" + cb).hexdigest()
== e["commit"]
)
assert cb.splitlines()[0] == ("tree " + e["tree"]).encode()
if "member_crc32" in e:
assert zlib.crc32(data) & 0xFFFFFFFF == e["member_crc32"]
members = json.loads((REF / (e["id"] + "-members.json")).read_bytes())
member = next(x for x in members if x["name"] == e["file"])
assert member["crc"] == e["member_crc32"] and member["size"] == len(data)
assert e["package_sha256"] == "unknown-not-downloaded-whole-archive"
if not e["license_file"].startswith("未知"):
assert (REF / e["license_file"]).is_file()
if e["file"].endswith(".py"):
trees[i] = ast.parse(data)
identities.append(
dict(
index=i,
file=e["file"],
sha256=e["sha256"],
git_blob=blob,
commit=e["commit"],
tree=e["tree"],
member_crc=e.get("member_crc32"),
)
)
accurate = [n for n in b["node_bindings"] if n["implementation_verified"]]
gaps = [n for n in b["node_bindings"] if n["source_id"].startswith("unresolved-")]
unknown = [n for n in b["node_bindings"] if n["source_id"] == "unknown-no-version"]
assert (len(accurate), len(gaps), len(unknown), len(b["boundaries"])) == (85, 2, 13, 4)
assert {n["node_id"] for n in gaps} == {"585", "586"}
registrations = []
for n in accurate:
candidate = n["candidates"][0]
tree = trees[candidate["source"]]
cls = next(
x for x in tree.body if isinstance(x, ast.ClassDef) and x.name == candidate["symbol"]
)
assert [cls.lineno, cls.end_lineno] == candidate["lines"]
if any(isinstance(m, ast.FunctionDef) and m.name == "define_schema" for m in cls.body):
exported = {
z.id
for f in ast.walk(tree)
if isinstance(f, ast.AsyncFunctionDef) and f.name == "get_node_list"
for z in ast.walk(f)
if isinstance(z, ast.Name)
}
assert cls.name in exported, (n["node_id"], cls.name)
registrations.append(
dict(
node=n["node_id"],
type=n["class_type"],
source=candidate["source"],
symbol=cls.name,
lines=candidate["lines"],
)
)
frontend = [e for e in rows if e["id"] == "frontend"]
assert g["extra"]["frontendVersion"] == "1.48.6"
package = next(e for e in frontend if e["file"] == "package.json")
pkg = json.loads((REF / package["local_file"]).read_bytes())
assert pkg["version"] == "1.48.6"
assert "@comfyorg/litegraph" not in pkg.get("dependencies", {})
assert any(e["file"] == "pnpm-lock.yaml" for e in frontend)
assert any(e["file"] == "src/lib/litegraph/LICENSE" for e in frontend)
assert len({e["commit"] for e in frontend}) == len({e["tree"] for e in frontend}) == 1
frontend_proof = dict(
declared="1.48.6",
exact_same_tree_lock=True,
litegraph="bundled exact frontend Git tree; no external0.15.15 claim",
members=[dict(file=e["file"], sha256=e["sha256"]) for e in frontend],
target_restore_verified=False,
)
assert [len(h["proxy_mappings"]) for h in b["boundaries"]] == [0, 0, 0, 0]
for s, host in zip(g["definitions"]["subgraphs"], b["boundaries"], strict=True):
assert host["widgets"] == s["widgets"] == []
assert "proxyWidgets" not in host["host"]["properties"]
hosts = {h["host_node_id"]: h["host"] for h in b["boundaries"]}
assert [len(hosts[n]["widgets_values"]) for n in ["261", "291", "477"]] == [20, 4, 20]
assert "widgets_values" not in hosts["606"]
scopes = [("root", g)] + [
("宿主" + h["host_node_id"] + "内部定义", s)
for h, s in zip(b["boundaries"], g["definitions"]["subgraphs"], strict=True)
]
scoped = [dict(scope=scope, edge=e) for scope, graph in scopes for e in graph["links"]]
assert len(scoped) == 221
duplicate_declarations = []
boundary_inconsistencies = []
inconsistencies = []
for scope, graph in scopes:
edges = {e[0] if isinstance(e, list) else e["id"]: e for e in graph["links"]}
for direction in ["inputs", "outputs"]:
for slot, port in enumerate(graph.get(direction, [])):
counts = collections.Counter(port.get("linkIds", []))
boundary_id = graph["inputNode" if direction == "inputs" else "outputNode"]["id"]
endpoint = "origin" if direction == "inputs" else "target"
actual_ids = [
e["id"]
for e in graph["links"]
if e[endpoint + "_id"] == boundary_id and e[endpoint + "_slot"] == slot
]
if counts != collections.Counter(actual_ids):
boundary_inconsistencies.append(
dict(
scope=scope,
direction=direction,
slot=slot,
declared_linkIds=port.get("linkIds", []),
graph_records=actual_ids,
)
)
for eid, count in counts.items():
if count > 1:
duplicate_declarations.append(
dict(scope=scope, direction=direction, slot=slot, id=eid, count=count)
)
for n in graph["nodes"]:
for direction in ["inputs", "outputs"]:
for slot, port in enumerate(n.get(direction, [])):
declared = (
[port["link"]]
if direction == "inputs" and port.get("link") is not None
else port.get("links", []) or []
)
for eid in declared:
edge = edges.get(eid)
if isinstance(edge, dict):
actual = (
(edge["target_id"], edge["target_slot"])
if direction == "inputs"
else (edge["origin_id"], edge["origin_slot"])
)
elif edge:
actual = (
(edge[3], edge[4]) if direction == "inputs" else (edge[1], edge[2])
)
else:
actual = None
if actual != (n["id"], slot):
inconsistencies.append(
dict(
scope=scope,
node=n["id"],
direction=direction,
slot=slot,
id=eid,
edge=edge,
)
)
nodes = {n["id"]: n for n in g["nodes"]}
assert nodes[586]["widgets_values"] == {}
assert nodes[477]["widgets_values"][3].endswith("\n")
assert (
nodes[261]["widgets_values"][13] == 807087399737133
and nodes[477]["widgets_values"][13] == 2
)
root_edges = {e[0]: e for e in g["links"]}
assert root_edges[937][1:5] == [606, 0, 477, 2]
assert root_edges[938][1:3] == [606, 1] and root_edges[938][3] == 477
assert root_edges[945][1:3] == [606, 2] and root_edges[945][3] == 608
histories = []
for nid in [585]:
v = nodes[nid]["widgets_values"]["videopreview"]
ordered = json.dumps(v, ensure_ascii=False, separators=(",", ":"))
histories.append(
dict(
node=str(nid),
original_order_json=ordered,
original_order_sha256=sha(ordered.encode()),
sorted_canonical_sha256=sha(canonical(v)),
)
)
texts = [
dict(
node=f["node_id"],
field=f["field"],
pointer=f["pointer"],
characters=len(f["value"]),
LF=f["value"].count("\n"),
CR=f["value"].count("\r"),
tail_LF=f["value"].endswith("\n"),
sha256=sha(f["value"].encode()),
)
for f in b["fields"]
if isinstance(f["value"], str) and len(f["value"]) > 100
]
write("independent-inventory.json", inv)
write(
"source-check.json",
dict(
original_sha256=sha(raw),
saved_fields=230,
notes=notes,
texts=texts,
accurate_instances=85,
unresolved_instances=2,
unknown_instances=13,
),
)
write(
"exact-source-check.json",
dict(
accurate=accurate,
unresolved=gaps,
unknown=unknown,
identities=identities,
sources=len(rows),
upstream_executed=False,
),
)
write("registration-check.json", registrations)
write("source-time-check.json", times)
write("frontend-lock-check.json", frontend_proof)
write("identity-gaps.json", dict(unresolved=gaps, unknown=unknown, boundary_hosts=4))
write("proxy-check.json", dict(proxy_widgets=0, hosts=b["boundaries"], frontend=frontend_proof))
write("expected-edges.json", {scope: graph["links"] for scope, graph in scopes})
write(
"scoped-edge-check.json",
dict(
records=221,
endpoints=scoped,
duplicate_boundary_declarations=duplicate_declarations,
boundary_port_inconsistencies=boundary_inconsistencies,
node_port_inconsistencies=inconsistencies,
repaired=False,
),
)
write("history-order-evidence.json", histories)
print(
"PASS230 typed saved/5 Notes/109 nodes/221 scoped records/no proxy;"
"85 exact/2 specific gaps/13 unknown;real source dates"
)
if __name__ == "__main__":
main()