"""Two editorial source bindings, exact saved values and frozen execution rejection.""" from __future__ import annotations 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/grid-document-evidence" NAMES = ["图像合并成四宫格.json", "图像分割-多宫格分割.json"] PATHS = [next(p for p in DEFINITIONS if p.endswith(n)) for n in NAMES] SOURCES = [json.loads((REPO / p).read_bytes()) for p in PATHS] REVIEWS = [review_for(p, hashlib.sha1((REPO / p).read_bytes()).hexdigest()) for p in PATHS] BLOCKS = [r["fixed_version_document"] for r in REVIEWS] POSITIONS = [(i, j) for i, b in enumerate(BLOCKS) for j in range(len(b["fields"]))] def pointer(obj, path): for p in path.strip("/").split("/"): p = p.replace("~1", "/").replace("~0", "~") obj = obj[int(p)] if isinstance(obj, list) else obj[p] return obj def doc(i, s=None, r=None, path=None): return build_document( path or PATHS[i], SOURCES[i] if s is None else s, None, None, [], REVIEWS[i] if r is None else r, ) @pytest.mark.parametrize("i,j", POSITIONS) def test_source_positions_and_content_addressed_reference(i, j): b = BLOCKS[i] f = b["fields"][j] value = pointer(SOURCES[i], f["pointer"]) assert canonical(value) == canonical(f["value"]) and type(value).__name__ == f["json_type"] node = pointer(SOURCES[i], f["pointer"].split("/widgets_values/")[0]) assert str(node["id"]) == f["node_id"] and node["type"] == f["class_type"] for name in ["label", "effect", "schema_default", "unit", "limits", "execution_note"]: assert f[name] for k in f["evidence"]: e = b["sources"][k] raw = (OUT / e["local_file"]).read_bytes() assert hashlib.sha256(raw).hexdigest() == e["sha256"] assert len(e["commit"]) == 40 and e["commit"] in e["url"] assert 1 <= e["lines"][0] <= e["lines"][1] <= len(raw.decode().splitlines()) @pytest.mark.parametrize("i,j", POSITIONS) @pytest.mark.parametrize("kind", ["value", "type", "pointer", "citation", "role", "default"]) def test_mismatches_cannot_be_trusted_by_recomputing_caller_digest(i, j, kind): s, r = copy.deepcopy(SOURCES[i]), copy.deepcopy(REVIEWS[i]) b = r["fixed_version_document"] f = b["fields"][j] if kind in ["value", "type"]: parent, _, key = f["pointer"].rpartition("/") v = {"tampered": True} if kind == "value" else 1 if isinstance(f["value"], bool) else False pointer(s, parent)[int(key)] = v elif kind == "citation": b["sources"][f["evidence"][0]]["sha256"] = "0" * 64 else: f[{"default": "schema_default"}.get(kind, kind)] = "forged" b["canonical_sha256"] = hashlib.sha256(canonical(s)).hexdigest() r["caller_digest"] = hashlib.sha256(canonical(b)).hexdigest() d = doc(i, s, r)["fixed_version"] assert d["matched"] is False and "fields" not in d MUTATIONS = [ (i, p) for i in range(2) for p in [ "/links/0/1", "/nodes/0/type", "/nodes/0/mode", "/nodes/0/properties/ver", "/nodes/0/properties/cnr_id", "/nodes/0/properties/ue_properties/version", ] ] + [ (0, "/extra/frontendVersion"), (0, "/nodes/8/widgets_values/0"), (1, "/nodes/1/properties/aux_id"), (1, "/nodes/0/outputs/0/type"), ] @pytest.mark.parametrize("i,path", MUTATIONS) def test_source_versions_providers_edges_notes_and_export_conflict_fail_closed(i, path): s, r = copy.deepcopy(SOURCES[i]), copy.deepcopy(REVIEWS[i]) parent, _, key = path.rpartition("/") t = pointer(s, parent) t[int(key) if isinstance(t, list) else key] = "forged" r["fixed_version_document"]["canonical_sha256"] = hashlib.sha256(canonical(s)).hexdigest() r["caller_digest"] = hashlib.sha256(canonical(r["fixed_version_document"])).hexdigest() assert not doc(i, s, r)["fixed_version"]["matched"] @pytest.mark.parametrize("i", range(2)) @pytest.mark.parametrize( "key", ["source_sha1", "source_sha256", "source_path", "notice", "user_overview"] ) def test_definition_identity_and_editorial_forgery_rejected(i, key): r = copy.deepcopy(REVIEWS[i]) r["fixed_version_document"][key] = "forged" r["caller_digest"] = hashlib.sha256(canonical(r["fixed_version_document"])).hexdigest() assert not doc(i, r=r)["fixed_version"]["matched"] @pytest.mark.parametrize("i", range(2)) def test_real_document_keeps_offline_status_and_rejects_cross_binding(i): d = doc(i) assert d["fixed_version"]["matched"] and len(d["fixed_version"]["fields"]) == [26, 9][i] assert d["status"] == dict(imported=True, doc_done=False, exec_verified=False) assert d["dependencies"]["check_state"] == "unchecked" r = copy.deepcopy(REVIEWS[i]) r["fixed_version_document"] = BLOCKS[1 - i] assert not doc(i, r=r)["fixed_version"]["matched"] assert "fixed_version" not in doc(i, path="untrusted.json") def test_exact_topology_and_reference_semantics(): grid, split = BLOCKS assert SOURCES[0]["links"] == [ [11, 12, 0, 16, 0, "IMAGE"], [12, 13, 0, 16, 1, "IMAGE"], [13, 14, 0, 17, 0, "IMAGE"], [14, 15, 0, 17, 1, "IMAGE"], [15, 16, 0, 18, 0, "IMAGE"], [16, 17, 0, 18, 1, "IMAGE"], [18, 21, 0, 19, 0, "IMAGE"], [19, 18, 0, 21, 0, "IMAGE"], ] assert SOURCES[1]["links"] == [[1, 1, 0, 2, 0, "IMAGE"], [2, 2, 0, 3, 0, "IMAGE"]] assert [SOURCES[i]["extra"].get("frontendVersion") for i in range(2)] == ["1.41.20", None] assert [grid["counts"]["source_nodes"], split["counts"]["source_nodes"]] == [10, 3] assert pointer(SOURCES[0], grid["comments"][0]["pointer"]) == grid["comments"][0]["text"] assert SOURCES[1]["nodes"][0]["outputs"][0]["type"] == "IMAGE" assert [z["node_id"] for z in split["zero_saved_nodes"]] == ["3"] assert all( t in "".join(split["flow"]) for t in [ "图像[0]", "阈值30", "余边", "tile.save", "RETURN_TYPES=()", "许可unknown", "转换仍关闭", ] ) assert all(t in "".join(grid["flow"]) for t in ["UE7.7", "不产prompt", "中心裁剪", "模型超分"]) assert all(b["counts"]["final_prompt_constructed"] is False for b in BLOCKS) assert len(grid["comments"]) == 1 and not split["comments"] def test_only_two_reviews_change_and_all_old_blocks_preserved(): resource = "calliope-backend/src/calliope/comfyui/source_reviews.json" old = json.loads(subprocess.check_output(["git", "show", "27e6d56:" + resource], cwd=REPO)) now = json.loads((REPO / resource).read_bytes()) multivideo = next(p for p in now if p.endswith("/多视频合成分屏展示.json")) now[multivideo].pop("fixed_version_document") watermark = next(p for p in now if p.endswith("/图像4种添加水印节点.json")) now[watermark].pop("fixed_version_document") birefnet = next(p for p in now if p.endswith("/birefnet_图像移除背景.json")) now[birefnet].pop("fixed_version_document") latent = next(p for p in now if p.endswith("/加载本地latent解码.json")) now[latent].pop("fixed_version_document") later = next(p for p in now if p.endswith("/截取视频每一帧.json")) now[later].pop("fixed_version_document") for added in [ p for p in now if p.endswith( ("视频转gif动图图片.json", "加载图片合成视频.json", "双图像或视频合并显示.json") ) ]: now[added].pop("fixed_version_document") reverse = next(p for p in now if p.endswith("/视频倒放.json")) now[reverse].pop("fixed_version_document") nlf = next(p for p in now if p.endswith("NLF_视频人物动作骨架获取.json")) now[nlf].pop("fixed_version_document") openpose = next(p for p in now if p.endswith("openpose-video-姿态检测.json")) now[openpose].pop("fixed_version_document") # SDPose is independently frozen against 7291887 in its own suite. sdpose = next(p for p in now if p.endswith("sdpose_视频人物姿势骨架提取.json")) now[sdpose].pop("fixed_version_document") # Gemma addition is independently protected against 9dac1b4 in its own suite. gemma = next(p for p in now if p.endswith("gemma4_提示词反推(图像视频音频).json")) now[gemma].pop("fixed_version_document") assert len(DEFINITIONS) == 26 assert ( sum( len(r["fixed_version_document"]["fields"]) for r in now.values() if "fixed_version_document" in r ) == 351 ) for path in PATHS: assert "fixed_version_document" not in old[path] now[path].pop("fixed_version_document") assert now == old assert len([r for r in old.values() if "fixed_version_document" in r]) == 11 @pytest.mark.parametrize("i", range(2)) def test_exact_raw_identity_and_saved_field_layout(i): raw = (REPO / PATHS[i]).read_bytes() b = BLOCKS[i] assert b["source_sha1"] == hashlib.sha1(raw).hexdigest() assert b["source_sha256"] == hashlib.sha256(raw).hexdigest() assert b["canonical_sha256"] == hashlib.sha256(canonical(SOURCES[i])).hexdigest() layout = {} for f in b["fields"]: layout.setdefault(f["node_id"], []).append(f["field"]) if i == 0: assert layout == { "12": ["image", "upload"], "13": ["image", "upload"], "14": ["image", "upload"], "15": ["image", "upload"], "16": ["direction", "match_image_size", "spacing_width", "spacing_color"], "17": ["direction", "match_image_size", "spacing_width", "spacing_color"], "18": ["direction", "match_image_size", "spacing_width", "spacing_color"], "21": [ "resize_type", "resize_type.width", "resize_type.height", "resize_type.crop", "scale_method", ], "19": ["filename_prefix"], } assert {e["commit"] for e in b["sources"]} == { "63d1bbdb407c69370d407ce5ced6ca3f917528a8", "f2254852b2a575b30cc1cde46742a89afbfac252", } else: assert layout == { "1": ["image", "upload"], "2": [ "保存目录", "水平张数", "垂直张数", "移除画布边缘", "移除描边", "文件名前缀", "保存格式", ], } assert all("参考假设" in f["execution_note"] for f in b["fields"]) assert {e["commit"] for e in b["sources"]} == { "26515acd23fa291a8f5ab53c5997258598de0701", "885cac3dd323d26a77b69ae904a9a344f0dee7d0", "f2254852b2a575b30cc1cde46742a89afbfac252", }