omega-tool-model
First-party Omega tool-use model source, serving harnesses, and verification records
Brought by drakestapleton
omega-tool-model / pilot / check_behavior.py
| 1 | import os |
| 2 | """External checker. Generated actions are restricted to disposable fixtures.""" |
| 3 | from pathlib import Path |
| 4 | import json,re,subprocess,hashlib |
| 5 | |
| 6 | R=Path(__file__).parent |
| 7 | OMC=(Path(os.path.expanduser('~')) / '/handoffs/omega-llm-20261101/omega-mv/seed/out/omc'.lstrip('/')) |
| 8 | FIX=R/'fixtures';FIX.mkdir(exist_ok=True) |
| 9 | |
| 10 | # No general execution of model output. Admit only this bounded lesson grammar: |
| 11 | # integer returns/comparisons, one literal text, and a fixed stdout write. |
| 12 | safe_lines=[r'fn main to int',r'end',r'else',r'ret (?:int )?\d{1,3}', |
| 13 | r'ret add int \d{1,3} int \d{1,3}',r'if gt int \d{1,3} int \d{1,3}', |
| 14 | r'text msg "[A-Za-z0-9 ]{0,80}\\n"', |
| 15 | r'n = write int 1 msg ulong 0 len msg',r'if ne n long len msg'] |
| 16 | def codecheck(text,row,label): |
| 17 | src=R/f'receipts/generated-{label}-{row["kind"]}.om' |
| 18 | src.write_text(text+'\n') |
| 19 | if not text or any(not any(re.fullmatch(p,s) for p in safe_lines) for s in text.splitlines()): |
| 20 | return {'pass':False,'reason':'outside bounded executable lesson grammar'} |
| 21 | p=subprocess.run([str(OMC)],input=(text+'\n').encode(),capture_output=True,timeout=10) |
| 22 | if p.returncode: |
| 23 | return {'pass':False,'compile_rc':p.returncode,'compiler_message':p.stderr.decode()} |
| 24 | exe=R/f'build/generated-{label}-{row["kind"]}';exe.write_bytes(p.stdout);exe.chmod(0o700) |
| 25 | q=subprocess.run([str(exe)],capture_output=True,timeout=2,cwd=FIX) |
| 26 | return {'pass':q.returncode==row['expected_exit'] and q.stdout.decode()==row.get('expected_stdout',''), |
| 27 | 'compile_rc':0,'run_rc':q.returncode,'stdout':q.stdout.decode()} |
| 28 | def dispatch(text,row): |
| 29 | try:d=json.loads(text) |
| 30 | except Exception:return {'pass':False,'reason':'not one JSON request'} |
| 31 | expected=json.loads(row['answer']) |
| 32 | if d!=expected:return {'pass':False,'reason':'wrong tool, argument or extra fields','parsed':d} |
| 33 | # Exact expected relative path required before any filesystem operation. |
| 34 | path=FIX/d['path'];path.parent.mkdir(parents=True,exist_ok=True) |
| 35 | if d['tool']=='read_file': |
| 36 | path.write_text('fixture contents\n');result=path.read_text() |
| 37 | elif d['tool']=='list_files': |
| 38 | path.mkdir(exist_ok=True);(path/'one.txt').write_text('one') |
| 39 | result=sorted(x.name for x in path.iterdir()) |
| 40 | elif d['tool']=='omega_check': |
| 41 | path.write_text('fn main to int\nret 0\nend\n') |
| 42 | p=subprocess.run([str(OMC)],input=path.read_bytes(),capture_output=True,timeout=10) |
| 43 | assert p.returncode==0;result='compile succeeded' |
| 44 | else:raise AssertionError('unlisted tool') |
| 45 | return {'pass':True,'executed_only_on_fixture':True,'tool_result':result} |
| 46 | |
| 47 | def check(text,row,label): |
| 48 | text=text.split('\nDone.')[0].strip() |
| 49 | if 'expected_exit' in row:return codecheck(text,row,label) |
| 50 | if row['kind'] in ('read','list','check'):return dispatch(text,row) |
| 51 | return {'pass':text==row['answer'],'reason':'exact report comparison'} |
| 52 | |
| 53 | def main(): |
| 54 | rows=json.loads((R/'probes.json').read_text());out=[] |
| 55 | for label in ('baseline','selected'): |
| 56 | for row in rows: |
| 57 | raw=(R/f'receipts/sample-{label}-{row["kind"]}.log').read_text() |
| 58 | item={'model':label,'kind':row['kind'],'prompt':row['prompt'],'expected':row['answer'],'raw_output':raw} |
| 59 | item.update(check(raw,row,label));out.append(item) |
| 60 | # Checker negative controls ensure wrong arguments, wrong calculation and host access fail. |
| 61 | addrow=next(x for x in rows if x['kind']=='add') |
| 62 | assert not check('fn main to int\nret 99\nend',addrow,'control')['pass'] |
| 63 | assert not check('fn main to int\nr = fopen x 0 1 0 0\nret 0\nend',addrow,'control')['pass'] |
| 64 | rr=next(x for x in rows if x['kind']=='read') |
| 65 | assert not check('{"tool":"read_file","path":"/etc/passwd"}',rr,'control')['pass'] |
| 66 | result={'probes':out,'counts':{k:{'passed':sum(x['pass'] for x in out if x['model']==k),'total':sum(x['model']==k for x in out)} for k in ('baseline','selected')},'checker_negative_controls':'pass'} |
| 67 | (R/'behavior.json').write_text(json.dumps(result,indent=2)+'\n') |
| 68 | print(json.dumps(result['counts'])) |
| 69 | if __name__=='__main__':main() |