test_activity_docs_truth.py 9.7 KB

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