code wiki / _hdl_build / nx_nofloat_serve_live_gate.nx
nx_nofloat_serve_live_gate.nx source
↩ module page · 102 lines · 6043 B
1// nx_nofloat_serve_live_gate.nx -- LIVE socket proof for the no-float LLM daemon: connects to
2// 127.0.0.1:8032 over a REAL TCP socket (not the in-process handler), sends GET /health and POST /gen,
3// prints the raw response bytes, and asserts the readable completion comes back over the wire. This is the
4// "paste live bytes" proof that the deployed daemon actually serves. Requires nx_nofloat_serve already
5// running on :8032. expect_exit: 0 license_tier: ORIGINAL
6import "nx_syscalls.nx"
7
8import "nx_connect.nx" // bounded connect: a raw sys_connect hangs ~127s on a black-holed host
9import "nx_gate_verdict.nx"
10func lv_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
11func lv_pn(v: i64) -> i64 { let b: *u8=sys_mmap(28); var m: i64=v; if m<0{sys_write(1,"-" as *u8,1);m=0-m} let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48 as u8;k=1} while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1} var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1} sys_write(1,b,k); return 0 }
12func lv_slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
13func lv_has(buf: *u8, n: i64, needle: *u8) -> i64 {
14 let m: i64 = lv_slen(needle)
15 if m == 0 { return 1 }
16 var i: i64 = 0
17 while i + m <= n {
18 var k: i64 = 0
19 var ok: i64 = 1
20 while k < m { if buf[i+k] != needle[k] { ok = 0; k = m } else { k = k + 1 } }
21 if ok == 1 { return 1 }
22 i = i + 1
23 }
24 return 0
25}
26func lv_addr(out: *u8, port: i64) -> i64 {
27 out[0]=2 as u8; out[1]=0 as u8
28 out[2]=((port>>8)&0xff) as u8; out[3]=(port&0xff) as u8
29 out[4]=127 as u8; out[5]=0 as u8; out[6]=0 as u8; out[7]=1 as u8
30 var i: i64=8; while i<16 { out[i]=0 as u8; i=i+1 } return 0
31}
32// one request/response over a fresh connection. returns bytes read (-1 on connect fail).
33func lv_do(req: *u8, resb: *u8, cap: i64) -> i64 {
34 let fd: i64 = sys_socket(AF_INET, SOCK_STREAM, 0)
35 if fd < 0 { return 0 - 1 }
36 let addr: *u8 = sys_mmap(16)
37 lv_addr(addr, 8032)
38 if nx_connect_bounded(fd, addr, 16, NX_CONN_DEFAULT_MS) < 0 { sys_close(fd); return 0 - 1 }
39 let rl: i64 = lv_slen(req)
40 var w: i64 = 0
41 while w < rl { let k: i64 = sys_write(fd, ((req as i64)+w) as *u8, rl-w); if k <= 0 { w = rl } else { w = w + k } }
42 var n: i64 = 0
43 var go: i64 = 1
44 while go == 1 {
45 let r: i64 = sys_read(fd, ((resb as i64)+n) as *u8, cap-1-n)
46 if r <= 0 { go = 0 } else { n = n + r; if n >= cap-1 { go = 0 } }
47 }
48 sys_close(fd)
49 return n
50}
51
52func main() -> i64 {
53 lv_puts("=== nx_nofloat_serve_live_gate: REAL socket to 127.0.0.1:8032 ===\n" as *u8)
54 var pass: i64=0
55 var total: i64=0
56 let resb: *u8 = sys_mmap(262144)
57
58 let n1: i64 = lv_do("GET /health HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n" as *u8, resb, 262144)
59 if n1 < 0 { lv_puts(" CANNOT CONNECT -- is the daemon running on :8032?\nNX-NOFLOAT-SERVE-LIVE-GATE verdict=RED\n" as *u8); return 1 }
60 lv_puts(" [health] " as *u8)
61 var b1: i64=0
62 var si: i64=0
63 while si+3 < n1 { if resb[si]==(13 as u8) { if resb[si+1]==(10 as u8) { if resb[si+2]==(13 as u8) { if resb[si+3]==(10 as u8) { b1=si+4; si=n1 } } } } si=si+1 }
64 sys_write(1, ((resb as i64)+b1) as *u8, n1-b1)
65 lv_puts("\n" as *u8)
66 total=total+1
67 if lv_has(resb, n1, "200 OK" as *u8)==1 { if lv_has(resb, n1, "\"ok\":1" as *u8)==1 { pass=pass+1; lv_puts(" PASS T1 live /health 200 ok:1\n" as *u8) } else { lv_puts(" FAIL T1\n" as *u8) } } else { lv_puts(" FAIL T1\n" as *u8) }
68
69 let n2: i64 = lv_do("POST /gen HTTP/1.1\r\nHost: localhost\r\nContent-Type: application/json\r\nConnection: close\r\nContent-Length: 62\r\n\r\n{\"prompt\":\"The capital of France is\",\"max_new\":6,\"mode\":\"i32\"}" as *u8, resb, 262144)
70 lv_puts(" [gen] " as *u8)
71 var b2: i64=0
72 si=0
73 while si+3 < n2 { if resb[si]==(13 as u8) { if resb[si+1]==(10 as u8) { if resb[si+2]==(13 as u8) { if resb[si+3]==(10 as u8) { b2=si+4; si=n2 } } } } si=si+1 }
74 sys_write(1, ((resb as i64)+b2) as *u8, n2-b2)
75 lv_puts("\n" as *u8)
76 total=total+1
77 if lv_has(resb, n2, "200 OK" as *u8)==1 { if lv_has(resb, n2, " Paris. It is the" as *u8)==1 { pass=pass+1; lv_puts(" PASS T2 live /gen -> readable ' Paris. It is the' over the wire\n" as *u8) } else { lv_puts(" FAIL T2 (no readable Paris)\n" as *u8) } } else { lv_puts(" FAIL T2 (not 200)\n" as *u8) }
78
79 // T3: STREAMING over the real socket -- SSE headers + piece frames + done frame from the daemon shell.
80 let n3: i64 = lv_do("POST /gen HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\nContent-Length: 57\r\n\r\n{\"prompt\":\"The capital of France is\",\"max_new\":4,\"stream\":1}" as *u8, resb, 262144)
81 lv_puts(" [stream] " as *u8)
82 var b3: i64=0
83 si=0
84 while si+3 < n3 { if resb[si]==(13 as u8) { if resb[si+1]==(10 as u8) { if resb[si+2]==(13 as u8) { if resb[si+3]==(10 as u8) { b3=si+4; si=n3 } } } } si=si+1 }
85 sys_write(1, ((resb as i64)+b3) as *u8, n3-b3)
86 lv_puts("\n" as *u8)
87 total=total+1
88 var ok3: i64 = 0
89 if lv_has(resb, n3, "text/event-stream" as *u8)==1 { if lv_has(resb, n3, "\"piece\":\" Paris\"" as *u8)==1 { if lv_has(resb, n3, "\"done\":1,\"ok\":1" as *u8)==1 { ok3 = 1 } } }
90 if ok3==1 { pass=pass+1; lv_puts(" PASS T3 live SSE stream over TCP (headers + pieces + done)\n" as *u8) } else { lv_puts(" FAIL T3\n" as *u8) }
91
92 lv_puts("NX-NOFLOAT-SERVE-LIVE-GATE passed " as *u8); lv_pn(pass); lv_puts("/" as *u8); lv_pn(total)
93 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check
94 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled
95 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify.
96 let ctr__dry: *i64 = gv_ctr()
97 ctr__dry[0] = pass
98 ctr__dry[1] = total
99 let rc__dry: i64 = gv_verdict("NOFLOAT-SERVE-LIVE-GATE" as *u8, ctr__dry, "daemon serves the no-float LLM live over TCP)" as *u8)
100 sys_exit(rc__dry)
101 return rc__dry
102}