code wiki / _hdl_build / nx_nofloat_serve_live_gate.nx
nx_nofloat_serve_live_gate.nx source
↩ module page · 95 lines · 5641 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
9func 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 }
10func 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 }
11func lv_slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
12func lv_has(buf: *u8, n: i64, needle: *u8) -> i64 {
13 let m: i64 = lv_slen(needle)
14 if m == 0 { return 1 }
15 var i: i64 = 0
16 while i + m <= n {
17 var k: i64 = 0
18 var ok: i64 = 1
19 while k < m { if buf[i+k] != needle[k] { ok = 0; k = m } else { k = k + 1 } }
20 if ok == 1 { return 1 }
21 i = i + 1
22 }
23 return 0
24}
25func lv_addr(out: *u8, port: i64) -> i64 {
26 out[0]=2 as u8; out[1]=0 as u8
27 out[2]=((port>>8)&0xff) as u8; out[3]=(port&0xff) as u8
28 out[4]=127 as u8; out[5]=0 as u8; out[6]=0 as u8; out[7]=1 as u8
29 var i: i64=8; while i<16 { out[i]=0 as u8; i=i+1 } return 0
30}
31// one request/response over a fresh connection. returns bytes read (-1 on connect fail).
32func lv_do(req: *u8, resb: *u8, cap: i64) -> i64 {
33 let fd: i64 = sys_socket(AF_INET, SOCK_STREAM, 0)
34 if fd < 0 { return 0 - 1 }
35 let addr: *u8 = sys_mmap(16)
36 lv_addr(addr, 8032)
37 if nx_connect_bounded(fd, addr, 16, NX_CONN_DEFAULT_MS) < 0 { sys_close(fd); return 0 - 1 }
38 let rl: i64 = lv_slen(req)
39 var w: i64 = 0
40 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 } }
41 var n: i64 = 0
42 var go: i64 = 1
43 while go == 1 {
44 let r: i64 = sys_read(fd, ((resb as i64)+n) as *u8, cap-1-n)
45 if r <= 0 { go = 0 } else { n = n + r; if n >= cap-1 { go = 0 } }
46 }
47 sys_close(fd)
48 return n
49}
50
51func main() -> i64 {
52 lv_puts("=== nx_nofloat_serve_live_gate: REAL socket to 127.0.0.1:8032 ===\n" as *u8)
53 var pass: i64=0
54 var total: i64=0
55 let resb: *u8 = sys_mmap(262144)
56
57 let n1: i64 = lv_do("GET /health HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n" as *u8, resb, 262144)
58 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 }
59 lv_puts(" [health] " as *u8)
60 var b1: i64=0
61 var si: i64=0
62 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 }
63 sys_write(1, ((resb as i64)+b1) as *u8, n1-b1)
64 lv_puts("\n" as *u8)
65 total=total+1
66 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) }
67
68 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)
69 lv_puts(" [gen] " as *u8)
70 var b2: i64=0
71 si=0
72 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 }
73 sys_write(1, ((resb as i64)+b2) as *u8, n2-b2)
74 lv_puts("\n" as *u8)
75 total=total+1
76 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) }
77
78 // T3: STREAMING over the real socket -- SSE headers + piece frames + done frame from the daemon shell.
79 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)
80 lv_puts(" [stream] " as *u8)
81 var b3: i64=0
82 si=0
83 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 }
84 sys_write(1, ((resb as i64)+b3) as *u8, n3-b3)
85 lv_puts("\n" as *u8)
86 total=total+1
87 var ok3: i64 = 0
88 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 } } }
89 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) }
90
91 lv_puts("NX-NOFLOAT-SERVE-LIVE-GATE passed " as *u8); lv_pn(pass); lv_puts("/" as *u8); lv_pn(total)
92 if pass==total { lv_puts(" verdict=GREEN (daemon serves the no-float LLM live over TCP)\n" as *u8); return 0 }
93 lv_puts(" verdict=RED\n" as *u8)
94 return 1
95}