"""Unversioned Llama source stays reference-only, including saved history and conflicts.""" import copy import hashlib import json import runpy 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/llama-document-evidence" PATH = next(p for p in DEFINITIONS if p.endswith("/Llama-cpp+Qwen3.5-提示词.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("/"): obj = obj[int(part)] if isinstance(obj, list) else obj[part] return obj def doc(s=SOURCE, r=REVIEW): return build_document(PATH, s, None, None, [], r) def refuse(s, r): r["fixed_version_document"]["canonical_sha256"] = hashlib.sha256(canonical(s)).hexdigest() assert not doc(s, r)["fixed_version"]["matched"] @pytest.mark.parametrize("f", BLOCK["fields"], ids=lambda f: f["node_id"] + "." + f["field"]) def test_all_33_positions_exact_types_and_separate_port_evidence(f): assert canonical(pointer(SOURCE, f["pointer"])) == canonical(f["value"]) assert type(pointer(SOURCE, f["pointer"])).__name__ == f["json_type"] assert not f["named_pointer"].startswith("/") and "named_value" not in f assert "假设" in f["effect"] and "未知" in f["execution_note"] if f["input_pointer"].startswith("/"): assert pointer(SOURCE, f["input_pointer"])["name"] == f["input_name"] else: assert f["field"] == "control_after_generate" 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", "definition"]) def test_caller_rehash_cannot_bypass_each_value_and_description(f, variant): s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW) if variant == "source": parent, _, key = f["pointer"].rpartition("/") pointer(s, parent)[int(key)] = "forged" else: r["fixed_version_document"]["fields"][BLOCK["fields"].index(f)]["effect"] = "forged" refuse(s, r) @pytest.mark.parametrize("ni", range(5)) @pytest.mark.parametrize("variant", ["mode", "identity", "named-values", "input-name"]) def test_all_nodes_bind_mode_absent_identity_and_ports(ni, variant): s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW) n = s["nodes"][ni] if variant == "mode": n["mode"] = 4 elif variant == "identity": n["properties"]["ver"] = "forged" elif variant == "named-values": n["widgets_values_named"] = {"seed": 0} else: n["inputs"][0]["name"] = "forged" refuse(s, r) @pytest.mark.parametrize("ei", range(4)) def test_each_physical_edge_is_bound(ei): s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW) s["links"][ei][3] = 99999 refuse(s, r) @pytest.mark.parametrize( "variant", ["overview", "citation", "source_sha", "missing", "empty", "frontend", "definition_digest"], ) def test_binding_and_missing_definitions_fail_closed(variant): s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW) if variant == "overview": r["fixed_version_document"]["user_overview"] = "forged" elif variant == "citation": r["fixed_version_document"]["sources"][0]["sha256"] = "forged" elif variant == "source_sha": r["source_sha"] = "forged" elif variant == "missing": r.pop("fixed_version_document") elif variant == "empty": r["fixed_version_document"] = {} elif variant == "frontend": s["extra"]["frontendVersion"] = "1.52.7" else: r["fixed_version_document"]["boundaries"] = [{"canonical_sha256": "forged"}] assert not doc(s, r)["fixed_version"]["matched"] @pytest.mark.parametrize("generated", [False, True]) def test_absent_identity_conflicting_schema_and_seed_layout_hypothesis(generated): b = runpy.run_path(str(OUT / "build_definition.py"))["build"]() if generated else BLOCK assert not any("frontendVersion" in n for n in SOURCE["nodes"]) assert all(not {"cnr_id", "aux_id", "ver"} & n["properties"].keys() for n in SOURCE["nodes"]) assert all("widgets_values_named" not in n for n in SOURCE["nodes"]) f = next(f for f in b["fields"] if f["field"] == "presence_penalty") assert f["value"] == 1 and "冲突" in f["effect"] and "未知" in f["schema_default"] tail = [f for f in b["fields"] if f["node_id"] == "2"][-4:] assert [f["value"] for f in tail] == [290079675294308, "randomize", False, False] assert [f["field"] for f in tail] == [ "seed", "control_after_generate", "force_offload", "save_states", ] assert not tail[1]["input_pointer"].startswith("/") assert all("假设" in f["effect"] for f in tail) assert "load_mtp" in "\n".join(b["flow"]) assert ( "images" in next(f for f in b["fields"] if f["field"] == "max_frames")["limits"] or "video" in next(f for f in b["fields"] if f["field"] == "max_frames")["unit"] ) assert all(e["commit"] != "71d2f2f2b5c6da9dee281836b5b72f3af368966f" for e in b["sources"]) assert not b["comments"] and not b["boundaries"] assert not b["counts"]["final_prompt_constructed"] def test_history_full_string_and_all_original_primitives_are_preserved(): f = next(f for f in BLOCK["fields"] if f["role"] == "history_text") assert len(f["value"]) == 456 and f["value"] == SOURCE["nodes"][0]["widgets_values"][0] assert "历史" in f["effect"] and "实跑" in f["effect"] assert any(f["value"] == "" and f["json_type"] == "str" for f in BLOCK["fields"]) assert ( len([f for f in BLOCK["fields"] if f["json_type"] == "bool" and f["value"] is False]) == 2 ) assert len([f for f in BLOCK["fields"] if f["value"] == -1]) == 2 assert doc()["fixed_version"]["matched"] def test_independent_inventory_and_38_frozen_objects(): inv = json.loads((OUT / "independent-inventory.json").read_bytes()) assert [f["pointer"] for f in inv["fields"]] == [f["pointer"] for f in BLOCK["fields"]] assert len(inv["fields"]) == 33 and not inv["notes"] resource = "calliope-backend/src/calliope/comfyui/source_reviews.json" old = json.loads( subprocess.check_output( ["git", "show", "92b7897ae2c394bb214d6a7fdec0c139b8f09c76:" + resource], cwd=REPO ) ) cur = json.loads((REPO / resource).read_bytes()) cur[PATH].pop("fixed_version_document") assert cur == old blocks = [r["fixed_version_document"] for r in old.values() if "fixed_version_document" in r] assert len(blocks) == 38 and sum(len(b["fields"]) for b in blocks) == 1348 assert ( sum( len( build_document(p, json.loads((REPO / p).read_bytes()), None, None, [], r)[ "source_notes" ] ) for p, r in old.items() if "fixed_version_document" in r ) == 41 )