File size: 12,924 Bytes
c8e832f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Static PR-review tasks and hidden grading rubrics."""

from __future__ import annotations

from dataclasses import dataclass, field
from typing import Dict, Iterable, List, Sequence

try:
    from models import Category, Difficulty, Severity, TaskDescriptor, TaskSummary
except ModuleNotFoundError:  # pragma: no cover
    from ..models import Category, Difficulty, Severity, TaskDescriptor, TaskSummary


@dataclass(frozen=True)
class RubricIssue:
    """One hidden issue that can be matched by the deterministic grader."""

    issue_id: str
    file_path: str
    line: int
    category: Category
    severity: Severity
    keywords: Sequence[str]
    min_keyword_hits: int
    weight: float


@dataclass(frozen=True)
class TaskSpec:
    """Complete task definition, including hidden rubric metadata."""

    task_id: str
    difficulty: Difficulty
    title: str
    goal: str
    repo_summary: str
    visible_diff: str
    file_contents: Dict[str, str]
    changed_files: Sequence[str]
    rubric_issues: Sequence[RubricIssue]
    max_steps: int

    @property
    def available_files(self) -> List[str]:
        return list(self.file_contents.keys())

    def to_descriptor(self) -> TaskDescriptor:
        return TaskDescriptor(
            task_id=self.task_id,
            difficulty=self.difficulty,
            title=self.title,
            goal=self.goal,
            repo_summary=self.repo_summary,
            changed_files=list(self.changed_files),
            available_files=self.available_files,
            max_steps=self.max_steps,
        )

    def to_summary(self) -> TaskSummary:
        return TaskSummary(
            task_id=self.task_id,
            difficulty=self.difficulty,
            title=self.title,
            goal=self.goal,
        )


TASKS: List[TaskSpec] = [
    TaskSpec(
        task_id="py-pr-review-easy",
        difficulty="easy",
        title="Retry Delay Regression",
        goal=(
            "Review the pull request and identify the real bug introduced in the retry "
            "delay helper before it ships."
        ),
        repo_summary=(
            "This service computes retry delays for background notification delivery. "
            "The change is intended to relax validation for legacy callers."
        ),
        visible_diff="\n".join(
            [
                "diff --git a/src/notifications/retry.py b/src/notifications/retry.py",
                "@@",
                "-    if base_delay <= 0:",
                "+    if base_delay < 0:",
                "         return 0.0",
            ]
        ),
        file_contents={
            "src/notifications/retry.py": "\n".join(
                [
                    "from __future__ import annotations",
                    "",
                    "def calculate_retry_delay(attempt: int, base_delay: float = 2.0) -> float:",
                    '    """Return the retry delay in seconds."""',
                    "    if attempt < 0:",
                    '        raise ValueError(\"attempt must be >= 0\")',
                    "    if base_delay < 0:",
                    "        return 0.0",
                    "    return attempt / base_delay",
                ]
            )
        },
        changed_files=("src/notifications/retry.py",),
        rubric_issues=(
            RubricIssue(
                issue_id="zero-base-delay-divides",
                file_path="src/notifications/retry.py",
                line=7,
                category="bug",
                severity="warning",
                keywords=("zero", "division", "base_delay"),
                min_keyword_hits=2,
                weight=1.0,
            ),
        ),
        max_steps=4,
    ),
    TaskSpec(
        task_id="py-pr-review-medium",
        difficulty="medium",
        title="Coupon Billing Rollout",
        goal=(
            "Review the billing change and identify both the production regression and "
            "the missing coverage that would have caught it."
        ),
        repo_summary=(
            "The billing service is adding coupon support for one-off invoices. The PR "
            "touches both the service code and its unit tests."
        ),
        visible_diff="\n".join(
            [
                "diff --git a/app/billing/invoice_service.py b/app/billing/invoice_service.py",
                "@@",
                " def charge_invoice(order: dict, gateway: Gateway) -> str:",
                "-    return gateway.charge(order[\"customer_id\"], order[\"amount_cents\"])",
                "+    total = order[\"amount_cents\"]",
                "+    coupon = order.get(\"coupon_code\")",
                "+    if coupon:",
                "+        discount = gateway.lookup_discount(coupon)",
                "+        total = max(total - discount, 0)",
                "+    return gateway.charge(order[\"customer_id\"], order[\"amount_cents\"])",
                "",
                "diff --git a/tests/test_invoice_service.py b/tests/test_invoice_service.py",
                "@@",
                " class FakeGateway:",
                "+    def lookup_discount(self, coupon: str) -> int:",
                "+        return 250",
            ]
        ),
        file_contents={
            "app/billing/invoice_service.py": "\n".join(
                [
                    "from gateway import Gateway",
                    "",
                    "def charge_invoice(order: dict, gateway: Gateway) -> str:",
                    '    total = order["amount_cents"]',
                    '    coupon = order.get("coupon_code")',
                    "    if coupon:",
                    "        discount = gateway.lookup_discount(coupon)",
                    "        total = max(total - discount, 0)",
                    '    return gateway.charge(order["customer_id"], order["amount_cents"])',
                ]
            ),
            "tests/test_invoice_service.py": "\n".join(
                [
                    "from app.billing.invoice_service import charge_invoice",
                    "",
                    "class FakeGateway:",
                    "    def lookup_discount(self, coupon: str) -> int:",
                    "        return 250",
                    "",
                    "    def charge(self, customer_id: str, amount_cents: int) -> str:",
                    "        self.last_charge = (customer_id, amount_cents)",
                    '        return "charge_123"',
                    "",
                    "def test_charge_invoice_without_coupon():",
                    "    gateway = FakeGateway()",
                    '    charge_invoice({"customer_id": "cus_1", "amount_cents": 1000}, gateway)',
                    '    assert gateway.last_charge == ("cus_1", 1000)',
                ]
            ),
        },
        changed_files=("app/billing/invoice_service.py", "tests/test_invoice_service.py"),
        rubric_issues=(
            RubricIssue(
                issue_id="discount-total-unused",
                file_path="app/billing/invoice_service.py",
                line=8,
                category="bug",
                severity="warning",
                keywords=("discount", "total", "charge", "amount"),
                min_keyword_hits=2,
                weight=0.6,
            ),
            RubricIssue(
                issue_id="missing-coupon-test",
                file_path="tests/test_invoice_service.py",
                line=11,
                category="testing",
                severity="warning",
                keywords=("missing", "test", "coupon", "discount"),
                min_keyword_hits=2,
                weight=0.4,
            ),
        ),
        max_steps=5,
    ),
    TaskSpec(
        task_id="py-pr-review-hard",
        difficulty="hard",
        title="Async Job Runner Deduplication",
        goal=(
            "Review the async job-runner PR and find the subtle concurrency issues "
            "without inventing extra problems."
        ),
        repo_summary=(
            "A shared webhook backfill service is deduplicating in-flight work with an "
            "async task cache and writing the latest result for operators to inspect."
        ),
        visible_diff="\n".join(
            [
                "diff --git a/app/jobs/runner.py b/app/jobs/runner.py",
                "@@",
                " async def run_job(job_id: str, payload: dict, worker) -> str:",
                "     if job_id in ACTIVE_RUNS:",
                "         return await ACTIVE_RUNS[job_id]",
                "+    lock = asyncio.Lock()",
                "+    async with lock:",
                "+        task = asyncio.create_task(worker.run(payload))",
                "+        ACTIVE_RUNS[job_id] = task",
                "     try:",
                "         result = await task",
                "     finally:",
                "         ACTIVE_RUNS.pop(job_id, None)",
                "+    Path(\"latest-result.json\").write_text(result)",
                "     return result",
            ]
        ),
        file_contents={
            "app/jobs/runner.py": "\n".join(
                [
                    "import asyncio",
                    "from pathlib import Path",
                    "",
                    "ACTIVE_RUNS: dict[str, asyncio.Task[str]] = {}",
                    "",
                    "async def run_job(job_id: str, payload: dict, worker) -> str:",
                    "    if job_id in ACTIVE_RUNS:",
                    "        return await ACTIVE_RUNS[job_id]",
                    "",
                    "    lock = asyncio.Lock()",
                    "    async with lock:",
                    "        task = asyncio.create_task(worker.run(payload))",
                    "        ACTIVE_RUNS[job_id] = task",
                    "    try:",
                    "        result = await task",
                    "    finally:",
                    "        ACTIVE_RUNS.pop(job_id, None)",
                    "",
                    '    Path("latest-result.json").write_text(result)',
                    "    return result",
                ]
            ),
            "tests/test_runner.py": "\n".join(
                [
                    "import pytest",
                    "",
                    "from app.jobs.runner import run_job",
                    "",
                    "class FakeWorker:",
                    "    async def run(self, payload: dict) -> str:",
                    '        return payload["job_id"]',
                    "",
                    "@pytest.mark.asyncio",
                    "async def test_run_job_returns_worker_result():",
                    "    worker = FakeWorker()",
                    '    result = await run_job("job-1", {"job_id": "job-1"}, worker)',
                    '    assert result == "job-1"',
                ]
            ),
        },
        changed_files=("app/jobs/runner.py", "tests/test_runner.py"),
        rubric_issues=(
            RubricIssue(
                issue_id="per-call-lock-race",
                file_path="app/jobs/runner.py",
                line=9,
                category="bug",
                severity="warning",
                keywords=("lock", "race", "concurrent", "duplicate"),
                min_keyword_hits=2,
                weight=0.55,
            ),
            RubricIssue(
                issue_id="shared-output-file-race",
                file_path="app/jobs/runner.py",
                line=18,
                category="maintainability",
                severity="warning",
                keywords=("latest", "result", "file", "concurrent", "overwrite"),
                min_keyword_hits=2,
                weight=0.45,
            ),
        ),
        max_steps=6,
    ),
]


TASKS_BY_ID: Dict[str, TaskSpec] = {task.task_id: task for task in TASKS}


def list_task_descriptors() -> List[TaskDescriptor]:
    """Return public descriptors for all tasks."""

    return [task.to_descriptor() for task in TASKS]


def list_task_summaries() -> List[TaskSummary]:
    """Return task summaries for lightweight route responses."""

    return [task.to_summary() for task in TASKS]


def get_task(task_id: str) -> TaskSpec:
    """Return a task by id."""

    try:
        return TASKS_BY_ID[task_id]
    except KeyError as exc:  # pragma: no cover
        raise ValueError(f"Unknown task_id: {task_id}") from exc


def task_ids() -> Iterable[str]:
    """Return task ids in benchmark order."""

    return [task.task_id for task in TASKS]