smolscript / validate.py
appvoid's picture
Set default benchmark to fixed 350-task edition (0.3.0)
53cafbc verified
Raw History Blame Contribute Delete
3.77 kB
"""Validate structure, then run every reference and negative control in Docker."""
import argparse
import json
from pathlib import Path
from validators.dataset import check,load
from runners import get_runner
from runners.sandbox import Sandbox,SandboxUnavailable
ROOT=Path(__file__).resolve().parent
def main():
p=argparse.ArgumentParser();p.add_argument('--static-only',action='store_true');p.add_argument('--image',default='smolscript-runtime:0.1.0');p.add_argument('--engine',default='docker');p.add_argument('--output',type=Path,default=ROOT/'validation_report.json');a=p.parse_args()
report=check(ROOT);report['reference_execution']='unverified';report['negative_controls']='unverified'
try:
if a.static_only:
report['remaining']='All 350 references and negative controls require sandbox execution.';return 0
sandbox=Sandbox(a.image,a.engine)
report['sandbox']=sandbox.identity
report['runtimes']=sandbox.run({'diagnostic':True},60)['runtimes']
if not all(report['runtimes'].values()):raise SandboxUnavailable('Missing language runtime')
hidden={r['id']:r for r in load(ROOT/'private_tests/hidden.jsonl')}
refs={r['id']:r['reference_solution'] for r in load(ROOT/'references/solutions.jsonl')}
results=[]
from generators.code import wrap
for row in load(ROOT/'data/smolscript.jsonl'):
rid=row['id'];runner=get_runner(row['language']);family=row['metadata']['family']
result=runner.evaluate(row,refs[rid],hidden[rid],sandbox)
if family in ('html','css'):wrong=''
elif family=='dom':wrong='function setup() {}'
else:
wrong=wrap(row['language'],family,[('return '+('"__wrong__"' if family=='string' else '-999999')+(';' if row['language'] not in ('python','lua') else ''))])
negative=runner.evaluate(row,wrong,hidden[rid],sandbox)
# Also reject a plausible near-miss: keep only the first composition
# stage, or remove the map/filter in collection tasks.
mutant=None
if family in ('scalar','string','array'):
from generators.code import render
spec=dict(hidden[rid]['spec'])
if family in ('scalar','string'):spec['steps']=spec['steps'][:1]
elif spec.get('map'):spec.pop('map')
else:spec['filter']='all' if spec['filter']!='all' else 'positive'
from generators.specs import oracle
if any(oracle(spec,t['input'])!=t['expected'] for t in hidden[rid]['tests']):
mutant=runner.evaluate(row,render(row['language'],spec),hidden[rid],sandbox)
results.append({'id':rid,'reference_passed':result['passed'],'negative_rejected':not negative['passed'],
'near_miss_rejected':not mutant['passed'] if mutant is not None else None})
report['execution_results']=results
if not result['passed'] or negative['passed'] or (mutant and mutant['passed']):
raise ValueError('Reference or negative control failed: '+rid)
if len(results)%25==0:
print(f'Validated {len(results)}/350',flush=True);a.output.write_text(json.dumps(report,indent=2)+'\n')
report['reference_execution']='passed';report['negative_controls']='passed';return 0
except SandboxUnavailable as e:
report['blocked_reason']=str(e);report['unverified_languages']=['python','javascript','html','css','cpp','c','lua','csharp','rust'];print(str(e));return 2
except Exception as e:report['error']=str(e);raise
finally:a.output.write_text(json.dumps(report,indent=2)+'\n')
if __name__=='__main__':raise SystemExit(main())