Update grader.py
Browse files
grader.py
CHANGED
|
@@ -1,142 +1,148 @@
|
|
| 1 |
-
# grader.py – Production‑grade, continuous reward, exploit‑aware
|
| 2 |
-
import ast
|
| 3 |
-
import subprocess
|
| 4 |
-
import tempfile
|
| 5 |
-
import os
|
| 6 |
-
import re
|
| 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 |
return 0.0
|
|
|
|
| 1 |
+
# grader.py – Production‑grade, continuous reward, exploit‑aware
|
| 2 |
+
import ast
|
| 3 |
+
import subprocess
|
| 4 |
+
import tempfile
|
| 5 |
+
import os
|
| 6 |
+
import re
|
| 7 |
+
import sys
|
| 8 |
+
import json
|
| 9 |
+
from dataclasses import dataclass
|
| 10 |
+
from typing import Optional
|
| 11 |
+
|
| 12 |
+
@dataclass
|
| 13 |
+
class RigorousGrader:
|
| 14 |
+
bug_id: str
|
| 15 |
+
oracle_code: Optional[str] = None
|
| 16 |
+
|
| 17 |
+
def grade_fix(self, proposed_fix: str) -> float:
|
| 18 |
+
"""Returns a smooth reward in [0,1]."""
|
| 19 |
+
# Syntax check
|
| 20 |
+
try:
|
| 21 |
+
ast.parse(proposed_fix)
|
| 22 |
+
except SyntaxError:
|
| 23 |
+
return 0.0
|
| 24 |
+
|
| 25 |
+
# Exploit detection (optional)
|
| 26 |
+
if self._is_exploit(proposed_fix):
|
| 27 |
+
return 0.0
|
| 28 |
+
|
| 29 |
+
# Continuous test score
|
| 30 |
+
test_score = self._run_continuous_tests(proposed_fix)
|
| 31 |
+
|
| 32 |
+
# Lint score
|
| 33 |
+
lint_score = self._get_lint_score(proposed_fix)
|
| 34 |
+
|
| 35 |
+
# Oracle similarity
|
| 36 |
+
oracle_score = self._ast_similarity(proposed_fix) if self.oracle_code else 0.0
|
| 37 |
+
|
| 38 |
+
# Weighted combination
|
| 39 |
+
final = (0.5 * test_score) + (0.3 * lint_score) + (0.2 * oracle_score)
|
| 40 |
+
return max(0.0, min(1.0, final))
|
| 41 |
+
|
| 42 |
+
def _run_continuous_tests(self, code: str) -> float:
|
| 43 |
+
"""Proportion of passed test cases."""
|
| 44 |
+
test_cases = self._get_test_cases()
|
| 45 |
+
if not test_cases:
|
| 46 |
+
return 0.0
|
| 47 |
+
passed = 0
|
| 48 |
+
for test_input, expected in test_cases:
|
| 49 |
+
if self._run_single_test(code, test_input, expected):
|
| 50 |
+
passed += 1
|
| 51 |
+
return passed / len(test_cases)
|
| 52 |
+
|
| 53 |
+
def _get_test_cases(self) -> list:
|
| 54 |
+
"""Define multiple test cases per bug type."""
|
| 55 |
+
if self.bug_id == "null_check":
|
| 56 |
+
return [
|
| 57 |
+
({"users": {"alice": "Alice"}, "id": "bob"}, None),
|
| 58 |
+
({"users": {"alice": "Alice"}, "id": "alice"}, "Alice"),
|
| 59 |
+
]
|
| 60 |
+
elif self.bug_id == "off_by_one":
|
| 61 |
+
return [
|
| 62 |
+
([1, 2, 3, 4], 4),
|
| 63 |
+
([], 0),
|
| 64 |
+
]
|
| 65 |
+
# Extend for other bugs …
|
| 66 |
+
return []
|
| 67 |
+
|
| 68 |
+
def _run_single_test(self, code: str, test_input, expected) -> bool:
|
| 69 |
+
"""Execute the agent's code with test_input and compare to expected."""
|
| 70 |
+
harness = f"""
|
| 71 |
+
import json
|
| 72 |
+
{code}
|
| 73 |
+
try:
|
| 74 |
+
result = fix({json.dumps(test_input)})
|
| 75 |
+
print(json.dumps({{"ok": True, "result": result}}))
|
| 76 |
+
except Exception as e:
|
| 77 |
+
print(json.dumps({{"ok": False, "error": str(e)}}))
|
| 78 |
+
"""
|
| 79 |
+
with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f:
|
| 80 |
+
f.write(harness)
|
| 81 |
+
tmp_path = f.name
|
| 82 |
+
try:
|
| 83 |
+
result = subprocess.run(
|
| 84 |
+
[sys.executable, tmp_path],
|
| 85 |
+
capture_output=True, text=True, timeout=5
|
| 86 |
+
)
|
| 87 |
+
data = json.loads(result.stdout.strip())
|
| 88 |
+
if data.get("ok") and data["result"] == expected:
|
| 89 |
+
return True
|
| 90 |
+
return False
|
| 91 |
+
except (json.JSONDecodeError, subprocess.TimeoutExpired, Exception):
|
| 92 |
+
return False
|
| 93 |
+
finally:
|
| 94 |
+
try:
|
| 95 |
+
os.unlink(tmp_path)
|
| 96 |
+
except:
|
| 97 |
+
pass
|
| 98 |
+
|
| 99 |
+
def _is_exploit(self, code: str) -> bool:
|
| 100 |
+
"""Detect hardcoded returns or trivial bypasses."""
|
| 101 |
+
lower = code.lower()
|
| 102 |
+
if "return 0" in lower and "if" not in lower:
|
| 103 |
+
return True
|
| 104 |
+
if code.strip() == "":
|
| 105 |
+
return True
|
| 106 |
+
return False
|
| 107 |
+
|
| 108 |
+
def _get_lint_score(self, code: str) -> float:
|
| 109 |
+
"""Continuous lint score, fallback 0.0 on error."""
|
| 110 |
+
try:
|
| 111 |
+
with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f:
|
| 112 |
+
f.write(code)
|
| 113 |
+
f.flush()
|
| 114 |
+
tmp_path = f.name
|
| 115 |
+
result = subprocess.run(
|
| 116 |
+
['pylint', tmp_path, '--score=y', '--exit-zero'],
|
| 117 |
+
capture_output=True,
|
| 118 |
+
text=True,
|
| 119 |
+
timeout=5
|
| 120 |
+
)
|
| 121 |
+
match = re.search(r"rated at (\d+\.\d+)/10", result.stdout)
|
| 122 |
+
if match:
|
| 123 |
+
score = float(match.group(1)) / 10.0
|
| 124 |
+
else:
|
| 125 |
+
score = 0.0
|
| 126 |
+
return max(0.0, min(1.0, score))
|
| 127 |
+
except Exception:
|
| 128 |
+
return 0.0
|
| 129 |
+
finally:
|
| 130 |
+
try:
|
| 131 |
+
os.unlink(tmp_path)
|
| 132 |
+
except:
|
| 133 |
+
pass
|
| 134 |
+
|
| 135 |
+
def _ast_similarity(self, proposed_code: str) -> float:
|
| 136 |
+
"""Structural similarity to oracle."""
|
| 137 |
+
if not self.oracle_code:
|
| 138 |
+
return 0.0
|
| 139 |
+
try:
|
| 140 |
+
tree_prop = ast.parse(proposed_code)
|
| 141 |
+
tree_oracle = ast.parse(self.oracle_code)
|
| 142 |
+
nodes_prop = [type(n) for n in ast.walk(tree_prop)]
|
| 143 |
+
nodes_oracle = [type(n) for n in ast.walk(tree_oracle)]
|
| 144 |
+
common = sum(1 for n in nodes_prop if n in nodes_oracle)
|
| 145 |
+
total = max(len(nodes_prop), len(nodes_oracle))
|
| 146 |
+
return common / total if total > 0 else 0.0
|
| 147 |
+
except:
|
| 148 |
return 0.0
|