"""HTML views for the workflow policy and guardrail inventory (#646).""" from __future__ import annotations import html import json from webui.policy_inventory import PolicyEntry, PolicyInventorySnapshot def _source_pointer(source) -> str: path = source.path if source.anchor: path = f"{path}#{source.anchor}" return ( f"
  • {html.escape(source.label)} — " f"{html.escape(path)} " f"({html.escape(source.kind)})
  • " ) def _active_block(entry: PolicyEntry) -> str: if entry.error: return ( "

    Active value unavailable: " f"{html.escape(entry.error)}

    " ) if not entry.active: return "

    No live projection for this guardrail.

    " pretty = json.dumps(entry.active, indent=2, sort_keys=True, default=str) return f"
    {html.escape(pretty)}
    " def _diff_block(entry: PolicyEntry) -> str: if entry.diff is None: if entry.documented_default is None: return "

    Diff vs documented default: not feasible (no declared default).

    " return "

    Diff vs documented default: unavailable.

    " status = entry.diff.get("status", "unknown") badge = "badge-claimed" if status == "matches_documented_default" else "badge-blocked" rows = [] for key, cell in (entry.diff.get("checked") or {}).items(): marker = "✓" if cell.get("matches") else "✗" rows.append( "" f"{html.escape(str(key))}" f"{html.escape(str(cell.get('expected')))}" f"{html.escape(str(cell.get('observed')))}" f"{marker}" "" ) table = "" if rows: table = ( "" "" "" f"{''.join(rows)}
    KeyDocumentedActiveMatch
    " ) return ( f"

    Diff vs documented default: " f"{html.escape(status)}

    " f"{table}" ) def _entry_card(entry: PolicyEntry) -> str: sources = "".join(_source_pointer(s) for s in entry.sources) return ( "
    " f"

    {html.escape(entry.title)} " f"{html.escape(entry.category)}

    " f"

    {html.escape(entry.summary)}

    " "

    Source pointers

    " f"" "

    Active configuration

    " f"{_active_block(entry)}" f"{_diff_block(entry)}" "
    " ) def render_policy_page(snapshot: PolicyInventorySnapshot) -> str: categories = ", ".join(html.escape(c) for c in snapshot.categories) or "none" cards = "".join(_entry_card(e) for e in snapshot.entries) build_errors = "" if snapshot.build_errors: items = "".join( f"
  • {html.escape(err)}
  • " for err in snapshot.build_errors ) build_errors = ( "

    Some guardrails could not be built:" f"

    " ) return ( "

    Workflow policy & guardrails

    " f"

    {html.escape(snapshot.note)}

    " f"

    Schema v{snapshot.schema_version} · " f"{len(snapshot.entries)} guardrails · categories: {categories}

    " f"{build_errors}" f"{cards}" "

    This page is read-only. It reports enforced policy " "and never edits or weakens a gate. Secret values are redacted.

    " )