File size: 4,516 Bytes
700c7f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Rubric criteria as binary checkpoints, and the weighted-mean aggregate.

Every public SWEQnA task ships ``hidden_tests/rubric.md``; its criteria become
equal-weight LLM checkpoints unless a criterion states a weight. The reward is
the weighted mean of per-item scores, where an item's score is the median of
the panel members' binary votes.
"""
from __future__ import annotations

import re
from dataclasses import dataclass, field
from pathlib import Path

from .rubric import parse_rubric

TIERS = ("critical", "important", "sanity")
_PERCENT = re.compile(r"\((\d+(?:\.\d+)?)\s*%\)")
_POINTS = re.compile(r"\((\d+(?:\.\d+)?)\s*(?:points?|pts?)\)", re.IGNORECASE)


class CheckpointError(ValueError):
    """The task cannot be scored under a checkpoint policy as authored."""


@dataclass
class Checkpoint:
    id: str
    method: str = "llm"
    tier: str = "important"
    weight: float = 1.0
    question: str = ""
    pass_anchor: str = ""
    files: list = field(default_factory=list)

    @property
    def is_gate(self) -> bool:
        return self.method == "rule" and self.tier == "sanity"


def derive_from_rubric(text: str) -> list:
    """Numbered/R-id rubric criteria -> llm checkpoints, equal weight unless stated."""
    rubric = parse_rubric(text)
    if not rubric.criteria:
        raise CheckpointError("rubric.md has no parseable criteria: " + "; ".join(rubric.problems))
    items = []
    for c in rubric.criteria:
        body, weight = c.text, c.weight
        if weight is None:
            m = _PERCENT.search(body) or _POINTS.search(body)
            if m:
                weight = float(m.group(1))
                body = (body[:m.start()] + body[m.end():]).strip()
        body = re.sub(r"\s+", " ", body).strip(" :.-—*")
        items.append(Checkpoint(id=str(c.id), question=body, weight=weight if weight else 1.0))
    return items


def validate_checkpoints(items) -> list:
    problems, seen = [], set()
    for it in items:
        where = f"checkpoint {it.id!r}"
        if not it.id:
            problems.append("checkpoint with an empty id")
        if it.id in seen:
            problems.append(f"{where}: duplicate id")
        seen.add(it.id)
        if it.method not in ("llm", "rule"):
            problems.append(f"{where}: unknown method {it.method!r}")
        if it.tier not in TIERS:
            problems.append(f"{where}: unknown tier {it.tier!r}")
        if not (it.weight > 0):
            problems.append(f"{where}: weight must be > 0")
        if it.method == "llm" and not it.question:
            problems.append(f"{where}: llm item has no question")
        if it.method == "llm" and it.tier == "sanity":
            problems.append(f"{where}: sanity (gate) tier is for rule items only")
    return problems


def load_checkpoints(hidden_dir: Path) -> tuple:
    """(items, source) for a task's hidden bundle; raises CheckpointError."""
    rubric_path = Path(hidden_dir) / "rubric.md"
    if not rubric_path.exists():
        raise CheckpointError(f"no rubric.md in {hidden_dir}")
    items = derive_from_rubric(rubric_path.read_text(encoding="utf-8"))
    problems = validate_checkpoints(items)
    if problems:
        raise CheckpointError("rubric.md: " + "; ".join(problems))
    return items, "rubric.md"


def render_for_judge(items) -> str:
    """The checkpoint block of the judge prompt: llm items only, ids verbatim."""
    lines = []
    for it in items:
        if it.method != "llm":
            continue
        head = f"[{it.id}] ({it.tier}, weight {it.weight:g})"
        if it.files:
            head += f" — judge from: {', '.join(it.files)}"
        lines.append(head)
        lines.append(f"  question: {it.question}")
        if it.pass_anchor:
            lines.append(f"  ground truth (the worker never saw this): {it.pass_anchor}")
    return "\n".join(lines)


def weighted_score(items, item_scores: dict) -> tuple:
    """(score, gate_failed): weighted mean over non-gate items; a failed gate zeroes it."""
    missing = [it.id for it in items if it.id not in item_scores]
    if missing:
        raise CheckpointError(f"no score for checkpoint(s) {missing}")
    gate_failed = [it.id for it in items if it.is_gate and item_scores[it.id] < 0.999]
    scored = [it for it in items if not it.is_gate]
    total = sum(it.weight for it in scored)
    value = sum(it.weight * float(item_scores[it.id]) for it in scored) / total if total else 0.0
    if gate_failed:
        return 0.0, gate_failed
    return round(value, 6), []