"""AST/text branch evidence and inert protocol descriptions; no engine simulation.""" import ast import csv import json from pathlib import Path from acquire import COMMITS, now, sha HERE = Path(__file__).resolve().parent ROOT = HERE.parents[2] PRIOR = ROOT / "docs/hel-892/batch177-controlnet-provenance" def write(name, value): with (HERE / name).open("x", encoding="utf-8") as stream: json.dump(value, stream, ensure_ascii=False, indent=2) stream.write("\n") def span(text, node): value = "\n".join(text.splitlines()[node.lineno - 1 : node.end_lineno]) return dict(lines=[node.lineno, node.end_lineno], text=value, sha256=sha(value.encode())) def function(tree, name): return next( n for n in ast.walk(tree) if isinstance(n, (ast.FunctionDef, ast.AsyncFunctionDef)) and n.name == name ) def main(): proofs = json.loads((HERE / "engine-sources.json").read_text()) + json.loads( (HERE / "direct-sources.json").read_text() ) evidence, versions, trees = {}, {}, {} for proof in proofs: text = (HERE / proof["file"]).read_text() assert sha(text.encode()) == proof["sha256"] tree = ast.parse(text) trees[(proof["version"], proof["path"])] = tree names = { "execution.py": [ "get_input_data", "_async_map_node_over_list", "get_output_data", "get_output_from_returns", "merge_result_data", "execute", "validate_inputs", "validate_prompt", ], "comfy_execution/graph.py": ["get_input_info", "get_cache", "cache_update"], "comfy_execution/graph_utils.py": ["is_link"], "comfy_execution/validation.py": ["validate_node_input"], }[proof["path"]] for name in names: node = function(tree, name) key = proof["version"] + ":" + name evidence[key] = dict( source=proof, function=name, **span(text, node), ast_sha256=sha(ast.dump(node).encode()), rechecked_at=now(), ) aux_inputs = json.loads((PRIOR / "class-schema-matrix.json").read_text()) for ct in ("AIO_Preprocessor", "DWPreprocessor", "AnimalPosePreprocessor"): record = aux_inputs[ct] evidence["aux:" + ct] = dict( source=record["source_proof"], properties=record.get("attributes"), original_class=record["class_excerpt"], original_retrieval=record["earliest_known_prior_member_retrieval"], new_release_binding=record["new_binding_time"], rechecked_at=now(), ) spec = [ ( "dict_ui", "get_output_from_returns", "'ui' in r", "dict含ui;本案ui.openpose_json是字符串列表", "uis追加ui,最后按首个UI字典的key合并;这是引擎UI数据,不证明前端展示", "UI键不一致或值不可迭代可在合并处抛异常", ), ( "dict_result", "get_output_from_returns", "elif 'result' in r", "dict有result且无expand;本案result为二项tuple", "把完整result加入results,不按RETURN_TYPES裁剪", "不是这一步拒绝额外返回;result形状错误会在后续合并出错", ), ( "expand", "get_output_from_returns", "'expand' in r", "dict有expand(本案无)", "has_subgraph,交subgraph_results,不走普通合并", "不能把普通分支结论套到扩图", ), ( "tuple", "get_output_from_returns", "results.append(r)", "普通tuple,非dict/NodeOutput/ExecutionBlocker", "完整tuple加入results;本案包装返回dict,tuple在result内", "非tuple对象不因本分支自动合法", ), ( "blocker", "get_output_from_returns", "len(obj.RETURN_TYPES)", "ExecutionBlocker或V3阻断(本案无)", "阻断占位按声明RETURN_TYPES长度构造", "这不等同普通额外返回按声明截断", ), ( "merge_actual_length", "merge_result_data", "[False] * len(results[0])", "无OUTPUT_IS_LIST属性;准确aux AIO类无此属性且仅继承object", "按首份实际result二项长度生成两个合并输出,不因单IMAGE声明拒绝或丢弃第二项", "后续result比首份短可能IndexError;长于首份的尾项不会进入首份长度循环", ), ( "merge_override", "merge_result_data", "zip(range(len(results[0])), output_is_list)", "存在显式OUTPUT_IS_LIST(本案AIO无)", "zip长度取短;若显式列表仅一项则只合并第一项", "额外项可能在此条件下丢弃;不是RETURN_TYPES导致", ), ( "cache", "execute", "outputs=output_data", "普通返回,无扩图/未决任务且未出错", "CacheEntry的outputs保存完整合并output_data;" "graph cache_update/get_cache原样传CacheEntry", "缓存provider、实际执行/缓存命中与后续持久性不在此认证", ), ( "consume", "get_input_data", "cached.outputs[output_index]", "有缓存且索引未超实际outputs长度;case01分别33→36及34→37,均索引0", "取实际缓存出口0,额外出口1未被本case01消费", "缺缓存/越界先mark_missing;不等同自动成功或一定立刻抛错", ), ( "declared_index", "validate_inputs", "received_type = r[val[1]]", "建立新增索引1输入连线时,AIO声明只有IMAGE一项", "按上游RETURN_TYPES索引检查,索引1发生IndexError;实际缓存有两项不绕过此检查", "由递归或外层validate_prompt捕获,形成inner_validation或validation异常;不认证新增连线可用", ), ( "type", "validate_inputs", "return_type_mismatch", "声明类型与下游要求不相容且未由自定义input_types验证替代", "记录return_type_mismatch并继续失败归因", "类型吻合只证明声明校验,不证明真实值类型/质量", ), ( "execution_error", "execute", "except Exception as ex", "调用、拆分、UI合并或结果合并等执行路径抛异常", "返回ExecutionResult.FAILURE与异常详情;中断有独立分支", "不是将错误result静默裁为单项成功", ), ( "validation_error", "validate_prompt", "prompt_outputs_failed_validation", "没有good_outputs", "返回失败;如存在其它合法输出,可只忽略失败输出", "不能泛称任一错误必然阻止所有独立输出", ), ] rows = [] for version, commit in COMMITS.items(): text = (HERE / "source-text" / (version + "__execution.py.txt")).read_text() tree = trees[(version, "execution.py")] for ident, fn, anchor, condition, output, error in spec: node = function(tree, fn) fragment = span(text, node) assert anchor in fragment["text"], (version, ident) matches = [ i for i in range(node.lineno, node.end_lineno + 1) if anchor in text.splitlines()[i - 1] ] rows.append( dict( id=version + ":" + ident, version=version, commit=commit, branch=ident, condition=condition, output=output, error=error, evidence=version + ":" + fn, anchor=anchor, lines=matches, engine_executed=False, target_compatibility="unknown", ) ) versions[version] = dict( commit=commit, host_version_verified=False, rows=[r["id"] for r in rows if r["version"] == version], ) case = json.loads((ROOT / "docs/hel-892/supplied-api/01.api.json").read_text()) uses = [ dict(source=v[0], index=v[1], target=nid, field=field) for nid, node in case.items() for field, v in node["inputs"].items() if isinstance(v, list) and len(v) == 2 and v[0] in ("33", "34") ] assert sorted((r["source"], r["target"], r["index"]) for r in uses) == [ ("33", "36", 0), ("34", "37", 0), ] same = { name: evidence["0.21.1:" + name]["ast_sha256"] == evidence["0.17.0:" + name]["ast_sha256"] for name in ( "get_input_data", "merge_result_data", "get_output_from_returns", "execute", "validate_inputs", ) } write("branch-evidence.json", evidence) write( "protocol-matrix.json", dict( versions=versions, branches=rows, case01_uses=uses, ast_equal=same, real_engine_tests=0, target_requests=0, ), ) with (HERE / "protocol-matrix.csv").open("x", encoding="utf-8", newline="") as stream: writer = csv.DictWriter(stream, list(rows[0]), lineterminator="\n") writer.writeheader() for row in rows: writer.writerow({k: json.dumps(v, ensure_ascii=False) for k, v in row.items()}) samples = [ dict( id="dw33", node="33", symbolic_only=True, upstream_invoked=False, shape=dict( kind="dict", ui=dict(openpose_json=[""]), result=["", ""], ), result_python_container="tuple", observed_branches=["dict_ui", "dict_result", "merge_actual_length", "consume"], ), dict( id="animal34", node="34", symbolic_only=True, upstream_invoked=False, shape=dict( kind="dict", ui=dict(openpose_json=[""]), result=["", ""], ), result_python_container="tuple", observed_branches=["dict_ui", "dict_result", "merge_actual_length", "consume"], ), ] write( "inert-samples.json", dict( purpose="shape annotations only, not engine input or generated results", samples=samples ), ) print(len(rows), "version-specific branch rows; inert annotations only; no engine tests") if __name__ == "__main__": main()