File size: 25,395 Bytes
b92d20c
 
 
 
 
 
 
 
2cd9867
 
d0cfac3
 
 
2cd9867
 
b92d20c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2d2c6e4
 
 
b92d20c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2d2c6e4
 
 
 
 
b92d20c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86b6703
b92d20c
 
 
 
d0cfac3
b92d20c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4910f36
b92d20c
2d2c6e4
 
 
 
 
 
 
b92d20c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4910f36
b92d20c
2d2c6e4
 
 
 
 
 
 
b92d20c
 
 
 
 
 
2d2c6e4
 
 
 
 
b92d20c
 
 
 
 
 
4910f36
b92d20c
 
 
 
2d2c6e4
 
 
b92d20c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4910f36
b92d20c
2d2c6e4
 
 
 
 
 
b92d20c
 
 
 
 
 
2d2c6e4
 
 
 
 
 
b92d20c
 
 
 
 
 
4910f36
b92d20c
2d2c6e4
 
 
 
 
 
 
b92d20c
 
2d2c6e4
 
 
 
 
 
 
 
 
b92d20c
 
 
 
 
 
 
 
 
 
2d2c6e4
 
 
 
 
 
b92d20c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2d2c6e4
b92d20c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2d2c6e4
 
 
b92d20c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86b6703
b92d20c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2d2c6e4
 
b92d20c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2d2c6e4
b92d20c
 
 
 
 
 
 
 
 
 
 
 
 
 
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
from __future__ import annotations

from dataclasses import dataclass, field
from typing import Dict, List, Literal, Sequence, Set

from codereview_env.models import ReviewFinding

Difficulty = Literal["easy", "medium", "hard"]
_MIN_PUBLIC_SCORE = 0.05
_MAX_PUBLIC_SCORE = 0.95


def _clamp_score(raw: float) -> float:
    """Normalize public grader scores into a conservative open interval."""
    return max(_MIN_PUBLIC_SCORE, min(_MAX_PUBLIC_SCORE, raw))


@dataclass(frozen=True)
class Artifact:
    artifact_id: str
    kind: str
    title: str
    preview: str
    content: str
    reward: float = 0.0


@dataclass(frozen=True)
class GraderCriterion:
    criterion_id: str
    description: str
    file_path: str
    severity: str
    weight: float
    required_terms: Sequence[Set[str]]
    recommendation_terms: Sequence[Set[str]] = field(default_factory=tuple)
    preferred_artifacts: Sequence[str] = field(default_factory=tuple)


@dataclass(frozen=True)
class ReviewTask:
    task_id: str
    title: str
    difficulty: Difficulty
    objective: str
    summary: str
    step_limit: int
    starting_artifacts: Sequence[str]
    artifacts: Dict[str, Artifact]
    grader: Sequence[GraderCriterion]


def _contains_group(text: str, groups: Sequence[Set[str]]) -> float:
    if not groups:
        return 1.0
    hits = 0
    for group in groups:
        if any(term in text for term in group):
            hits += 1
    return hits / len(groups)


def grade_findings(
    task: ReviewTask, findings: Sequence[ReviewFinding], opened_artifacts: Set[str]
) -> Dict[str, object]:
    matched = []
    total = 0.0
    finding_texts = []
    for finding in findings:
        combined = " ".join(
            [
                finding.title.lower(),
                finding.file_path.lower(),
                (finding.line_hint or "").lower(),
                finding.rationale.lower(),
                finding.recommendation.lower(),
                finding.severity.lower(),
            ]
        )
        finding_texts.append((finding, combined))

    for criterion in task.grader:
        best = 0.0
        for finding, combined in finding_texts:
            issue_score = _contains_group(combined, criterion.required_terms)
            fix_score = _contains_group(combined, criterion.recommendation_terms)
            severity_score = 1.0 if criterion.severity in combined else 0.4
            file_score = 1.0 if criterion.file_path.lower() in combined else 0.5
            evidence_score = (
                1.0
                if not criterion.preferred_artifacts
                else min(
                    1.0,
                    sum(
                        1
                        for artifact_id in criterion.preferred_artifacts
                        if artifact_id in opened_artifacts
                    )
                    / len(criterion.preferred_artifacts),
                )
            )
            score = criterion.weight * (
                0.45 * issue_score
                + 0.25 * fix_score
                + 0.15 * severity_score
                + 0.10 * file_score
                + 0.05 * evidence_score
            )
            if score > best:
                best = score
        total += best
        matched.append(
            {
                "criterion_id": criterion.criterion_id,
                "description": criterion.description,
                "score": round(_clamp_score(best), 4),
                "weight": criterion.weight,
            }
        )

    normalized = _clamp_score(total)
    return {"score": normalized, "criteria": matched}


