code wiki / (root) / nx_embed_live_gate.nx

nx_embed_live_gate.nx source

↩ module page · 159 lines · 5693 B

1// nx_embed_live_gate.nx -- LIVE probe of the RUNNING no-float serve (E-arc e1): proves the /embed 2// capability is API-reachable on a real socket, not just in-process. argv[1]=port (default 8033 = 3// the coder no-float serve; registry: 8031 survey, 8032 instruct app, 11434/11438 f32 seats). 4// T1 GET /health -> 200 + "ok":1 5// T2 POST /embed -> 200 + "ok":1 + "dim":896 (nl2code query through the live daemon) 6// Sovereign client = nx_http_client primitives (the proven of_llm_complete loopback pattern). 7// license_tier: ORIGINAL No hw writes (Rule 26). expect_exit: 0 8import "nx_http_client.nx" 9 10import "nx_connect.nx" // bounded connect: a raw sys_connect hangs ~127s on a black-holed host 11func el_ws(s: *u8) -> i64 { 12 var n: i64 = 0 13 while s[n] != (0 as u8) { n = n + 1 } 14 sys_write(1, s, n) 15 return 0 16} 17 18func el_wdec(v: i64) -> i64 { 19 let b: *u8 = sys_mmap(28) 20 let t: *u8 = sys_mmap(28) 21 var m: i64 = v 22 if m < 0 { el_ws("-" as *u8); m = 0 - m } 23 var k: i64 = 0 24 if m == 0 { t[0] = 48 as u8; k = 1 } 25 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 26 var i: i64 = 0 27 while i < k { b[i] = t[k - 1 - i]; i = i + 1 } 28 sys_write(1, b, k) 29 return 0 30} 31 32func el_find(hay: *u8, hn: i64, needle: *u8) -> i64 { 33 var nl: i64 = 0 34 while needle[nl] != (0 as u8) { nl = nl + 1 } 35 if nl == 0 { return 0 } 36 var i: i64 = 0 37 while i + nl <= hn { 38 var k: i64 = 0 39 var ok: i64 = 1 40 while k < nl { if hay[i+k] != needle[k] { ok = 0; k = nl } else { k = k + 1 } } 41 if ok == 1 { return i } 42 i = i + 1 43 } 44 return 0 - 1 45} 46 47// one round-trip: connect 127.0.0.1:port, send req[0,rn), read all into resp (cap), return bytes (<0 fail) 48func el_rt(port: i64, req: *u8, rn: i64, resp: *u8, cap: i64) -> i64 { 49 let addr: *u8 = sys_mmap(16) 50 nx_http_client_sockaddr_ipv4(addr, 127, 0, 0, 1, port) 51 let fd: i64 = sys_socket(2, 1, 0) 52 if fd < 0 { return 0 - 1 } 53 if nx_connect_bounded(fd, addr, 16, NX_CONN_DEFAULT_MS) < 0 { sys_close(fd); return 0 - 2 } 54 sys_set_socket_timeout(fd, 60) 55 var w: i64 = 0 56 while w < rn { 57 let k: i64 = sys_write(fd, (req as i64 + w) as *u8, rn - w) 58 if k <= 0 { sys_close(fd); return 0 - 3 } 59 w = w + k 60 } 61 var n: i64 = 0 62 var go: i64 = 1 63 while go == 1 { 64 if n >= cap - 1 { go = 0 } else { 65 let r: i64 = sys_read(fd, (resp as i64 + n) as *u8, cap - 1 - n) 66 if r <= 0 { go = 0 } else { n = n + r } 67 } 68 } 69 sys_close(fd) 70 return n 71} 72 73func el_cat(dst: *u8, off: i64, s: *u8) -> i64 { 74 var i: i64 = 0 75 while s[i] != (0 as u8) { dst[off+i] = s[i]; i = i + 1 } 76 return off + i 77} 78 79func main(argc: i64, argv: *i64) -> i64 { 80 var port: i64 = 8033 81 if argc >= 2 { 82 let ps: *u8 = argv[1] as *u8 83 var pv: i64 = 0 84 var pi: i64 = 0 85 while ps[pi] != (0 as u8) { 86 let c: i64 = ps[pi] & 0xff 87 if c >= 48 { if c <= 57 { pv = pv * 10 + (c - 48) } } 88 pi = pi + 1 89 } 90 if pv > 0 { port = pv } 91 } 92 el_ws("[embed-live] probing no-float serve on 127.0.0.1:" as *u8) 93 el_wdec(port) 94 el_ws("\n" as *u8) 95 let resp: *u8 = sys_mmap(65536) 96 // T1: GET /health 97 let hreq: *u8 = "GET /health HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n" as *u8 98 var hl: i64 = 0 99 while hreq[hl] != (0 as u8) { hl = hl + 1 } 100 let hn: i64 = el_rt(port, hreq, hl, resp, 65536) 101 var t1: i64 = 0 102 if hn > 20 { if resp[9]==(50 as u8) { if resp[10]==(48 as u8) { if resp[11]==(48 as u8) { 103 if el_find(resp, hn, "\"ok\":1" as *u8) >= 0 { t1 = 1 } 104 } } } } 105 el_ws("[T1 /health] n=" as *u8) 106 el_wdec(hn) 107 el_ws(" pass=" as *u8) 108 el_wdec(t1) 109 el_ws("\n" as *u8) 110 if hn > 0 { 111 var sh: i64 = hn 112 if sh > 160 { sh = 160 } 113 sys_write(1, resp, sh) 114 el_ws("\n" as *u8) 115 } 116 if t1 == 0 { el_ws("VERDICT RED (daemon down or unhealthy)\n" as *u8); return 1 } 117 // T2: POST /embed 118 let body: *u8 = "{\"text\":\"parse gguf metadata and locate a tensor by name\",\"task\":\"nl2code\",\"kind\":\"query\",\"mode\":\"i8\"}" as *u8 119 var bl: i64 = 0 120 while body[bl] != (0 as u8) { bl = bl + 1 } 121 let req: *u8 = sys_mmap(4096) 122 var o: i64 = el_cat(req, 0, "POST /embed HTTP/1.1\r\nHost: localhost\r\nContent-Type: application/json\r\nContent-Length: " as *u8) 123 // decimal of bl 124 let t: *u8 = sys_mmap(28) 125 var m: i64 = bl 126 var k: i64 = 0 127 if m == 0 { t[0] = 48 as u8; k = 1 } 128 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 129 var ki: i64 = 0 130 while ki < k { req[o] = t[k - 1 - ki]; o = o + 1; ki = ki + 1 } 131 o = el_cat(req, o, "\r\nConnection: close\r\n\r\n" as *u8) 132 o = el_cat(req, o, body) 133 let t0: i64 = sys_now_ms() 134 let en: i64 = el_rt(port, req, o, resp, 65536) 135 let t1ms: i64 = sys_now_ms() 136 var t2: i64 = 0 137 if en > 60 { if resp[9]==(50 as u8) { if resp[10]==(48 as u8) { if resp[11]==(48 as u8) { 138 if el_find(resp, en, "\"ok\":1" as *u8) >= 0 { if el_find(resp, en, "\"dim\":896" as *u8) >= 0 { t2 = 1 } } 139 } } } } 140 el_ws("[T2 /embed] n=" as *u8) 141 el_wdec(en) 142 el_ws(" ms=" as *u8) 143 el_wdec(t1ms - t0) 144 el_ws(" pass=" as *u8) 145 el_wdec(t2) 146 el_ws("\n" as *u8) 147 if en > 0 { 148 var se: i64 = en 149 if se > 220 { se = 220 } 150 sys_write(1, resp, se) 151 el_ws("...\n" as *u8) 152 } 153 if t2 == 1 { 154 el_ws("VERDICT GREEN embed-live (the /embed capability is served on a real socket)\n" as *u8) 155 return 0 156 } 157 el_ws("VERDICT RED (embed route failed)\n" as *u8) 158 return 1 159}