"""Reverse source binding, all saved positions, nested history and old-seventeen preservation.""" 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/reverse-document-evidence" PATH = next(p for p in DEFINITIONS if p.endswith("/视频倒放.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, path): for part in path.strip("/").split("/"): part = part.replace("~1", "/").replace("~0", "~") obj = obj[int(part)] if isinstance(obj, list) else obj[part] return obj def set_value(obj, path, value): parent, _, key = path.rpartition("/") target = pointer(obj, parent) target[int(key) if isinstance(target, list) else key] = value def doc(source=None, review=None, path=PATH): return build_document( path, SOURCE if source is None else source, None, None, [], REVIEW if review is None else review, ) @pytest.mark.parametrize("field", BLOCK["fields"], ids=lambda f: f["node_id"] + "." + f["field"]) def test_value_pointer_json_type_and_citations(field): value = pointer(SOURCE, field["pointer"]) assert canonical(value) == canonical(field["value"]) assert type(value).__name__ == field["json_type"] n = pointer(SOURCE, field["pointer"].split("/widgets_values/")[0]) assert str(n["id"]) == field["node_id"] and n["type"] == field["class_type"] for k in ["label", "schema_default", "unit", "limits", "effect", "execution_note", "role"]: assert field[k] for k in field["evidence"]: e = BLOCK["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"]) or ( e["commit"].startswith("发布包SHA256:") and e["git_commit_binding"] == "unknown" ) assert 1 <= e["lines"][0] <= e["lines"][1] <= len(raw.decode().splitlines()) @pytest.mark.parametrize("index", range(21)) @pytest.mark.parametrize("kind", ["value", "type", "pointer", "citation", "role", "default"]) def test_forged_field_even_with_recalculated_digest_rejected(index, kind): source, review = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW) b = review["fixed_version_document"] f = b["fields"][index] if kind in ["value", "type"]: set_value(source, f["pointer"], {"forged": True} if kind == "value" else "forged-type") f["value"] = pointer(source, f["pointer"]) f["json_type"] = type(f["value"]).__name__ 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(source)).hexdigest() review["caller_digest"] = hashlib.sha256(canonical(b)).hexdigest() d = doc(source, review)["fixed_version"] assert not d["matched"] and "fields" not in d def leaves(obj, path): if isinstance(obj, dict): for k, v in obj.items(): yield from leaves(v, path + "/" + k) elif isinstance(obj, list): for i, v in enumerate(obj): yield from leaves(v, path + "/" + str(i)) else: yield path # Includes every physical edge endpoint, source version/UE/mode, nested previews, # and named input/output slot properties. No plugin is imported or evaluated. MUTATIONS = ( [f"/links/{i}/{j}" for i in range(4) for j in range(6)] + [p for p in leaves(SOURCE["extra"], "/extra")] + [f"/nodes/{i}/{key}" for i in range(4) for key in ["mode", "type"]] + [ p for i, n in enumerate(SOURCE["nodes"]) for key in ["properties", "inputs", "outputs"] for p in leaves(n.get(key), f"/nodes/{i}/{key}") ] + [ p for f in BLOCK["fields"] if isinstance(f["value"], dict) for p in leaves(f["value"], f["pointer"]) ] ) @pytest.mark.parametrize("path", MUTATIONS) def test_graph_mode_version_nested_preview_slot_mutation_fails_closed(path): source, review = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW) set_value(source, path, "forged") review["fixed_version_document"]["canonical_sha256"] = hashlib.sha256( canonical(source) ).hexdigest() review["caller_digest"] = hashlib.sha256( canonical(review["fixed_version_document"]) ).hexdigest() assert not doc(source, review)["fixed_version"]["matched"] @pytest.mark.parametrize("nid", [20, 15]) @pytest.mark.parametrize("value", [None, {}, "missing"]) def test_zero_lists_distinguished_from_missing_null_empty_object(nid, value): source = copy.deepcopy(SOURCE) n = next(n for n in source["nodes"] if n["id"] == nid) if value == "missing": del n["widgets_values"] else: n["widgets_values"] = value assert not doc(source)["fixed_version"]["matched"] @pytest.mark.parametrize( "key", [ "source_path", "source_sha1", "source_sha256", "notice", "user_overview", "versions", "comments", "flow", "sources", "counts", "zero_saved_nodes", ], ) def test_editorial_forgery_rejected(key): review = copy.deepcopy(REVIEW) review["fixed_version_document"][key] = "forged" review["caller_digest"] = hashlib.sha256( canonical(review["fixed_version_document"]) ).hexdigest() assert not doc(review=review)["fixed_version"]["matched"] def test_exact_graph_and_original_seventeen_unchanged(): d = doc() assert d["fixed_version"]["matched"] assert len(BLOCK["fields"]) == 21 and BLOCK["comments"] == [] assert sum(isinstance(f["value"], dict) for f in BLOCK["fields"]) == 2 nodes = {n["id"]: n for n in SOURCE["nodes"]} assert {k: len(n["widgets_values"]) for k, n in nodes.items()} == {20: 0, 21: 11, 1: 10, 15: 0} assert all(n["mode"] == 0 for n in nodes.values()) assert "frontendVersion" not in SOURCE["extra"] and "definitions" not in SOURCE assert SOURCE["links"] == [ [15, 1, 0, 20, 0, "IMAGE"], [16, 20, 0, 15, 0, "IMAGE"], [17, 20, 0, 21, 0, "IMAGE"], [18, 1, 2, 21, 1, "AUDIO"], ] for eid, origin, slot, target, inp, typ in SOURCE["links"]: assert eid in nodes[origin]["outputs"][slot]["links"] assert nodes[origin]["outputs"][slot]["type"] == typ assert nodes[target]["inputs"][inp]["link"] == eid assert nodes[target]["inputs"][inp]["type"] == typ fields = {(f["node_id"], f["field"]): f for f in BLOCK["fields"]} assert fields["1", "force_rate"]["value"] == fields["21", "frame_rate"]["value"] == 16 assert fields["1", "force_rate"]["json_type"] == "int" assert fields["1", "choose video to upload"]["role"] == "frontend_control" assert all( f["role"] == "frontend_history" for f in BLOCK["fields"] if f["field"] == "videopreview" ) text = "".join(BLOCK["flow"]) for token in [ "音频link18", "绕过Reverse20", "torch.flip(images,[0])", "没有frontendVersion", "-shortest", "RETURN_TYPES", "未接", "历史", "未知", "内存", "VHS1.7.7", ]: assert token in text assert not BLOCK["counts"]["final_prompt_constructed"] resource = "calliope-backend/src/calliope/comfyui/source_reviews.json" old = json.loads(subprocess.check_output(["git", "show", "ab18cd3:" + 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") for added in [ p for p in now if p.endswith( ( "视频转gif动图图片.json", "加载图片合成视频.json", "双图像或视频合并显示.json", "截取视频每一帧.json", "加载本地latent解码.json", ) ) ]: now[added].pop("fixed_version_document") assert now[PATH].pop("fixed_version_document") == BLOCK assert now == old and len(DEFINITIONS) == 26 blocks = [r["fixed_version_document"] for r in old.values() if "fixed_version_document" in r] assert len(blocks) == 17 and sum(len(b["fields"]) for b in blocks) == 495 assert ( sum(len(b.get("comments", [])) for b in blocks) == 14 ) # ASR historical note counted separately assert d["status"] == dict(imported=True, doc_done=False, exec_verified=False) assert d["dependencies"]["check_state"] == "unchecked" for p in DEFINITIONS: if p in [watermark, multivideo]: continue if p.endswith("/birefnet_图像移除背景.json"): continue if p.endswith( ( "视频转gif动图图片.json", "加载图片合成视频.json", "双图像或视频合并显示.json", "截取视频每一帧.json", "加载本地latent解码.json", ) ): continue if p == PATH: continue r = copy.deepcopy(REVIEW) r["fixed_version_document"] = old[p]["fixed_version_document"] assert not doc(review=r)["fixed_version"]["matched"] assert not doc(path="untrusted.json").get("fixed_version") def test_raw_source_sha_and_missing_packages(tmp_path): assert review_for(PATH, "0" * 40) is None review = copy.deepcopy(REVIEW) review["source_sha"] = "0" * 40 assert not doc(review=review)["fixed_version"]["matched"] import sys code = ( "import runpy,sys; from pathlib import Path; ns=r" "unpy.run_path('recompute.py'); ns['require_packa" "ges'].__globals__['REPO']=Path(sys.argv[1]); ns[" "'require_packages']()" ) r = subprocess.run( [sys.executable, "-c", code, str(tmp_path)], cwd=OUT, capture_output=True, text=True ) assert r.returncode != 0 and "缺少wheel/sdist包产物" in r.stderr assert "uv build --project calliope-backend --offline" in r.stderr @pytest.mark.parametrize("nid", [1, 21]) @pytest.mark.parametrize("value", [None, {}, [], "missing"]) def test_history_shape_is_source_bound(nid, value): source = copy.deepcopy(SOURCE) node = next(n for n in source["nodes"] if n["id"] == nid) if value == "missing": del node["widgets_values"]["videopreview"] else: node["widgets_values"]["videopreview"] = value assert not doc(source)["fixed_version"]["matched"] @pytest.mark.parametrize("nid,field", [(1, "force_rate"), (21, "frame_rate")]) def test_json_integer_rate_is_not_float(nid, field): source = copy.deepcopy(SOURCE) node = next(n for n in source["nodes"] if n["id"] == nid) node["widgets_values"][field] = 16.0 assert not doc(source)["fixed_version"]["matched"]