code wiki / _hdl_build / nx_queue_cohesion.nx

nx_queue_cohesion.nx source

↩ module page · 246 lines · 9244 B

1// nx_queue_cohesion.nx -- X-CONS-003: the AUDITOR's anti-sprawl gate 2// (operator law 2026-06-11: "make sure we are cohesive always and avoid 3// sprawl and duplicative work" -- enforced mechanically every beat). 4// DUP-ID two queue rows with the same id -> RED (corruption) 5// DUP-TITLE two queue rows with byte-equal titles -> RED (sprawl) 6// LEDGERGAP open debt-ledger T#/B# ids with NO queue mention -> counted 7// (informational: gaps are WORK to route, not corruption; 8// X-CONS-002 is the sink that drives the count to 0) 9// Composes nx_assign_core (the queue parser) -- no second grammar. 10// argv[1]=queue override argv[2]=ledger override (gates use scratch) 11// Evidence: COHESION row -> stdout + knowledge/status/queue_cohesion.log. 12// Exit: 0 cohesive (gaps allowed), 1 dup found, 101 queue missing. 13// license_tier: ORIGINAL 14 15import "nx_assign_core.nx" 16import "nx_itoa_lib.nx" // shared MSB-first emitter (zero-alloc) 17const K_MAGIC_1048576: i64 = 1048576 18 19func qc_w(fd: i64, s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(fd,s,n); return 0 } 20// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer 21// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the 22// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls). 23// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign. 24func qc_wn(fd: i64, v: i64) -> i64 { nxi_fd(fd, v); return 0 } 25 26// full-length streq (an_streq caps at 64; titles run to 127) 27func qc_streq(a: *u8, b: *u8) -> i64 { 28 var i: i64 = 0 29 while 1 == 1 { 30 if a[i] != b[i] { return 0 } 31 if a[i] == (0 as u8) { return 1 } 32 i = i + 1 33 } 34 return 0 35} 36 37func qc_read(path: *u8, buf: *u8, cap: i64) -> i64 { 38 let fd: i64 = sys_openat_rd(path) 39 if fd < 0 { return 0 } 40 var n: i64 = 0 41 var r: i64 = sys_read(fd, buf, cap - 1) 42 while r > 0 { n = n + r; r = sys_read(fd, buf + n, cap - 1 - n) } 43 sys_close(fd) 44 return n 45} 46 47// does hay (n bytes) contain NUL-terminated pat? 48func qc_has(hay: *u8, n: i64, pat: *u8) -> i64 { 49 var pl: i64 = 0 50 while pat[pl] != (0 as u8) { pl = pl + 1 } 51 if pl == 0 { return 0 } 52 var i: i64 = 0 53 while i + pl <= n { 54 var k: i64 = 0 55 var hit: i64 = 1 56 while k < pl { 57 if hay[i + k] != pat[k] { hit = 0; k = pl } else { k = k + 1 } 58 } 59 if hit == 1 { return 1 } 60 i = i + 1 61 } 62 return 0 63} 64 65// duplicate ids: an_find returns the FIRST row with that id; any row whose 66// own index differs from the first match is a duplicate. 67func qc_dup_ids(cx: *i64, lfd: i64) -> i64 { 68 var dups: i64 = 0 69 var r: i64 = 0 70 while r < cx[0] { 71 let f: i64 = an_find(cx, an_id_at(cx, r)) 72 if f != r { 73 dups = dups + 1 74 qc_w(1, "COHESION-DUP-ID id=" as *u8) 75 qc_w(1, an_id_at(cx, r)) 76 qc_w(1, "\n" as *u8) 77 if lfd >= 0 { 78 qc_w(lfd, "COHESION-DUP-ID id=" as *u8) 79 qc_w(lfd, an_id_at(cx, r)) 80 qc_w(lfd, "\n" as *u8) 81 } 82 } 83 r = r + 1 84 } 85 return dups 86} 87 88// duplicate titles (byte-equal, non-empty, both real rows) 89func qc_dup_titles(cx: *i64, lfd: i64) -> i64 { 90 var dups: i64 = 0 91 var r: i64 = 0 92 while r < cx[0] { 93 let tr: *u8 = an_title_at(cx, r) 94 if tr[0] != (0 as u8) { 95 var q: i64 = 0 96 var hit: i64 = 0 97 while q < r { 98 if hit == 0 { 99 if qc_streq(tr, an_title_at(cx, q)) == 1 { hit = 1 } 100 } 101 q = q + 1 102 } 103 if hit == 1 { 104 dups = dups + 1 105 qc_w(1, "COHESION-DUP-TITLE id=" as *u8) 106 qc_w(1, an_id_at(cx, r)) 107 qc_w(1, "\n" as *u8) 108 if lfd >= 0 { 109 qc_w(lfd, "COHESION-DUP-TITLE id=" as *u8) 110 qc_w(lfd, an_id_at(cx, r)) 111 qc_w(lfd, "\n" as *u8) 112 } 113 } 114 } 115 r = r + 1 116 } 117 return dups 118} 119 120// open debt-ledger ids (T#... / B#... on lines containing "open") with no 121// mention anywhere in the queue bytes 122func qc_ledger_gaps(ledger: *u8, ln: i64, qbuf: *u8, qn: i64, lfd: i64) -> i64 { 123 var gaps: i64 = 0 124 let tok: *u8 = sys_mmap(96) 125 var ls: i64 = 0 126 var i: i64 = 0 127 while i <= ln { 128 var eol: i64 = 0 129 if i == ln { eol = 1 } else { if ledger[i] == (10 as u8) { eol = 1 } } 130 if eol == 1 { 131 // line [ls, i): must contain "open" (case-folded o/O) 132 var has_open: i64 = 0 133 var j: i64 = ls 134 while j + 4 <= i { 135 var c0: i64 = ledger[j] as i64 136 if c0 == 79 { c0 = 111 } 137 if c0 == 111 { 138 var c1: i64 = ledger[j + 1] as i64 139 var c2: i64 = ledger[j + 2] as i64 140 var c3: i64 = ledger[j + 3] as i64 141 if c1 == 112 { if c2 == 101 { if c3 == 110 { has_open = 1 } } } 142 } 143 j = j + 1 144 } 145 if has_open == 1 { 146 // extract first T#/B# token on the line 147 j = ls 148 while j + 2 <= i { 149 var lead: i64 = 0 150 if ledger[j] == (84 as u8) { lead = 1 } 151 if ledger[j] == (66 as u8) { lead = 1 } 152 if lead == 1 { 153 if ledger[j + 1] == (35 as u8) { 154 var k: i64 = 0 155 tok[k] = ledger[j] 156 tok[k + 1] = 35 as u8 157 k = 2 158 var p: i64 = j + 2 159 var go: i64 = 1 160 while go == 1 { 161 if p >= i { go = 0 } else { 162 let ch: i64 = ledger[p] as i64 163 var okc: i64 = 0 164 if ch >= 97 { if ch <= 122 { okc = 1 } } 165 if ch >= 48 { if ch <= 57 { okc = 1 } } 166 if ch == 45 { okc = 1 } 167 if okc == 1 { 168 if k < 94 { tok[k] = ch as u8; k = k + 1 } 169 p = p + 1 170 } else { go = 0 } 171 } 172 } 173 tok[k] = 0 as u8 174 if k > 3 { 175 if qc_has(qbuf, qn, tok) == 0 { 176 gaps = gaps + 1 177 qc_w(1, "COHESION-LEDGER-GAP id=" as *u8) 178 qc_w(1, tok) 179 qc_w(1, "\n" as *u8) 180 if lfd >= 0 { 181 qc_w(lfd, "COHESION-LEDGER-GAP id=" as *u8) 182 qc_w(lfd, tok) 183 qc_w(lfd, "\n" as *u8) 184 } 185 } 186 } 187 j = i 188 } 189 } 190 j = j + 1 191 } 192 } 193 ls = i + 1 194 } 195 i = i + 1 196 } 197 return gaps 198} 199 200func main(argc: i64, argv: *i64) -> i64 { 201 var qp: *u8 = "knowledge/registry/assignment_queue.tsv" as *u8 202 var ledp: *u8 = "NISHI_DEBT_LEDGER.tsv" as *u8 203 if argc >= 2 { qp = argv[1] as *u8 } 204 if argc >= 3 { ledp = argv[2] as *u8 } 205 206 let cx: *i64 = an_newcx() 207 let rows: i64 = an_load(qp, cx) 208 if rows <= 0 { 209 qc_w(1, "COHESION verdict=RED reason=queue-missing\n" as *u8) 210 sys_exit(101) 211 } 212 let lfd: i64 = sys_openat_append("knowledge/status/queue_cohesion.log" as *u8, 0x1a4) 213 214 let qbuf: *u8 = sys_mmap(K_MAGIC_1048576) 215 let qn: i64 = qc_read(qp, qbuf, K_MAGIC_1048576) 216 let lbuf: *u8 = sys_mmap(K_MAGIC_1048576) 217 let ln: i64 = qc_read(ledp, lbuf, K_MAGIC_1048576) 218 219 let di: i64 = qc_dup_ids(cx, lfd) 220 let dt: i64 = qc_dup_titles(cx, lfd) 221 var gaps: i64 = 0 222 if ln > 0 { gaps = qc_ledger_gaps(lbuf, ln, qbuf, qn, lfd) } 223 224 var verdict: *u8 = "GREEN" as *u8 225 var rc: i64 = 0 226 if di > 0 { verdict = "RED" as *u8; rc = 1 } 227 if dt > 0 { verdict = "RED" as *u8; rc = 1 } 228 var p: i64 = 0 229 while p < 2 { 230 var fd: i64 = 1 231 if p == 1 { fd = lfd } 232 if fd >= 0 { 233 qc_w(fd, "COHESION rows=" as *u8); qc_wn(fd, rows) 234 qc_w(fd, " dup_id=" as *u8); qc_wn(fd, di) 235 qc_w(fd, " dup_title=" as *u8); qc_wn(fd, dt) 236 qc_w(fd, " ledger_gaps=" as *u8); qc_wn(fd, gaps) 237 qc_w(fd, " epoch=" as *u8); qc_wn(fd, sys_now_realtime_sec()) 238 qc_w(fd, " verdict=" as *u8); qc_w(fd, verdict) 239 qc_w(fd, "\n" as *u8) 240 } 241 p = p + 1 242 } 243 if lfd >= 0 { sys_close(lfd) } 244 sys_exit(rc) 245 return rc 246}