Files
Jonathan Boniface 66c32745aa
Some checks failed
check agents / check (push) Failing after 14s
F02: Assessment report and package (ADR 0014, 0015); register agents in KAB
- Report (cp1-assessment-report-agent 0.2.0, step s7): a model writes the
  Assessment report from facts worked out in code (register, gaps, scorecard,
  rule traces, checks, and what each application is from the Discovery
  package); code sets the verdict and renders the pages. The
  disposition_signoff review point moves from the critic to the report.
- Package (cp1-assessment-package-agent 0.1.1, step s8): after sign-off, every
  step's output in one package, with a register, a gaps register, the
  sign-off record, lineage and a sha256 per part, in code. Page step s9.
- scripts/kab_register_agents.py: registers deployed agents in KAB's agent
  catalog (an in-place workflow update doesn't).
- Runs exec_1b1257380df7 (014) and exec_374d5d577077 (015) completed on
  crucible-kab-dev: critic pass, report figures all in its facts, package
  complete with every digest recomputing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 20:19:10 +00:00

98 lines
5.0 KiB
Python

"""The report's HTML, rendered in code from the model's Markdown (F02 ADR 0015). Standard library only.
Only the Markdown the prompt asks for is understood: headings, paragraphs, bullet and numbered lists, tables,
blockquotes, **bold**, *italic* and `code`. Every character of the model's text is HTML-escaped before any markup is
added, so nothing in the report (or in the estate data it quotes) can inject HTML or script into the page.
"""
from __future__ import annotations
import html
import re
from typing import List
STYLE = """
:root { --fg: #1f2328; --muted: #59636e; --line: #d1d9e0; --head: #f6f8fa; --bg: #ffffff; --warn: #9a6700; --warnbg: #fff8c5; }
@media (prefers-color-scheme: dark) { :root { --fg: #e6edf3; --muted: #9198a1; --line: #3d444d; --head: #151b23; --bg: #0d1117; --warn: #d29922; --warnbg: #2e2a1f; } }
body { background: var(--bg); color: var(--fg); font: 15px/1.55 -apple-system, "Segoe UI", Roboto, sans-serif; margin: 0; }
main { max-width: 960px; margin: 0 auto; padding: 24px 16px 48px; }
h1 { font-size: 1.7em; margin: 0 0 .4em; } h2 { font-size: 1.3em; margin: 1.6em 0 .5em; border-bottom: 1px solid var(--line); padding-bottom: .2em; }
h3 { font-size: 1.1em; margin: 1.2em 0 .4em; } p, ul, ol, blockquote { margin: 0 0 .9em; }
.table { overflow-x: auto; margin: 0 0 1em; } table { border-collapse: collapse; min-width: 100%; font-size: .93em; }
th, td { border: 1px solid var(--line); padding: 5px 9px; text-align: left; vertical-align: top; } th { background: var(--head); }
code { background: var(--head); padding: 1px 4px; border-radius: 4px; font-size: .92em; }
blockquote { border-left: 3px solid var(--line); padding: 0 12px; color: var(--muted); }
.draft { background: var(--warnbg); color: var(--warn); border: 1px solid var(--warn); border-radius: 6px; padding: 8px 12px; margin-bottom: 18px; font-weight: 600; }
"""
def _inline(text: str) -> str:
out = html.escape(text, quote=True)
out = re.sub(r"`([^`]+)`", r"<code>\1</code>", out)
out = re.sub(r"\*\*([^*]+)\*\*", r"<strong>\1</strong>", out)
out = re.sub(r"(?<![\w*])\*([^*\s][^*]*)\*(?![\w*])", r"<em>\1</em>", out)
return out
def _cells(line: str) -> List[str]:
return [c.strip() for c in line.strip().strip("|").split("|")]
def _is_rule(line: str) -> bool:
return bool(re.fullmatch(r"\|?\s*:?-{3,}:?\s*(\|\s*:?-{3,}:?\s*)*\|?", line.strip()))
def to_html(markdown: str) -> str:
lines = (markdown or "").replace("\r\n", "\n").split("\n")
out: List[str] = []
i = 0
while i < len(lines):
line = lines[i]
if not line.strip():
i += 1
continue
heading = re.match(r"(#{1,4})\s+(.*)", line)
if heading:
level = len(heading.group(1))
out.append(f"<h{level}>{_inline(heading.group(2).strip())}</h{level}>")
i += 1
elif line.lstrip().startswith("|") and i + 1 < len(lines) and _is_rule(lines[i + 1]):
head = "".join(f"<th>{_inline(c)}</th>" for c in _cells(line))
i += 2
rows = []
while i < len(lines) and lines[i].lstrip().startswith("|"):
rows.append("<tr>" + "".join(f"<td>{_inline(c)}</td>" for c in _cells(lines[i])) + "</tr>")
i += 1
out.append(f'<div class="table"><table><thead><tr>{head}</tr></thead><tbody>{"".join(rows)}</tbody></table></div>')
elif re.match(r"\s*[-*]\s+", line) or re.match(r"\s*\d+[.)]\s+", line):
ordered = bool(re.match(r"\s*\d+[.)]\s+", line))
pattern = r"\s*\d+[.)]\s+(.*)" if ordered else r"\s*[-*]\s+(.*)"
items = []
while i < len(lines) and re.match(pattern, lines[i]):
items.append(f"<li>{_inline(re.match(pattern, lines[i]).group(1))}</li>")
i += 1
tag = "ol" if ordered else "ul"
out.append(f"<{tag}>{''.join(items)}</{tag}>")
elif line.lstrip().startswith(">"):
quote = []
while i < len(lines) and lines[i].lstrip().startswith(">"):
quote.append(lines[i].lstrip()[1:].strip())
i += 1
out.append(f"<blockquote><p>{_inline(' '.join(quote))}</p></blockquote>")
else:
para = []
while i < len(lines) and lines[i].strip() and not re.match(r"(#{1,4})\s|\s*[-*]\s|\s*\d+[.)]\s|\s*[|>]", lines[i]):
para.append(lines[i].strip())
i += 1
if not para: # a line no rule above took: keep it as text rather than loop on it
para, i = [line.strip()], i + 1
out.append(f"<p>{_inline(' '.join(para))}</p>")
return "\n".join(out)
def page(markdown: str, title: str, banner: str = "") -> str:
note = f'<div class="draft">{html.escape(banner)}</div>' if banner else ""
return ("<!doctype html><html lang=\"en\"><head><meta charset=\"utf-8\">"
"<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">"
f"<title>{html.escape(title)}</title><style>{STYLE}</style></head>"
f"<body><main>{note}{to_html(markdown)}</main></body></html>")