TASKS: List[ReviewTask] = [
    ReviewTask(
        task_id="pagination-regression",
        title="Review a pagination bug fix before release",
        difficulty="easy",
        objective="Identify whether the change really fixes the paging bug and call out any remaining production risk.",
        summary=(
            "A product engineer changed the pagination helper after customer reports that page 2 sometimes skips results. "
            "You are the final reviewer before the patch is merged."
        ),
        step_limit=4,
        starting_artifacts=("ticket", "helper_diff"),
        artifacts={
            "ticket": Artifact(
                artifact_id="ticket",
                kind="ticket",
                title="Support ticket",
                preview="Customers say page 2 repeats the first record and page 3 skips one row.",
                content=(
                    "Customer impact: exporting page 2 shows one duplicate row and drops one expected row. "
                    "The issue reproduces when page numbers are 1-indexed in the API layer."
                ),
            ),
            "helper_diff": Artifact(
                artifact_id="helper_diff",
                kind="file",
                title="utils/pagination.py diff",
                preview="Diff updates `start = page * page_size` to `(page - 1) * page_size`.",
                content=(
                    "@@ utils/pagination.py\n"
                    "- start = page * page_size\n"
                    "+ start = (page - 1) * page_size\n"
                    "  end = start + page_size\n"
                    "  return items[start:end]\n"
                ),
                reward=0.08,
            ),
            "test_log": Artifact(
                artifact_id="test_log",
                kind="test",
                title="Failing test excerpt",
                preview="One regression test still fails around invalid page numbers.",
                content=(
                    "FAILED tests/test_pagination.py::test_page_zero_returns_error\n"
                    "Expected ValueError('page must be >= 1') but helper returned items[-10:0]."
                ),
                reward=0.10,
            ),
        },
        grader=(
            GraderCriterion(
                criterion_id="page-zero-validation",
                description="Reviewer flags that page 0 or negative pages still slice from the end and need validation.",
                file_path="utils/pagination.py",
                severity="medium",
                weight=0.60,
                required_terms=(
                    {
                        "page 0",
                        "page zero",
                        "negative page",
                        "page must be >= 1",
                        "invalid page",
                    },
                    {"slice", "negative index", "items[-", "from the end"},
                ),
                recommendation_terms=(
                    {"validate", "guard", "reject", "raise"},
                    {"valueerror", "page must be >= 1", "before slicing"},
                ),
                preferred_artifacts=("helper_diff", "test_log"),
            ),
            GraderCriterion(
                criterion_id="off-by-one-context",
                description="Reviewer confirms why the 1-indexing fix is correct instead of rejecting it.",
                file_path="utils/pagination.py",
                severity="low",
                weight=0.35,
                required_terms=(
                    {"1-index", "1 indexed", "page - 1", "off by one"},
                    {"page 2", "duplicate", "skip", "customer"},
                ),
                recommendation_terms=({"keep", "correct", "right fix", "expected"},),
                preferred_artifacts=("ticket", "helper_diff"),
            ),
        ),
    ),
    ReviewTask(
        task_id="tenant-export-auth",
        title="Review a multi-tenant export endpoint",
        difficulty="medium",
        objective="Decide whether the export endpoint is safe for a SaaS deployment and report any merge blockers.",
        summary=(
            "Finance customers can export invoice CSVs. The team added a convenience endpoint for admins, "
            "but this service runs in a multi-tenant environment."
        ),
        step_limit=5,
        starting_artifacts=("pr_summary", "route_diff"),
        artifacts={
            "pr_summary": Artifact(
                artifact_id="pr_summary",
                kind="ticket",
                title="PR summary",
                preview="Adds `/api/admin/invoices/export` and reuses the invoice repository.",
                content=(
                    "Author note: internal support asked for a quick CSV export endpoint. "
                    "The route is only linked from the admin console."
                ),
            ),
            "route_diff": Artifact(
                artifact_id="route_diff",
                kind="file",
                title="api/admin_exports.py diff",
                preview="New route returns repository.export_csv(account_id=request.query_params['account_id']).",
                content=(
                    "@@ api/admin_exports.py\n"
                    "@router.get('/api/admin/invoices/export')\n"
                    "async def export_invoices(request):\n"
                    "    account_id = request.query_params['account_id']\n"
                    "    csv_bytes = await invoice_repo.export_csv(account_id=account_id)\n"
                    "    return Response(csv_bytes, media_type='text/csv')\n"
                ),
                reward=0.08,
            ),
            "auth_middleware": Artifact(
                artifact_id="auth_middleware",
                kind="file",
                title="middleware/authz.py",
                preview="Helpers expose `require_admin(request)` and `require_account_scope(request, account_id)`.",
                content=(
                    "def require_admin(request):\n"
                    "    if request.user.role != 'admin':\n"
                    "        raise Forbidden('admin role required')\n\n"
                    "def require_account_scope(request, account_id):\n"
                    "    if request.user.account_id != account_id and not request.user.is_global_admin:\n"
                    "        raise Forbidden('cross-tenant export denied')\n"
                ),
                reward=0.12,
            ),
            "security_policy": Artifact(
                artifact_id="security_policy",
                kind="policy",
                title="Tenant isolation policy",
                preview="All admin endpoints must validate both actor role and tenant/account scope.",
                content=(
                    "Security policy: multi-tenant admin features must enforce actor role and tenant scope. "
                    "Reading an arbitrary `account_id` from request parameters is not sufficient."
                ),
                reward=0.10,
            ),
        },
        grader=(
            GraderCriterion(
                criterion_id="missing-tenant-scope",
                description="Reviewer catches missing tenant scoping on account_id, creating a cross-tenant data leak.",
                file_path="api/admin_exports.py",
                severity="critical",
                weight=0.65,
                required_terms=(
                    {
                        "cross-tenant",
                        "tenant",
                        "account scope",
                        "data leak",
                        "authorization",
                    },
                    {"query param", "account_id", "untrusted", "arbitrary"},
                ),
                recommendation_terms=(
                    {"require_account_scope", "tenant check", "scope", "authorize"},
                    {"before export", "request.user.account_id", "is_global_admin"},
                ),
                preferred_artifacts=(
                    "route_diff",
                    "auth_middleware",
                    "security_policy",
                ),
            ),
            GraderCriterion(
                criterion_id="missing-admin-gate",
                description="Reviewer notes that the route never calls require_admin, so any authenticated user could hit it if routed.",
                file_path="api/admin_exports.py",
                severity="high",
                weight=0.30,
                required_terms=(
                    {"require_admin", "admin role", "admin gate", "privilege"},
                    {"missing", "not called", "no check", "unguarded"},
                ),
                recommendation_terms=(
                    {"call require_admin", "guard", "before reading params"},
                ),
                preferred_artifacts=("route_diff", "auth_middleware"),
            ),
        ),
    ),
    ReviewTask(
        task_id="refund-idempotency",
        title="Review a refund worker retry patch",
        difficulty="hard",
        objective="Determine whether the retry patch is safe under real payment retries and concurrent workers.",
        summary=(
            "Payments saw duplicate refunds during a processor outage. An engineer added a retry path in the refund worker. "
            "You need to decide if the patch prevents another incident."
        ),
        step_limit=6,
        starting_artifacts=("incident_ticket", "worker_diff"),
        artifacts={
            "incident_ticket": Artifact(
                artifact_id="incident_ticket",
                kind="ticket",
                title="Incident summary",
                preview="Processor timeout caused the worker to retry refunds; some customers were refunded twice.",
                content=(
                    "Root cause under investigation: payment processor timed out after accepting the refund. "
                    "The worker retried and created a second refund because no durable idempotency key was recorded."
                ),
            ),
            "worker_diff": Artifact(
                artifact_id="worker_diff",
                kind="file",
                title="workers/refunds.py diff",
                preview="Patch wraps `payments.refund` in retry logic and writes status after success.",
                content=(
                    "@@ workers/refunds.py\n"
                    "async def process_refund(job):\n"
                    "    try:\n"
                    "        result = await payments.refund(job.charge_id, amount=job.amount)\n"
                    "        await db.execute('UPDATE refunds SET status = ? WHERE id = ?', ['sent', job.id])\n"
                    "        return result\n"
                    "    except TimeoutError:\n"
                    "        return await payments.refund(job.charge_id, amount=job.amount)\n"
                ),
                reward=0.08,
            ),
            "payment_client": Artifact(
                artifact_id="payment_client",
                kind="file",
                title="integrations/payments.py",
                preview="`refund` accepts an optional idempotency_key but callers rarely pass it.",
                content=(
                    "async def refund(charge_id, amount, idempotency_key=None):\n"
                    "    payload = {'charge_id': charge_id, 'amount': amount}\n"
                    "    headers = {}\n"
                    "    if idempotency_key:\n"
                    "        headers['Idempotency-Key'] = idempotency_key\n"
                    "    return await processor.post('/refunds', json=payload, headers=headers)\n"
                ),
                reward=0.12,
            ),
            "db_model": Artifact(
                artifact_id="db_model",
                kind="file",
                title="models/refund.py",
                preview="Rows include `idempotency_key`, `status`, and `processor_refund_id`.",
                content=(
                    "refunds(id, charge_id, amount, status, idempotency_key, processor_refund_id, updated_at)\n"
                    "status values: queued, sent, confirmed, failed\n"
                ),
                reward=0.08,
            ),
            "worker_log": Artifact(
                artifact_id="worker_log",
                kind="log",
                title="Worker log excerpt",
                preview="Two workers processed the same refund id during retry storm.",
                content=(
                    "worker-a refund_id=rf_102 timeout from processor after 30s\n"
                    "worker-b refund_id=rf_102 picked queued job after visibility timeout\n"
                    "both requests returned processor_refund_id values"
                ),
                reward=0.10,
            ),
            "regression_test": Artifact(
                artifact_id="regression_test",
                kind="test",
                title="Missing regression test note",
                preview="No test covers timeout-after-success or concurrent dequeue.",
                content=(
                    "TODO: add integration test where processor commits refund then times out, and a second worker replays the same job."
                ),
                reward=0.07,
            ),
        },
        grader=(
            GraderCriterion(
                criterion_id="retry-without-idempotency",
                description="Reviewer flags that the retry calls refund twice without a durable idempotency key.",
                file_path="workers/refunds.py",
                severity="critical",
                weight=0.50,
                required_terms=(
                    {
                        "idempotency",
                        "duplicate refund",
                        "same refund twice",
                        "processor accepted",
                    },
                    {"timeout", "retry", "second call", "replay"},
                ),
                recommendation_terms=(
                    {"idempotency_key", "persist", "reuse"},
                    {"before calling processor", "db", "same key on retry"},
                ),
                preferred_artifacts=(
                    "worker_diff",
                    "payment_client",
                    "db_model",
                    "incident_ticket",
                ),
            ),
            GraderCriterion(
                criterion_id="status-update-race",
                description="Reviewer notes that status is updated only after the processor call, so concurrent workers can both send the refund.",
                file_path="workers/refunds.py",
                severity="high",
                weight=0.30,
                required_terms=(
                    {
                        "concurrent",
                        "two workers",
                        "race",
                        "visibility timeout",
                        "picked queued job",
                    },
                    {"status", "after success", "not locked", "before call"},
                ),
                recommendation_terms=(
                    {
                        "claim",
                        "lock",
                        "transaction",
                        "update status first",
                        "compare-and-set",
                    },
                ),
                preferred_artifacts=("worker_diff", "worker_log"),
            ),
            GraderCriterion(
                criterion_id="missing-regression-test",
                description="Reviewer asks for a test covering timeout-after-success and replay.",
                file_path="workers/refunds.py",
                severity="medium",
                weight=0.15,
                required_terms=(
                    {"test", "regression", "integration test"},
                    {
                        "timeout after success",
                        "replay",
                        "concurrent worker",
                        "duplicate refund",
                    },
                ),
                recommendation_terms=({"add", "cover", "simulate"},),
                preferred_artifacts=("regression_test",),
            ),
        ),
    ),
]


