"""validate_docs_no_formula_duplication_v1.py — P8-T02 문서 내 공식/수식 중복 기재 방지 검증기 docs/ (doctrine.md, runbook.md 등) 및 AGENTS.md 내에 하드코딩된 수식이나 공식 정의가 중복 기재되어 있지 않은지 엄격히 검증한다. """ from __future__ import annotations import json import sys from pathlib import Path # Windows stdout 인코딩 에러 방지 if sys.stdout.encoding and sys.stdout.encoding.lower() not in ("utf-8", "utf8"): sys.stdout = open(sys.stdout.fileno(), mode="w", encoding="utf-8", buffering=1) ROOT = Path(__file__).resolve().parents[1] # 검사 대상 파일 목록 TARGET_DOCS = [ ROOT / "AGENTS.md", ROOT / "docs" / "doctrine.md", ROOT / "docs" / "runbook.md", ] def main() -> int: duplication_count = 0 errors: list[str] = [] # 공식/수식으로 판단되는 패턴 예: "Formula =", "Score = ", "Decision = ", "QEDD_R_Score =" 등 # 또는 'f(x) =' 등 수학식 하드코딩 스타일 math_patterns = [ "QEDD_R_Score =", "Decision = f(", "Report = copy(", "Release_PASS = all(", "NewRule = Contract", ] for doc_path in TARGET_DOCS: if not doc_path.exists(): continue try: content = doc_path.read_text(encoding="utf-8") except Exception as e: errors.append(f"Failed to read {doc_path.name}: {e}") continue # docs 디렉토리 내 문서와 AGENTS.md에 하드코딩 수식이 존재하면 중복으로 판단 for pattern in math_patterns: if pattern in content: duplication_count += 1 errors.append( f"Duplicated formula pattern '{pattern}' found in human doc: {doc_path.name}" ) status = "PASS" if duplication_count == 0 else "FAIL" result = { "formula_id": "VALIDATE_DOCS_NO_FORMULA_DUPLICATION_V1", "status": status, "docs_formula_duplication_count": duplication_count, "errors": errors, } out_path = ROOT / "Temp" / "docs_no_formula_duplication_v1.json" out_path.parent.mkdir(parents=True, exist_ok=True) out_path.write_text(json.dumps(result, ensure_ascii=False, indent=2), encoding="utf-8") print(json.dumps(result, ensure_ascii=True, indent=2)) if status == "PASS": print("VALIDATE_DOCS_NO_FORMULA_DUPLICATION_OK") else: print("VALIDATE_DOCS_NO_FORMULA_DUPLICATION_FAIL") for err in errors: print(f" ERROR: {err}") return 0 if status == "PASS" else 1 if __name__ == "__main__": raise SystemExit(main())