Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 63 additions & 37 deletions src/evaltrust/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,16 @@
from .audit.two_sample import audit_two_sample
from .core.ingest import load_comparison, load_run_level, load_suite
from .diff import compare
from .report.html import render_html
from .report.html import render_html, render_html_from_parts
from .report.terminal import (
print_diff,
print_report,
print_suite,
render_diff_plain,
render_markdown,
render_markdown_from_parts,
render_plain,
render_plain_from_parts,
render_suite_markdown,
render_suite_plain,
)
Expand Down Expand Up @@ -215,53 +217,77 @@ def audit(
"findings": [f.to_dict() for f in findings],
}

# Build the subtitle string shared by all three text formats.
rl_subtitle = (
f"{rl_data.model_a} vs {rl_data.model_b} · "
f"{rl_data.n_a} runs / {rl_data.n_b} runs"
)

if as_json:
typer.echo(json.dumps(report_dict, indent=2))
elif plain or md:
# --plain and --md emit the same plain-text summary (no colour, ASCII icons only).
_warn.print(
"[yellow]--md/--plain are not yet fully supported for --run-level "
"audits; printing the standard plain summary instead.[/yellow]"
elif md:
# Delegate to the shared renderer (no verdict → verdict args omitted).
typer.echo(
render_markdown_from_parts(rl_subtitle, findings, explain=explain),
nl=False,
)
icon_map = {"pass": "[PASS]", "warn": "[WARN]", "fail": "[FAIL]", "skip": "[SKIP]"}
elif plain:
# Delegate to the shared renderer (no verdict → verdict args omitted).
typer.echo(
f"\nRun-level two-sample audit: "
f"{rl_data.model_a} vs {rl_data.model_b} "
f"({rl_data.n_a} runs / {rl_data.n_b} runs)\n"
render_plain_from_parts(rl_subtitle, findings, explain=explain),
nl=False,
)
for f in findings:
icon = icon_map.get(f.status.value, "[?]")
typer.echo(f" {icon} {f.title}")
if explain:
typer.echo(f" {f.how_detected}")
typer.echo("")
else:
# Model names and finding titles may contain square brackets
# (e.g. "gpt-4[preview]") which Rich interprets as markup tags.
# Escape all interpolated values before passing to _con.print.
from rich.console import Console as _Console
# Rich colour terminal — model names may contain square brackets that
# Rich interprets as markup, so escape before passing to print.
from rich.markup import escape as _escape
_con = _Console()
from rich.text import Text as _Text
from .report.terminal import (
_display_title, _display_how_detected,
_grouped, _SYMBOL, _bullets,
)
_con = Console()
_con.print(
f"\n[bold]Run-level two-sample audit:[/bold] "
f"[cyan]{_escape(rl_data.model_a)}[/cyan] vs "
f"[cyan]{_escape(rl_data.model_b)}[/cyan] "
f"({rl_data.n_a} runs / {rl_data.n_b} runs)\n"
f"\n[bold]EvalTrust[/bold] "
f"[dim]{_escape(rl_subtitle)}[/dim]\n"
)
for f in findings:
icon = {"pass": "✓", "warn": "⚠", "fail": "✗", "skip": "–"}.get(
f.status.value, "?"
)
colour = {"pass": "green", "warn": "yellow", "fail": "red",
"skip": "dim"}.get(f.status.value, "white")
_con.print(f" [{colour}]{icon}[/{colour}] {_escape(f.title)}")
if explain:
_con.print(f" [dim]{_escape(f.how_detected)}[/dim]")
_con.print()
for pillar, items in _grouped(findings).items():
_con.print(f"[bold]{_escape(pillar)}[/bold]")
for f in items:
sym, color = _SYMBOL[f.status]
_title = _escape(_display_title(f))
_con.print(
f" [{color}]{sym}[/{color}] "
+ (_title if f.status is not Status.SKIP
else f"[dim]{_title}[/dim]")
)
if explain:
_con.print(
f" [dim]{_escape(_display_how_detected(f))}[/dim]"
)
_t = _Text()
_bullets(_t, "What to do",
[f.how_to_fix for f in findings
if f.status in (Status.WARN, Status.FAIL)])
_bullets(_t, "To check more",
[f.how_to_fix for f in findings if f.status is Status.SKIP],
style="dim")
if _t:
_con.print(_t)
else:
_con.print()

if html_out is not None:
_warn.print(
"[yellow]--html is not yet supported for --run-level audits.[/yellow]"
Path(html_out).write_text(
render_html_from_parts(
rl_subtitle,
findings,
title_suffix=(
f"Run-level: {rl_data.model_a} vs {rl_data.model_b}"
),
explain=explain,
),
encoding="utf-8",
)

# Exit code: fail if --strict or --fail-under and worst finding is bad.
Expand Down
73 changes: 54 additions & 19 deletions src/evaltrust/report/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from ..audit.runner import AuditReport
from ..audit.verdict import VerdictLevel
from ..core.schema import Status
from ..core.schema import Finding, Status
from .terminal import (
_detail_findings,
_display_how_detected,
Expand Down Expand Up @@ -67,10 +67,25 @@ def _e(s: object) -> str:
return _html.escape(str(s))


def render_html(report: AuditReport, explain: bool = False) -> str:
"""Return a self-contained HTML page for *report*."""
v = report.verdict
vc = _VERDICT_COLOR[v.level]
def render_html_from_parts(
subtitle: str,
findings: "list[Finding]",
*,
title_suffix: str | None = None,
verdict_color: str | None = None,
verdict_label: str | None = None,
verdict_summary: str | None = None,
preamble_html: str | None = None,
explain: bool = False,
) -> str:
"""Return a self-contained HTML page built from a subtitle string and findings.

This is the shared implementation used by both the standard AuditReport path
(via ``render_html``) and the run-level path (which has no verdict). When
``verdict_color`` / ``verdict_label`` / ``verdict_summary`` are omitted, the
verdict block is skipped, keeping run-level output in sync with the main path.
"""
display_title = f"EvalTrust \u2014 {_e(title_suffix or subtitle)}"

parts: list[str] = []
p = parts.append
Expand All @@ -79,23 +94,23 @@ def render_html(report: AuditReport, explain: bool = False) -> str:
p("<html lang='en'><head>")
p("<meta charset='utf-8'>")
p("<meta name='viewport' content='width=device-width, initial-scale=1'>")
p(f"<title>EvalTrust \u2014 {_e(_subtitle(report))}</title>")
p(f"<title>{display_title}</title>")
p(f"<style>{_CSS}</style>")
p("</head><body>")

p("<h1>EvalTrust</h1>")
p(f"<div class='subtitle'>{_e(_subtitle(report))}</div>")
p(f"<div class='subtitle'>{_e(subtitle)}</div>")

others = _others(report)
if others:
p(f"<div class='subtitle'>comparing the two strongest of "
f"{len(report.models_available)}; "
f"others: {_e(', '.join(others))}</div>")
if preamble_html:
p(preamble_html)

p(f"<div class='verdict' style='color:{vc}'>\u25cf {_e(v.level.value)}</div>")
p(f"<div class='summary'>{_e(v.summary)}</div>")
if verdict_label is not None:
vc = verdict_color or "#6b7280"
p(f"<div class='verdict' style='color:{vc}'>\u25cf {_e(verdict_label)}</div>")
if verdict_summary is not None:
p(f"<div class='summary'>{_e(verdict_summary)}</div>")

for pillar, items in _grouped(report.findings).items():
for pillar, items in _grouped(findings).items():
p(f"<div class='pillar'>{_e(pillar)}</div>")
for f in items:
fc = _STATUS_COLOR[f.status]
Expand All @@ -105,23 +120,22 @@ def render_html(report: AuditReport, explain: bool = False) -> str:
p(f" {_e(_display_title(f))}")
p("</div>")

todo = [f.how_to_fix for f in report.findings
if f.status in (Status.WARN, Status.FAIL)]
todo = [f.how_to_fix for f in findings if f.status in (Status.WARN, Status.FAIL)]
if todo:
p("<div class='todo'><h2>What to do</h2><ul>")
for item in todo:
p(f" <li>{_e(item)}</li>")
p("</ul></div>")

optional = [f.how_to_fix for f in report.findings if f.status is Status.SKIP]
optional = [f.how_to_fix for f in findings if f.status is Status.SKIP]
if optional:
p("<div class='todo'><h2>To check more</h2><ul>")
for item in optional:
p(f" <li>{_e(item)}</li>")
p("</ul></div>")

if explain:
flagged = _detail_findings(report.findings)
flagged = _detail_findings(findings)
if flagged:
p("<div class='detail'><h2>Detail</h2>")
for f in flagged:
Expand All @@ -137,3 +151,24 @@ def render_html(report: AuditReport, explain: bool = False) -> str:

p("</body></html>")
return "\n".join(parts)


def render_html(report: AuditReport, explain: bool = False) -> str:
"""Return a self-contained HTML page for *report*."""
v = report.verdict
others = _others(report)
preamble = (
f"<div class='subtitle'>comparing the two strongest of "
f"{len(report.models_available)}; "
f"others: {_e(', '.join(others))}</div>"
if others else None
)
return render_html_from_parts(
_subtitle(report),
report.findings,
verdict_color=_VERDICT_COLOR[v.level],
verdict_label=v.level.value,
verdict_summary=v.summary,
preamble_html=preamble,
explain=explain,
)
Loading