| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- #!/usr/bin/env python3
- """Fail-closed consistency gates for active status docs vs current 3-axis truth.
- Activity docs must assert X/Y/Z, six limits, 450/5000/500, permanent EN, hard
- truncate, and bound tip-TD/bitstream identity. Stale 2-axis/solenoid IO,
- runtime-limit-inert policy, and old bit/commit-as-current claims must not reappear.
- """
- from __future__ import annotations
- import re
- import subprocess
- import tempfile
- import unittest
- from pathlib import Path
- ROOT = Path(__file__).resolve().parents[2]
- ARTIFACT_SOURCE_COMMIT = "0de9f93988344e3c11931482902196b02ac7a713"
- ARTIFACT_SOURCE_SHORT = "0de9f939"
- BITSTREAM_SHA256 = (
- "54ce967da4dce40d7cbb6821eb1e8d7d95b3bdda27a7edf30cbcad7131cb667c"
- )
- BITSTREAM_SHORT = "54ce967d"
- TD_TOOL_BUILD = "6.2.2.200067"
- TRUNCATE_SEMANTICS = "truncate_current_high_pulse_immediately_after_synchronized_limit"
- PRIOR_BIT_SHORT = "45f548ab"
- PRIOR_BIT_COMMIT = "bb515af"
- BASELINE_TIP_SHORT = "cdf9f76"
- PENDING_MARKER = "pending final tip TD"
- ACTIVITY_DOCS = (
- "README.zh-CN.md",
- "FINAL_DELIVERY_REPORT.md",
- "ACCEPTANCE_MATRIX.md",
- "OPEN_QUESTIONS.md",
- "PLAN.md",
- "docs/TD_USAGE.zh-CN.md",
- "docs/HARDWARE_TEST_ORDER_WAVEFORMS.zh-CN.md",
- )
- # Must appear in every activity doc.
- PER_DOC_MARKERS = (
- ARTIFACT_SOURCE_SHORT,
- BITSTREAM_SHORT,
- PRIOR_BIT_SHORT,
- PRIOR_BIT_COMMIT,
- )
- # Must appear somewhere in the activity-doc corpus.
- CORPUS_REQUIRED = (
- ARTIFACT_SOURCE_COMMIT,
- BITSTREAM_SHA256,
- TD_TOOL_BUILD,
- TRUNCATE_SEMANTICS,
- "permanent_high",
- "450",
- "5000",
- "500",
- "CON104",
- "CON105",
- "CON106",
- "CON120",
- "CON12",
- "CON129",
- BASELINE_TIP_SHORT,
- PRIOR_BIT_SHORT,
- PRIOR_BIT_COMMIT,
- "ARTIFACT-SOURCE-COMMIT",
- )
- # Superseded identifiers / policies that must not be presented as current tip truth.
- STALE_AS_CURRENT = (
- (
- re.compile(
- r"(?i)FINAL_COMMIT\s*[|::=]*\s*`?da7b05a[0-9a-f]*`?"
- ),
- "stale 2-axis FINAL_COMMIT da7b05a presented as current",
- ),
- (
- re.compile(
- r"(?i)Bitstream SHA-256\s*[|::=]*\s*`?ce3157c5[0-9a-f]*`?"
- ),
- "stale bitstream ce3157c5 presented as current",
- ),
- (
- re.compile(
- r"(?i)TD built commit\s*[|::=]*\s*`?8f28660[0-9a-f]*`?"
- ),
- "stale TD 8f28660 presented as current",
- ),
- (
- re.compile(
- r"(?i)(?:(?:as|@)\s*tip|tip\s+(?:bitstream|bit|TD|证据)|"
- r"当前\s*(?:tip\s*)?(?:bitstream|位流|闭包证据))\s*[`:]?\s*45f548ab"
- ),
- "prior bit 45f548ab presented as tip/current",
- ),
- (
- re.compile(
- r"(?i)(?:runtime\s+limits?\s+are\s+inert|"
- r"(?<!not\s)(?<!non-)runtime\s+limit\s+inert|"
- r"限位对普通\s*jog\s*\*?惰性\*?|"
- r"完成后限位对普通 jog\s*\*?惰性\*?)"
- ),
- "runtime limit inert presented as current policy",
- ),
- (
- re.compile(
- r"(?i)(?:进风(?:门)?默认\*?电磁铁\*?|intake_solenoid_out\s*[::].*\bP2\b|"
- r"偏移\s*8\s*进风门\s*\|\s*默认\*\*电磁铁\*\*)"
- ),
- "solenoid intake presented as active product path",
- ),
- (
- re.compile(
- r"(?i)(?:X\s*限位[^\n]{0,40}CON68|Y\s*限位[^\n]{0,40}CON34|"
- r"CON68\.3\s*/\s*M6|CON34\.3\s*/\s*F9|"
- r"X\s*电机[^\n]{0,20}CON139|Y\s*电机[^\n]{0,20}CON138|"
- r"进风电磁铁[^\n]{0,20}CON141)"
- ),
- "obsolete CON68/CON34/CON138-141 mapping presented as active probe",
- ),
- (
- re.compile(r"(?i)(?:FINAL_COMMIT|文档对齐 tip|worktree 起点 tip|Full matrix @ tip)\s*[|::]*\s*`?f82c41e"),
- "stale tip f82c41e presented as current FINAL/tip",
- ),
- (
- re.compile(r"(?i)bitstream SHA-256\s*`?a7b5c119"),
- "stale bitstream a7b5c119 presented as current",
- ),
- (
- re.compile(re.escape(PENDING_MARKER)),
- "unbound pending marker still present in activity docs after tip TD bind",
- ),
- )
- ROOT_RESIDUE = (
- ".run_id",
- ".run_id_linux",
- "evidence-batch-summary.txt",
- )
- def _read(rel: str) -> str:
- return (ROOT / rel).read_text(encoding="utf-8")
- class ActivityDocsTruthTests(unittest.TestCase):
- def test_activity_docs_exist(self) -> None:
- missing = [rel for rel in ACTIVITY_DOCS if not (ROOT / rel).is_file()]
- self.assertEqual(missing, [], f"missing activity docs: {missing}")
- def test_each_activity_doc_carries_bound_identity_and_prior_bit_markers(self) -> None:
- for rel in ACTIVITY_DOCS:
- text = _read(rel)
- for marker in PER_DOC_MARKERS:
- self.assertIn(
- marker,
- text,
- f"{rel} missing required marker {marker!r}",
- )
- def test_activity_docs_agree_on_three_axis_corpus(self) -> None:
- corpus = "\n".join(_read(rel) for rel in ACTIVITY_DOCS)
- for marker in CORPUS_REQUIRED:
- self.assertIn(marker, corpus, f"activity corpus missing {marker!r}")
- self.assertRegex(corpus, r"(?i)(?:X\s*/\s*Y\s*/\s*Z|三轴)")
- self.assertRegex(corpus, r"(?i)(?:六限位|six limits)")
- self.assertRegex(corpus, r"(?i)(?:硬截断|立即截断|hard truncate)")
- self.assertRegex(corpus, r"(?i)(?:Hardware|硬件)[^\n]{0,80}(?:NOT RUN|not_run)")
- self.assertRegex(
- corpus,
- r"(?i)45f548ab[^\n]{0,80}bb515af|bb515af[^\n]{0,80}45f548ab",
- )
- # Active mapping must include 3-axis connectors somewhere.
- self.assertRegex(corpus, r"CON104")
- self.assertRegex(corpus, r"CON105")
- self.assertRegex(corpus, r"CON106")
- self.assertRegex(corpus, r"CON120")
- self.assertRegex(corpus, r"CON129")
- def test_activity_docs_reject_stale_current_claims(self) -> None:
- for rel in ACTIVITY_DOCS:
- text = _read(rel)
- for pattern, message in STALE_AS_CURRENT:
- self.assertIsNone(
- pattern.search(text),
- f"{rel}: {message}",
- )
- def test_obsolete_io_mentions_are_negated(self) -> None:
- """If obsolete connectors appear, they must be clearly non-active."""
- obsolete = ("CON68", "CON34", "CON138", "CON139", "CON140", "CON141", "solenoid", "电磁铁")
- negate = re.compile(
- r"(?i)(?:不是|禁止|无|not\b|historical|归档|退役|obsolete|非活动|not\s+active|"
- r"已迁|作废|superseded|forbid)"
- )
- for rel in ACTIVITY_DOCS:
- text = _read(rel)
- for token in obsolete:
- if token.lower() not in text.lower() and token not in text:
- continue
- # Allow token only in sentences/lines that negate active use.
- for line in text.splitlines():
- if token.lower() not in line.lower() and token not in line:
- continue
- self.assertIsNotNone(
- negate.search(line),
- f"{rel}: obsolete token {token!r} on non-negated line: {line!r}",
- )
- def test_root_runtime_residue_not_tracked_or_present(self) -> None:
- for name in ROOT_RESIDUE:
- path = ROOT / name
- self.assertFalse(path.exists(), f"root residue still present: {name}")
- ignored = (ROOT / ".gitignore").read_text(encoding="utf-8")
- for token in (
- ".run_id",
- ".run_id_linux",
- "evidence-batch-summary.txt",
- "td_20260804_*.log*",
- ):
- self.assertIn(token, ignored, f".gitignore missing {token!r}")
- hist = ROOT / "docs/historical/evidence-batch-summary-linux-continue-20260804T155114Z.txt"
- self.assertTrue(hist.is_file(), "expected migrated evidence-batch summary")
- hist_readme = _read("docs/historical/README.md")
- self.assertIn(ARTIFACT_SOURCE_SHORT, hist_readme + _read("FINAL_DELIVERY_REPORT.md"))
- self.assertIn("2-axis", hist_readme.lower() + hist_readme)
- self.assertRegex(hist_readme, r"(?i)not.*old solenoid|not.*solenoid/2-axis|不是.*solenoid|不是.*电磁铁|Root activity docs are \*\*not\*\*")
- def test_cleanup_runtime_residue_helper_removes_root_probes(self) -> None:
- script = ROOT / "tools/cleanup_runtime_residue.py"
- self.assertTrue(script.is_file())
- with tempfile.TemporaryDirectory(prefix="prod2u-residue-") as tmp:
- fake_root = Path(tmp)
- (fake_root / "AGENTS.md").write_text("x\n", encoding="utf-8")
- proj = fake_root / "projects" / "prod2u-door-temp"
- proj.mkdir(parents=True)
- (proj / "project.json").write_text("{}\n", encoding="utf-8")
- (fake_root / ".run_id").write_text("run\n", encoding="utf-8")
- (fake_root / ".run_id_linux").write_text("run\n", encoding="utf-8")
- (fake_root / "evidence-batch-summary.txt").write_text("old\n", encoding="utf-8")
- (fake_root / "td_20260804_probe.log").write_text("log\n", encoding="utf-8")
- nested = fake_root / "runs" / "td_20260804_probe.log"
- nested.parent.mkdir()
- nested.write_text("keep\n", encoding="utf-8")
- result = subprocess.run(
- ["python3", "-B", str(script), "--root", str(fake_root), "--json"],
- check=True,
- capture_output=True,
- text=True,
- )
- self.assertIn('"count": 4', result.stdout)
- self.assertFalse((fake_root / ".run_id").exists())
- self.assertFalse((fake_root / "evidence-batch-summary.txt").exists())
- self.assertFalse((fake_root / "td_20260804_probe.log").exists())
- self.assertTrue(nested.exists())
- if __name__ == "__main__":
- unittest.main()
|