render-validation-html.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. #!/usr/bin/env python3
  2. # /// script
  3. # requires-python = ">=3.10"
  4. # ///
  5. """Render a PRD validation findings JSON into HTML + markdown reports.
  6. Reads structured findings produced by the validator subagent, groups them by
  7. category (explicit `category` field, else derived from ID prefix), computes a
  8. pass/warn/fail summary and grade, substitutes into the configured HTML
  9. template, writes a markdown companion at the same path with `.md` extension,
  10. and optionally opens the HTML in the default browser.
  11. """
  12. import argparse
  13. import html
  14. import json
  15. import string
  16. import sys
  17. import webbrowser
  18. from datetime import datetime
  19. from pathlib import Path
  20. CATEGORY_FROM_PREFIX = {
  21. "Q": "Quality",
  22. "D": "Discipline",
  23. "S": "Structural integrity",
  24. "STK": "Stakes-gated",
  25. "M": "Mechanical",
  26. }
  27. CATEGORY_ORDER = ["Quality", "Discipline", "Structural integrity", "Stakes-gated", "Mechanical"]
  28. def category_for(finding: dict) -> str:
  29. explicit = finding.get("category")
  30. if explicit:
  31. return explicit
  32. fid = finding.get("id", "")
  33. prefix = fid.split("-", 1)[0] if "-" in fid else fid
  34. return CATEGORY_FROM_PREFIX.get(prefix, prefix or "Other")
  35. def compute_stats(findings: list[dict]) -> dict:
  36. total = len(findings)
  37. by_status = {"pass": 0, "warn": 0, "fail": 0, "n/a": 0}
  38. failed_critical = 0
  39. failed_high = 0
  40. for f in findings:
  41. status = (f.get("status") or "n/a").lower()
  42. if status in by_status:
  43. by_status[status] += 1
  44. if status == "fail":
  45. sev = (f.get("severity") or "low").lower()
  46. if sev == "critical":
  47. failed_critical += 1
  48. elif sev == "high":
  49. failed_high += 1
  50. return {
  51. "total": total,
  52. "passed": by_status["pass"],
  53. "warned": by_status["warn"],
  54. "failed": by_status["fail"],
  55. "na": by_status["n/a"],
  56. "failed_critical": failed_critical,
  57. "failed_high": failed_high,
  58. }
  59. def grade_from(stats: dict) -> tuple[str, str]:
  60. if stats["failed_critical"] > 0:
  61. return "Poor", "grade-poor"
  62. if stats["failed_high"] >= 1 or stats["failed"] >= 4:
  63. return "Fair", "grade-fair"
  64. if stats["failed"] > 0 or stats["warned"] > 2:
  65. return "Good", "grade-good"
  66. return "Excellent", "grade-excellent"
  67. def render_score_bar(stats: dict, width: int = 480, height: int = 22) -> str:
  68. total = max(stats["total"], 1)
  69. p = stats["passed"] / total * width
  70. w = stats["warned"] / total * width
  71. f = stats["failed"] / total * width
  72. n = stats["na"] / total * width
  73. return (
  74. f'<svg width="{width}" height="{height}" viewBox="0 0 {width} {height}" role="img" '
  75. f'aria-label="Pass / warn / fail / n-a breakdown">'
  76. f'<rect x="0" y="0" width="{p:.1f}" height="{height}" fill="#22c55e"/>'
  77. f'<rect x="{p:.1f}" y="0" width="{w:.1f}" height="{height}" fill="#eab308"/>'
  78. f'<rect x="{p + w:.1f}" y="0" width="{f:.1f}" height="{height}" fill="#ef4444"/>'
  79. f'<rect x="{p + w + f:.1f}" y="0" width="{n:.1f}" height="{height}" fill="#94a3b8"/>'
  80. f"</svg>"
  81. )
  82. def render_finding(f: dict) -> str:
  83. status = (f.get("status") or "n/a").lower()
  84. severity = (f.get("severity") or "low").lower()
  85. fid = html.escape(f.get("id") or "")
  86. title = html.escape(f.get("title") or fid)
  87. location = html.escape(f.get("location") or "")
  88. note = html.escape(f.get("note") or "")
  89. fix = html.escape(f.get("suggested_fix") or "")
  90. status_class = "na" if status == "n/a" else status
  91. parts = [
  92. f'<article class="finding finding-{status_class}">',
  93. '<header>',
  94. f'<span class="badge badge-status badge-{status_class}">{status.upper()}</span>',
  95. f'<span class="badge badge-severity badge-sev-{severity}">{severity}</span>',
  96. f'<span class="finding-id">{fid}</span>',
  97. f'<h3 class="finding-title">{title}</h3>',
  98. '</header>',
  99. ]
  100. if location:
  101. parts.append(f'<div class="finding-location"><strong>Location:</strong> {location}</div>')
  102. if note:
  103. parts.append(f'<div class="finding-note">{note}</div>')
  104. if fix:
  105. parts.append(f'<div class="finding-fix"><strong>Suggested fix:</strong> {fix}</div>')
  106. parts.append("</article>")
  107. return "\n".join(parts)
  108. def render_category(name: str, findings: list[dict]) -> str:
  109. items = "\n".join(render_finding(f) for f in findings)
  110. name_e = html.escape(name)
  111. return (
  112. f'<section class="category">'
  113. f"<details open>"
  114. f'<summary><h2>{name_e} <span class="count">({len(findings)})</span></h2></summary>'
  115. f"{items}"
  116. f"</details>"
  117. f"</section>"
  118. )
  119. SEVERITY_ORDER = ["critical", "high", "medium", "low"]
  120. def render_finding_md(f: dict) -> str:
  121. status = (f.get("status") or "n/a").upper()
  122. severity = (f.get("severity") or "low").lower()
  123. fid = f.get("id") or ""
  124. title = f.get("title") or fid
  125. location = f.get("location") or ""
  126. note = f.get("note") or ""
  127. fix = f.get("suggested_fix") or ""
  128. lines = [f"### [{status}] {fid} — {title} _(severity: {severity})_"]
  129. if location:
  130. lines.append(f"- **Location:** {location}")
  131. if note:
  132. lines.append(f"- **Finding:** {note}")
  133. if fix:
  134. lines.append(f"- **Suggested fix:** {fix}")
  135. return "\n".join(lines)
  136. def render_markdown_report(data: dict, findings: list[dict], stats: dict, grade: str) -> str:
  137. prd_name = data.get("prd_name") or "PRD"
  138. prd_path = data.get("prd_path") or ""
  139. checklist_path = data.get("checklist_path") or ""
  140. timestamp = data.get("timestamp") or datetime.now().isoformat(timespec="seconds")
  141. synthesis = data.get("overall_synthesis") or ""
  142. out = [
  143. f"# Validation Report — {prd_name}",
  144. "",
  145. f"- **PRD:** `{prd_path}`",
  146. f"- **Checklist:** `{checklist_path}`",
  147. f"- **Run at:** {timestamp}",
  148. f"- **Grade:** {grade}",
  149. "",
  150. f"**Summary:** {stats['passed']} pass · {stats['warned']} warn · {stats['failed']} fail · {stats['na']} n/a "
  151. f"(total {stats['total']}; critical fails: {stats['failed_critical']}, high fails: {stats['failed_high']})",
  152. ]
  153. if synthesis:
  154. out += ["", "## Overall synthesis", "", synthesis]
  155. # Group by severity then status: failed criticals first, then highs, etc.
  156. by_sev: dict[str, list[dict]] = {s: [] for s in SEVERITY_ORDER}
  157. other: list[dict] = []
  158. for f in findings:
  159. sev = (f.get("severity") or "low").lower()
  160. if sev in by_sev:
  161. by_sev[sev].append(f)
  162. else:
  163. other.append(f)
  164. out += ["", "## Findings by severity"]
  165. any_findings = False
  166. for sev in SEVERITY_ORDER:
  167. items = by_sev[sev]
  168. if not items:
  169. continue
  170. any_findings = True
  171. out += ["", f"### {sev.capitalize()} ({len(items)})", ""]
  172. out += [render_finding_md(f) for f in items]
  173. if other:
  174. any_findings = True
  175. out += ["", f"### Other ({len(other)})", ""]
  176. out += [render_finding_md(f) for f in other]
  177. if not any_findings:
  178. out += ["", "_No findings._"]
  179. return "\n".join(out) + "\n"
  180. def main(argv: list[str]) -> int:
  181. parser = argparse.ArgumentParser(description="Render PRD validation findings to HTML.")
  182. parser.add_argument("--findings", required=True, help="Path to validation-findings.json")
  183. parser.add_argument("--template", required=True, help="Path to HTML template")
  184. parser.add_argument("--output", required=True, help="Path to write the rendered HTML")
  185. parser.add_argument("--open", action="store_true", help="Open the rendered HTML in the default browser")
  186. args = parser.parse_args(argv)
  187. findings_path = Path(args.findings)
  188. template_path = Path(args.template)
  189. output_path = Path(args.output)
  190. try:
  191. data = json.loads(findings_path.read_text(encoding="utf-8"))
  192. except FileNotFoundError:
  193. print(f"error: findings file not found: {findings_path}", file=sys.stderr)
  194. return 1
  195. except json.JSONDecodeError as e:
  196. print(f"error: findings file is not valid JSON ({findings_path}): {e}", file=sys.stderr)
  197. return 1
  198. try:
  199. template = template_path.read_text(encoding="utf-8")
  200. except FileNotFoundError:
  201. print(f"error: template file not found: {template_path}", file=sys.stderr)
  202. return 1
  203. findings = data.get("findings", []) or []
  204. by_cat: dict[str, list[dict]] = {}
  205. for f in findings:
  206. by_cat.setdefault(category_for(f), []).append(f)
  207. sorted_cats = sorted(
  208. by_cat.keys(),
  209. key=lambda c: (CATEGORY_ORDER.index(c) if c in CATEGORY_ORDER else 99, c),
  210. )
  211. categories_html = "\n".join(render_category(c, by_cat[c]) for c in sorted_cats)
  212. stats = compute_stats(findings)
  213. grade, grade_class = grade_from(stats)
  214. score_svg = render_score_bar(stats)
  215. timestamp = data.get("timestamp") or datetime.now().isoformat(timespec="seconds")
  216. substitutions = {
  217. "prd_name": html.escape(str(data.get("prd_name") or "PRD")),
  218. "prd_path": html.escape(str(data.get("prd_path") or "")),
  219. "checklist_path": html.escape(str(data.get("checklist_path") or "")),
  220. "timestamp": html.escape(timestamp),
  221. "overall_synthesis": html.escape(str(data.get("overall_synthesis") or "")),
  222. "grade": grade,
  223. "grade_class": grade_class,
  224. "total": str(stats["total"]),
  225. "passed": str(stats["passed"]),
  226. "failed": str(stats["failed"]),
  227. "warned": str(stats["warned"]),
  228. "na": str(stats["na"]),
  229. "score_svg": score_svg,
  230. "categories_html": categories_html,
  231. }
  232. rendered = string.Template(template).safe_substitute(substitutions)
  233. output_path.write_text(rendered, encoding="utf-8")
  234. md_path = output_path.with_suffix(".md")
  235. md_path.write_text(render_markdown_report(data, findings, stats, grade), encoding="utf-8")
  236. print(json.dumps({
  237. "output": str(output_path),
  238. "markdown": str(md_path),
  239. "grade": grade,
  240. "stats": stats,
  241. }))
  242. if args.open:
  243. webbrowser.open(output_path.resolve().as_uri())
  244. return 0
  245. if __name__ == "__main__":
  246. sys.exit(main(sys.argv[1:]))