test_activity_docs_truth.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #!/usr/bin/env python3
  2. """Fail-closed consistency gates for active status docs vs current 3-axis truth.
  3. Activity docs must assert X/Y/Z, six limits, 450/5000/500, permanent EN, hard
  4. truncate, and unbound tip-TD/bitstream placeholders. Stale 2-axis/solenoid IO,
  5. runtime-limit-inert policy, and old bit/commit-as-current claims must not reappear.
  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. PENDING_MARKER = "pending final tip TD"
  15. TRUNCATE_SEMANTICS = "truncate_current_high_pulse_immediately_after_synchronized_limit"
  16. PRIOR_BIT_SHORT = "45f548ab"
  17. PRIOR_BIT_COMMIT = "bb515af"
  18. BASELINE_TIP_SHORT = "cdf9f76"
  19. ACTIVITY_DOCS = (
  20. "README.zh-CN.md",
  21. "FINAL_DELIVERY_REPORT.md",
  22. "ACCEPTANCE_MATRIX.md",
  23. "OPEN_QUESTIONS.md",
  24. "PLAN.md",
  25. "docs/TD_USAGE.zh-CN.md",
  26. "docs/HARDWARE_TEST_ORDER_WAVEFORMS.zh-CN.md",
  27. )
  28. # Must appear in every activity doc.
  29. PER_DOC_MARKERS = (
  30. PENDING_MARKER,
  31. PRIOR_BIT_SHORT,
  32. PRIOR_BIT_COMMIT,
  33. )
  34. # Must appear somewhere in the activity-doc corpus.
  35. CORPUS_REQUIRED = (
  36. PENDING_MARKER,
  37. TRUNCATE_SEMANTICS,
  38. "permanent_high",
  39. "450",
  40. "5000",
  41. "500",
  42. "CON104",
  43. "CON105",
  44. "CON106",
  45. "CON120",
  46. "CON12",
  47. "CON129",
  48. BASELINE_TIP_SHORT,
  49. PRIOR_BIT_SHORT,
  50. PRIOR_BIT_COMMIT,
  51. )
  52. # Superseded identifiers / policies that must not be presented as current tip truth.
  53. STALE_AS_CURRENT = (
  54. (
  55. re.compile(
  56. r"(?i)FINAL_COMMIT\s*[|::=]*\s*`?da7b05a[0-9a-f]*`?"
  57. ),
  58. "stale 2-axis FINAL_COMMIT da7b05a presented as current",
  59. ),
  60. (
  61. re.compile(
  62. r"(?i)Bitstream SHA-256\s*[|::=]*\s*`?ce3157c5[0-9a-f]*`?"
  63. ),
  64. "stale bitstream ce3157c5 presented as current",
  65. ),
  66. (
  67. re.compile(
  68. r"(?i)TD built commit\s*[|::=]*\s*`?8f28660[0-9a-f]*`?"
  69. ),
  70. "stale TD 8f28660 presented as current",
  71. ),
  72. (
  73. re.compile(
  74. r"(?i)(?:(?:as|@)\s*tip|tip\s+(?:bitstream|bit|TD|证据)|"
  75. r"当前\s*(?:tip\s*)?(?:bitstream|位流|闭包证据))\s*[`:]?\s*45f548ab"
  76. ),
  77. "prior bit 45f548ab presented as tip/current",
  78. ),
  79. (
  80. re.compile(
  81. r"(?i)(?:runtime\s+limits?\s+are\s+inert|"
  82. r"(?<!not\s)(?<!non-)runtime\s+limit\s+inert|"
  83. r"限位对普通\s*jog\s*\*?惰性\*?|"
  84. r"完成后限位对普通 jog\s*\*?惰性\*?)"
  85. ),
  86. "runtime limit inert presented as current policy",
  87. ),
  88. (
  89. re.compile(
  90. r"(?i)(?:进风(?:门)?默认\*?电磁铁\*?|intake_solenoid_out\s*[::].*\bP2\b|"
  91. r"偏移\s*8\s*进风门\s*\|\s*默认\*\*电磁铁\*\*)"
  92. ),
  93. "solenoid intake presented as active product path",
  94. ),
  95. (
  96. re.compile(
  97. r"(?i)(?:X\s*限位[^\n]{0,40}CON68|Y\s*限位[^\n]{0,40}CON34|"
  98. r"CON68\.3\s*/\s*M6|CON34\.3\s*/\s*F9|"
  99. r"X\s*电机[^\n]{0,20}CON139|Y\s*电机[^\n]{0,20}CON138|"
  100. r"进风电磁铁[^\n]{0,20}CON141)"
  101. ),
  102. "obsolete CON68/CON34/CON138-141 mapping presented as active probe",
  103. ),
  104. (
  105. re.compile(r"(?i)(?:FINAL_COMMIT|文档对齐 tip|worktree 起点 tip|Full matrix @ tip)\s*[|::]*\s*`?f82c41e"),
  106. "stale tip f82c41e presented as current FINAL/tip",
  107. ),
  108. (
  109. re.compile(r"(?i)bitstream SHA-256\s*`?a7b5c119"),
  110. "stale bitstream a7b5c119 presented as current",
  111. ),
  112. )
  113. ROOT_RESIDUE = (
  114. ".run_id",
  115. ".run_id_linux",
  116. "evidence-batch-summary.txt",
  117. )
  118. def _read(rel: str) -> str:
  119. return (ROOT / rel).read_text(encoding="utf-8")
  120. class ActivityDocsTruthTests(unittest.TestCase):
  121. def test_activity_docs_exist(self) -> None:
  122. missing = [rel for rel in ACTIVITY_DOCS if not (ROOT / rel).is_file()]
  123. self.assertEqual(missing, [], f"missing activity docs: {missing}")
  124. def test_each_activity_doc_carries_pending_and_prior_bit_markers(self) -> None:
  125. for rel in ACTIVITY_DOCS:
  126. text = _read(rel)
  127. for marker in PER_DOC_MARKERS:
  128. self.assertIn(
  129. marker,
  130. text,
  131. f"{rel} missing required marker {marker!r}",
  132. )
  133. def test_activity_docs_agree_on_three_axis_corpus(self) -> None:
  134. corpus = "\n".join(_read(rel) for rel in ACTIVITY_DOCS)
  135. for marker in CORPUS_REQUIRED:
  136. self.assertIn(marker, corpus, f"activity corpus missing {marker!r}")
  137. self.assertRegex(corpus, r"(?i)(?:X\s*/\s*Y\s*/\s*Z|三轴)")
  138. self.assertRegex(corpus, r"(?i)(?:六限位|six limits)")
  139. self.assertRegex(corpus, r"(?i)(?:硬截断|立即截断|hard truncate)")
  140. self.assertRegex(corpus, r"(?i)(?:Hardware|硬件)[^\n]{0,80}(?:NOT RUN|not_run)")
  141. self.assertRegex(
  142. corpus,
  143. r"(?i)45f548ab[^\n]{0,80}bb515af|bb515af[^\n]{0,80}45f548ab",
  144. )
  145. # Active mapping must include 3-axis connectors somewhere.
  146. self.assertRegex(corpus, r"CON104")
  147. self.assertRegex(corpus, r"CON105")
  148. self.assertRegex(corpus, r"CON106")
  149. self.assertRegex(corpus, r"CON120")
  150. self.assertRegex(corpus, r"CON129")
  151. def test_activity_docs_reject_stale_current_claims(self) -> None:
  152. for rel in ACTIVITY_DOCS:
  153. text = _read(rel)
  154. for pattern, message in STALE_AS_CURRENT:
  155. self.assertIsNone(
  156. pattern.search(text),
  157. f"{rel}: {message}",
  158. )
  159. def test_obsolete_io_mentions_are_negated(self) -> None:
  160. """If obsolete connectors appear, they must be clearly non-active."""
  161. obsolete = ("CON68", "CON34", "CON138", "CON139", "CON140", "CON141", "solenoid", "电磁铁")
  162. negate = re.compile(
  163. r"(?i)(?:不是|禁止|无|not\b|historical|归档|退役|obsolete|非活动|not\s+active|"
  164. r"已迁|作废|superseded|forbid)"
  165. )
  166. for rel in ACTIVITY_DOCS:
  167. text = _read(rel)
  168. for token in obsolete:
  169. if token.lower() not in text.lower() and token not in text:
  170. continue
  171. # Allow token only in sentences/lines that negate active use.
  172. for line in text.splitlines():
  173. if token.lower() not in line.lower() and token not in line:
  174. continue
  175. self.assertIsNotNone(
  176. negate.search(line),
  177. f"{rel}: obsolete token {token!r} on non-negated line: {line!r}",
  178. )
  179. def test_root_runtime_residue_not_tracked_or_present(self) -> None:
  180. for name in ROOT_RESIDUE:
  181. path = ROOT / name
  182. self.assertFalse(path.exists(), f"root residue still present: {name}")
  183. ignored = (ROOT / ".gitignore").read_text(encoding="utf-8")
  184. for token in (
  185. ".run_id",
  186. ".run_id_linux",
  187. "evidence-batch-summary.txt",
  188. "td_20260804_*.log*",
  189. ):
  190. self.assertIn(token, ignored, f".gitignore missing {token!r}")
  191. hist = ROOT / "docs/historical/evidence-batch-summary-linux-continue-20260804T155114Z.txt"
  192. self.assertTrue(hist.is_file(), "expected migrated evidence-batch summary")
  193. hist_readme = _read("docs/historical/README.md")
  194. self.assertIn("pending final tip TD", hist_readme)
  195. self.assertIn("2-axis", hist_readme.lower() + hist_readme)
  196. self.assertRegex(hist_readme, r"(?i)not.*old solenoid|not.*solenoid/2-axis|不是.*solenoid|不是.*电磁铁|Root activity docs are \*\*not\*\*")
  197. def test_cleanup_runtime_residue_helper_removes_root_probes(self) -> None:
  198. script = ROOT / "tools/cleanup_runtime_residue.py"
  199. self.assertTrue(script.is_file())
  200. with tempfile.TemporaryDirectory(prefix="prod2u-residue-") as tmp:
  201. fake_root = Path(tmp)
  202. (fake_root / "AGENTS.md").write_text("x\n", encoding="utf-8")
  203. proj = fake_root / "projects" / "prod2u-door-temp"
  204. proj.mkdir(parents=True)
  205. (proj / "project.json").write_text("{}\n", encoding="utf-8")
  206. (fake_root / ".run_id").write_text("run\n", encoding="utf-8")
  207. (fake_root / ".run_id_linux").write_text("run\n", encoding="utf-8")
  208. (fake_root / "evidence-batch-summary.txt").write_text("old\n", encoding="utf-8")
  209. (fake_root / "td_20260804_probe.log").write_text("log\n", encoding="utf-8")
  210. nested = fake_root / "runs" / "td_20260804_probe.log"
  211. nested.parent.mkdir()
  212. nested.write_text("keep\n", encoding="utf-8")
  213. result = subprocess.run(
  214. ["python3", "-B", str(script), "--root", str(fake_root), "--json"],
  215. check=True,
  216. capture_output=True,
  217. text=True,
  218. )
  219. self.assertIn('"count": 4', result.stdout)
  220. self.assertFalse((fake_root / ".run_id").exists())
  221. self.assertFalse((fake_root / "evidence-batch-summary.txt").exists())
  222. self.assertFalse((fake_root / "td_20260804_probe.log").exists())
  223. self.assertTrue(nested.exists())
  224. if __name__ == "__main__":
  225. unittest.main()