File size: 9,908 Bytes
d044abe
 
 
 
 
 
 
 
 
 
 
4106468
d044abe
 
 
53cafbc
d044abe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4106468
d044abe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4106468
 
 
 
 
 
 
 
 
53cafbc
 
 
 
 
 
 
 
 
4106468
 
 
d044abe
 
 
 
 
 
 
 
 
234f07a
 
53cafbc
4106468
 
 
d044abe
 
 
53cafbc
d044abe
 
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
"""Deterministically build 2,000 task records, references and separate hidden tests."""
import hashlib
import json
from pathlib import Path
import sys
ROOT=Path(__file__).resolve().parents[1]
sys.path.insert(0,str(ROOT))
from generators.specs import task_specs,oracle,probes
from generators.code import render,signature
from generators.web import html_tasks,css_tasks,dom_tasks,BASE_CSS

FULL_ALLOCATION={'python':(150,75,25),'javascript':(150,75,25),'html':(120,60,20),'css':(120,60,20),
            'cpp':(135,68,22),'c':(135,68,22),'lua':(120,60,20),'csharp':(135,67,23),'rust':(135,67,23)}
NAMES={'python':'Python','javascript':'JavaScript','html':'HTML','css':'CSS','cpp':'C++','c':'C','lua':'Lua','csharp':'C#','rust':'Rust'}
VERSION='smolscript-0.1.0'
from generators.subset import ALLOCATION,ALLOCATION_1000,select,VERSION as DEFAULT_VERSION

def stable(value):return json.dumps(value,sort_keys=True,separators=(',',':'),ensure_ascii=False)
def sha(value):return hashlib.sha256(stable(value).encode()).hexdigest()
def write_rows(path,rows):path.write_text(''.join(stable(x)+'\n' for x in rows))

