code wiki / _hdl_build / nx_backlog_gen.nx
nx_backlog_gen.nx source
↩ module page · 282 lines · 10274 B
1// nx_backlog_gen.nx -- X-Q-001 (w9, Q-arc): THE QUEUE FEEDS ITSELF. v2:
2// walks knowledge/registry/census_set.tsv (path \t rung per row) so ONE
3// organ serves every census. Each census file (contract: status \t
4// feature \t union \t evidence) contributes GEN-<rung>-<feature> queue
5// rows for ABSENT|TOY entries, IDEMPOTENT per file (substring probe
6// "\nGEN-<rung>-<feature>\t" against a fresh queue read). Rows append in
7// ONE write each (no torn rows vs concurrent beats); trailing-newline
8// guarded. Per-file evidence: CENSUS file=<path> rung=<rung> features=N
9// verdict=GREEN; total: BACKLOGGEN files= generated= skipped= verdict=.
10// A missing census file logs RED for that row and the roster continues
11// (fail-loud, no livelock); total verdict RED if any file failed.
12// license_tier: ORIGINAL
13import "nx_syscalls.nx"
14const BG_MAGIC_65535: i64 = 65535
15const BG_MAGIC_1000000: i64 = 1000000
16const BG_MAGIC_8192: i64 = 8192
17const BG_MAGIC_65536: i64 = 65536
18const BG_MAGIC_2048: i64 = 2048
19const BG_MAGIC_8191: i64 = 8191
20
21const BG_SET: *u8 = "knowledge/registry/census_set.tsv"
22const BG_QUEUE: *u8 = "knowledge/registry/assignment_queue.tsv"
23const BG_LOG: *u8 = "knowledge/status/backlog_gen.log"
24const BG_MODE: i64 = 420
25// dedup reads the WHOLE queue into qbuf; a too-small buffer silently truncates the read so rows
26// appended past the boundary become invisible to the idempotency probe -> DUPLICATES every re-run.
27// (latent: triggered 2026-06-14 when the queue crossed 256KB.) Size generously + GUARD on truncation
28// (fail loud, never silently duplicate). Raise BG_QCAP if the guard ever fires.
29const BG_QCAP: i64 = 8388608
30
31func bg_w(fd: i64, s: *u8) -> i64 {
32 var n: i64 = 0
33 while s[n] != 0 as u8 { n = n + 1 }
34 sys_write(fd, s, n)
35 return 0
36}
37
38func bg_wn(fd: i64, n: i64) -> i64 {
39 if n == 0 { sys_write(fd, "0" as *u8, 1); return 0 }
40 var m: i64 = n
41 let d: *u8 = sys_mmap(24)
42 var k: i64 = 0
43 while m > 0 { d[k] = (0x30 + (m % 10)) as u8; m = m / 10; k = k + 1 }
44 var i: i64 = k - 1
45 while i >= 0 { let o: *u8 = sys_mmap(1); o[0] = d[i]; sys_write(fd, o, 1); i = i - 1 }
46 return 0
47}
48
49func bg_read_all(path: *u8, buf: *u8, cap: i64) -> i64 {
50 let fd: i64 = sys_openat_rd(path)
51 if fd < 0 { return 0 - 1 }
52 var n: i64 = 0
53 var r: i64 = 1
54 while r > 0 {
55 let dst: *u8 = ((buf as i64) + n) as *u8
56 r = sys_read(fd, dst, cap - n)
57 if r > 0 { n = n + r }
58 }
59 sys_close(fd)
60 return n
61}
62
63func bg_app(buf: *u8, off: i64, s: *u8) -> i64 {
64 var o: i64 = off
65 var i: i64 = 0
66 while s[i] != 0 as u8 { buf[o] = s[i]; o = o + 1; i = i + 1 }
67 return o
68}
69
70func bg_appc(buf: *u8, off: i64, c: i64) -> i64 {
71 buf[off] = c as u8
72 return off + 1
73}
74
75func bg_appr(buf: *u8, off: i64, src: *u8, a: i64, b: i64) -> i64 {
76 var o: i64 = off
77 var i: i64 = a
78 while i < b { buf[o] = src[i]; o = o + 1; i = i + 1 }
79 return o
80}
81
82func bg_has(hay: *u8, hn: i64, nd: *u8, na: i64, nb: i64) -> i64 {
83 let nn: i64 = nb - na
84 if nn < 1 { return 0 }
85 var i: i64 = 0
86 while i + nn <= hn {
87 var j: i64 = 0
88 var ok: i64 = 1
89 while j < nn {
90 if hay[i + j] != nd[na + j] { ok = 0; j = nn } else { j = j + 1 }
91 }
92 if ok == 1 { return 1 }
93 i = i + 1
94 }
95 return 0
96}
97
98func bg_find(buf: *u8, from: i64, lim: i64, c: i64) -> i64 {
99 var i: i64 = from
100 var done: i64 = 0
101 while done == 0 {
102 if i >= lim { done = 1 } else {
103 if buf[i] == (c as u8) { done = 1 } else { i = i + 1 }
104 }
105 }
106 return i
107}
108
109func bg_status_pfx(cbuf: *u8, ls: i64, le: i64) -> i64 {
110 if le - ls > 7 {
111 if cbuf[ls] == (65 as u8) {
112 if cbuf[ls + 1] == (66 as u8) {
113 if cbuf[ls + 6] == (9 as u8) { return ls + 7 }
114 }
115 }
116 }
117 if le - ls > 4 {
118 if cbuf[ls] == (84 as u8) {
119 if cbuf[ls + 1] == (79 as u8) {
120 if cbuf[ls + 3] == (9 as u8) { return ls + 4 }
121 }
122 }
123 }
124 return 0 - 1
125}
126
127// process one census: returns gen*1000000 + skip, or -1 on read failure.
128func bg_gen_one(cpath: *u8, rung: *u8, cbuf: *u8, qbuf: *u8, row: *u8, lfd: i64) -> i64 {
129 let cn: i64 = bg_read_all(cpath, cbuf, BG_MAGIC_65535)
130 if cn < 1 { return 0 - 1 }
131 let qn: i64 = bg_read_all(BG_QUEUE, qbuf, BG_QCAP - 1)
132 if qn < 1 { return 0 - 1 }
133 // truncation guard: a partial queue read blinds the dedup probe -> silent duplicates. Fail loud.
134 if qn >= BG_QCAP - 1 {
135 if lfd >= 0 { bg_w(lfd, "BACKLOGGEN queue-exceeds-dedup-buffer raise-BG_QCAP verdict=RED\n" as *u8) }
136 bg_w(1, "BACKLOGGEN queue-exceeds-dedup-buffer raise-BG_QCAP verdict=RED\n" as *u8)
137 return 0 - 1
138 }
139 let qfd: i64 = sys_openat_append(BG_QUEUE, BG_MODE)
140 if qfd < 0 { return 0 - 1 }
141 if qbuf[qn - 1] != (10 as u8) {
142 let nl: *u8 = sys_mmap(1)
143 nl[0] = 10 as u8
144 sys_write(qfd, nl, 1)
145 }
146 var scanned: i64 = 0
147 var gen: i64 = 0
148 var skip: i64 = 0
149 var ls: i64 = 0
150 while ls < cn {
151 let le: i64 = bg_find(cbuf, ls, cn, 10)
152 if le > ls {
153 if cbuf[ls] != (35 as u8) {
154 scanned = scanned + 1
155 let fs: i64 = bg_status_pfx(cbuf, ls, le)
156 if fs >= 0 {
157 let fe: i64 = bg_find(cbuf, fs, le, 9)
158 var po: i64 = 0
159 po = bg_appc(row, po, 10)
160 po = bg_app(row, po, "GEN-" as *u8)
161 po = bg_app(row, po, rung)
162 po = bg_appc(row, po, 45)
163 po = bg_appr(row, po, cbuf, fs, fe)
164 po = bg_appc(row, po, 9)
165 if bg_has(qbuf, qn, row, 0, po) == 1 {
166 skip = skip + 1
167 } else {
168 po = bg_app(row, po, rung)
169 po = bg_appc(row, po, 9)
170 po = bg_app(row, po, "5" as *u8)
171 po = bg_appc(row, po, 9)
172 po = bg_app(row, po, "M" as *u8)
173 po = bg_appc(row, po, 9)
174 po = bg_app(row, po, "Builder" as *u8)
175 po = bg_appc(row, po, 9)
176 po = bg_app(row, po, "TODO" as *u8)
177 po = bg_appc(row, po, 9)
178 po = bg_app(row, po, "-" as *u8)
179 po = bg_appc(row, po, 9)
180 po = bg_app(row, po, "gen-" as *u8)
181 po = bg_appr(row, po, cbuf, fs, fe)
182 po = bg_app(row, po, "-gate" as *u8)
183 po = bg_appc(row, po, 9)
184 po = bg_app(row, po, "capability from census: " as *u8)
185 po = bg_appr(row, po, cbuf, fs, fe)
186 po = bg_app(row, po, " -- generated by nx_backlog_gen" as *u8)
187 po = bg_appc(row, po, 10)
188 let rp: *u8 = ((row as i64) + 1) as *u8
189 sys_write(qfd, rp, po - 1)
190 gen = gen + 1
191 }
192 }
193 }
194 }
195 ls = le + 1
196 }
197 sys_close(qfd)
198 if lfd >= 0 {
199 bg_w(lfd, "CENSUS file=" as *u8)
200 bg_w(lfd, cpath)
201 bg_w(lfd, " rung=" as *u8)
202 bg_w(lfd, rung)
203 bg_w(lfd, " features=" as *u8)
204 bg_wn(lfd, scanned)
205 bg_w(lfd, " generated=" as *u8)
206 bg_wn(lfd, gen)
207 bg_w(lfd, " verdict=GREEN\n" as *u8)
208 }
209 return gen * BG_MAGIC_1000000 + skip
210}
211
212func main() -> i64 {
213 let setbuf: *u8 = sys_mmap(BG_MAGIC_8192)
214 let cbuf: *u8 = sys_mmap(BG_MAGIC_65536)
215 let qbuf: *u8 = sys_mmap(BG_QCAP)
216 let row: *u8 = sys_mmap(BG_MAGIC_2048)
217 let pbuf: *u8 = sys_mmap(512)
218 let rbuf: *u8 = sys_mmap(64)
219 let sn: i64 = bg_read_all(BG_SET, setbuf, BG_MAGIC_8191)
220 if sn < 1 { bg_w(1, "BACKLOGGEN census-set-missing verdict=RED\n" as *u8); sys_exit(1) }
221 let lfd: i64 = sys_openat_append(BG_LOG, BG_MODE)
222 var files: i64 = 0
223 var totg: i64 = 0
224 var tots: i64 = 0
225 var allok: i64 = 1
226 var ls: i64 = 0
227 while ls < sn {
228 let le: i64 = bg_find(setbuf, ls, sn, 10)
229 if le > ls {
230 if setbuf[ls] != (35 as u8) {
231 let tb: i64 = bg_find(setbuf, ls, le, 9)
232 if tb < le {
233 var i: i64 = ls
234 var o: i64 = 0
235 while i < tb { pbuf[o] = setbuf[i]; o = o + 1; i = i + 1 }
236 pbuf[o] = 0 as u8
237 i = tb + 1
238 o = 0
239 while i < le { rbuf[o] = setbuf[i]; o = o + 1; i = i + 1 }
240 rbuf[o] = 0 as u8
241 files = files + 1
242 let res: i64 = bg_gen_one(pbuf, rbuf, cbuf, qbuf, row, lfd)
243 if res < 0 {
244 allok = 0
245 if lfd >= 0 {
246 bg_w(lfd, "CENSUS file=" as *u8)
247 bg_w(lfd, pbuf)
248 bg_w(lfd, " verdict=RED\n" as *u8)
249 }
250 bg_w(1, "CENSUS-RED file=" as *u8)
251 bg_w(1, pbuf)
252 bg_w(1, "\n" as *u8)
253 } else {
254 totg = totg + res / BG_MAGIC_1000000
255 tots = tots + res % BG_MAGIC_1000000
256 }
257 }
258 }
259 }
260 ls = le + 1
261 }
262 if lfd >= 0 {
263 bg_w(lfd, "BACKLOGGEN files=" as *u8)
264 bg_wn(lfd, files)
265 bg_w(lfd, " generated=" as *u8)
266 bg_wn(lfd, totg)
267 bg_w(lfd, " skipped=" as *u8)
268 bg_wn(lfd, tots)
269 if allok == 1 { bg_w(lfd, " verdict=GREEN\n" as *u8) } else { bg_w(lfd, " verdict=RED\n" as *u8) }
270 sys_close(lfd)
271 }
272 bg_w(1, "BACKLOGGEN files=" as *u8)
273 bg_wn(1, files)
274 bg_w(1, " generated=" as *u8)
275 bg_wn(1, totg)
276 bg_w(1, " skipped=" as *u8)
277 bg_wn(1, tots)
278 if allok == 1 { bg_w(1, " verdict=GREEN\n" as *u8) } else { bg_w(1, " verdict=RED\n" as *u8) }
279 if allok != 1 { sys_exit(1) }
280 sys_exit(0)
281 return 0
282}