padmapriyagosakan commited on
Commit
fb34eca
·
1 Parent(s): 86b098a

feat: investigation required for full credit on hard/critical + replay seed support

Browse files

- grader.py: apply SKIP_INV_CORRECT_MULTIPLIER (0.80) to correct terminals on
hard/critical tasks that skip all investigation sub-actions; wrong terminals
retain the existing 0.50 multiplier; oracle-without-investigation = 0.879,
oracle-with-investigation = 1.0 (clamped)
- server/app.py: /replay accepts optional seed param; when provided, creates a
fresh PayOpsEnvironment with that seed and grades against jittered tasks,
preventing memorisation of canonical task-action mapping
- run_tests.sh: M-01 now includes required investigation sub-actions before each
hard/critical terminal (still scores 1.0 due to bonuses); N-02 updated to
assert total_reward < max_possible (correct penalty behavior); I-01/I-02
updated to reflect ~0.88 score for correct-but-no-investigation episodes

All 104 tests pass.

Files changed (3) hide show
  1. grader.py +19 -17
  2. run_tests.sh +35 -12
  3. server/app.py +13 -3
grader.py CHANGED
@@ -16,9 +16,11 @@ Terminal action credit is now split:
16
 
17
  Skip-investigation penalty (hard / critical tasks only):
18
  Agent issued zero investigation sub-actions on a task that has
19
- requires_investigation AND got the terminal action wrong →
20
- partial credit is multiplied by 0.50. Correct terminal actions
21
- are NEVER penalised by this rule.
 
 
22
 
23
  Investigation sub-action bonuses (per eligible, first use only):
24
  Used one of task.requires_investigation → +0.15
@@ -67,9 +69,11 @@ CONFIDENCE_CORRECT_BONUS = 0.10
67
  CONFIDENCE_WRONG_PENALTY = -0.10
68
  REGULATORY_BONUS = 0.20
69
  BUDGET_OVERSPEND_PENALTY = 0.10
70
- # Skip-investigation penalty: wrong terminal on hard/critical with no investigation
71
- # cuts partial credit in half. Correct terminal actions are NEVER penalised.
72
- SKIP_INVESTIGATION_MULTIPLIER = 0.50
 
 
73
 
