#!/usr/bin/env python3 """Fail-closed consistency gates for active status docs vs final closure truth. Activity docs must agree on FINAL_COMMIT, matrix (incl. cocotb), TD built commit, bitstream SHA-256, hardware not_run, and physical blockers. Stale tip / fail-matrix / old-bitstream claims must not reappear as current truth. """ from __future__ import annotations import re import subprocess import tempfile import unittest from pathlib import Path ROOT = Path(__file__).resolve().parents[2] FINAL_COMMIT = "da7b05a340a096ccfb4ccf4f656eb3efe6a957e2" FINAL_COMMIT_SHORT = "da7b05a" TD_BUILT_COMMIT = "8f28660f176e18e97a015e66bdae0ff747e0fc63" TD_BUILT_SHORT = "8f28660" BITSTREAM_SHA256 = "ce3157c593858c33c36e9805fe477a3955ba6f380b4a04f5f9236d3fbddb376d" BITSTREAM_SHORT = "ce3157c5" CLOSURE_RUN_ID = "grok45-final-20260805T013238Z" MATRIX_RUN_ID = "c43c4201f74517c50bd349fce87f9e0b" ACTIVITY_DOCS = ( "README.zh-CN.md", "FINAL_DELIVERY_REPORT.md", "ACCEPTANCE_MATRIX.md", "OPEN_QUESTIONS.md", "PLAN.md", "docs/TD_USAGE.zh-CN.md", ) # Must appear in every activity doc (full or unambiguous short form). PER_DOC_MARKERS = ( FINAL_COMMIT_SHORT, TD_BUILT_SHORT, BITSTREAM_SHORT, ) # Must appear somewhere in the activity-doc corpus. CORPUS_REQUIRED = ( FINAL_COMMIT, TD_BUILT_COMMIT, BITSTREAM_SHA256, CLOSURE_RUN_ID, MATRIX_RUN_ID, "cocotb", "intake", "A11", "CON124", ) # Superseded identifiers that must not be presented as current FINAL/tip truth. STALE_AS_CURRENT = ( ( re.compile( r"(?i)(?:FINAL_COMMIT|文档对齐 tip|worktree 起点 tip|Full matrix @ tip)\s*[|::]*\s*`?f82c41e" ), "stale tip f82c41e presented as current FINAL/tip", ), ( re.compile(r"(?i)最近矩阵绑定 commit\s*[|::]*\s*`?6f32493"), "stale fail-matrix 6f32493 presented as current bind", ), ( re.compile( r"(?i)(?:TD built commit|最近 TD pass|TD \+ bitstream)[^\n]{0,80}`?ead5a3e" ), "stale TD ead5a3e presented as current TD evidence", ), ( re.compile(r"(?i)bitstream SHA-256\s*`?a7b5c119"), "stale bitstream a7b5c119 presented as current", ), ( re.compile(r"(?i)cocotb[^\n]{0,40}\*\*fail\*\*"), "cocotb still marked fail in activity docs", ), ( re.compile(r"(?i)Full matrix @ `?6f32493`?[^\n]{0,40}\*\*fail\*\*"), "full matrix still marked fail at stale commit", ), ) ROOT_RESIDUE = ( ".run_id", ".run_id_linux", "evidence-batch-summary.txt", ) def _read(rel: str) -> str: return (ROOT / rel).read_text(encoding="utf-8") class ActivityDocsTruthTests(unittest.TestCase): def test_activity_docs_exist(self) -> None: missing = [rel for rel in ACTIVITY_DOCS if not (ROOT / rel).is_file()] self.assertEqual(missing, [], f"missing activity docs: {missing}") def test_each_activity_doc_carries_final_td_bitstream_markers(self) -> None: for rel in ACTIVITY_DOCS: text = _read(rel) for marker in PER_DOC_MARKERS: self.assertIn( marker, text, f"{rel} missing required marker {marker!r}", ) def test_activity_docs_agree_on_closure_corpus_fields(self) -> None: corpus = "\n".join(_read(rel) for rel in ACTIVITY_DOCS) for marker in CORPUS_REQUIRED: self.assertIn(marker, corpus, f"activity corpus missing {marker!r}") # Matrix / cocotb / hardware / blockers self.assertRegex(corpus, r"(?i)cocotb[^\n]{0,80}pass") self.assertRegex(corpus, r"(?i)(?:Full matrix|全矩阵)[^\n]{0,80}pass") self.assertRegex(corpus, r"(?i)(?:Hardware|硬件)[^\n]{0,80}(?:NOT RUN|not_run)") self.assertRegex(corpus, r"(?i)solenoid") self.assertRegex(corpus, r"(?i)(?:silkscreen|丝印)") self.assertRegex(corpus, r"A11") self.assertRegex(corpus, r"CON124") # Cross-doc agreement: every doc that mentions FINAL_COMMIT uses the same hash. for rel in ACTIVITY_DOCS: text = _read(rel) if "FINAL_COMMIT" in text or "da7b05a" in text: self.assertIn(FINAL_COMMIT_SHORT, text) if re.search(r"da7b05a[0-9a-f]*", text): matches = set(re.findall(r"da7b05a[0-9a-f]*", text)) for match in matches: self.assertTrue( FINAL_COMMIT.startswith(match), f"{rel} has unexpected commit token {match!r}", ) def test_activity_docs_reject_stale_current_claims(self) -> None: for rel in ACTIVITY_DOCS: text = _read(rel) for pattern, message in STALE_AS_CURRENT: self.assertIsNone( pattern.search(text), f"{rel}: {message}", ) def test_root_runtime_residue_not_tracked_or_present(self) -> None: for name in ROOT_RESIDUE: path = ROOT / name self.assertFalse(path.exists(), f"root residue still present: {name}") ignored = (ROOT / ".gitignore").read_text(encoding="utf-8") for token in ( ".run_id", ".run_id_linux", "evidence-batch-summary.txt", "td_20260804_*.log*", ): self.assertIn(token, ignored, f".gitignore missing {token!r}") # Migrated historical probe must exist; active root copy must not. hist = ROOT / "docs/historical/evidence-batch-summary-linux-continue-20260804T155114Z.txt" self.assertTrue(hist.is_file(), "expected migrated evidence-batch summary") def test_cleanup_runtime_residue_helper_removes_root_probes(self) -> None: script = ROOT / "tools/cleanup_runtime_residue.py" self.assertTrue(script.is_file()) with tempfile.TemporaryDirectory(prefix="prod2u-residue-") as tmp: fake_root = Path(tmp) (fake_root / "AGENTS.md").write_text("x\n", encoding="utf-8") proj = fake_root / "projects" / "prod2u-door-temp" proj.mkdir(parents=True) (proj / "project.json").write_text("{}\n", encoding="utf-8") (fake_root / ".run_id").write_text("run\n", encoding="utf-8") (fake_root / ".run_id_linux").write_text("run\n", encoding="utf-8") (fake_root / "evidence-batch-summary.txt").write_text("old\n", encoding="utf-8") (fake_root / "td_20260804_probe.log").write_text("log\n", encoding="utf-8") # Nested path must not be deleted. nested = fake_root / "runs" / "td_20260804_probe.log" nested.parent.mkdir() nested.write_text("keep\n", encoding="utf-8") result = subprocess.run( ["python3", "-B", str(script), "--root", str(fake_root), "--json"], check=True, capture_output=True, text=True, ) self.assertIn('"count": 4', result.stdout) self.assertFalse((fake_root / ".run_id").exists()) self.assertFalse((fake_root / "evidence-batch-summary.txt").exists()) self.assertFalse((fake_root / "td_20260804_probe.log").exists()) self.assertTrue(nested.exists()) if __name__ == "__main__": unittest.main()