omega-tool-model
First-party Omega tool-use model source, serving harnesses, and verification records
Brought by drakestapleton
omega-tool-model / deployment / ui / check.py
| 1 | import os |
| 2 | """External HTTP measuring/checking instrument; never deployed.""" |
| 3 | import concurrent.futures,json,socket,time |
| 4 | from http.client import HTTPConnection |
| 5 | from pathlib import Path |
| 6 | R=Path(__file__).resolve().parent |
| 7 | cases=[] |
| 8 | def http(path='/',body=None,headers=None): |
| 9 | c=HTTPConnection('127.0.0.1',18173,timeout=10) |
| 10 | c.request('GET' if body is None else 'POST',path,body=body,headers=headers or {}) |
| 11 | r=c.getresponse();b=r.read();result=(r.status,dict(r.getheaders()),b);c.close();return result |
| 12 | def record(name,passed): |
| 13 | cases.append({'name':name,'pass':bool(passed)});assert passed,name |
| 14 | def raw(req): |
| 15 | with socket.create_connection(('127.0.0.1',18173),timeout=8) as s: |
| 16 | s.sendall(req);s.shutdown(socket.SHUT_WR);b=b'' |
| 17 | while True: |
| 18 | x=s.recv(65536) |
| 19 | if not x:break |
| 20 | b+=x |
| 21 | return b |
| 22 | code,headers,page=http();record('page exact UTF-8 bytes',code==200 and page==(R/'index.html').read_bytes()) |
| 23 | record('browser restrictions and no-store','Content-Security-Policy' in headers and headers['Cache-Control']=='no-store') |
| 24 | p=b'Task: Write Omega that returns the larger of 24 and 27.\nAnswer:\n' |
| 25 | expected=b'fn main to int\nif gt int 24 int 27\nret int 24\nelse\nret int 27\nend\nend' |
| 26 | h={'X-Omega-UI':'1','Content-Type':'text/plain'} |
| 27 | code,headers,b=http('/generate',p,h);record('real model comparison response',code==200 and b==expected and headers.get('Transfer-Encoding')=='chunked') |
| 28 | tool=(Path(os.path.expanduser('~')) / '/.local/share/omega-tool-model/round40/example.prompt'.lstrip('/')).read_bytes() |
| 29 | code,_,b=http('/generate',tool,h);record('real model tool response',code==200 and b==b'{"tool":"read_file","path":"jobs/item76666.om"}') |
| 30 | record('context overflow has clear error',http('/generate',b'abcdefg '*400,h)[0]==422) |
| 31 | for name,body,hh in [('empty',b'',h),('NUL',b'a\0b',h),('too many bytes',b'x'*4001,h),('missing UI header',p,{})]: |
| 32 | record(name,http('/generate',body,hh)[0]==400) |
| 33 | record('unknown route',http('/unavailable')[0]==404) |
| 34 | for name,request in [ |
| 35 | ('cross origin preflight',b'OPTIONS /generate HTTP/1.1\r\nHost: 127.0.0.1:18173\r\nOrigin: https://example.org\r\n\r\n'), |
| 36 | ('untrusted host',b'GET / HTTP/1.1\r\nHost: example.org\r\n\r\n'), |
| 37 | ('duplicate length',b'POST /generate HTTP/1.1\r\nHost: 127.0.0.1:18173\r\nX-Omega-UI: 1\r\nContent-Length: 1\r\nContent-Length: 1\r\n\r\nx'), |
| 38 | ('unsupported transfer',b'POST /generate HTTP/1.1\r\nHost: 127.0.0.1:18173\r\nX-Omega-UI: 1\r\nTransfer-Encoding: chunked\r\n\r\n0\r\n\r\n'), |
| 39 | ('negative length',b'POST /generate HTTP/1.1\r\nHost: 127.0.0.1:18173\r\nX-Omega-UI: 1\r\nContent-Length: -1\r\n\r\n'), |
| 40 | ('truncated body',b'POST /generate HTTP/1.1\r\nHost: 127.0.0.1:18173\r\nX-Omega-UI: 1\r\nContent-Length: 8\r\n\r\nx'), |
| 41 | ('oversized headers',b'GET / HTTP/1.1\r\nHost: 127.0.0.1:18173\r\nX-Fill: '+b'a'*8150+b'\r\n\r\n')]: |
| 42 | try:b=raw(request) |
| 43 | except ConnectionResetError:b=b'HTTP/1.1 400' |
| 44 | record(name,b.startswith(b'HTTP/1.1 400') or (name=='cross origin preflight' and b.startswith(b'HTTP/1.1 404'))) |
| 45 | with socket.create_connection(('127.0.0.1',18173)) as idle: |
| 46 | start=time.monotonic();code,_,b=http('/generate',p,h) |
| 47 | record('idle browser socket does not block another request',code==200 and b==expected and time.monotonic()-start<2) |
| 48 | with concurrent.futures.ThreadPoolExecutor(max_workers=4) as pool: |
| 49 | outputs=list(pool.map(lambda _:http('/generate',p,h),range(4))) |
| 50 | record('four concurrent requests isolated',all(x[0]==200 and x[2]==expected for x in outputs)) |
| 51 | record('healthy after controls',http()[0]==200) |
| 52 | (R/'http-checks.json').write_text(json.dumps(cases,indent=2)+'\n') |
| 53 | print(f'{len(cases)} HTTP/model checks passed.') |