Spaces:
Sleeping
Sleeping
fix(openenv): align env step rewards with YAML task graders
Browse files- Add tasks/scoring.finalize_task_reward for strict (0,1) scores and non-finite guards
- Route CodeBugEnvironment.step through the same grade() functions as openenv.yaml
- Accept **kwargs on task graders for validator compatibility; make extra1/extra2 proper packages
- Harden inference.py score_action for non-finite intermediate rewards
Made-with: Cursor
- environment.py +21 -30
- inference.py +4 -1
- tasks/easy/grader.py +4 -4
- tasks/extra1/__init__.py +1 -0
- tasks/extra1/grader.py +4 -4
- tasks/extra2/__init__.py +1 -0
- tasks/extra2/grader.py +4 -4
- tasks/hard/grader.py +4 -4
- tasks/medium/grader.py +4 -4
- tasks/scoring.py +19 -0
environment.py
CHANGED
|
@@ -1,6 +1,12 @@
|
|
| 1 |
import uuid
|
| 2 |
from openenv.core.env_server import Environment
|
| 3 |
from models import CodeReviewAction, CodeReviewObservation, CodeReviewState
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
|
| 6 |
class CodeBugEnvironment(Environment):
|
|
@@ -71,6 +77,14 @@ class CodeBugEnvironment(Environment):
|
|
| 71 |
},
|
| 72 |
]
|
| 73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
def __init__(self):
|
| 75 |
self._state = CodeReviewState()
|
| 76 |
|
|
@@ -94,43 +108,20 @@ class CodeBugEnvironment(Environment):
|
|
| 94 |
),
|
| 95 |
)
|
| 96 |
|
| 97 |
-
@staticmethod
|
| 98 |
-
def _clamp_reward(raw: float) -> float:
|
| 99 |
-
"""Clamp to (0, 1) exclusive β required by hackathon task validator."""
|
| 100 |
-
return max(0.01, min(0.99, raw))
|
| 101 |
-
|
| 102 |
def step(self, action: CodeReviewAction, timeout_s=None, **kwargs) -> CodeReviewObservation:
|
| 103 |
"""Grade the agent's action and advance to the next task."""
|
| 104 |
self._state.step_count += 1
|
| 105 |
|
| 106 |
idx = self._state.current_task_idx
|
| 107 |
task = self.TASKS[idx]
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
# Compute raw reward, then clamp to (0, 1) exclusive
|
| 111 |
-
raw_reward = 0.0
|
| 112 |
-
if action.category.strip().lower() == truth["category"]:
|
| 113 |
-
raw_reward += 0.5
|
| 114 |
-
|
| 115 |
-
try:
|
| 116 |
-
sev = int(action.severity)
|
| 117 |
-
sev_diff = abs(sev - truth["severity"])
|
| 118 |
-
except (ValueError, TypeError):
|
| 119 |
-
sev_diff = 99
|
| 120 |
-
|
| 121 |
-
if sev_diff == 0:
|
| 122 |
-
raw_reward += 0.3
|
| 123 |
-
elif sev_diff == 1:
|
| 124 |
-
raw_reward += 0.15
|
| 125 |
-
|
| 126 |
-
if action.line_hint is not None:
|
| 127 |
-
try:
|
| 128 |
-
if int(action.line_hint) == truth["line_hint"]:
|
| 129 |
-
raw_reward += 0.2
|
| 130 |
-
except (ValueError, TypeError):
|
| 131 |
-
pass
|
| 132 |
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
self._state.total_score += reward
|
| 135 |
self._state.current_task_idx += 1
|
| 136 |
|
|
|
|
| 1 |
import uuid
|
| 2 |
from openenv.core.env_server import Environment
|
| 3 |
from models import CodeReviewAction, CodeReviewObservation, CodeReviewState
|
| 4 |
+
from tasks.easy.grader import grade as grade_task_easy_001
|
| 5 |
+
from tasks.medium.grader import grade as grade_task_medium_002
|
| 6 |
+
from tasks.hard.grader import grade as grade_task_hard_003
|
| 7 |
+
from tasks.extra1.grader import grade as grade_task_extra_004
|
| 8 |
+
from tasks.extra2.grader import grade as grade_task_extra_005
|
| 9 |
+
from tasks.scoring import finalize_task_reward
|
| 10 |
|
| 11 |
|
| 12 |
class CodeBugEnvironment(Environment):
|
|
|
|
| 77 |
},
|
| 78 |
]
|
| 79 |
|
| 80 |
+
_TASK_GRADERS = {
|
| 81 |
+
"task_easy_001": grade_task_easy_001,
|
| 82 |
+
"task_medium_002": grade_task_medium_002,
|
| 83 |
+
"task_hard_003": grade_task_hard_003,
|
| 84 |
+
"task_extra_004": grade_task_extra_004,
|
| 85 |
+
"task_extra_005": grade_task_extra_005,
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
def __init__(self):
|
| 89 |
self._state = CodeReviewState()
|
| 90 |
|
|
|
|
| 108 |
),
|
| 109 |
)
|
| 110 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
def step(self, action: CodeReviewAction, timeout_s=None, **kwargs) -> CodeReviewObservation:
|
| 112 |
"""Grade the agent's action and advance to the next task."""
|
| 113 |
self._state.step_count += 1
|
| 114 |
|
| 115 |
idx = self._state.current_task_idx
|
| 116 |
task = self.TASKS[idx]
|
| 117 |
+
task_id = task["id"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
+
grader = self._TASK_GRADERS.get(task_id)
|
| 120 |
+
if grader is None:
|
| 121 |
+
reward = 0.5
|
| 122 |
+
else:
|
| 123 |
+
reward = float(grader(action))
|
| 124 |
+
reward = finalize_task_reward(reward)
|
| 125 |
self._state.total_score += reward
|
| 126 |
self._state.current_task_idx += 1
|
| 127 |
|
inference.py
CHANGED
|
@@ -11,6 +11,7 @@ Strictly follows the Meta PyTorch Hackathon pre-submission checklist:
|
|
| 11 |
import os
|
| 12 |
import json
|
| 13 |
import sys
|
|
|
|
| 14 |
|
| 15 |
from openai import OpenAI
|
| 16 |
|
|
@@ -168,7 +169,9 @@ def score_action(action: dict, truth: dict) -> float:
|
|
| 168 |
pass
|
| 169 |
|
| 170 |
# Clamp to (0.0, 1.0) exclusive: 0.0 β 0.01, 1.0 β 0.99
|
| 171 |
-
|
|
|
|
|
|
|
| 172 |
return round(clamped, 4)
|
| 173 |
|
| 174 |
|
|
|
|
| 11 |
import os
|
| 12 |
import json
|
| 13 |
import sys
|
| 14 |
+
import math
|
| 15 |
|
| 16 |
from openai import OpenAI
|
| 17 |
|
|
|
|
| 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 |
|
tasks/easy/grader.py
CHANGED
|
@@ -2,12 +2,12 @@
|
|
| 2 |
Grader for task_easy_001 β Balance Transfer boundary bug.
|
| 3 |
"""
|
| 4 |
|
|
|
|
|
|
|
| 5 |
TRUTH = {"category": "logic", "severity": 4, "line_hint": 2}
|
| 6 |
|
| 7 |
-
def _clamp(score: float) -> float:
|
| 8 |
-
return max(0.01, min(0.99, score))
|
| 9 |
|
| 10 |
-
def grade(action:
|
| 11 |
reward = 0.0
|
| 12 |
|
| 13 |
# Handle dict or Pydantic model
|
|
@@ -42,4 +42,4 @@ def grade(action: any) -> float:
|
|
| 42 |
except (ValueError, TypeError):
|
| 43 |
pass
|
| 44 |
|
| 45 |
-
return
|
|
|
|
| 2 |
Grader for task_easy_001 β Balance Transfer boundary bug.
|
| 3 |
"""
|
| 4 |
|
| 5 |
+
from tasks.scoring import finalize_task_reward
|
| 6 |
+
|
| 7 |
TRUTH = {"category": "logic", "severity": 4, "line_hint": 2}
|
| 8 |
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
def grade(action: object, **kwargs) -> float:
|
| 11 |
reward = 0.0
|
| 12 |
|
| 13 |
# Handle dict or Pydantic model
|
|
|
|
| 42 |
except (ValueError, TypeError):
|
| 43 |
pass
|
| 44 |
|
| 45 |
+
return finalize_task_reward(reward)
|
tasks/extra1/__init__.py
CHANGED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
# Subpackage for task_extra_004 grader (explicit package for all import paths).
|
tasks/extra1/grader.py
CHANGED
|
@@ -2,12 +2,12 @@
|
|
| 2 |
Grader for task_extra_004 β Hardcoded API key.
|
| 3 |
"""
|
| 4 |
|
|
|
|
|
|
|
| 5 |
TRUTH = {"category": "security", "severity": 5, "line_hint": 1}
|
| 6 |
|
| 7 |
-
def _clamp(score: float) -> float:
|
| 8 |
-
return max(0.01, min(0.99, score))
|
| 9 |
|
| 10 |
-
def grade(action:
|
| 11 |
reward = 0.0
|
| 12 |
def get_attr(obj, attr, default=None):
|
| 13 |
if isinstance(obj, dict): return obj.get(attr, default)
|
|
@@ -30,4 +30,4 @@ def grade(action: any) -> float:
|
|
| 30 |
if int(lh) == TRUTH["line_hint"]: reward += 0.2
|
| 31 |
except: pass
|
| 32 |
|
| 33 |
-
return
|
|
|
|
| 2 |
Grader for task_extra_004 β Hardcoded API key.
|
| 3 |
"""
|
| 4 |
|
| 5 |
+
from tasks.scoring import finalize_task_reward
|
| 6 |
+
|
| 7 |
TRUTH = {"category": "security", "severity": 5, "line_hint": 1}
|
| 8 |
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
def grade(action: object, **kwargs) -> float:
|
| 11 |
reward = 0.0
|
| 12 |
def get_attr(obj, attr, default=None):
|
| 13 |
if isinstance(obj, dict): return obj.get(attr, default)
|
|
|
|
| 30 |
if int(lh) == TRUTH["line_hint"]: reward += 0.2
|
| 31 |
except: pass
|
| 32 |
|
| 33 |
+
return finalize_task_reward(reward)
|
tasks/extra2/__init__.py
CHANGED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
# Subpackage for task_extra_005 grader (explicit package for all import paths).
|
tasks/extra2/grader.py
CHANGED
|
@@ -2,12 +2,12 @@
|
|
| 2 |
Grader for task_extra_005 β Division by zero.
|
| 3 |
"""
|
| 4 |
|
|
|
|
|
|
|
| 5 |
TRUTH = {"category": "logic", "severity": 3, "line_hint": 3}
|
| 6 |
|
| 7 |
-
def _clamp(score: float) -> float:
|
| 8 |
-
return max(0.01, min(0.99, score))
|
| 9 |
|
| 10 |
-
def grade(action:
|
| 11 |
reward = 0.0
|
| 12 |
def get_attr(obj, attr, default=None):
|
| 13 |
if isinstance(obj, dict): return obj.get(attr, default)
|
|
@@ -30,4 +30,4 @@ def grade(action: any) -> float:
|
|
| 30 |
if int(lh) == TRUTH["line_hint"]: reward += 0.2
|
| 31 |
except: pass
|
| 32 |
|
| 33 |
-
return
|
|
|
|
| 2 |
Grader for task_extra_005 β Division by zero.
|
| 3 |
"""
|
| 4 |
|
| 5 |
+
from tasks.scoring import finalize_task_reward
|
| 6 |
+
|
| 7 |
TRUTH = {"category": "logic", "severity": 3, "line_hint": 3}
|
| 8 |
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
def grade(action: object, **kwargs) -> float:
|
| 11 |
reward = 0.0
|
| 12 |
def get_attr(obj, attr, default=None):
|
| 13 |
if isinstance(obj, dict): return obj.get(attr, default)
|
|
|
|
| 30 |
if int(lh) == TRUTH["line_hint"]: reward += 0.2
|
| 31 |
except: pass
|
| 32 |
|
| 33 |
+
return finalize_task_reward(reward)
|
tasks/hard/grader.py
CHANGED
|
@@ -2,12 +2,12 @@
|
|
| 2 |
Grader for task_hard_003 β SQL injection detection.
|
| 3 |
"""
|
| 4 |
|
|
|
|
|
|
|
| 5 |
TRUTH = {"category": "security", "severity": 5, "line_hint": 2}
|
| 6 |
|
| 7 |
-
def _clamp(score: float) -> float:
|
| 8 |
-
return max(0.01, min(0.99, score))
|
| 9 |
|
| 10 |
-
def grade(action:
|
| 11 |
reward = 0.0
|
| 12 |
|
| 13 |
def get_attr(obj, attr, default=None):
|
|
@@ -38,4 +38,4 @@ def grade(action: any) -> float:
|
|
| 38 |
except (ValueError, TypeError):
|
| 39 |
pass
|
| 40 |
|
| 41 |
-
return
|
|
|
|
| 2 |
Grader for task_hard_003 β SQL injection detection.
|
| 3 |
"""
|
| 4 |
|
| 5 |
+
from tasks.scoring import finalize_task_reward
|
| 6 |
+
|
| 7 |
TRUTH = {"category": "security", "severity": 5, "line_hint": 2}
|
| 8 |
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
def grade(action: object, **kwargs) -> float:
|
| 11 |
reward = 0.0
|
| 12 |
|
| 13 |
def get_attr(obj, attr, default=None):
|
|
|
|
| 38 |
except (ValueError, TypeError):
|
| 39 |
pass
|
| 40 |
|
| 41 |
+
return finalize_task_reward(reward)
|
tasks/medium/grader.py
CHANGED
|
@@ -2,12 +2,12 @@
|
|
| 2 |
Grader for task_medium_002 β find_max off-by-one bug.
|
| 3 |
"""
|
| 4 |
|
|
|
|
|
|
|
| 5 |
TRUTH = {"category": "logic", "severity": 3, "line_hint": 5}
|
| 6 |
|
| 7 |
-
def _clamp(score: float) -> float:
|
| 8 |
-
return max(0.01, min(0.99, score))
|
| 9 |
|
| 10 |
-
def grade(action:
|
| 11 |
reward = 0.0
|
| 12 |
|
| 13 |
def get_attr(obj, attr, default=None):
|
|
@@ -38,4 +38,4 @@ def grade(action: any) -> float:
|
|
| 38 |
except (ValueError, TypeError):
|
| 39 |
pass
|
| 40 |
|
| 41 |
-
return
|
|
|
|
| 2 |
Grader for task_medium_002 β find_max off-by-one bug.
|
| 3 |
"""
|
| 4 |
|
| 5 |
+
from tasks.scoring import finalize_task_reward
|
| 6 |
+
|
| 7 |
TRUTH = {"category": "logic", "severity": 3, "line_hint": 5}
|
| 8 |
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
def grade(action: object, **kwargs) -> float:
|
| 11 |
reward = 0.0
|
| 12 |
|
| 13 |
def get_attr(obj, attr, default=None):
|
|
|
|
| 38 |
except (ValueError, TypeError):
|
| 39 |
pass
|
| 40 |
|
| 41 |
+
return finalize_task_reward(reward)
|
tasks/scoring.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Shared task reward shaping for OpenEnv validation (scores must lie strictly in (0, 1))."""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
import math
|
| 6 |
+
from numbers import Real
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def finalize_task_reward(raw: float) -> float:
|
| 10 |
+
"""
|
| 11 |
+
Return a finite float strictly between 0 and 1.
|
| 12 |
+
OpenEnv task validators reject 0.0, 1.0, NaN, and inf.
|
| 13 |
+
"""
|
| 14 |
+
if not isinstance(raw, Real):
|
| 15 |
+
return 0.5
|
| 16 |
+
x = float(raw)
|
| 17 |
+
if not math.isfinite(x):
|
| 18 |
+
return 0.5
|
| 19 |
+
return min(0.99, max(0.01, x))
|