test_activity_docs_truth.py 11 KB

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