omega-tool-model
First-party Omega tool-use model source, serving harnesses, and verification records
Brought by drakestapleton
omega-tool-model / deployment / workbench / build.py
| 1 | """External build instrument. The deployed adapter is entirely native Omega.""" |
| 2 | from pathlib import Path |
| 3 | import base64,hashlib,json,subprocess |
| 4 | from specialize import specialize |
| 5 | R=Path(__file__).resolve().parent |
| 6 | C=R.parents[1]/'omega-llm-20261101/omega-mv/seed/out/omc' |
| 7 | assert hashlib.sha256(C.read_bytes()).hexdigest()=='2c82480596bbdc50cda9626866209ef662d5834f278f839ff4ce0250c495029f' |
| 8 | page=(R/'index.html').read_text() |
| 9 | page=page.replace('/* PRACTICE_SCRIPT */','const practiceJobs='+json.dumps(json.loads((R/'practice-jobs.json').read_text()),separators=(',',':'))+';\n'+(R/'practice.js').read_text()) |
| 10 | (R/'page.html').write_text(page) |
| 11 | script=page.split('<script>')[1].split('</script>')[0] |
| 12 | csp="default-src 'none'; script-src 'sha256-"+base64.b64encode(hashlib.sha256(script.encode()).digest()).decode()+"'; style-src 'unsafe-inline'; connect-src 'self'; base-uri 'none'; form-action 'none'; frame-ancestors 'none'" |
| 13 | common='Connection: close\r\nCache-Control: no-store\r\nX-Content-Type-Options: nosniff\r\nReferrer-Policy: no-referrer\r\nContent-Security-Policy: '+csp+'\r\n' |
| 14 | def response(status,body,ctype='text/plain; charset=utf-8'): |
| 15 | return f'HTTP/1.1 {status}\r\n'+common+f'Content-Type: {ctype}\r\nContent-Length: {len(body.encode())}\r\n\r\n'+body |
| 16 | responses={ |
| 17 | 'respondpage':response('200 OK',page,'text/html; charset=utf-8'), |
| 18 | 'respond400':response('400 Bad Request','Please send a short, nonempty request using this page.\n'), |
| 19 | 'respond404':response('404 Not Found','Page not found.\n'), |
| 20 | 'respond422':response('422 Unprocessable Content','This request is too long for the model. Please shorten it and try again.\n'), |
| 21 | 'respond502':response('502 Bad Gateway','The model response was interrupted. Please try again.\n'), |
| 22 | 'respond503':response('503 Service Unavailable','The model service is unavailable. Please try again shortly.\n'), |
| 23 | 'streamhead':'HTTP/1.1 200 OK\r\n'+common+'Content-Type: text/plain; charset=utf-8\r\nTransfer-Encoding: chunked\r\nX-Accel-Buffering: no\r\n\r\n', |
| 24 | 'streamend':'0\r\n\r\n'} |
| 25 | def omtext(s):return '"'+s.replace('\\','\\\\').replace('"','\\"').replace('\n','\\n').replace('\t','\\t')+'"' |
| 26 | generated=[] |
| 27 | jobs=json.loads((R/'jobs.json').read_text()) |
| 28 | generated.append('fn wjobload id ulong to void\n') |
| 29 | for i,j in enumerate(jobs): |
| 30 | kind={'add':0,'mul':1,'loop':2}[j['kind']] |
| 31 | goal=(f'calc starts with x and adds {j["k"]} exactly {j["n"]} times in a for loop. main calls calc with {j["a"]}.' if kind==2 else f'calc returns x {"plus" if kind==0 else "times"} {j["k"]}. main calls calc with {j["a"]}.') |
| 32 | generated.append(f'if eq id {i}\n') |
| 33 | for k,v in enumerate([kind,j['a'],j['k'],j.get('n',0),int(j['fault']=='missing')]):generated.append(f'put wjob {k} {v}\n') |
| 34 | for value,buf,length in [(j['path'],'wpath','wplen'),(goal,'wgoal','wglen')]: |
| 35 | generated.append(f'wz\nwt {omtext(value)}\nput {length} 0 get wlen 0\nfor i ulong get wlen 0\nput {buf} i get wbuf i\nend\n') |
| 36 | generated.append('ret\nend\n') |
| 37 | generated.append('end\n') |
| 38 | for index,(name,value) in enumerate(responses.items()): |
| 39 | # Build CRLF from LF in native code. Omega text escapes do not include CR. |
| 40 | textname=f't{index}';value=value.replace('\r\n','\n') |
| 41 | # Only HTTP framing needs CRLF. HTML/JS linefeeds stay LF. |
| 42 | if name=='respondpage': |
| 43 | header,body=value.split('\n\n',1) |
| 44 | generated.append(f'text {textname} {omtext(header+chr(10)+chr(10))}\ntext page {omtext(body)}\n') |
| 45 | else:generated.append(f'text {textname} {omtext(value)}\n') |
| 46 | generated.append(f'fn {name} to bool\np = ulong 0\nfor i ulong len {textname}\nc = get {textname} i\nif eq c 10\nput hbuf p 13\np = add p 1\nend\nput hbuf p c\np = add p 1\nend\n') |
| 47 | if name=='respondpage':generated.append('for i ulong len page\nput hbuf p get page i\np = add p 1\nend\n') |
| 48 | # Error body LF must not turn into CRLF beyond declared Content-Length. |
| 49 | elif name.startswith('respond'): |
| 50 | # Error messages only have the final newline. Drop inserted CR for this byte. |
| 51 | generated.append('put hbuf sub p 2 10\np = sub p 1\n') |
| 52 | generated.append('ret hsend p\nend\n') |
| 53 | matches={'isroot':'GET / HTTP/1.1','isgen':'POST /generate HTTP/1.1','isrepair':'POST /repair HTTP/1.1','iscl':'content-length:','iste':'transfer-encoding:','isguard':'x-omega-ui: 1','ishost':'host:'} |
| 54 | hosts=['host: 127.0.0.1:18173','host: localhost:18173','host: 127.0.0.1:18174','host: localhost:18174'] |
| 55 | matches.update({f'host{i}':h for i,h in enumerate(hosts)}) |
| 56 | for index,(name,value) in enumerate(matches.items()): |
| 57 | t=f'm{index}';exact=name not in ('iscl','iste','ishost') |
| 58 | generated.append(f'text {t} {omtext(value)}\nfn {name} a ulong b ulong to bool\nif {"ne" if exact else "lt"} sub b a len {t}\nret false\nend\nfor i ulong len {t}\nif ne get hreq add a i get {t} i\nret false\nend\nend\nret true\nend\n') |
| 59 | generated.append('fn goodhost a ulong b ulong to bool\nret or or host0 a b host1 a b or host2 a b host3 a b\nend\n') |
| 60 | source=(R.parent/'fast/transport.om').read_text()+'\n'+(R/'web.om').read_text()+'\n'+(R/'runner.om').read_text()+'\n'+''.join(generated) |
| 61 | source=specialize(source) |
| 62 | (R/'server.om').write_text(source) |
| 63 | p=subprocess.run([str(C)],input=source.encode(),capture_output=True,timeout=60) |
| 64 | (R/'compile.stderr').write_bytes(p.stderr) |
| 65 | assert p.returncode==0,(p.returncode,p.stderr.decode()) |
| 66 | (R/'server.new').write_bytes(p.stdout);(R/'server.new').chmod(0o700);(R/'server.new').replace(R/'server') |
| 67 | candidate=source.replace('put haddr 3 253','put haddr 3 255').replace(':18173',':18175').replace('$HOME/.local/share/omega-tool-model/round40-workbench/omc',str(R/'omc')) |
| 68 | # Compiler is deliberately pinned and copied into each independent release. |
| 69 | (R/'omc').write_bytes(C.read_bytes());(R/'omc').chmod(0o700) |
| 70 | q=subprocess.run([str(C)],input=candidate.encode(),capture_output=True,timeout=60);assert q.returncode==0,q.stderr |
| 71 | (R/'candidate.om').write_text(candidate);(R/'candidate.new').write_bytes(q.stdout);(R/'candidate.new').chmod(0o700);(R/'candidate.new').replace(R/'candidate') |
| 72 | (R/'build.json').write_text(json.dumps({'compiler':str(C),'compiler_sha256':hashlib.sha256(C.read_bytes()).hexdigest(),'source_sha256':hashlib.sha256(source.encode()).hexdigest(),'binary_sha256':hashlib.sha256(p.stdout).hexdigest(),'compile_rc':p.returncode,'http_reference':'https://www.rfc-editor.org/rfc/rfc9112.html#name-chunked-transfer-coding'},indent=2)+'\n') |
| 73 | print('Native browser adapter compiled successfully.') |