#!/usr/bin/env python3 """Static source-boundary checks for the top-level CPLD PID project.""" from __future__ import annotations import json import re import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] ACTIVE_ROOTS = ["src", "tests", "tools", "td", "config", "constraints", "docs", "schemas"] FORBIDDEN_OLD_MARKERS = [ "chipwatcher_" + "runner", "chipwatcher_python_ui_" + "template", "pywin" + "auto", "pyauto" + "gui", "System.Windows." + "Automation", "PowerShell UI" + "Automation", "powershell_" + "uia", "TD" + "5.6", "TD " + "5.6", "5.6.53426", ] FORBIDDEN_OLD_PATH_MARKERS = [ re.compile(r"(? str: return path.relative_to(ROOT).as_posix() def read_text(path: Path) -> str: return path.read_text(encoding="utf-8", errors="replace") def is_under(path: Path, directory: Path) -> bool: try: resolved_path = path.resolve() resolved_dir = directory.resolve() except OSError: return False return resolved_path == resolved_dir or resolved_dir in resolved_path.parents def generated_runtime_paths() -> list[Path]: paths = [ROOT / item for item in GENERATED_DIRS] for pattern in GENERATED_GLOBS: paths.extend(ROOT.glob(pattern)) return paths def is_generated_runtime_path(path: Path) -> bool: return any(is_under(path, generated) for generated in generated_runtime_paths() if generated.exists()) def fail(errors: list[str], message: str) -> None: errors.append(message) def active_text_files() -> list[Path]: files: list[Path] = [] for root_name in ACTIVE_ROOTS: root = ROOT / root_name if not root.exists(): continue for path in root.rglob("*"): if not path.is_file(): continue if is_generated_runtime_path(path): continue if path.suffix.lower() in GENERATED_SUFFIXES: continue if path.suffix.lower() in {".png", ".jpg", ".jpeg", ".gif", ".pdf", ".brd", ".dsn"}: continue files.append(path) return files def check_required_files(errors: list[str]) -> None: for item in REQUIRED_FILES: if not (ROOT / item).is_file(): fail(errors, f"missing required file: {item}") for item in FORBIDDEN_ACTIVE_PATHS: if (ROOT / item).exists(): fail(errors, f"legacy path must not be active: {item}") for item in GENERATED_DIRS: if (ROOT / item).exists(): fail(errors, f"generated/deprecated directory must not exist in active tree: {item}") for pattern in GENERATED_GLOBS: for path in ROOT.glob(pattern): fail(errors, f"generated/deprecated directory must not exist in active tree: {rel(path)}") for item in TD_GUI_RUNTIME_DIRS: path = ROOT / item if path.exists(): contents = [child for child in path.rglob("*") if child.is_file()] if any(child.stat().st_size for child in contents): fail(errors, f"td gui runtime log directory must be cleaned before final static gate: {item}") def check_config(errors: list[str]) -> None: path = ROOT / "config/pid_channels.json" if not path.is_file(): return data = json.loads(read_text(path)) if data.get("schema") != "pid-high-temp-chamber-config-v2": fail(errors, "config schema must be pid-high-temp-chamber-config-v2") if data.get("project") != "pid_study_01": fail(errors, "config project id remains pid_study_01 after promotion") channels = data.get("channels", {}) if channels.get("logical_channel_count") != 24: fail(errors, "config must define 24 logical channels") if channels.get("channels_per_airflow_zone") != [6, 12, 6]: fail(errors, "config must define 6/12/6 airflow zones") if channels.get("channel_to_cpu") != ([0] * 12 + [1] * 12): fail(errors, "config must map CPU0 to channels 0..11 and CPU1 to 12..23") safety = data.get("safety", {}) if safety.get("overtemp_limit_q") != 90: fail(errors, "hard overtemp limit must be 90C") if safety.get("global_bmc_timeout_cycles") != 10: fail(errors, "BMC stale timeout must be 10 one-second cycles") def check_active_text_boundaries(errors: list[str]) -> None: for path in active_text_files(): if path == ROOT / "tools" / "verify_pid_study.py": continue text = read_text(path) for marker in FORBIDDEN_OLD_PATH_MARKERS: if marker.search(text): fail(errors, f"forbidden old workspace path marker in {rel(path)}") for marker in FORBIDDEN_OLD_MARKERS: if marker in text: fail(errors, f"forbidden old marker {marker!r} in {rel(path)}") if "__pycache__" in path.parts: fail(errors, f"pycache artifact must not be active: {rel(path)}") if path.suffix.lower() in GENERATED_SUFFIXES: fail(errors, f"generated artifact must not be active: {rel(path)}") def check_constraints(errors: list[str]) -> None: adc = ROOT / "constraints/pid_study_01.adc" sdc = ROOT / "constraints/pid_study_01.sdc" if adc.is_file(): text = read_text(adc) ports = set(re.findall(r"set_pin_assignment\s+\{\s*([^}]+?)\s*\}", text)) expected = {"sys_clk", "sys_rst_n", *{f"led[{index}]" for index in range(8)}} if ports != expected: fail(errors, f"ADC must constrain only MINI smoke ports, got {sorted(ports)}") for forbidden in ["ptc", "fan", "i2c", "rs232", "rs485", "motor", "tach"]: if forbidden in text.lower(): fail(errors, f"ADC must not bind real board interface marker: {forbidden}") if sdc.is_file(): text = read_text(sdc) if "create_clock" not in text or "period 40" not in text: fail(errors, "SDC must define 25 MHz sys_clk with 40 ns period") def check_td_sources(errors: list[str]) -> None: prj = ROOT / "td/uart_rs232_j6_host_sensor_real_fan_ptc_debug921_primary.prj" if not prj.is_file(): return data = prj.read_bytes() text = read_text(prj) if data.startswith(b"\xef\xbb\xbf"): fail(errors, "td/uart_rs232_j6_host_sensor_real_fan_ptc_debug921_primary.prj must be UTF-8 without BOM for TD CLI") for required in [ "uart_rs232_j6_host_sensor_real_fan_ptc_debug921_top", "../src/rtl/uart_rx_8n1.v", "../src/rtl/uart_tx_8n1.v", "../src/rtl/bmc_uart_raw48_poll_lite.v", "../src/top/uart_rs232_j6_host_sensor_real_fan_ptc_debug921_top.v", "../constraints/uart_rs232_j6_host_sensor_real_fan_ptc_debug921_primary.adc", ]: if required not in text: fail(errors, f"td/uart_rs232_j6_host_sensor_real_fan_ptc_debug921_primary.prj missing {required}") if "td/build" in text or "chipwatcher" in text.lower(): fail(errors, "td/uart_rs232_j6_host_sensor_real_fan_ptc_debug921_primary.prj must not reference td/build or ChipWatcher") def check_uart_profile(errors: list[str]) -> None: top = ROOT / "src/top/uart_rs232_j6_host_sensor_real_fan_ptc_debug921_top.v" if top.is_file(): text = read_text(top) for required in [ "bmc_uart_rx", "bmc_uart_tx", "host_uart_rx", "host_uart_tx", "debug_uart_rx", "debug_uart_tx", "ptc_pwm_out", "fan_pwm_out", "fan_fg_in", "fan_install_in", "uart_rx_8n1", "uart_tx_8n1", "bmc_uart_raw48_poll_lite", "SET PTC 01..24", "SET FAN 01..04", "J6TEMP", ]: if required not in text: fail(errors, f"src/top/uart_rs232_j6_host_sensor_real_fan_ptc_debug921_top.v missing {required}") contract = ROOT / "src/top/system_contract_top.v" if contract.is_file(): text = read_text(contract) for required in ["debug_uart_tx", "debug_probe_bus", "debug_probe_bus u_debug_probe_bus"]: if required not in text: fail(errors, f"src/top/system_contract_top.v missing {required}") def check_harness(errors: list[str]) -> None: text = read_text(ROOT / "tools/pid_harness.py") for required in [ "uart-preflight", "uart-protocol-offline", "uart-validate", "final-gate-v2", "offline-fidelity-gate-v1", "offline_fidelity_gate_v1_payload", "clean-runtime", "toolchain-clean", "validate_uart_receipt", "validate_offline_fidelity_gate_v1_receipt", "uart_rs232_j6_host_sensor_real_fan_ptc_debug921_top", ]: if required not in text: fail(errors, f"pid_harness.py missing required marker: {required}") for forbidden in ["chipwatcher-gui-prepare", "chipwatcher-gui-smoke", "chipwatcher-run", "cmd_chipwatcher_gui"]: if forbidden in text: fail(errors, f"pid_harness.py must not expose legacy GUI command: {forbidden}") receipt_text = read_text(ROOT / "tools/uart_receipts.py") for required in [ "PROGRAM_SRAM", "fail_forbidden_action", "validate_uart_receipt", "validate_final_gate_v2_receipt", "validate_offline_fidelity_gate_v1_receipt", "OFFLINE_FIDELITY_REQUIRED_LAYERS", "OFFLINE_FIDELITY_FORBIDDEN_LAYERS", "powershell_dotnet_limited", ]: if required not in receipt_text: fail(errors, f"uart_receipts.py missing required marker: {required}") def check_docs(errors: list[str]) -> None: docs = { "README.zh-CN.md": ["src/rtl", "src/top", "tests/sim", "uart-validate", "final-gate-v2"], "AGENTS.md": ["workspace", "final-gate-v2", "flash"], "docs/verification-plan.zh-CN.md": ["uart-validate", "final-gate-v2", "blocked_", "fail_"], "docs/j6-host-sensor-real-fan-ptc-debug921-runbook.zh-CN.md": [ "blocked_pin_conflict_with_locked_debug_uart", "COM8", "COM7", "GET SENSOR", ], "docs/verification-gates-v2.zh-CN.md": ["strict", "uart-board-sram-receipt-v1", "final-gate-v2"], "docs/offline-fidelity-gate-v1.zh-CN.md": [ "offline-fidelity-gate-v1", "protocol_fuzz", "cocotb_virtual_lab", "virtual_driver_lab", "toolchain-clean", "thermal_digital_twin", "hardware", "uart_strict", "chipwatcher", "thermal_performance_pass", "does not certify real hardware or thermal performance", "uart_rs232_j6_host_sensor_real_fan_ptc_debug921_top", ], "docs/uart-rs232-validation-strategy.zh-CN.md": ["debug UART", "register dump", "software safety", "thermal performance"], } for item, phrases in docs.items(): path = ROOT / item if not path.is_file(): continue text = read_text(path) for phrase in phrases: if phrase not in text: fail(errors, f"{item} missing documentation phrase: {phrase}") def main() -> int: errors: list[str] = [] check_required_files(errors) check_config(errors) check_active_text_boundaries(errors) check_constraints(errors) check_td_sources(errors) check_uart_profile(errors) check_harness(errors) check_docs(errors) if errors: for error in errors: print(f"FAIL: {error}", file=sys.stderr) return 1 print("PASS: top-level CPLD PID static checks") return 0 if __name__ == "__main__": raise SystemExit(main())