File size: 5,013 Bytes
b1a18a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Build private git task packages using a public, dependency-only OCI image.

This generates task source, never writes a framework cache or launches compute.
"""
import argparse
import hashlib
import json
from pathlib import Path
import re
import shutil

ROOT = Path(__file__).resolve().parent
TASKS = ('ds2-task_00687_c4c7ea', 'ds2-task_12106_bd7f8d')
TRAIN_TASKS = TASKS + ('task_00002_aa2b6f', 'task_00004_88fabf')
# Exact source suffixes: retain the original setup bytes alongside the derived
# setup, but remove redundant package installs from the executed copy.
INSTALL_SUFFIXES = {
    'ds2-task_00687_c4c7ea': b'pip install pytest 2>/dev/null\nmkdir -p /tests',
    'task_00002_aa2b6f': b'pip install pytest 2>/dev/null || apt-get update && apt-get install -y python3-pip && pip install pytest',
}
BOOTSTRAP = '''#!/bin/bash
set -eu
# Dependencies are immutable image contents. No per-episode package installs.
mkdir -p /app /tests /solution
/opt/verifier_venv/bin/python -c 'from importlib.metadata import version; assert version("pytest") == "8.4.1"; assert version("numpy") == "1.26.4"; assert version("pytest-json-ctrf") == "0.3.5"'
command -v xxd >/dev/null
bash /app/.vf-task-setup.sh
# Preserve input fixtures. Remove only executable hidden assertions and sources.
rm -f /tests/*.py /solution/* /app/setup.sh /app/.vf-task-setup.sh /app/.vf-bootstrap.sh
rm -f /tmp/ds_setup.sh /tmp/setup.sh /setup.sh
'''


def lightweight_setup(task, original):
    suffix = INSTALL_SUFFIXES.get(task)
    if suffix is not None:
        assert original.endswith(suffix), f'Unreviewed installer suffix in {task}'
        return original[:-len(suffix)] + b'mkdir -p /tests\n'
    assert not re.search(rb'\b(?:pip|apt-get)\s+(?:install|update)\b', original), task
    return original


def prepare(output, image):
    assert re.fullmatch(r'[^\s@]+@sha256:[0-9a-f]{64}', image), 'An immutable OCI digest reference is required'
    manifest = {}
    for task in TRAIN_TASKS:
        source = ROOT.parent / ('data/prime-tasks' if task in TASKS else 'data/harbor') / task
        target = output / 'tasks' / task
        if target.exists():
            shutil.rmtree(target)
        shutil.copytree(source, target)
        shutil.rmtree(target / 'environment')
        (target / 'environment').mkdir()
        original = (source / 'environment/setup.sh').read_bytes()
        (target / 'environment/setup.sh').write_bytes(original)
        (target / 'environment/.vf-task-setup.sh').write_bytes(lightweight_setup(task, original))
        bootstrap = BOOTSTRAP
        if task == 'ds2-task_12106_bd7f8d':
            bootstrap += 'rm -f /app/fixcalc\n'
        (target / 'environment/.vf-bootstrap.sh').write_text(bootstrap)
        config = re.sub(r'^docker_image\s*=.*\n', '', (source / 'task.toml').read_text(), flags=re.MULTILINE)
        if '[agent]' not in config:
            config += '\n[agent]\ntimeout_sec = 1800\n'
        config = config.replace('[environment]\n', '[environment]\ndocker_image = ' + json.dumps(image) + '\n')
        config = config.replace('[agent]\n', '[environment.healthcheck]\ncommand = "bash /app/.vf-bootstrap.sh"\ntimeout_sec = 600\nretries = 1\nstart_period_sec = 0\n[agent]\n')
        if task == 'ds2-task_00687_c4c7ea':
            command = 'if [ -L /app/topk ] && [ "$(readlink /app/topk)" = /app/topk.py ]; then ln -sfn topk.py /app/topk; fi'
            config += '\n[[verifier.collect]]\ncommand = ' + json.dumps(command) + '\ntimeout_sec = 10\n'
        (target / 'task.toml').write_text(config)
        for relative in ('instruction.md', 'tests/test.sh', 'tests/test_outputs.py', 'solution/solve.sh', 'environment/setup.sh'):
            assert (target / relative).read_bytes() == (source / relative).read_bytes(), relative
        manifest[task] = {str(p.relative_to(target)): hashlib.sha256(p.read_bytes()).hexdigest()
                          for p in sorted(target.rglob('*')) if p.is_file()}
    rows = [{'name': name, 'version': '1', 'description': description,
             'tasks': [{'name': task, 'path': 'tasks/' + task} for task in cohort]}
            for name, description, cohort in [
                ('workspace-four', 'Four pinned workspace coding tasks with isolated original correctness graders.', TRAIN_TASKS),
                ('workspace-two', 'Two-task evaluation cohort.', TASKS)]]
    (output / 'registry.json').write_text(json.dumps(rows, indent=2) + '\n')
    return manifest


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('--output', type=Path, required=True, help='Private task git checkout; never a Harbor cache')
    parser.add_argument('--dependency-image', required=True, help='Dependency-only OCI reference pinned by sha256')
    args = parser.parse_args()
    args.output.mkdir(parents=True, exist_ok=True)
    manifest = prepare(args.output, args.dependency_image)
    print(json.dumps({'tasks': sorted(manifest), 'image': args.dependency_image}))


if __name__ == '__main__':
    main()