code wiki / _hdl_build / nx_debtmine.nx

nx_debtmine.nx source

↩ module page · 526 lines · 27929 B

1// nx_debtmine.nx -- BUGS-CLASS MINER over the debt- plane (miner-sota F1009/MS06a, 2026-07-23). 2// The census (nx_minecov MC06) measured 661 debt rows COVERED but UNMINED: listed, never analysed. 3// This mines the management signals a list cannot give: WHERE debt concentrates (per-scope open 4// counts), WHICH lanes actually eat it (eat_permil = eaten/(open+eaten) per scope -- low eat-rate 5// with high open = rot), severity distribution, and AGE (ids ARE epochs, so open-debt age buckets 6// and oldest_open are derivable). Source is a seg-store plane loaded through the ALLOWLIST-RESOLVED 7// loader, or a FILE when loader is "-" (deterministic gate fixtures; live uses nx_store_put). 8// now is INJECTABLE so aging is bit-deterministic under test. Composes nx_sovjson_lib (sj_*). 9// Read-only. No hw writes (Rule 26). 10// mine <source> [loader] [minsup] [now] -> JSON: totals, by_scope ranked, sev dist, aging 11// selftest <scratch-prefix> -> gate T1..T8 12// plane row cols: id(epoch) TAB sev TAB scope TAB status(open|eaten) TAB desc 13// license_tier: ORIGINAL 14import "nx_sovjson_lib.nx" 15import "nx_syscalls.nx" 16import "nx_gate_verdict.nx" 17import "nx_tool_run.nx" 18import "nx_debt_recurrence_lib.nx" 19const DM_MAGIC_1785000000: i64 = 1785000000 20const DM_MAGIC_4088: i64 = 4088 21 22// 2026-08-08 RAISED 4194304 -> 33554432, completing the lockstep this organ was LEFT OUT OF on 23// 2026-08-06 (nx_debt DB_CAP, nx_debtlive DL_CAP, nx_debtcluster, nx_debt_view, nx_dora DCAP all moved 24// then; this one did not). MEASURED TODAY, with the loader's own 1 MiB cap lifted first: the plane is 25// 4,974,565B / 3,785 rows, and at 4194304 this buffer became the NEXT binding cap -- bytes came back as 26// exactly DM_CAP-8 = 4194296 and tr_run_capture_tr correctly flagged truncated=1. 27// ***WHAT THE TRUNCATED VIEW WAS ACTUALLY DOING TO THE NUMBERS*** (1 MiB vs 4 MiB read of the same plane): 28// rows 1273 -> 3287, open 857 -> 2086, sev8_open 32 -> 324, sev7 124 -> 399, sev6 167 -> 424, 29// malformed 0 -> 24, aging d1_7 0 -> 260, and sev9 went from ABSENT ENTIRELY to 9. So the error was not 30// merely "partial": it understated the most severe classes by an order of magnitude and reported ZERO 31// debt filed in the last week. That is the append-only-head signature nx_dora names -- A PREFIX CAP ON 32// AN APPEND-ONLY PLANE LOSES THE NEWEST ROWS FIRST, so the instrument degrades exactly as new work 33// arrives, and it degrades toward looking healthy. 34// Re-verify the sizing with the estate's own ruler: nx_planefit knowledge/store/debt- 33554432 35const DM_CAP: i64 = 33554432 36const DM_OUT: i64 = 65536 37const DM_PATH: i64 = 512 38// (no DM_SCOPES: the scope table is sized from the rows by drc_capacity, CE3 2026-09-06 -- the 256-slot bound bound live) 39const DM_TOPN: i64 = 20 40const DM_ALLOW: i64 = 131072 41const DM_DAY: i64 = 86400 42const DM_PERMIL: i64 = 1000 43const DM_SEVN: i64 = 12 44const DM_NAME_MAX: i64 = 120 45const DM_MODE_644: i64 = 420 46 47func dm_read(path: *u8, buf: *u8, cap: i64) -> i64 { 48 let fd: i64 = sys_openat_rd(path) 49 if fd < 0 { return -1 } 50 var n: i64 = 0 51 var go: i64 = 1 52 while go == 1 { let r: i64 = sys_read(fd, ((buf as i64)+n) as *u8, cap-n); if r <= 0 { go = 0 } else { n = n + r } if n >= cap { go = 0 } } 53 sys_close(fd) 54 return n 55} 56func dm_find(q: *u8, n: i64, lit: *u8) -> i64 { 57 let ll: i64 = sj_vlen(lit) 58 if ll == 0 { return 0 } 59 var i: i64 = 0 60 while i + ll <= n { 61 var k: i64 = 0 62 var ok: i64 = 1 63 while k < ll { if q[i+k] != lit[k] { ok = 0; k = ll } else { k = k + 1 } } 64 if ok == 1 { return 1 } 65 i = i + 1 66 } 67 return 0 68} 69// DELEGATES to the shared base (rule-15 extraction 2026-07-23) 70func dm_allow_path(name: *u8, outp: *u8) -> i64 { return sj_allow_path(name, outp) } 71func dm_key(d: *u8, o: i64, name: *u8) -> i64 { 72 d[o] = 34 as u8 73 var p: i64 = o + 1 74 p = sj_cat(d, p, name) 75 d[p] = 34 as u8 76 p = p + 1 77 d[p] = 58 as u8 78 p = p + 1 79 return p 80} 81func dm_digits(q: *u8, s: i64, e: i64) -> i64 { 82 if e <= s { return 0 } 83 var i: i64 = s 84 while i < e { let c: i64 = q[i] as i64; if c < 48 { return 0 } if c > 57 { return 0 } i = i + 1 } 85 return 1 86} 87// load the source: loader "-" => read the file directly; else fork <loader> <source> load. 88// scx[0]=bytes scx[1]=truncated scx[2]=loader_resolved 89func dm_load(src: *u8, loader: *u8, buf: *u8, scx: *i64) -> i64 { 90 scx[0] = 0 91 scx[1] = 0 92 scx[2] = 0 93 if sj_lit_eq(loader, 0, sj_vlen(loader), "-" as *u8) == 1 { 94 let n: i64 = dm_read(src, buf, DM_CAP - 8) 95 if n < 0 { return -1 } 96 scx[0] = n 97 if n >= DM_CAP - 8 { scx[1] = 1 } 98 return 0 99 } 100 let lp: *u8 = sys_mmap(DM_PATH) 101 if dm_allow_path(loader, lp) == 0 { return -2 } 102 scx[2] = 1 103 let av: *i64 = sys_mmap(64) as *i64 104 av[0] = lp as i64 105 av[1] = src as i64 106 av[2] = "load" as *u8 as i64 107 av[3] = 0 108 let cl: *i64 = sys_mmap(16) as *i64 109 // ADOPTS tr_run_capture_tr (2026-08-08, debt 1786235483 -- FIRST REAL CALLER, not a demo). The 110 // fork-capture cap here is DM_CAP-8 = 4194296 and this plane ALREADY measures 4921787 bytes, so the 111 // moment the loader's own 2^20 cap is lifted THIS layer becomes the binding one -- and the legacy 112 // tr_run_capture cannot say it truncated, so the organ would silently re-acquire the exact lie that 113 // was just fixed one layer up. The trap is LATENT, not hypothetical: the input is already bigger 114 // than the buffer. timeout 0 = keep the legacy unbounded drain, so this is a truncation-reporting 115 // change ONLY and not also a deadline change. 116 let trp: *i64 = sys_mmap(16) as *i64 117 let ex: i64 = tr_run_capture_tr(lp, av, buf, DM_CAP - 8, cl, 0, trp) 118 if ex != 0 { return -3 } 119 scx[0] = cl[0] 120 if cl[0] >= DM_CAP - 8 { scx[1] = 1 } 121 if trp[0] != 0 { scx[1] = 1 } 122 return 0 123} 124func dm_mine_json(src: *u8, loader: *u8, minsup: i64, now: i64, d: *u8) -> i64 { 125 let q: *u8 = sys_mmap(DM_CAP) 126 let scx: *i64 = sys_mmap(64) as *i64 127 let lr: i64 = dm_load(src, loader, q, scx) 128 if lr < 0 { return lr } 129 let n: i64 = scx[0] 130 if n <= 0 { return -1 } 131 // ***COVERAGE IS NOT BUFFER SPACE.*** scx[1] only knows whether OUR buffer overflowed. The forked 132 // loader has its OWN cap and cuts upstream, where dm_load structurally cannot see it. MEASURED 133 // 2026-08-08: nx_store_put returned exactly 1048576 bytes of a 4921787-byte plane and scx[1] was 0, 134 // because 1048576 < DM_CAP-8 -- so a field NAMED truncated reported COMPLETE coverage on a 21.3% 135 // read, and every by_scope / aging / eat_permil figure described the OLDEST FIFTH of an append-only 136 // plane, whose head is its past (debt 1786235467). A row-oriented stream cut at a BYTE cap ends 137 // MID-ROW, so a final byte that is not a newline is positive evidence of a cut at WHICHEVER layer 138 // imposed it -- and that is the only question the caller actually has. 139 if q[n-1] != (10 as u8) { scx[1] = 1 } 140 // THE SCOPE TABLE IS SIZED FROM THE ROWS (CE3, 2026-09-06): the 256-slot bound BOUND on the live plane and the 141 // by_scope census silently became a floor; the derivation is the recurrence lib's, so both rulers share ONE 142 let mcap: i64 = drc_capacity(q, n) 143 let ss: *i64 = sys_mmap(8 * mcap) as *i64 144 let se: *i64 = sys_mmap(8 * mcap) as *i64 145 let sopen: *i64 = sys_mmap(8 * mcap) as *i64 146 let seat: *i64 = sys_mmap(8 * mcap) as *i64 147 let sevo: *i64 = sys_mmap(8 * DM_SEVN) as *i64 148 let done: *i64 = sys_mmap(8 * mcap) as *i64 149 let sp: *i64 = sys_mmap(16) as *i64 150 var nsc: i64 = 0 151 var scapped: i64 = 0 152 var rows: i64 = 0 153 var open: i64 = 0 154 var eaten: i64 = 0 155 var malformed: i64 = 0 156 var b0: i64 = 0 157 var b1: i64 = 0 158 var b2: i64 = 0 159 var b3: i64 = 0 160 var oldest: i64 = 0 161 var i: i64 = 0 162 while i < n { 163 let le: i64 = sj_le(q, i, n) 164 var ok: i64 = 0 165 var cmt: i64 = 0 166 if le > i { if q[i] == (35 as u8) { cmt = 1 } } 167 if le > i { if cmt == 0 { if sj_col(q, i, le, 3, sp) == 1 { ok = 1 } } } 168 if ok == 0 { if le > i { if cmt == 0 { malformed = malformed + 1 } } } else { 169 rows = rows + 1 170 var isopen: i64 = 0 171 if sj_lit_eq(q, sp[0], sp[1], "open" as *u8) == 1 { isopen = 1 } 172 if isopen == 1 { open = open + 1 } else { eaten = eaten + 1 } 173 // scope intern (col 2) 174 sj_col(q, i, le, 2, sp) 175 let cs: i64 = sp[0] 176 let ce: i64 = sp[1] 177 var idx: i64 = 0 - 1 178 var k: i64 = 0 179 while k < nsc { 180 if ce - cs == se[k] - ss[k] { 181 var m: i64 = 0 182 var eq: i64 = 1 183 while m < ce - cs { if q[cs+m] != q[ss[k]+m] { eq = 0; m = ce - cs } else { m = m + 1 } } 184 if eq == 1 { idx = k; k = nsc } 185 } 186 k = k + 1 187 } 188 if idx < 0 { 189 if nsc >= mcap { scapped = 1 } else { 190 ss[nsc] = cs 191 se[nsc] = ce 192 idx = nsc 193 nsc = nsc + 1 194 } 195 } 196 if idx >= 0 { 197 if isopen == 1 { sopen[idx] = sopen[idx] + 1 } else { seat[idx] = seat[idx] + 1 } 198 } 199 // severity (col 1), open only 200 if isopen == 1 { 201 sj_col(q, i, le, 1, sp) 202 let sv: i64 = sj_atoi_span(q, sp[0], sp[1]) 203 if sv >= 0 { if sv < DM_SEVN { sevo[sv] = sevo[sv] + 1 } } 204 // aging from the id-epoch (col 0), open only 205 sj_col(q, i, le, 0, sp) 206 if dm_digits(q, sp[0], sp[1]) == 1 { 207 let ep: i64 = sj_atoi_span(q, sp[0], sp[1]) 208 if ep > 0 { 209 if oldest == 0 { oldest = ep } else { if ep < oldest { oldest = ep } } 210 let age: i64 = now - ep 211 if age < DM_DAY { b0 = b0 + 1 } else { 212 if age < 7 * DM_DAY { b1 = b1 + 1 } else { 213 if age < 30 * DM_DAY { b2 = b2 + 1 } else { b3 = b3 + 1 } 214 } 215 } 216 } 217 } 218 } 219 } 220 i = le + 1 221 } 222 var p: i64 = 0 223 d[p] = 123 as u8; p = p + 1 224 p = dm_key(d, p, "v" as *u8); p = sj_catn(d, p, 1); d[p] = 44 as u8; p = p + 1 225 p = dm_key(d, p, "tool" as *u8) 226 d[p] = 34 as u8; p = p + 1 227 p = sj_cat(d, p, "nx_debtmine" as *u8) 228 d[p] = 34 as u8; p = p + 1 229 d[p] = 44 as u8; p = p + 1 230 p = dm_key(d, p, "averb" as *u8) 231 d[p] = 34 as u8; p = p + 1 232 p = sj_cat(d, p, "mine" as *u8) 233 d[p] = 34 as u8; p = p + 1 234 d[p] = 44 as u8; p = p + 1 235 p = dm_key(d, p, "source" as *u8) 236 d[p] = 34 as u8; p = p + 1 237 p = sj_cat_esc(d, p, src, 0, sj_vlen(src), DM_NAME_MAX) 238 d[p] = 34 as u8; p = p + 1 239 d[p] = 44 as u8; p = p + 1 240 p = dm_key(d, p, "loader_resolved" as *u8); p = sj_catn(d, p, scx[2]); d[p] = 44 as u8; p = p + 1 241 p = dm_key(d, p, "bytes" as *u8); p = sj_catn(d, p, n); d[p] = 44 as u8; p = p + 1 242 p = dm_key(d, p, "truncated" as *u8); p = sj_catn(d, p, scx[1]); d[p] = 44 as u8; p = p + 1 243 // THE DENOMINATOR-BEARING FIELD: truncated says what happened, coverage_complete says whether the 244 // numbers below may be read as POPULATION FACTS. Emitted beside them so no reader can take a 245 // distribution without also taking its coverage. 246 var covc: i64 = 1 247 if scx[1] != 0 { covc = 0 } 248 p = dm_key(d, p, "coverage_complete" as *u8); p = sj_catn(d, p, covc); d[p] = 44 as u8; p = p + 1 249 p = dm_key(d, p, "minsup" as *u8); p = sj_catn(d, p, minsup); d[p] = 44 as u8; p = p + 1 250 p = dm_key(d, p, "rows" as *u8); p = sj_catn(d, p, rows); d[p] = 44 as u8; p = p + 1 251 p = dm_key(d, p, "open" as *u8); p = sj_catn(d, p, open); d[p] = 44 as u8; p = p + 1 252 p = dm_key(d, p, "eaten" as *u8); p = sj_catn(d, p, eaten); d[p] = 44 as u8; p = p + 1 253 p = dm_key(d, p, "malformed" as *u8); p = sj_catn(d, p, malformed); d[p] = 44 as u8; p = p + 1 254 p = dm_key(d, p, "scopes" as *u8); p = sj_catn(d, p, nsc); d[p] = 44 as u8; p = p + 1 255 var eatp: i64 = 0 - 1 256 if rows > 0 { eatp = eaten * DM_PERMIL / rows } 257 p = dm_key(d, p, "eat_permil" as *u8); p = sj_catn(d, p, eatp); d[p] = 44 as u8; p = p + 1 258 // by_scope ranked by OPEN desc (bounded selection, capped + declared) 259 p = dm_key(d, p, "by_scope" as *u8) 260 d[p] = 91 as u8; p = p + 1 261 var shown: i64 = 0 262 var efirst: i64 = 1 263 var below: i64 = 0 264 var pass: i64 = 0 265 while pass < nsc { 266 var mi: i64 = 0 - 1 267 var mx: i64 = 0 - 1 268 var k2: i64 = 0 269 while k2 < nsc { 270 if done[k2] == 0 { if sopen[k2] > mx { mx = sopen[k2]; mi = k2 } } 271 k2 = k2 + 1 272 } 273 if mi >= 0 { 274 done[mi] = 1 275 if mx < minsup { below = below + 1 } else { 276 if shown < DM_TOPN { 277 if efirst == 0 { d[p] = 44 as u8; p = p + 1 } 278 efirst = 0 279 d[p] = 123 as u8; p = p + 1 280 p = dm_key(d, p, "scope" as *u8) 281 d[p] = 34 as u8; p = p + 1 282 p = sj_cat_esc(d, p, q, ss[mi], se[mi], DM_NAME_MAX) 283 d[p] = 34 as u8; p = p + 1 284 d[p] = 44 as u8; p = p + 1 285 p = dm_key(d, p, "open" as *u8); p = sj_catn(d, p, sopen[mi]); d[p] = 44 as u8; p = p + 1 286 p = dm_key(d, p, "eaten" as *u8); p = sj_catn(d, p, seat[mi]); d[p] = 44 as u8; p = p + 1 287 var sep: i64 = 0 - 1 288 let tot: i64 = sopen[mi] + seat[mi] 289 if tot > 0 { sep = seat[mi] * DM_PERMIL / tot } 290 p = dm_key(d, p, "eat_permil" as *u8); p = sj_catn(d, p, sep) 291 d[p] = 125 as u8; p = p + 1 292 shown = shown + 1 293 } 294 } 295 } 296 pass = pass + 1 297 } 298 d[p] = 93 as u8; p = p + 1 299 d[p] = 44 as u8; p = p + 1 300 p = dm_key(d, p, "scopes_shown" as *u8); p = sj_catn(d, p, shown); d[p] = 44 as u8; p = p + 1 301 p = dm_key(d, p, "scopes_below_minsup" as *u8); p = sj_catn(d, p, below); d[p] = 44 as u8; p = p + 1 302 p = dm_key(d, p, "scopes_capped" as *u8); p = sj_catn(d, p, scapped); d[p] = 44 as u8; p = p + 1 303 // severity distribution of OPEN debt 304 p = dm_key(d, p, "sev_open" as *u8) 305 d[p] = 91 as u8; p = p + 1 306 var sv2: i64 = 0 307 var sfirst: i64 = 1 308 while sv2 < DM_SEVN { 309 if sevo[sv2] > 0 { 310 if sfirst == 0 { d[p] = 44 as u8; p = p + 1 } 311 sfirst = 0 312 d[p] = 123 as u8; p = p + 1 313 p = dm_key(d, p, "sev" as *u8); p = sj_catn(d, p, sv2); d[p] = 44 as u8; p = p + 1 314 p = dm_key(d, p, "open" as *u8); p = sj_catn(d, p, sevo[sv2]) 315 d[p] = 125 as u8; p = p + 1 316 } 317 sv2 = sv2 + 1 318 } 319 d[p] = 93 as u8; p = p + 1 320 d[p] = 44 as u8; p = p + 1 321 // aging of OPEN debt (ids are epochs) 322 p = dm_key(d, p, "aging_open" as *u8) 323 d[p] = 123 as u8; p = p + 1 324 p = dm_key(d, p, "lt_1d" as *u8); p = sj_catn(d, p, b0); d[p] = 44 as u8; p = p + 1 325 p = dm_key(d, p, "d1_7" as *u8); p = sj_catn(d, p, b1); d[p] = 44 as u8; p = p + 1 326 p = dm_key(d, p, "d7_30" as *u8); p = sj_catn(d, p, b2); d[p] = 44 as u8; p = p + 1 327 p = dm_key(d, p, "gt_30d" as *u8); p = sj_catn(d, p, b3); d[p] = 44 as u8; p = p + 1 328 // DECLARED: rows whose id is not an epoch contribute no age -- never silently dropped 329 p = dm_key(d, p, "counted" as *u8); p = sj_catn(d, p, b0 + b1 + b2 + b3); d[p] = 44 as u8; p = p + 1 330 p = dm_key(d, p, "no_epoch_id" as *u8); p = sj_catn(d, p, open - (b0 + b1 + b2 + b3)) 331 d[p] = 125 as u8; p = p + 1 332 d[p] = 44 as u8; p = p + 1 333 // CE3 (codeeffectiveness ce_recurrence): recurrence per scope from the same rows through the ONE ruler in 334 // nx_debt_recurrence_lib; the spine is written by the recurrence verb only, so a beat running both never double-counts. 335 let rcap: i64 = drc_capacity(q, n) 336 let rst: *i64 = sys_mmap(rcap * DRC_S_SLOTS * DRC_WORD) as *i64 337 let rids: *i64 = sys_mmap(rcap * DRC_IDS_PER_SCOPE * DRC_WORD) as *i64 338 let rr: *i64 = sys_mmap(DRC_R_SLOTS * DRC_WORD) as *i64 339 let rns: i64 = drc_scan(q, n, rcap, rst, rids, rr) 340 p = drc_emit_json(d, p, q, rst, rids, rns, rr, DM_TOPN) 341 d[p] = 44 as u8; p = p + 1 342 var oldd: i64 = 0 - 1 343 if oldest > 0 { oldd = (now - oldest) / DM_DAY } 344 p = dm_key(d, p, "oldest_open_days" as *u8); p = sj_catn(d, p, oldd) 345 d[p] = 125 as u8; p = p + 1 346 d[p] = 10 as u8; p = p + 1 347 return p 348} 349// CE3 (codeeffectiveness ce_recurrence): the recurrence document on its own. Same loader, same rows, the ONE ruler in 350// nx_debt_recurrence_lib; this verb only loads, prints and appends the spine row. Returns the document length, or the 351// loader's negative code (source unreadable / loader refused) -- never an empty document that reads like a measurement. 352func ce_recurrence(src: *u8, loader: *u8, now: i64, d: *u8) -> i64 { 353 let q: *u8 = sys_mmap(DM_CAP) 354 let scx: *i64 = sys_mmap(64) as *i64 355 let lr: i64 = dm_load(src, loader, q, scx) 356 if lr < 0 { return lr } 357 let n: i64 = scx[0] 358 if n <= 0 { return -1 } 359 if q[n-1] != (10 as u8) { scx[1] = 1 } 360 let rcap: i64 = drc_capacity(q, n) 361 let rst: *i64 = sys_mmap(rcap * DRC_S_SLOTS * DRC_WORD) as *i64 362 let rids: *i64 = sys_mmap(rcap * DRC_IDS_PER_SCOPE * DRC_WORD) as *i64 363 let rr: *i64 = sys_mmap(DRC_R_SLOTS * DRC_WORD) as *i64 364 let rns: i64 = drc_scan(q, n, rcap, rst, rids, rr) 365 var p: i64 = 0 366 d[p] = 123 as u8; p = p + 1 367 p = dm_key(d, p, "v" as *u8); p = sj_catn(d, p, 1); d[p] = 44 as u8; p = p + 1 368 p = dm_key(d, p, "tool" as *u8); d[p] = 34 as u8; p = p + 1; p = sj_cat(d, p, "nx_debtmine" as *u8); d[p] = 34 as u8; p = p + 1; d[p] = 44 as u8; p = p + 1 369 p = dm_key(d, p, "averb" as *u8); d[p] = 34 as u8; p = p + 1; p = sj_cat(d, p, "recurrence" as *u8); d[p] = 34 as u8; p = p + 1; d[p] = 44 as u8; p = p + 1 370 p = dm_key(d, p, "source" as *u8); d[p] = 34 as u8; p = p + 1; p = sj_cat_esc(d, p, src, 0, sj_vlen(src), DM_NAME_MAX); d[p] = 34 as u8; p = p + 1; d[p] = 44 as u8; p = p + 1 371 p = dm_key(d, p, "loader_resolved" as *u8); p = sj_catn(d, p, scx[2]); d[p] = 44 as u8; p = p + 1 372 p = dm_key(d, p, "bytes" as *u8); p = sj_catn(d, p, n); d[p] = 44 as u8; p = p + 1 373 p = dm_key(d, p, "truncated" as *u8); p = sj_catn(d, p, scx[1]); d[p] = 44 as u8; p = p + 1 374 var covc: i64 = 1 375 if scx[1] != 0 { covc = 0 } 376 p = dm_key(d, p, "coverage_complete" as *u8); p = sj_catn(d, p, covc); d[p] = 44 as u8; p = p + 1 377 p = drc_emit_json(d, p, q, rst, rids, rns, rr, DM_TOPN) 378 d[p] = 44 as u8; p = p + 1 379 p = drc_spine_json(d, p, src, now, rr) 380 d[p] = 125 as u8; p = p + 1 381 d[p] = 10 as u8; p = p + 1 382 return p 383} 384func dm_selftest(prefix: *u8) -> i64 { 385 let ctr: *i64 = gv_ctr() 386 gv_head("nx_debtmine selftest -- bugs-class mining teeth (exact counts, honest eat-rate, deterministic aging)" as *u8) 387 let now: i64 = DM_MAGIC_1785000000 388 let fx: *u8 = sys_mmap(DM_PATH) 389 var o: i64 = sj_cat(fx, 0, prefix) 390 o = sj_catn(fx, o, sys_now_realtime_sec()) 391 o = sj_cat(fx, o, ".dm" as *u8) 392 fx[o] = 0 as u8 393 let miss: *u8 = sys_mmap(DM_PATH) 394 o = sj_cat(miss, 0, fx) 395 o = sj_cat(miss, o, ".absent" as *u8) 396 miss[o] = 0 as u8 397 // fixture: alpha 3 open (sev 8,8,5) + 1 eaten ; beta 1 open ; gamma 0 open 2 eaten ; 1 malformed 398 // ages vs now=1785000000: 1784990000 (~2.8h <1d) 1784800000 (~2.3d) 1784200000 (~9.3d) 1782000000 (~34.7d) 399 let fd: i64 = sys_openat_wr(fx, DM_MODE_644) 400 let lb: *u8 = sys_mmap(DM_MAGIC_4088) 401 var lo: i64 = sj_cat(lb, 0, "# fixture\n" as *u8) 402 lo = sj_cat(lb, lo, "1784990000\t8\talpha\topen\tfresh one\n" as *u8) 403 lo = sj_cat(lb, lo, "1784800000\t8\talpha\topen\ttwo days\n" as *u8) 404 lo = sj_cat(lb, lo, "1784200000\t5\talpha\topen\tnine days\n" as *u8) 405 lo = sj_cat(lb, lo, "1784100000\t5\talpha\teaten\tdone one\n" as *u8) 406 lo = sj_cat(lb, lo, "1782000000\t9\tbeta\topen\tthirty five days\n" as *u8) 407 lo = sj_cat(lb, lo, "1784000000\t3\tgamma\teaten\tdone two\n" as *u8) 408 lo = sj_cat(lb, lo, "1784000001\t3\tgamma\teaten\tdone three\n" as *u8) 409 lo = sj_cat(lb, lo, "nocolumns\n" as *u8) 410 sys_write(fd, lb, lo) 411 sys_close(fd) 412 let d: *u8 = sys_mmap(DM_OUT) 413 // T1 missing source fails closed 414 let r1: i64 = dm_mine_json(miss, "-" as *u8, 1, now, d) 415 var ok1: i64 = 0 416 if r1 < 0 { ok1 = 1 } 417 gv_check("T1 missing source refused fail closed" as *u8, ok1, ctr) 418 let dl: i64 = dm_mine_json(fx, "-" as *u8, 1, now, d) 419 var ok2: i64 = 0 420 if dl > 0 { if dm_find(d, dl, "\"rows\":7,\"open\":4,\"eaten\":3" as *u8) == 1 { ok2 = 1 } } 421 gv_check("T2 exact totals rows 7 open 4 eaten 3 (comment skipped)" as *u8, ok2, ctr) 422 var ok3: i64 = 0 423 if dm_find(d, dl, "\"malformed\":1" as *u8) == 1 { if dm_find(d, dl, "\"scopes\":3" as *u8) == 1 { ok3 = 1 } } 424 gv_check("T3 malformed row COUNTED not silently dropped; 3 distinct scopes" as *u8, ok3, ctr) 425 var ok4: i64 = 0 426 if dm_find(d, dl, "\"scope\":\"alpha\",\"open\":3,\"eaten\":1,\"eat_permil\":250" as *u8) == 1 { ok4 = 1 } 427 gv_check("T4 top scope by open with EXACT per-scope eat_permil 250" as *u8, ok4, ctr) 428 // minsup 0 = show every scope; separate buffer so the T6/T7 assertions keep reading the minsup-1 run 429 let d5: *u8 = sys_mmap(DM_OUT) 430 let dl5: i64 = dm_mine_json(fx, "-" as *u8, 0, now, d5) 431 var ok5: i64 = 0 432 // the absence needle is the MINE census shape (scope followed by open), because the CE3 recurrence object in the same 433 // document legitimately NAMES a suppressed scope that has a resolved prior -- a whole-document absence check read 434 // that as a suppression failure (caught 2026-09-06 on the promoted binary: 9/10) 435 if dl5 > 0 { if dm_find(d5, dl5, "\"scope\":\"gamma\",\"open\":0,\"eaten\":2,\"eat_permil\":1000" as *u8) == 1 { if dm_find(d, dl, "\"scope\":\"gamma\",\"open\"" as *u8) == 0 { ok5 = 1 } } } 436 gv_check("T5 minsup 0 surfaces the fully-eaten scope at EXACT 1000 permil; minsup 1 suppresses it from the mine census (the recurrence object may still name it)" as *u8, ok5, ctr) 437 var ok6: i64 = 0 438 if dm_find(d, dl, "\"sev\":8,\"open\":2" as *u8) == 1 { if dm_find(d, dl, "\"sev\":3" as *u8) == 0 { ok6 = 1 } } 439 gv_check("T6 severity distribution counts OPEN only (sev8 = 2; eaten sev3 absent)" as *u8, ok6, ctr) 440 var ok7: i64 = 0 441 if dm_find(d, dl, "\"lt_1d\":1,\"d1_7\":1,\"d7_30\":1,\"gt_30d\":1" as *u8) == 1 { if dm_find(d, dl, "\"oldest_open_days\":34" as *u8) == 1 { ok7 = 1 } } 442 gv_check("T7 aging buckets exact 1/1/1/1 + oldest_open_days 34 (injected now)" as *u8, ok7, ctr) 443 let dl8: i64 = dm_mine_json(fx, "-" as *u8, 3, now, d) 444 var ok8: i64 = 0 445 if dl8 > 0 { if dm_find(d, dl8, "\"scope\":\"alpha\"" as *u8) == 1 { if dm_find(d, dl8, "\"scope\":\"beta\"" as *u8) == 0 { if dm_find(d, dl8, "\"scopes_below_minsup\":2" as *u8) == 1 { ok8 = 1 } } } } 446 gv_check("T8 minsup 3 emits only alpha and DECLARES the 2 suppressed scopes" as *u8, ok8, ctr) 447 // T9 THE COVERAGE TOOTH (2026-08-08): a load ending MID-ROW must report truncated=1 and 448 // coverage_complete=0. ***THE EXISTING EIGHT TEETH COULD NOT FAIL ON THIS*** -- their fixture is 449 // ~400 bytes against a 4194304 cap, so the truncation branch was UNREACHABLE, and the organ duly 450 // shipped truncated=0 on a 21.3% read of the real plane (debt 1786235467). A fixture the defect 451 // cannot fail is not a test, and a cliff above the file size is the exact vacuity shape. 452 let cut: *u8 = sys_mmap(DM_PATH) 453 var co: i64 = sj_cat(cut, 0, fx) 454 co = sj_cat(cut, co, ".cut" as *u8) 455 cut[co] = 0 as u8 456 let cfd: i64 = sys_openat_wr(cut, DM_MODE_644) 457 let cb: *u8 = sys_mmap(DM_MAGIC_4088) 458 var cl2: i64 = sj_cat(cb, 0, "1784990000\t8\talpha\topen\tfresh one\n" as *u8) 459 cl2 = sj_cat(cb, cl2, "1784800000\t8\talpha\topen\tthis row is CUT mid-" as *u8) 460 sys_write(cfd, cb, cl2) 461 sys_close(cfd) 462 let d9: *u8 = sys_mmap(DM_OUT) 463 let dl9: i64 = dm_mine_json(cut, "-" as *u8, 1, now, d9) 464 var ok9: i64 = 0 465 if dl9 > 0 { if dm_find(d9, dl9, "\"truncated\":1" as *u8) == 1 { if dm_find(d9, dl9, "\"coverage_complete\":0" as *u8) == 1 { ok9 = 1 } } } 466 gv_check("T9 a load ending MID-ROW reports truncated 1 and coverage_complete 0" as *u8, ok9, ctr) 467 468 // neg-control-coverage: the ORIGINAL newline-terminated fixture must STILL report COMPLETE. 469 // Without this, a check that marked everything truncated would pass T9 and look correct -- 470 // a guard that refuses everything passes every deny test. 471 var ok10: i64 = 0 472 if dm_find(d, dl8, "\"coverage_complete\":1" as *u8) == 1 { ok10 = 1 } 473 gv_check("neg-control-coverage: newline-terminated fixture still reports coverage_complete 1" as *u8, ok10, ctr) 474 475 let rc: i64 = gv_verdict("DEBTMINE-GATE" as *u8, ctr, "bugs-class mining: exact totals, per-scope eat-rate, open-only severity, deterministic aging, declared suppression" as *u8) 476 return rc 477} 478 479func main(argc: i64, argv: *i64) -> i64 { 480 if argc < 3 { sj_puts("usage: nx_debtmine {mine <source> [loader|-] [minsup] [now] | selftest <scratch-prefix>}\n" as *u8); sys_exit(2); return 2 } 481 let verb: *u8 = argv[1] as *u8 482 let vl: i64 = sj_vlen(verb) 483 if sj_lit_eq(verb, 0, vl, "selftest" as *u8) == 1 { 484 let rc: i64 = dm_selftest(argv[2] as *u8) 485 sys_exit(rc) 486 return rc 487 } 488 // CE3: nx_debtmine recurrence <source> [loader|-] [now] -- prints the recurrence document and appends the spine row 489 if sj_lit_eq(verb, 0, vl, "recurrence" as *u8) == 1 { 490 var ldr: *u8 = "-" as *u8 491 if argc > 3 { ldr = argv[3] as *u8 } 492 var nowr: i64 = sys_now_realtime_sec() 493 if argc > 4 { let a4: *u8 = argv[4] as *u8; nowr = sj_atoi_span(a4, 0, sj_vlen(a4)) } 494 let dr: *u8 = sys_mmap(DM_OUT) 495 let dl: i64 = ce_recurrence(argv[2] as *u8, ldr, nowr, dr) 496 if dl < 0 { sj_puts("nx_debtmine recurrence: source unreadable or loader refused (fail closed, nothing published)\n" as *u8); sys_exit(3); return 3 } 497 sys_write(1, dr, dl) 498 sys_exit(0) 499 return 0 500 } 501 if sj_lit_eq(verb, 0, vl, "mine" as *u8) == 1 { 502 let loader: *u8 = sys_mmap(128) 503 var lo: i64 = 0 504 if argc >= 4 { lo = sj_cat(loader, 0, argv[3] as *u8) } else { lo = sj_cat(loader, 0, "nx_store_put" as *u8) } 505 loader[lo] = 0 as u8 506 var minsup: i64 = 1 507 if argc >= 5 { let mz: i64 = sj_atoi_z(argv[4] as *u8); if mz > 0 { minsup = mz } } 508 var now: i64 = sys_now_realtime_sec() 509 if argc >= 6 { let nz: i64 = sj_atoi_z(argv[5] as *u8); if nz > 0 { now = nz } } 510 let d: *u8 = sys_mmap(DM_OUT) 511 let dl: i64 = dm_mine_json(argv[2] as *u8, loader, minsup, now, d) 512 if dl == (0 - 2) { sj_puts("REFUSED loader not in the allowlist\n" as *u8); sys_exit(3); return 3 } 513 if dl == (0 - 3) { sj_puts("REFUSED loader exited nonzero\n" as *u8); sys_exit(3); return 3 } 514 if dl < 0 { sj_puts("REFUSED source missing or empty\n" as *u8); sys_exit(3); return 3 } 515 sys_write(1, d, dl) 516 // ***THE EXIT CODE MUST CARRY THE COVERAGE VERDICT.*** A caller branching on exit status would 517 // otherwise read a 21%-coverage load as a clean success -- which is precisely how the original 518 // defect stayed invisible. 4 = numbers WERE emitted but they are PARTIAL; read the JSON. 519 if dm_find(d, dl, "\"coverage_complete\":0" as *u8) == 1 { sys_exit(4); return 4 } 520 sys_exit(0) 521 return 0 522 } 523 sj_puts("unknown verb\n" as *u8) 524 sys_exit(2) 525 return 2 526}