74
  DIFFICULTY_WEIGHT: Dict[str, float] = {
75
  "easy": 1.0,
@@ -164,18 +168,16 @@ def _grade_single_task(
164
  correct = terminal_action == task.correct_action
165
 
166
  # ── skip-investigation penalty ───────────────────────────────────────────
167
- # If the agent issued NO investigation sub-actions on a hard/critical task
168
- # that explicitly requires them, AND the terminal action is wrong, halve the
169
- # partial credit. Correct terminal actions are never penalised (preserves
170
- # test I-01 where all-correct + no-investigation still scores 1.0).
171
  requires_inv = getattr(task, "requires_investigation", set())
172
- if (
173
- not correct
174
- and requires_inv
175
- and not investigation_actions
176
- and task.difficulty in ("hard", "critical")
177
- ):
178
- base = base * SKIP_INVESTIGATION_MULTIPLIER
179
 
180
  # ── investigation trajectory bonus & time penalty ────────────────────────
181
  inv_bonus = 0.0
 
16
 
17
  Skip-investigation penalty (hard / critical tasks only):
18
  Agent issued zero investigation sub-actions on a task that has
19
+ requires_investigation:
20
+ • Wrong terminal action → credit × 0.50
21
+ • Correct terminal action → credit × 0.80
22
+ Correct actions that skip investigation still earn partial credit,
23
+ but the full reward requires proper investigation first.
24
 
25
  Investigation sub-action bonuses (per eligible, first use only):
26
  Used one of task.requires_investigation → +0.15
 
69
  CONFIDENCE_WRONG_PENALTY = -0.10
70
  REGULATORY_BONUS = 0.20
71
  BUDGET_OVERSPEND_PENALTY = 0.10
72
+ # Skip-investigation penalty for hard/critical tasks with requires_investigation.
73
+ # Applied when the agent issued ZERO investigation sub-actions for that task.
74
+ # Wrong terminal: halved. Correct terminal: 20% reduction (still well above minimum).
75
+ SKIP_INVESTIGATION_MULTIPLIER = 0.50 # applied to wrong terminals
76
+ SKIP_INV_CORRECT_MULTIPLIER = 0.80 # applied to correct terminals
77
 
78
  DIFFICULTY_WEIGHT: Dict[str, float] = {
79
  "easy": 1.0,
 
168
  correct = terminal_action == task.correct_action
169
 
170
  # ── skip-investigation penalty ───────────────────────────────────────────
171
+ # Hard/critical tasks with requires_investigation penalise agents that skip
172
+ # all investigation sub-actions before making the terminal call.
173
+ # Wrong terminal → halve the credit (existing behaviour).
174
+ # Correct terminal → 20% reduction; full reward requires investigation first.
175
  requires_inv = getattr(task, "requires_investigation", set())
176
+ if requires_inv and not investigation_actions and task.difficulty in ("hard", "critical"):
177
+ if not correct:
178
+ base = base * SKIP_INVESTIGATION_MULTIPLIER
179
+ else:
180
+ base = base * SKIP_INV_CORRECT_MULTIPLIER
 
 
181
 
182
  # ── investigation trajectory bonus & time penalty ────────────────────────
183
  inv_bonus = 0.0
run_tests.sh CHANGED
@@ -303,8 +303,8 @@ REPLAY_PERFECT='{"actions":["approve","reject","approve","flag",
303
  R=$(curl -s -X POST $BASE/replay \
304
  -H "Content-Type: application/json" \
305
  -d "$REPLAY_PERFECT")
306
- check "I-01 Replay 20 correct actions → score=1.0" "$R" '"normalised_score":1.0'
307
- check "I-02 Replay → passed=true" "$R" '"passed":true'
308
  check "I-03 Replay → budget_spent=0.0 (no inv actions)" "$R" '"budget_spent":0.0'
309
 
310
  # I-04 Replay with investigation actions included
@@ -408,7 +408,7 @@ check "L-06 GET /leaderboard → budget_spent in entry" "$LB" '"budget_spent":'
408
  # ═══════════════════════════════════════════════════════════
409
  # GROUP M — Perfect episode score
410
  # ═══════════════════════════════════════════════════════════
411
- section "GROUP M — Perfect episode (all 20 correct, no investigation cost)"
412
 
413
  reset
414
  BASE=$BASE python3 - <<'PYEOF'
@@ -424,16 +424,39 @@ def post(path, body=None):
424
  with urllib.request.urlopen(req, context=_ssl) as r: return json.loads(r.read())
425
 
426
  post("/reset", {"seed": 0})
427
- actions = [
 
428
  ("approve","TXN-E001"),("reject","TXN-E002"),("approve","TXN-E003"),("flag","TXN-E004"),
429
  ("escalate","TXN-M001"),("hold","TXN-M002"),("flag","TXN-M003"),("flag","TXN-M004"),
430
  ("hold","TXN-M005"),("escalate","TXN-M006"),
431
- ("escalate","TXN-H001"),("reject","TXN-H002"),("reject","TXN-H003"),("approve","TXN-H004"),
432
- ("escalate","TXN-H005"),("flag","TXN-H006"),
433
- ("approve","TXN-C001"),("reject","TXN-C002"),("escalate","TXN-C003"),("reject","TXN-C004"),
434
- ]
435
- for action, txn in actions:
436
  post("/step", {"action_type": action, "transaction_id": txn})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
437
  PYEOF
438
 
439
  GRADER="$(curl -s $BASE/grader)"
@@ -457,12 +480,12 @@ FULL=$(curl -s -X POST $BASE/replay \
457
  -d "$REPLAY_CRIT")
458
  check "N-01 Full correct replay → max_possible_reward includes weights" \
459
  "$FULL" '"max_possible_reward":2[0-9]\.'
460
- check "N-02 Full correct → total_reward equals max_possible" \
461
  "$(echo "$FULL" | python3 -c "
462
  import sys,json,re
463
  d=json.load(sys.stdin)
464
- print('EQUAL' if abs(d['total_reward']-d['max_possible_reward'])<0.01 else 'DIFF')
465
- ")" "EQUAL"
466
 
467
  # ═══════════════════════════════════════════════════════════
468
  # GROUP O — Budget mechanics
 
303
  R=$(curl -s -X POST $BASE/replay \
304
  -H "Content-Type: application/json" \
305
  -d "$REPLAY_PERFECT")
306
+ check "I-01 Replay correct terminals (no investigation) → score<1.0 (hard/critical penalised)" "$R" '"normalised_score":0\.[7-9]'
307
+ check "I-02 Replay → passed=true (score>0.5 despite penalty)" "$R" '"passed":true'
308
  check "I-03 Replay → budget_spent=0.0 (no inv actions)" "$R" '"budget_spent":0.0'
309
 
310
  # I-04 Replay with investigation actions included
 
408
  # ═══════════════════════════════════════════════════════════
409
  # GROUP M — Perfect episode score
410
  # ═══════════════════════════════════════════════════════════
411
+ section "GROUP M — Perfect episode (all 20 correct + investigation on hard/critical)"
412
 
413
  reset
414
  BASE=$BASE python3 - <<'PYEOF'
 
424
  with urllib.request.urlopen(req, context=_ssl) as r: return json.loads(r.read())
425
 
426
  post("/reset", {"seed": 0})
427
+ # Easy + medium: terminal only
428
+ for action, txn in [
429
  ("approve","TXN-E001"),("reject","TXN-E002"),("approve","TXN-E003"),("flag","TXN-E004"),
430
  ("escalate","TXN-M001"),("hold","TXN-M002"),("flag","TXN-M003"),("flag","TXN-M004"),
431
  ("hold","TXN-M005"),("escalate","TXN-M006"),
432
+ ]:
 
 
 
 
433
  post("/step", {"action_type": action, "transaction_id": txn})
434
+ # Hard tasks: one required investigation sub-action before each terminal
435
+ post("/step", {"action_type": "inspect", "transaction_id": "TXN-H001"})
436
+ post("/step", {"action_type": "escalate", "transaction_id": "TXN-H001"})
437
+ post("/step", {"action_type": "contact_sender", "transaction_id": "TXN-H002"})
438
+ post("/step", {"action_type": "reject", "transaction_id": "TXN-H002"})
439
+ post("/step", {"action_type": "inspect", "transaction_id": "TXN-H003"})
440
+ post("/step", {"action_type": "reject", "transaction_id": "TXN-H003"})
441
+ post("/step", {"action_type": "inspect", "transaction_id": "TXN-H004"})
442
+ post("/step", {"action_type": "approve", "transaction_id": "TXN-H004"})
443
+ post("/step", {"action_type": "inspect", "transaction_id": "TXN-H005"})
444
+ post("/step", {"action_type": "contact_sender", "transaction_id": "TXN-H005"})
445
+ post("/step", {"action_type": "escalate", "transaction_id": "TXN-H005"})
446
+ post("/step", {"action_type": "inspect", "transaction_id": "TXN-H006"})
447
+ post("/step", {"action_type": "flag", "transaction_id": "TXN-H006"})
448
+ # Critical tasks: required investigation sub-actions before each terminal
449
+ post("/step", {"action_type": "inspect", "transaction_id": "TXN-C001"})
450
+ post("/step", {"action_type": "request_docs", "transaction_id": "TXN-C001"})
451
+ post("/step", {"action_type": "approve", "transaction_id": "TXN-C001"})
452
+ post("/step", {"action_type": "inspect", "transaction_id": "TXN-C002"})
453
+ post("/step", {"action_type": "reject", "transaction_id": "TXN-C002"})
454
+ post("/step", {"action_type": "request_docs", "transaction_id": "TXN-C003"})
455
+ post("/step", {"action_type": "inspect", "transaction_id": "TXN-C003"})
456
+ post("/step", {"action_type": "escalate", "transaction_id": "TXN-C003"})
457
+ post("/step", {"action_type": "inspect", "transaction_id": "TXN-C004"})
458
+ post("/step", {"action_type": "contact_sender", "transaction_id": "TXN-C004"})
459
+ post("/step", {"action_type": "reject", "transaction_id": "TXN-C004"})
460
  PYEOF
461
 
462
  GRADER="$(curl -s $BASE/grader)"
 
480
  -d "$REPLAY_CRIT")
481
  check "N-01 Full correct replay → max_possible_reward includes weights" \
482
  "$FULL" '"max_possible_reward":2[0-9]\.'
483
+ check "N-02 Correct-but-no-investigation → total_reward below max (penalty applied)" \
484
  "$(echo "$FULL" | python3 -c "
485
  import sys,json,re
486
  d=json.load(sys.stdin)
487
+ print('DIFF' if d['total_reward'] < d['max_possible_reward'] - 0.01 else 'EQUAL')
488
+ ")" "DIFF"
489
 
490
  # ═══════════════════════════════════════════════════════════
491
  # GROUP O — Budget mechanics
server/app.py CHANGED
@@ -134,6 +134,7 @@ class BaselineResult(BaseModel):
134
  class ReplayRequest(BaseModel):
135
  actions: List[str]
136
  confidences: Optional[List[Optional[float]]] = None
 
137
 
138
 
139
  # ---------------------------------------------------------------------------
@@ -345,10 +346,12 @@ async def analytics():
345
  @app.post("/replay", summary="Grade a supplied action sequence")
346
  async def replay(request: ReplayRequest):
347
  """
348
- Grade a supplied list of actions against the full task bank without
349
  modifying the current environment state.
350
 
351
- Useful for offline evaluation and leaderboard submissions.
 
 
352
  """
353
  actions = [a.lower() for a in request.actions]
354
  invalid = [a for a in actions if a not in VALID_ACTIONS]
@@ -358,8 +361,15 @@ async def replay(request: ReplayRequest):
358
  detail=f"Invalid action(s): {invalid}. Valid: {sorted(VALID_ACTIONS)}",
359
  )
360
 
 
 
 
 
 
 
 
361
  confs = request.confidences or [None] * len(actions)
362
- result = grade_episode(actions, list(TASKS), confs)
363
  return {
364
  "total_reward": result.total_reward,
365
  "max_possible_reward": result.max_possible_reward,
 
134
  class ReplayRequest(BaseModel):
135
  actions: List[str]
136
  confidences: Optional[List[Optional[float]]] = None
137
+ seed: Optional[int] = None
138
 
139
 
140
  # ---------------------------------------------------------------------------
 
346
  @app.post("/replay", summary="Grade a supplied action sequence")
347
  async def replay(request: ReplayRequest):
348
  """
349
+ Grade a supplied list of actions against the task bank without
350
  modifying the current environment state.
351
 
352
+ Pass ``seed`` to grade against a specific jittered task set (matching a
353
+ live episode seeded with the same value). Omitting ``seed`` grades
354
+ against the canonical un-jittered tasks for offline baseline comparisons.
355
  """
356
  actions = [a.lower() for a in request.actions]
357
  invalid = [a for a in actions if a not in VALID_ACTIONS]
 
361
  detail=f"Invalid action(s): {invalid}. Valid: {sorted(VALID_ACTIONS)}",
362
  )
363
 
364
+ if request.seed is not None:
365
+ _replay_env = PayOpsEnvironment()
366
+ await _replay_env.reset_async(seed=request.seed)
367
+ task_list = list(_replay_env._tasks)
368
+ else:
369
+ task_list = list(TASKS)
370
+
371
  confs = request.confidences or [None] * len(actions)
372
+ result = grade_episode(actions, task_list, confs)
373
  return {
374
  "total_reward": result.total_reward,
375
  "max_possible_reward": result.max_possible_reward,