verify_pid_study.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. #!/usr/bin/env python3
  2. """Static source-boundary checks for the top-level CPLD PID project."""
  3. from __future__ import annotations
  4. import json
  5. import re
  6. import sys
  7. from pathlib import Path
  8. ROOT = Path(__file__).resolve().parents[1]
  9. ACTIVE_ROOTS = ["src", "tests", "tools", "td", "config", "constraints", "docs", "schemas"]
  10. FORBIDDEN_OLD_MARKERS = [
  11. "chipwatcher_" + "runner",
  12. "chipwatcher_python_ui_" + "template",
  13. "pywin" + "auto",
  14. "pyauto" + "gui",
  15. "System.Windows." + "Automation",
  16. "PowerShell UI" + "Automation",
  17. "powershell_" + "uia",
  18. "TD" + "5.6",
  19. "TD " + "5.6",
  20. "5.6.53426",
  21. ]
  22. FORBIDDEN_OLD_PATH_MARKERS = [
  23. re.compile(r"(?<![A-Za-z0-9_])workspace[\\/]"),
  24. ]
  25. GENERATED_DIRS = ["workspace", "dist", "tmp", "_archive", "td/build"]
  26. GENERATED_GLOBS = ["td/project/*_Runs"]
  27. TD_GUI_RUNTIME_DIRS = ["td/project/log"]
  28. GENERATED_SUFFIXES = {".area", ".bid", ".bit", ".data", ".db", ".dmp", ".f", ".log", ".logw", ".pyc", ".timing", ".tsm"}
  29. REQUIRED_FILES = [
  30. "README.zh-CN.md",
  31. "AGENTS.md",
  32. ".gitignore",
  33. "config/pid_channels.json",
  34. "constraints/pid_study_01.adc",
  35. "constraints/pid_study_01.sdc",
  36. "docs/fact-source-boundary.zh-CN.md",
  37. "docs/fake-stress-verification-v2.zh-CN.md",
  38. "docs/high-temp-chamber-design-v2.zh-CN.md",
  39. "docs/bmc-uart-temperature-protocol-v2.zh-CN.md",
  40. "docs/interface-contract.zh-CN.md",
  41. "docs/pid-safety-contract-v2.zh-CN.md",
  42. "docs/j6-host-sensor-real-fan-ptc-debug921-runbook.zh-CN.md",
  43. "docs/verification-plan.zh-CN.md",
  44. "docs/verification-gates-v2.zh-CN.md",
  45. "docs/offline-fidelity-gate-v1.zh-CN.md",
  46. "docs/uart-rs232-validation-strategy.zh-CN.md",
  47. "src/rtl/pid_core.v",
  48. "src/rtl/uart_rx_8n1.v",
  49. "src/rtl/uart_tx_8n1.v",
  50. "src/rtl/bmc_uart_phy_bridge.v",
  51. "src/rtl/debug_probe_bus.v",
  52. "src/rtl/bmc_uart_ascii_poll.v",
  53. "src/rtl/safety_supervisor.v",
  54. "src/top/system_contract_top.v",
  55. "src/top/uart_rs232_j6_host_sensor_real_fan_ptc_debug921_top.v",
  56. "tests/sim/pid_core_tb.v",
  57. "tests/sim/uart_8n1_phy_tb.v",
  58. "tests/sim/uart_rx_error_tb.v",
  59. "tests/sim/bmc_uart_phy_bridge_tb.v",
  60. "tests/formal/fault_implies_ptc_off.sby",
  61. "tests/cocotb/run_pid_core_smoke.py",
  62. "tests/fixtures/bad_icarus_tb.v",
  63. "tools/uart_receipts.py",
  64. "tools/pin_inventory.py",
  65. "tools/pid_harness.py",
  66. "tools/toolchain_probe.py",
  67. "td/uart_rs232_j6_host_sensor_real_fan_ptc_debug921_primary.prj",
  68. "schemas/uart_preflight_receipt.schema.json",
  69. "schemas/bmc_uart_protocol_receipt.schema.json",
  70. "schemas/uart_loopback_receipt.schema.json",
  71. "schemas/uart_board_sram_receipt.schema.json",
  72. "schemas/uart_soak_receipt.schema.json",
  73. "schemas/final_gate_v2_receipt.schema.json",
  74. "schemas/offline_fidelity_gate_v1_receipt.schema.json",
  75. "config/pin_inventory.mini.json",
  76. "config/pin_inventory.product.placeholder.json",
  77. "reference/original/MANIFEST.zh-CN.md",
  78. ]
  79. FORBIDDEN_ACTIVE_PATHS = [
  80. "tools/restructure_pid_study.py",
  81. "tools/chipwatcher_" + "runner.py",
  82. "tools/chipwatcher_python_ui_" + "template.py",
  83. "tools/chipwatcher_preflight.py",
  84. "tools/chipwatcher_receipts.py",
  85. "tools/computer_use/chipwatcher_fake_run.mjs",
  86. "src/rtl/debug_chipwatcher_bus.v",
  87. "src/top/chipwatcher_fake_stress_top.v",
  88. "td/project/chipwatcher_fake_stress.al",
  89. "td/syn/chipwatcher_fake_stress.prj",
  90. "td/phy/chipwatcher_fake_stress.prj",
  91. "td/phy/chipwatcher_fake_stress.cwc",
  92. "schemas/chipwatcher_gui_layer_receipt.schema.json",
  93. "schemas/chipwatcher_computer_use_receipt.schema.json",
  94. "schemas/chipwatcher_soak_receipt.schema.json",
  95. "td/pid_study_01.al",
  96. "td/syn/run_led_01.prj",
  97. "td/phy/run_led_01.prj",
  98. "src/top/run_led_01.v",
  99. ]
  100. def rel(path: Path) -> str:
  101. return path.relative_to(ROOT).as_posix()
  102. def read_text(path: Path) -> str:
  103. return path.read_text(encoding="utf-8", errors="replace")
  104. def is_under(path: Path, directory: Path) -> bool:
  105. try:
  106. resolved_path = path.resolve()
  107. resolved_dir = directory.resolve()
  108. except OSError:
  109. return False
  110. return resolved_path == resolved_dir or resolved_dir in resolved_path.parents
  111. def generated_runtime_paths() -> list[Path]:
  112. paths = [ROOT / item for item in GENERATED_DIRS]
  113. for pattern in GENERATED_GLOBS:
  114. paths.extend(ROOT.glob(pattern))
  115. return paths
  116. def is_generated_runtime_path(path: Path) -> bool:
  117. return any(is_under(path, generated) for generated in generated_runtime_paths() if generated.exists())
  118. def fail(errors: list[str], message: str) -> None:
  119. errors.append(message)
  120. def active_text_files() -> list[Path]:
  121. files: list[Path] = []
  122. for root_name in ACTIVE_ROOTS:
  123. root = ROOT / root_name
  124. if not root.exists():
  125. continue
  126. for path in root.rglob("*"):
  127. if not path.is_file():
  128. continue
  129. if is_generated_runtime_path(path):
  130. continue
  131. if path.suffix.lower() in GENERATED_SUFFIXES:
  132. continue
  133. if path.suffix.lower() in {".png", ".jpg", ".jpeg", ".gif", ".pdf", ".brd", ".dsn"}:
  134. continue
  135. files.append(path)
  136. return files
  137. def check_required_files(errors: list[str]) -> None:
  138. for item in REQUIRED_FILES:
  139. if not (ROOT / item).is_file():
  140. fail(errors, f"missing required file: {item}")
  141. for item in FORBIDDEN_ACTIVE_PATHS:
  142. if (ROOT / item).exists():
  143. fail(errors, f"legacy path must not be active: {item}")
  144. for item in GENERATED_DIRS:
  145. if (ROOT / item).exists():
  146. fail(errors, f"generated/deprecated directory must not exist in active tree: {item}")
  147. for pattern in GENERATED_GLOBS:
  148. for path in ROOT.glob(pattern):
  149. fail(errors, f"generated/deprecated directory must not exist in active tree: {rel(path)}")
  150. for item in TD_GUI_RUNTIME_DIRS:
  151. path = ROOT / item
  152. if path.exists():
  153. contents = [child for child in path.rglob("*") if child.is_file()]
  154. if any(child.stat().st_size for child in contents):
  155. fail(errors, f"td gui runtime log directory must be cleaned before final static gate: {item}")
  156. def check_config(errors: list[str]) -> None:
  157. path = ROOT / "config/pid_channels.json"
  158. if not path.is_file():
  159. return
  160. data = json.loads(read_text(path))
  161. if data.get("schema") != "pid-high-temp-chamber-config-v2":
  162. fail(errors, "config schema must be pid-high-temp-chamber-config-v2")
  163. if data.get("project") != "pid_study_01":
  164. fail(errors, "config project id remains pid_study_01 after promotion")
  165. channels = data.get("channels", {})
  166. if channels.get("logical_channel_count") != 24:
  167. fail(errors, "config must define 24 logical channels")
  168. if channels.get("channels_per_airflow_zone") != [6, 12, 6]:
  169. fail(errors, "config must define 6/12/6 airflow zones")
  170. if channels.get("channel_to_cpu") != ([0] * 12 + [1] * 12):
  171. fail(errors, "config must map CPU0 to channels 0..11 and CPU1 to 12..23")
  172. safety = data.get("safety", {})
  173. if safety.get("overtemp_limit_q") != 90:
  174. fail(errors, "hard overtemp limit must be 90C")
  175. if safety.get("global_bmc_timeout_cycles") != 10:
  176. fail(errors, "BMC stale timeout must be 10 one-second cycles")
  177. def check_active_text_boundaries(errors: list[str]) -> None:
  178. for path in active_text_files():
  179. if path == ROOT / "tools" / "verify_pid_study.py":
  180. continue
  181. text = read_text(path)
  182. for marker in FORBIDDEN_OLD_PATH_MARKERS:
  183. if marker.search(text):
  184. fail(errors, f"forbidden old workspace path marker in {rel(path)}")
  185. for marker in FORBIDDEN_OLD_MARKERS:
  186. if marker in text:
  187. fail(errors, f"forbidden old marker {marker!r} in {rel(path)}")
  188. if "__pycache__" in path.parts:
  189. fail(errors, f"pycache artifact must not be active: {rel(path)}")
  190. if path.suffix.lower() in GENERATED_SUFFIXES:
  191. fail(errors, f"generated artifact must not be active: {rel(path)}")
  192. def check_constraints(errors: list[str]) -> None:
  193. adc = ROOT / "constraints/pid_study_01.adc"
  194. sdc = ROOT / "constraints/pid_study_01.sdc"
  195. if adc.is_file():
  196. text = read_text(adc)
  197. ports = set(re.findall(r"set_pin_assignment\s+\{\s*([^}]+?)\s*\}", text))
  198. expected = {"sys_clk", "sys_rst_n", *{f"led[{index}]" for index in range(8)}}
  199. if ports != expected:
  200. fail(errors, f"ADC must constrain only MINI smoke ports, got {sorted(ports)}")
  201. for forbidden in ["ptc", "fan", "i2c", "rs232", "rs485", "motor", "tach"]:
  202. if forbidden in text.lower():
  203. fail(errors, f"ADC must not bind real board interface marker: {forbidden}")
  204. if sdc.is_file():
  205. text = read_text(sdc)
  206. if "create_clock" not in text or "period 40" not in text:
  207. fail(errors, "SDC must define 25 MHz sys_clk with 40 ns period")
  208. def check_td_sources(errors: list[str]) -> None:
  209. prj = ROOT / "td/uart_rs232_j6_host_sensor_real_fan_ptc_debug921_primary.prj"
  210. if not prj.is_file():
  211. return
  212. data = prj.read_bytes()
  213. text = read_text(prj)
  214. if data.startswith(b"\xef\xbb\xbf"):
  215. fail(errors, "td/uart_rs232_j6_host_sensor_real_fan_ptc_debug921_primary.prj must be UTF-8 without BOM for TD CLI")
  216. for required in [
  217. "uart_rs232_j6_host_sensor_real_fan_ptc_debug921_top",
  218. "../src/rtl/uart_rx_8n1.v",
  219. "../src/rtl/uart_tx_8n1.v",
  220. "../src/rtl/bmc_uart_raw48_poll_lite.v",
  221. "../src/top/uart_rs232_j6_host_sensor_real_fan_ptc_debug921_top.v",
  222. "../constraints/uart_rs232_j6_host_sensor_real_fan_ptc_debug921_primary.adc",
  223. ]:
  224. if required not in text:
  225. fail(errors, f"td/uart_rs232_j6_host_sensor_real_fan_ptc_debug921_primary.prj missing {required}")
  226. if "td/build" in text or "chipwatcher" in text.lower():
  227. fail(errors, "td/uart_rs232_j6_host_sensor_real_fan_ptc_debug921_primary.prj must not reference td/build or ChipWatcher")
  228. def check_uart_profile(errors: list[str]) -> None:
  229. top = ROOT / "src/top/uart_rs232_j6_host_sensor_real_fan_ptc_debug921_top.v"
  230. if top.is_file():
  231. text = read_text(top)
  232. for required in [
  233. "bmc_uart_rx",
  234. "bmc_uart_tx",
  235. "host_uart_rx",
  236. "host_uart_tx",
  237. "debug_uart_rx",
  238. "debug_uart_tx",
  239. "ptc_pwm_out",
  240. "fan_pwm_out",
  241. "fan_fg_in",
  242. "fan_install_in",
  243. "uart_rx_8n1",
  244. "uart_tx_8n1",
  245. "bmc_uart_raw48_poll_lite",
  246. "SET PTC 01..24",
  247. "SET FAN 01..04",
  248. "J6TEMP",
  249. ]:
  250. if required not in text:
  251. fail(errors, f"src/top/uart_rs232_j6_host_sensor_real_fan_ptc_debug921_top.v missing {required}")
  252. contract = ROOT / "src/top/system_contract_top.v"
  253. if contract.is_file():
  254. text = read_text(contract)
  255. for required in ["debug_uart_tx", "debug_probe_bus", "debug_probe_bus u_debug_probe_bus"]:
  256. if required not in text:
  257. fail(errors, f"src/top/system_contract_top.v missing {required}")
  258. def check_harness(errors: list[str]) -> None:
  259. text = read_text(ROOT / "tools/pid_harness.py")
  260. for required in [
  261. "uart-preflight",
  262. "uart-protocol-offline",
  263. "uart-validate",
  264. "final-gate-v2",
  265. "offline-fidelity-gate-v1",
  266. "offline_fidelity_gate_v1_payload",
  267. "clean-runtime",
  268. "toolchain-clean",
  269. "validate_uart_receipt",
  270. "validate_offline_fidelity_gate_v1_receipt",
  271. "uart_rs232_j6_host_sensor_real_fan_ptc_debug921_top",
  272. ]:
  273. if required not in text:
  274. fail(errors, f"pid_harness.py missing required marker: {required}")
  275. for forbidden in ["chipwatcher-gui-prepare", "chipwatcher-gui-smoke", "chipwatcher-run", "cmd_chipwatcher_gui"]:
  276. if forbidden in text:
  277. fail(errors, f"pid_harness.py must not expose legacy GUI command: {forbidden}")
  278. receipt_text = read_text(ROOT / "tools/uart_receipts.py")
  279. for required in [
  280. "PROGRAM_SRAM",
  281. "fail_forbidden_action",
  282. "validate_uart_receipt",
  283. "validate_final_gate_v2_receipt",
  284. "validate_offline_fidelity_gate_v1_receipt",
  285. "OFFLINE_FIDELITY_REQUIRED_LAYERS",
  286. "OFFLINE_FIDELITY_FORBIDDEN_LAYERS",
  287. "powershell_dotnet_limited",
  288. ]:
  289. if required not in receipt_text:
  290. fail(errors, f"uart_receipts.py missing required marker: {required}")
  291. def check_docs(errors: list[str]) -> None:
  292. docs = {
  293. "README.zh-CN.md": ["src/rtl", "src/top", "tests/sim", "uart-validate", "final-gate-v2"],
  294. "AGENTS.md": ["workspace", "final-gate-v2", "flash"],
  295. "docs/verification-plan.zh-CN.md": ["uart-validate", "final-gate-v2", "blocked_", "fail_"],
  296. "docs/j6-host-sensor-real-fan-ptc-debug921-runbook.zh-CN.md": [
  297. "blocked_pin_conflict_with_locked_debug_uart",
  298. "COM8",
  299. "COM7",
  300. "GET SENSOR",
  301. ],
  302. "docs/verification-gates-v2.zh-CN.md": ["strict", "uart-board-sram-receipt-v1", "final-gate-v2"],
  303. "docs/offline-fidelity-gate-v1.zh-CN.md": [
  304. "offline-fidelity-gate-v1",
  305. "protocol_fuzz",
  306. "cocotb_virtual_lab",
  307. "virtual_driver_lab",
  308. "toolchain-clean",
  309. "thermal_digital_twin",
  310. "hardware",
  311. "uart_strict",
  312. "chipwatcher",
  313. "thermal_performance_pass",
  314. "does not certify real hardware or thermal performance",
  315. "uart_rs232_j6_host_sensor_real_fan_ptc_debug921_top",
  316. ],
  317. "docs/uart-rs232-validation-strategy.zh-CN.md": ["debug UART", "register dump", "software safety", "thermal performance"],
  318. }
  319. for item, phrases in docs.items():
  320. path = ROOT / item
  321. if not path.is_file():
  322. continue
  323. text = read_text(path)
  324. for phrase in phrases:
  325. if phrase not in text:
  326. fail(errors, f"{item} missing documentation phrase: {phrase}")
  327. def main() -> int:
  328. errors: list[str] = []
  329. check_required_files(errors)
  330. check_config(errors)
  331. check_active_text_boundaries(errors)
  332. check_constraints(errors)
  333. check_td_sources(errors)
  334. check_uart_profile(errors)
  335. check_harness(errors)
  336. check_docs(errors)
  337. if errors:
  338. for error in errors:
  339. print(f"FAIL: {error}", file=sys.stderr)
  340. return 1
  341. print("PASS: top-level CPLD PID static checks")
  342. return 0
  343. if __name__ == "__main__":
  344. raise SystemExit(main())