Spaces:
Sleeping
Sleeping
Deploy: server/grader.py
Browse files- server/grader.py +128 -0
server/grader.py
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from .models import ExecutionResult, TaskInfo
|
| 2 |
+
from .llm_judge import llm_judge
|
| 3 |
+
|
| 4 |
+
def force_valid_reward(value) -> float:
|
| 5 |
+
"""Hard guarantee: reward is strictly in (0, 1) — never 0 or 1, no exceptions."""
|
| 6 |
+
try:
|
| 7 |
+
r = float(value)
|
| 8 |
+
except Exception:
|
| 9 |
+
return 0.5
|
| 10 |
+
|
| 11 |
+
if r <= 0.001:
|
| 12 |
+
return 0.001
|
| 13 |
+
if r >= 0.999:
|
| 14 |
+
return 0.999
|
| 15 |
+
|
| 16 |
+
return r
|
| 17 |
+
|
| 18 |
+
def safe_reward(reward) -> float:
|
| 19 |
+
"""Clamp reward to open interval (0, 1) via force_valid_reward."""
|
| 20 |
+
if reward is None:
|
| 21 |
+
reward = 0.5
|
| 22 |
+
return force_valid_reward(reward)
|
| 23 |
+
|
| 24 |
+
def normalize_reward(passed: int, total: int) -> float:
|
| 25 |
+
if total == 0:
|
| 26 |
+
return 0.5
|
| 27 |
+
raw = passed / total
|
| 28 |
+
return force_valid_reward(raw)
|
| 29 |
+
|
| 30 |
+
def get_llm_quality_score(proposed_fix: str) -> dict:
|
| 31 |
+
return llm_judge("", proposed_fix, "unknown")
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def calculate_codearena_reward(
|
| 35 |
+
*,
|
| 36 |
+
compile_ok: bool,
|
| 37 |
+
passed: int,
|
| 38 |
+
total: int,
|
| 39 |
+
execution_time_seconds: float,
|
| 40 |
+
optimal_time_seconds: float,
|
| 41 |
+
buggy_code: str,
|
| 42 |
+
proposed_fix: str,
|
| 43 |
+
task_category: str,
|
| 44 |
+
step_count: int,
|
| 45 |
+
is_repeated_fix: bool,
|
| 46 |
+
) -> tuple[float, dict]:
|
| 47 |
+
compile_score = 1.0 if compile_ok else 0.0
|
| 48 |
+
test_pass_ratio = passed / total if total else 0.0
|
| 49 |
+
|
| 50 |
+
efficiency_score = 0.0
|
| 51 |
+
if test_pass_ratio == 1.0:
|
| 52 |
+
if execution_time_seconds <= optimal_time_seconds:
|
| 53 |
+
efficiency_score = 1.0
|
| 54 |
+
else:
|
| 55 |
+
ratio = execution_time_seconds / max(0.001, optimal_time_seconds)
|
| 56 |
+
efficiency_score = max(0.0, 1.0 - (ratio - 1.0) / 2.0)
|
| 57 |
+
|
| 58 |
+
llm_scores = llm_judge(buggy_code, proposed_fix, task_category)
|
| 59 |
+
llm_correctness = float(llm_scores.get("correctness", 0.5))
|
| 60 |
+
llm_security = float(llm_scores.get("security", 0.5))
|
| 61 |
+
llm_code_quality = float(llm_scores.get("code_quality", 0.5))
|
| 62 |
+
llm_judge_score = (llm_correctness + llm_security + llm_code_quality) / 3
|
| 63 |
+
novelty_penalty = 1.0 if is_repeated_fix else 0.0
|
| 64 |
+
step_penalty = 0.02 * step_count
|
| 65 |
+
|
| 66 |
+
final_reward = (
|
| 67 |
+
0.20 * compile_score
|
| 68 |
+
+ 0.40 * test_pass_ratio
|
| 69 |
+
+ 0.10 * efficiency_score
|
| 70 |
+
+ 0.30 * llm_judge_score
|
| 71 |
+
- step_penalty
|
| 72 |
+
- 0.10 * novelty_penalty
|
| 73 |
+
)
|
| 74 |
+
final_reward = force_valid_reward(round(final_reward, 4))
|
| 75 |
+
|
| 76 |
+
return final_reward, {
|
| 77 |
+
"compile_score": compile_score,
|
| 78 |
+
"test_pass_ratio": test_pass_ratio,
|
| 79 |
+
"efficiency_score": efficiency_score,
|
| 80 |
+
"llm_correctness": llm_correctness,
|
| 81 |
+
"llm_security": llm_security,
|
| 82 |
+
"llm_code_quality": llm_code_quality,
|
| 83 |
+
"step_penalty": step_penalty,
|
| 84 |
+
"novelty_penalty": novelty_penalty,
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
def calculate_reward_components(exec_result: ExecutionResult, task_info: TaskInfo, proposed_fix: str) -> dict:
|
| 88 |
+
_, components = calculate_codearena_reward(
|
| 89 |
+
compile_ok=exec_result.compile_success,
|
| 90 |
+
passed=exec_result.test_passed,
|
| 91 |
+
total=exec_result.test_total,
|
| 92 |
+
execution_time_seconds=exec_result.execution_time_seconds,
|
| 93 |
+
optimal_time_seconds=task_info.optimal_time_seconds,
|
| 94 |
+
buggy_code=task_info.buggy_code,
|
| 95 |
+
proposed_fix=proposed_fix,
|
| 96 |
+
task_category=task_info.difficulty,
|
| 97 |
+
step_count=0,
|
| 98 |
+
is_repeated_fix=False,
|
| 99 |
+
)
|
| 100 |
+
return components
|
| 101 |
+
|
| 102 |
+
def calculate_reward(
|
| 103 |
+
exec_result: ExecutionResult,
|
| 104 |
+
task_info: TaskInfo,
|
| 105 |
+
proposed_fix: str,
|
| 106 |
+
step_count: int = 0,
|
| 107 |
+
is_repeated_fix: bool = False,
|
| 108 |
+
) -> tuple[float, dict]:
|
| 109 |
+
return calculate_codearena_reward(
|
| 110 |
+
compile_ok=exec_result.compile_success,
|
| 111 |
+
passed=exec_result.test_passed,
|
| 112 |
+
total=exec_result.test_total,
|
| 113 |
+
execution_time_seconds=exec_result.execution_time_seconds,
|
| 114 |
+
optimal_time_seconds=task_info.optimal_time_seconds,
|
| 115 |
+
buggy_code=task_info.buggy_code,
|
| 116 |
+
proposed_fix=proposed_fix,
|
| 117 |
+
task_category=task_info.difficulty,
|
| 118 |
+
step_count=step_count,
|
| 119 |
+
is_repeated_fix=is_repeated_fix,
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
def grade(*args, **kwargs) -> float:
|
| 123 |
+
try:
|
| 124 |
+
if len(args) == 3:
|
| 125 |
+
return calculate_reward(args[0], args[1], args[2])[0]
|
| 126 |
+
return 0.5
|
| 127 |
+
except Exception:
|
| 128 |
+
return 0.5
|