code wiki / _hdl_build / nx_nofloat_serve_gate.nx

nx_nofloat_serve_gate.nx source

↩ module page · 224 lines · 12550 B

1// nx_nofloat_serve_gate.nx -- gates the no-float LLM serve CORE in-process (pure-core+gate+daemon idiom: 2// no sockets here; synthetic HTTP requests straight into nsv_handle). Teeth: 3// T1 GET /health -> 200 + ok:1 (init fail-fast worked; model + both caches live) 4// T2 POST /gen "The capital of France is" (i32) -> 200 + READABLE " Paris. It is the" (real spaces -- 5// the byte-level-BPE decode; raw pieces would read "GParis." with G-dot 0xC4 0xA0) 6// T3 arbitrary second prompt -> 200 + gen_tokens>0 + NO 0xC4 0xA0 byte pair anywhere (decode total) 7// T4 mode i8 on the France prompt -> 200 + contains "Paris" (fast mode serves) 8// T5 GET /nope -> 404 (router honest) 9// T8-T10 (2026-09-17, the prompt-length class, four pillars): nsv_prompt_admit refuses by BYTES, tk_bpe_encode_b refuses 10// by capacity, and a prompt past the byte cap (T10a) or the token cap (T10b, more ids than NSV_MAXT held by the 11// byte-sized ids buffer) answers 400 err 3 with the process alive; the pre-fix engine died in tk_rank_fast on both 12// Requires /home/elderwesto/nx_stage/nx_real_model.gguf. expect_exit: 0 license_tier: ORIGINAL 13import "nx_nofloat_serve_core.nx" 14import "nx_gate_verdict.nx" 15import "nx_stage_path.nx" 16 17func sg_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 18func sg_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 } 19func sg_slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n } 20// substring search (needle NUL-terminated) in buf[0,n) 21func sg_has(buf: *u8, n: i64, needle: *u8) -> i64 { 22 let m: i64 = sg_slen(needle) 23 if m == 0 { return 1 } 24 var i: i64 = 0 25 while i + m <= n { 26 var k: i64 = 0 27 var ok: i64 = 1 28 while k < m { if buf[i+k] != needle[k] { ok = 0; k = m } else { k = k + 1 } } 29 if ok == 1 { return 1 } 30 i = i + 1 31 } 32 return 0 33} 34func sg_check(name: *u8, cond: i64) -> i64 { 35 if cond==1 { sg_puts(" PASS " as *u8) } else { sg_puts(" FAIL " as *u8) } 36 sg_puts(name) 37 sg_puts("\n" as *u8) 38 return cond 39} 40// drive one synthetic request through the core; returns response length. 41func sg_cat(d: *u8, o: i64, s: *u8) -> i64 { var i: i64 = 0; var p: i64 = o; while s[i] != (0 as u8) { d[p] = s[i]; p = p + 1; i = i + 1 } d[p] = 0 as u8; return p } 42func sg_catn(d: *u8, o: i64, v: i64) -> i64 { 43 var p: i64 = o 44 var m: i64 = v 45 if m < 0 { d[p] = 45 as u8; p = p + 1; m = 0 - m } 46 if m == 0 { d[p] = 48 as u8; p = p + 1; d[p] = 0 as u8; return p } 47 var pw: i64 = 1 48 while m / pw >= 10 { pw = pw * 10 } 49 while pw > 0 { d[p] = (48 + ((m / pw) % 10)) as u8; p = p + 1; pw = pw / 10 } 50 d[p] = 0 as u8 51 return p 52} 53func sg_req(req: *u8, resb: *u8) -> i64 { 54 return nsv_handle(req, sg_slen(req), resb, 262144) 55} 56 57func main() -> i64 { 58 sg_puts("=== nx_nofloat_serve_gate: pure-core HTTP handler over the sovereign no-float LLM ===\n" as *u8) 59 var pass: i64 = 0 60 var total: i64 = 0 61 let t0: i64 = sys_now_ms() 62 let rc: i64 = nsv_init(sp_guarded("NOFLOAT-SERVE-GATE" as *u8, "nx_real_model.gguf" as *u8)) 63 let t1: i64 = sys_now_ms() 64 sg_puts(" [init] rc=" as *u8) 65 sg_pn(rc) 66 sg_puts(" (" as *u8) 67 sg_pn(t1-t0) 68 sg_puts(" ms: model + i32 cache + i8 cache + head)\n" as *u8) 69 if rc != 0 { sg_puts("NX-NOFLOAT-SERVE-GATE verdict=RED (init)\n" as *u8); return 1 } 70 71 let resb: *u8 = sys_mmap(262144) 72 // T1 health 73 let n1: i64 = sg_req("GET /health HTTP/1.1\r\nHost: x\r\n\r\n" as *u8, resb) 74 var ok1: i64 = 0 75 if sg_has(resb, n1, "200 OK" as *u8) == 1 { if sg_has(resb, n1, "\"ok\":1" as *u8) == 1 { ok1 = 1 } } 76 pass = pass + sg_check("T1 GET /health -> 200 ok:1" as *u8, ok1) 77 total = total + 1 78 // T2 France prompt, i32 lossless, readable text 79 let n2: i64 = sg_req("POST /gen HTTP/1.1\r\nHost: x\r\nContent-Type: application/json\r\nContent-Length: 62\r\n\r\n{\"prompt\":\"The capital of France is\",\"max_new\":6,\"mode\":\"i32\"}" as *u8, resb) 80 sg_puts(" [gen-i32] " as *u8) 81 var show: i64 = n2 82 if show > 300 { show = 300 } 83 var si: i64 = 0 84 var bodyat: i64 = 0 85 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) { bodyat = si + 4; si = n2 } } } } si = si + 1 } 86 sys_write(1, ((resb as i64)+bodyat) as *u8, n2-bodyat) 87 sg_puts("\n" as *u8) 88 var ok2: i64 = 0 89 if sg_has(resb, n2, "200 OK" as *u8) == 1 { if sg_has(resb, n2, " Paris. It is the" as *u8) == 1 { ok2 = 1 } } 90 pass = pass + sg_check("T2 POST /gen i32 -> 200 + READABLE ' Paris. It is the' (spaces real, BPE-decoded)" as *u8, ok2) 91 total = total + 1 92 // T3 arbitrary prompt + zero raw G-dot bytes (0xC4 0xA0) in the response 93 let n3: i64 = sg_req("POST /gen HTTP/1.1\r\nHost: x\r\nContent-Length: 44\r\n\r\n{\"prompt\":\"Water is made of\",\"max_new\":8}" as *u8, resb) 94 var ok3: i64 = 0 95 if sg_has(resb, n3, "200 OK" as *u8) == 1 { if sg_has(resb, n3, "\"gen_tokens\":" as *u8) == 1 { ok3 = 1 } } 96 var gdot: i64 = 0 97 var gi: i64 = 0 98 while gi + 1 < n3 { if resb[gi]==(196 as u8) { if resb[gi+1]==(160 as u8) { gdot = 1; gi = n3 } } gi = gi + 1 } 99 if gdot == 1 { ok3 = 0 } 100 pass = pass + sg_check("T3 arbitrary prompt -> 200 + tokens + NO raw G-dot bytes (decode is total)" as *u8, ok3) 101 total = total + 1 102 // T4 i8 fast mode 103 let n4: i64 = sg_req("POST /gen HTTP/1.1\r\nHost: x\r\nContent-Length: 61\r\n\r\n{\"prompt\":\"The capital of France is\",\"max_new\":4,\"mode\":\"i8\"}" as *u8, resb) 104 var ok4: i64 = 0 105 if sg_has(resb, n4, "200 OK" as *u8) == 1 { if sg_has(resb, n4, "Paris" as *u8) == 1 { if sg_has(resb, n4, "\"mode\":\"i8\"" as *u8) == 1 { ok4 = 1 } } } 106 pass = pass + sg_check("T4 mode i8 -> 200 + Paris + mode echoed" as *u8, ok4) 107 total = total + 1 108 // T5 unknown route 109 let n5: i64 = sg_req("GET /nope HTTP/1.1\r\nHost: x\r\n\r\n" as *u8, resb) 110 var ok5: i64 = 0 111 if sg_has(resb, n5, "404" as *u8) == 1 { ok5 = 1 } 112 pass = pass + sg_check("T5 GET /nope -> 404" as *u8, ok5) 113 total = total + 1 114 // T8-T10 (2026-09-17, four pillars for the prompt-length class): the door refuses by BYTES before a byte is 115 // tokenised, the bounded tokenizer refuses by capacity, and the handler answers 400 err 3 for a prompt past 116 // either cap instead of dying. The pre-fix engine died in tk_rank_fast on a 19222-byte BRIGHT query. 117 var ok8: i64 = 0 118 if nsv_prompt_admit(NSV_MAXIN) == 0 { if nsv_prompt_admit(NSV_MAXIN + 1) == NSV_ERR_TOO_LONG { ok8 = 1 } } 119 pass = pass + sg_check("T8 nsv_prompt_admit: NSV_MAXIN bytes admitted, NSV_MAXIN+1 refused with NSV_ERR_TOO_LONG" as *u8, ok8) 120 total = total + 1 121 let tp9: *i64 = sys_mmap(64 * 8) as *i64 122 let tl9: *i64 = sys_mmap(64 * 8) as *i64 123 let id9: *i64 = sys_mmap(64 * 8) as *i64 124 let r9a: i64 = tk_bpe_encode_b(g_nsv_buf, g_nsv_mt[0], g_nsv_mt[1], g_nsv_mt[2], g_nsv_mt[3], "hello world" as *u8, 11, tp9, tl9, 10, id9, 64) 125 let r9b: i64 = tk_bpe_encode_b(g_nsv_buf, g_nsv_mt[0], g_nsv_mt[1], g_nsv_mt[2], g_nsv_mt[3], "hello world" as *u8, 11, tp9, tl9, 64, id9, 10) 126 let r9c: i64 = tk_bpe_encode_b(g_nsv_buf, g_nsv_mt[0], g_nsv_mt[1], g_nsv_mt[2], g_nsv_mt[3], "hello world" as *u8, 11, tp9, tl9, 11, id9, 11) 127 var ok9: i64 = 0 128 if r9a == TK_REFUSED_TOO_LONG { if r9b == TK_REFUSED_TOO_LONG { if r9c >= 1 { if r9c <= 11 { ok9 = 1 } } } } 129 sg_puts(" [tok-bound] scratch_cap 10 -> " as *u8); sg_pn(r9a); sg_puts(" ids_cap 10 -> " as *u8); sg_pn(r9b); sg_puts(" caps 11 -> ntok " as *u8); sg_pn(r9c); sg_puts("\n" as *u8) 130 pass = pass + sg_check("T9 tk_bpe_encode_b refuses a scratch or ids capacity below the input and encodes at exactly the input length (ntok <= bytes)" as *u8, ok9) 131 total = total + 1 132 let big: i64 = NSV_MAXIN + 1 133 let req10: *u8 = sys_mmap(big + 256) 134 var o10: i64 = sg_cat(req10, 0, "POST /gen HTTP/1.1\r\nHost: x\r\nContent-Length: " as *u8) 135 o10 = sg_catn(req10, o10, big + 25) 136 o10 = sg_cat(req10, o10, "\r\n\r\n{\"prompt\":\"" as *u8) 137 var f10: i64 = 0 138 while f10 < big { req10[o10] = 97 as u8; o10 = o10 + 1; f10 = f10 + 1 } 139 o10 = sg_cat(req10, o10, "\",\"max_new\":2}" as *u8) 140 let n10: i64 = sg_req(req10, resb) 141 var ok10: i64 = 0 142 if sg_has(resb, n10, "400" as *u8) == 1 { if sg_has(resb, n10, "\"err\":3" as *u8) == 1 { ok10 = 1 } } 143 pass = pass + sg_check("T10a a prompt of NSV_MAXIN+1 bytes -> 400 err 3 and the process is still here (the byte door)" as *u8, ok10) 144 total = total + 1 145 let big2: i64 = NSV_MAXIN 146 let req11: *u8 = sys_mmap(big2 + 256) 147 var o11: i64 = sg_cat(req11, 0, "POST /gen HTTP/1.1\r\nHost: x\r\nContent-Length: " as *u8) 148 o11 = sg_catn(req11, o11, big2 + 25) 149 o11 = sg_cat(req11, o11, "\r\n\r\n{\"prompt\":\"" as *u8) 150 var f11: i64 = 0 151 while f11 < big2 { if f11 % 2 == 0 { req11[o11] = (97 + ((f11 / 2) % 26)) as u8 } else { req11[o11] = 32 as u8 } o11 = o11 + 1; f11 = f11 + 1 } 152 o11 = sg_cat(req11, o11, "\",\"max_new\":2}" as *u8) 153 let n11: i64 = sg_req(req11, resb) 154 var ok11: i64 = 0 155 if sg_has(resb, n11, "400" as *u8) == 1 { if sg_has(resb, n11, "\"err\":3" as *u8) == 1 { ok11 = 1 } } 156 pass = pass + sg_check("T10b NSV_MAXIN bytes of single-letter words (more ids than NSV_MAXT) -> 400 err 3, no overrun (the token door)" as *u8, ok11) 157 total = total + 1 158 // T6 SSE streaming: drive nsv_handle_stream with a FILE fd (same bytes a socket would get), then 159 // read the file back and assert the event-stream shape + the first streamed piece. 160 let sfd: i64 = sys_openat_wr("/tmp/nsv_sse_test.out" as *u8, 0x1a4) 161 let sreq: *u8 = "POST /gen HTTP/1.1\r\nHost: x\r\nContent-Length: 57\r\n\r\n{\"prompt\":\"The capital of France is\",\"max_new\":4,\"stream\":1}" as *u8 162 let hs: i64 = nsv_handle_stream(sfd, sreq, sg_slen(sreq)) 163 sys_close(sfd) 164 let slen_p: *i64 = sys_mmap(16) as *i64 165 let sbuf: *u8 = sys_read_file("/tmp/nsv_sse_test.out" as *u8, slen_p) 166 var ok6: i64 = 0 167 if hs == 1 { if (sbuf as i64) != 0 { 168 if sg_has(sbuf, slen_p[0], "text/event-stream" as *u8) == 1 { 169 if sg_has(sbuf, slen_p[0], "\"piece\":\" Paris\"" as *u8) == 1 { 170 if sg_has(sbuf, slen_p[0], "\"done\":1,\"ok\":1" as *u8) == 1 { ok6 = 1 } 171 } 172 } 173 } } 174 pass = pass + sg_check("T6 SSE stream: event-stream headers + piece ' Paris' + done frame" as *u8, ok6) 175 total = total + 1 176 // T7 seeded sampling is DETERMINISTIC: same prompt+temp+seed twice -> byte-identical text (the 177 // no-float exceed extended to sampling: the seed is part of the input). 178 let pr7: *u8 = "The capital of France is" as *u8 179 let tx1: *u8 = sys_mmap(8192) 180 let tx2: *u8 = sys_mmap(8192) 181 let mt7: *i64 = sys_mmap(8*8) as *i64 182 let gp7: *i64 = sys_mmap(16*8) as *i64 183 gp7[0]=pr7 as i64 184 gp7[1]=sg_slen(pr7) 185 gp7[2]=6 186 gp7[3]=0 187 gp7[4]=tx1 as i64 188 gp7[5]=8000 189 gp7[6]=mt7 as i64 190 gp7[7]=0-1 191 gp7[8]=800 192 gp7[9]=950 193 gp7[10]=64 194 gp7[11]=42 195 let l1: i64 = nsv_generate(gp7) 196 gp7[4]=tx2 as i64 197 let l2: i64 = nsv_generate(gp7) 198 var ok7: i64 = 0 199 if l1 > 0 { if l1 == l2 { 200 var eq: i64 = 1 201 var ci7: i64 = 0 202 while ci7 < l1 { if tx1[ci7] != tx2[ci7] { eq = 0; ci7 = l1 } else { ci7 = ci7 + 1 } } 203 if eq == 1 { ok7 = 1 } 204 } } 205 sg_puts(" [sample temp=0.8 seed=42] '" as *u8) 206 if l1 > 0 { sys_write(1, tx1, l1) } 207 sg_puts("'\n" as *u8) 208 pass = pass + sg_check("T7 seeded sampling deterministic: same seed -> byte-identical text" as *u8, ok7) 209 total = total + 1 210 211 sg_puts("NX-NOFLOAT-SERVE-GATE passed " as *u8) 212 sg_pn(pass) 213 sg_puts("/" as *u8) 214 sg_pn(total) 215 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check 216 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled 217 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify. 218 let ctr__dry: *i64 = gv_ctr() 219 ctr__dry[0] = pass 220 ctr__dry[1] = total 221 let rc__dry: i64 = gv_verdict("NOFLOAT-SERVE-GATE" as *u8, ctr__dry, "serve core: readable text, both modes, honest routes)" as *u8) 222 sys_exit_group(rc__dry) 223 return rc__dry 224}