| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382 |
- #!/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"(?<![A-Za-z0-9_])workspace[\\/]"),
- ]
- GENERATED_DIRS = ["workspace", "dist", "tmp", "_archive", "td/build"]
- GENERATED_GLOBS = ["td/project/*_Runs"]
- TD_GUI_RUNTIME_DIRS = ["td/project/log"]
- GENERATED_SUFFIXES = {".area", ".bid", ".bit", ".data", ".db", ".dmp", ".f", ".log", ".logw", ".pyc", ".timing", ".tsm"}
- REQUIRED_FILES = [
- "README.zh-CN.md",
- "AGENTS.md",
- ".gitignore",
- "config/pid_channels.json",
- "constraints/pid_study_01.adc",
- "constraints/pid_study_01.sdc",
- "docs/fact-source-boundary.zh-CN.md",
- "docs/fake-stress-verification-v2.zh-CN.md",
- "docs/high-temp-chamber-design-v2.zh-CN.md",
- "docs/bmc-uart-temperature-protocol-v2.zh-CN.md",
- "docs/interface-contract.zh-CN.md",
- "docs/pid-safety-contract-v2.zh-CN.md",
- "docs/j6-host-sensor-real-fan-ptc-debug921-runbook.zh-CN.md",
- "docs/verification-plan.zh-CN.md",
- "docs/verification-gates-v2.zh-CN.md",
- "docs/offline-fidelity-gate-v1.zh-CN.md",
- "docs/uart-rs232-validation-strategy.zh-CN.md",
- "src/rtl/pid_core.v",
- "src/rtl/uart_rx_8n1.v",
- "src/rtl/uart_tx_8n1.v",
- "src/rtl/bmc_uart_phy_bridge.v",
- "src/rtl/debug_probe_bus.v",
- "src/rtl/bmc_uart_ascii_poll.v",
- "src/rtl/safety_supervisor.v",
- "src/top/system_contract_top.v",
- "src/top/uart_rs232_j6_host_sensor_real_fan_ptc_debug921_top.v",
- "tests/sim/pid_core_tb.v",
- "tests/sim/uart_8n1_phy_tb.v",
- "tests/sim/uart_rx_error_tb.v",
- "tests/sim/bmc_uart_phy_bridge_tb.v",
- "tests/formal/fault_implies_ptc_off.sby",
- "tests/cocotb/run_pid_core_smoke.py",
- "tests/fixtures/bad_icarus_tb.v",
- "tools/uart_receipts.py",
- "tools/pin_inventory.py",
- "tools/pid_harness.py",
- "tools/toolchain_probe.py",
- "td/uart_rs232_j6_host_sensor_real_fan_ptc_debug921_primary.prj",
- "schemas/uart_preflight_receipt.schema.json",
- "schemas/bmc_uart_protocol_receipt.schema.json",
- "schemas/uart_loopback_receipt.schema.json",
- "schemas/uart_board_sram_receipt.schema.json",
- "schemas/uart_soak_receipt.schema.json",
- "schemas/final_gate_v2_receipt.schema.json",
- "schemas/offline_fidelity_gate_v1_receipt.schema.json",
- "config/pin_inventory.mini.json",
- "config/pin_inventory.product.placeholder.json",
- "reference/original/MANIFEST.zh-CN.md",
- ]
- FORBIDDEN_ACTIVE_PATHS = [
- "tools/restructure_pid_study.py",
- "tools/chipwatcher_" + "runner.py",
- "tools/chipwatcher_python_ui_" + "template.py",
- "tools/chipwatcher_preflight.py",
- "tools/chipwatcher_receipts.py",
- "tools/computer_use/chipwatcher_fake_run.mjs",
- "src/rtl/debug_chipwatcher_bus.v",
- "src/top/chipwatcher_fake_stress_top.v",
- "td/project/chipwatcher_fake_stress.al",
- "td/syn/chipwatcher_fake_stress.prj",
- "td/phy/chipwatcher_fake_stress.prj",
- "td/phy/chipwatcher_fake_stress.cwc",
- "schemas/chipwatcher_gui_layer_receipt.schema.json",
- "schemas/chipwatcher_computer_use_receipt.schema.json",
- "schemas/chipwatcher_soak_receipt.schema.json",
- "td/pid_study_01.al",
- "td/syn/run_led_01.prj",
- "td/phy/run_led_01.prj",
- "src/top/run_led_01.v",
- ]
- def rel(path: Path) -> 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())
|