"""HTML views for the workflow policy and guardrail inventory (#646).""" from __future__ import annotations import html import json from typing import Any from webui import console_redaction from webui.policy_inventory import ( PolicyEntry, PolicyInventorySnapshot, SourcePointer, snapshot_to_dict, ) def _source_pointer(source: dict[str, Any] | SourcePointer) -> str: if isinstance(source, dict): label = str(source.get("label") or "") path = str(source.get("path") or "") anchor = source.get("anchor") kind = str(source.get("kind") or "") else: label = source.label path = source.path anchor = source.anchor kind = source.kind if anchor: path = f"{path}#{anchor}" return ( f"
  • {html.escape(label)} — " f"{html.escape(path)} " f"({html.escape(kind)})
  • " ) def _active_block(entry: dict[str, Any] | PolicyEntry) -> str: error = entry.get("error") if isinstance(entry, dict) else entry.error active = entry.get("active") if isinstance(entry, dict) else entry.active if error: return ( "

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

    " ) if not active: return "

    No live projection for this guardrail.

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

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

    " return "

    Diff vs documented default: unavailable.

    " status = str(diff.get("status") if isinstance(diff, dict) else "unknown") badge = "badge-claimed" if status == "matches_documented_default" else "badge-blocked" rows = [] checked = diff.get("checked") if isinstance(diff, dict) else {} if isinstance(checked, dict): for key, cell in checked.items(): cell_dict = cell if isinstance(cell, dict) else {} marker = "✓" if cell_dict.get("matches") else "✗" rows.append( "" f"{html.escape(str(key))}" f"{html.escape(str(cell_dict.get('expected')))}" f"{html.escape(str(cell_dict.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: dict[str, Any] | PolicyEntry) -> str: title = str(entry.get("title") if isinstance(entry, dict) else entry.title) category = str(entry.get("category") if isinstance(entry, dict) else entry.category) summary = str(entry.get("summary") if isinstance(entry, dict) else entry.summary) sources_data = entry.get("sources", []) if isinstance(entry, dict) else entry.sources sources = "".join(_source_pointer(s) for s in sources_data) return ( "
    " f"

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

    " f"

    {html.escape(summary)}

    " "

    Source pointers

    " f"" "

    Active configuration

    " f"{_active_block(entry)}" f"{_diff_block(entry)}" "
    " ) def render_policy_page(snapshot: PolicyInventorySnapshot | dict[str, Any]) -> str: if isinstance(snapshot, PolicyInventorySnapshot): payload = snapshot_to_dict(snapshot) elif isinstance(snapshot, dict): payload = console_redaction.redact_payload(snapshot) else: payload = {} categories_list = payload.get("categories") or [] categories = ", ".join(html.escape(str(c)) for c in categories_list) or "none" entries_list = payload.get("entries") or [] cards = "".join(_entry_card(e) for e in entries_list) build_errors = "" errors_list = payload.get("build_errors") or [] if errors_list: items = "".join( f"
  • {html.escape(str(err))}
  • " for err in errors_list ) build_errors = ( "

    Some guardrails could not be built:" f"

    " ) note = str(payload.get("note") or "") schema_version = payload.get("schema_version") or 1 return ( "

    Workflow policy & guardrails

    " f"

    {html.escape(note)}

    " f"

    Schema v{schema_version} · " f"{len(entries_list)} 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.

    " )