world_sync/tests/checks/m11b2d_a3a1b_cmd_index_consistency.py
2026-09-23 04:09:40 +08:00

54 lines
2.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# -*- coding: utf-8 -*-
# A3a-1b 重做轮:让 §8 全行 CMD 一致性核对脚本正确区分三类行
# (1) 单命令证据件:列内首反引号段以 `# CMD: ` 开头 → 与文件首行去前缀逐字比对
# (2) 转录件(非单命令输出,如 a1c_damage_forensics):列内显式声明「首行原文为 `# FILE: ...`」→ 比对文件首行原文
# (3) 本核对件自身(尚未生成,自指)→ 标记「自指,本件生成后由本行结论覆盖」
import io, os, sys
ROOT = "/d/pipeline/workspaces/0/sdlc_general/modules/world_sync"
DOC = os.path.join(ROOT, "docs/work-log-2026-09-22-m11b2d-fixture-contract.md")
SELF = "tests/evidence/m11b2d_a3a1b_cmd_index_consistency.txt"
lines = io.open(DOC, encoding="utf-8").read().split("\n")
rows = []
for ln, text in enumerate(lines, 1):
t = text.strip()
if not (t.startswith("| `tests/evidence/") and t.endswith("|")):
continue
cells = [c.strip() for c in t.strip("|").split("`")]
fpath = t.strip("|").split("|")[0].strip().strip("`")
if fpath == SELF:
rows.append((ln, fpath, "自指行(本核对件自身,生成前磁盘无此文件;生成后本行结论即由本件正文覆盖)", "SKIP-SELF"))
continue
# 找列内第一个反引号段
seg = None
for c in cells[1:]:
if c.strip().startswith("# CMD: ") or c.strip().startswith("# FILE: "):
seg = c.strip()
break
if seg is None:
rows.append((ln, fpath, "列内无 CMD/FILE 原文可核", "SKIP"))
continue
full = os.path.join(ROOT, fpath)
if not os.path.exists(full):
rows.append((ln, fpath, "证据文件不存在", "FAIL"))
continue
first = io.open(full, encoding="utf-8").read().split("\n")[0]
if seg.startswith("# CMD: "):
doc_cmd = seg[len("# CMD: "):]
file_cmd = first[len("# CMD: "):] if first.startswith("# CMD: ") else "(文件首行非 # CMD:)"
verdict = "一致" if doc_cmd == file_cmd else "不一致 DOC=%r FILE=%r" % (doc_cmd, file_cmd)
rows.append((ln, fpath, seg[:60] + "…", verdict))
else: # 转录件:核首行原文
verdict = "一致" if first == seg else "不一致 DOC=%r FILE=%r" % (seg, first)
rows.append((ln, fpath, seg[:60] + "…", verdict))
fails = [r for r in rows if r[3].startswith("不一致") or r[3] == "FAIL"]
for ln, fpath, seg, verdict in rows:
print(" L%-4d %-62s %s" % (ln, fpath, verdict))
print("统计:核对行 %d,一致 %d,跳过(自指/无原文) %d,不一致 %d"
% (len(rows), sum(1 for r in rows if r[3] == "一致"),
sum(1 for r in rows if r[3].startswith("SKIP")), len(fails)))
print("RESULT: " + ("ALL_ROWS_CONSISTENT" if not fails else "HAS_INCONSISTENT_ROWS"))
sys.exit(0 if not fails else 1)