code wiki / _hdl_build / nx_backlog_gen.nx

nx_backlog_gen.nx source

↩ module page · 287 lines · 10960 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" 14// ★BUFFER SIZES ARE AUTHORED; THEIR READ CAPS ARE DERIVED. These were BG_MAGIC_65535 and BG_MAGIC_8191 15// sitting beside BG_MAGIC_65536 and BG_MAGIC_8192 -- names that restate the value and hide the fact that 16// each cap is simply "one less than its buffer", leaving room for the terminator. Written as two authored 17// numbers, moving a buffer size and forgetting its cap gives a short read that still compiles and still 18// looks plausible. Derived, there is nowhere left to type it wrong. 19const BG_CBUF_BYTES: i64 = 65536 // conf-file read buffer 20const BG_CBUF_CAP: i64 = BG_CBUF_BYTES - 1 21const BG_SETBUF_BYTES: i64 = 8192 // backlog-set read buffer 22const BG_SETBUF_CAP: i64 = BG_SETBUF_BYTES - 1 23const BG_ROWBUF_BYTES: i64 = 2048 // one assembled output row 24const BG_PACK_BASE: i64 = 1000000 // res = gen*BASE + skip, so skip must stay below it 25 26const BG_SET: *u8 = "knowledge/registry/census_set.tsv" 27const BG_QUEUE: *u8 = "knowledge/registry/assignment_queue.tsv" 28const BG_LOG: *u8 = "knowledge/status/backlog_gen.log" 29const BG_MODE: i64 = 420 30// dedup reads the WHOLE queue into qbuf; a too-small buffer silently truncates the read so rows 31// appended past the boundary become invisible to the idempotency probe -> DUPLICATES every re-run. 32// (latent: triggered 2026-06-14 when the queue crossed 256KB.) Size generously + GUARD on truncation 33// (fail loud, never silently duplicate). Raise BG_QCAP if the guard ever fires. 34const BG_QCAP: i64 = 8388608 35 36func bg_w(fd: i64, s: *u8) -> i64 { 37 var n: i64 = 0 38 while s[n] != 0 as u8 { n = n + 1 } 39 sys_write(fd, s, n) 40 return 0 41} 42 43func bg_wn(fd: i64, n: i64) -> i64 { 44 if n == 0 { sys_write(fd, "0" as *u8, 1); return 0 } 45 var m: i64 = n 46 let d: *u8 = sys_mmap(24) 47 var k: i64 = 0 48 while m > 0 { d[k] = (0x30 + (m % 10)) as u8; m = m / 10; k = k + 1 } 49 var i: i64 = k - 1 50 while i >= 0 { let o: *u8 = sys_mmap(1); o[0] = d[i]; sys_write(fd, o, 1); i = i - 1 } 51 return 0 52} 53 54func bg_read_all(path: *u8, buf: *u8, cap: i64) -> i64 { 55 let fd: i64 = sys_openat_rd(path) 56 if fd < 0 { return 0 - 1 } 57 var n: i64 = 0 58 var r: i64 = 1 59 while r > 0 { 60 let dst: *u8 = ((buf as i64) + n) as *u8 61 r = sys_read(fd, dst, cap - n) 62 if r > 0 { n = n + r } 63 } 64 sys_close(fd) 65 return n 66} 67 68func bg_app(buf: *u8, off: i64, s: *u8) -> i64 { 69 var o: i64 = off 70 var i: i64 = 0 71 while s[i] != 0 as u8 { buf[o] = s[i]; o = o + 1; i = i + 1 } 72 return o 73} 74 75func bg_appc(buf: *u8, off: i64, c: i64) -> i64 { 76 buf[off] = c as u8 77 return off + 1 78} 79 80func bg_appr(buf: *u8, off: i64, src: *u8, a: i64, b: i64) -> i64 { 81 var o: i64 = off 82 var i: i64 = a 83 while i < b { buf[o] = src[i]; o = o + 1; i = i + 1 } 84 return o 85} 86 87func bg_has(hay: *u8, hn: i64, nd: *u8, na: i64, nb: i64) -> i64 { 88 let nn: i64 = nb - na 89 if nn < 1 { return 0 } 90 var i: i64 = 0 91 while i + nn <= hn { 92 var j: i64 = 0 93 var ok: i64 = 1 94 while j < nn { 95 if hay[i + j] != nd[na + j] { ok = 0; j = nn } else { j = j + 1 } 96 } 97 if ok == 1 { return 1 } 98 i = i + 1 99 } 100 return 0 101} 102 103func bg_find(buf: *u8, from: i64, lim: i64, c: i64) -> i64 { 104 var i: i64 = from 105 var done: i64 = 0 106 while done == 0 { 107 if i >= lim { done = 1 } else { 108 if buf[i] == (c as u8) { done = 1 } else { i = i + 1 } 109 } 110 } 111 return i 112} 113 114func bg_status_pfx(cbuf: *u8, ls: i64, le: i64) -> i64 { 115 if le - ls > 7 { 116 if cbuf[ls] == (65 as u8) { 117 if cbuf[ls + 1] == (66 as u8) { 118 if cbuf[ls + 6] == (9 as u8) { return ls + 7 } 119 } 120 } 121 } 122 if le - ls > 4 { 123 if cbuf[ls] == (84 as u8) { 124 if cbuf[ls + 1] == (79 as u8) { 125 if cbuf[ls + 3] == (9 as u8) { return ls + 4 } 126 } 127 } 128 } 129 return 0 - 1 130} 131 132// process one census: returns gen*1000000 + skip, or -1 on read failure. 133func bg_gen_one(cpath: *u8, rung: *u8, cbuf: *u8, qbuf: *u8, row: *u8, lfd: i64) -> i64 { 134 let cn: i64 = bg_read_all(cpath, cbuf, BG_CBUF_CAP) 135 if cn < 1 { return 0 - 1 } 136 let qn: i64 = bg_read_all(BG_QUEUE, qbuf, BG_QCAP - 1) 137 if qn < 1 { return 0 - 1 } 138 // truncation guard: a partial queue read blinds the dedup probe -> silent duplicates. Fail loud. 139 if qn >= BG_QCAP - 1 { 140 if lfd >= 0 { bg_w(lfd, "BACKLOGGEN queue-exceeds-dedup-buffer raise-BG_QCAP verdict=RED\n" as *u8) } 141 bg_w(1, "BACKLOGGEN queue-exceeds-dedup-buffer raise-BG_QCAP verdict=RED\n" as *u8) 142 return 0 - 1 143 } 144 let qfd: i64 = sys_openat_append(BG_QUEUE, BG_MODE) 145 if qfd < 0 { return 0 - 1 } 146 if qbuf[qn - 1] != (10 as u8) { 147 let nl: *u8 = sys_mmap(1) 148 nl[0] = 10 as u8 149 sys_write(qfd, nl, 1) 150 } 151 var scanned: i64 = 0 152 var gen: i64 = 0 153 var skip: i64 = 0 154 var ls: i64 = 0 155 while ls < cn { 156 let le: i64 = bg_find(cbuf, ls, cn, 10) 157 if le > ls { 158 if cbuf[ls] != (35 as u8) { 159 scanned = scanned + 1 160 let fs: i64 = bg_status_pfx(cbuf, ls, le) 161 if fs >= 0 { 162 let fe: i64 = bg_find(cbuf, fs, le, 9) 163 var po: i64 = 0 164 po = bg_appc(row, po, 10) 165 po = bg_app(row, po, "GEN-" as *u8) 166 po = bg_app(row, po, rung) 167 po = bg_appc(row, po, 45) 168 po = bg_appr(row, po, cbuf, fs, fe) 169 po = bg_appc(row, po, 9) 170 if bg_has(qbuf, qn, row, 0, po) == 1 { 171 skip = skip + 1 172 } else { 173 po = bg_app(row, po, rung) 174 po = bg_appc(row, po, 9) 175 po = bg_app(row, po, "5" as *u8) 176 po = bg_appc(row, po, 9) 177 po = bg_app(row, po, "M" as *u8) 178 po = bg_appc(row, po, 9) 179 po = bg_app(row, po, "Builder" as *u8) 180 po = bg_appc(row, po, 9) 181 po = bg_app(row, po, "TODO" as *u8) 182 po = bg_appc(row, po, 9) 183 po = bg_app(row, po, "-" as *u8) 184 po = bg_appc(row, po, 9) 185 po = bg_app(row, po, "gen-" as *u8) 186 po = bg_appr(row, po, cbuf, fs, fe) 187 po = bg_app(row, po, "-gate" as *u8) 188 po = bg_appc(row, po, 9) 189 po = bg_app(row, po, "capability from census: " as *u8) 190 po = bg_appr(row, po, cbuf, fs, fe) 191 po = bg_app(row, po, " -- generated by nx_backlog_gen" as *u8) 192 po = bg_appc(row, po, 10) 193 let rp: *u8 = ((row as i64) + 1) as *u8 194 sys_write(qfd, rp, po - 1) 195 gen = gen + 1 196 } 197 } 198 } 199 } 200 ls = le + 1 201 } 202 sys_close(qfd) 203 if lfd >= 0 { 204 bg_w(lfd, "CENSUS file=" as *u8) 205 bg_w(lfd, cpath) 206 bg_w(lfd, " rung=" as *u8) 207 bg_w(lfd, rung) 208 bg_w(lfd, " features=" as *u8) 209 bg_wn(lfd, scanned) 210 bg_w(lfd, " generated=" as *u8) 211 bg_wn(lfd, gen) 212 bg_w(lfd, " verdict=GREEN\n" as *u8) 213 } 214 return gen * BG_PACK_BASE + skip 215} 216 217func main() -> i64 { 218 let setbuf: *u8 = sys_mmap(BG_SETBUF_BYTES) 219 let cbuf: *u8 = sys_mmap(BG_CBUF_BYTES) 220 let qbuf: *u8 = sys_mmap(BG_QCAP) 221 let row: *u8 = sys_mmap(BG_ROWBUF_BYTES) 222 let pbuf: *u8 = sys_mmap(512) 223 let rbuf: *u8 = sys_mmap(64) 224 let sn: i64 = bg_read_all(BG_SET, setbuf, BG_SETBUF_CAP) 225 if sn < 1 { bg_w(1, "BACKLOGGEN census-set-missing verdict=RED\n" as *u8); sys_exit(1) } 226 let lfd: i64 = sys_openat_append(BG_LOG, BG_MODE) 227 var files: i64 = 0 228 var totg: i64 = 0 229 var tots: i64 = 0 230 var allok: i64 = 1 231 var ls: i64 = 0 232 while ls < sn { 233 let le: i64 = bg_find(setbuf, ls, sn, 10) 234 if le > ls { 235 if setbuf[ls] != (35 as u8) { 236 let tb: i64 = bg_find(setbuf, ls, le, 9) 237 if tb < le { 238 var i: i64 = ls 239 var o: i64 = 0 240 while i < tb { pbuf[o] = setbuf[i]; o = o + 1; i = i + 1 } 241 pbuf[o] = 0 as u8 242 i = tb + 1 243 o = 0 244 while i < le { rbuf[o] = setbuf[i]; o = o + 1; i = i + 1 } 245 rbuf[o] = 0 as u8 246 files = files + 1 247 let res: i64 = bg_gen_one(pbuf, rbuf, cbuf, qbuf, row, lfd) 248 if res < 0 { 249 allok = 0 250 if lfd >= 0 { 251 bg_w(lfd, "CENSUS file=" as *u8) 252 bg_w(lfd, pbuf) 253 bg_w(lfd, " verdict=RED\n" as *u8) 254 } 255 bg_w(1, "CENSUS-RED file=" as *u8) 256 bg_w(1, pbuf) 257 bg_w(1, "\n" as *u8) 258 } else { 259 totg = totg + res / BG_PACK_BASE 260 tots = tots + res % BG_PACK_BASE 261 } 262 } 263 } 264 } 265 ls = le + 1 266 } 267 if lfd >= 0 { 268 bg_w(lfd, "BACKLOGGEN files=" as *u8) 269 bg_wn(lfd, files) 270 bg_w(lfd, " generated=" as *u8) 271 bg_wn(lfd, totg) 272 bg_w(lfd, " skipped=" as *u8) 273 bg_wn(lfd, tots) 274 if allok == 1 { bg_w(lfd, " verdict=GREEN\n" as *u8) } else { bg_w(lfd, " verdict=RED\n" as *u8) } 275 sys_close(lfd) 276 } 277 bg_w(1, "BACKLOGGEN files=" as *u8) 278 bg_wn(1, files) 279 bg_w(1, " generated=" as *u8) 280 bg_wn(1, totg) 281 bg_w(1, " skipped=" as *u8) 282 bg_wn(1, tots) 283 if allok == 1 { bg_w(1, " verdict=GREEN\n" as *u8) } else { bg_w(1, " verdict=RED\n" as *u8) } 284 if allok != 1 { sys_exit(1) } 285 sys_exit(0) 286 return 0 287}