test_activity_docs_truth.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #!/usr/bin/env python3
  2. """Fail-closed consistency gates for active status docs vs final closure truth.
  3. Activity docs must agree on FINAL_COMMIT, matrix (incl. cocotb), TD built commit,
  4. bitstream SHA-256, hardware not_run, and physical blockers. Stale tip / fail-matrix
  5. / old-bitstream claims must not reappear as current truth.
  6. """
  7. from __future__ import annotations
  8. import re
  9. import subprocess
  10. import tempfile
  11. import unittest
  12. from pathlib import Path
  13. ROOT = Path(__file__).resolve().parents[2]
  14. FINAL_COMMIT = "da7b05a340a096ccfb4ccf4f656eb3efe6a957e2"
  15. FINAL_COMMIT_SHORT = "da7b05a"
  16. TD_BUILT_COMMIT = "8f28660f176e18e97a015e66bdae0ff747e0fc63"
  17. TD_BUILT_SHORT = "8f28660"
  18. BITSTREAM_SHA256 = "ce3157c593858c33c36e9805fe477a3955ba6f380b4a04f5f9236d3fbddb376d"
  19. BITSTREAM_SHORT = "ce3157c5"
  20. CLOSURE_RUN_ID = "grok45-final-20260805T013238Z"
  21. MATRIX_RUN_ID = "c43c4201f74517c50bd349fce87f9e0b"
  22. ACTIVITY_DOCS = (
  23. "README.zh-CN.md",
  24. "FINAL_DELIVERY_REPORT.md",
  25. "ACCEPTANCE_MATRIX.md",
  26. "OPEN_QUESTIONS.md",
  27. "PLAN.md",
  28. "docs/TD_USAGE.zh-CN.md",
  29. )
  30. # Must appear in every activity doc (full or unambiguous short form).
  31. PER_DOC_MARKERS = (
  32. FINAL_COMMIT_SHORT,
  33. TD_BUILT_SHORT,
  34. BITSTREAM_SHORT,
  35. )
  36. # Must appear somewhere in the activity-doc corpus.
  37. CORPUS_REQUIRED = (
  38. FINAL_COMMIT,
  39. TD_BUILT_COMMIT,
  40. BITSTREAM_SHA256,
  41. CLOSURE_RUN_ID,
  42. MATRIX_RUN_ID,
  43. "cocotb",
  44. "intake",
  45. "A11",
  46. "CON124",
  47. )
  48. # Superseded identifiers that must not be presented as current FINAL/tip truth.
  49. STALE_AS_CURRENT = (
  50. (
  51. re.compile(
  52. r"(?i)(?:FINAL_COMMIT|文档对齐 tip|worktree 起点 tip|Full matrix @ tip)\s*[|::]*\s*`?f82c41e"
  53. ),
  54. "stale tip f82c41e presented as current FINAL/tip",
  55. ),
  56. (
  57. re.compile(r"(?i)最近矩阵绑定 commit\s*[|::]*\s*`?6f32493"),
  58. "stale fail-matrix 6f32493 presented as current bind",
  59. ),
  60. (
  61. re.compile(
  62. r"(?i)(?:TD built commit|最近 TD pass|TD \+ bitstream)[^\n]{0,80}`?ead5a3e"
  63. ),
  64. "stale TD ead5a3e presented as current TD evidence",
  65. ),
  66. (
  67. re.compile(r"(?i)bitstream SHA-256\s*`?a7b5c119"),
  68. "stale bitstream a7b5c119 presented as current",
  69. ),
  70. (
  71. re.compile(r"(?i)cocotb[^\n]{0,40}\*\*fail\*\*"),
  72. "cocotb still marked fail in activity docs",
  73. ),
  74. (
  75. re.compile(r"(?i)Full matrix @ `?6f32493`?[^\n]{0,40}\*\*fail\*\*"),
  76. "full matrix still marked fail at stale commit",
  77. ),
  78. )
  79. ROOT_RESIDUE = (
  80. ".run_id",
  81. ".run_id_linux",
  82. "evidence-batch-summary.txt",
  83. )
  84. def _read(rel: str) -> str:
  85. return (ROOT / rel).read_text(encoding="utf-8")
  86. class ActivityDocsTruthTests(unittest.TestCase):
  87. def test_activity_docs_exist(self) -> None:
  88. missing = [rel for rel in ACTIVITY_DOCS if not (ROOT / rel).is_file()]
  89. self.assertEqual(missing, [], f"missing activity docs: {missing}")
  90. def test_each_activity_doc_carries_final_td_bitstream_markers(self) -> None:
  91. for rel in ACTIVITY_DOCS:
  92. text = _read(rel)
  93. for marker in PER_DOC_MARKERS:
  94. self.assertIn(
  95. marker,
  96. text,
  97. f"{rel} missing required marker {marker!r}",
  98. )
  99. def test_activity_docs_agree_on_closure_corpus_fields(self) -> None:
  100. corpus = "\n".join(_read(rel) for rel in ACTIVITY_DOCS)
  101. for marker in CORPUS_REQUIRED:
  102. self.assertIn(marker, corpus, f"activity corpus missing {marker!r}")
  103. # Matrix / cocotb / hardware / blockers
  104. self.assertRegex(corpus, r"(?i)cocotb[^\n]{0,80}pass")
  105. self.assertRegex(corpus, r"(?i)(?:Full matrix|全矩阵)[^\n]{0,80}pass")
  106. self.assertRegex(corpus, r"(?i)(?:Hardware|硬件)[^\n]{0,80}(?:NOT RUN|not_run)")
  107. self.assertRegex(corpus, r"(?i)solenoid")
  108. self.assertRegex(corpus, r"(?i)(?:silkscreen|丝印)")
  109. self.assertRegex(corpus, r"A11")
  110. self.assertRegex(corpus, r"CON124")
  111. # Cross-doc agreement: every doc that mentions FINAL_COMMIT uses the same hash.
  112. for rel in ACTIVITY_DOCS:
  113. text = _read(rel)
  114. if "FINAL_COMMIT" in text or "da7b05a" in text:
  115. self.assertIn(FINAL_COMMIT_SHORT, text)
  116. if re.search(r"da7b05a[0-9a-f]*", text):
  117. matches = set(re.findall(r"da7b05a[0-9a-f]*", text))
  118. for match in matches:
  119. self.assertTrue(
  120. FINAL_COMMIT.startswith(match),
  121. f"{rel} has unexpected commit token {match!r}",
  122. )
  123. def test_activity_docs_reject_stale_current_claims(self) -> None:
  124. for rel in ACTIVITY_DOCS:
  125. text = _read(rel)
  126. for pattern, message in STALE_AS_CURRENT:
  127. self.assertIsNone(
  128. pattern.search(text),
  129. f"{rel}: {message}",
  130. )
  131. def test_root_runtime_residue_not_tracked_or_present(self) -> None:
  132. for name in ROOT_RESIDUE:
  133. path = ROOT / name
  134. self.assertFalse(path.exists(), f"root residue still present: {name}")
  135. ignored = (ROOT / ".gitignore").read_text(encoding="utf-8")
  136. for token in (
  137. ".run_id",
  138. ".run_id_linux",
  139. "evidence-batch-summary.txt",
  140. "td_20260804_*.log*",
  141. ):
  142. self.assertIn(token, ignored, f".gitignore missing {token!r}")
  143. # Migrated historical probe must exist; active root copy must not.
  144. hist = ROOT / "docs/historical/evidence-batch-summary-linux-continue-20260804T155114Z.txt"
  145. self.assertTrue(hist.is_file(), "expected migrated evidence-batch summary")
  146. def test_cleanup_runtime_residue_helper_removes_root_probes(self) -> None:
  147. script = ROOT / "tools/cleanup_runtime_residue.py"
  148. self.assertTrue(script.is_file())
  149. with tempfile.TemporaryDirectory(prefix="prod2u-residue-") as tmp:
  150. fake_root = Path(tmp)
  151. (fake_root / "AGENTS.md").write_text("x\n", encoding="utf-8")
  152. proj = fake_root / "projects" / "prod2u-door-temp"
  153. proj.mkdir(parents=True)
  154. (proj / "project.json").write_text("{}\n", encoding="utf-8")
  155. (fake_root / ".run_id").write_text("run\n", encoding="utf-8")
  156. (fake_root / ".run_id_linux").write_text("run\n", encoding="utf-8")
  157. (fake_root / "evidence-batch-summary.txt").write_text("old\n", encoding="utf-8")
  158. (fake_root / "td_20260804_probe.log").write_text("log\n", encoding="utf-8")
  159. # Nested path must not be deleted.
  160. nested = fake_root / "runs" / "td_20260804_probe.log"
  161. nested.parent.mkdir()
  162. nested.write_text("keep\n", encoding="utf-8")
  163. result = subprocess.run(
  164. ["python3", "-B", str(script), "--root", str(fake_root), "--json"],
  165. check=True,
  166. capture_output=True,
  167. text=True,
  168. )
  169. self.assertIn('"count": 4', result.stdout)
  170. self.assertFalse((fake_root / ".run_id").exists())
  171. self.assertFalse((fake_root / "evidence-batch-summary.txt").exists())
  172. self.assertFalse((fake_root / "td_20260804_probe.log").exists())
  173. self.assertTrue(nested.exists())
  174. if __name__ == "__main__":
  175. unittest.main()