379 lines
16 KiB
Python
379 lines
16 KiB
Python
(
|
|
'Source-only UI conversion'
|
|
' inventory. This does not'
|
|
' implement export rules.'
|
|
)
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import hashlib
|
|
import json
|
|
from collections import Counter
|
|
from pathlib import Path
|
|
|
|
from calliope.comfyui.delivery_scope import exclusion_for
|
|
|
|
|
|
def graph_rows(graph: dict, pointer: str = "", scope: str = ""):
|
|
(
|
|
'Use the same scoped IDs a'
|
|
's library documents, incl'
|
|
'uding unused definitions.'
|
|
)
|
|
yield graph, pointer, scope
|
|
for i, sub in enumerate(graph.get("definitions", {}).get("subgraphs", [])):
|
|
yield from graph_rows(sub, f"{pointer}/definitions/subgraphs/{i}", f"{scope}{sub['id']}/")
|
|
|
|
|
|
def cycles(nodes: list[dict], links: list) -> list[list[str]]:
|
|
(
|
|
'Strongly connected compon'
|
|
'ents of literal edges, no'
|
|
't plugin execution loops.'
|
|
)
|
|
adjacency = {str(n["id"]): set() for n in nodes}
|
|
for link in links:
|
|
if isinstance(link, list) and len(link) >= 6:
|
|
a, b = str(link[1]), str(link[3])
|
|
elif isinstance(link, dict) and {"origin_id", "target_id"} <= link.keys():
|
|
a, b = str(link["origin_id"]), str(link["target_id"])
|
|
else:
|
|
continue
|
|
if a in adjacency and b in adjacency:
|
|
adjacency[a].add(b)
|
|
order, low, stack, active, found = {}, {}, [], set(), []
|
|
|
|
def visit(n):
|
|
order[n] = low[n] = len(order)
|
|
stack.append(n)
|
|
active.add(n)
|
|
for target in sorted(adjacency[n]):
|
|
if target not in order:
|
|
visit(target)
|
|
low[n] = min(low[n], low[target])
|
|
elif target in active:
|
|
low[n] = min(low[n], order[target])
|
|
if low[n] == order[n]:
|
|
component = []
|
|
while True:
|
|
target = stack.pop()
|
|
active.remove(target)
|
|
component.append(target)
|
|
if target == n:
|
|
break
|
|
if len(component) > 1 or n in adjacency[n]:
|
|
found.append(sorted(component))
|
|
|
|
for n in adjacency:
|
|
if n not in order:
|
|
visit(n)
|
|
return found
|
|
|
|
|
|
# Source-review rules: these describe what must be checked, not what an export
|
|
# will do. Candidates are kept even when not referenced in the main graph.
|
|
RULES = {
|
|
"subgraph": (
|
|
'按定义ID及实例输入输出逐槽核对展开、实例ID重写'
|
|
'和未使用定义剔除;源UI不能证明展开规则。'
|
|
),
|
|
"reroute": "沿所列输入输出核对转接节点的剔除和端点重连,不能按相邻布局猜线。",
|
|
"virtual_candidate": (
|
|
"Set/Get或Primitive等候选虚拟节点需对应前端/"
|
|
"插件规则及真实导出;type字符串不能单独证明应剔除。"
|
|
),
|
|
"note": (
|
|
'注释原值保留用于说明;实际导出是否剔除需核对,不把'
|
|
'注释当服务端模型节点。'
|
|
),
|
|
"nonzero_mode": (
|
|
'源mode非0(2/4保留为静音/旁路候选);逐输'
|
|
'入核对跳过/穿透与实际端点,不能只删除节点。'
|
|
),
|
|
"loop_candidate": (
|
|
'循环命名节点仅是候选;核对循环边界、迭代输入和前端'
|
|
'展开规则,不能声称静态DAG即可执行。'
|
|
),
|
|
"literal_cycle": (
|
|
'字面连线存在强连通分量;需真实导出与插件循环语义确'
|
|
'认,不能直接交队列。'
|
|
),
|
|
"position_widgets": (
|
|
'位置数组逐节点待具名;object_info的in'
|
|
'put_order本身不足以证明第三方控件序列化顺'
|
|
'序。'
|
|
),
|
|
"named_widgets": (
|
|
'具名控件仍须核对前端状态、动态槽和服务端字段声明;'
|
|
'不保证可直接原样拷贝。'
|
|
),
|
|
"slots": (
|
|
'逐条核对起止节点、非零输出槽、输入槽索引、端点声明'
|
|
'及源type;通配/联合/类型不一致仍保留待验证。'
|
|
),
|
|
"node_definitions": (
|
|
'逐节点核对服务端字段/槽/必填与插件版本;源UI的'
|
|
'type不是可信object_info,实际未采集'
|
|
'。'
|
|
),
|
|
"nonconcrete_slots": (
|
|
'源槽声明为通配/联合/枚举等非单一类型;严格相等校'
|
|
'验不能自动放行,需真实定义与导出。'
|
|
),
|
|
"nonzero_output_links": "源连线使用非零输出槽;保留槽号,不能全部改成0。",
|
|
"endpoint_findings": (
|
|
'仅检查源声明:本地端点缺失可能为子图边界,索引/端'
|
|
'点不一致需原前端导出核对,禁止静默纠正。'
|
|
),
|
|
}
|
|
|
|
|
|
def inventory(repo: Path, *, historical: bool = False) -> dict:
|
|
docs = repo / "docs/hel-892"
|
|
manifest = json.loads((docs / "offline/manifest.json").read_text(encoding="utf-8"))
|
|
cases = json.loads((docs / "golden-cases.json").read_text(encoding="utf-8"))["cases"]
|
|
files = []
|
|
for entry in manifest:
|
|
raw = (repo / entry["path"]).read_bytes()
|
|
source = json.loads(raw.decode("utf-8-sig"))
|
|
assert hashlib.sha1(raw).hexdigest() == entry["source_sha"]
|
|
if "nodes" not in source:
|
|
continue
|
|
findings = {key: [] for key in RULES}
|
|
graphs = []
|
|
for graph, pointer, scope in graph_rows(source):
|
|
nodes = graph["nodes"]
|
|
links = graph.get("links", [])
|
|
definitions = graph.get("definitions", {}).get("subgraphs", [])
|
|
sub_ids = {s["id"] for s in definitions}
|
|
# Also allow a subgraph definition referenced from a nested graph.
|
|
all_sub_ids = {
|
|
s["id"]
|
|
for g, _, _ in graph_rows(source)
|
|
for s in g.get("definitions", {}).get("subgraphs", [])
|
|
}
|
|
graph_ref = {
|
|
"scope": scope,
|
|
"pointer": pointer or "/",
|
|
"node_count": len(nodes),
|
|
"links": links,
|
|
"inputs": graph.get("inputs", []),
|
|
"outputs": graph.get("outputs", []),
|
|
"extra_keys": sorted(set(graph) - {"nodes", "links"}),
|
|
}
|
|
graphs.append(graph_ref)
|
|
for i, sub in enumerate(definitions):
|
|
findings["subgraph"].append(
|
|
{
|
|
"kind": "definition",
|
|
"id": sub["id"],
|
|
"name": sub.get("name"),
|
|
"pointer": f"{pointer}/definitions/subgraphs/{i}",
|
|
"local_instances": [
|
|
scope + str(n["id"]) for n in nodes if n.get("type") == sub["id"]
|
|
],
|
|
}
|
|
)
|
|
for i, node in enumerate(nodes):
|
|
ct = node.get("type", "")
|
|
ref = {
|
|
"node_id": scope + str(node["id"]),
|
|
"type": ct,
|
|
"pointer": f"{pointer}/nodes/{i}",
|
|
"mode": node.get("mode", 0),
|
|
}
|
|
if ct in all_sub_ids or ct in sub_ids:
|
|
findings["subgraph"].append({**ref, "kind": "instance"})
|
|
if "reroute" in ct.lower():
|
|
findings["reroute"].append(ref)
|
|
if ct in {"SetNode", "GetNode", "PrimitiveNode"} or ct.startswith("Primitive"):
|
|
findings["virtual_candidate"].append(
|
|
{**ref, "widgets_values": node.get("widgets_values")}
|
|
)
|
|
if ct in {"Note", "MarkdownNote"}:
|
|
findings["note"].append(ref)
|
|
if node.get("mode", 0) != 0:
|
|
findings["nonzero_mode"].append(ref)
|
|
if any(word in ct.lower() for word in ("loop", "foreach", "forloop", "while")):
|
|
findings["loop_candidate"].append(ref)
|
|
widgets = node.get("widgets_values")
|
|
if ct not in {"Note", "MarkdownNote"} and isinstance(widgets, list) and widgets:
|
|
findings["position_widgets"].append({**ref, "values": widgets})
|
|
if isinstance(widgets, dict) and widgets:
|
|
findings["named_widgets"].append({**ref, "fields": sorted(widgets)})
|
|
findings["slots"].append(
|
|
{**ref, "inputs": node.get("inputs", []), "outputs": node.get("outputs", [])}
|
|
)
|
|
for direction in ("inputs", "outputs"):
|
|
for slot, socket in enumerate(node.get(direction, [])):
|
|
value = socket.get("type")
|
|
if not isinstance(value, str) or value in {"*", "ANY"} or "," in value:
|
|
findings["nonconcrete_slots"].append(
|
|
{
|
|
**ref,
|
|
"direction": direction,
|
|
"slot": slot,
|
|
"name": socket.get("name"),
|
|
"source_type": value,
|
|
}
|
|
)
|
|
if ct not in {"Note", "MarkdownNote"}:
|
|
findings["node_definitions"].append(ref)
|
|
by_id = {str(n["id"]): n for n in nodes}
|
|
for i, link in enumerate(links):
|
|
ref = {"scope": scope, "pointer": f"{pointer}/links/{i}", "link": link}
|
|
if isinstance(link, dict):
|
|
values = [
|
|
link.get(k)
|
|
for k in (
|
|
"id",
|
|
"origin_id",
|
|
"origin_slot",
|
|
"target_id",
|
|
"target_slot",
|
|
"type",
|
|
)
|
|
]
|
|
elif isinstance(link, list) and len(link) >= 6:
|
|
values = link
|
|
else:
|
|
findings["endpoint_findings"].append({**ref, "reason": "nonstandard_link"})
|
|
continue
|
|
lid, origin, output, target, input_slot = values[:5]
|
|
if type(output) is int and output != 0:
|
|
findings["nonzero_output_links"].append(ref)
|
|
for node_id, direction, slot in (
|
|
(origin, "outputs", output),
|
|
(target, "inputs", input_slot),
|
|
):
|
|
node = by_id.get(str(node_id))
|
|
if node is None:
|
|
reason = (
|
|
'endpoint_not_local_may_be'
|
|
'_subgraph_boundary'
|
|
)
|
|
elif type(slot) is not int or not 0 <= slot < len(node.get(direction, [])):
|
|
reason = (
|
|
'slot_index_outside_source'
|
|
'_declaration'
|
|
)
|
|
elif direction == "inputs" and node[direction][slot].get("link") != lid:
|
|
reason = "input_link_id_not_reciprocal"
|
|
elif direction == "outputs" and lid not in (
|
|
node[direction][slot].get("links") or []
|
|
):
|
|
reason = "output_link_id_not_reciprocal"
|
|
else:
|
|
continue
|
|
findings["endpoint_findings"].append(
|
|
{
|
|
**ref,
|
|
"node_id": scope + str(node_id),
|
|
"direction": direction,
|
|
"slot": slot,
|
|
"reason": reason,
|
|
}
|
|
)
|
|
for component in cycles(nodes, links):
|
|
findings["literal_cycle"].append(
|
|
{"scope": scope, "node_ids": [scope + n for n in component]}
|
|
)
|
|
matching = [
|
|
c
|
|
for c in cases
|
|
if c["source_path"] == entry["path"] and c["source_sha"] == entry["source_sha"]
|
|
]
|
|
files.append(
|
|
{
|
|
"path": entry["path"],
|
|
"source_sha": entry["source_sha"],
|
|
"facts_method": (
|
|
'mechanical_source_structu'
|
|
're_extraction'
|
|
),
|
|
"source_use_review": "docs/hel-892/逐文件第三批审校.md",
|
|
"graphs": graphs,
|
|
"findings": findings,
|
|
"rule_review": [
|
|
{
|
|
"category": key,
|
|
"evidence_count": len(value),
|
|
"rule": RULES[key],
|
|
"status": "requires_materials_not_implemented",
|
|
}
|
|
for key, value in findings.items()
|
|
if value
|
|
],
|
|
"golden_case_ids": [c["id"] for c in matching],
|
|
"golden_coverage": "exact_source_candidate_only" if matching else "no_exact_sample",
|
|
"materials": {
|
|
"original_frontend_api_export": {
|
|
"source_path": entry["path"],
|
|
"source_sha": entry["source_sha"],
|
|
"case_export": matching[0]["export_file"] if matching else None,
|
|
"needed_for": [
|
|
k for k in findings if findings[k] and k != "node_definitions"
|
|
],
|
|
},
|
|
"trusted_definitions": (
|
|
'findings.node_definitions'
|
|
'逐节点字段和槽;不是模型包或机器访问请求'
|
|
),
|
|
},
|
|
"converted_reliably": False,
|
|
"executable": False,
|
|
"execution_verified": False,
|
|
"remaining": "源结构清单不能当作可靠转换;即使差分equal也不代表可执行",
|
|
}
|
|
)
|
|
actual_ui = {
|
|
p.relative_to(repo).as_posix()
|
|
for p in (repo / "example_ComfyUI_workflows").rglob("*")
|
|
if p.is_file() and "nodes" in json.loads(p.read_text(encoding="utf-8-sig"))
|
|
}
|
|
assert len(files) == len(actual_ui) == 146
|
|
assert {f["path"] for f in files} == actual_ui
|
|
report = {
|
|
"schema_version": 1,
|
|
"files": files,
|
|
"total_ui_files": len(files),
|
|
"remaining_reliable_conversions": 146,
|
|
"not_execution_evidence": True,
|
|
"files_by_finding": dict(Counter(k for f in files for k, v in f["findings"].items() if v)),
|
|
"exact_golden_candidates": sum(bool(f["golden_case_ids"]) for f in files),
|
|
"rule_review_method": (
|
|
'人工规则核对与机器证据拼接;所有规则仍待真实导出/'
|
|
'定义,不是146次实际转换验证'
|
|
),
|
|
"negative_finding_limit": "未命中命名候选或静态环不证明没有插件隐藏循环/虚拟语义",
|
|
}
|
|
|
|
if not historical:
|
|
excluded = [f for f in files if exclusion_for(f["path"])]
|
|
active = [f for f in files if not exclusion_for(f["path"])]
|
|
for row in files:
|
|
row["delivery_status"] = "user_excluded" if exclusion_for(row["path"]) else "in_scope"
|
|
report.update(
|
|
inventory_scope="all_source_files_with_current_delivery_scope",
|
|
source_files=len(manifest), target_files=len(manifest) - len(excluded),
|
|
target_ui_files=len(active), user_excluded=len(excluded),
|
|
remaining_reliable_conversions=len(active),
|
|
historical_exact_golden_candidates=report["exact_golden_candidates"],
|
|
exact_golden_candidates=sum(bool(f["golden_case_ids"]) for f in active),
|
|
exclusions=[{"path": f["path"], **exclusion_for(f["path"])} for f in excluded],
|
|
)
|
|
return report
|
|
|
|
|
|
if __name__ == "__main__":
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("--repo", type=Path, required=True)
|
|
parser.add_argument("--output", type=Path, required=True)
|
|
args = parser.parse_args()
|
|
report = inventory(args.repo)
|
|
args.output.write_text(
|
|
json.dumps(report, ensure_ascii=False, indent=2) + "\n", encoding="utf-8"
|
|
)
|
|
print(json.dumps({k: v for k, v in report.items() if k != "files"}, ensure_ascii=False))
|