TASKS_BY_ID = {task.task_id: task for task in TASKS}


# ──────────────────────────────────────────────────────────────────────────────
# Compatibility shims β€” used by environment.py, app.py, and inference.py
# ──────────────────────────────────────────────────────────────────────────────


def list_tasks() -> List[Dict[str, object]]:
    """Return lightweight task metadata list (for /tasks endpoint)."""
    return [
        {
            "task_id": t.task_id,
            "difficulty": t.difficulty,
            "title": t.title,
            "objective": t.objective,
            "step_limit": t.step_limit,
        }
        for t in TASKS
    ]


def get_task(task_id: str) -> Dict[str, object]:
    """Return a task as a plain dict by ID. Raises ValueError if unknown."""
    if task_id not in TASKS_BY_ID:
        raise ValueError(
            f"Unknown task '{task_id}'. Available: {list(TASKS_BY_ID.keys())}"
        )
    t = TASKS_BY_ID[task_id]

    # Build the dict shape expected by environment.py
    return {
        "task_id": t.task_id,
        "difficulty": t.difficulty,
        "title": t.title,
        "objective": t.objective,
        "summary": t.summary,
        "step_limit": t.step_limit,
        "filename": _primary_file(t),
        "language": "python",
        "patch": _primary_diff(t),
        "artifacts": [
            {
                "artifact_id": a.artifact_id,
                "kind": a.kind,
                "title": a.title,
                "preview": a.preview,
                "content": a.content,
            }
            for a in t.artifacts.values()
        ],
        # Keep the native grader criteria accessible
        "_grader": t.grader,
    }


