forked from pinchbench/skill
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib_grading.py
More file actions
470 lines (409 loc) · 16.6 KB
/
lib_grading.py
File metadata and controls
470 lines (409 loc) · 16.6 KB
1
2
3
4
5
6
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
"""
PinchBench grading engine.
"""
from __future__ import annotations
import json
import logging
import re
from dataclasses import dataclass
from pathlib import Path
from typing import Any, Dict, List, Optional
from lib_agent import ensure_agent_exists, run_openclaw_prompt, slugify_model
from lib_tasks import Task
logger = logging.getLogger(__name__)
DEFAULT_JUDGE_MODEL = "openrouter/anthropic/claude-opus-4.5"
DEFAULT_JUDGE_AGENT_PREFIX = "bench-judge"
DEFAULT_JUDGE_TIMEOUT_SECONDS = 180
@dataclass
class GradeResult:
task_id: str
score: float
max_score: float
grading_type: str
breakdown: Dict[str, float]
notes: str
def to_dict(self) -> Dict[str, Any]:
return {
"task_id": self.task_id,
"score": self.score,
"max_score": self.max_score,
"grading_type": self.grading_type,
"breakdown": self.breakdown,
"notes": self.notes,
}
def grade_task(
*,
task: Task,
execution_result: Dict[str, Any],
skill_dir: Path,
judge_model: str = DEFAULT_JUDGE_MODEL,
judge_agent_prefix: str = DEFAULT_JUDGE_AGENT_PREFIX,
judge_timeout_seconds: float = DEFAULT_JUDGE_TIMEOUT_SECONDS,
verbose: bool = False,
) -> GradeResult:
grading_type = task.grading_type
if verbose:
logger.info(" [VERBOSE] Grading task %s with type: %s", task.task_id, grading_type)
logger.info(" [VERBOSE] Execution status: %s", execution_result.get("status", "unknown"))
if grading_type == "automated":
result = _grade_automated(task, execution_result, verbose=verbose)
if verbose:
logger.info(" [VERBOSE] Automated grade breakdown: %s", result.breakdown)
return result
if grading_type == "llm_judge":
result = _grade_llm_judge(
task=task,
execution_result=execution_result,
judge_model=judge_model,
judge_agent_prefix=judge_agent_prefix,
judge_timeout_seconds=judge_timeout_seconds,
skill_dir=skill_dir,
verbose=verbose,
)
if verbose:
logger.info(" [VERBOSE] LLM judge breakdown: %s", result.breakdown)
return result
if grading_type == "hybrid":
auto_result = _grade_automated(task, execution_result, verbose=verbose)
llm_result = _grade_llm_judge(
task=task,
execution_result=execution_result,
judge_model=judge_model,
judge_agent_prefix=judge_agent_prefix,
judge_timeout_seconds=judge_timeout_seconds,
skill_dir=skill_dir,
verbose=verbose,
)
return _combine_grades(task, auto_result, llm_result)
raise ValueError(f"Unknown grading type: {grading_type}")
def _grade_automated(task: Task, execution_result: Dict[str, Any], verbose: bool = False) -> GradeResult:
grading_code = _extract_grading_code(task)
if not grading_code:
return GradeResult(
task_id=task.task_id,
score=0.0,
max_score=1.0,
grading_type="automated",
breakdown={},
notes="No automated grading code found",
)
namespace: Dict[str, Any] = {}
exec(grading_code, namespace)
grade_func = namespace.get("grade")
if not callable(grade_func):
return GradeResult(
task_id=task.task_id,
score=0.0,
max_score=1.0,
grading_type="automated",
breakdown={},
notes="Automated grading function missing",
)
scores = grade_func(
execution_result.get("transcript", []),
execution_result.get("workspace", ""),
)
if not isinstance(scores, dict):
scores = {}
if verbose:
logger.info(" [VERBOSE] Automated grading scores: %s", scores)
total = _average_scores(scores)
return GradeResult(
task_id=task.task_id,
score=total,
max_score=1.0,
grading_type="automated",
breakdown=_normalize_score_dict(scores),
notes="",
)
def _grade_llm_judge(
*,
task: Task,
execution_result: Dict[str, Any],
judge_model: str,
judge_agent_prefix: str,
judge_timeout_seconds: float,
skill_dir: Path,
verbose: bool = False,
) -> GradeResult:
transcript_summary = _summarize_transcript(execution_result.get("transcript", []))
if verbose:
logger.info(" [VERBOSE] Transcript summary for judge (first 1000 chars):\n%s", transcript_summary[:1000])
rubric = task.llm_judge_rubric or _format_grading_criteria(task)
prompt = _build_judge_prompt(task, transcript_summary, rubric)
agent_id = _ensure_judge_agent(judge_agent_prefix, judge_model, skill_dir)
judge_workspace = Path(f"/tmp/pinchbench/judge/{task.task_id}")
judge_result = run_openclaw_prompt(
agent_id=agent_id,
prompt=prompt,
workspace=judge_workspace,
timeout_seconds=judge_timeout_seconds,
)
raw_parsed = _parse_judge_response(judge_result.get("transcript", []))
if verbose:
logger.info(" [VERBOSE] Judge raw response parsed: %s", raw_parsed)
# Normalize the response to handle various formats (criteria_scores, score, justification, etc.)
parsed = _normalize_judge_response(raw_parsed)
if verbose:
logger.info(" [VERBOSE] Normalized judge response: %s", parsed)
breakdown = parsed.get("scores", {})
total = parsed.get("total")
notes = parsed.get("notes", "")
return GradeResult(
task_id=task.task_id,
score=float(total) if total is not None else 0.0,
max_score=1.0,
grading_type="llm_judge",
breakdown=_normalize_score_dict(breakdown),
notes=str(notes) if notes is not None else "",
)
def _combine_grades(task: Task, auto_result: GradeResult, llm_result: GradeResult) -> GradeResult:
weights = task.grading_weights or {"automated": 0.5, "llm_judge": 0.5}
auto_weight = float(weights.get("automated", 0.5))
llm_weight = float(weights.get("llm_judge", 0.5))
total_weight = auto_weight + llm_weight
if total_weight <= 0:
auto_weight = llm_weight = 0.5
total_weight = 1.0
combined_score = (
auto_result.score * auto_weight + llm_result.score * llm_weight
) / total_weight
breakdown = {
**{f"automated.{k}": v for k, v in auto_result.breakdown.items()},
**{f"llm_judge.{k}": v for k, v in llm_result.breakdown.items()},
}
notes = " | ".join(filter(None, [auto_result.notes, llm_result.notes]))
return GradeResult(
task_id=task.task_id,
score=combined_score,
max_score=1.0,
grading_type="hybrid",
breakdown=breakdown,
notes=notes,
)
def _extract_grading_code(task: Task) -> str:
if not task.automated_checks:
return ""
match = re.search(r"```python\s*(.*?)\s*```", task.automated_checks, re.DOTALL)
if not match:
return ""
return match.group(1)
def _average_scores(scores: Dict[str, Any]) -> float:
values = [float(v) for v in scores.values() if isinstance(v, (int, float))]
if not values:
return 0.0
return sum(values) / len(values)
def _normalize_score_dict(scores: Dict[str, Any]) -> Dict[str, float]:
normalized: Dict[str, float] = {}
for key, value in scores.items():
try:
normalized[str(key)] = float(value)
except (TypeError, ValueError):
continue
return normalized
def _format_grading_criteria(task: Task) -> str:
if not task.grading_criteria:
return ""
return "\n".join(f"- {criterion}" for criterion in task.grading_criteria)
def _summarize_transcript(transcript: List[Dict[str, Any]]) -> str:
summary_parts: List[str] = []
for event in transcript:
if event.get("type") != "message":
continue
msg = event.get("message", {})
role = msg.get("role")
if role == "assistant":
for item in msg.get("content", []):
if item.get("type") == "toolCall":
summary_parts.append(
f"Tool: {item.get('name')}({json.dumps(item.get('arguments', {}))})"
)
elif role == "toolResult":
content = msg.get("content", [])
if content:
result_preview = str(content[0])[:200]
summary_parts.append(f"Result: {result_preview}")
elif role == "user":
content = msg.get("content", [])
if content:
summary_parts.append(f"User: {content[0]}")
return "\n".join(summary_parts)
def _build_judge_prompt(task: Task, transcript_summary: str, rubric: str) -> str:
return (
"You are a grading function. Your ONLY job is to output a single JSON object.\n\n"
"CRITICAL RULES:\n"
"- Do NOT use any tools (no Read, Write, exec, or any other tool calls)\n"
"- Do NOT create files or run commands\n"
"- Do NOT write any prose, explanation, or commentary outside the JSON\n"
"- Respond with ONLY a JSON object — nothing else\n\n"
"Be a strict evaluator. Reserve 1.0 for genuinely excellent performance. "
"An average acceptable completion should score around 0.6-0.7. "
"Deduct points for unnecessary steps, verbose output, and inefficient tool usage.\n\n"
"## Task\n"
f"{task.prompt}\n\n"
"## Expected Behavior\n"
f"{task.expected_behavior}\n\n"
"## Agent Transcript (summarized)\n"
f"{transcript_summary}\n\n"
"## Grading Rubric\n"
f"{rubric}\n\n"
"Score each criterion from 0.0 to 1.0.\n\n"
"Respond with ONLY this JSON structure (no markdown, no code fences, no extra text):\n"
'{"scores": {"criterion_name": 0.0}, "total": 0.0, "notes": "brief justification"}'
)
def _ensure_judge_agent(judge_agent_prefix: str, judge_model: str, skill_dir: Path) -> str:
model_slug = slugify_model(judge_model)
agent_id = f"{judge_agent_prefix}-{model_slug}"
workspace = Path("/tmp/pinchbench/judge/workspace")
created = ensure_agent_exists(agent_id, judge_model, workspace)
# OpenClaw `agents add` scaffolds AGENTS.md, SOUL.md, BOOTSTRAP.md, etc.
# into the workspace. These template files instruct the agent to perform a
# bootstrap / personality flow (read SOUL.md, do introductions, etc.)
# instead of acting as a pure grading function. Remove them so the judge
# only responds to the grading prompt with JSON.
_clean_judge_workspace(workspace)
return agent_id
def _clean_judge_workspace(workspace: Path) -> None:
"""Remove OpenClaw-scaffolded template files that interfere with judge grading."""
import shutil
template_files = (
"AGENTS.md", "SOUL.md", "BOOTSTRAP.md", "HEARTBEAT.md",
"IDENTITY.md", "TOOLS.md", "USER.md", "MEMORY.md",
)
template_dirs = (".git", "memory")
removed = 0
for name in template_files:
p = workspace / name
if p.exists():
p.unlink()
removed += 1
for name in template_dirs:
d = workspace / name
if d.is_dir():
shutil.rmtree(d, ignore_errors=True)
removed += 1
if removed:
logger.info("Cleaned %d template files/dirs from judge workspace", removed)
def _parse_judge_response(transcript: List[Dict[str, Any]]) -> Dict[str, Any]:
content_chunks: List[str] = []
for event in transcript:
if event.get("type") != "message":
continue
msg = event.get("message", {})
if msg.get("role") != "assistant":
continue
for item in msg.get("content", []):
if item.get("type") == "text":
content_chunks.append(item.get("text", ""))
raw_text = "\n".join(content_chunks).strip()
if not raw_text:
return {}
# First, try to extract JSON from code blocks (```json ... ```)
code_block_match = re.search(r"```json\s*(.*?)\s*```", raw_text, re.DOTALL)
if code_block_match:
try:
parsed = json.loads(code_block_match.group(1))
if isinstance(parsed, dict):
return parsed
except json.JSONDecodeError:
pass
# Find all potential JSON objects by looking for balanced braces
# We'll extract chunks that start with { and try to parse them
json_candidates: List[str] = []
brace_depth = 0
current_json = []
for char in raw_text:
if char == "{":
if brace_depth == 0:
current_json = []
brace_depth += 1
if brace_depth > 0:
current_json.append(char)
if char == "}":
brace_depth -= 1
if brace_depth == 0 and current_json:
json_candidates.append("".join(current_json))
# Try parsing from the last JSON object backwards (most recent response)
for candidate in reversed(json_candidates):
try:
parsed = json.loads(candidate)
if isinstance(parsed, dict) and "scores" in parsed:
# Prefer JSON that has the expected structure
return parsed
except json.JSONDecodeError:
continue
# Try any valid JSON dict
for candidate in reversed(json_candidates):
try:
parsed = json.loads(candidate)
if isinstance(parsed, dict):
return parsed
except json.JSONDecodeError:
continue
# Fallback: try to extract numeric scores from prose responses.
# Models sometimes return "Total: 0.72" or "Overall score: 0.65" instead of JSON.
score_pattern = re.search(
r"(?:total|overall|final)\s*(?:score)?[:\s]*(0\.\d+|1\.0+)",
raw_text,
re.IGNORECASE,
)
if score_pattern:
try:
total = float(score_pattern.group(1))
if 0.0 <= total <= 1.0:
logger.warning(
"Fell back to regex score extraction from prose (total=%.2f)", total
)
return {"scores": {}, "total": total, "notes": "Score extracted from prose (JSON parse failed)"}
except ValueError:
pass
logger.warning("Failed to parse judge JSON response")
return {}
def _normalize_judge_response(parsed: Dict[str, Any]) -> Dict[str, Any]:
"""
Normalize judge response to expected format with 'scores', 'total', and 'notes'.
Handles various response formats:
- {"scores": {...}, "total": 0.9, "notes": "..."} (expected)
- {"criteria_scores": {...}, ...} (Claude sometimes uses this)
- {"score": 0.9, "justification": "..."} (simplified format)
"""
result: Dict[str, Any] = {"scores": {}, "total": None, "notes": ""}
# Extract scores from various keys
if "scores" in parsed:
scores_data = parsed["scores"]
if isinstance(scores_data, dict):
# Handle nested structure: {"criterion": {"score": 0.9, "weight": 0.3}}
for key, value in scores_data.items():
if isinstance(value, dict) and "score" in value:
result["scores"][key] = float(value["score"]) if isinstance(value["score"], (int, float, str)) else value["score"]
elif isinstance(value, (int, float)):
result["scores"][key] = value
elif "criteria_scores" in parsed:
# Handle Claude's alternate format
criteria = parsed["criteria_scores"]
if isinstance(criteria, dict):
for key, value in criteria.items():
if isinstance(value, dict) and "score" in value:
result["scores"][key] = value["score"]
elif isinstance(value, (int, float)):
result["scores"][key] = value
# Extract total score
if "total" in parsed and parsed["total"] is not None:
result["total"] = float(parsed["total"]) if isinstance(parsed["total"], (int, float)) else None
elif "score" in parsed and isinstance(parsed["score"], (int, float)):
result["total"] = float(parsed["score"])
elif "overall_score" in parsed and isinstance(parsed["overall_score"], (int, float)):
result["total"] = float(parsed["overall_score"])
elif result["scores"]:
# Calculate average if we have individual scores but no total
values = [v for v in result["scores"].values() if isinstance(v, (int, float))]
if values:
result["total"] = sum(values) / len(values)
# Extract notes/justification
if "notes" in parsed:
result["notes"] = str(parsed["notes"])
elif "justification" in parsed:
result["notes"] = str(parsed["justification"])
elif "reasoning" in parsed:
result["notes"] = str(parsed["reasoning"])
return result