|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
5 | 5 | import asyncio |
| 6 | +import math |
6 | 7 | from typing import Any, Protocol, runtime_checkable |
7 | 8 |
|
8 | 9 | from pydantic import BaseModel, Field |
@@ -41,14 +42,56 @@ class JudgeResult(BaseModel): |
41 | 42 | score: float = Field(ge=0.0, le=1.0, default=0.5) |
42 | 43 | reason: str = "" |
43 | 44 |
|
44 | | - def to_finding(self, text_length: int) -> Finding: |
| 45 | + def enforced_score( |
| 46 | + self, |
| 47 | + *, |
| 48 | + block_threshold: float = 0.8, |
| 49 | + redact_threshold: float = 0.5, |
| 50 | + review_threshold: float = 0.3, |
| 51 | + ) -> float: |
| 52 | + """Clamp score so score-driven policy honors the declared action. |
| 53 | +
|
| 54 | + Inconsistent pairs are resolved in favor of ``action``: |
| 55 | + - ``BLOCK`` + low score → raise to at least ``block_threshold`` |
| 56 | + - ``REVIEW`` → land in ``[review_threshold, min(redact, block))`` |
| 57 | + - ``ALLOW`` + high score → cap below ``review_threshold`` |
| 58 | + """ |
| 59 | + score = self.score |
| 60 | + if self.action == Action.BLOCK: |
| 61 | + return min(1.0, max(score, block_threshold)) |
| 62 | + if self.action == Action.REVIEW: |
| 63 | + upper = min(redact_threshold, block_threshold) |
| 64 | + if upper <= review_threshold: |
| 65 | + return min(1.0, max(score, review_threshold)) |
| 66 | + floored = max(score, review_threshold) |
| 67 | + if floored >= upper: |
| 68 | + return max(review_threshold, math.nextafter(upper, 0.0)) |
| 69 | + return floored |
| 70 | + if self.action == Action.ALLOW: |
| 71 | + if score >= review_threshold: |
| 72 | + return max(0.0, math.nextafter(review_threshold, 0.0)) |
| 73 | + return score |
| 74 | + return score |
| 75 | + |
| 76 | + def to_finding( |
| 77 | + self, |
| 78 | + text_length: int, |
| 79 | + *, |
| 80 | + block_threshold: float = 0.8, |
| 81 | + redact_threshold: float = 0.5, |
| 82 | + review_threshold: float = 0.3, |
| 83 | + ) -> Finding: |
45 | 84 | return Finding( |
46 | 85 | category=self.category, |
47 | 86 | subcategory="llm_judge", |
48 | 87 | stage="llm_judge", |
49 | 88 | span_start=0, |
50 | 89 | span_end=text_length, |
51 | | - score=self.score, |
| 90 | + score=self.enforced_score( |
| 91 | + block_threshold=block_threshold, |
| 92 | + redact_threshold=redact_threshold, |
| 93 | + review_threshold=review_threshold, |
| 94 | + ), |
52 | 95 | evidence=self.reason, |
53 | 96 | ) |
54 | 97 |
|
|
0 commit comments