vinith17 commited on
Commit
ee58e44
Β·
1 Parent(s): 854a163

fix(validation): match inference [STEP] rewards to openenv.yaml graders

Browse files

- Remove score_action linear remap; call the same grade() as each task manifest
- Extend openenv.yaml with spec_version, app, port, type, runtime for HF/OpenEnv CLI

Made-with: Cursor

Files changed (2) hide show
  1. inference.py +19 -39
  2. openenv.yaml +7 -0
inference.py CHANGED
@@ -11,9 +11,13 @@ Strictly follows the Meta PyTorch Hackathon pre-submission checklist:
11
  import os
12
  import json
13
  import sys
14
- import math
15
 
16
  from openai import OpenAI
 
 
 
 
 
17
 
18
  # ─────────────────────────────────────────────────────────────
19
  # Environment variables (checklist requirement)
@@ -136,43 +140,20 @@ def call_llm(code: str, difficulty: str) -> dict:
136
  return json.loads(raw)
137
 
138
 
139
- def score_action(action: dict, truth: dict) -> float:
140
- """
141
- Score the agent's action against ground truth.
142
- Returns a float strictly between 0.0 and 1.0 (exclusive).
143
- """
144
- reward = 0.0
145
-
146
- # Category match β†’ 0.5
147
- if str(action.get("category", "")).lower() == truth["category"]:
148
- reward += 0.5
149
-
150
- # Severity accuracy β†’ up to 0.3
151
- try:
152
- sev = int(action.get("severity", 0))
153
- sev_diff = abs(sev - truth["severity"])
154
- except:
155
- sev_diff = 99
156
-
157
- if sev_diff == 0:
158
- reward += 0.3
159
- elif sev_diff == 1:
160
- reward += 0.15
161
-
162
- # Line hint match β†’ 0.2
163
- lh = action.get("line_hint")
164
- if lh is not None:
165
- try:
166
- if int(lh) == truth["line_hint"]:
167
- reward += 0.2
168
- except:
169
- pass
170
 
171
- # Clamp to (0.0, 1.0) exclusive: 0.0 β†’ 0.01, 1.0 β†’ 0.99
172
- if not math.isfinite(reward):
173
- reward = 0.0
174
- clamped = 0.01 + (min(max(reward, 0.0), 1.0) * 0.98)
175
- return round(clamped, 4)
176
 
177
 
178
  def run_inference():
@@ -185,7 +166,6 @@ def run_inference():
185
  task_id = task["id"]
186
  code = task["code"]
187
  difficulty = task["difficulty"]
188
- truth = task["truth"]
189
 
190
  # ── [STEP] send action ─────────────────────────────────
191
  try:
@@ -193,7 +173,7 @@ def run_inference():
193
  except Exception as exc:
194
  action = {"category": "approve", "severity": 1, "line_hint": None, "comment": str(exc)}
195
 
196
- reward = score_action(action, truth)
197
  total_reward += reward
198
 
199
  print(
 
11
  import os
12
  import json
13
  import sys
 
14
 
15
  from openai import OpenAI
16
+ from tasks.easy.grader import grade as grade_task_easy_001
17
+ from tasks.medium.grader import grade as grade_task_medium_002
18
+ from tasks.hard.grader import grade as grade_task_hard_003
19
+ from tasks.extra1.grader import grade as grade_task_extra_004
20
+ from tasks.extra2.grader import grade as grade_task_extra_005
21
 
22
  # ─────────────────────────────────────────────────────────────
23
  # Environment variables (checklist requirement)
 
140
  return json.loads(raw)
141
 
142
 
143
+ # Same callables as openenv.yaml `grader:` entries β€” validators compare [STEP] rewards to these.
144
+ _TASK_GRADERS = {
145
+ "task_easy_001": grade_task_easy_001,
146
+ "task_medium_002": grade_task_medium_002,
147
+ "task_hard_003": grade_task_hard_003,
148
+ "task_extra_004": grade_task_extra_004,
149
+ "task_extra_005": grade_task_extra_005,
150
+ }
151
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
152
 
153
+ def grade_action_for_task(task_id: str, action: dict) -> float:
154
+ """Delegate to the YAML-configured grader so stdout matches manifest grading exactly."""
155
+ grader = _TASK_GRADERS[task_id]
156
+ return round(float(grader(action)), 6)
 
157
 
158
 
159
  def run_inference():
 
166
  task_id = task["id"]
167
  code = task["code"]
168
  difficulty = task["difficulty"]
 
169
 
170
  # ── [STEP] send action ─────────────────────────────────
171
  try:
 
173
  except Exception as exc:
174
  action = {"category": "approve", "severity": 1, "line_hint": None, "comment": str(exc)}
175
 
176
+ reward = grade_action_for_task(task_id, action)
177
  total_reward += reward
178
 
179
  print(
openenv.yaml CHANGED
@@ -1,3 +1,10 @@
 
 
 
 
 
 
 
1
  name: "CodeBug Review Environment"
2
  version: "1.0.0"
3
  description: "RL environment where an AI agent reviews code snippets to identify bugs. Five tasks covering logic and security. Reward strictly in (0,1) per step."
 
1
+ # Modern HF / OpenEnv manifest fields (see openenv-cli templates) + legacy keys for older tooling
2
+ spec_version: 1
3
+ type: space
4
+ runtime: fastapi
5
+ app: "main:app"
6
+ port: 7860
7
+
8
  name: "CodeBug Review Environment"
9
  version: "1.0.0"
10
  description: "RL environment where an AI agent reviews code snippets to identify bugs. Five tasks covering logic and security. Reward strictly in (0,1) per step."