278 lines
12 KiB
Python
278 lines
12 KiB
Python
"Exact graph binding, all saved positions, dual scopes and closed eligibility."
|
|
|
|
import copy
|
|
import hashlib
|
|
import json
|
|
import runpy
|
|
import sys
|
|
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/krea-realn-document-evidence")
|
|
PATH = "example_ComfyUI_workflows/3-图像生成/krea2/krea2_turbo_文生图_极致写实(N版).json"
|
|
RAW = (REPO / PATH).read_bytes()
|
|
SOURCE = json.loads(RAW)
|
|
REVIEW = review_for(PATH, hashlib.sha1(RAW).hexdigest())
|
|
BLOCK = REVIEW["fixed_version_document"]
|
|
|
|
|
|
def pointer(obj, p):
|
|
for x in p.strip("/").split("/"):
|
|
obj = obj[int(x)] if isinstance(obj, list) else obj[x]
|
|
return obj
|
|
|
|
|
|
def doc(s=SOURCE, r=REVIEW):
|
|
return build_document(PATH, s, None, None, [], r)
|
|
|
|
|
|
@pytest.mark.parametrize("f", BLOCK["fields"], ids=lambda f: f["node_id"] + "." + f["field"])
|
|
def test_each_saved_position_and_citation(f):
|
|
assert canonical(pointer(SOURCE, f["pointer"])) == canonical(f["value"])
|
|
assert type(pointer(SOURCE, f["pointer"])).__name__ == f["json_type"]
|
|
assert "无此具名保存键" in f["named_pointer"] and "named_value" not in f
|
|
assert all(
|
|
f[k] for k in ["label", "unit", "limits", "schema_default", "effect", "execution_note"]
|
|
)
|
|
if f["input_pointer"].startswith("/"):
|
|
assert pointer(SOURCE, f["input_pointer"])["name"] == f["input_name"]
|
|
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"])
|
|
def test_caller_rehash_cannot_expand_trust(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, _, key = f["pointer"].rpartition("/")
|
|
pointer(s, p)[int(key)] = "forged"
|
|
r["fixed_version_document"]["canonical_sha256"] = hashlib.sha256(canonical(s)).hexdigest()
|
|
assert not doc(s, r)["fixed_version"]["matched"]
|
|
|
|
|
|
@pytest.mark.parametrize("key", ["extra", "links", "id", "groups", "version"])
|
|
def test_whole_graph_mutations_fail_closed(key):
|
|
source, review = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
|
|
source[key] = "forged"
|
|
review["fixed_version_document"]["canonical_sha256"] = hashlib.sha256(
|
|
canonical(source)
|
|
).hexdigest()
|
|
assert not doc(source, review)["fixed_version"]["matched"]
|
|
|
|
|
|
@pytest.mark.parametrize("key", ["mode", "properties", "inputs", "outputs"])
|
|
@pytest.mark.parametrize("index", [0, 4, 5])
|
|
def test_node_identity_and_state_fail_closed(key, index):
|
|
source, review = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
|
|
source["nodes"][index][key] = "forged"
|
|
review["fixed_version_document"]["canonical_sha256"] = hashlib.sha256(
|
|
canonical(source)
|
|
).hexdigest()
|
|
assert not doc(source, review)["fixed_version"]["matched"]
|
|
|
|
|
|
def test_path_and_source_binding_cannot_be_extended():
|
|
from calliope.comfyui.fixed_document import fixed_document_for
|
|
|
|
assert fixed_document_for(PATH + "forged", SOURCE, REVIEW) is None
|
|
review = copy.deepcopy(REVIEW)
|
|
review["source_sha"] = "forged"
|
|
assert not doc(SOURCE, review)["fixed_version"]["matched"]
|
|
|
|
|
|
@pytest.mark.parametrize("suffix", [b"\n", b" ", b"\t"])
|
|
def test_original_byte_identity_rejects_equivalent_json(suffix):
|
|
altered = RAW + suffix
|
|
assert json.loads(altered) == SOURCE
|
|
assert review_for(PATH, hashlib.sha1(altered).hexdigest()) is None
|
|
|
|
|
|
@pytest.mark.parametrize("key", ["user_overview", "source_sha256", "sources", "flow", "counts"])
|
|
def test_editorial_or_citation_rehash_cannot_replace_code_owned_pin(key):
|
|
source, review = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
|
|
review["fixed_version_document"][key] = "forged"
|
|
review["fixed_version_document"]["canonical_sha256"] = hashlib.sha256(
|
|
canonical(source)
|
|
).hexdigest()
|
|
assert not doc(source, review)["fixed_version"]["matched"]
|
|
|
|
|
|
@pytest.mark.parametrize("note", BLOCK["comments"])
|
|
def test_full_note_original_and_forgery(note):
|
|
assert pointer(SOURCE, note["pointer"]) == note["text"]
|
|
r = copy.deepcopy(REVIEW)
|
|
r["fixed_version_document"]["comments"][BLOCK["comments"].index(note)]["text"] = "forged"
|
|
assert not doc(SOURCE, r)["fixed_version"]["matched"]
|
|
|
|
|
|
@pytest.mark.parametrize("key", ["properties", "inputs", "outputs", "mode"])
|
|
@pytest.mark.parametrize("nid", [29, 49, 30])
|
|
def test_control_identity_and_state_forgery_fail_closed(nid, key):
|
|
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
|
|
next(n for n in s["nodes"] if n["id"] == nid)[key] = "forged"
|
|
r["fixed_version_document"]["canonical_sha256"] = hashlib.sha256(canonical(s)).hexdigest()
|
|
assert not doc(s, r)["fixed_version"]["matched"]
|
|
|
|
|
|
@pytest.mark.parametrize("key", ["mode", "properties", "inputs", "outputs"])
|
|
@pytest.mark.parametrize("index", [0, 2, 9, 13, 15, 19])
|
|
def test_nested_node_identity_and_state_forgery_is_closed(key, index):
|
|
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
|
|
s["definitions"]["subgraphs"][0]["nodes"][index][key] = "forged"
|
|
r["fixed_version_document"]["canonical_sha256"] = hashlib.sha256(canonical(s)).hexdigest()
|
|
assert not doc(s, r)["fixed_version"]["matched"]
|
|
|
|
|
|
@pytest.mark.parametrize("key", ["inputs", "outputs", "widgets", "inputNode", "outputNode"])
|
|
def test_subgraph_boundary_forgery_is_closed(key):
|
|
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
|
|
s["definitions"]["subgraphs"][0][key] = "forged"
|
|
r["fixed_version_document"]["canonical_sha256"] = hashlib.sha256(canonical(s)).hexdigest()
|
|
assert not doc(s, r)["fixed_version"]["matched"]
|
|
|
|
|
|
def test_exact_build_and_qualification():
|
|
assert canonical(runpy.run_path(str(OUT / "build_definition.py"))["build"]()) == canonical(
|
|
BLOCK
|
|
)
|
|
assert len(DEFINITIONS) == 87 and doc()["fixed_version"]["matched"]
|
|
assert BLOCK["counts"]["saved_fields"] == 105
|
|
assert BLOCK["counts"]["root_saved_fields"] == 20
|
|
assert BLOCK["counts"]["internal_saved_fields"] == 85
|
|
assert (
|
|
BLOCK["counts"]["main_saved_fields"] == 53 and BLOCK["counts"]["nested_saved_fields"] == 32
|
|
)
|
|
assert len(BLOCK["comments"]) == 3 and len(BLOCK["boundaries"]) == 2
|
|
assert not doc()["status"]["doc_done"] and not doc()["status"]["exec_verified"]
|
|
sys.path.insert(0, str(OUT))
|
|
try:
|
|
runpy.run_path(str(OUT / "verify_sources.py"))["main"]()
|
|
finally:
|
|
sys.path.remove(str(OUT))
|
|
|
|
|
|
def test_two_scopes_modes_and_unknown_plugin_positions():
|
|
assert hashlib.sha256(RAW).hexdigest() == (
|
|
"ec98804940c28956af1719096751bba2d274eef8a251b97207ac655a05779384"
|
|
)
|
|
root = {n["id"]: n for n in SOURCE["nodes"]}
|
|
main, nested = SOURCE["definitions"]["subgraphs"]
|
|
ns = {n["id"]: n for n in main["nodes"]}
|
|
llm = {n["id"]: n for n in nested["nodes"]}
|
|
assert len(root) == 8 and len(ns) == 21 and len(llm) == 4
|
|
assert [len(g["links"]) for g in [SOURCE, main, nested]] == [3, 41, 14]
|
|
assert all(n["mode"] == 0 for n in root.values())
|
|
assert {i for i, n in ns.items() if n["mode"] == 4} == {16, 17, 200}
|
|
assert all(n["mode"] == 4 for n in llm.values())
|
|
assert root[204]["mode"] == 0 and "widgets_values" not in root[204]
|
|
assert root[204]["outputs"][0]["links"] is None
|
|
assert all(ns[i]["widgets_values"] == [] for i in [8, 13, 20])
|
|
assert len(root[30]["widgets_values"]) == len(root[30]["inputs"]) == 15
|
|
assert len(ns[200]["widgets_values"]) == 9 and len(ns[200]["inputs"]) == 10
|
|
assert main["widgets"] == nested["widgets"] == []
|
|
assert all("proxyWidgets" not in h["properties"] for h in [root[30], ns[200]])
|
|
assert ns[200]["properties"]["ver"] == "0.20.1"
|
|
assert (
|
|
ns[200]["properties"]["proxyWidgetErrorQuarantine"][0]["reason"] == "missingSubgraphInput"
|
|
)
|
|
unknown = [f for f in BLOCK["fields"] if f["scope"] == "30:200"]
|
|
assert all(
|
|
f["unit"].startswith("未知") and f["schema_default"].startswith("未知")
|
|
for f in BLOCK["fields"]
|
|
if f["node_id"] == "30:200"
|
|
)
|
|
assert all(f["input_pointer"].startswith("未知") for f in unknown)
|
|
assert len(unknown) == 32 and all(
|
|
f["field"].startswith("saved_position_")
|
|
and f["input_name"] is None
|
|
and f["schema_default"].startswith("未知")
|
|
for f in unknown
|
|
)
|
|
assert all(
|
|
len(llm[i]["widgets_values"]) == c for i, c in [(191, 2), (192, 7), (193, 13), (194, 10)]
|
|
)
|
|
assert all(
|
|
llm[i]["properties"]["ver"] == ("20571a3cf504b8308781d3cad2e432444ec3d75b")
|
|
for i in [192, 193, 194]
|
|
)
|
|
assert "ver" not in llm[191]["properties"] and "cnr_id" not in llm[191]["properties"]
|
|
|
|
|
|
def test_texts_boundaries_and_caches_are_not_execution_results():
|
|
root = {n["id"]: n for n in SOURCE["nodes"]}
|
|
ns = {n["id"]: n for n in SOURCE["definitions"]["subgraphs"][0]["nodes"]}
|
|
assert len(root[205]["widgets_values"][0]) == 334 and root[205]["outputs"][0]["links"] is None
|
|
assert root[205]["properties"]["ver"] == "0.27.0"
|
|
assert root[30]["widgets_values"][0] == ns[19]["widgets_values"][0]
|
|
assert len(ns[19]["widgets_values"][0]) == 958 and ns[19]["widgets_values"][0].endswith("\n")
|
|
assert len(ns[6]["widgets_values"][0]) == 1754
|
|
assert [n["node_id"] for n in BLOCK["comments"]] == ["48", "50", "206"]
|
|
assert [len(n["text"]) for n in BLOCK["comments"]] == [1657, 1965, 70]
|
|
assert ns[16]["mode"] == ns[17]["mode"] == 4
|
|
assert ns[16]["inputs"][0]["link"] is None and ns[16]["outputs"][0]["links"] == []
|
|
assert ns[18]["outputs"][0]["links"] == [363, 369]
|
|
assert ns[19]["outputs"][0]["links"] == [34, 364]
|
|
assert [i["link"] for i in ns[200]["inputs"]] == [
|
|
None,
|
|
367,
|
|
368,
|
|
None,
|
|
365,
|
|
None,
|
|
364,
|
|
363,
|
|
None,
|
|
None,
|
|
]
|
|
assert ns[200]["outputs"][0]["links"] == [366]
|
|
assert ns[21]["inputs"][1]["link"] == 366
|
|
assert root[30]["widgets_values"][-1] == "Qwen3.5-9b-mmproj-BF16.gguf"
|
|
assert ns[200]["widgets_values"][1] == "Qwen3.5-9B-mmproj-F32.gguf"
|
|
|
|
|
|
def test_exact_sources_active_model_condition_caches_and_conflicts():
|
|
root = {n["id"]: n for n in SOURCE["nodes"]}
|
|
ns = {n["id"]: n for n in SOURCE["definitions"]["subgraphs"][0]["nodes"]}
|
|
assert {e["id"] for e in BLOCK["sources"]} == {
|
|
"core0250",
|
|
"core0270",
|
|
"frontend",
|
|
"llama-unavailable",
|
|
}
|
|
assert root[30]["widgets_values"][1] is False and ns[24]["widgets_values"] == [False]
|
|
assert root[30]["widgets_values"][6] is True and ns[23]["widgets_values"] == [True]
|
|
assert ns[15]["widgets_values"] == ["Krea2\\krea2_realism_engine_v3.1.safetensors", 0.9]
|
|
assert ns[27]["widgets_values"] == ["", "photograph", ", "]
|
|
assert ns[3]["widgets_values"] == [341313354252232, "randomize", 8, 1, "euler", "simple", 1]
|
|
assert root[49]["widgets_values"] == ["9:16 (Portrait Widescreen)", 1.5, 8]
|
|
assert ns[5]["outputs"][0]["links"] == [2]
|
|
assert ns[15]["outputs"][0]["links"] == [29]
|
|
assert root[29]["outputs"][0]["links"] is None
|
|
assert (
|
|
"krea2枚举缺失"
|
|
in next(f for f in BLOCK["fields"] if f["node_id"] == "30:11" and f["field"] == "type")[
|
|
"limits"
|
|
]
|
|
)
|
|
presence = next(f for f in BLOCK["fields"] if f["field"] == "sampling_mode.presence_penalty")
|
|
assert presence["json_type"] == "int" and presence["schema_default"] == "0.0"
|
|
|
|
|
|
@pytest.mark.parametrize("key", ["inputs", "outputs", "widgets", "inputNode", "outputNode"])
|
|
def test_nested_definition_boundary_forgery_fails_closed(key):
|
|
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
|
|
s["definitions"]["subgraphs"][1][key] = "forged"
|
|
r["fixed_version_document"]["canonical_sha256"] = hashlib.sha256(canonical(s)).hexdigest()
|
|
assert not doc(s, r)["fixed_version"]["matched"]
|