feat(HEL-892): audit library reference profiles and add bounded Qwen ASR contract
Co-authored-by: multica-agent <github@multica.ai>
This commit is contained in:
@@ -11,13 +11,18 @@ from __future__ import annotations
|
||||
import argparse
|
||||
import hashlib
|
||||
import json
|
||||
from collections import Counter, defaultdict
|
||||
from pathlib import Path
|
||||
|
||||
from compare_frontend_exports import differences
|
||||
try:
|
||||
from scripts.compare_frontend_exports import differences
|
||||
except ModuleNotFoundError:
|
||||
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 (
|
||||
ASR_PROFILE,
|
||||
AUX_PROFILE,
|
||||
CASE04_PROFILE,
|
||||
PREVIEW_PROFILE,
|
||||
@@ -269,13 +274,324 @@ def audit(repo: Path, profile: str = PROFILE) -> dict:
|
||||
}
|
||||
|
||||
|
||||
REFERENCE_PROFILES = (
|
||||
PROFILE,
|
||||
AUX_PROFILE,
|
||||
PRIMITIVE_PROFILE,
|
||||
PREVIEW_PROFILE,
|
||||
*STRUCTURE_PROFILES,
|
||||
*PROMOTED_PROFILES,
|
||||
CASE04_PROFILE,
|
||||
ASR_PROFILE,
|
||||
)
|
||||
|
||||
|
||||
def canonical_sha(value):
|
||||
"""SHA256 of UTF-8 JSON, sorted keys, compact separators, no ASCII escaping."""
|
||||
return hashlib.sha256(
|
||||
json.dumps(
|
||||
value,
|
||||
sort_keys=True,
|
||||
separators=(",", ":"),
|
||||
ensure_ascii=False,
|
||||
allow_nan=False,
|
||||
).encode("utf-8")
|
||||
).hexdigest()
|
||||
|
||||
|
||||
def node_inventory(source):
|
||||
rows = []
|
||||
graphs = [("", source)] + [
|
||||
(f"/definitions/subgraphs/{i}", graph)
|
||||
for i, graph in enumerate((source.get("definitions") or {}).get("subgraphs", []))
|
||||
]
|
||||
for prefix, graph in graphs:
|
||||
for i, node in enumerate(graph.get("nodes", [])):
|
||||
props = node.get("properties") or {}
|
||||
rows.append(
|
||||
{
|
||||
"pointer": f"{prefix}/nodes/{i}",
|
||||
"node_id": str(node.get("id")),
|
||||
"type": node.get("type"),
|
||||
"provider": props.get("cnr_id"),
|
||||
"version": props.get("ver"),
|
||||
"mode": node.get("mode", 0),
|
||||
"widget_count": len(node.get("widgets_values", [])),
|
||||
"widget_layout": type(node.get("widgets_values", [])).__name__,
|
||||
"ue_version": (props.get("ue_properties") or {}).get("version"),
|
||||
}
|
||||
)
|
||||
return rows
|
||||
|
||||
|
||||
def asr_field_evidence(source, prompt):
|
||||
"""Bind every final ASR-profile input to original bytes or fixed UI rules."""
|
||||
fields = []
|
||||
for index, node in enumerate(source["nodes"]):
|
||||
nid = str(node["id"])
|
||||
if nid not in prompt:
|
||||
continue
|
||||
names = {
|
||||
"AILab_Qwen3ASR": ("model", "precision", "language", "hints", "unload_models"),
|
||||
"LoadAudio": ("audio",),
|
||||
"PreviewAny": ("previewMode",),
|
||||
}[node["type"]]
|
||||
for name, value in prompt[nid]["inputs"].items():
|
||||
socket = next((s for s in node.get("inputs", []) if s["name"] == name), {})
|
||||
if socket.get("link") is not None:
|
||||
li, link = next(
|
||||
(i, link) for i, link in enumerate(source["links"]) if link[0] == socket["link"]
|
||||
)
|
||||
expected = [str(link[1]), link[2]]
|
||||
pointer, rule = f"/links/{li}", "upstream ID to string; original output slot"
|
||||
elif name in names:
|
||||
wi = names.index(name)
|
||||
expected = node["widgets_values"][wi]
|
||||
pointer, rule = f"/nodes/{index}/widgets_values/{wi}", "fixed constructor identity"
|
||||
else:
|
||||
assert name in ("audioUI", "preview")
|
||||
expected = ""
|
||||
pointer, rule = None, "1.37.11 DOM without getValue; options.serialize boundary"
|
||||
assert value == expected and type(value) is type(expected)
|
||||
fields.append(
|
||||
{
|
||||
"node_id": nid,
|
||||
"class_type": node["type"],
|
||||
"field": name,
|
||||
"value": value,
|
||||
"json_type": type(value).__name__,
|
||||
"source_pointer": pointer,
|
||||
"rule": rule,
|
||||
}
|
||||
)
|
||||
return fields
|
||||
|
||||
|
||||
def profile_attempt(source, profile):
|
||||
expected = {**STRUCTURE_PROFILES, **PROMOTED_PROFILES, CASE04_PROFILE: "1.53.6"}.get(
|
||||
profile, "1.37.11"
|
||||
)
|
||||
assumptions = {
|
||||
"frontend": expected,
|
||||
"unmodified_frontend_and_widgets": True,
|
||||
"target_version_match": "unknown",
|
||||
"ue_serializer_match": "unknown",
|
||||
"dynamic_options_and_files_available": "unknown",
|
||||
"qwen_commit": "b037d7be121111a1ba6f40a4d99f6b43ac6411b6"
|
||||
if profile in (ASR_PROFILE, PREVIEW_PROFILE, PRIMITIVE_PROFILE)
|
||||
else None,
|
||||
"qwen_missing_declaration": "explicit_reference_assumption_only",
|
||||
"aux_commit": "e8b689a513c3e6b63edc44066560ca5919c0576e"
|
||||
if profile == AUX_PROFILE
|
||||
else None,
|
||||
"aux_release_artifact_match": "unknown" if profile == AUX_PROFILE else "not_used",
|
||||
"reference_sources": [
|
||||
"serialization-evidence/sources.json",
|
||||
"structure-reference/sources.json",
|
||||
"controlnet-reference/sources.json",
|
||||
"promoted-reference/sources.json",
|
||||
"case04-reference/sources.json",
|
||||
],
|
||||
}
|
||||
base = {"profile": profile, "assumptions": assumptions}
|
||||
declared_frontend = (source.get("extra") or {}).get("frontendVersion")
|
||||
assumptions["source_frontend_declaration"] = declared_frontend
|
||||
assumptions["missing_frontend_policy"] = (
|
||||
"explicit_legacy_reference_assumption"
|
||||
if profile in (PROFILE, AUX_PROFILE, PRIMITIVE_PROFILE)
|
||||
else "requires_explicit_declaration"
|
||||
)
|
||||
assumptions["missing_node_declarations"] = [
|
||||
n["pointer"]
|
||||
for n in node_inventory(source)
|
||||
if n["type"] not in ("Note", "MarkdownNote", "Reroute", "PrimitiveNode")
|
||||
and (n["provider"] is None or n["version"] is None)
|
||||
]
|
||||
if declared_frontend != expected and not (
|
||||
declared_frontend is None and profile in (PROFILE, AUX_PROFILE, PRIMITIVE_PROFILE)
|
||||
):
|
||||
return {
|
||||
**base,
|
||||
"status": "not_applicable",
|
||||
"issues": [
|
||||
{
|
||||
"code": "frontend_not_applicable",
|
||||
"message": "前端声明缺失或版本不匹配",
|
||||
}
|
||||
],
|
||||
}
|
||||
result = convert_reference(source, profile)
|
||||
if result.prompt is None:
|
||||
return {
|
||||
**base,
|
||||
"status": "rejected",
|
||||
"rejection_class": "missing_contract"
|
||||
if any(i["code"] == "reference_node_unsupported" for i in result.issues)
|
||||
else "serializer_or_structure_rejected",
|
||||
"issues": result.issues,
|
||||
"unresolved_boundaries": result.unresolved_boundaries,
|
||||
}
|
||||
# Legacy core serializers explicitly assume absent declarations. Record
|
||||
# that assumption instead of implying that a type-name match proved it.
|
||||
return {
|
||||
**base,
|
||||
"status": "complete_reference_candidate",
|
||||
"issues": [],
|
||||
"serialization_stage": result.serialization_stage
|
||||
or "restored_saved_state_before_queue_hooks",
|
||||
"candidate_sha256": canonical_sha(result.prompt),
|
||||
"candidate_prompt": result.prompt,
|
||||
**(
|
||||
{"field_evidence": asr_field_evidence(source, result.prompt)}
|
||||
if profile == ASR_PROFILE
|
||||
else {}
|
||||
),
|
||||
}
|
||||
|
||||
|
||||
def audit_library(repo: Path, profiles=REFERENCE_PROFILES):
|
||||
"""Actually invoke every version-applicable profile; never choose first success.
|
||||
|
||||
The immutable manifest/SHA binding and delivery_scope define the partition.
|
||||
Candidates remain embedded in this isolated report, never the production DB.
|
||||
"""
|
||||
manifest = json.loads((repo / "docs/hel-892/offline/manifest.json").read_text())
|
||||
source_paths = {
|
||||
p.relative_to(repo).as_posix()
|
||||
for p in (repo / "example_ComfyUI_workflows").rglob("*")
|
||||
if p.is_file()
|
||||
}
|
||||
manifest_paths = [r["path"] for r in manifest]
|
||||
if len(manifest_paths) != len(set(manifest_paths)) or set(manifest_paths) != source_paths:
|
||||
raise ValueError("源目录与manifest集合不一致或路径重复")
|
||||
companions = load_companions(repo / "docs/hel-892/supplied-api/manifest.json")
|
||||
rows, excluded, api_paths, gap_groups = [], [], [], defaultdict(list)
|
||||
for record in manifest:
|
||||
path = record["path"]
|
||||
raw = (repo / path).read_bytes()
|
||||
if hashlib.sha1(raw).hexdigest() != record["source_sha"]:
|
||||
raise ValueError(f"源SHA与固定manifest不符:{path}")
|
||||
if exclusion_for(path):
|
||||
excluded.append(path)
|
||||
continue
|
||||
source = json.loads(raw)
|
||||
if "nodes" not in source:
|
||||
api_paths.append(path)
|
||||
continue
|
||||
inventory = node_inventory(source)
|
||||
attempts = [profile_attempt(source, profile) for profile in profiles]
|
||||
complete = [a for a in attempts if a["status"] == "complete_reference_candidate"]
|
||||
hashes = sorted({a["candidate_sha256"] for a in complete})
|
||||
status = (
|
||||
"candidate_conflict"
|
||||
if len(hashes) > 1
|
||||
else ("complete_reference_candidate" if complete else "blocked")
|
||||
)
|
||||
companion = companions.get(path)
|
||||
row = {
|
||||
"source_path": path,
|
||||
"source_sha1": record["source_sha"],
|
||||
"source_sha256": hashlib.sha256(raw).hexdigest(),
|
||||
"frontend_version": (source.get("extra") or {}).get("frontendVersion"),
|
||||
"cohort": "with_companion" if companion else "without_companion",
|
||||
"comparison_status": "comparison_available" if companion else "comparison_unavailable",
|
||||
"status": status,
|
||||
"inventory": inventory,
|
||||
"attempts": attempts,
|
||||
"complete_profiles": [a["profile"] for a in complete],
|
||||
"candidate_hashes": hashes,
|
||||
"target_version_match": "unknown",
|
||||
"production_prompt_available": convert(source).prompt is not None,
|
||||
"execution_verified": False,
|
||||
}
|
||||
if companion:
|
||||
api = json.loads(companion["raw"])
|
||||
row["comparisons"] = [
|
||||
{
|
||||
"profile": a["profile"],
|
||||
"api_sha256": companion["binding"]["export_sha256"],
|
||||
"differences": differences(api, a["candidate_prompt"]),
|
||||
}
|
||||
for a in complete
|
||||
]
|
||||
rows.append(row)
|
||||
if status == "blocked":
|
||||
for attempt in attempts:
|
||||
if attempt["status"] != "rejected":
|
||||
continue
|
||||
for issue in attempt["issues"]:
|
||||
matches = [n for n in inventory if n["node_id"] == issue.get("node_id")]
|
||||
for node in matches or [{}]:
|
||||
key = (
|
||||
node.get("type"),
|
||||
node.get("provider"),
|
||||
node.get("version"),
|
||||
attempt["profile"],
|
||||
issue["code"],
|
||||
issue["message"],
|
||||
)
|
||||
gap_groups[key].append(
|
||||
{
|
||||
"source_path": path,
|
||||
"node_pointer": node.get("pointer"),
|
||||
"node_id": node.get("node_id"),
|
||||
}
|
||||
)
|
||||
summary = {
|
||||
cohort: dict(Counter(r["status"] for r in rows if r["cohort"] == cohort))
|
||||
for cohort in ("without_companion", "with_companion")
|
||||
}
|
||||
old_complete = {
|
||||
row["source_path"]
|
||||
for row in rows
|
||||
if any(
|
||||
a["status"] == "complete_reference_candidate" and a["profile"] != ASR_PROFILE
|
||||
for a in row["attempts"]
|
||||
)
|
||||
}
|
||||
now_complete = {row["source_path"] for row in rows if row["complete_profiles"]}
|
||||
return {
|
||||
"authority": "public_reference_only_not_object_info",
|
||||
"source_total": len(manifest),
|
||||
"target_total": len(rows) + len(api_paths),
|
||||
"coverage_delta": {
|
||||
"baseline": "all previous profiles excluding ASR_PROFILE; applied this run",
|
||||
"previous_complete_paths": sorted(old_complete),
|
||||
"current_complete_paths": sorted(now_complete),
|
||||
"new_complete_paths": sorted(now_complete - old_complete),
|
||||
},
|
||||
"ui_total": len(rows),
|
||||
"api_paths": api_paths,
|
||||
"excluded": excluded,
|
||||
"summary": summary,
|
||||
"profiles": list(profiles),
|
||||
"cases": rows,
|
||||
"gap_groups": [
|
||||
{
|
||||
"type": k[0],
|
||||
"provider": k[1],
|
||||
"version": k[2],
|
||||
"profile": k[3],
|
||||
"code": k[4],
|
||||
"rule_gap": k[5],
|
||||
"occurrences": v,
|
||||
}
|
||||
for k, v in sorted(gap_groups.items(), key=lambda item: str(item[0]))
|
||||
],
|
||||
"production_state_changed": False,
|
||||
"execution_verified": False,
|
||||
}
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--library", action="store_true", help="Audit current manifest UI scope")
|
||||
parser.add_argument("--repo", type=Path, required=True)
|
||||
parser.add_argument("--output", type=Path, required=True)
|
||||
parser.add_argument(
|
||||
"--profile",
|
||||
choices=(
|
||||
ASR_PROFILE,
|
||||
PROFILE,
|
||||
AUX_PROFILE,
|
||||
PRIMITIVE_PROFILE,
|
||||
@@ -290,7 +606,7 @@ def main():
|
||||
default=PROFILE,
|
||||
)
|
||||
args = parser.parse_args()
|
||||
report = audit(args.repo, args.profile)
|
||||
report = audit_library(args.repo) if args.library else 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"])}))
|
||||
|
||||
Reference in New Issue
Block a user