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

100 lines
4.2 KiB
Python

"""Compare exported source-only documentation with repository bytes, without services."""
from __future__ import annotations
import argparse
import hashlib
import json
from collections import Counter
from pathlib import Path
def audit(repo: Path, directory: Path) -> dict:
manifest = json.loads((directory / "manifest.json").read_text(encoding="utf-8"))
sources = {
p.relative_to(repo).as_posix()
for p in (repo / "example_ComfyUI_workflows").rglob("*")
if p.is_file()
}
assert {e["path"] for e in manifest} == sources
assert len(manifest) == len(sources)
files = []
for entry in manifest:
raw = (repo / entry["path"]).read_bytes()
source = json.loads(raw.decode("utf-8-sig"))
doc = json.loads((directory / entry["document"]).read_text(encoding="utf-8"))
assert hashlib.sha1(raw).hexdigest() == entry["source_sha"]
assert doc["source_review"]["source_sha"] == entry["source_sha"]
nodes = {}
def visit(graph, scope=""):
ui = "nodes" in graph
rows = graph["nodes"] if ui else [dict(n, id=k) for k, n in graph.items()]
for node in rows:
nid = scope + str(node["id"])
nodes[nid] = (
node["widgets_values"]
if ui and "widgets_values" in node
else ([] if ui else node["inputs"])
)
for sub in graph.get("definitions", {}).get("subgraphs", []):
visit(sub, scope + sub["id"] + "/")
visit(source)
assert {n["node_id"]: n["source_values"] for n in doc["source_nodes"]} == nodes
for param in doc["params"]:
assert param["default"] == nodes[param["node_id"]][param["field"]]
assert param["unit"] is None
assert param["min"] is None and param["max"] is None
for note in doc["source_notes"]:
assert note["content"] == nodes[note["node_id"]]
checked_fields = doc["source_review"].get("reviewed_named_fields", [])
for field in checked_fields:
assert field["source_value"] == nodes[field["node_id"]][field["field"]]
assert doc["status"] == {"imported": True, "doc_done": False, "exec_verified": False}
assert doc["source_review"]["complete"] is False
files.append(
{
"path": entry["path"],
"source_sha": entry["source_sha"],
"document": entry["document"],
"state": entry["state"],
"manual_scope": doc["source_review"]["scope"],
"purpose": doc["purpose"],
"source_specific_instruction": doc["steps"][0],
"source_evidence": doc["source_review"]["evidence"],
"unconfirmed": doc["source_review"]["unconfirmed"],
"reviewed_named_fields": checked_fields,
}
)
return {
"files": files,
"total": len(files),
"source_structure_purpose_partially_reviewed": len(files),
"full_documents_completed": 0,
"files_with_selected_named_fields_reviewed": sum(
bool(f["reviewed_named_fields"]) for f in files
),
"selected_named_fields_reviewed": sum(len(f["reviewed_named_fields"]) for f in files),
"real_executions": 0,
"states": dict(Counter(f["state"] for f in files)),
"named_values_preserved": sum(f["source_evidence"]["named_value_count"] for f in files),
"unnamed_widget_nodes": sum(
len(f["source_evidence"]["unnamed_widget_node_ids"]) for f in files
),
"scope_limit": "人工审校只覆盖结构与用途局部;值/注释逐字保留是机械比对,非语义定稿",
}
if __name__ == "__main__":
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--repo", type=Path, required=True)
parser.add_argument("--documents", type=Path, required=True)
parser.add_argument("--output", type=Path, required=True)
args = parser.parse_args()
report = audit(args.repo, args.documents)
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))