HEL-892: pin core0.33 first-group saved-state contracts and fail-closed diagnostics
Co-authored-by: multica-agent <github@multica.ai>
This commit is contained in:
@@ -0,0 +1,246 @@
|
||||
"""Six core0.33.0 saved-state contracts, under explicit frontend1.53.10 assumptions.
|
||||
|
||||
Sources: docs/hel-892/h3-core-reference/sources.json. Never imports upstream,
|
||||
evaluates expressions, computes dimensions or inspects a target installation.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
from copy import deepcopy
|
||||
from string import ascii_lowercase
|
||||
|
||||
CORE_PROFILE = "comfy-frontend-1.53.10-core-0.33.0-h3-first-group-reference"
|
||||
CORE_COMMIT = "2f35f4a08176d993cded35dac3332be4f7287f41"
|
||||
LAYOUTS = {
|
||||
"PrimitiveStringMultiline": ("value",),
|
||||
"ResolutionSelector": ("aspect_ratio", "megapixels", "multiple"),
|
||||
"ComfyMathExpression": ("expression",),
|
||||
"PrimitiveFloat": ("value",),
|
||||
"RandomNoise": ("noise_seed", "control_after_generate"),
|
||||
"BasicScheduler": ("scheduler", "steps", "denoise"),
|
||||
}
|
||||
RATIOS = (
|
||||
"1:1 (Square)",
|
||||
"2:3 (Portrait Photo)",
|
||||
"3:2 (Photo)",
|
||||
"3:4 (Portrait Standard)",
|
||||
"4:3 (Standard)",
|
||||
"9:16 (Portrait Widescreen)",
|
||||
"16:9 (Widescreen)",
|
||||
"21:9 (Ultrawide)",
|
||||
)
|
||||
SCHEDULERS = (
|
||||
"simple",
|
||||
"sgm_uniform",
|
||||
"karras",
|
||||
"exponential",
|
||||
"ddim_uniform",
|
||||
"beta",
|
||||
"normal",
|
||||
"linear_quadratic",
|
||||
"kl_optimal",
|
||||
)
|
||||
NUMERIC_UNION = "FLOAT,INT,BOOLEAN"
|
||||
OVERRIDES = (
|
||||
"ue_properties",
|
||||
"widget_ue_connectable",
|
||||
"input_ue_unconnectable",
|
||||
"ue_links",
|
||||
"links_added_by_ue",
|
||||
"widgets",
|
||||
"serializeValue",
|
||||
"comfyDynamic",
|
||||
"serialize_widgets",
|
||||
"fallbackWidgetsValuesNames",
|
||||
"comfyClass",
|
||||
"onNodeCreated",
|
||||
"onConnectInput",
|
||||
"onConnectionsChange",
|
||||
"onInputAdded",
|
||||
"onConfigure",
|
||||
"onSerialize",
|
||||
"getInputLink",
|
||||
"resolveInput",
|
||||
"resolveOutput",
|
||||
"onExecute",
|
||||
"isVirtualNode",
|
||||
"applyToGraph",
|
||||
"beforeQueued",
|
||||
"afterQueued",
|
||||
)
|
||||
|
||||
|
||||
def definitions():
|
||||
"""Public schemas only; no injected test helpers in this class set."""
|
||||
|
||||
def spec(required, outputs, optional=None):
|
||||
return {
|
||||
"input": {
|
||||
"required": {k: [v] for k, v in required.items()},
|
||||
"optional": {k: [v] for k, v in (optional or {}).items()},
|
||||
},
|
||||
"output": outputs,
|
||||
}
|
||||
|
||||
return {
|
||||
"PrimitiveStringMultiline": spec({"value": "STRING"}, ["STRING"]),
|
||||
"ResolutionSelector": spec(
|
||||
{"aspect_ratio": "COMBO", "megapixels": "FLOAT", "multiple": "INT"},
|
||||
["INT", "INT"],
|
||||
),
|
||||
"ComfyMathExpression": spec(
|
||||
{"expression": "STRING", "values.a": NUMERIC_UNION},
|
||||
["FLOAT", "INT", "BOOLEAN"],
|
||||
{f"values.{c}": NUMERIC_UNION for c in ascii_lowercase[1:]},
|
||||
),
|
||||
"PrimitiveFloat": spec({"value": "FLOAT"}, ["FLOAT"]),
|
||||
"RandomNoise": spec({"noise_seed": "INT"}, ["NOISE"]),
|
||||
"BasicScheduler": spec(
|
||||
{"model": "MODEL", "scheduler": "COMBO", "steps": "INT", "denoise": "FLOAT"},
|
||||
["SIGMAS"],
|
||||
),
|
||||
}
|
||||
|
||||
|
||||
def check_overrides(container):
|
||||
if any(k in container for k in OVERRIDES):
|
||||
raise ValueError("第一组不解释UE或未知控件/恢复/执行覆盖")
|
||||
|
||||
|
||||
def saved_values(node):
|
||||
"""Decode only proved widget order; no graph/slot validity claim here."""
|
||||
ct, props = node.get("type"), node.get("properties") or {}
|
||||
if ct not in LAYOUTS:
|
||||
raise ValueError("第一组只覆盖六类core节点")
|
||||
for k, expected in (("cnr_id", "comfy-core"), ("ver", "0.33.0")):
|
||||
if k in props and props[k] != expected:
|
||||
raise ValueError("第一组提供方/core版本声明冲突;缺失仅为显式参考假设")
|
||||
if "aux_id" in props:
|
||||
raise ValueError("第一组不解释其它来源身份")
|
||||
if type(node.get("mode", 0)) is not int or node.get("mode", 0) != 0:
|
||||
raise ValueError("第一组仅正常mode=0")
|
||||
check_overrides(node)
|
||||
check_overrides(props)
|
||||
layout, values = LAYOUTS[ct], node.get("widgets_values")
|
||||
if not isinstance(values, list) or len(values) != len(layout):
|
||||
raise ValueError("第一组控件保存数组缺项/多项")
|
||||
if "widgets_values_named" in node:
|
||||
named = node["widgets_values_named"]
|
||||
if not isinstance(named, dict) or set(named) != set(layout):
|
||||
raise ValueError("第一组具名保存缺项/多项")
|
||||
if any(type(named[k]) is not type(v) or named[k] != v for k, v in zip(layout, values)):
|
||||
raise ValueError("第一组具名/位置恢复冲突")
|
||||
result = dict(zip(layout, values))
|
||||
|
||||
def number(name, kind, lo, hi):
|
||||
value = result[name]
|
||||
# Conservative typed intersection: never coerce bool, text or FLOAT into INT.
|
||||
valid_type = type(value) is int if kind == "INT" else type(value) in (int, float)
|
||||
if not valid_type or not lo <= value <= hi or not math.isfinite(value):
|
||||
raise ValueError(f"第一组{name}数值类型/范围不符")
|
||||
|
||||
if ct in ("PrimitiveStringMultiline", "ComfyMathExpression"):
|
||||
if type(values[0]) is not str:
|
||||
raise ValueError("第一组文本必须是原样字符串")
|
||||
elif ct == "ResolutionSelector":
|
||||
if result["aspect_ratio"] not in RATIOS:
|
||||
raise ValueError("第一组aspect_ratio枚举不符")
|
||||
number("megapixels", "FLOAT", 0.1, 16.0)
|
||||
number("multiple", "INT", 8, 128)
|
||||
elif ct == "PrimitiveFloat":
|
||||
# Core uses platform-dependent sys.maxsize. Accept only the conservative
|
||||
# 32/64-bit intersection; do not claim the target platform was observed.
|
||||
number("value", "FLOAT", -(2**31 - 1), 2**31 - 1)
|
||||
elif ct == "RandomNoise":
|
||||
# JS Number cannot safely retain arbitrary 64-bit integers. Intersect
|
||||
# core's uint64 bound with exact saved/restored numeric representation.
|
||||
number("noise_seed", "INT", 0, 2**53 - 1)
|
||||
if result.pop("control_after_generate") != "fixed":
|
||||
raise ValueError("第一组seed只接受fixed;排队变值控制未证明")
|
||||
elif ct == "BasicScheduler":
|
||||
if result["scheduler"] not in SCHEDULERS:
|
||||
raise ValueError("第一组scheduler枚举不符")
|
||||
number("steps", "INT", 1, 10000)
|
||||
number("denoise", "FLOAT", 0.0, 1.0)
|
||||
return result
|
||||
|
||||
|
||||
def serialize_node(node):
|
||||
result = saved_values(node)
|
||||
ct, spec = node["type"], definitions()[node["type"]]
|
||||
outputs = node.get("outputs", [])
|
||||
if [s.get("type") for s in outputs] != spec["output"]:
|
||||
raise ValueError("第一组输出槽数量/类型不符")
|
||||
expected_names = {
|
||||
"PrimitiveStringMultiline": ["STRING"],
|
||||
"ResolutionSelector": ["width", "height"],
|
||||
"ComfyMathExpression": ["FLOAT", "INT", "BOOL"],
|
||||
"PrimitiveFloat": ["FLOAT"],
|
||||
"RandomNoise": ["NOISE"],
|
||||
"BasicScheduler": ["SIGMAS"],
|
||||
}[ct]
|
||||
if [s.get("name") for s in outputs] != expected_names or any(
|
||||
"slot_index" in s and (type(s["slot_index"]) is not int or s["slot_index"] != i)
|
||||
for i, s in enumerate(outputs)
|
||||
):
|
||||
raise ValueError("第一组输出槽名字/slot_index不符")
|
||||
sockets = node.get("inputs", [])
|
||||
names = [s.get("name") for s in sockets]
|
||||
if len(names) != len(set(names)):
|
||||
raise ValueError("第一组重复输入槽")
|
||||
declared = {**spec["input"]["required"], **spec["input"]["optional"]}
|
||||
for s in sockets:
|
||||
name = s.get("name")
|
||||
if name not in declared or s.get("type") != declared[name][0]:
|
||||
raise ValueError("第一组输入槽/类型或动态布局未证明")
|
||||
if "widget" in s and s["widget"] != {"name": name}:
|
||||
raise ValueError("第一组控件输入绑定不符")
|
||||
if "slot_index" in s:
|
||||
raise ValueError("第一组输入槽额外slot_index未证明")
|
||||
if ct == "ComfyMathExpression":
|
||||
dynamic = [n for n in names if n != "expression"]
|
||||
if not 2 <= len(dynamic) <= 26 or dynamic != [
|
||||
f"values.{c}" for c in ascii_lowercase[: len(dynamic)]
|
||||
]:
|
||||
raise ValueError("第一组算式需已构造values.a/b连续动态槽;不恢复旧values占位")
|
||||
return result
|
||||
|
||||
|
||||
def validate_structure(source):
|
||||
"""Reuse direct root invariants without mutating absent source identity.
|
||||
|
||||
Only the proved numeric MultiType autogrow slots may specialize a link type.
|
||||
No unknown node obtains a schema through this validation.
|
||||
"""
|
||||
from .structure_reference import validate_grid_structure
|
||||
|
||||
declared = (source.get("extra") or {}).get("frontendVersion")
|
||||
if declared is not None and declared != "1.53.10":
|
||||
raise ValueError("第一组前端声明冲突;缺失仅显式采用1.53.10参考")
|
||||
for container in (source, source.get("extra") or {}):
|
||||
check_overrides(container)
|
||||
for node in source["nodes"]:
|
||||
check_overrides(node)
|
||||
check_overrides(node.get("properties") or {})
|
||||
if node.get("type") == "Reroute":
|
||||
raise ValueError("第一组不扩展虚拟节点")
|
||||
graph = deepcopy(source)
|
||||
graph.setdefault("extra", {})["frontendVersion"] = "1.53.10"
|
||||
nodes = {n["id"]: n for n in graph["nodes"]}
|
||||
for edge in graph.get("links", []):
|
||||
if not isinstance(edge, list) or len(edge) != 6:
|
||||
continue # Shared validator gives the precise structural rejection.
|
||||
target = nodes.get(edge[3], {})
|
||||
slot = edge[4]
|
||||
if type(slot) is not int or not 0 <= slot < len(target.get("inputs", [])):
|
||||
continue
|
||||
socket = target["inputs"][slot]
|
||||
if (
|
||||
target.get("type") == "ComfyMathExpression"
|
||||
and socket.get("name") in {f"values.{c}" for c in ascii_lowercase}
|
||||
and socket.get("type") == NUMERIC_UNION
|
||||
and edge[5] in NUMERIC_UNION.split(",")
|
||||
):
|
||||
socket["type"] = edge[5]
|
||||
validate_grid_structure(graph, frontend="1.53.10")
|
||||
@@ -14,6 +14,7 @@ import json
|
||||
import math
|
||||
from typing import Any
|
||||
|
||||
from .h3_core_reference import CORE_PROFILE
|
||||
from .structure_reference import (
|
||||
GRID_PROFILE,
|
||||
H3_PROFILE,
|
||||
@@ -488,6 +489,10 @@ def reference_definitions(profile: str = PROFILE) -> dict[str, Any]:
|
||||
"output": outputs,
|
||||
}
|
||||
|
||||
if profile == CORE_PROFILE:
|
||||
from .h3_core_reference import definitions
|
||||
|
||||
return definitions()
|
||||
if profile == H3_PROFILE:
|
||||
return h3_definitions()
|
||||
if profile == GRID_PROFILE:
|
||||
@@ -574,6 +579,10 @@ def reference_definitions(profile: str = PROFILE) -> dict[str, Any]:
|
||||
|
||||
def serialize_reference_widgets(node: dict[str, Any], profile: str = PROFILE) -> dict[str, Any]:
|
||||
"""Apply only the pinned identity serializers; reject any extra/missing value."""
|
||||
if profile == CORE_PROFILE:
|
||||
from .h3_core_reference import serialize_node
|
||||
|
||||
return serialize_node(node)
|
||||
if profile == H3_PROFILE:
|
||||
return serialize_h3_node(node)
|
||||
if profile == GRID_PROFILE:
|
||||
|
||||
@@ -13,6 +13,7 @@ from __future__ import annotations
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Any
|
||||
|
||||
from .h3_core_reference import CORE_PROFILE
|
||||
from .serialization_reference import (
|
||||
ASR_PROFILE,
|
||||
AUX_PROFILE,
|
||||
@@ -77,6 +78,7 @@ def convert_reference(source: Any, profile: str = PROFILE) -> Conversion:
|
||||
PROFILE,
|
||||
GRID_PROFILE,
|
||||
H3_PROFILE,
|
||||
CORE_PROFILE,
|
||||
ASR_PROFILE,
|
||||
*MEDIA_PROFILES,
|
||||
SEPARATION_PROFILE,
|
||||
@@ -101,6 +103,10 @@ def convert_reference(source: Any, profile: str = PROFILE) -> Conversion:
|
||||
True,
|
||||
)
|
||||
try:
|
||||
if profile == CORE_PROFILE:
|
||||
from .h3_core_reference import validate_structure
|
||||
|
||||
validate_structure(source)
|
||||
if profile == GRID_PROFILE:
|
||||
validate_grid_structure(source)
|
||||
if profile == H3_PROFILE:
|
||||
@@ -233,12 +239,13 @@ def convert_reference(source: Any, profile: str = PROFILE) -> Conversion:
|
||||
result.diagnostic_graph = source
|
||||
except (ValueError, TypeError, KeyError, AttributeError, IndexError) as exc:
|
||||
issue = {"code": "invalid_reference_contract", "message": str(exc)}
|
||||
if profile in (GRID_PROFILE, H3_PROFILE):
|
||||
if profile in (GRID_PROFILE, H3_PROFILE, CORE_PROFILE):
|
||||
issue["pointer"] = "/"
|
||||
return Conversion(None, [issue], True)
|
||||
result.reference_only = True
|
||||
if profile in (
|
||||
H3_PROFILE,
|
||||
CORE_PROFILE,
|
||||
GRID_PROFILE,
|
||||
ASR_PROFILE,
|
||||
*MEDIA_PROFILES,
|
||||
@@ -257,7 +264,7 @@ def _convert(
|
||||
|
||||
def fail(code: str, message: str, node_id: Any = "") -> None:
|
||||
issue = {"code": code, "message": message, "node_id": str(node_id)}
|
||||
if reference and reference_profile in (GRID_PROFILE, H3_PROFILE):
|
||||
if reference and reference_profile in (GRID_PROFILE, H3_PROFILE, CORE_PROFILE):
|
||||
issue["pointer"] = next(
|
||||
(
|
||||
f"/nodes/{i}"
|
||||
@@ -318,6 +325,7 @@ def _convert(
|
||||
CASE04_PROFILE: "1.53.6",
|
||||
GRID_PROFILE: "1.41.20",
|
||||
H3_PROFILE: "1.53.10",
|
||||
CORE_PROFILE: "1.53.10",
|
||||
**VHS_PROFILES,
|
||||
**SETGET_PROFILES,
|
||||
**VIDEOINFO_PROFILES,
|
||||
@@ -464,12 +472,24 @@ def _convert(
|
||||
expected = declared[name][0] if declared[name] else None
|
||||
actual = outputs[link[2]]
|
||||
# Custom unions/wildcards need frontend evidence; do not guess coercions.
|
||||
if expected != actual and not (
|
||||
reference
|
||||
and reference_profile in (PREVIEW_PROFILE, CASE04_PROFILE, ASR_PROFILE, H3_PROFILE)
|
||||
and ct == "PreviewAny"
|
||||
and name == "source"
|
||||
and expected == "*"
|
||||
if (
|
||||
expected != actual
|
||||
and not (
|
||||
reference
|
||||
and reference_profile == CORE_PROFILE
|
||||
and ct == "ComfyMathExpression"
|
||||
and name.startswith("values.")
|
||||
and actual in ("FLOAT", "INT", "BOOLEAN")
|
||||
and expected == "FLOAT,INT,BOOLEAN"
|
||||
)
|
||||
and not (
|
||||
reference
|
||||
and reference_profile
|
||||
in (PREVIEW_PROFILE, CASE04_PROFILE, ASR_PROFILE, H3_PROFILE)
|
||||
and ct == "PreviewAny"
|
||||
and name == "source"
|
||||
and expected == "*"
|
||||
)
|
||||
):
|
||||
fail(
|
||||
"link_type_unverified",
|
||||
|
||||
Reference in New Issue
Block a user