omega-tool-model
First-party Omega tool-use model source, serving harnesses, and verification records
Brought by drakestapleton
omega-tool-model / deployment / ui / failure-check.py
| 1 | """Isolated external model-protocol fixture. Never stops the real model.""" |
| 2 | from pathlib import Path |
| 3 | import http.client,json,socket,struct,subprocess,threading,time |
| 4 | R=Path(__file__).resolve().parent |
| 5 | sock=R/'test.sock' |
| 6 | source=(R/'server.om').read_text().replace('$HOME/.local/share/omega-tool-model/run/model.sock',str(sock)).replace('put haddr 3 253','put haddr 3 255').replace(':18173',':18175') |
| 7 | c=json.loads((R/'build.json').read_text())['compiler'] |
| 8 | p=subprocess.run([c],input=source.encode(),capture_output=True);assert p.returncode==0,p.stderr |
| 9 | (R/'failure-server.om').write_text(source);(R/'failure-server').write_bytes(p.stdout);(R/'failure-server').chmod(0o700) |
| 10 | server=subprocess.Popen([str(R/'failure-server')]);checks=[] |
| 11 | def query(): |
| 12 | conn=http.client.HTTPConnection('127.0.0.1',18175,timeout=8) |
| 13 | conn.request('POST','/generate',b'a',{'X-Omega-UI':'1','Content-Type':'text/plain'}) |
| 14 | r=conn.getresponse() |
| 15 | try:return r.status,r.read(),False |
| 16 | except http.client.IncompleteRead as e:return r.status,e.partial,True |
| 17 | finally:conn.close() |
| 18 | def case(name,frames,status,broken=False,body=None): |
| 19 | if sock.exists():sock.unlink() |
| 20 | listener=socket.socket(socket.AF_UNIX);listener.bind(str(sock));listener.listen(1) |
| 21 | errors=[] |
| 22 | def mock(): |
| 23 | try: |
| 24 | with listener.accept()[0] as conn: |
| 25 | conn.settimeout(8);buf=b'' |
| 26 | while len(buf)<3:buf+=conn.recv(3-len(buf)) |
| 27 | assert buf==b'\1\0a',buf |
| 28 | for piece in frames: |
| 29 | conn.sendall(piece);time.sleep(.002) |
| 30 | except Exception as e:errors.append(str(e)) |
| 31 | finally:listener.close() |
| 32 | t=threading.Thread(target=mock);t.start() |
| 33 | actual,payload,cut=query();t.join(timeout=10) |
| 34 | passed=actual==status and cut==broken and (body is None or body==payload) and not errors |
| 35 | checks.append({'name':name,'pass':passed,'status':actual,'incomplete':cut});assert passed,(name,actual,payload,cut,errors) |
| 36 | try: |
| 37 | for i in range(50): |
| 38 | try: |
| 39 | with socket.create_connection(('127.0.0.1',18175),timeout=.2):break |
| 40 | except OSError:time.sleep(.02) |
| 41 | if sock.exists():sock.unlink() |
| 42 | checks.append({'name':'unavailable model is explicit 503','pass':query()[0]==503});assert checks[-1]['pass'] |
| 43 | case('prompt overflow', [b'\xff\xff\x03'],422) |
| 44 | case('model refuses request',[b'\xff\xff\x02'],502) |
| 45 | case('truncated first frame', [b'\x01'],502) |
| 46 | case('oversized frame', [struct.pack('<H',4097)],502) |
| 47 | case('truncated token', [b'\x04\x00ab'],200,True) |
| 48 | case('missing success terminator', [b'\x02\x00ok'],200,True) |
| 49 | case('failure after output', [b'\x02\x00ok',b'\xff\xff\x02'],200,True) |
| 50 | case('empty successful response',[b'\0\0'],200,False,b'') |
| 51 | payload=b'<script>alert("model text")</script>' |
| 52 | frames=[bytes([b]) for b in struct.pack('<H',len(payload))+payload+b'\0\0'] |
| 53 | case('fragmented literal model text',frames,200,False,payload) |
| 54 | (R/'failure-checks.json').write_text(json.dumps(checks,indent=2)+'\n') |
| 55 | print(f'{len(checks)} isolated model failure/framing controls passed.') |
| 56 | finally: |
| 57 | server.terminate();server.wait(timeout=5) |
| 58 | if sock.exists():sock.unlink() |