code wiki / _hdl_build / nx_gen_pipeline_gate.nx
nx_gen_pipeline_gate.nx source
↩ module page · 150 lines · 7206 B
1// nx_gen_pipeline_gate.nx -- offline gate for the GENERATE pipeline core (no sockets). Builds a
2// synthetic GPU-worker response by base64-encoding a real PNG TWICE (= a batch of 2), runs
3// gp_response, and asserts: 2 images processed with DISTINCT CIDs (seed incremented), each PNG
4// blob written (PNG sig + larger than base = GENREC tEXt injected), the CID->path sidecar grew to
5// 2 lines, a re-run is idempotent (sidecar stays 2), and a response lacking images[] returns -1.
6// license_tier: ORIGINAL
7import "nx_gen_pipeline.nx"
8import "nx_syscalls.nx"
9
10func tg(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
11func tg_cideq(a: *u8, b: *u8) -> i64 { var i: i64=0; while i<69 { if (a[i]&0xff)!=(b[i]&0xff){return 0} i=i+1 } return 1 }
12
13func tg_basepng(b: *u8) -> i64 {
14 b[0]=137 as u8; b[1]=80 as u8; b[2]=78 as u8; b[3]=71 as u8
15 b[4]=13 as u8; b[5]=10 as u8; b[6]=26 as u8; b[7]=10 as u8
16 b[8]=0 as u8; b[9]=0 as u8; b[10]=0 as u8; b[11]=14 as u8
17 b[12]=73 as u8; b[13]=72 as u8; b[14]=68 as u8; b[15]=82 as u8
18 b[16]=0 as u8; b[17]=0 as u8; b[18]=0 as u8; b[19]=0 as u8; b[20]=1 as u8
19 b[21]=0 as u8; b[22]=0 as u8; b[23]=0 as u8; b[24]=1 as u8; b[25]=8 as u8
20 b[26]=6 as u8; b[27]=0 as u8; b[28]=0 as u8; b[29]=0 as u8
21 b[30]=115 as u8; b[31]=22 as u8; b[32]=206 as u8; b[33]=242 as u8
22 b[34]=0 as u8; b[35]=0 as u8; b[36]=0 as u8; b[37]=0 as u8
23 b[38]=73 as u8; b[39]=69 as u8; b[40]=78 as u8; b[41]=68 as u8
24 b[42]=174 as u8; b[43]=66 as u8; b[44]=96 as u8; b[45]=130 as u8
25 return 46
26}
27
28func tg_enc_char(v: i64) -> i64 {
29 let x: i64 = v & 0x3f
30 if x < 26 { return 65 + x }
31 if x < 52 { return 97 + (x - 26) }
32 if x < 62 { return 48 + (x - 52) }
33 if x == 62 { return 43 }
34 return 47
35}
36func tg_b64_encode(inp: *u8, n: i64, out: *u8) -> i64 {
37 var i: i64 = 0
38 var o: i64 = 0
39 while i + 3 <= n {
40 let b0: i64 = inp[i] & 0xff
41 let b1: i64 = inp[i+1] & 0xff
42 let b2: i64 = inp[i+2] & 0xff
43 out[o] = tg_enc_char(b0 >> 2) as u8
44 out[o+1] = tg_enc_char(((b0 << 4) | (b1 >> 4)) & 0x3f) as u8
45 out[o+2] = tg_enc_char(((b1 << 2) | (b2 >> 6)) & 0x3f) as u8
46 out[o+3] = tg_enc_char(b2 & 0x3f) as u8
47 o = o + 4; i = i + 3
48 }
49 let rem: i64 = n - i
50 if rem == 1 {
51 let b0: i64 = inp[i] & 0xff
52 out[o] = tg_enc_char(b0 >> 2) as u8
53 out[o+1] = tg_enc_char((b0 << 4) & 0x3f) as u8
54 out[o+2] = 61 as u8; out[o+3] = 61 as u8; o = o + 4
55 }
56 if rem == 2 {
57 let b0: i64 = inp[i] & 0xff
58 let b1: i64 = inp[i+1] & 0xff
59 out[o] = tg_enc_char(b0 >> 2) as u8
60 out[o+1] = tg_enc_char(((b0 << 4) | (b1 >> 4)) & 0x3f) as u8
61 out[o+2] = tg_enc_char((b1 << 2) & 0x3f) as u8
62 out[o+3] = 61 as u8; o = o + 4
63 }
64 return o
65}
66
67func tg_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 }
68func tg_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 }
69
70func tg_freshns(prefix: *u8) -> i64 {
71 let mfn: *u8 = sys_mmap(128)
72 var mo: i64 = 0
73 while (prefix[mo]&0xff) != 0 { mfn[mo]=prefix[mo]; mo=mo+1 }
74 let suf: *u8 = "manifest.txt" as *u8
75 var so: i64=0
76 while (suf[so]&0xff)!=0 { mfn[mo+so]=suf[so]; so=so+1 }
77 mfn[mo+so]=0 as u8
78 let fd: i64 = sys_openat_wr(mfn, 0x1a4)
79 if fd>=0 { sys_close(fd) }
80 return 0
81}
82func tg_truncate(path: *u8) -> i64 { let fd: i64 = sys_openat_wr(path, 0x1a4); if fd>=0 { sys_close(fd) } return 0 }
83func tg_linecount(path: *u8) -> i64 {
84 let lp: *i64 = sys_mmap(16) as *i64
85 let b: *u8 = sys_read_file(path, lp)
86 if (b as i64)==0 { return 0 }
87 let n: i64 = lp[0]
88 var i: i64=0; var c: i64=0
89 while i<n { if (b[i]&0xff)==10 { c=c+1 } i=i+1 }
90 return c
91}
92
93func main() -> i64 {
94 let png: *u8 = sys_mmap(256)
95 let plen: i64 = tg_basepng(png)
96 let b64: *u8 = sys_mmap(512)
97 let b64len: i64 = tg_b64_encode(png, plen, b64)
98
99 let resp: *u8 = sys_mmap(4096)
100 var o: i64 = 0
101 o = tg_cat(resp, o, "{\"images\":[\"" as *u8)
102 o = tg_catb(resp, o, b64, b64len)
103 o = tg_cat(resp, o, "\",\"" as *u8)
104 o = tg_catb(resp, o, b64, b64len)
105 o = tg_cat(resp, o, "\"]}" as *u8)
106 let resplen: i64 = o
107
108 let storeprefix: *u8 = "/tmp/genpipe-" as *u8
109 let blobdir: *u8 = "/tmp/genpipeblob-" as *u8
110 let sidecar: *u8 = "/tmp/genpipe_cids.tsv" as *u8
111 tg_freshns(storeprefix)
112 tg_truncate(sidecar)
113
114 let cids: *u8 = sys_mmap(640)
115 let count: i64 = gp_response(resp, resplen, 1000, "zit" as *u8, "euler" as *u8, "8" as *u8, "laptop" as *u8, "a red fox" as *u8, storeprefix, blobdir, sidecar, cids, 8)
116 if count != 2 { tg("PIPE FAIL: count != 2\n" as *u8); sys_exit(1); return 1 }
117 let cid0: *u8 = cids
118 let cid1: *u8 = (cids as i64 + 80) as *u8
119 if tg_cideq(cid0, cid1) == 1 { tg("PIPE FAIL: cid0==cid1 (seed not a CID factor)\n" as *u8); sys_exit(1); return 1 }
120 tg("GATE ROW batch: GREEN -- 2 images processed, DISTINCT CIDs (seed incremented per image)\n" as *u8)
121
122 let path0: *u8 = sys_mmap(512)
123 gp_blob_path(blobdir, cid0, path0)
124 let lp: *i64 = sys_mmap(16) as *i64
125 let bb: *u8 = sys_read_file(path0, lp)
126 if (bb as i64) == 0 { tg("PIPE FAIL: blob0 not written\n" as *u8); sys_exit(1); return 1 }
127 if lp[0] <= 46 { tg("PIPE FAIL: blob0 not larger than base (GENREC tEXt not injected)\n" as *u8); sys_exit(1); return 1 }
128 if (bb[0]&0xff) != 137 { tg("PIPE FAIL: blob0 not a PNG (bad signature)\n" as *u8); sys_exit(1); return 1 }
129 tg("GATE ROW blob: GREEN -- PNG blob written (sig ok, larger than base = tEXt injected), servable via sidecar path\n" as *u8)
130
131 if tg_linecount(sidecar) != 2 { tg("PIPE FAIL: sidecar != 2 lines\n" as *u8); sys_exit(1); return 1 }
132 tg("GATE ROW sidecar: GREEN -- 2 CID->path lines (the gallery serves /img/<cid> from these)\n" as *u8)
133
134 let cids2: *u8 = sys_mmap(640)
135 let count2: i64 = gp_response(resp, resplen, 1000, "zit" as *u8, "euler" as *u8, "8" as *u8, "laptop" as *u8, "a red fox" as *u8, storeprefix, blobdir, sidecar, cids2, 8)
136 if count2 != 2 { tg("PIPE FAIL: re-run count != 2\n" as *u8); sys_exit(1); return 1 }
137 if tg_linecount(sidecar) != 2 { tg("PIPE FAIL: sidecar grew on re-ingest (not idempotent)\n" as *u8); sys_exit(1); return 1 }
138 tg("GATE ROW idempotent: GREEN -- re-run dedups (same factors->same CID), sidecar still 2 lines\n" as *u8)
139
140 let bad: *u8 = "{\"foo\":[\"x\"]}" as *u8
141 var bn: i64=0; while bad[bn]!=(0 as u8){bn=bn+1}
142 let badcids: *u8 = sys_mmap(640)
143 let badc: i64 = gp_response(bad, bn, 1, "m" as *u8, "s" as *u8, "1" as *u8, "h" as *u8, "p" as *u8, storeprefix, blobdir, sidecar, badcids, 8)
144 if badc >= 0 { tg("PIPE FAIL: response without images[] did not return -1\n" as *u8); sys_exit(1); return 1 }
145 tg("GATE ROW no-images: GREEN -- response lacking images[] -> -1 (no spurious work)\n" as *u8)
146
147 tg("GEN-PIPELINE-GATE 5/5 GREEN -- batch+distinct-CID + blob-written + sidecar + idempotent + no-images-guard\n" as *u8)
148 sys_exit(0)
149 return 0
150}