Preserve Second Pass audio and scoped Review conditioning, strengthen Take compatibility, and align Review Driving Audio. Keep v5 history readable, document v6 restart requirements, and describe the fixes at the top of both READMEs. Remove workstation-specific paths and private operational records from public documentation.
211 lines
7.8 KiB
Python
211 lines
7.8 KiB
Python
"""Deterministic fingerprints for arbitrary ComfyUI upstream loader graphs."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import hashlib
|
|
import json
|
|
from typing import Any
|
|
|
|
|
|
GRAPH_CONTRACT_VERSION = 4
|
|
|
|
CHECKPOINT_REF2VA = "ref2va"
|
|
CHECKPOINT_FL2VA = "fl2va"
|
|
CHECKPOINT_UNKNOWN = "unknown"
|
|
|
|
|
|
def _canonical(value: Any) -> str:
|
|
return json.dumps(value, sort_keys=True, separators=(",", ":"), ensure_ascii=True)
|
|
|
|
|
|
def _sha256(value: Any) -> str:
|
|
return hashlib.sha256(_canonical(value).encode("utf-8")).hexdigest()
|
|
|
|
|
|
def _literal(value: Any) -> tuple[Any, bool]:
|
|
if value is None or isinstance(value, (bool, int, float, str)):
|
|
return value, True
|
|
if isinstance(value, (list, tuple)):
|
|
result = []
|
|
safe = True
|
|
for item in value:
|
|
rendered, item_safe = _literal(item)
|
|
result.append(rendered)
|
|
safe = safe and item_safe
|
|
return result, safe
|
|
if isinstance(value, dict):
|
|
result = {}
|
|
safe = True
|
|
for key in sorted(value, key=str):
|
|
rendered, item_safe = _literal(value[key])
|
|
result[str(key)] = rendered
|
|
safe = safe and item_safe
|
|
return result, safe
|
|
return {"type": f"{type(value).__module__}.{type(value).__qualname__}"}, False
|
|
|
|
|
|
def _node(prompt: dict[str, Any], node_id: str, *, stack: set[str]) -> tuple[dict[str, Any], bool, list[str]]:
|
|
if node_id in stack:
|
|
return {"node_id": node_id, "error": "cycle"}, False, [f"upstream graph contains a cycle at node {node_id}"]
|
|
raw = prompt.get(str(node_id))
|
|
if not isinstance(raw, dict):
|
|
return {"node_id": node_id, "error": "missing"}, False, [f"upstream node {node_id} is missing"]
|
|
class_type = str(raw.get("class_type", ""))
|
|
safe = bool(class_type)
|
|
reasons = [] if safe else [f"upstream node {node_id} has no class_type"]
|
|
inputs = raw.get("inputs") or {}
|
|
if not isinstance(inputs, dict):
|
|
return {"node_id": node_id, "class_type": class_type, "error": "invalid inputs"}, False, reasons + [f"upstream node {node_id} inputs are invalid"]
|
|
rendered_inputs: dict[str, Any] = {}
|
|
next_stack = set(stack)
|
|
next_stack.add(node_id)
|
|
for name in sorted(inputs):
|
|
value = inputs[name]
|
|
if (
|
|
isinstance(value, (list, tuple))
|
|
and len(value) == 2
|
|
and isinstance(value[0], (str, int))
|
|
and isinstance(value[1], int)
|
|
and str(value[0]) in prompt
|
|
):
|
|
child, child_safe, child_reasons = _node(
|
|
prompt, str(value[0]), stack=next_stack
|
|
)
|
|
rendered_inputs[str(name)] = {"output": int(value[1]), "node": child}
|
|
safe = safe and child_safe
|
|
reasons.extend(child_reasons)
|
|
else:
|
|
rendered, literal_safe = _literal(value)
|
|
rendered_inputs[str(name)] = rendered
|
|
safe = safe and literal_safe
|
|
if not literal_safe:
|
|
reasons.append(f"upstream node {node_id}.{name} is not serializable")
|
|
return {
|
|
"node_id": str(node_id),
|
|
"class_type": class_type,
|
|
"inputs": rendered_inputs,
|
|
}, safe, reasons
|
|
|
|
|
|
def build_upstream_graph_contract(
|
|
prompt: Any, unique_id: Any, *, require_video_vae: bool,
|
|
require_reference_audio_vae: bool = False,
|
|
require_audio_vae: bool = False,
|
|
reference_audio_route: str | None = None,
|
|
) -> tuple[dict[str, Any], bool, list[str]]:
|
|
"""Fingerprint MODEL/CLIP/VAE/SAMPLER routes feeding Continuum."""
|
|
if not isinstance(prompt, dict):
|
|
return {"version": GRAPH_CONTRACT_VERSION}, False, ["ComfyUI PROMPT graph is unavailable"]
|
|
node_id = str(unique_id)
|
|
sampler_node = prompt.get(node_id)
|
|
if not isinstance(sampler_node, dict):
|
|
return {"version": GRAPH_CONTRACT_VERSION}, False, ["Continuum sampler node is absent from PROMPT graph"]
|
|
inputs = sampler_node.get("inputs") or {}
|
|
routes: dict[str, Any] = {}
|
|
safe = True
|
|
reasons: list[str] = []
|
|
specs = [
|
|
("model", "model", True),
|
|
("clip", "clip", True),
|
|
("sampler", "sampler", True),
|
|
("video_vae", "video_vae", bool(require_video_vae)),
|
|
]
|
|
if require_reference_audio_vae:
|
|
if reference_audio_route is None:
|
|
bundle_link = inputs.get("audio_references")
|
|
reference_audio_route = "audio_references" if (
|
|
isinstance(bundle_link, (list, tuple))
|
|
and len(bundle_link) == 2
|
|
and isinstance(bundle_link[0], (str, int))
|
|
and isinstance(bundle_link[1], int)
|
|
) else "reference_audio_vae"
|
|
# Keep the established descriptor key. The bundled route fingerprints
|
|
# the helper and therefore its Audio VAE plus every ordered source.
|
|
specs.append(("reference_audio_vae", reference_audio_route, True))
|
|
if require_audio_vae:
|
|
specs.append(("audio_vae", "audio_vae", True))
|
|
for name, input_name, required in specs:
|
|
if not required:
|
|
continue
|
|
link = inputs.get(input_name)
|
|
if not (
|
|
isinstance(link, (list, tuple))
|
|
and len(link) == 2
|
|
and isinstance(link[0], (str, int))
|
|
and isinstance(link[1], int)
|
|
):
|
|
routes[name] = {"error": "input is not connected"}
|
|
safe = False
|
|
reasons.append(f"{name} upstream connection is unavailable")
|
|
continue
|
|
route, route_safe, route_reasons = _node(
|
|
prompt, str(link[0]), stack={node_id}
|
|
)
|
|
routes[name] = {"output": int(link[1]), "node": route}
|
|
safe = safe and route_safe
|
|
reasons.extend(route_reasons)
|
|
descriptor = {
|
|
"version": GRAPH_CONTRACT_VERSION,
|
|
"sampler_class_type": str(sampler_node.get("class_type", "")),
|
|
"routes": routes,
|
|
}
|
|
descriptor["sha256"] = _sha256(descriptor)
|
|
return descriptor, safe, list(dict.fromkeys(reasons))
|
|
|
|
|
|
def classify_h3_checkpoint(prompt: Any, unique_id: Any) -> str:
|
|
"""Classify the base H3 UNET connected to the sampler, without guessing."""
|
|
if not isinstance(prompt, dict):
|
|
return CHECKPOINT_UNKNOWN
|
|
sampler_node = prompt.get(str(unique_id))
|
|
if not isinstance(sampler_node, dict):
|
|
return CHECKPOINT_UNKNOWN
|
|
model_link = (sampler_node.get("inputs") or {}).get("model")
|
|
if not (
|
|
isinstance(model_link, (list, tuple))
|
|
and len(model_link) == 2
|
|
and isinstance(model_link[0], (str, int))
|
|
):
|
|
return CHECKPOINT_UNKNOWN
|
|
|
|
stack = [str(model_link[0])]
|
|
visited: set[str] = set()
|
|
classifications: set[str] = set()
|
|
while stack:
|
|
node_id = stack.pop()
|
|
if node_id in visited:
|
|
continue
|
|
visited.add(node_id)
|
|
node = prompt.get(node_id)
|
|
if not isinstance(node, dict):
|
|
return CHECKPOINT_UNKNOWN
|
|
inputs = node.get("inputs") or {}
|
|
if str(node.get("class_type", "")) == "UNETLoader":
|
|
loader_text = " ".join(
|
|
str(value).lower()
|
|
for value in inputs.values()
|
|
if isinstance(value, str)
|
|
)
|
|
if "ref2va" in loader_text or "ref2v" in loader_text:
|
|
classifications.add(CHECKPOINT_REF2VA)
|
|
elif "fl2va" in loader_text or "fl2v" in loader_text:
|
|
classifications.add(CHECKPOINT_FL2VA)
|
|
else:
|
|
classifications.add(CHECKPOINT_UNKNOWN)
|
|
continue
|
|
linked = False
|
|
for value in inputs.values():
|
|
if (
|
|
isinstance(value, (list, tuple))
|
|
and len(value) == 2
|
|
and isinstance(value[0], (str, int))
|
|
and str(value[0]) in prompt
|
|
):
|
|
stack.append(str(value[0]))
|
|
linked = True
|
|
if classifications == {CHECKPOINT_REF2VA}:
|
|
return CHECKPOINT_REF2VA
|
|
if classifications == {CHECKPOINT_FL2VA}:
|
|
return CHECKPOINT_FL2VA
|
|
return CHECKPOINT_UNKNOWN
|