Files
aigc-home/docs/hel-892/zimage-upscale-note-correction-evidence/semantic_diff.py
T
2026-10-31 17:31:35 +08:00

80 lines
3.2 KiB
Python

"""Record the actual before/after semantic pointers for this review correction."""
import hashlib
import json
import subprocess
from pathlib import Path
from build_definition import OUT, PATHS, REPO, canonical
from rebuild import BASE, RESOURCE
def differences(a, b, pointer=""):
if a == b:
return []
if isinstance(a, dict) and isinstance(b, dict) and a.keys() == b.keys():
return [d for k in a for d in differences(a[k], b[k], pointer + "/" + k)]
if isinstance(a, list) and isinstance(b, list) and len(a) == len(b):
return [
d
for i, (x, y) in enumerate(zip(a, b, strict=True))
for d in differences(x, y, pointer + "/" + str(i))
]
return [dict(pointer=pointer, before=a, after=b)]
def main():
previous = json.loads(subprocess.check_output(["git", "show", f"{BASE}:{RESOURCE}"], cwd=REPO))
current = json.loads((REPO / RESOURCE).read_bytes())
target = PATHS[0]
assert all(current[p] == r for p, r in previous.items() if p != target)
old = previous[target]["fixed_version_document"]
new = current[target]["fixed_version_document"]
delta = differences(old, new)
assert len(delta) == 2
assert any(d["pointer"] == "/comments/5/meaning" for d in delta)
assert any(d["pointer"].startswith("/flow/") for d in delta)
assert all(d["after"] == d["before"].replace("三步原文", "两条编号建议") for d in delta)
raw = (REPO / target).read_bytes()
assert raw == subprocess.check_output(["git", "show", f"{BASE}:{target}"], cwd=REPO)
original = json.loads(raw)["definitions"]["subgraphs"][0]["nodes"][13]["widgets_values"][0]
assert original.count("1. ") == 1 and original.count("2. ") == 1 and "3. " not in original
original_evidence = OUT.parent / "zimage-upscale-document-evidence"
actual_name = "actual-" + Path(target).stem + ".json"
old_doc = json.loads(
subprocess.check_output(
["git", "show", f"{BASE}:{original_evidence.relative_to(REPO) / actual_name}"], cwd=REPO
)
)
new_doc = json.loads((OUT / actual_name).read_bytes())
doc_delta = differences(old_doc, new_doc)
assert len(doc_delta) == 3
assert all(
d["pointer"] == "/source_review_sha"
or d["after"] == d["before"].replace("三步原文", "两条编号建议")
for d in doc_delta
)
result = dict(
base=BASE,
target=target,
definition_changes=delta,
actual_document_changes=doc_delta,
other_reviews_unchanged=155,
old_other_blocks_unchanged=70,
fields_unchanged=25,
source_notes_unchanged=6,
source_sha256=hashlib.sha256(raw).hexdigest(),
source_note_pointer="/definitions/subgraphs/0/nodes/13/widgets_values/0",
source_note_original=original,
numbered_suggestions=[1, 2],
before_definition_sha256=hashlib.sha256(canonical(old)).hexdigest(),
after_definition_sha256=hashlib.sha256(canonical(new)).hexdigest(),
source_and_identity_unchanged=True,
)
(OUT / "semantic-diff.json").write_text(json.dumps(result, ensure_ascii=False, indent=2) + "\n")
print("PASS exactly two definition pointers; one editorial count correction only")
if __name__ == "__main__":
main()