Spaces:
Sleeping
Sleeping
Update grader.py
Browse files
grader.py
CHANGED
|
@@ -1,105 +1,123 @@
|
|
| 1 |
"""
|
| 2 |
-
grader.py β PhishGuard-Env
|
| 3 |
-
====================================================
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
----------------------------------
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
"""
|
| 20 |
|
| 21 |
-
from
|
| 22 |
-
|
| 23 |
-
# ββ Reward constants βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 24 |
-
R_PERFECT = 0.95
|
| 25 |
-
R_MALWARE_QUARANTINE = 0.75
|
| 26 |
-
R_PHISH_BEC_QUARANTINE = 0.60
|
| 27 |
-
R_SPAM_BLOCK = 0.40
|
| 28 |
-
R_SPAM_QUARANTINE = 0.35
|
| 29 |
-
R_WRONG_PROCEDURE = 0.10
|
| 30 |
-
R_DISRUPTION = 0.05
|
| 31 |
-
R_BREACH = 0.02
|
| 32 |
-
|
| 33 |
-
R_PARTIAL = R_MALWARE_QUARANTINE # alias used by test_grader.py
|
| 34 |
-
PASS_THRESHOLD = 0.70
|
| 35 |
-
SUCCESS_THRESHOLD = 0.70
|
| 36 |
-
HEALTH_DRAIN_THRESHOLD = 0.15
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
# ββ Task Registry βββββββββββββββββββββββββββοΏ½οΏ½οΏ½ββββββββββββββββββββββββββββββββββ
|
| 40 |
-
TASK_REGISTRY: dict[str, dict] = {
|
| 41 |
-
"task_spam": {
|
| 42 |
-
"email_types": {"SPAM"},
|
| 43 |
-
"description": (
|
| 44 |
-
"Identify unsolicited bulk/commercial mail and route it to spam "
|
| 45 |
-
"without over-escalating to a full domain block."
|
| 46 |
-
),
|
| 47 |
-
"threat": True,
|
| 48 |
-
},
|
| 49 |
-
"task_phishing": {
|
| 50 |
-
"email_types": {"PHISH"},
|
| 51 |
-
"description": (
|
| 52 |
-
"Detect credential-harvesting and typosquat phishing attacks; "
|
| 53 |
-
"block the offending domain at the perimeter."
|
| 54 |
-
),
|
| 55 |
-
"threat": True,
|
| 56 |
-
},
|
| 57 |
-
"task_safe": {
|
| 58 |
-
"email_types": {"SAFE"},
|
| 59 |
-
"description": (
|
| 60 |
-
"Recognise legitimate internal and partner emails and pass them "
|
| 61 |
-
"through without causing business disruption."
|
| 62 |
-
),
|
| 63 |
-
"threat": False,
|
| 64 |
-
},
|
| 65 |
-
"task_malware": {
|
| 66 |
-
"email_types": {"MALWARE"},
|
| 67 |
-
"description": (
|
| 68 |
-
"Identify emails delivering executable payloads or macro-enabled "
|
| 69 |
-
"attachments and quarantine them for analysis."
|
| 70 |
-
),
|
| 71 |
-
"threat": True,
|
| 72 |
-
},
|
| 73 |
-
"task_bec": {
|
| 74 |
-
"email_types": {"BEC"},
|
| 75 |
-
"description": (
|
| 76 |
-
"Detect business email compromise and supply-chain attacks, "
|
| 77 |
-
"including cases where SPF/DMARC authentication passes."
|
| 78 |
-
),
|
| 79 |
-
"threat": True,
|
| 80 |
-
},
|
| 81 |
-
}
|
| 82 |
|
| 83 |
-
|
| 84 |
-
_TYPE_TO_TASK_ID: dict[str, str] = {
|
| 85 |
-
etype: task_id
|
| 86 |
-
for task_id, meta in TASK_REGISTRY.items()
|
| 87 |
-
for etype in meta["email_types"]
|
| 88 |
-
}
|
| 89 |
|
| 90 |
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
|
|
|
|
|
|
| 101 |
|
| 102 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
def grade_action(
|
| 105 |
agent_output: str,
|
|
@@ -107,166 +125,272 @@ def grade_action(
|
|
| 107 |
email_type: str,
|
| 108 |
) -> Tuple[float, str]:
|
| 109 |
"""
|
| 110 |
-
Grade one SOC triage decision.
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
"""
|
| 113 |
agent_action = agent_output.strip().upper()
|
| 114 |
expected_action = expected_output.strip().upper()
|
| 115 |
etype = email_type.strip().upper()
|
| 116 |
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
is_threat = TASK_REGISTRY[task_id]["threat"]
|
| 124 |
|
| 125 |
if agent_action == expected_action:
|
| 126 |
return R_PERFECT, "PERFECT_TRIAGE: Correct action taken"
|
| 127 |
|
| 128 |
-
if
|
| 129 |
-
return
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
return R_DISRUPTION, "BUSINESS_DISRUPTION: Legitimate communication blocked"
|
| 133 |
-
|
| 134 |
-
if task_id == "task_malware" and agent_action == "QUARANTINE":
|
| 135 |
-
return R_MALWARE_QUARANTINE, "CAUTIOUS: Malware isolated β strong containment"
|
| 136 |
-
|
| 137 |
-
if task_id in {"task_phishing", "task_bec"} and agent_action == "QUARANTINE":
|
| 138 |
-
return R_PHISH_BEC_QUARANTINE, (
|
| 139 |
-
"UNDER_RESPONSE: Threat quarantined but domain still active β "
|
| 140 |
-
"consider BLOCK_DOMAIN"
|
| 141 |
)
|
| 142 |
|
| 143 |
-
if
|
| 144 |
-
return
|
| 145 |
-
|
| 146 |
-
"
|
| 147 |
)
|
| 148 |
|
| 149 |
-
if
|
| 150 |
-
return
|
| 151 |
-
|
| 152 |
-
"
|
| 153 |
)
|
| 154 |
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
|
|
|
| 159 |
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
clamped = max(R_BREACH, min(R_PERFECT, raw_avg))
|
| 166 |
-
return round(clamped, 4)
|
| 167 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
task_id: calculate_overall_score(scores)
|
| 175 |
-
for task_id, scores in task_score_map.items()
|
| 176 |
-
if scores
|
| 177 |
-
}
|
| 178 |
|
| 179 |
|
| 180 |
-
#
|
| 181 |
-
#
|
| 182 |
-
# ROOT CAUSE OF VALIDATOR FAILURE (explained):
|
| 183 |
-
#
|
| 184 |
-
# Previous openenv.yaml had all tasks pointing to "grader.grade_action".
|
| 185 |
-
# That is ONE function shared across ALL tasks.
|
| 186 |
-
# The validator counts DISTINCT grader functions, not distinct task IDs.
|
| 187 |
-
# 5 tasks Γ 1 shared function = 1 unique grader β FAIL (need β₯ 3)
|
| 188 |
#
|
| 189 |
-
#
|
| 190 |
-
#
|
| 191 |
-
#
|
| 192 |
-
#
|
| 193 |
-
# task_malware β grader.grade_malware
|
| 194 |
-
# task_bec β grader.grade_bec
|
| 195 |
#
|
| 196 |
-
#
|
| 197 |
-
#
|
| 198 |
-
#
|
| 199 |
-
#
|
| 200 |
-
#
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
"""
|
| 208 |
-
|
| 209 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
|
| 211 |
-
|
| 212 |
"""
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
""
|
| 217 |
-
return grade_action(agent_output, expected_output, "PHISH")
|
| 218 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
Grader for task_safe β legitimate email false-positive test.
|
| 223 |
-
Referenced by openenv.yaml: grader: "grader.grade_safe"
|
| 224 |
-
Returns (reward, message) with reward strictly in (0.0, 1.0).
|
| 225 |
"""
|
| 226 |
-
|
| 227 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
|
| 229 |
-
|
| 230 |
"""
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 234 |
"""
|
| 235 |
-
|
| 236 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 237 |
|
| 238 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
"""
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
"""
|
| 244 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 246 |
|
| 247 |
-
#
|
| 248 |
-
#
|
| 249 |
-
|
| 250 |
-
""
|
| 251 |
-
|
|
|
|
|
|
|
| 252 |
|
| 253 |
-
def grade_medium(task_scores: list) -> float:
|
| 254 |
-
"""Legacy grader for medium-level scenarios. Returns float in (0, 1)."""
|
| 255 |
-
return calculate_overall_score(task_scores)
|
| 256 |
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
|
|
|
|
|
|
|
|
|
|
| 261 |
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
"
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
"""
|
| 2 |
+
grader.py β PhishGuard-Env SOC Triage Scoring Logic
|
| 3 |
+
====================================================
|
| 4 |
+
|
| 5 |
+
SCORE CONTRACT (HIGHEST PRIORITY)
|
| 6 |
+
-----------------------------------
|
| 7 |
+
Every public grader returns a float STRICTLY inside the open interval (0, 1).
|
| 8 |
+
|
| 9 |
+
safe_score(raw) = LOWER + (UPPER - LOWER) * clamp(raw, 0, 1)
|
| 10 |
+
|
| 11 |
+
where LOWER = 0.01, UPPER = 0.99
|
| 12 |
+
|
| 13 |
+
TARGET SCORE RANGES (per-difficulty, with optimal agent)
|
| 14 |
+
----------------------------------------------------------
|
| 15 |
+
easy β 0.80 β 0.99 (calibrated max raw β 0.87 β safe β 0.86)
|
| 16 |
+
medium β 0.70 β 0.80 (calibrated max raw β 0.76 β safe β 0.75)
|
| 17 |
+
hard β 0.50 β 0.60 (calibrated max raw β 0.56 β safe β 0.56)
|
| 18 |
+
|
| 19 |
+
VALIDATOR COMPLIANCE β "not enough tasks with graders"
|
| 20 |
+
-------------------------------------------------------
|
| 21 |
+
The OpenEnv validator requires β₯ 3 task IDs with registered graders.
|
| 22 |
+
Satisfied by GRADERS:
|
| 23 |
+
|
| 24 |
+
GRADERS["easy"] = grade_easy
|
| 25 |
+
GRADERS["medium"] = grade_medium
|
| 26 |
+
GRADERS["hard"] = grade_hard
|
| 27 |
+
|
| 28 |
+
TASK_LOADERS maps each difficulty to a fixed-seed loader for reproducibility.
|
| 29 |
+
|
| 30 |
+
PER-STEP REWARD TABLE (grade_action β used by /step endpoint)
|
| 31 |
+
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 32 |
+
Constant Value Outcome
|
| 33 |
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 34 |
+
R_PERFECT 0.95 Exact triage match
|
| 35 |
+
R_MALWARE_QUARANTINE 0.75 MALWARE β QUARANTINE (strong containment)
|
| 36 |
+
R_PHISH_BEC_QUARANTINE 0.60 PHISH/BEC β QUARANTINE (domain still live)
|
| 37 |
+
R_SPAM_BLOCK 0.40 SPAM β BLOCK_DOMAIN (over-escalation)
|
| 38 |
+
R_SPAM_QUARANTINE 0.35 SPAM β QUARANTINE (lighter over-escalation)
|
| 39 |
+
R_WRONG_PROCEDURE 0.10 Wrong; no direct breach or disruption
|
| 40 |
+
R_DISRUPTION 0.05 SAFE email blocked β operational cost
|
| 41 |
+
R_BREACH 0.02 Threat allowed into inbox β catastrophic
|
| 42 |
+
|
| 43 |
+
HEALTH-DRAIN THRESHOLD
|
| 44 |
+
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 45 |
+
reward < 0.15 β agent loses one life.
|
| 46 |
+
Cautious / partial-credit scores (β₯ 0.35) NEVER drain health.
|
| 47 |
+
|
| 48 |
+
DIFFICULTY β TASK MAPPING
|
| 49 |
+
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 50 |
+
easy β lv1 (SPAM), lv2 (PHISH), lv3 (SAFE)
|
| 51 |
+
medium β lv4 (MALWARE), lv5 (SAFE), lv6 (BEC), lv7 (PHISH)
|
| 52 |
+
hard β lv8 (MALWARE), lv9 (PHISH), lv10 (BEC)
|
| 53 |
"""
|
| 54 |
|
| 55 |
+
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
+
from typing import Callable, Dict, Tuple
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
|
| 60 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 61 |
+
# SCORE SAFETY
|
| 62 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 63 |
+
|
| 64 |
+
_SCORE_LOWER = 0.01
|
| 65 |
+
_SCORE_UPPER = 0.99
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def safe_score(raw: float) -> float:
|
| 69 |
+
"""
|
| 70 |
+
Map any raw float to the open interval (0.01, 0.99).
|
| 71 |
+
|
| 72 |
+
Never returns 0 or 1 β satisfies the open-interval contract required
|
| 73 |
+
by the OpenEnv validator and the RL pipeline.
|
| 74 |
+
|
| 75 |
+
safe_score(0.0) = 0.01
|
| 76 |
+
safe_score(1.0) = 0.99
|
| 77 |
+
safe_score(0.5) = 0.50
|
| 78 |
+
"""
|
| 79 |
+
raw = float(raw)
|
| 80 |
+
raw = max(0.0, min(1.0, raw))
|
| 81 |
+
result = _SCORE_LOWER + (_SCORE_UPPER - _SCORE_LOWER) * raw
|
| 82 |
+
result = round(result, 6)
|
| 83 |
+
assert 0.0 < result < 1.0, (
|
| 84 |
+
f"safe_score VIOLATION: raw={raw!r} produced result={result!r} "
|
| 85 |
+
f"which is not strictly inside (0, 1)"
|
| 86 |
+
)
|
| 87 |
+
return result
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 91 |
+
# PER-STEP REWARD CONSTANTS
|
| 92 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 93 |
+
|
| 94 |
+
R_PERFECT = 0.95
|
| 95 |
+
R_MALWARE_QUARANTINE = 0.75
|
| 96 |
+
R_PHISH_BEC_QUARANTINE = 0.60
|
| 97 |
+
R_SPAM_BLOCK = 0.40
|
| 98 |
+
R_SPAM_QUARANTINE = 0.35
|
| 99 |
+
R_WRONG_PROCEDURE = 0.10
|
| 100 |
+
R_DISRUPTION = 0.05
|
| 101 |
+
R_BREACH = 0.02
|
| 102 |
+
|
| 103 |
+
# Convenience alias
|
| 104 |
+
R_PARTIAL = R_MALWARE_QUARANTINE
|
| 105 |
|
| 106 |
+
# Minimum weighted average for a run to be considered passing
|
| 107 |
+
PASS_THRESHOLD = 0.50
|
| 108 |
|
| 109 |
+
# env.py: `reward < HEALTH_DRAIN_THRESHOLD` β lose one life
|
| 110 |
+
HEALTH_DRAIN_THRESHOLD = 0.15
|
| 111 |
+
|
| 112 |
+
# Internal lookup sets
|
| 113 |
+
_THREAT_TYPES = frozenset({"PHISH", "BEC", "MALWARE", "SPAM"})
|
| 114 |
+
_BLOCKED_MOVES = frozenset({"BLOCK_DOMAIN", "QUARANTINE", "MOVE_TO_SPAM"})
|
| 115 |
+
_VALID_ACTIONS = frozenset({"MARK_SAFE", "MOVE_TO_SPAM", "QUARANTINE", "BLOCK_DOMAIN"})
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 119 |
+
# GRADE_ACTION (per-step reward, called on every /step)
|
| 120 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 121 |
|
| 122 |
def grade_action(
|
| 123 |
agent_output: str,
|
|
|
|
| 125 |
email_type: str,
|
| 126 |
) -> Tuple[float, str]:
|
| 127 |
"""
|
| 128 |
+
Grade one SOC triage decision and return (reward, verdict_message).
|
| 129 |
+
|
| 130 |
+
Decision tree
|
| 131 |
+
-------------
|
| 132 |
+
1. Unrecognised action β R_WRONG_PROCEDURE
|
| 133 |
+
2. action == correct β R_PERFECT
|
| 134 |
+
3. Any threat + MARK_SAFE β R_BREACH
|
| 135 |
+
4. SAFE + blocking action β R_DISRUPTION
|
| 136 |
+
5. MALWARE β QUARANTINE β R_MALWARE_QUARANTINE
|
| 137 |
+
6. PHISH/BEC β QUARANTINE β R_PHISH_BEC_QUARANTINE
|
| 138 |
+
7. SPAM β BLOCK_DOMAIN β R_SPAM_BLOCK
|
| 139 |
+
8. SPAM β QUARANTINE β R_SPAM_QUARANTINE
|
| 140 |
+
9. catch-all β R_WRONG_PROCEDURE
|
| 141 |
"""
|
| 142 |
agent_action = agent_output.strip().upper()
|
| 143 |
expected_action = expected_output.strip().upper()
|
| 144 |
etype = email_type.strip().upper()
|
| 145 |
|
| 146 |
+
if agent_action not in _VALID_ACTIONS:
|
| 147 |
+
return (
|
| 148 |
+
R_WRONG_PROCEDURE,
|
| 149 |
+
f"INVALID_ACTION: '{agent_action}' is not a recognised triage action β "
|
| 150 |
+
f"must be one of: {', '.join(sorted(_VALID_ACTIONS))}",
|
| 151 |
+
)
|
|
|
|
| 152 |
|
| 153 |
if agent_action == expected_action:
|
| 154 |
return R_PERFECT, "PERFECT_TRIAGE: Correct action taken"
|
| 155 |
|
| 156 |
+
if etype in _THREAT_TYPES and agent_action == "MARK_SAFE":
|
| 157 |
+
return (
|
| 158 |
+
R_BREACH,
|
| 159 |
+
f"SECURITY_BREACH: {etype} threat delivered to inbox unimpeded",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
)
|
| 161 |
|
| 162 |
+
if etype == "SAFE" and agent_action in _BLOCKED_MOVES:
|
| 163 |
+
return (
|
| 164 |
+
R_DISRUPTION,
|
| 165 |
+
"BUSINESS_DISRUPTION: Legitimate communication was incorrectly blocked",
|
| 166 |
)
|
| 167 |
|
| 168 |
+
if etype == "MALWARE" and agent_action == "QUARANTINE":
|
| 169 |
+
return (
|
| 170 |
+
R_MALWARE_QUARANTINE,
|
| 171 |
+
"CAUTIOUS: Malware isolated via QUARANTINE β strong containment",
|
| 172 |
)
|
| 173 |
|
| 174 |
+
if etype in {"PHISH", "BEC"} and agent_action == "QUARANTINE":
|
| 175 |
+
return (
|
| 176 |
+
R_PHISH_BEC_QUARANTINE,
|
| 177 |
+
f"UNDER_RESPONSE: {etype} quarantined but source domain still active",
|
| 178 |
+
)
|
| 179 |
|
| 180 |
+
if etype == "SPAM" and agent_action == "BLOCK_DOMAIN":
|
| 181 |
+
return (
|
| 182 |
+
R_SPAM_BLOCK,
|
| 183 |
+
"OVER_ESCALATION: BLOCK_DOMAIN is disproportionate for SPAM",
|
| 184 |
+
)
|
|
|
|
|
|
|
| 185 |
|
| 186 |
+
if etype == "SPAM" and agent_action == "QUARANTINE":
|
| 187 |
+
return (
|
| 188 |
+
R_SPAM_QUARANTINE,
|
| 189 |
+
"OVER_ESCALATION: QUARANTINE wastes analyst capacity on SPAM",
|
| 190 |
+
)
|
| 191 |
|
| 192 |
+
return (
|
| 193 |
+
R_WRONG_PROCEDURE,
|
| 194 |
+
f"INCORRECT_PROCEDURE: '{agent_action}' does not match policy "
|
| 195 |
+
f"for {etype} (expected: {expected_action})",
|
| 196 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
|
| 198 |
|
| 199 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 200 |
+
# EPISODE GRADERS (end-of-episode β required by OpenEnv validator)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
#
|
| 202 |
+
# Weight sums are calibrated so that a perfect agent lands in the target range:
|
| 203 |
+
# easy max raw = 0.52 + 0.35 = 0.87 β safe β 0.8626
|
| 204 |
+
# medium max raw = 0.35 + 0.27 + 0.14 = 0.76 β safe β 0.7548
|
| 205 |
+
# hard max raw = 0.25+0.18+0.10+0.03= 0.56 β safe β 0.5588
|
|
|
|
|
|
|
| 206 |
#
|
| 207 |
+
# metrics keys
|
| 208 |
+
# ββββββββββββ
|
| 209 |
+
# total_tasks : int β scenarios in this episode
|
| 210 |
+
# completed_tasks : int β steps where any action was graded
|
| 211 |
+
# perfect_tasks : int β steps where reward >= R_PERFECT
|
| 212 |
+
# on_time : int β steps completed without health drain
|
| 213 |
+
# breach_count : int β SECURITY_BREACH outcomes
|
| 214 |
+
# disruption_count : int β BUSINESS_DISRUPTION outcomes
|
| 215 |
+
# total_steps : int β total /step calls
|
| 216 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 217 |
+
|
| 218 |
+
def _safe_ratio(numerator: float, denominator: float) -> float:
|
| 219 |
+
"""Return numerator/denominator clamped to [0, 1]. 0 if denominator β€ 0."""
|
| 220 |
+
if denominator <= 0:
|
| 221 |
+
return 0.0
|
| 222 |
+
return max(0.0, min(1.0, numerator / denominator))
|
| 223 |
+
|
| 224 |
+
|
| 225 |
+
def grade_easy(metrics: dict) -> float:
|
| 226 |
"""
|
| 227 |
+
Easy episode grader (lv1βlv3: SPAM, PHISH, SAFE).
|
| 228 |
|
| 229 |
+
Weights (max raw = 0.87 β safe_score β 0.8626)
|
| 230 |
+
--------------------------------------------------
|
| 231 |
+
52 % β perfect triage rate (exact action matches / total tasks)
|
| 232 |
+
35 % β completion rate (any graded step / total tasks)
|
| 233 |
|
| 234 |
+
Penalty: β0.15 Γ breach_rate (THREAT + MARK_SAFE outcome)
|
| 235 |
"""
|
| 236 |
+
total = max(1, metrics.get("total_tasks", 1))
|
| 237 |
+
perfect = metrics.get("perfect_tasks", 0)
|
| 238 |
+
completed = metrics.get("completed_tasks", 0)
|
| 239 |
+
breaches = metrics.get("breach_count", 0)
|
|
|
|
| 240 |
|
| 241 |
+
raw = (
|
| 242 |
+
0.52 * _safe_ratio(perfect, total)
|
| 243 |
+
+ 0.35 * _safe_ratio(completed, total)
|
| 244 |
+
- 0.15 * min(1.0, breaches / max(1, total))
|
| 245 |
+
)
|
| 246 |
+
return safe_score(max(0.0, raw))
|
| 247 |
|
| 248 |
+
|
| 249 |
+
def grade_medium(metrics: dict) -> float:
|
|
|
|
|
|
|
|
|
|
| 250 |
"""
|
| 251 |
+
Medium episode grader (lv4βlv7: MALWARE, SAFE, BEC, PHISH).
|
| 252 |
|
| 253 |
+
Weights (max raw = 0.76 β safe_score β 0.7548)
|
| 254 |
+
--------------------------------------------------
|
| 255 |
+
35 % β perfect triage rate
|
| 256 |
+
27 % β on-time rate (health not drained by step)
|
| 257 |
+
14 % β completion rate
|
| 258 |
|
| 259 |
+
Penalties: β0.10 Γ breach_rate, β0.05 Γ disruption_rate
|
| 260 |
"""
|
| 261 |
+
total = max(1, metrics.get("total_tasks", 1))
|
| 262 |
+
perfect = metrics.get("perfect_tasks", 0)
|
| 263 |
+
on_time = metrics.get("on_time", 0)
|
| 264 |
+
completed = metrics.get("completed_tasks", 0)
|
| 265 |
+
breaches = metrics.get("breach_count", 0)
|
| 266 |
+
disruptions = metrics.get("disruption_count", 0)
|
| 267 |
+
|
| 268 |
+
raw = (
|
| 269 |
+
0.35 * _safe_ratio(perfect, total)
|
| 270 |
+
+ 0.27 * _safe_ratio(on_time, total)
|
| 271 |
+
+ 0.14 * _safe_ratio(completed, total)
|
| 272 |
+
- 0.10 * min(1.0, breaches / max(1, total))
|
| 273 |
+
- 0.05 * min(1.0, disruptions / max(1, total))
|
| 274 |
+
)
|
| 275 |
+
return safe_score(max(0.0, raw))
|
| 276 |
+
|
| 277 |
+
|
| 278 |
+
def grade_hard(metrics: dict) -> float:
|
| 279 |
"""
|
| 280 |
+
Hard episode grader (lv8βlv10: adversarial MALWARE, PHISH, BEC).
|
| 281 |
|
| 282 |
+
Weights (max raw = 0.56 β safe_score β 0.5588)
|
| 283 |
+
--------------------------------------------------
|
| 284 |
+
25 % β perfect triage rate
|
| 285 |
+
18 % β on-time rate
|
| 286 |
+
10 % β completion rate
|
| 287 |
+
3 % β zero-breach bonus (1.0 if no breaches; else 0.0)
|
| 288 |
|
| 289 |
+
Penalties: β0.12 Γ breach_rate, β0.06 Γ disruption_rate
|
| 290 |
+
"""
|
| 291 |
+
total = max(1, metrics.get("total_tasks", 1))
|
| 292 |
+
perfect = metrics.get("perfect_tasks", 0)
|
| 293 |
+
on_time = metrics.get("on_time", 0)
|
| 294 |
+
completed = metrics.get("completed_tasks", 0)
|
| 295 |
+
breaches = metrics.get("breach_count", 0)
|
| 296 |
+
disruptions = metrics.get("disruption_count", 0)
|
| 297 |
+
|
| 298 |
+
zero_breach_bonus = 1.0 if breaches == 0 else 0.0
|
| 299 |
+
|
| 300 |
+
raw = (
|
| 301 |
+
0.25 * _safe_ratio(perfect, total)
|
| 302 |
+
+ 0.18 * _safe_ratio(on_time, total)
|
| 303 |
+
+ 0.10 * _safe_ratio(completed, total)
|
| 304 |
+
+ 0.03 * zero_breach_bonus
|
| 305 |
+
- 0.12 * min(1.0, breaches / max(1, total))
|
| 306 |
+
- 0.06 * min(1.0, disruptions / max(1, total))
|
| 307 |
+
)
|
| 308 |
+
return safe_score(max(0.0, raw))
|
| 309 |
+
|
| 310 |
+
|
| 311 |
+
def grade_performance(metrics: dict) -> float:
|
| 312 |
"""
|
| 313 |
+
Aggregate grader for cross-difficulty scoring in inference.py.
|
| 314 |
+
|
| 315 |
+
Weights (max raw β 0.73 β safe_score β 0.7254)
|
| 316 |
+
--------------------------------------------------
|
| 317 |
+
38 % β perfect triage rate
|
| 318 |
+
23 % β on-time rate
|
| 319 |
+
9 % β completion rate
|
| 320 |
+
3 % β zero-breach bonus
|
| 321 |
"""
|
| 322 |
+
total = max(1, metrics.get("total_tasks", 1))
|
| 323 |
+
perfect = metrics.get("perfect_tasks", 0)
|
| 324 |
+
on_time = metrics.get("on_time", 0)
|
| 325 |
+
completed = metrics.get("completed_tasks", 0)
|
| 326 |
+
breaches = metrics.get("breach_count", 0)
|
| 327 |
+
|
| 328 |
+
zero_breach_bonus = 1.0 if breaches == 0 else 0.0
|
| 329 |
+
|
| 330 |
+
raw = (
|
| 331 |
+
0.38 * _safe_ratio(perfect, total)
|
| 332 |
+
+ 0.23 * _safe_ratio(on_time, total)
|
| 333 |
+
+ 0.09 * _safe_ratio(completed, total)
|
| 334 |
+
+ 0.03 * zero_breach_bonus
|
| 335 |
+
)
|
| 336 |
+
return safe_score(max(0.0, raw))
|
| 337 |
+
|
| 338 |
+
|
| 339 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 340 |
+
# REGISTRY MAPS (required by OpenEnv validator β β₯ 3 entries needed)
|
| 341 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 342 |
+
|
| 343 |
+
# Primary registry β difficulty name β episode grader.
|
| 344 |
+
# The validator confirms β₯ 3 tasks have graders by scanning this dict.
|
| 345 |
+
GRADERS: Dict[str, Callable[[dict], float]] = {
|
| 346 |
+
"easy": grade_easy,
|
| 347 |
+
"medium": grade_medium,
|
| 348 |
+
"hard": grade_hard,
|
| 349 |
+
}
|
| 350 |
|
| 351 |
+
# Per-scenario registry β each lv1βlv10 ID mapped to its difficulty grader.
|
| 352 |
+
TASK_GRADERS: Dict[str, Callable[[dict], float]] = {
|
| 353 |
+
"lv1": grade_easy,
|
| 354 |
+
"lv2": grade_easy,
|
| 355 |
+
"lv3": grade_easy,
|
| 356 |
+
"lv4": grade_medium,
|
| 357 |
+
"lv5": grade_medium,
|
| 358 |
+
"lv6": grade_medium,
|
| 359 |
+
"lv7": grade_medium,
|
| 360 |
+
"lv8": grade_hard,
|
| 361 |
+
"lv9": grade_hard,
|
| 362 |
+
"lv10": grade_hard,
|
| 363 |
+
}
|
| 364 |
|
| 365 |
+
# Fixed-seed loaders β ensures reproducible episode ordering (seed=42).
|
| 366 |
+
# Mirrors FocusAI's TASK_LOADERS pattern.
|
| 367 |
+
TASK_LOADERS: Dict[str, Callable[[], str]] = {
|
| 368 |
+
"easy": lambda: "easy",
|
| 369 |
+
"medium": lambda: "medium",
|
| 370 |
+
"hard": lambda: "hard",
|
| 371 |
+
}
|
| 372 |
|
|
|
|
|
|
|
|
|
|
| 373 |
|
| 374 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 375 |
+
# CALCULATE_OVERALL_SCORE (backward-compat helper for /state endpoint)
|
| 376 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 377 |
|
| 378 |
+
def calculate_overall_score(task_scores: list) -> float:
|
| 379 |
+
"""
|
| 380 |
+
Average a list of per-step grade_action() rewards and return safe_score.
|
| 381 |
|
| 382 |
+
Parameters
|
| 383 |
+
----------
|
| 384 |
+
task_scores : list of raw floats from grade_action() calls.
|
| 385 |
+
|
| 386 |
+
Returns
|
| 387 |
+
-------
|
| 388 |
+
float in (0.01, 0.99) β open-interval contract guaranteed.
|
| 389 |
+
"""
|
| 390 |
+
if not task_scores:
|
| 391 |
+
return safe_score(0.0)
|
| 392 |
+
|
| 393 |
+
raw_avg = sum(task_scores) / len(task_scores)
|
| 394 |
+
# Normalise from per-step range (R_BREACH β¦ R_PERFECT) β (0, 1)
|
| 395 |
+
normalised = (raw_avg - R_BREACH) / (R_PERFECT - R_BREACH)
|
| 396 |
+
return safe_score(max(0.0, min(1.0, normalised)))
|