279 lines
10 KiB
Python
279 lines
10 KiB
Python
"MoGe3 independent scopes, accurate interface conflicts and fail-closed trust."
|
|
|
|
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/moge3-document-evidence"
|
|
PATH = next(p for p in DEFINITIONS if p.endswith("/moge3_深度图_几何图_线稿图.json"))
|
|
SOURCE = json.loads((REPO / PATH).read_bytes())
|
|
REVIEW = review_for(PATH, hashlib.sha1((REPO / PATH).read_bytes()).hexdigest())
|
|
BLOCK = REVIEW["fixed_version_document"]
|
|
|
|
|
|
def pointer(obj, path):
|
|
for part in path.strip("/").split("/"):
|
|
obj = obj[int(part)] if isinstance(obj, list) else obj[part]
|
|
return obj
|
|
|
|
|
|
def doc(s=SOURCE, r=REVIEW):
|
|
return build_document(PATH, s, None, None, [], r)
|
|
|
|
|
|
def refuse(s, r):
|
|
r["fixed_version_document"]["canonical_sha256"] = hashlib.sha256(canonical(s)).hexdigest()
|
|
assert not doc(s, r)["fixed_version"]["matched"]
|
|
|
|
|
|
@pytest.mark.parametrize("f", BLOCK["fields"], ids=lambda f: f["node_id"] + "." + f["field"])
|
|
def test_all_saved_positions_dual_pointers_types_and_local_evidence(f):
|
|
assert canonical(pointer(SOURCE, f["pointer"])) == canonical(f["value"])
|
|
assert type(pointer(SOURCE, f["pointer"])).__name__ == f["json_type"]
|
|
assert f["scope"] == (
|
|
"root"
|
|
if ":" not in f["node_id"]
|
|
else next(
|
|
s["id"]
|
|
for s in SOURCE["definitions"]["subgraphs"]
|
|
if any(str(n["id"]) == f["definition_node_id"] for n in s["nodes"])
|
|
)
|
|
)
|
|
assert canonical(pointer(SOURCE, f["named_pointer"])) == canonical(f["named_value"])
|
|
if f["input_pointer"].startswith("/"):
|
|
inp = pointer(SOURCE, f["input_pointer"])
|
|
assert inp.get("widget", {}).get("name", inp["name"]) == f["field"]
|
|
for key in [
|
|
"effect",
|
|
"label",
|
|
"schema_default",
|
|
"unit",
|
|
"limits",
|
|
"execution_note",
|
|
"role",
|
|
"type",
|
|
]:
|
|
assert f[key]
|
|
for i in f["evidence"]:
|
|
e = BLOCK["sources"][i]
|
|
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())
|
|
|
|
|
|
@pytest.mark.parametrize("f", BLOCK["fields"], ids=lambda f: f["node_id"] + "." + f["field"])
|
|
@pytest.mark.parametrize("variant", ["source", "text", "named"])
|
|
def test_each_saved_value_or_description_forgery_cannot_retrust(f, variant):
|
|
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
|
|
if variant == "text":
|
|
r["fixed_version_document"]["fields"][BLOCK["fields"].index(f)]["effect"] = "forged"
|
|
else:
|
|
p = f["pointer"] if variant == "source" else f["named_pointer"]
|
|
parent, _, key = p.rpartition("/")
|
|
c = pointer(s, parent)
|
|
c[int(key) if isinstance(c, list) else key] = "forged"
|
|
refuse(s, r)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"scope,index",
|
|
[
|
|
(base, i)
|
|
for base, ns in [
|
|
("/nodes", SOURCE["nodes"]),
|
|
*[
|
|
(f"/definitions/subgraphs/{i}/nodes", s["nodes"])
|
|
for i, s in enumerate(SOURCE["definitions"]["subgraphs"])
|
|
],
|
|
]
|
|
for i in range(len(ns))
|
|
],
|
|
)
|
|
@pytest.mark.parametrize("variant", ["mode", "identity"])
|
|
def test_every_node_identity_and_mode_bound(scope, index, variant):
|
|
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
|
|
n = pointer(s, scope)[index]
|
|
if variant == "mode":
|
|
n["mode"] = 1 if n["mode"] != 1 else 0
|
|
else:
|
|
n["properties"]["ver"] = "forged"
|
|
refuse(s, r)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"scope,index",
|
|
[
|
|
(base, i)
|
|
for base, es in [
|
|
("/links", SOURCE["links"]),
|
|
*[
|
|
(f"/definitions/subgraphs/{i}/links", s["links"])
|
|
for i, s in enumerate(SOURCE["definitions"]["subgraphs"])
|
|
],
|
|
]
|
|
for i in range(len(es))
|
|
],
|
|
)
|
|
def test_every_physical_or_boundary_edge_bound(scope, index):
|
|
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
|
|
e = pointer(s, scope)[index]
|
|
if isinstance(e, list):
|
|
e[3] = 99999
|
|
else:
|
|
e["target_slot"] = 99999
|
|
refuse(s, r)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"si,key,i",
|
|
[
|
|
(si, key, i)
|
|
for si, s in enumerate(SOURCE["definitions"]["subgraphs"])
|
|
for key in ["inputs", "outputs"]
|
|
for i in range(len(s[key]))
|
|
],
|
|
)
|
|
def test_each_definition_boundary_is_bound(si, key, i):
|
|
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
|
|
s["definitions"]["subgraphs"][si][key][i]["linkIds"] = []
|
|
refuse(s, r)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"variant",
|
|
[
|
|
"note",
|
|
"named-note",
|
|
"meaning",
|
|
"source_sha",
|
|
"overview",
|
|
"citation",
|
|
"definition-digest",
|
|
"empty",
|
|
"missing",
|
|
],
|
|
)
|
|
def test_note_and_definition_trust(variant):
|
|
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
|
|
if variant in ["note", "named-note"]:
|
|
p = (
|
|
BLOCK["comments"][0]["pointer"]
|
|
if variant == "note"
|
|
else BLOCK["comments"][0]["named_pointer"]
|
|
)
|
|
parent, _, key = p.rpartition("/")
|
|
c = pointer(s, parent)
|
|
c[int(key) if isinstance(c, list) else key] = "forged"
|
|
elif variant == "meaning":
|
|
r["fixed_version_document"]["comments"][0]["meaning"] = "forged"
|
|
elif variant == "source_sha":
|
|
r["source_sha"] = "forged"
|
|
elif variant == "overview":
|
|
r["fixed_version_document"]["user_overview"] = "forged"
|
|
elif variant == "citation":
|
|
r["fixed_version_document"]["sources"][0]["sha256"] = "forged"
|
|
elif variant == "definition-digest":
|
|
r["fixed_version_document"]["boundaries"][0]["canonical_sha256"] = "forged"
|
|
elif variant == "empty":
|
|
r["fixed_version_document"] = {}
|
|
else:
|
|
r.pop("fixed_version_document")
|
|
assert not doc(s, r)["fixed_version"]["matched"]
|
|
|
|
|
|
def test_inventory_all_instances_and_frozen_previous_reviews():
|
|
inv = json.loads((OUT / "independent-inventory.json").read_bytes())
|
|
assert [f["pointer"] for f in inv["fields"]] == [f["pointer"] for f in BLOCK["fields"]]
|
|
assert len(BLOCK["fields"]) == 67 and len(BLOCK["comments"]) == 1
|
|
assert sum(f["scope"] == "root" for f in BLOCK["fields"]) == 18
|
|
assert len({f["scope"] for f in BLOCK["fields"]}) == 6
|
|
assert [e["state"] for e in inv["empty"]] == ["list"] * 5 + ["missing"]
|
|
assert BLOCK["comments"][0]["text"] == pointer(SOURCE, BLOCK["comments"][0]["pointer"])
|
|
resource = "calliope-backend/src/calliope/co" + "mfyui/source_reviews.json"
|
|
old = json.loads(
|
|
subprocess.check_output(
|
|
["git", "show", ("a7ea35daecd51369d28e42e925362527" + "76ea5d7b:") + resource], cwd=REPO
|
|
)
|
|
)
|
|
cur = json.loads((REPO / resource).read_bytes())
|
|
cur[PATH].pop("fixed_version_document")
|
|
assert cur == old
|
|
blocks = [r["fixed_version_document"] for r in old.values() if "fixed_version_document" in r]
|
|
assert (
|
|
len(blocks) == 37
|
|
and sum(len(b["fields"]) for b in blocks) == 1281
|
|
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
|
|
)
|
|
== 40
|
|
)
|
|
assert doc()["fixed_version"]["matched"]
|
|
|
|
|
|
@pytest.mark.parametrize("generated", [False, True])
|
|
def test_exact_versions_conflicting_refine_and_preview_protocol(generated):
|
|
import runpy
|
|
|
|
b = runpy.run_path(str(OUT / "build_definition.py"))["build"]() if generated else BLOCK
|
|
assert {e["commit"] for e in b["sources"] if e["id"] == "frontend"} == {
|
|
"1a7a6b82514874545dc5e8203d0bd2188c2d9b18"
|
|
}
|
|
assert {e["commit"] for e in b["sources"] if e["id"] == "core"} == {
|
|
"ee71d5c4993f29086b27fde1629a945ae48425bf"
|
|
}
|
|
import ast
|
|
|
|
text = (OUT.parent / ("moge3-reference/core__comfy_extr" + "as__nodes_moge.py")).read_text()
|
|
tree = ast.parse(text)
|
|
cls = next(n for n in tree.body if isinstance(n, ast.ClassDef) and n.name == "MoGeInference")
|
|
execute = next(n for n in cls.body if isinstance(n, ast.FunctionDef) and n.name == "execute")
|
|
assert "refine_steps" not in [a.arg for a in execute.args.args]
|
|
assert "refine_steps" not in ast.get_source_segment(text, cls)
|
|
extra = [f for f in b["fields"] if f["field"] == "refine_steps"]
|
|
assert len(extra) == 4
|
|
assert all("冲突" in f["effect"] and "没有此输入" in f["schema_default"] for f in extra)
|
|
ns = ast.parse((OUT.parent / "moge3-reference/core__nodes.py").read_text())
|
|
preview = next(n for n in ns.body if isinstance(n, ast.ClassDef) and n.name == "PreviewImage")
|
|
assert [x.id for x in preview.bases] == ["SaveImage"]
|
|
save = next(n for n in ns.body if isinstance(n, ast.ClassDef) and n.name == "SaveImage")
|
|
assert next(
|
|
ast.literal_eval(n.value)
|
|
for n in save.body
|
|
if isinstance(n, ast.Assign)
|
|
and any(isinstance(t, ast.Name) and t.id == "RETURN_TYPES" for t in n.targets)
|
|
) == ("IMAGE",)
|
|
assert '"result" : (images,)' in (OUT.parent / "moge3-reference/core__nodes.py").read_text()
|
|
assert (
|
|
len(
|
|
[
|
|
f
|
|
for f in b["fields"]
|
|
if f["field"] == "output" and f["scope"] != "root" and f["value"] == "mask"
|
|
]
|
|
)
|
|
== 4
|
|
)
|
|
assert "mode4" in "\n".join(b["flow"]) and "2048方图" in b["user_overview"]
|
|
|
|
|
|
def test_grid_template_keeps_only_current_graph_effects():
|
|
fields = [f for f in BLOCK["fields"] if f["node_id"].startswith("111:")]
|
|
assert len(fields) == 17
|
|
assert all("16/17" not in f["effect"] and "21重采样" not in f["effect"] for f in fields)
|
|
assert all("mode4" in f["effect"] for f in fields if f["node_id"] == "111:110")
|
|
assert len([f for f in BLOCK["fields"] if "第1支路" in f["effect"]]) == 12
|