def build():
    public=[];private=[];references=[];specs=task_specs()
    suites={}
    for kind in ('scalar','string','array'):
        candidates=[s for group in specs.values() for s in group if s['kind']==kind]
        pool=probes(kind)
        chosen=([-120,-11,-10,-5,-2,-1,0,1,2,5,10,11,120] if kind=='scalar' else pool[:13])+pool[-8:]
        while True:
            groups={}
            for spec in candidates:
                key=tuple(oracle(spec,v) for v in chosen)
                groups.setdefault(key,[]).append(spec)
            collisions=[g for g in groups.values() if len(g)>1]
            if not collisions:break
            def discrimination(v):
                return sum(len(set(oracle(spec,v) for spec in group))-1 for group in collisions)
            best=max(pool,key=discrimination)
            if discrimination(best)==0:raise ValueError('Cannot distinguish hidden tasks')
            chosen.append(best)
        suites[kind]=chosen
    for lang,counts in FULL_ALLOCATION.items():
        items=[]
        if lang in ('html','css'):items=html_tasks() if lang=='html' else css_tasks()
        else:
            for difficulty,count in zip(('easy','medium','moderate'),counts):
                if lang=='javascript' and difficulty=='medium':count-=15
                if lang=='javascript' and difficulty=='moderate':count-=5
                for spec in specs[difficulty][:count]:items.append({'difficulty':difficulty,'spec':spec})
            if lang=='javascript':items+=dom_tasks()
        for i,item in enumerate(items,1):
            spec=item['spec'];kind=spec['kind'];difficulty=item['difficulty']
            rid=f'smolscript-{lang}-{i:04d}'
            metadata={'protocol':VERSION,'family':kind,'semantic_id':sha(spec),'input_domain':None}
            if kind in ('scalar','string','array'):
                from generators.specs import instruction
                prompt=f'In {NAMES[lang]}, '+instruction(spec)
                sig=signature(lang,kind)
                output_format='Return only the function '+sig+(' inside public class Solution.' if lang=='csharp' else '. Do not include a main program.')
                metadata['input_domain']={'scalar':'Integer n from -120 to 120.','string':'ASCII s of at most 80 characters, without newlines.','array':'At most eight integers, each from -6 to 6.'}[kind]
                reference=render(lang,spec)
                values=probes(kind)
                # Public examples do not use the hidden edge/adversarial inputs.
                public_values={'scalar':[3,-2],'string':['Hello','  Code '],'array':[[1,2,3],[-2,0,2]]}[kind]
                public_tests=[{'input':v,'expected':oracle(spec,v)} for v in public_values]
                selected=[v for v in suites[kind] if v not in public_values]
                hidden=[{'input':v,'expected':oracle(spec,v)} for v in selected]
                category={'scalar':'arithmetic_conditions','string':'strings','array':'collections_loops'}[kind]
                if spec.get('reduce')=='frequency':category='frequency_tables'
                entry='Solve' if lang=='csharp' else 'solve'
            else:
                prompt='In '+NAMES[lang]+', '+item['prompt'];reference=item['reference'];entry='setup' if kind=='dom' else ''
                output_format={'html':'Return an HTML fragment only.','css':'Return a stylesheet only.','dom':'Return function setup() only. The harness calls it once after loading the fixture.'}[kind]
                public_tests=[];category={'html':'semantic_structure','css':'styles_layout','dom':'dom_events'}[kind]
                metadata['fixture']=item['fixture']
                metadata['base_css']=BASE_CSS if kind=='css' else ''
                hidden=[{'checks':item['checks'],'steps':item.get('steps',[])}]
                if kind=='dom':
                    # A no-op toggler must not pass merely because two toggles cancel.
                    from generators.web import DOM_ACTIONS
                    action=spec['action'];check=item['checks']
                    initial=[{'kind':'text','selector':'#output','value':'0'}, {'kind':'value','selector':'#input','value':''},
                      {'kind':'count','selector':'#items > li','value':0}, {'kind':'property','selector':'#input','name':'disabled','value':False},
                      {'kind':'property','selector':'#output','name':'hidden','value':False}, {'kind':'property','selector':'#check','name':'checked','value':False},
                      {'kind':'attribute','selector':'#output','name':'class','value':None},{'kind':'attribute','selector':'#output','name':'title','value':None}]
                    hidden.insert(0,{'checks':initial,'steps':[]})
                    if action=='toggle_class':hidden.append({'steps':[{'action':'click','selector':'#action'}],'checks':[{'kind':'attribute','selector':'#output','name':'class','value':'active'}]})
                    # Nonconstant input-dependent behaviors also tested with empty input.
                    expected={'mirror':'','uppercase':'','length':'0','reverse':'','mark_empty':'Empty'}
                    if action in expected:
                        steps=[{'action':'event','selector':'#input','value':'input'}] if spec['event']=='input' else [{'action':'click','selector':'#action'}]
                        hidden.append({'steps':steps,'checks':[{'kind':'text','selector':'#output','value':expected[action]}]})
            row={'id':rid,'language':lang,'difficulty':difficulty,'category':category,'prompt':prompt,
                 'output_format':output_format,'entry_point':entry,'reference_solution':None,'tests':public_tests,
                 'timeout_seconds':3,'metadata':metadata}
            public.append(row);private.append({'id':rid,'tests':hidden,'spec':spec});references.append({'id':rid,'reference_solution':reference})
    assert len(public)==2000
    # Preserve the original edition outside the default HF split.
    archive=ROOT/'archive/v0.1.0'
    archive.mkdir(parents=True,exist_ok=True)
    write_rows(archive/'smolscript.jsonl',public)
    write_rows(archive/'hidden.jsonl',private)
    write_rows(archive/'solutions.jsonl',references)
    import pyarrow as pa
    import pyarrow.parquet as pq
    pq.write_table(pa.Table.from_pylist([{**r,'tests':stable(r['tests']),'metadata':stable(r['metadata'])} for r in public]),archive/'smolscript.parquet',compression='zstd')
    previous=select(public,ALLOCATION_1000,'smolscript-0.2.0')
    previous_ids={r['id'] for r in previous}
    previous_archive=ROOT/'archive/v0.2.0'
    previous_archive.mkdir(parents=True,exist_ok=True)
    write_rows(previous_archive/'smolscript.jsonl',previous)
    write_rows(previous_archive/'hidden.jsonl',[r for r in private if r['id'] in previous_ids])
    write_rows(previous_archive/'solutions.jsonl',[r for r in references if r['id'] in previous_ids])
    pq.write_table(pa.Table.from_pylist([{**r,'tests':stable(r['tests']),'metadata':stable(r['metadata'])} for r in previous]),previous_archive/'smolscript.parquet',compression='zstd')
    public=select(previous)
    ids={r['id'] for r in public}
    private=[r for r in private if r['id'] in ids]
    references=[r for r in references if r['id'] in ids]
    for directory in ('data','private_tests','references'): (ROOT/directory).mkdir(exist_ok=True)
    write_rows(ROOT/'data/smolscript.jsonl',public)
    write_rows(ROOT/'private_tests/hidden.jsonl',private)
    write_rows(ROOT/'references/solutions.jsonl',references)
    import pyarrow as pa
    import pyarrow.parquet as pq
    # HF-compatible nested metadata; JSON strings give consistent test schemas across modalities.
    parquet_rows=[{**r,'tests':stable(r['tests']),'metadata':stable(r['metadata'])} for r in public]
    pq.write_table(pa.Table.from_pylist(parquet_rows),ROOT/'data/smolscript.parquet',compression='zstd')
    from generation_settings import PROTOCOL
    manifest={'evaluation_protocol':PROTOCOL,'version':DEFAULT_VERSION,'examples':350,'languages':{k:sum(v) for k,v in ALLOCATION.items()},
              'difficulty':{'easy':210,'medium':105,'moderate':35},'generator_seed':7301,
              'validation_status':'execution_unverified','selection_protocol':DEFAULT_VERSION,
              'selected_ids':[r['id'] for r in public],
              'selection_sha256':hashlib.sha256(('\n'.join(r['id'] for r in public)+'\n').encode()).hexdigest(),'files':{}}
    for name in ('data/smolscript.jsonl','data/smolscript.parquet','private_tests/hidden.jsonl','references/solutions.jsonl'):
        manifest['files'][name]=hashlib.sha256((ROOT/name).read_bytes()).hexdigest()
    (ROOT/'manifest.json').write_text(json.dumps(manifest,indent=2)+'\n')
    print('Generated fixed 350-example default; archived original 2000 tasks.')

if __name__=='__main__':build()