#!/usr/bin/env python3 """Generate deterministic FE-M2 controls/state-panel evidence.""" from __future__ import annotations import argparse import hashlib import json import subprocess from pathlib import Path from typing import Any CONTEXT = Path("context-lock.json") FE1 = Path("coverage/frontend-m1.report.json") DEVICES = Path("coverage/virtual-device-fault-matrix.report.json") GATE = Path("coverage/release-candidate-gate.report.json") OUTPUT = Path("coverage/frontend-m2.report.json") FILES = (Path("web/app/controls.mjs"), Path("web/app/state-panel.mjs"), Path("web/app/app.mjs"), Path("web/app/styles.css"), Path("web/app/test_frontend_m2.mjs")) def canonical(value: Any) -> str: return json.dumps(value, ensure_ascii=False, sort_keys=True, separators=(",", ":")) def digest(value: Any) -> str: return hashlib.sha256(canonical(value).encode()).hexdigest() def artifact(root: Path, path: Path, role: str) -> dict[str, Any]: full = root / path if not full.is_file(): return {"path": path.as_posix(), "role": role, "status": "missing"} return {"path": path.as_posix(), "role": role, "status": "present", "bytes": full.stat().st_size, "sha256": hashlib.sha256(full.read_bytes()).hexdigest()} def run(root: Path) -> dict[str, Any]: result = subprocess.run(["node", "web/app/test_frontend_m2.mjs"], cwd=root, check=False, capture_output=True, text=True) return {"status": "passed" if result.returncode == 0 else "blocked", "exit_code": result.returncode, "stdout": result.stdout.strip(), "stdout_sha256": digest(result.stdout), "stderr_sha256": digest(result.stderr)} def generate(root: Path, output: Path = OUTPUT) -> dict[str, Any]: root = root.resolve() lock = json.loads((root / CONTEXT).read_text()) fe1 = json.loads((root / FE1).read_text()) devices = json.loads((root / DEVICES).read_text()) gate = json.loads((root / GATE).read_text()) outputs = [artifact(root, path, "frontend_source") for path in FILES] app = (root / "web/app/app.mjs").read_text() if (root / "web/app/app.mjs").is_file() else "" controls = (root / "web/app/controls.mjs").read_text() if (root / "web/app/controls.mjs").is_file() else "" panel = (root / "web/app/state-panel.mjs").read_text() if (root / "web/app/state-panel.mjs").is_file() else "" smoke = run(root) actions = ("run", "pause", "resume", "abort", "home", "estop", "recover") checks = { "fe1_dependency": {"status": "verified" if fe1.get("implementation_status") == "verified" else "blocked"}, "operation_controls": {"status": "verified" if all(action in controls for action in actions) else "blocked", "actions": list(actions)}, "authoritative_state_panel": {"status": "verified" if "reduceWorkerState" in panel and "renderStatePanel" in panel and "onMessage" in app else "blocked"}, "device_error_visibility": {"status": "verified" if devices.get("implementation_status") == "verified" and "Error:" in app else "blocked"}, "source_files": {"status": "verified" if all(row["status"] == "present" for row in outputs) else "blocked"}, "browser_smoke": {"status": "verified" if smoke["status"] == "passed" else "blocked", "stdout": smoke["stdout"]}, "release_stop_preserved": {"status": "verified" if gate.get("release_action") == "STOP_RELEASE" and gate.get("candidate_eligible") is False else "blocked", "release_action": gate.get("release_action")}, } checks_ok = all(row["status"] == "verified" for row in checks.values()) report: dict[str, Any] = {"schema": "frontend-m2.v1", "context_id": lock.get("context_id"), "task_id": "FE-M2", "status": "partial" if checks_ok else "blocked", "implementation_status": "verified" if checks_ok else "blocked", "candidate_eligible": False, "release_action": "STOP_RELEASE", "inputs": [artifact(root, CONTEXT, "context_lock"), artifact(root, FE1, "frontend_m1"), artifact(root, DEVICES, "virtual_device_fault_matrix"), artifact(root, GATE, "release_candidate_gate")], "outputs": outputs, "checks": checks, "smoke": smoke, "next_transition": "execute FE-M3 project and recovery workflow", "report_sha256": None} report["report_sha256"] = digest({key: value for key, value in report.items() if key != "report_sha256"}) destination = output if output.is_absolute() else root / output destination.parent.mkdir(parents=True, exist_ok=True) destination.write_text(json.dumps(report, ensure_ascii=False, sort_keys=True, indent=2) + "\n") return report def main() -> int: parser = argparse.ArgumentParser() parser.add_argument("--repo-root", type=Path, default=Path(__file__).resolve().parents[1]) parser.add_argument("--output", type=Path, default=OUTPUT) args = parser.parse_args() report = generate(args.repo_root.resolve(), args.output) print(f"frontend_m2={report['status']} smoke={report['checks']['browser_smoke']['status']}") return 0 if __name__ == "__main__": raise SystemExit(main())