"""The critic's mechanical checks run in code (F02 ADR 0011); the model judges the rest. `pre_process` works out K-04 to K-07 and K-11 from the producers' records, before the model runs, and shows the result to the model as `checked_in_code`. `post_process` then sets those checks, the sign-off queue and the verdict in the reply. What the model judged (K-01 to K-03, K-08 to K-10, K-12) is kept as it wrote it; where its reading of a code check differs, it's kept in `unverified_claims` for the Engagement Lead. The two hooks run in the same request, so the result passes between them in a ContextVar: one per request, never shared between requests that run at once. """ from __future__ import annotations import contextvars import logging from typing import Any, Optional from app import arithmetic, evidence logger = logging.getLogger(__name__) _found: contextvars.ContextVar[Optional[dict]] = contextvars.ContextVar("critic_checked_in_code", default=None) def pre_process(payload: dict) -> Optional[Any]: if (payload.get("preconditions") or {}).get("failed"): _found.set(None) # the planner refused: the critic returns fail on K-01 alone, and nothing is checked here return None found = arithmetic.check(payload) found["K-02"] = evidence.resolve(payload) # the lookup only (ADR 0013) _found.set(found) payload["checked_in_code"] = { "note": "K-04, K-05, K-06, K-07 and K-11 are checked in code from the producers' records (ADR 0011), and " "their results are set in your reply after you answer. Report them as you find them; the other checks " "are yours alone.", "K-04": found["K-04"] or "every risk score recalculates", "K-05": found["K-05"] or "every confidence recalculates", "K-06": found["K-06"] or "every TCO model adds up against the rate card", "K-07": found["K-07"] or "the disposition agent routed exactly its dispositions below 0.80", "K-11": found["K-11"] or "every record passed the input contract; every category is in the 7R enum", "K-02 lookup": found["K-02"] or ("every disposition's evidence_ref resolves to a record in the Discovery " "package; judge only whether the cited records support the rule-trace basis"), } return payload def post_process(result: dict) -> Optional[dict]: found = _found.get() if found is None: if result.get("verdict") is not None: logger.info("critic_code_checks_skipped") return None _found.set(None) return arithmetic.apply(result, found)