"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-flat-document-evidence") PATH = "example_ComfyUI_workflows/3-图像生成/krea2/krea2_turbo_文生图_动漫风格线条动漫.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_exact_build_and_qualification(): assert canonical(runpy.run_path(str(OUT / "build_definition.py"))["build"]()) == canonical( BLOCK ) assert len(DEFINITIONS) == 83 and doc()["fixed_version"]["matched"] assert BLOCK["counts"]["saved_fields"] == 61 and BLOCK["counts"]["root_saved_fields"] == 17 assert BLOCK["counts"]["source_notes"] == 4 and len(BLOCK["comments"]) == 4 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_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"] def test_krea_original_caches_boundaries_dynamic_branch_and_conflicts(): assert hashlib.sha256(RAW).hexdigest() == ( "9bab028943604b4b167a56dcc51475f67bac897c9eae3c7bf9d311b2c5cb7d91" ) roots = {n["id"]: n for n in SOURCE["nodes"]} sg = SOURCE["definitions"]["subgraphs"][0] ns = {n["id"]: n for n in sg["nodes"]} assert SOURCE["extra"]["frontendVersion"] == "1.45.19" assert len(roots) == 7 and len(ns) == 20 assert len(SOURCE["links"]) == 3 and len(sg["links"]) == 39 assert roots[49]["widgets_values"] == ["16:9 (Widescreen)", 1.5, 8] assert type(roots[49]["widgets_values"][1]) is float assert len(roots[30]["widgets_values"]) == len(sg["inputs"]) == 13 assert sg["widgets"] == [] and "proxyWidgets" not in roots[30]["properties"] assert roots[30]["widgets_values"][3:5] == [1024, 1024] assert ns[5]["widgets_values"] == [1024, 1024, 1] assert [x["link"] for x in ns[5]["inputs"]] == [50, 51, None] assert roots[30]["widgets_values"][10].startswith("Krea2\\") assert ns[10]["widgets_values"][0].startswith("krea2\\") assert ns[10]["properties"]["models"][0]["name"] == "krea2_turbo_fp8_scaled.safetensors" assert "Krea2_Anime_Flat_V20_线条动漫.safetensors" in ns[15]["widgets_values"][0] assert ns[15]["properties"]["models"][0]["name"] == "krea2_darkbrush.safetensors" assert ns[16]["widgets_values"] == ["", 512, "on", 0.7, 64, 0.95, 0.05, 1.05, 0, 0, False, True] 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" assert ( "krea2枚举缺失" in next(f for f in BLOCK["fields"] if f["node_id"] == "30:11" and f["field"] == "type")[ "limits" ] ) assert ns[24]["widgets_values"] == [True] and ns[23]["widgets_values"] == [True] assert all(ns[i]["widgets_values"] == [False] for i in [21, 22, 28]) assert all(ns[i]["widgets_values"] == [] for i in [8, 13, 20]) assert {n["node_id"]: len(n["text"]) for n in BLOCK["comments"]} == { "47": 1023, "48": 1657, "50": 1965, "51": 70, } assert 52 not in roots and roots[51]["type"] == "Note" assert roots[30]["widgets_values"][5] == ns[3]["widgets_values"][0] == 829586144120094 assert ns[3]["widgets_values"][4] == "er_sde" assert ns[27]["widgets_values"] == ["", "线条动漫", ", "] assert roots[30]["widgets_values"][7] == ns[15]["widgets_values"][0] assert len(roots[30]["widgets_values"][0]) == len(ns[19]["widgets_values"][0]) == 245 assert BLOCK["counts"]["boundary_saved_fields"] == 13 assert not any(n["type"] in ["OpenAI", "Gemini"] for n in ns.values()) @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_flat_single_loader_modes_and_exact_sources(): sg = SOURCE["definitions"]["subgraphs"][0] ns = {n["id"]: n for n in sg["nodes"]} assert 51 not in ns and all(n["mode"] == 0 for n in sg["nodes"]) assert [ (e["id"], e["origin_id"], e["origin_slot"], e["target_id"], e["target_slot"]) for e in sg["links"] if e["id"] in [29, 72, 73, 74] ] == [(29, 15, 0, 22, 1)] assert {s["id"] for s in BLOCK["sources"]} == {"core0250", "frontend"} assert BLOCK["flow_evidence"] == list(range(len(BLOCK["sources"]))) strength = next( f for f in BLOCK["fields"] if f["node_id"] == "30:15" and f["field"] == "strength_model" ) assert ( strength["value"] == 1 and strength["json_type"] == "int" and strength["schema_default"] == "1.0" ) assert ns[19]["widgets_values"][0].startswith("生成图片:【三视图】风格为3D图") assert len([n for n in sg["nodes"] if n["type"] == "LoraLoaderModelOnly"]) == 1