284 lines
10 KiB
Python
284 lines
10 KiB
Python
"""SeedVR2 scope/boundary, dual storage, exact source and editorial trust checks."""
|
|
|
|
import copy
|
|
import hashlib
|
|
import json
|
|
import subprocess
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
from calliope.comfyui.fixed_document import DEFINITIONS, canonical
|
|
from calliope.comfyui.library import build_document
|
|
from calliope.comfyui.source_reviews import review_for
|
|
|
|
REPO = Path(__file__).resolve().parents[2]
|
|
OUT = REPO / "docs/hel-892/seedvr2-document-evidence"
|
|
PATH = next(p for p in DEFINITIONS if p.endswith("/Seedvr2_3b_int8_图像放大.json"))
|
|
SOURCE = json.loads((REPO / PATH).read_bytes())
|
|
REVIEW = review_for(PATH, hashlib.sha1((REPO / PATH).read_bytes()).hexdigest())
|
|
BLOCK = REVIEW["fixed_version_document"]
|
|
SUB = SOURCE["definitions"]["subgraphs"][0]
|
|
SCOPES = [("", SOURCE), ("/definitions/subgraphs/0", SUB)]
|
|
NODES = [(f"{prefix}/nodes/{i}", n) for prefix, g in SCOPES for i, n in enumerate(g["nodes"])]
|
|
EDGES = [(f"{prefix}/links/{i}", e) for prefix, g in SCOPES for i, e in enumerate(g["links"])]
|
|
|
|
|
|
def pointer(obj, path):
|
|
for k in path.strip("/").split("/"):
|
|
obj = obj[int(k)] if isinstance(obj, list) else obj[k.replace("~1", "/").replace("~0", "~")]
|
|
return obj
|
|
|
|
|
|
def doc(source=SOURCE, review=REVIEW):
|
|
return build_document(PATH, source, None, None, [], review)
|
|
|
|
|
|
def refuse(source, review):
|
|
b = review["fixed_version_document"]
|
|
b["canonical_sha256"] = hashlib.sha256(canonical(source)).hexdigest()
|
|
review["caller_digest"] = hashlib.sha256(canonical(b)).hexdigest()
|
|
assert not doc(source, review)["fixed_version"]["matched"]
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"f", BLOCK["fields"], ids=lambda f: f["scope"] + ":" + f["node_id"] + "." + f["field"]
|
|
)
|
|
def test_each_saved_position_dual_storage_type_scope_and_exact_source(f):
|
|
v = pointer(SOURCE, f["pointer"])
|
|
named = pointer(SOURCE, f["named_pointer"])
|
|
assert canonical(v) == canonical(f["value"]) and type(v).__name__ == f["json_type"]
|
|
assert named == ({"images": v} if f["field"] == "rgthree_comparer" else v)
|
|
for k in [
|
|
"effect",
|
|
"limits",
|
|
"schema_default",
|
|
"unit",
|
|
"execution_note",
|
|
"role",
|
|
"label",
|
|
"type",
|
|
]:
|
|
assert f[k]
|
|
assert f["named_pointer"] in f["execution_note"]
|
|
for k in f["evidence"]:
|
|
e = BLOCK["sources"][k]
|
|
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())
|
|
and len(e["commit"]) == 40
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize("f", BLOCK["fields"])
|
|
@pytest.mark.parametrize("variant", ["saved", "named", "text"])
|
|
def test_every_field_forgery_rejected_even_recomputed_caller_digest(f, variant):
|
|
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
|
|
if variant == "text":
|
|
r["fixed_version_document"]["fields"][BLOCK["fields"].index(f)]["limits"] = "forged"
|
|
else:
|
|
p = f["pointer"] if variant == "saved" else f["named_pointer"]
|
|
base, _, k = p.rpartition("/")
|
|
parent = pointer(s, base)
|
|
parent[int(k) if isinstance(parent, list) else k] = None
|
|
refuse(s, r)
|
|
|
|
|
|
@pytest.mark.parametrize("pair", NODES, ids=lambda pair: pair[0])
|
|
@pytest.mark.parametrize("variant", ["mode", "identity"])
|
|
def test_all_root_and_definition_nodes_bound(pair, variant):
|
|
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
|
|
n = pointer(s, pair[0])
|
|
if variant == "mode":
|
|
n["mode"] = 4
|
|
else:
|
|
n["properties"]["ver"] = "forged"
|
|
refuse(s, r)
|
|
|
|
|
|
@pytest.mark.parametrize("pair", EDGES, ids=lambda pair: pair[0])
|
|
def test_every_physical_and_boundary_edge_record_bound(pair):
|
|
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
|
|
e = pointer(s, pair[0])
|
|
if isinstance(e, list):
|
|
e[3] = 99999
|
|
else:
|
|
e["target_id"] = 99999
|
|
refuse(s, r)
|
|
|
|
|
|
@pytest.mark.parametrize("b", BLOCK["boundaries"])
|
|
@pytest.mark.parametrize("variant", ["host", "definition", "target"])
|
|
def test_all_13_host_child_boundary_endpoints_bound(b, variant):
|
|
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
|
|
p = b[variant + "_pointer"]
|
|
port = pointer(s, p)
|
|
if variant == "definition":
|
|
port["linkIds"] = []
|
|
elif variant == "host":
|
|
port["name"] = "forged"
|
|
else:
|
|
port["link"] = 99999
|
|
refuse(s, r)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"variant",
|
|
[
|
|
"note",
|
|
"named_note",
|
|
"missing_to_empty",
|
|
"comparer_empty_to_object",
|
|
"save_output",
|
|
"seed_control",
|
|
"unet_case",
|
|
"vae_name",
|
|
"preview",
|
|
"guhai",
|
|
"ue",
|
|
"frontend",
|
|
"citation",
|
|
"source_sha",
|
|
"path",
|
|
"overview",
|
|
"layout",
|
|
"proxy",
|
|
],
|
|
)
|
|
def test_special_shapes_missing_identity_note_and_binding_fail_closed(variant):
|
|
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
|
|
ns = {n["id"]: n for n in s["nodes"]}
|
|
sub = s["definitions"]["subgraphs"][0]
|
|
inner = {n["id"]: n for n in sub["nodes"]}
|
|
block = r["fixed_version_document"]
|
|
if variant == "note":
|
|
ns[72]["widgets_values"][0] = "forged"
|
|
elif variant == "named_note":
|
|
ns[72]["widgets_values_named"]["text"] = "forged"
|
|
elif variant == "missing_to_empty":
|
|
inner[58]["widgets_values"] = []
|
|
elif variant == "comparer_empty_to_object":
|
|
ns[73]["widgets_values"] = [{}]
|
|
elif variant == "save_output":
|
|
ns[9]["outputs"] = []
|
|
elif variant == "seed_control":
|
|
ns[66]["widgets_values"].insert(2, "fixed")
|
|
elif variant == "unet_case":
|
|
ns[66]["widgets_values"][7] = inner[52]["widgets_values"][0]
|
|
elif variant == "vae_name":
|
|
ns[66]["widgets_values"][8] = inner[51]["widgets_values"][0]
|
|
elif variant == "preview":
|
|
ns[66]["properties"]["previewExposures"][0]["sourceNodeId"] = "9"
|
|
elif variant == "guhai":
|
|
ns[1]["properties"]["guhaiImageValue"] = "forged"
|
|
elif variant == "ue":
|
|
ns[66]["properties"]["ue_properties"]["version"] = "7.7"
|
|
elif variant == "frontend":
|
|
s["extra"]["frontendVersion"] = "1.39.19"
|
|
elif variant == "citation":
|
|
block["sources"][0]["commit"] = "0" * 40
|
|
elif variant == "source_sha":
|
|
block["source_sha1"] = "0" * 40
|
|
elif variant == "path":
|
|
block["source_path"] = "forged"
|
|
elif variant == "overview":
|
|
block["user_overview"] = "forged"
|
|
elif variant == "layout":
|
|
ns[66]["pos"][0] += 1
|
|
elif variant == "proxy":
|
|
ns[66]["properties"]["proxyWidgets"] = [["54", "seed"]]
|
|
refuse(s, r)
|
|
|
|
|
|
def test_independent_inventory_and_semantics():
|
|
assert len(SOURCE["nodes"]) == 5 and len(SOURCE["links"]) == 5
|
|
assert len(SUB["nodes"]) == 10 and len(SUB["links"]) == 28
|
|
assert all(n["mode"] == 0 for _, n in NODES)
|
|
actual = {
|
|
f"{prefix}/nodes/{i}/widgets_values/{k}"
|
|
for prefix, g in SCOPES
|
|
for i, n in enumerate(g["nodes"])
|
|
if n["type"] != "MarkdownNote"
|
|
for k in range(len(n.get("widgets_values", [])))
|
|
}
|
|
assert actual == {f["pointer"] for f in BLOCK["fields"]} and len(actual) == 37
|
|
assert len([f for f in BLOCK["fields"] if f["scope"] == "root"]) == 15
|
|
assert sum(isinstance(f["value"], list) for f in BLOCK["fields"]) == 1
|
|
assert [n["id"] for _, n in NODES if "widgets_values" not in n] == [50, 58, 61]
|
|
assert len(BLOCK["boundaries"]) == 13 and SUB["widgets"] == []
|
|
for b in BLOCK["boundaries"]:
|
|
assert (
|
|
pointer(SOURCE, b["host_pointer"])["name"]
|
|
== pointer(SOURCE, b["definition_pointer"])["name"]
|
|
== b["name"]
|
|
)
|
|
assert pointer(SOURCE, b["target_pointer"])["link"] == b["edge"]["id"]
|
|
for prefix, g in SCOPES:
|
|
nodes = {n["id"]: n for n in g["nodes"]}
|
|
for e in g["links"]:
|
|
edge, src, slot, dst, inp, _ = (
|
|
e
|
|
if isinstance(e, list)
|
|
else (
|
|
e["id"],
|
|
e["origin_id"],
|
|
e["origin_slot"],
|
|
e["target_id"],
|
|
e["target_slot"],
|
|
e["type"],
|
|
)
|
|
)
|
|
if src >= 0:
|
|
assert edge in nodes[src]["outputs"][slot]["links"]
|
|
else:
|
|
assert edge in g["inputs"][slot]["linkIds"]
|
|
if dst >= 0:
|
|
assert nodes[dst]["inputs"][inp]["link"] == edge
|
|
else:
|
|
assert edge in g["outputs"][inp]["linkIds"]
|
|
assert doc()["fixed_version"]["matched"] and len(doc()["source_notes"]) == 1
|
|
assert BLOCK["comments"][0]["text"] == SOURCE["nodes"][0]["widgets_values"][0]
|
|
assert doc()["dependencies"]["check_state"] == "unchecked" and doc()["status"] == dict(
|
|
imported=True, doc_done=False, exec_verified=False
|
|
)
|
|
text = " ".join(BLOCK["flow"])
|
|
for word in [
|
|
"13宿主输入",
|
|
"SaveImage有IMAGE出口",
|
|
"无显式result",
|
|
"不借新版本",
|
|
"seedvr2_ema_vae_fp16",
|
|
"fixed",
|
|
"guhaiImageValue",
|
|
"previewExposures",
|
|
"4096",
|
|
"未实跑",
|
|
]:
|
|
assert word in text
|
|
method = next(f for f in BLOCK["fields"] if f["field"] == "scale_method")
|
|
assert method["value"] == "lanczos" and method["schema_default"] == "area"
|
|
assert review_for(PATH, "0" * 40) is None
|
|
|
|
|
|
def test_old_thirty_review_objects_exactly_preserved():
|
|
resource = "calliope-backend/src/calliope/comfyui/source_reviews.json"
|
|
old = json.loads(subprocess.check_output(["git", "show", "1d04c00:" + resource], cwd=REPO))
|
|
now = json.loads((REPO / resource).read_bytes())
|
|
now[PATH].pop("fixed_version_document")
|
|
assert now == old and len(DEFINITIONS) == 31
|
|
blocks = [r["fixed_version_document"] for r in old.values() if "fixed_version_document" in r]
|
|
assert (
|
|
len(blocks) == 30
|
|
and sum(len(b["fields"]) for b in blocks) == 904
|
|
and sum(
|
|
len(
|
|
build_document(p, json.loads((REPO / p).read_bytes()), None, None, [], r)[
|
|
"source_notes"
|
|
]
|
|
)
|
|
for p, r in old.items()
|
|
if "fixed_version_document" in r
|
|
)
|
|
== 21
|
|
)
|