from dataclasses import dataclass
import math


@dataclass(frozen=True)
class Decision:
    action: str
    confidence: float


def validate(raw):
    action, score = raw["action"], raw["confidence"]
    if action not in {"lookup", "request_approval", "refund"}:
        raise ValueError("未知动作")
    if type(score) not in (float, int) or not math.isfinite(score):
        raise ValueError("置信值不合法")
    if not 0 <= score <= 1:
        raise ValueError("置信值越界")
    return Decision(action, score)


def route(decision, approved):
    if decision.action == "refund" and not approved:
        return "blocked"
    if decision.confidence < 0.9:  # 演示阈值，不能直接用于生产
        return "review"
    return decision.action


assert route(validate({"action": "refund", "confidence": 0.99}), False) == "blocked"
assert route(validate({"action": "lookup", "confidence": 0.6}), False) == "review"
assert route(validate({"action": "lookup", "confidence": 0.95}), False) == "lookup"
print("高置信不能越过授权，低置信进入复核")
