"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-style-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_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, 71, 75, 30, 69]) 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) == 85 and doc()["fixed_version"]["matched"] assert BLOCK["counts"]["saved_fields"] == 63 assert BLOCK["counts"]["root_saved_fields"] == 17 assert BLOCK["counts"]["internal_saved_fields"] == 46 assert BLOCK["counts"]["source_notes"] == 1 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_original_identity_scopes_and_boundary_fanout(): assert ( hashlib.sha256(RAW).hexdigest() == "8c248ccc44df79b475c81e0bb260596ece066d65f2a21c0b2a72dc6b2441b170" ) 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.21" assert len(roots) == 6 and len(ns) == 23 assert len(SOURCE["links"]) == 5 and len(sg["links"]) == 43 assert roots[71]["widgets_values"] == ["1:1 (Square)", 1, 8] assert type(roots[71]["widgets_values"][1]) is int assert len(roots[30]["widgets_values"]) == 11 assert len(roots[30]["inputs"]) == len(sg["inputs"]) == 12 assert sum(len(i["linkIds"]) for i in sg["inputs"]) == 16 assert [i["linkIds"] for i in sg["inputs"] if i["name"] in ["width", "height"]] == [ [50, 99, 101], [51, 100, 102], ] assert roots[30]["inputs"][11] == {"name": "image1", "shape": 7, "type": "IMAGE", "link": 136} assert sg["widgets"] == [] and "proxyWidgets" not in roots[30]["properties"] assert roots[30]["properties"]["previewExposures"][0]["sourceNodeId"] == "3" and 3 not in ns assert not {6, 22, 23, 27, 28, 51} & ns.keys() def test_reference_condition_and_true_sampling_start_are_separate(): roots = {n["id"]: n for n in SOURCE["nodes"]} ns = {n["id"]: n for n in SOURCE["definitions"]["subgraphs"][0]["nodes"]} assert roots[69]["widgets_values"] == ["微信图片_20260727203512_143_6.jpg", "image"] assert roots[69]["outputs"][0]["links"] == [136, 137] assert roots[69]["outputs"][1]["links"] is None assert roots[75]["mode"] == 4 and roots[75]["widgets_values"] == [] assert [o["links"] for o in roots[75]["outputs"]] == [[], [], None] assert ns[5]["outputs"][0]["links"] == [] assert ns[61]["outputs"][0]["links"] == [91] assert ns[58]["inputs"][-1]["name"] == "latent_image" and ns[58]["inputs"][-1]["link"] == 91 assert ns[58]["outputs"][1]["links"] is None assert ns[52]["widgets_values"] == ["cute"] assert {i["name"]: i["link"] for i in ns[52]["inputs"]} == { "clip": 73, "vae": 120, "image1": 78, "image2": None, "image3": None, "prompt": 112, } assert ns[53]["widgets_values"] == ["index_timestep_zero"] assert [i["link"] for i in ns[57]["inputs"]] == [86, 96, 97, None] assert all( any( f["node_id"] == nid and f["field"] == field and f["value"] == 1024 for f in BLOCK["fields"] ) for nid in ["30", "30:5", "30:61", "30:64"] for field in ["width", "height"] ) def test_independent_versions_single_lora_text_and_seeds(): roots = {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", "core0280", "core0292", "core051", "frontend", } assert BLOCK["flow_evidence"] == list(range(len(BLOCK["sources"]))) assert ns[53]["properties"]["ver"] == "0.5.1" assert roots[75]["properties"]["ver"] == "0.29.2" assert all(ns[i]["properties"]["ver"] == "0.28.0" for i in [52, 57, 58, 59, 60, 61, 62, 63, 64]) assert ns[15]["widgets_values"] == ["krea2_style_reference.safetensors", 1] assert type(ns[15]["widgets_values"][1]) is int assert ns[15]["outputs"][0]["links"] == [95] assert ns[64]["outputs"][0]["links"] == [86, 92] assert ns[63]["widgets_values"] == [355028178891957, "randomize"] assert ns[63]["inputs"][0]["name"] == "noise_seed" and ns[63]["inputs"][0]["link"] == 113 assert roots[30]["widgets_values"][5] == 465267279053083 assert ( roots[30]["widgets_values"][0] == 'a white yeti with horns reading a book that is titled "Krea2 Style Reference"' ) assert ( ns[19]["widgets_values"][0] == 'a white yeti with horns reading a book that is titled "Ostris + Krea2 Style Reference"' ) assert ns[24]["widgets_values"] == ns[21]["widgets_values"] == [False] assert ( len(BLOCK["comments"]) == 1 and BLOCK["comments"][0]["node_id"] == "48" and len(BLOCK["comments"][0]["text"]) == 1668 ) assert roots[29]["outputs"][0]["links"] == [] 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" cfg = next(f for f in BLOCK["fields"] if f["node_id"] == "30:57") assert cfg["json_type"] == "int" and cfg["schema_default"] == "8.0"