Spaces:
Running
Running
File size: 18,563 Bytes
ae36543 6920aae d378e5d 6920aae d378e5d 6920aae d378e5d ae36543 043d9e1 ae36543 8241eb5 ae36543 043d9e1 ae36543 6920aae e3dfee6 6920aae 043d9e1 6920aae ae36543 8241eb5 043d9e1 ae36543 043d9e1 ae36543 6920aae 043d9e1 6920aae ae36543 043d9e1 ae36543 d378e5d ae36543 d378e5d 043d9e1 d378e5d ae36543 043d9e1 ae36543 6920aae d378e5d 6920aae 043d9e1 6920aae 8241eb5 d378e5d ae36543 d378e5d 043d9e1 d378e5d ae36543 043d9e1 ae36543 d378e5d 043d9e1 d378e5d 043d9e1 d378e5d 6920aae d378e5d 6920aae ae36543 | 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 215 216 217 218 219 220 221 222 223 224 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 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 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 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 | from __future__ import annotations
import unittest
import openenv_test_stubs # noqa: F401
from models import HelpdeskTicketAction, HelpdeskTicketRecord
from server.grader import (
ASSIGNMENT_GROUP_SIMILARITY,
ISSUE_TYPE_SIMILARITY,
PRIORITY_SCORES,
RESOLUTION_ACTION_SIMILARITY,
TASK_WEIGHTS,
grade_action,
)
from vocabulary import ASSIGNMENT_GROUPS, ISSUE_TYPES, PRIORITIES, RESOLUTION_ACTIONS
def _expected_breakdown(task_id: int, **field_scores: float) -> dict[str, float]:
return {field: field_scores[field] for field in TASK_WEIGHTS[task_id]}
def _expected_task_score(task_id: int, **field_scores: float) -> float:
raw_score = sum(
field_scores[field] * TASK_WEIGHTS[task_id][field]
for field in TASK_WEIGHTS[task_id]
)
return max(0.0, min(1.0, raw_score))
def _ticket(
*,
issue_type: str = "billing_license",
priority: str = "high",
assignment_group: str = "license_ops",
resolution_action: str = "fulfill",
) -> HelpdeskTicketRecord:
return HelpdeskTicketRecord(
ticket_id="ticket-test",
title="Test ticket",
requester="user@example.com",
description="Synthetic ticket used for deterministic grader tests.",
issue_type=issue_type,
priority=priority,
assignment_group=assignment_group,
resolution_action=resolution_action,
)
class GraderUnitTests(unittest.TestCase):
def test_task_3_exact_match_scores_one(self) -> None:
ticket = _ticket()
action = HelpdeskTicketAction(
issue_type="billing_license",
priority="high",
assignment_group="license_ops",
resolution_action="fulfill",
)
score, breakdown = grade_action(action, ticket, task_id=3)
self.assertAlmostEqual(score, 1.0)
self.assertEqual(
breakdown,
{
"issue_type": 1.0,
"priority": 1.0,
"assignment_group": 1.0,
"resolution_action": 1.0,
},
)
def test_unknown_task_id_raises(self) -> None:
ticket = _ticket()
action = HelpdeskTicketAction(issue_type="billing_license")
with self.assertRaisesRegex(ValueError, "Unsupported task_id"):
grade_action(action, ticket, task_id=99)
def test_issue_type_partial_credit_only_for_known_similarity_pair(self) -> None:
ticket = _ticket(issue_type="billing_license")
action = HelpdeskTicketAction(issue_type="service_request")
score, breakdown = grade_action(action, ticket, task_id=1)
expected_breakdown = _expected_breakdown(
1,
issue_type=0.4,
priority=0.0,
assignment_group=0.0,
resolution_action=0.0,
)
self.assertEqual(breakdown, expected_breakdown)
self.assertAlmostEqual(
score,
_expected_task_score(
1,
issue_type=0.4,
priority=0.0,
assignment_group=0.0,
resolution_action=0.0,
),
)
def test_issue_type_scoring_matches_declared_similarity_table_exhaustively(self) -> None:
for expected in ISSUE_TYPES:
for predicted in ISSUE_TYPES:
with self.subTest(expected=expected, predicted=predicted):
ticket = _ticket(issue_type=expected)
action = HelpdeskTicketAction(issue_type=predicted)
score, breakdown = grade_action(action, ticket, task_id=1)
raw_expected_score = (
1.0
if predicted == expected
else ISSUE_TYPE_SIMILARITY.get((predicted, expected), 0.0)
)
expected_breakdown = _expected_breakdown(
1,
issue_type=raw_expected_score,
priority=0.0,
assignment_group=0.0,
resolution_action=0.0,
)
self.assertAlmostEqual(
score,
_expected_task_score(
1,
issue_type=raw_expected_score,
priority=0.0,
assignment_group=0.0,
resolution_action=0.0,
),
)
self.assertEqual(breakdown, expected_breakdown)
def test_unrelated_issue_type_gets_zero_not_fuzzy_credit(self) -> None:
ticket = _ticket(issue_type="onboarding")
action = HelpdeskTicketAction(issue_type="spam_phishing")
score, breakdown = grade_action(action, ticket, task_id=1)
self.assertAlmostEqual(score, 0.0)
self.assertEqual(
breakdown,
_expected_breakdown(
1,
issue_type=0.0,
priority=0.0,
assignment_group=0.0,
resolution_action=0.0,
),
)
def test_priority_scoring_uses_defined_proximity_table(self) -> None:
ticket = _ticket(priority="critical")
action = HelpdeskTicketAction(issue_type="billing_license", priority="high")
score, breakdown = grade_action(action, ticket, task_id=2)
self.assertAlmostEqual(breakdown["issue_type"], 1.0)
self.assertAlmostEqual(breakdown["priority"], 0.6)
self.assertAlmostEqual(
score,
_expected_task_score(
2,
issue_type=1.0,
priority=0.6,
assignment_group=0.0,
resolution_action=0.0,
),
)
def test_priority_scoring_matches_declared_table_exhaustively(self) -> None:
for expected in PRIORITIES:
for predicted in PRIORITIES:
with self.subTest(expected=expected, predicted=predicted):
ticket = _ticket(priority=expected)
action = HelpdeskTicketAction(
issue_type="billing_license",
priority=predicted,
)
score, breakdown = grade_action(action, ticket, task_id=2)
priority_score = (
1.0
if predicted == expected
else PRIORITY_SCORES.get((predicted, expected), 0.0)
)
self.assertEqual(
breakdown,
_expected_breakdown(
2,
issue_type=1.0,
priority=priority_score,
assignment_group=0.0,
resolution_action=0.0,
),
)
self.assertAlmostEqual(
score,
_expected_task_score(
2,
issue_type=1.0,
priority=priority_score,
assignment_group=0.0,
resolution_action=0.0,
),
)
def test_task_2_weights_apply_as_documented(self) -> None:
ticket = _ticket(priority="high")
action = HelpdeskTicketAction(issue_type="billing_license", priority="medium")
score, breakdown = grade_action(action, ticket, task_id=2)
self.assertEqual(
breakdown,
_expected_breakdown(
2,
issue_type=1.0,
priority=0.5,
assignment_group=0.0,
resolution_action=0.0,
),
)
self.assertAlmostEqual(
score,
_expected_task_score(
2,
issue_type=1.0,
priority=0.5,
assignment_group=0.0,
resolution_action=0.0,
),
)
def test_assignment_group_partial_credit_uses_declared_similarity_table(self) -> None:
ticket = _ticket()
action = HelpdeskTicketAction(
issue_type="billing_license",
priority="high",
assignment_group="procurement",
resolution_action="fulfill",
)
score, breakdown = grade_action(action, ticket, task_id=3)
self.assertEqual(breakdown["assignment_group"], 0.55)
self.assertAlmostEqual(
score,
_expected_task_score(
3,
issue_type=1.0,
priority=1.0,
assignment_group=0.55,
resolution_action=1.0,
),
)
def test_assignment_group_unrelated_miss_stays_zero(self) -> None:
ticket = _ticket()
action = HelpdeskTicketAction(
issue_type="billing_license",
priority="high",
assignment_group="security_team",
resolution_action="fulfill",
)
score, breakdown = grade_action(action, ticket, task_id=3)
self.assertEqual(breakdown["assignment_group"], 0.0)
self.assertAlmostEqual(
score,
_expected_task_score(
3,
issue_type=1.0,
priority=1.0,
assignment_group=0.0,
resolution_action=1.0,
),
)
def test_task_3_weights_apply_as_documented(self) -> None:
ticket = _ticket(priority="high")
action = HelpdeskTicketAction(
issue_type="billing_license",
priority="medium",
assignment_group="security_team",
resolution_action="fulfill",
)
score, breakdown = grade_action(action, ticket, task_id=3)
self.assertEqual(
breakdown,
_expected_breakdown(
3,
issue_type=1.0,
priority=0.5,
assignment_group=0.0,
resolution_action=1.0,
),
)
self.assertAlmostEqual(
score,
_expected_task_score(
3,
issue_type=1.0,
priority=0.5,
assignment_group=0.0,
resolution_action=1.0,
),
)
def test_alternate_route_can_win_when_primary_route_is_worse(self) -> None:
ticket = HelpdeskTicketRecord(
ticket_id="ticket-alt",
title="Planning ticket",
requester="planner@example.com",
description="Capacity-sensitive routing decision.",
issue_type="service_request",
priority="medium",
assignment_group="procurement",
resolution_action="assign",
alternate_issue_type="billing_license",
alternate_priority="high",
alternate_assignment_group="license_ops",
alternate_resolution_action="fulfill",
alternate_route_score_multiplier=0.85,
)
action = HelpdeskTicketAction(
issue_type="billing_license",
priority="high",
assignment_group="license_ops",
resolution_action="fulfill",
)
score, breakdown = grade_action(action, ticket, task_id=3)
self.assertAlmostEqual(score, 0.85)
self.assertEqual(
breakdown,
{
"issue_type": 0.85,
"priority": 0.85,
"assignment_group": 0.85,
"resolution_action": 0.85,
},
)
def test_resolution_action_partial_credit_uses_declared_similarity_table(self) -> None:
ticket = _ticket()
action = HelpdeskTicketAction(
issue_type="billing_license",
priority="high",
assignment_group="license_ops",
resolution_action="acknowledge",
)
score, breakdown = grade_action(action, ticket, task_id=3)
self.assertEqual(breakdown["resolution_action"], 0.35)
self.assertAlmostEqual(
score,
_expected_task_score(
3,
issue_type=1.0,
priority=1.0,
assignment_group=1.0,
resolution_action=0.35,
),
)
def test_resolution_action_unrelated_miss_stays_zero(self) -> None:
ticket = _ticket()
action = HelpdeskTicketAction(
issue_type="billing_license",
priority="high",
assignment_group="license_ops",
resolution_action="ignore",
)
score, breakdown = grade_action(action, ticket, task_id=3)
self.assertEqual(breakdown["resolution_action"], 0.0)
self.assertAlmostEqual(
score,
_expected_task_score(
3,
issue_type=1.0,
priority=1.0,
assignment_group=1.0,
resolution_action=0.0,
),
)
def test_assignment_group_scoring_matches_declared_similarity_table_exhaustively(self) -> None:
for expected in ASSIGNMENT_GROUPS:
for predicted in ASSIGNMENT_GROUPS:
with self.subTest(expected=expected, predicted=predicted):
ticket = _ticket(assignment_group=expected)
action = HelpdeskTicketAction(
issue_type="billing_license",
priority="high",
assignment_group=predicted,
resolution_action="fulfill",
)
score, breakdown = grade_action(action, ticket, task_id=3)
assignment_group_score = (
1.0
if predicted == expected
else ASSIGNMENT_GROUP_SIMILARITY.get((predicted, expected), 0.0)
)
self.assertEqual(
breakdown,
_expected_breakdown(
3,
issue_type=1.0,
priority=1.0,
assignment_group=assignment_group_score,
resolution_action=1.0,
),
)
self.assertAlmostEqual(
score,
_expected_task_score(
3,
issue_type=1.0,
priority=1.0,
assignment_group=assignment_group_score,
resolution_action=1.0,
),
)
def test_resolution_action_scoring_matches_declared_similarity_table_exhaustively(self) -> None:
for expected in RESOLUTION_ACTIONS:
for predicted in RESOLUTION_ACTIONS:
with self.subTest(expected=expected, predicted=predicted):
ticket = _ticket(resolution_action=expected)
action = HelpdeskTicketAction(
issue_type="billing_license",
priority="high",
assignment_group="license_ops",
resolution_action=predicted,
)
score, breakdown = grade_action(action, ticket, task_id=3)
resolution_action_score = (
1.0
if predicted == expected
else RESOLUTION_ACTION_SIMILARITY.get((predicted, expected), 0.0)
)
self.assertEqual(
breakdown,
_expected_breakdown(
3,
issue_type=1.0,
priority=1.0,
assignment_group=1.0,
resolution_action=resolution_action_score,
),
)
self.assertAlmostEqual(
score,
_expected_task_score(
3,
issue_type=1.0,
priority=1.0,
assignment_group=1.0,
resolution_action=resolution_action_score,
),
)
def test_partial_credit_tables_never_override_exact_match(self) -> None:
for pair, value in ISSUE_TYPE_SIMILARITY.items():
with self.subTest(table="issue_type", pair=pair):
self.assertGreater(value, 0.0)
self.assertLess(value, 1.0)
for pair, value in PRIORITY_SCORES.items():
with self.subTest(table="priority", pair=pair):
self.assertGreater(value, 0.0)
self.assertLess(value, 1.0)
for pair, value in ASSIGNMENT_GROUP_SIMILARITY.items():
with self.subTest(table="assignment_group", pair=pair):
self.assertGreater(value, 0.0)
self.assertLess(value, 1.0)
for pair, value in RESOLUTION_ACTION_SIMILARITY.items():
with self.subTest(table="resolution_action", pair=pair):
self.assertGreater(value, 0.0)
self.assertLess(value, 1.0)
def test_task_weights_sum_to_one_for_each_task(self) -> None:
for task_id, weights in TASK_WEIGHTS.items():
with self.subTest(task_id=task_id):
self.assertAlmostEqual(sum(weights.values()), 1.0)
def test_grade_action_is_deterministic_for_same_inputs(self) -> None:
ticket = _ticket(issue_type="service_request", priority="medium")
action = HelpdeskTicketAction(
issue_type="general_inquiry",
priority="low",
assignment_group="license_ops",
resolution_action="assign",
)
first_score, first_breakdown = grade_action(action, ticket, task_id=3)
second_score, second_breakdown = grade_action(action, ticket, task_id=3)
self.assertEqual(first_score, second_score)
self.assertEqual(first_breakdown, second_breakdown)
if __name__ == "__main__":
unittest.main()
|