File size: 1,517 Bytes
877add7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"""Safety critic agent."""

from __future__ import annotations

from app.common.enums import ActionType, DecisionMode, DoseBucket
from app.common.types import PolyGuardAction, PolyGuardState
from app.env.verifier import verify_action_legality


class CriticAgent:
    name = "CriticAgent"

    def run(self, state: PolyGuardState, proposed: PolyGuardAction) -> dict:
        report = verify_action_legality(state, proposed)
        if report.legal:
            report_payload = report.model_dump(mode="json")
            return {
                "approved": True,
                "report": report_payload,
                "final_action": proposed,
                "legal": True,
                "violations": report_payload.get("violations", []),
            }
        fallback = PolyGuardAction(
            mode=DecisionMode.REVIEW,
            action_type=ActionType.REQUEST_SPECIALIST_REVIEW,
            target_drug=None,
            replacement_drug=None,
            dose_bucket=DoseBucket.NA,
            taper_days=None,
            monitoring_plan="critic_veto",
            candidate_id="cand_veto_fallback",
            confidence=0.62,
            rationale_brief=f"Critic veto: {', '.join(report.violations)}",
        )
        report_payload = report.model_dump(mode="json")
        return {
            "approved": False,
            "report": report_payload,
            "final_action": fallback,
            "legal": False,
            "violations": report_payload.get("violations", []),
        }