Spaces:
Sleeping
Sleeping
Update grader.py
Browse files
grader.py
CHANGED
|
@@ -1,403 +1,397 @@
|
|
| 1 |
-
"""
|
| 2 |
-
grader.py β PhishGuard-Env SOC Triage Scoring Logic
|
| 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 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
"
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
"
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
#
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
(reward,
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
if
|
| 150 |
-
return (
|
| 151 |
-
|
| 152 |
-
f"
|
| 153 |
-
"
|
| 154 |
-
)
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
if etype == "SPAM" and agent_action == "BLOCK_DOMAIN":
|
| 184 |
-
return (
|
| 185 |
-
R_SPAM_BLOCK,
|
| 186 |
-
"OVER_ESCALATION: BLOCK_DOMAIN is disproportionate for SPAM
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
#
|
| 208 |
-
#
|
| 209 |
-
#
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
return
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
raw =
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
)
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
"""
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
""
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
0.
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
)
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
"""
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
SCENARIO_LOADERS = {
|
| 400 |
-
"easy": _get_easy_scenarios,
|
| 401 |
-
"medium": _get_medium_scenarios,
|
| 402 |
-
"hard": _get_hard_scenarios,
|
| 403 |
-
}
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
grader.py β PhishGuard-Env SOC Triage Scoring Logic
|
| 3 |
+
====================================================
|
| 4 |
+
|
| 5 |
+
SCORE CONTRACT (HIGHEST PRIORITY β mirrors FocusAI reward_and_tasks.py)
|
| 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 |
+
This guarantees:
|
| 14 |
+
raw = 0.0 β 0.01 (> 0, never equals 0)
|
| 15 |
+
raw = 1.0 β 0.99 (< 1, never equals 1)
|
| 16 |
+
raw = 0.5 β 0.50
|
| 17 |
+
|
| 18 |
+
VALIDATOR COMPLIANCE β "not enough tasks with graders"
|
| 19 |
+
-------------------------------------------------------
|
| 20 |
+
The OpenEnv validator requires at least 3 task IDs that each have a
|
| 21 |
+
registered grader function. This is satisfied by the GRADERS dict:
|
| 22 |
+
|
| 23 |
+
GRADERS["easy"] = grade_easy
|
| 24 |
+
GRADERS["medium"] = grade_medium
|
| 25 |
+
GRADERS["hard"] = grade_hard
|
| 26 |
+
|
| 27 |
+
TASK_GRADERS additionally maps every lv1βlv10 scenario ID to its level
|
| 28 |
+
grader for per-task lookups from env.py.
|
| 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 (good 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 |
+
LEVEL β 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 (mirrors FocusAI safe_score exactly)
|
| 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 (mid-range cautious signal)
|
| 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,
|
| 124 |
+
expected_output: str,
|
| 125 |
+
email_type: str,
|
| 126 |
+
) -> Tuple[float, str]:
|
| 127 |
+
"""
|
| 128 |
+
Grade one SOC triage decision and return (reward, verdict_message).
|
| 129 |
+
|
| 130 |
+
reward is a raw float in (0.02, 0.95).
|
| 131 |
+
Callers may wrap with safe_score() for the hard (0.01, 0.99) contract.
|
| 132 |
+
|
| 133 |
+
Decision tree
|
| 134 |
+
-------------
|
| 135 |
+
1. Unrecognised action β R_WRONG_PROCEDURE
|
| 136 |
+
2. action == correct β R_PERFECT
|
| 137 |
+
3. Any threat + MARK_SAFE β R_BREACH
|
| 138 |
+
4. SAFE + blocking action β R_DISRUPTION
|
| 139 |
+
5. MALWARE β QUARANTINE β R_MALWARE_QUARANTINE
|
| 140 |
+
6. PHISH/BEC β QUARANTINE β R_PHISH_BEC_QUARANTINE
|
| 141 |
+
7. SPAM β BLOCK_DOMAIN β R_SPAM_BLOCK
|
| 142 |
+
8. SPAM β QUARANTINE β R_SPAM_QUARANTINE
|
| 143 |
+
9. catch-all β R_WRONG_PROCEDURE
|
| 144 |
+
"""
|
| 145 |
+
agent_action = agent_output.strip().upper()
|
| 146 |
+
expected_action = expected_output.strip().upper()
|
| 147 |
+
etype = email_type.strip().upper()
|
| 148 |
+
|
| 149 |
+
if agent_action not in _VALID_ACTIONS:
|
| 150 |
+
return (
|
| 151 |
+
R_WRONG_PROCEDURE,
|
| 152 |
+
f"INVALID_ACTION: '{agent_action}' is not a recognised triage action β "
|
| 153 |
+
f"must be one of: {', '.join(sorted(_VALID_ACTIONS))}",
|
| 154 |
+
)
|
| 155 |
+
|
| 156 |
+
if agent_action == expected_action:
|
| 157 |
+
return R_PERFECT, "PERFECT_TRIAGE: Correct action taken"
|
| 158 |
+
|
| 159 |
+
if etype in _THREAT_TYPES and agent_action == "MARK_SAFE":
|
| 160 |
+
return (
|
| 161 |
+
R_BREACH,
|
| 162 |
+
f"SECURITY_BREACH: {etype} threat delivered to inbox unimpeded",
|
| 163 |
+
)
|
| 164 |
+
|
| 165 |
+
if etype == "SAFE" and agent_action in _BLOCKED_MOVES:
|
| 166 |
+
return (
|
| 167 |
+
R_DISRUPTION,
|
| 168 |
+
"BUSINESS_DISRUPTION: Legitimate communication was incorrectly blocked",
|
| 169 |
+
)
|
| 170 |
+
|
| 171 |
+
if etype == "MALWARE" and agent_action == "QUARANTINE":
|
| 172 |
+
return (
|
| 173 |
+
R_MALWARE_QUARANTINE,
|
| 174 |
+
"CAUTIOUS: Malware isolated via QUARANTINE β strong containment",
|
| 175 |
+
)
|
| 176 |
+
|
| 177 |
+
if etype in {"PHISH", "BEC"} and agent_action == "QUARANTINE":
|
| 178 |
+
return (
|
| 179 |
+
R_PHISH_BEC_QUARANTINE,
|
| 180 |
+
f"UNDER_RESPONSE: {etype} quarantined but source domain still active",
|
| 181 |
+
)
|
| 182 |
+
|
| 183 |
+
if etype == "SPAM" and agent_action == "BLOCK_DOMAIN":
|
| 184 |
+
return (
|
| 185 |
+
R_SPAM_BLOCK,
|
| 186 |
+
"OVER_ESCALATION: BLOCK_DOMAIN is disproportionate for SPAM",
|
| 187 |
+
)
|
| 188 |
+
|
| 189 |
+
if etype == "SPAM" and agent_action == "QUARANTINE":
|
| 190 |
+
return (
|
| 191 |
+
R_SPAM_QUARANTINE,
|
| 192 |
+
"OVER_ESCALATION: QUARANTINE wastes analyst capacity on SPAM",
|
| 193 |
+
)
|
| 194 |
+
|
| 195 |
+
return (
|
| 196 |
+
R_WRONG_PROCEDURE,
|
| 197 |
+
f"INCORRECT_PROCEDURE: '{agent_action}' does not match policy "
|
| 198 |
+
f"for {etype} (expected: {expected_action})",
|
| 199 |
+
)
|
| 200 |
+
|
| 201 |
+
|
| 202 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 203 |
+
# EPISODE GRADERS (end-of-episode β required by OpenEnv validator)
|
| 204 |
+
#
|
| 205 |
+
# Each grader accepts a `metrics` dict built by env.py and returns safe_score.
|
| 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 (threat + MARK_SAFE)
|
| 214 |
+
# disruption_count : int β BUSINESS_DISRUPTION outcomes (SAFE + blocked)
|
| 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
|
| 230 |
+
-------
|
| 231 |
+
60 % β perfect triage rate (exact action matches / total tasks)
|
| 232 |
+
40 % β completion rate (any graded step / total tasks)
|
| 233 |
+
|
| 234 |
+
Penalty: β0.15 Γ breach_rate (THREAT + MARK_SAFE outcomes)
|
| 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.60 * _safe_ratio(perfect, total)
|
| 243 |
+
+ 0.40 * _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
|
| 254 |
+
-------
|
| 255 |
+
45 % β perfect triage rate
|
| 256 |
+
35 % β on-time rate (health not drained by step)
|
| 257 |
+
20 % β completion rate
|
| 258 |
+
|
| 259 |
+
Penalty: β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.45 * _safe_ratio(perfect, total)
|
| 270 |
+
+ 0.35 * _safe_ratio(on_time, total)
|
| 271 |
+
+ 0.20 * _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
|
| 283 |
+
-------
|
| 284 |
+
40 % β perfect triage rate
|
| 285 |
+
30 % β on-time rate
|
| 286 |
+
20 % β completion rate
|
| 287 |
+
10 % β zero-breach bonus (1.0 if no breaches; else 0.0)
|
| 288 |
+
|
| 289 |
+
Penalty: β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.40 * _safe_ratio(perfect, total)
|
| 302 |
+
+ 0.30 * _safe_ratio(on_time, total)
|
| 303 |
+
+ 0.20 * _safe_ratio(completed, total)
|
| 304 |
+
+ 0.10 * 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-level scoring in inference.py.
|
| 314 |
+
|
| 315 |
+
Produces one float for the full run (all levels combined).
|
| 316 |
+
Mirrors FocusAI's grade_performance signature exactly.
|
| 317 |
+
|
| 318 |
+
Weights
|
| 319 |
+
-------
|
| 320 |
+
40 % β perfect triage rate
|
| 321 |
+
30 % β on-time rate
|
| 322 |
+
20 % β completion rate
|
| 323 |
+
10 % β zero-breach bonus
|
| 324 |
+
"""
|
| 325 |
+
total = max(1, metrics.get("total_tasks", 1))
|
| 326 |
+
perfect = metrics.get("perfect_tasks", 0)
|
| 327 |
+
on_time = metrics.get("on_time", 0)
|
| 328 |
+
completed = metrics.get("completed_tasks", 0)
|
| 329 |
+
breaches = metrics.get("breach_count", 0)
|
| 330 |
+
|
| 331 |
+
zero_breach_bonus = 1.0 if breaches == 0 else 0.0
|
| 332 |
+
|
| 333 |
+
raw = (
|
| 334 |
+
0.40 * _safe_ratio(perfect, total)
|
| 335 |
+
+ 0.30 * _safe_ratio(on_time, total)
|
| 336 |
+
+ 0.20 * _safe_ratio(completed, total)
|
| 337 |
+
+ 0.10 * zero_breach_bonus
|
| 338 |
+
)
|
| 339 |
+
return safe_score(max(0.0, raw))
|
| 340 |
+
|
| 341 |
+
|
| 342 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 343 |
+
# REGISTRY MAPS (required by OpenEnv validator β β₯ 3 entries needed)
|
| 344 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 345 |
+
|
| 346 |
+
# Primary registry β level name β episode grader.
|
| 347 |
+
# The validator scans GRADERS to confirm β₯ 3 tasks have graders.
|
| 348 |
+
GRADERS: Dict[str, Callable[[dict], float]] = {
|
| 349 |
+
"easy": grade_easy,
|
| 350 |
+
"medium": grade_medium,
|
| 351 |
+
"hard": grade_hard,
|
| 352 |
+
}
|
| 353 |
+
|
| 354 |
+
# Per-scenario registry β each lv1βlv10 ID mapped to its level grader.
|
| 355 |
+
# env.py uses this for per-task score lookups in the /step response.
|
| 356 |
+
TASK_GRADERS: Dict[str, Callable[[dict], float]] = {
|
| 357 |
+
"lv1": grade_easy,
|
| 358 |
+
"lv2": grade_easy,
|
| 359 |
+
"lv3": grade_easy,
|
| 360 |
+
"lv4": grade_medium,
|
| 361 |
+
"lv5": grade_medium,
|
| 362 |
+
"lv6": grade_medium,
|
| 363 |
+
"lv7": grade_medium,
|
| 364 |
+
"lv8": grade_hard,
|
| 365 |
+
"lv9": grade_hard,
|
| 366 |
+
"lv10": grade_hard,
|
| 367 |
+
}
|
| 368 |
+
|
| 369 |
+
|
| 370 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 371 |
+
# CALCULATE_OVERALL_SCORE (backward-compat helper for /state endpoint)
|
| 372 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 373 |
+
|
| 374 |
+
def calculate_overall_score(task_scores: list) -> float:
|
| 375 |
+
"""
|
| 376 |
+
Average a list of per-step grade_action() rewards and return safe_score.
|
| 377 |
+
|
| 378 |
+
Parameters
|
| 379 |
+
----------
|
| 380 |
+
task_scores : list of raw floats from grade_action() calls.
|
| 381 |
+
|
| 382 |
+
Returns
|
| 383 |
+
-------
|
| 384 |
+
float in (0.01, 0.99) β open-interval contract guaranteed.
|
| 385 |
+
|
| 386 |
+
Edge cases
|
| 387 |
+
----------
|
| 388 |
+
β’ Empty list β safe_score(0) = 0.01
|
| 389 |
+
β’ All perfect β safe_score(~0.968) β 0.968
|
| 390 |
+
"""
|
| 391 |
+
if not task_scores:
|
| 392 |
+
return safe_score(0.0)
|
| 393 |
+
|
| 394 |
+
raw_avg = sum(task_scores) / len(task_scores)
|
| 395 |
+
# Normalise from the per-step range (R_BREACH β¦ R_PERFECT) β (0, 1)
|
| 396 |
+
normalised = (raw_avg - R_BREACH) / (R_PERFECT - R_BREACH)
|
| 397 |
+
return safe_score(max(0.0, min(1.0, normalised)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|