code wiki / _hdl_build / nx_gen_real_gate.nx
nx_gen_real_gate.nx source
↩ module page · 131 lines · 7538 B
1// nx_gen_real_gate.nx -- R3: drive the sovereign orchestrator against the REAL Elder AI sd-server
2// (127.0.0.1:7861, reachable from WSL2 via mirrored networking). Forks the orchestrator
3// (go_serve_conn) pointed at the live GPU engine, then as an HTTP client POSTs /api/generate ->
4// the orchestrator dispatches a real batch to the 5080, decodes/injects-GENREC/ingests/blobs each
5// image, and serves /img/<cid>. Blobs land in the Windows scratchpad so a real generated PNG can be
6// eyeballed. Asserts: 2 real images, substantial PNG bytes served. license_tier: ORIGINAL
7import "nx_gen_orchestrator.nx"
8import "nx_connect.nx" // bounded connect: a raw sys_connect hangs ~127s on a black-holed host
9import "nx_syscalls.nx"
10
11func og(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
12func og_slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
13func og_cat(dst: *u8, off: i64, s: *u8) -> i64 { var o: i64=off; var i: i64=0; while s[i]!=(0 as u8){dst[o]=s[i]; o=o+1; i=i+1} return o }
14func og_catb(dst: *u8, off: i64, src: *u8, n: i64) -> i64 { var o: i64=off; var i: i64=0; while i<n {dst[o]=src[i]; o=o+1; i=i+1} return o }
15func og_itoa(dst: *u8, off: i64, v: i64) -> i64 { let t: *u8=sys_mmap(28); var m: i64=v; 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 o: i64=off; var q: i64=k-1; while q>=0{dst[o]=t[q];o=o+1;q=q-1} return o }
16func og_find(buf: *u8, n: i64, needle: *u8) -> i64 {
17 let nl: i64 = og_slen(needle)
18 if nl==0 {return 0}
19 var i: i64=0
20 while i+nl<=n { var j: i64=0; var ok: i64=1; while j<nl { if (buf[i+j]&0xff)!=(needle[j]&0xff){ok=0;j=nl} else {j=j+1} } if ok==1{return i} i=i+1 }
21 return 0-1
22}
23func og_listen(port: i64) -> i64 {
24 let lfd: i64 = sys_socket(2,1,0); if lfd<0 {return 0-1}
25 let optv: *u8 = sys_mmap(4); optv[0]=1 as u8; sys_setsockopt(lfd,1,2,optv,4)
26 let addr: *u8 = sys_mmap(16)
27 addr[0]=2 as u8; addr[1]=0 as u8; addr[2]=((port>>8)&0xff) as u8; addr[3]=(port&0xff) as u8
28 addr[4]=127 as u8; addr[5]=0 as u8; addr[6]=0 as u8; addr[7]=1 as u8
29 var zi: i64=8; while zi<16 {addr[zi]=0 as u8; zi=zi+1}
30 if sys_bind(lfd, addr, 16) < 0 { return 0-1 }
31 if sys_listen(lfd, 16) < 0 { return 0-1 }
32 return lfd
33}
34func og_connect(port: i64) -> i64 {
35 let fd: i64 = sys_socket(2,1,0); if fd<0 {return 0-1}
36 sys_set_socket_timeout(fd, 200)
37 let a: *u8 = sys_mmap(16)
38 a[0]=2 as u8; a[1]=0 as u8; a[2]=((port>>8)&0xff) as u8; a[3]=(port&0xff) as u8
39 a[4]=127 as u8; a[5]=0 as u8; a[6]=0 as u8; a[7]=1 as u8
40 var zi: i64=8; while zi<16 {a[zi]=0 as u8; zi=zi+1}
41 if nx_connect_bounded(fd, a, 16, NX_CONN_DEFAULT_MS) != 0 { sys_close(fd); return 0-1 }
42 return fd
43}
44func og_freshns(prefix: *u8) -> i64 {
45 let mfn: *u8 = sys_mmap(256); var mo: i64=0
46 while (prefix[mo]&0xff)!=0 { mfn[mo]=prefix[mo]; mo=mo+1 }
47 let suf: *u8 = "manifest.txt" as *u8; var so: i64=0
48 while (suf[so]&0xff)!=0 { mfn[mo+so]=suf[so]; so=so+1 }
49 mfn[mo+so]=0 as u8
50 let fd: i64 = sys_openat_wr(mfn, 0x1a4); if fd>=0 {sys_close(fd)}
51 return 0
52}
53func og_truncate(path: *u8) -> i64 { let fd: i64 = sys_openat_wr(path, 0x1a4); if fd>=0 {sys_close(fd)} return 0 }
54func og_readresp(fd: i64, resp: *u8, cap: i64) -> i64 {
55 var rn: i64 = 0; var g: i64 = 1
56 while g==1 { let r: i64 = sys_read(fd, (resp as i64+rn) as *u8, cap-rn); if r<=0 {g=0} else { rn=rn+r; if rn>=cap {g=0} } }
57 return rn
58}
59
60func main() -> i64 {
61 let OPORT: i64 = 39280
62 let storep: *u8 = "/tmp/genreal-" as *u8
63 let blobd: *u8 = "/mnt/c/Users/elder/AppData/Local/Temp/claude/C--Users-elder/9926a27d-9147-4523-9f7f-a6f1091ee823/scratchpad/genreal-" as *u8
64 let sidep: *u8 = "/tmp/genreal_cids.tsv" as *u8
65 let hostn: *u8 = "laptop-5080" as *u8
66 og_freshns(storep)
67 og_truncate(sidep)
68
69 let cfg: *i64 = sys_mmap(8*16) as *i64
70 cfg[0]=127; cfg[1]=0; cfg[2]=0; cfg[3]=1; cfg[4]=7861
71 cfg[5]=storep as i64; cfg[6]=blobd as i64; cfg[7]=sidep as i64; cfg[8]=hostn as i64
72
73 let ofd: i64 = og_listen(OPORT); if ofd<0 { og("R3 FAIL: orch listen\n" as *u8); sys_exit(1); return 1 }
74
75 let pidO: i64 = sys_fork()
76 if pidO == 0 {
77 var i: i64 = 0
78 while i < 3 { let c: i64 = sys_accept(ofd); if c>=0 { go_serve_conn(c, cfg); sys_close(c) } i=i+1 }
79 sys_exit(0)
80 }
81 sys_close(ofd)
82
83 // POST /api/generate -> dispatch to the real 5080
84 let fd1: i64 = og_connect(OPORT)
85 if fd1 < 0 { og("R3 FAIL: connect orch\n" as *u8); sys_exit(1); return 1 }
86 let gbody: *u8 = "{\"prompt\":\"a red fox sitting in a snowy pine forest, wildlife photograph\",\"negative\":\"blurry, lowres\",\"width\":512,\"height\":512,\"steps\":8,\"cfg\":\"1.0\",\"seed\":2026,\"count\":2,\"sampler\":\"euler\"}" as *u8
87 var gbl: i64 = 0; while gbody[gbl]!=(0 as u8){gbl=gbl+1}
88 let greq: *u8 = sys_mmap(4096); var go1: i64 = 0
89 go1 = og_cat(greq, go1, "POST /api/generate HTTP/1.1\r\nHost: x\r\nConnection: close\r\nContent-Type: application/json\r\nContent-Length: " as *u8)
90 go1 = og_itoa(greq, go1, gbl); go1 = og_cat(greq, go1, "\r\n\r\n" as *u8); go1 = og_catb(greq, go1, gbody, gbl)
91 sys_write(fd1, greq, go1)
92 let gresp: *u8 = sys_mmap(131072)
93 let grn: i64 = og_readresp(fd1, gresp, 131072)
94 sys_close(fd1)
95 if og_find(gresp, grn, "200 OK" as *u8) < 0 { og("R3 FAIL: /api/generate not 200\n" as *u8); sys_write(1, gresp, grn); sys_exit(1); return 1 }
96 if og_find(gresp, grn, "\"count\":2" as *u8) < 0 { og("R3 FAIL: did not get 2 images from the real engine\n" as *u8); sys_write(1, gresp, grn); sys_exit(1); return 1 }
97 og("R3 ROW real-generate: GREEN -- POST /api/generate -> the 5080 produced 2 real images, ingested+blobbed\n" as *u8)
98 sys_write(1, gresp, grn); og("\n" as *u8)
99
100 // extract first cid
101 let cidp: i64 = og_find(gresp, grn, "\"cids\":[\"" as *u8)
102 if cidp < 0 { og("R3 FAIL: no cids[]\n" as *u8); sys_exit(1); return 1 }
103 let cid: *u8 = sys_mmap(128)
104 var ci: i64 = cidp + 9
105 var co: i64 = 0
106 var cstop: i64 = 0
107 while cstop==0 { if ci>=grn { cstop=1 } else { if (gresp[ci]&0xff)==34 { cstop=1 } else { cid[co]=gresp[ci]; co=co+1; ci=ci+1 } } }
108 cid[co]=0 as u8
109
110 // GET /img/<cid> -> a real PNG
111 let fd2: i64 = og_connect(OPORT)
112 if fd2 < 0 { og("R3 FAIL: connect orch (img)\n" as *u8); sys_exit(1); return 1 }
113 let ireq: *u8 = sys_mmap(256); var io: i64 = 0
114 io = og_cat(ireq, io, "GET /img/" as *u8); io = og_catb(ireq, io, cid, co)
115 io = og_cat(ireq, io, " HTTP/1.1\r\nHost: x\r\nConnection: close\r\n\r\n" as *u8)
116 sys_write(fd2, ireq, io)
117 let iresp: *u8 = sys_mmap(1048576)
118 let irn: i64 = og_readresp(fd2, iresp, 1048576)
119 sys_close(fd2)
120 if og_find(iresp, irn, "200 OK" as *u8) < 0 { og("R3 FAIL: /img not 200\n" as *u8); sys_exit(1); return 1 }
121 if og_find(iresp, irn, "image/png" as *u8) < 0 { og("R3 FAIL: /img not image/png\n" as *u8); sys_exit(1); return 1 }
122 if irn < 50000 { og("R3 FAIL: /img body too small to be a real 512x512 image\n" as *u8); sys_exit(1); return 1 }
123 og("R3 ROW serve-real-img: GREEN -- GET /img/<cid> -> 200 image/png, substantial real PNG over the wire\n" as *u8)
124 og("R3 cid0: " as *u8); sys_write(1, cid, co); og("\n" as *u8)
125
126 let stx: *i64 = sys_mmap(16) as *i64
127 sys_wait4(pidO, stx, 0)
128 og("GEN-REAL-GATE GREEN -- the sovereign orchestrator drove the live 5080 end-to-end (gen->ingest->gallery->serve), off Docker\n" as *u8)
129 sys_exit(0)
130 return 0
131}