File size: 2,666 Bytes
10d6c29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Offline structural validation only; external rubric judge assigns 0–7 points."""
import json, os, stat
LIMIT = 200_000

def read_own(path):
    """Reject links, special files, multiple hard links, and oversized files."""
    parts = os.path.abspath(path).split('/')[1:]
    folder = os.open('/', os.O_RDONLY | os.O_DIRECTORY)
    try:
        for part in parts[:-1]:
            inner = os.open(part, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW, dir_fd=folder)
            os.close(folder)
            folder = inner
        fd = os.open(parts[-1], os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK, dir_fd=folder)
    finally:
        os.close(folder)
    with os.fdopen(fd, 'rb') as stream:
        info = os.fstat(stream.fileno())
        if not stat.S_ISREG(info.st_mode) or info.st_nlink != 1:
            raise ValueError('not a regular file with one link')
        data = stream.read(LIMIT + 1)
    if len(data) > LIMIT:
        raise ValueError('file exceeds 200000 bytes')
    return data

def write_new(path, data):
    try: os.unlink(path)
    except FileNotFoundError: pass
    fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW, 0o644)
    with os.fdopen(fd, 'wb') as f: f.write(data)

def main():
    out = os.environ['VERIFIER_DIR']
    os.makedirs(out, exist_ok=True)
    write_new(os.path.join(out, 'reward.txt'), b'0\n')
    proof_path = os.path.join(out, 'proof.md')
    try: os.unlink(proof_path)
    except FileNotFoundError: pass
    reference = open(os.path.join(os.environ['TESTS_DIR'], 'reference.json'), 'rb').read()
    json.loads(reference)
    write_new(os.path.join(out, 'reference.json'), reference)
    status, count, reason = 'missing', 0, 'proof unavailable or invalid'
    try:
        raw = read_own(os.environ['PROOF'])
        proof = raw.decode('utf-8', 'strict')
        count = len(proof)
        write_new(proof_path, raw)
        if sum(not c.isspace() for c in proof) < 20:
            status, reason = 'empty', 'fewer than 20 non-whitespace characters'
        else:
            status, reason = 'submitted_for_judge', 'structurally valid submission'
    except (OSError, ValueError, UnicodeError) as e:
        reason = 'proof unavailable or invalid: ' + str(e)[:200]
    result = {'status': status, 'char_count': count, 'requires_llm_judge': True, 'max_score': 7, 'reason': reason}
    write_new(os.path.join(out, 'grade.json'), (json.dumps(result, ensure_ascii=False, indent=2)+'\n').encode())
    if status == 'submitted_for_judge': write_new(os.path.join(out, 'reward.txt'), b'1\n')
    print(status, 'structural reward:', int(status == 'submitted_for_judge'))
if __name__ == '__main__': main()