File size: 2,411 Bytes
1cb0c01
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Score bounds make incomplete verification visible; unresolved is never pass."""
import json
from pathlib import Path


class ReportError(ValueError):
    pass


def summarize(task_id, checks, provenance=None):
    if not checks or sum(c['points'] for c in checks) != 100:
        raise ReportError('Criterion points must sum to 100')
    if len({c['id'] for c in checks}) != len(checks):
        raise ReportError('Duplicate criterion IDs')
    lower = upper = assessed = 0
    unresolved = []
    for check in checks:
        points, earned = check['points'], check.get('earned_points')
        if not isinstance(points, (int, float)) or points <= 0:
            raise ReportError('Invalid criterion points')
        if earned is None:
            if check['status'] not in ('needs_review', 'verifier_error', 'unsupported', 'inspection_error'):
                raise ReportError('Resolved criterion missing earned points')
            upper += points
            unresolved.append(check['id'])
        else:
            if not 0 <= earned <= points:
                raise ReportError('Invalid earned points')
            lower += earned
            upper += earned
            assessed += points
    return {'schema_version': 1, 'task_id': task_id, 'checks': checks,
        'score': lower / 100 if not unresolved else None,
        'score_bounds': [lower / 100, upper / 100],
        'assessed_weight': assessed, 'unresolved_criteria': unresolved,
        'status': 'complete' if not unresolved else 'needs_review',
        'provenance': provenance or {}}


def write(report, directory):
    directory = Path(directory)
    directory.mkdir(parents=True, exist_ok=True)
    (directory / 'score.json').write_text(json.dumps(report, indent=2) + '\n')
    # Harbor scalar rewards must not silently turn an incomplete grade into a zero.
    # Callers leave old deployed reward untouched in diagnostic/shadow mode.
    if report['score'] is not None:
        (directory / 'reward.txt').write_text(f"{report['score']:.8f}\n")
    else:
        pending = directory / 'reward.txt'
        if pending.exists():
            pending.unlink()
        (directory / 'grading-pending.json').write_text(json.dumps({
            'reason': 'Unresolved criteria require calibration or inspection',
            'score_bounds': report['score_bounds'],
            'unresolved': report['unresolved_criteria']}, indent=2) + '\n')