"""The model writes the Assessment report; code supplies its facts and renders its pages (F02 ADR 0015). `pre_process` replaces the step outputs with the facts worked out in code (`app/facts.py`), so the model writes from the same register, gaps and scorecard the package holds. `post_process` sets the verdict and ids from those facts, never from the model, and renders the HTML in code: `report_html` (marked as a draft, shown at the `disposition_signoff` review point) and `final_html` (for the page step after approval). The two hooks run in the same request, so the facts pass between them in a ContextVar. """ from __future__ import annotations import contextvars from typing import Any, Optional from app import facts, markdown DRAFT = "Draft for sign-off. Not for release until the Engagement Lead continues the run at disposition_signoff." _facts: contextvars.ContextVar[Optional[dict]] = contextvars.ContextVar("report_facts", default=None) def pre_process(payload: dict) -> Optional[Any]: found = facts.build(payload) _facts.set(found) return {"engagement_id": payload.get("engagement_id"), "run_id": payload.get("run_id"), "discovery_execution_id": payload.get("discovery_execution_id"), "facts": found} def post_process(result: dict) -> Optional[dict]: found = _facts.get() _facts.set(None) if found is None: return None text = result.get("report_markdown") or "" title = f"Assessment report: {found.get('engagement_id') or ''} {found.get('run_id') or ''}".strip() return {**result, "engagement_id": found.get("engagement_id"), "run_id": found.get("run_id"), "verdict": found.get("verdict") or "not supplied", "signoff_queue": found["critic"]["signoff_queue"], "report_html": markdown.page(text, title, DRAFT), "final_html": markdown.page(text, title)}