test_activity_docs_truth.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #!/usr/bin/env python3
  2. """Fail-closed consistency gates for active status docs vs door-percent contract."""
  3. from __future__ import annotations
  4. import re
  5. import subprocess
  6. import tempfile
  7. import unittest
  8. from pathlib import Path
  9. ROOT = Path(__file__).resolve().parents[2]
  10. TRUNCATE_SEMANTICS = "truncate_current_high_pulse_immediately_after_synchronized_limit"
  11. PENDING_TIP_TD = "pending final tip TD"
  12. ACTIVITY_DOCS = (
  13. "README.zh-CN.md",
  14. "FINAL_DELIVERY_REPORT.md",
  15. "ACCEPTANCE_MATRIX.md",
  16. "OPEN_QUESTIONS.md",
  17. "PLAN.md",
  18. "docs/TD_USAGE.zh-CN.md",
  19. "docs/HARDWARE_TEST_ORDER_WAVEFORMS.zh-CN.md",
  20. )
  21. PER_DOC_MARKERS = (
  22. "百分比",
  23. "0x03",
  24. "0x83",
  25. )
  26. CORPUS_REQUIRED = (
  27. TRUNCATE_SEMANTICS,
  28. "permanent_high",
  29. "5000",
  30. "30",
  31. "CON104",
  32. "CON105",
  33. "CON106",
  34. "CON120",
  35. "CON12",
  36. "CON129",
  37. "GET_MOTOR_STATUS",
  38. "startup_complete",
  39. "latest-wins",
  40. )
  41. STALE_AS_CURRENT = (
  42. (
  43. re.compile(r"(?i)P4_FIXED_MOTOR_STEPS"),
  44. "FIXED_MOTOR presented as current",
  45. ),
  46. (
  47. re.compile(r"(?i)(?:进风(?:门)?默认\*?电磁铁\*?|intake_solenoid_out)"),
  48. "solenoid intake presented as active product path",
  49. ),
  50. (
  51. re.compile(
  52. r"(?i)(?:X\s*限位[^\n]{0,40}CON68|Y\s*限位[^\n]{0,40}CON34)"
  53. ),
  54. "obsolete CON68/CON34 mapping presented as active probe",
  55. ),
  56. )
  57. ROOT_RESIDUE = (
  58. ".run_id",
  59. ".run_id_linux",
  60. "evidence-batch-summary.txt",
  61. )
  62. def _read(rel: str) -> str:
  63. return (ROOT / rel).read_text(encoding="utf-8")
  64. class ActivityDocsTruthTests(unittest.TestCase):
  65. def test_activity_docs_exist(self) -> None:
  66. missing = [rel for rel in ACTIVITY_DOCS if not (ROOT / rel).is_file()]
  67. self.assertEqual(missing, [], f"missing activity docs: {missing}")
  68. def test_each_activity_doc_carries_percent_protocol_markers(self) -> None:
  69. for rel in ACTIVITY_DOCS:
  70. text = _read(rel)
  71. for marker in PER_DOC_MARKERS:
  72. self.assertIn(marker, text, f"{rel} missing {marker!r}")
  73. def test_activity_docs_agree_on_percent_corpus(self) -> None:
  74. corpus = "\n".join(_read(rel) for rel in ACTIVITY_DOCS)
  75. for marker in CORPUS_REQUIRED:
  76. self.assertIn(marker, corpus, f"activity corpus missing {marker!r}")
  77. self.assertRegex(corpus, r"(?i)(?:X\s*/\s*Y\s*/\s*Z|三轴)")
  78. self.assertRegex(corpus, r"(?i)(?:六限位|six limits)")
  79. self.assertRegex(corpus, r"(?i)(?:Hardware|硬件)[^\n]{0,80}(?:NOT RUN|not_run)")
  80. self.assertRegex(corpus, r"(?i)(?:破坏性|全开|full open)")
  81. # Door 00 is full open under new contract (must warn), not old jog heartbeat.
  82. self.assertRegex(corpus, r"(?i)(?:00.*=.*全开|0\s*=\s*full open|全开)")
  83. # Must not claim old 450-step jog as active contract without negation.
  84. for line in corpus.splitlines():
  85. if "450" in line and "jog" in line.lower():
  86. self.assertRegex(
  87. line,
  88. r"(?i)(?:旧|legacy|historical|废弃|破坏|不再|not)",
  89. f"450 jog line not negated: {line!r}",
  90. )
  91. def test_activity_docs_reject_stale_current_claims(self) -> None:
  92. for rel in ACTIVITY_DOCS:
  93. text = _read(rel)
  94. for pattern, message in STALE_AS_CURRENT:
  95. self.assertIsNone(pattern.search(text), f"{rel}: {message}")
  96. def test_final_delivery_mentions_pending_or_bound_td(self) -> None:
  97. text = _read("FINAL_DELIVERY_REPORT.md")
  98. self.assertTrue(
  99. PENDING_TIP_TD in text
  100. or "pending" in text.casefold()
  101. or "ARTIFACT-SOURCE" in text
  102. or "Bitstream SHA" in text
  103. or "intermediate" in text.casefold(),
  104. "FINAL_DELIVERY_REPORT must track TD bind / intermediate state",
  105. )
  106. def test_activity_docs_reject_merge_dev_as_this_round_step(self) -> None:
  107. for rel in ("PLAN.md", "README.zh-CN.md", "FINAL_DELIVERY_REPORT.md"):
  108. text = _read(rel)
  109. for line in text.splitlines():
  110. if re.search(r"合入\s*(干净\s*)?`?dev`?", line) and not re.search(
  111. r"(?i)(?:不|勿|禁止|未|不要|等待|独立决策|not|never|no\b)",
  112. line,
  113. ):
  114. self.fail(f"{rel} presents merge-to-dev as active step: {line!r}")
  115. def test_activity_docs_reject_stale_in_progress_when_fixes_landed(self) -> None:
  116. corpus = "\n".join(_read(rel) for rel in ACTIVITY_DOCS)
  117. # Must acknowledge P0/P1 landed or intermediate/TD-pending truth — not only "重构进行中".
  118. self.assertRegex(
  119. corpus,
  120. r"(?i)(?:已修|已落地|landed|P0/P1|intermediate|TD\s*(?:待|pending))",
  121. )
  122. # If claiming final TD pass, must not still say 重构进行中 as the only status.
  123. if re.search(r"(?i)TD[^\n]{0,40}pass", corpus) and "9cd778" not in corpus:
  124. self.assertNotRegex(corpus, r"重构进行中")
  125. def test_root_runtime_residue_not_tracked_or_present(self) -> None:
  126. for name in ROOT_RESIDUE:
  127. self.assertFalse((ROOT / name).exists(), f"root residue still present: {name}")
  128. ignored = (ROOT / ".gitignore").read_text(encoding="utf-8")
  129. for token in (".run_id", ".run_id_linux", "evidence-batch-summary.txt"):
  130. self.assertIn(token, ignored)
  131. def test_cleanup_runtime_residue_helper_removes_root_probes(self) -> None:
  132. script = ROOT / "tools/cleanup_runtime_residue.py"
  133. self.assertTrue(script.is_file())
  134. with tempfile.TemporaryDirectory(prefix="prod2u-residue-") as tmp:
  135. fake_root = Path(tmp)
  136. (fake_root / "AGENTS.md").write_text("x\n", encoding="utf-8")
  137. proj = fake_root / "projects" / "prod2u-door-temp"
  138. proj.mkdir(parents=True)
  139. (proj / "project.json").write_text("{}\n", encoding="utf-8")
  140. (fake_root / ".run_id").write_text("run\n", encoding="utf-8")
  141. (fake_root / ".run_id_linux").write_text("run\n", encoding="utf-8")
  142. (fake_root / "evidence-batch-summary.txt").write_text("old\n", encoding="utf-8")
  143. (fake_root / "td_20260804_probe.log").write_text("log\n", encoding="utf-8")
  144. nested = fake_root / "runs" / "td_20260804_probe.log"
  145. nested.parent.mkdir()
  146. nested.write_text("keep\n", encoding="utf-8")
  147. result = subprocess.run(
  148. ["python3", "-B", str(script), "--root", str(fake_root), "--json"],
  149. check=True,
  150. capture_output=True,
  151. text=True,
  152. )
  153. self.assertIn('"count": 4', result.stdout)
  154. self.assertFalse((fake_root / ".run_id").exists())
  155. self.assertTrue(nested.exists())
  156. if __name__ == "__main__":
  157. unittest.main()