-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrader.py
More file actions
214 lines (191 loc) · 8.17 KB
/
grader.py
File metadata and controls
214 lines (191 loc) · 8.17 KB
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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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
184
185
186
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
"""Deterministic public scoring for the honest narrow incident-remediation environment."""
from __future__ import annotations
from typing import Any
from ..models import GraderCheck, GraderReport
MIN_PUBLIC_SCORE = 0.01
MAX_PUBLIC_SCORE = 0.99
def _strict_public_score(score: float) -> float:
return round(min(MAX_PUBLIC_SCORE, max(MIN_PUBLIC_SCORE, score)), 4)
def _service_score(status: str) -> float:
return {
"healthy": 1.0,
"degraded": 0.4,
"crashed": 0.0,
"isolated": 0.2,
}.get(status, 0.0)
class UnifiedIncidentGrader:
"""Deterministic scorer focused on executed effects, not scripted clues.
Hardened schedule (post Track-A headroom patch):
- recovery 0.00 – 0.25
- containment 0.00 – 0.15
- verification 0.00 – 0.20
- impact 0.00 – 0.05
- efficiency 0.00 – 0.05
- speed_bonus 0.00 – 0.10 (positive only when faster than optimal)
- noise_handling 0.00 – 0.05 (penalizes querying noise services)
Scripted deterministic baseline (which matches optimal_ticks exactly and
avoids noise queries) caps at ~0.70. Headroom 0.70 → 0.85 is reachable only
by an agent that (a) is strictly faster than optimal and (b) touches zero
noise services. That's the training target.
"""
def compute_breakdown(
self,
state: dict[str, Any],
scenario: dict[str, Any],
) -> dict[str, float]:
services = state.get("service_health", {})
weights = scenario["critical_service_weights"]
recovery_raw = sum(
weights.get(service, 0.0) * _service_score((services.get(service) or {}).get("status", "crashed"))
for service in weights
)
recovery_score = round(0.25 * recovery_raw, 4)
contained = bool(state.get("containment_applied"))
rollback_target = scenario.get("remediation_recipe", {}).get("rollback_target")
rollback_service_healthy = bool(
rollback_target and (services.get(rollback_target) or {}).get("status") == "healthy"
)
if contained and rollback_service_healthy:
containment_score = 0.15
elif contained:
containment_score = 0.10
else:
containment_score = 0.0
checks = {item.get("name"): bool(item.get("passed")) for item in state.get("checks", [])}
verification_score = 0.0
if checks.get("database_recovery"):
verification_score += 0.08
if checks.get("end_to_end"):
verification_score += 0.12
user_impact = float(state.get("user_impact", 1.0))
impact_score = round(max(0.0, 0.05 * (1.0 - user_impact)), 4)
wasteful_ticks = int(state.get("wasteful_ticks", 0))
efficiency_score = round(max(0.0, 0.05 - (0.005 * wasteful_ticks)), 4)
# speed_bonus: fully earned only if the agent finishes well under optimal_ticks.
optimal_ticks = int(scenario.get("optimal_ticks", 10))
current_tick = int(state.get("current_tick", 0))
incident_resolved = bool(state.get("incident_resolved"))
if incident_resolved and current_tick > 0 and current_tick < optimal_ticks:
speed_bonus = round(0.10 * (optimal_ticks - current_tick) / optimal_ticks, 4)
elif incident_resolved and current_tick == optimal_ticks:
speed_bonus = 0.0
else:
speed_bonus = 0.0
# noise_handling: deduct per query against a noise service, up to the cap of 0.05.
noise_services = set(scenario.get("difficulty_knobs", {}).get("noise_services", []))
noise_queries = int(state.get("noise_queries", 0))
if noise_services:
noise_handling_score = round(max(0.0, 0.05 - 0.015 * noise_queries), 4)
else:
noise_handling_score = 0.0
final_score = _strict_public_score(
recovery_score
+ containment_score
+ verification_score
+ impact_score
+ efficiency_score
+ speed_bonus
+ noise_handling_score
)
return {
"recovery_score": recovery_score,
"containment_score": round(containment_score, 4),
"verification_score": round(verification_score, 4),
"impact_score": impact_score,
"efficiency_score": efficiency_score,
"speed_bonus": speed_bonus,
"noise_handling_score": noise_handling_score,
"final_score": final_score,
}
def build_report(self, state: dict[str, Any], scenario: dict[str, Any]) -> GraderReport:
breakdown = self.compute_breakdown(state, scenario)
checks = {item.get("name"): bool(item.get("passed")) for item in state.get("checks", [])}
passed = bool(
state.get("incident_resolved")
and checks.get("database_recovery")
and checks.get("end_to_end")
)
report_checks = [
GraderCheck(
name="root_cause_removed",
passed=bool(state.get("containment_applied")),
detail=(
"The root cause has been safely contained or removed."
if state.get("containment_applied")
else "The root cause is still active or only partially contained."
),
weight=0.20,
),
GraderCheck(
name="database_recovery",
passed=checks.get("database_recovery", False),
detail=(
"The database recovery check passed."
if checks.get("database_recovery")
else "The database recovery check has not passed yet."
),
weight=0.15,
),
GraderCheck(
name="end_to_end_check",
passed=checks.get("end_to_end", False),
detail=(
"The end-to-end service check passed."
if checks.get("end_to_end")
else "The end-to-end service check has not passed yet."
),
weight=0.20,
),
GraderCheck(
name="critical_services_recovered",
passed=breakdown["recovery_score"] >= 0.20,
detail=(
"Critical-path services are recovered."
if breakdown["recovery_score"] >= 0.20
else "Critical-path services are still degraded or crashed."
),
weight=0.20,
),
GraderCheck(
name="declare_resolved",
passed=bool(state.get("incident_resolved")),
detail=(
"The agent declared the incident resolved after objective checks passed."
if state.get("incident_resolved")
else "The incident has not been safely declared resolved."
),
weight=0.10,
),
GraderCheck(
name="speed_bonus_earned",
passed=breakdown.get("speed_bonus", 0.0) > 0.0,
detail=(
"Resolved faster than optimal_ticks."
if breakdown.get("speed_bonus", 0.0) > 0.0
else "Did not beat optimal tick budget."
),
weight=0.10,
),
GraderCheck(
name="noise_handling",
passed=breakdown.get("noise_handling_score", 0.0) >= 0.035,
detail=(
"Minimal or no queries against noise services."
if breakdown.get("noise_handling_score", 0.0) >= 0.035
else "Wasted queries on noise services."
),
weight=0.05,
),
]
return GraderReport(
scenario_id=scenario["id"],
passed=passed,
score=breakdown["final_score"],
message=(
"Incident diagnosed, remediated, and verified honestly."
if passed
else "Incident is not yet safely resolved."
),
breakdown=breakdown,
checks=report_checks,
)