File size: 9,485 Bytes
730588c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""V2: 12 families x 4 repair patterns x 25 reproducible case sets.

The published 1,200 rows are episodes, not 1,200 unrelated algorithms.
Arena trains on 50 explicitly listed representatives, not every dataset row.
"""
import copy
import json
import random
import runpy
from pathlib import Path

ROOT = Path(__file__).resolve().parent
SETS = 25
MUTATIONS = {
    'merge-intervals': [
        ('touching-boundary', 'a <= out[-1][1]', 'a < out[-1][1]'),
        ('nested-endpoint', 'max(b, out[-1][1])', 'min(b, out[-1][1])'),
        ('unsorted-input', 'for a,b in sorted(intervals):', 'for a,b in intervals:')],
    'moving-sums': [
        ('zero-window', 'k <= 0', 'k < 0'),
        ('window-width', 'values[i:i+k]', 'values[i:i+k-1]'),
        ('missing-first-window', 'range(len(values)-k+1)', 'range(1,len(values)-k+1)')],
    'weighted-mean': [
        ('unweighted-numerator', 'x*w', 'x'),
        ('wrong-denominator', 'total = sum(weights)', 'total = len(weights)'),
        ('zero-weight-result', 'if total else None', 'if total else 0')],
    'stable-unique': [
        ('reverse-traversal', 'for x in values:', 'for x in reversed(values):'),
        ('reverse-output', 'out.append(x)', 'out.insert(0,x)'),
        ('wrong-seen-value', 'seen.add(x)', 'seen.add(-x)')],
    'run-length': [
        ('inverted-grouping', 'out[-1][0] == c', 'out[-1][0] != c'),
        ('double-increment', 'out[-1][1] += 1', 'out[-1][1] += 2'),
        ('zero-initial-count', 'out.append([c,1])', 'out.append([c,0])')],
    'luhn': [
        ('wrong-parity', 'if i%2:', 'if not i%2:'),
        ('wrong-reduction', 'v -= 9', 'v -= 8'),
        ('wrong-modulus', 'total%10', 'total%9')],
    'edit-distance': [
        ('row-boundary', 'nxt = [i]', 'nxt = [0]'),
        ('inverted-substitution', '(x != y)', '(x == y)'),
        ('diagonal-index', 'row[j-1]+', 'row[j]+')],
    'meeting-rooms': [
        ('tie-order', 'sorted(events)', 'sorted(events,key=lambda item:(item[0],-item[1]))'),
        ('peak-tracking', 'best = max(best,active)', 'best = min(best,active)'),
        ('end-event-sign', 'events.append([b,-1])', 'events.append([b,1])')],
    'nearest-percentile': [
        ('floor-not-ceiling', '+99', '+0'),
        ('rank-index', '[rank-1]', '[rank-2]'),
        ('descending-order', 'sorted(values)', 'sorted(values,reverse=True)')],
    'fifo-inventory': [
        ('zero-lot-retention', 'if q-used:', 'if q >= used:'),
        ('over-consumption', 'used = min(q,demand)', 'used = max(q,demand)'),
        ('unit-cost-not-quantity', 'cost += used*p', 'cost += p')],
    'csv-row': [
        ('quoted-comma', "elif c == ',' and not quoted:", "elif c == ',':"),
        ('escaped-quote', "field += '\"'", "field += '\"\"'"),
        ('lost-final-field', '    out.append(field)\n    return out', '    if field: out.append(field)\n    return out')],
    'shortest-path': [
        ('farthest-first', 'n = min(candidates', 'n = max(candidates'),
        ('lost-path-prefix', 'd = dist[n]+w', 'd = w'),
        ('missing-relaxation', 'if nxt not in dist or d < dist[nxt]:', 'if nxt not in dist:')],
}


def extra_inputs(family, rng):
    """Distinct boundary instances, including failures absent from random pools."""
    out = []
    for i in range(20):
        a = rng.randrange(-40, 40)
        n = rng.randrange(3, 13)
        if family == 'merge-intervals':
            out.extend([[[[a+3,a+8],[a,a+3]]], [[[a,a+8],[a+1,a+2]]]])
        elif family == 'moving-sums':
            v = [rng.randrange(-30,31) for _ in range(n)]
            out.extend([[v,0], [v,1], [v,2]])
        elif family == 'weighted-mean':
            out.extend([[[a,a+7],[0,0]], [[a,a+7],[1,5]]])
        elif family == 'stable-unique':
            out.append([[a+2,a,a+2,a+1,a]])
        elif family == 'run-length':
            out.append([rng.choice('ab猫')*(i+2)+rng.choice('cd狗')*(i+1)])
        elif family == 'luhn':
            prefix = str(rng.randrange(10**5,10**8))
            total = 0
            for j,c in enumerate(reversed(prefix+'0')):
                digit = int(c)
                if j%2:
                    digit *= 2
                    if digit > 9: digit -= 9
                total += digit
            check = (-total)%10
            out.extend([[prefix+str(check)], [prefix+str((check+1)%10)]])
        elif family == 'edit-distance':
            text = ''.join(rng.choice('abc猫') for _ in range(n))
            out.extend([[text,''], [text,text], [text,text[::-1]]])
        elif family == 'meeting-rooms':
            out.extend([[[[a,a+2],[a+2,a+4]]], [[[a,a+8],[a+1,a+3],[a+2,a+7]]]])
        elif family == 'nearest-percentile':
            vals = list(range(a,a+2*n+1))
            rng.shuffle(vals)
            out.extend([[vals,33], [vals,50], [vals,99]])
        elif family == 'fifo-inventory':
            p = rng.randrange(1,500)
            out.extend([[[[0,p],[n,p+1]],0], [[[n,p],[n+1,p+2]],n-1]])
        elif family == 'csv-row':
            out.extend([[f'x{i},'], [f'"x{i},y",z'], [f'"x{i}""y",z']])
        elif family == 'shortest-path':
            # First-discovered route is not cheapest; later relaxation matters.
            prefix = rng.randrange(1,500)+i
            out.append([{'a':[['b',prefix+n],['c',prefix]],'c':[['b',1],['d',prefix+n+7]],'b':[['d',2]],'d':[]},'a','d'])
    return out


def build():
    tasks, solutions = [], {}
    patterns = {}
    for cohort in range(SETS):
        seed = 20261008 + 7919*cohort
        base = runpy.run_path(str(ROOT/'generate_tasks.py'),
                             init_globals={'CURRICULUM_SEED':seed}, run_name='base_curriculum')
        for original in base['tasks']:
            family = original['task_id']
            gold = base['solutions'][family]
            scope = {}
            exec(gold, scope)
            cases = copy.deepcopy(original['cases'])
            for args in extra_inputs(family, random.Random(seed+sum(map(ord,family)))):
                cases.append({'args':args,'expected':scope['solve'](*copy.deepcopy(args))})
            # Never inflate case counts with duplicated input instances.
            unique = {json.dumps(c['args'],ensure_ascii=False,sort_keys=True):c for c in cases}
            cases = list(unique.values())
            starters = [('baseline', original['starter'])]
            for label, before, after in MUTATIONS[family]:
                assert gold.count(before)==1, (family,label,before)
                starters.append((label,gold.replace(before,after)))
            patterns[family] = [name for name,_ in starters]
            for bug, (label, starter) in enumerate(starters):
                task_id = family if cohort==0 and bug==0 else f'{family}-b{bug}-s{cohort:02}'
                # Examples change with the cohort; this is visible training variation.
                chosen = random.Random(seed+bug).sample(cases, min(3,len(cases)))
                examples = [{'args':c['args'],'expected':c['expected']} for c in chosen]
                prompt = original['prompt']+'\nExamples (JSON args and expected result): '+json.dumps(examples,ensure_ascii=False)
                tasks.append({'task_id':task_id,'family':family,'bug_pattern':label,
                              'case_seed':seed,'prompt':prompt,'starter':starter,'cases':cases})
                solutions[task_id] = gold
    assert len(tasks)==1200 and len({t['task_id'] for t in tasks})==1200
    # All 48 patterns, distributed across cohorts, plus two further boundary-heavy episodes.
    representatives = []
    families = list(patterns)
    for index,family in enumerate(families):
        for bug in range(4):
            cohort = (index*2+bug*5)%SETS
            representatives.append(family if cohort==0 and bug==0 else f'{family}-b{bug}-s{cohort:02}')
    representatives += ['csv-row-b2-s24','shortest-path-b3-s24']
    assert len(set(representatives))==50
    selected = set(representatives)
    (ROOT/'tasks.json').write_text(json.dumps(tasks,ensure_ascii=False,separators=(',',':'))+'\n')
    (ROOT/'solutions.json').write_text(json.dumps(solutions,ensure_ascii=False,indent=2)+'\n')
    (ROOT/'tasks.jsonl').write_text(''.join(json.dumps({**t,'split':'train'},ensure_ascii=False,separators=(',',':'))+'\n' for t in tasks))
    manifest = {'version':2,'families':patterns,'case_sets_per_pattern':SETS,'rows':len(tasks),
                'distinct_bug_patterns':48,'cases_total':sum(len(t['cases']) for t in tasks),
                'arena_task_ids':representatives,'arena_task_count':50,
                'note':'Only the 50 listed representatives train in Arena; the remaining rows are reproducible public episodes.'}
    (ROOT/'curriculum.json').write_text(json.dumps(manifest,indent=2)+'\n')
    # Retain full canonical row data for download; use stable primitive columns in Arrow.
    viewer=[]
    for task in tasks:
        row={k:task[k] for k in ('task_id','family','bug_pattern','case_seed','prompt','starter')}
        row.update(split='train',arena_selected=task['task_id'] in selected,num_cases=len(task['cases']),
                   cases_json=json.dumps(task['cases'],ensure_ascii=False,separators=(',',':')))
        assert json.loads(row['cases_json'])==task['cases']
        viewer.append(row)
    (ROOT/'viewer_train.jsonl').write_text(''.join(json.dumps(row,ensure_ascii=False,separators=(',',':'))+'\n' for row in viewer))
    print(json.dumps({k:v for k,v in manifest.items() if k!='families'},indent=2))


if __name__=='__main__':
    build()