"""HTML views for the operator request surface (#643). The form is deliberately a *preview* form. It has no initiate button, because initiating requires a confirmed POST to ``/api/v1/requests/apply`` and a stray form submission must not be able to produce one by accident. Nothing rendered here is trusted input: every interpolated value is escaped, and the page renders only values the service already produced rather than echoing a raw request body back. """ from __future__ import annotations import html import json from typing import Any from webui.layout import render_page from webui.request_service import ( REQUESTABLE_ROLES, WORK_KINDS, RequestError, RequestPreview, ) REQUESTS_PATH = "/requests" PREVIEW_API_PATH = "/api/v1/requests/preview" APPLY_API_PATH = "/api/v1/requests/apply" def _escape(text: Any) -> str: return html.escape(str(text if text is not None else ""), quote=True) REQUEST_PAGE_STYLES = """ """ def _options(values: tuple[str, ...], selected: Any) -> str: return "".join( f"" for value in values ) def _form(values: dict[str, Any] | None = None) -> str: current = dict(values or {}) number = current.get("work_number") return ( f"
" "" "" "" "" "" "" "

Preview is read-only and creates no assignment. " f"Initiating requires a confirmed POST to {APPLY_API_PATH}." "

" "
" ) def _checks_block(preview: RequestPreview) -> str: rows = [] for check in preview.checks: verdict = "PASS" if check.ok else "FAIL" css = "verdict-ok" if check.ok else "verdict-fail" rows.append( "
  • " f"{verdict} " f"{_escape(check.name)} — {_escape(check.detail)} " f"({_escape(check.reason_code)})" "
  • " ) return "" def _preview_block(preview: RequestPreview) -> str: verdict = "AUTHORIZED" if preview.authorized else "DENIED" prohibited = "".join( f"{_escape(action)}" for action in preview.prohibited_actions ) request = preview.request evidence = json.dumps(preview.allocator_evidence, indent=2, default=str) return ( "

    Intent preview

    " f"

    {verdict} — {_escape(preview.detail)}

    " "

    " f"Role {_escape(request.desired_role)} · " f"{_escape(request.work_kind)} {_escape(request.display_ref)}" f" · profile {_escape(preview.required_profile)} · " f"namespace {_escape(preview.required_namespace)} · " f"permission {_escape(preview.required_permission)}" "

    " f"

    Intent: {_escape(request.intent_summary)}

    " f"{_checks_block(preview)}" f"

    Next safe action: " f"{_escape(preview.next_safe_action)}

    " "

    Prohibited for this role: " + (prohibited or "none declared") + "

    " "

    Correlation id " f"{_escape(preview.correlation_id)}

    " "
    Allocator evidence" f"
    {_escape(evidence)}
    " "
    " ) def _error_block(error: RequestError) -> str: field = ( f"

    Field: {_escape(error.field_name)}

    " if error.field_name else "" ) return ( "

    Request rejected

    " f"

    {_escape(error.reason_code)} — " f"{_escape(error.detail)}

    {field}" ) def render_requests_page( *, preview: RequestPreview | None = None, error: RequestError | None = None, submitted: dict[str, Any] | None = None, ) -> str: """Render the request form, plus a preview or rejection when one exists.""" body = ( "

    Requests

    " "

    Submit a work request — desired role, issue or PR, and intent — " "and see whether it would be authorized before anything is reserved. " "Initiation goes through the allocator (#600/#613); this console never " "self-selects work, never approves, and never merges.

    " + _form(submitted) + (_error_block(error) if error is not None else "") + (_preview_block(preview) if preview is not None else "") + f"

    Preview API · " "RBAC model

    " + REQUEST_PAGE_STYLES ) return render_page(title="Requests", body_html=body)