test_activity_docs_truth.py 12 KB

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