def grade_submission(
    task_id: str,
    review_text: str,
    findings: List[Dict] = None,
) -> Dict[str, object]:
    """
    Deterministic grader shim β€” bridges raw text / dict findings to grade_findings().

    Returns a dict with at minimum: {"score": float, "task_id": str, "difficulty": str}
    Score is always in (0.05, 0.95).
    """
    if task_id not in TASKS_BY_ID:
        raise ValueError(f"Unknown task '{task_id}'.")

    from codereview_env.models import ReviewFinding

    task = TASKS_BY_ID[task_id]

    # Convert dict findings to ReviewFinding objects where possible
    rf_list: List[ReviewFinding] = []
    if findings:
        for f in findings:
            try:
                rf_list.append(ReviewFinding(**f))
            except Exception:
                # If the dict doesn't match the schema, synthesise a minimal finding
                try:
                    rf_list.append(
                        ReviewFinding(
                            title=f.get("title", "finding"),
                            file_path=f.get("file_path", _primary_file(task)),
                            line_hint=f.get("line_hint"),
                            severity=f.get("severity", "medium"),
                            rationale=f.get("rationale", review_text[:200]),
                            recommendation=f.get("recommendation", "see rationale"),
                        )
                    )
                except Exception:
                    pass  # Skip unparseable findings

    # If no structured findings, synthesise one from the review_comment text
    if not rf_list and review_text and review_text.strip():
        try:
            rf_list.append(
                ReviewFinding(
                    title="Free-text review",
                    file_path=_primary_file(task),
                    line_hint=None,
                    severity="medium",
                    rationale=review_text[:500],
                    recommendation=review_text[:200],
                )
            )
        except Exception:
            pass

    # Determine which artifacts were "opened" (inferred from review text mentioning artifact IDs)
    text_lower = (review_text or "").lower()
    opened: Set[str] = {
        aid
        for aid in task.artifacts
        if aid.replace("_", " ") in text_lower or aid in text_lower
    }

    result = grade_findings(task, rf_list, opened)
    result["task_id"] = task_id
    result["difficulty"] = task.difficulty
    result["keyword_matches"] = sum(
        1 for c in result.get("criteria", []) if c["score"] > 0
    )
    result["total_keywords"] = len(result.get("criteria", []))
    return result


# ── Internal helpers ──────────────────────────────────────────────────────────


def _primary_file(task: "ReviewTask") -> str:
    """Return the first grader criterion's file_path as the 'primary' file."""
    if task.grader:
        return task.grader[0].file_path
    return "unknown"


def _primary_diff(task: "ReviewTask") -> str:
    """Return the content of the first 'file' artifact as the primary diff."""
    for a in task.artifacts.values():
        if a.kind == "file":
            return a.content
    # Fallback: concatenate all artifact contents
    return "\n\n".join(a.content for a in task.artifacts.values())