docs(HEL-892): bind scoped SCAIL2 workflow reference
Co-authored-by: multica-agent <github@multica.ai>
This commit is contained in:
@@ -0,0 +1,229 @@
|
||||
"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/scail-document-evidence")
|
||||
PATH = "example_ComfyUI_workflows/6-动作迁移/Wan2.1_SCAIL2_角色迁移_动作迁移_FOR循环长视频.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 f["named_state"] == "missing" and "named_pointer" not in f 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"]
|
||||
if raw:
|
||||
assert 1 <= e["lines"][0] <= e["lines"][1] <= len(raw.decode().splitlines())
|
||||
else:
|
||||
assert e["lines"] == [0, 0] and "零字节" in e["claim"]
|
||||
|
||||
|
||||
@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:
|
||||
selected_pointer = f["pointer"]
|
||||
p, _, key = selected_pointer.rpartition("/")
|
||||
parent = pointer(s, p)
|
||||
parent[int(key) if isinstance(parent, list) else 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", "definitions"])
|
||||
def test_whole_graph_mutations_fail_closed(key):
|
||||
source, review = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
|
||||
source[key] = {"subgraphs": []} if key == "definitions" else "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, 1, 4])
|
||||
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("key", ["properties", "inputs", "outputs", "mode"])
|
||||
@pytest.mark.parametrize(
|
||||
"node_pointer",
|
||||
[
|
||||
f"{pre}/nodes/{i}"
|
||||
for pre, g in [("", SOURCE)]
|
||||
+ [
|
||||
(f"/definitions/subgraphs/{j}", s)
|
||||
for j, s in enumerate(SOURCE["definitions"]["subgraphs"])
|
||||
]
|
||||
for i, _ in enumerate(g["nodes"])
|
||||
],
|
||||
)
|
||||
def test_control_identity_and_state_forgery_fail_closed(node_pointer, key):
|
||||
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
|
||||
pointer(s, node_pointer)[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.parent / "scail-document-evidence/build_definition.py"))["build"]()
|
||||
) == canonical(BLOCK)
|
||||
assert PATH in DEFINITIONS and len(DEFINITIONS) >= 140 and doc()["fixed_version"]["matched"]
|
||||
assert BLOCK["counts"]["saved_fields"] == 195 and len(BLOCK["comments"]) == 10
|
||||
assert len(BLOCK["boundaries"]) == 3 and len(BLOCK["zero_saved_nodes"]) == 32
|
||||
assert not doc()["status"]["doc_done"] and not doc()["status"]["exec_verified"]
|
||||
sys.path.insert(0, str(OUT.parent / "scail-document-evidence"))
|
||||
try:
|
||||
runpy.run_path(str(OUT.parent / "scail-document-evidence/verify_sources.py"))["main"]()
|
||||
finally:
|
||||
sys.path.remove(str(OUT.parent / "scail-document-evidence"))
|
||||
|
||||
|
||||
def test_raw_source_version_mismatch_is_fail_closed():
|
||||
source, review = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
|
||||
source["nodes"][1]["properties"]["ver"] = "other-plugin-version"
|
||||
review["fixed_version_document"]["canonical_sha256"] = hashlib.sha256(
|
||||
canonical(source)
|
||||
).hexdigest()
|
||||
assert not doc(source, review)["fixed_version"]["matched"]
|
||||
|
||||
|
||||
def test_old_reviews_and_qualification_are_unchanged():
|
||||
import subprocess
|
||||
|
||||
name = "calliope-backend/src/calliope/comfyui/source_reviews.json"
|
||||
previous = json.loads(
|
||||
subprocess.check_output(
|
||||
["git", "show", "3f6373062fa7f0c9417e6c8a73ba9783ed7412ef:" + name], cwd=REPO
|
||||
)
|
||||
)
|
||||
current = json.loads((REPO / name).read_bytes())
|
||||
for path, review in previous.items():
|
||||
if path != PATH:
|
||||
assert current[path] == review
|
||||
else:
|
||||
current_without = dict(current[path])
|
||||
current_without.pop("fixed_version_document")
|
||||
assert current_without == review
|
||||
old = [r["fixed_version_document"] for r in previous.values() if "fixed_version_document" in r]
|
||||
assert len(old) == 139 and sum(len(b["fields"]) for b in old) == 8627
|
||||
|
||||
|
||||
def test_exact_classes_and_concrete_protocol_gaps():
|
||||
assert len(BLOCK["node_bindings"]) == 139
|
||||
assert sum(b["implementation_verified"] for b in BLOCK["node_bindings"]) == 111
|
||||
assert {
|
||||
b["node_id"] for b in BLOCK["node_bindings"] if b["source_id"].startswith("unresolved-")
|
||||
} == {"213:197", "262:245"}
|
||||
assert sum(b["source_id"] == "unknown-no-version" for b in BLOCK["node_bindings"]) == 23
|
||||
assert sum(b["source_id"] == "subgraph-definition" for b in BLOCK["node_bindings"]) == 3
|
||||
assert sum(f["json_type"] == "NoneType" for f in BLOCK["fields"]) == 6
|
||||
assert sum(len(b["proxy_mappings"]) for b in BLOCK["boundaries"]) == 48
|
||||
previews = [p for b in BLOCK["boundaries"] for p in b["proxy_mappings"] if p["preview_only"]]
|
||||
assert len(previews) == 4 and all(
|
||||
"saved_value" not in p and "saved_pointer" not in p for p in previews
|
||||
)
|
||||
assert (
|
||||
BLOCK["counts"]["root_saved_fields"] == 39
|
||||
and BLOCK["counts"]["internal_saved_fields"] == 156
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"host,slot",
|
||||
[(b["host_node_id"], i) for b in BLOCK["boundaries"] for i, _ in enumerate(b["proxyWidgets"])],
|
||||
)
|
||||
def test_caller_rehash_cannot_replace_real_proxy_mapping(host, slot):
|
||||
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
|
||||
next(n for n in s["nodes"] if str(n["id"]) == host)["properties"]["proxyWidgets"][slot] = [
|
||||
"forged",
|
||||
"widget",
|
||||
]
|
||||
r["fixed_version_document"]["canonical_sha256"] = hashlib.sha256(canonical(s)).hexdigest()
|
||||
assert not doc(s, r)["fixed_version"]["matched"]
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"key", ["named_value", "named_pointer", "schema_default", "unit", "limits"]
|
||||
)
|
||||
def test_guessed_data_cannot_expand_trust(key):
|
||||
r = copy.deepcopy(REVIEW)
|
||||
r["fixed_version_document"]["fields"][0][key] = "forged"
|
||||
assert not doc(SOURCE, r)["fixed_version"]["matched"]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("note", BLOCK["comments"], ids=lambda n: n["node_id"])
|
||||
def test_complete_notes_are_preserved_and_pinned(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"]
|
||||
Reference in New Issue
Block a user