Files
aigc-home/calliope-backend/scripts/audit_reference_conversion.py
T

250 lines
11 KiB
Python

"""Compare bounded public-reference behavior to six supplied APIs, never mark ready.
PYTHONPATH=src python scripts/audit_reference_conversion.py --repo .. --output report.json
The report intentionally includes only supported fields/legacy links, alongside
whole-graph blockers. It is not a substitute for compare_frontend_exports.py's
source/version binding checks or a target node snapshot.
"""
from __future__ import annotations
import argparse
import hashlib
import json
from pathlib import Path
from compare_frontend_exports import differences
from calliope.comfyui.companions import load_companions
from calliope.comfyui.delivery_scope import exclusion_for
from calliope.comfyui.serialization_reference import (
AUX_PROFILE,
PREVIEW_PROFILE,
PRIMITIVE_PROFILE,
PROFILE,
resolve_reference_link,
serialize_promoted_node,
serialize_reference_widgets,
)
from calliope.comfyui.structure_reference import PROMOTED_PROFILES, STRUCTURE_PROFILES
from calliope.comfyui.ui2api import convert, convert_reference
def audit(repo: Path, profile: str = PROFILE) -> dict:
rows = []
records = load_companions(repo / "docs/hel-892/supplied-api/manifest.json")
for path, record in records.items():
binding = record["binding"]
if exclusion_for(path):
continue
raw = (repo / path).read_bytes()
assert hashlib.sha1(raw).hexdigest() == binding["source_sha"]
assert hashlib.sha256(raw).hexdigest() == binding["source_sha256"]
source, api = json.loads(raw), json.loads(record["raw"])
assert convert(api).prompt is not None
selected = profile
if profile in ("structure-batch-reference", "promoted-batch-reference"):
version = source.get("extra", {}).get("frontendVersion")
structural = f"comfy-frontend-{version}-structure-reference"
selected = (
structural
if (source.get("definitions") or {}).get("subgraphs")
and structural in STRUCTURE_PROFILES
else PRIMITIVE_PROFILE
if any(n.get("type") == "PrimitiveNode" for n in source["nodes"])
else AUX_PROFILE
)
if profile == "promoted-batch-reference":
selected = (
selected.replace("-structure-reference", "-promoted-reference")
if selected.replace("-structure-reference", "-promoted-reference")
in PROMOTED_PROFILES
else PREVIEW_PROFILE
if selected == PRIMITIVE_PROFILE
else selected
)
candidate = convert_reference(source, selected)
projected = candidate.diagnostic_graph or source
nodes = {str(n["id"]): n for n in projected["nodes"]}
links = {
str(link[0]): link
for link in projected.get("links", [])
if isinstance(link, list) and len(link) == 6
}
fields, edges, unsupported, metadata, node_types, api_only = [], [], [], [], [], []
for nid, node in nodes.items():
if node.get("type") in ("Reroute", "Note", "MarkdownNote"):
continue
try:
values = (
serialize_promoted_node(node, PROMOTED_PROFILES[selected])
if selected in PROMOTED_PROFILES
else serialize_reference_widgets(node, selected)
)
for boundary in candidate.resolved_boundaries:
if boundary["node_id"] == nid:
values[boundary["input_name"]] = boundary["value"]
except ValueError as exc:
unsupported.append({"node_id": nid, "type": node["type"], "reason": str(exc)})
else:
for name, value in values.items():
expected = api.get(nid, {}).get("inputs", {}).get(name)
fields.append(
{
"node_id": nid,
"field": name,
"source_reference": value,
"api": expected,
"differences": (
differences({}, {name: value})
if selected in (PREVIEW_PROFILE, *PROMOTED_PROFILES)
and name not in api.get(nid, {}).get("inputs", {})
else differences(expected, value)
),
}
)
linked = {s["name"] for s in node.get("inputs", []) if s.get("link") is not None}
api_only.extend(
{"node_id": nid, "field": name, "api_value": value}
for name, value in api.get(nid, {}).get("inputs", {}).items()
if name not in values and name not in linked
)
metadata.append(
{
"node_id": nid,
"differences": differences(
api.get(nid, {}).get("_meta"), {"title": node.get("title") or node["type"]}
),
}
)
node_types.append(
{
"node_id": nid,
"source_type": node["type"],
"api_type": api.get(nid, {}).get("class_type"),
"differences": differences(api.get(nid, {}).get("class_type"), node["type"]),
}
)
for socket in node.get("inputs", []):
lid = socket.get("link")
if lid is None:
continue
try:
endpoint = resolve_reference_link(nodes, links, lid)
except (ValueError, KeyError, TypeError, AttributeError) as exc:
edges.append(
{
"node_id": nid,
"input": socket["name"],
"status": "unsupported",
"reason": str(exc),
}
)
continue
expected = api.get(nid, {}).get("inputs", {}).get(socket["name"])
edges.append(
{
"node_id": nid,
"input": socket["name"],
"via_reroute": nodes[str(links[str(lid)][1])]["type"] == "Reroute",
"source_reference": endpoint,
"api": expected,
"differences": differences(expected, endpoint),
}
)
rows.append(
{
"id": binding["id"],
"selected_profile": selected,
"source_path": path,
"source_sha256": binding["source_sha256"],
"api_sha256": binding["export_sha256"],
"source_frontend_version": source.get("extra", {}).get("frontendVersion"),
"export_frontend_version": binding["frontend_version"],
"whole_prompt_available": candidate.prompt is not None,
"candidate_prompt": candidate.prompt
if profile == "promoted-batch-reference"
else None,
"whole_prompt_differences": differences(api, candidate.prompt)
if candidate.prompt is not None
else None,
"whole_prompt_issues": candidate.issues,
"production_prompt_available": convert(source).prompt is not None,
"serialized_fields": fields,
"topology_edges": edges,
"unsupported_widget_nodes": unsupported,
"diagnostic_graph": candidate.diagnostic_graph,
"unresolved_boundaries": candidate.unresolved_boundaries,
"resolved_boundaries": [
{
**v,
"api": api.get(v["node_id"], {}).get("inputs", {}).get(v["input_name"]),
"differences": differences(
api.get(v["node_id"], {}).get("inputs", {}).get(v["input_name"]),
v["value"],
),
}
for v in candidate.resolved_boundaries
],
"node_types": node_types,
"metadata": metadata,
"api_only_fields_for_supported_nodes": api_only,
"comparison_is_complete": candidate.prompt is not None,
"executable": False,
"execution_verified": False,
}
)
return {
"profile": profile,
"authority": "public_reference_only_not_object_info",
"comparison_scope": "explicit supported scalar widgets and projected endpoints only; "
"diagnostic structures never prove widget values, full equivalence or execution",
"reference_conditions": {
"unmodified_frontend_widgets_assumed": True,
"promoted_named_positional": "1.53.6 requires both representations to agree when named values exist; 1.48.7 ignores unknown named keys",
"promoted_missing_or_null": "no silent default; typed scalar consumption rejects null",
"primitive_inside_promoted_profile": "unsupported; whole prompt remains closed",
"combo_availability": "not evaluated; fixed scalar constructor only",
"ue_extension_serializer_match": "unknown",
"aux_release_artifact_commit_match": "unknown"
if profile == AUX_PROFILE
else "not_used",
"aio_combo_membership": "unknown_not_evaluated",
"model_and_media_files_available": "unknown",
"resolution_bounds": "schema metadata only; integer values serialized without clamping",
},
"target_version_match": "unknown",
"whole_source_equivalence_verified": False,
"execution_verified": False,
"cases": rows,
}
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--repo", type=Path, required=True)
parser.add_argument("--output", type=Path, required=True)
parser.add_argument(
"--profile",
choices=(
PROFILE,
AUX_PROFILE,
PRIMITIVE_PROFILE,
PREVIEW_PROFILE,
*STRUCTURE_PROFILES,
*PROMOTED_PROFILES,
"promoted-batch-reference",
"structure-batch-reference",
),
default=PROFILE,
)
args = parser.parse_args()
report = audit(args.repo, args.profile)
args.output.parent.mkdir(parents=True, exist_ok=True)
args.output.write_text(json.dumps(report, ensure_ascii=False, indent=2) + "\n")
print(json.dumps({"reference_only": True, "active_cases": len(report["cases"])}))
if __name__ == "__main__":
main()