code wiki / (root) / nx_debt_recurrence_lib.nx

nx_debt_recurrence_lib.nx source

↩ module page · 261 lines · 15123 B

1// nx_debt_recurrence_lib.nx -- CE3 (codeeffectiveness ce_recurrence): RECURRENCE PER DEFECT CLASS over the debt plane. 2// 3// Resolution effectiveness is not a close count: the estate's own record has one defect filed five times in three 4// weeks. This is the ONE ruler for that class, composed by nx_debtmine (the incumbent bugs-class miner, which already 5// held per-scope eat-rate and aging) and driven in-process by its gate. Over the plane rows 6// (id TAB sev TAB scope TAB status TAB desc) it interns scopes and, per scope, records the FIRST EATEN filing as the 7// prior and counts every row filed AFTER it as a recurrence of the class: recur_total (history, non-decreasing by 8// construction) and recur_open (the live re-filings, which FALL when they are eaten). The ids are named -- the prior 9// id and a bounded prefix of the recurring ids beside their total -- because a bare number is not the measure the 10// rung asked for. CLOSE TIME IS UNRECORDED ON THIS PLANE (ids are file-time), so a row filed after a prior that was 11// eaten later still counts: the count is an UPPER BOUND and says so in the document. A run with no rows writes no 12// spine row; the spine path is derived from the source (the production spine iff the source is the live plane, 13// else a <source>.recurrence.spine sidecar), so a fixture can never forge the production trajectory. 14// The scope table has NO PICKED BOUND: its capacity is drc_capacity(q, n), derived from the rows, so scopes_capped is 15// 0 by construction and stays in the document as an invariant the gate asserts (the first live beat, 2026-09-06, hit a 16// 256-slot bound on a 4,353-row plane and published a floor wearing a total). Ids are epoch-seconds, so an autofile 17// burst shares one id across several rows: those are distinct filings and count as such, and the adjacent-burst count 18// is published beside the ids (adjacent only, a declared floor) so a reader knows why an id can repeat. 19// The row walk mirrors nx_debtmine's inline loop (sj_le / sj_col / comment skip); extraction of that walk into a 20// shared reader is the declared follow-up, not a second parser by intent. 21// license_tier: ORIGINAL No hw writes. 22import "nx_sovjson_lib.nx" 23import "nx_syscalls.nx" 24 25// (no scope-table constant: the capacity is drc_capacity(q, n), derived from the rows -- see drc_scan) 26const DRC_IDS_PER_SCOPE: i64 = 8 // the PRINTED prefix of recurring ids; the count beside it is the total 27const DRC_WORD: i64 = 8 28const DRC_LINE: i64 = 512 29const DRC_PATH: i64 = 1024 30const DRC_MODE644: i64 = 420 31const DRC_NL: i64 = 10 32const DRC_COMMENT: i64 = 35 33const DRC_QUOTE: i64 = 34 34const DRC_COLON: i64 = 58 35const DRC_COMMA: i64 = 44 36const DRC_LBRACE: i64 = 123 37const DRC_RBRACE: i64 = 125 38const DRC_LBRACK: i64 = 91 39const DRC_RBRACK: i64 = 93 40const DRC_NAME_MAX: i64 = 120 41const DRC_PROD_PREFIX: *u8 = "knowledge/store/debt-" 42const DRC_SPINE_PROD: *u8 = "knowledge/status/debt_recurrence.spine" 43const DRC_SPINE_SIDECAR: *u8 = ".recurrence.spine" 44const DRC_COL_ID: i64 = 0 45const DRC_COL_SCOPE: i64 = 2 46const DRC_COL_STATUS: i64 = 3 47// per-scope slots 48const DRC_S_OFF: i64 = 0 49const DRC_S_LEN: i64 = 1 50const DRC_S_FILED: i64 = 2 51const DRC_S_EATEN: i64 = 3 52const DRC_S_PRIOR: i64 = 4 53const DRC_S_RTOTAL: i64 = 5 54const DRC_S_ROPEN: i64 = 6 55const DRC_S_NIDS: i64 = 7 56const DRC_S_SLOTS: i64 = 8 57// result slots (the ONE place the counts live: the emitter, the spine and the gate read the same array) 58const DRC_R_ROWS: i64 = 0 59const DRC_R_SCOPES: i64 = 1 60const DRC_R_SCOPES_RECUR: i64 = 2 61const DRC_R_ROPEN: i64 = 3 62const DRC_R_RTOTAL: i64 = 4 63const DRC_R_CAPPED: i64 = 5 64const DRC_R_MALFORMED: i64 = 6 65const DRC_R_IDBURST: i64 = 7 // rows whose id equals the previous row's id (an autofile burst shares an epoch-second): adjacent only, a declared floor 66const DRC_R_SLOTS: i64 = 8 67 68func drc_key(d: *u8, o: i64, name: *u8) -> i64 { d[o] = DRC_QUOTE as u8; var p: i64 = sj_cat(d, o + 1, name); d[p] = DRC_QUOTE as u8; d[p + 1] = DRC_COLON as u8; return p + 2 } 69func drc_streq(a: *u8, b: *u8) -> i64 { var i: i64 = 0; while a[i] == b[i] { if a[i] == (0 as u8) { return 1 } i = i + 1 } return 0 } 70func drc_str(d: *u8, o: i64, s: *u8) -> i64 { d[o] = DRC_QUOTE as u8; var p: i64 = sj_cat(d, o + 1, s); d[p] = DRC_QUOTE as u8; return p + 1 } 71 72// TABLE CAPACITY IS DERIVED FROM THE ROWS, NEVER PICKED: distinct scopes cannot exceed rows, so a table of 73// drc_capacity(q, n) scope slots can never bind. scopes_capped therefore reads 0 by construction on every plane of 74// every size and the gate asserts that as an invariant -- the first live beat (2026-09-06) hit a 256-slot bound on a 75// 4,353-row plane and published a FLOOR wearing a total, which is the silent-cap class this removes. 76func drc_capacity(q: *u8, n: i64) -> i64 { 77 var c: i64 = 1 78 var i: i64 = 0 79 while i < n { if q[i] == (DRC_NL as u8) { c = c + 1 } i = i + 1 } 80 return c 81} 82// THE RULER. q/n = the plane bytes; cap = drc_capacity(q, n); st = cap*DRC_S_SLOTS slots; ids = cap*DRC_IDS_PER_SCOPE; r = DRC_R_SLOTS. 83func drc_scan(q: *u8, n: i64, cap: i64, st: *i64, ids: *i64, r: *i64) -> i64 { 84 var z: i64 = 0 85 while z < DRC_R_SLOTS { r[z] = 0; z = z + 1 } 86 var nsc: i64 = 0 87 var lastid: i64 = 0 88 let sp: *i64 = sys_mmap(16) as *i64 89 var i: i64 = 0 90 while i < n { 91 let le: i64 = sj_le(q, i, n) 92 var cmt: i64 = 0 93 if le > i { if q[i] == (DRC_COMMENT as u8) { cmt = 1 } } 94 var ok: i64 = 0 95 if le > i { if cmt == 0 { if sj_col(q, i, le, DRC_COL_STATUS, sp) == 1 { ok = 1 } } } 96 if ok == 0 { if le > i { if cmt == 0 { r[DRC_R_MALFORMED] = r[DRC_R_MALFORMED] + 1 } } } else { 97 r[DRC_R_ROWS] = r[DRC_R_ROWS] + 1 98 var isopen: i64 = 0 99 if sj_lit_eq(q, sp[0], sp[1], "open" as *u8) == 1 { isopen = 1 } 100 sj_col(q, i, le, DRC_COL_ID, sp) 101 let id: i64 = sj_atoi_span(q, sp[0], sp[1]) 102 // an id equal to the previous row's is a burst (several filings in one epoch-second), counted, never merged 103 if id == lastid { r[DRC_R_IDBURST] = r[DRC_R_IDBURST] + 1 } 104 lastid = id 105 sj_col(q, i, le, DRC_COL_SCOPE, sp) 106 let cs: i64 = sp[0] 107 let ce: i64 = sp[1] 108 var idx: i64 = 0 - 1 109 var k: i64 = 0 110 while k < nsc { 111 let b: i64 = k * DRC_S_SLOTS 112 if ce - cs == st[b + DRC_S_LEN] { 113 var m: i64 = 0 114 var eq: i64 = 1 115 while m < ce - cs { if q[cs + m] != q[st[b + DRC_S_OFF] + m] { eq = 0; m = ce - cs } else { m = m + 1 } } 116 if eq == 1 { idx = k; k = nsc } 117 } 118 k = k + 1 119 } 120 if idx < 0 { 121 if nsc >= cap { r[DRC_R_CAPPED] = 1 } else { 122 let b2: i64 = nsc * DRC_S_SLOTS 123 st[b2 + DRC_S_OFF] = cs; st[b2 + DRC_S_LEN] = ce - cs; st[b2 + DRC_S_FILED] = 0; st[b2 + DRC_S_EATEN] = 0 124 st[b2 + DRC_S_PRIOR] = 0; st[b2 + DRC_S_RTOTAL] = 0; st[b2 + DRC_S_ROPEN] = 0; st[b2 + DRC_S_NIDS] = 0 125 idx = nsc 126 nsc = nsc + 1 127 } 128 } 129 if idx >= 0 { 130 let b3: i64 = idx * DRC_S_SLOTS 131 st[b3 + DRC_S_FILED] = st[b3 + DRC_S_FILED] + 1 132 // a row filed after this scope's first resolved filing is a recurrence of the class (tested BEFORE this 133 // row can become the prior, so the prior itself never counts) 134 if st[b3 + DRC_S_PRIOR] != 0 { 135 st[b3 + DRC_S_RTOTAL] = st[b3 + DRC_S_RTOTAL] + 1 136 if isopen == 1 { st[b3 + DRC_S_ROPEN] = st[b3 + DRC_S_ROPEN] + 1 } 137 if st[b3 + DRC_S_NIDS] < DRC_IDS_PER_SCOPE { ids[idx * DRC_IDS_PER_SCOPE + st[b3 + DRC_S_NIDS]] = id; st[b3 + DRC_S_NIDS] = st[b3 + DRC_S_NIDS] + 1 } 138 } 139 if isopen == 0 { st[b3 + DRC_S_EATEN] = st[b3 + DRC_S_EATEN] + 1; if st[b3 + DRC_S_PRIOR] == 0 { st[b3 + DRC_S_PRIOR] = id } } 140 } 141 } 142 i = le + 1 143 } 144 r[DRC_R_SCOPES] = nsc 145 var k2: i64 = 0 146 while k2 < nsc { 147 let b4: i64 = k2 * DRC_S_SLOTS 148 if st[b4 + DRC_S_RTOTAL] > 0 { r[DRC_R_SCOPES_RECUR] = r[DRC_R_SCOPES_RECUR] + 1 } 149 r[DRC_R_ROPEN] = r[DRC_R_ROPEN] + st[b4 + DRC_S_ROPEN] 150 r[DRC_R_RTOTAL] = r[DRC_R_RTOTAL] + st[b4 + DRC_S_RTOTAL] 151 k2 = k2 + 1 152 } 153 return nsc 154} 155 156// the JSON object "recurrence":{...} at d[p0..]; returns the new offset. Ranked by recur_open desc, then recur_total. 157func drc_emit_json(d: *u8, p0: i64, q: *u8, st: *i64, ids: *i64, nsc: i64, r: *i64, topn: i64) -> i64 { 158 var p: i64 = p0 159 p = drc_key(d, p, "recurrence" as *u8) 160 d[p] = DRC_LBRACE as u8; p = p + 1 161 p = drc_key(d, p, "close_time" as *u8); p = drc_str(d, p, "UNRECORDED" as *u8); d[p] = DRC_COMMA as u8; p = p + 1 162 p = drc_key(d, p, "bound" as *u8); p = drc_str(d, p, "UPPER" as *u8); d[p] = DRC_COMMA as u8; p = p + 1 163 p = drc_key(d, p, "rows" as *u8); p = sj_catn(d, p, r[DRC_R_ROWS]); d[p] = DRC_COMMA as u8; p = p + 1 164 p = drc_key(d, p, "malformed" as *u8); p = sj_catn(d, p, r[DRC_R_MALFORMED]); d[p] = DRC_COMMA as u8; p = p + 1 165 p = drc_key(d, p, "id_bursts_adjacent" as *u8); p = sj_catn(d, p, r[DRC_R_IDBURST]); d[p] = DRC_COMMA as u8; p = p + 1 166 p = drc_key(d, p, "scopes" as *u8); p = sj_catn(d, p, r[DRC_R_SCOPES]); d[p] = DRC_COMMA as u8; p = p + 1 167 p = drc_key(d, p, "scopes_capped" as *u8); p = sj_catn(d, p, r[DRC_R_CAPPED]); d[p] = DRC_COMMA as u8; p = p + 1 168 p = drc_key(d, p, "scopes_with_recurrence" as *u8); p = sj_catn(d, p, r[DRC_R_SCOPES_RECUR]); d[p] = DRC_COMMA as u8; p = p + 1 169 p = drc_key(d, p, "recur_open" as *u8); p = sj_catn(d, p, r[DRC_R_ROPEN]); d[p] = DRC_COMMA as u8; p = p + 1 170 p = drc_key(d, p, "recur_total" as *u8); p = sj_catn(d, p, r[DRC_R_RTOTAL]); d[p] = DRC_COMMA as u8; p = p + 1 171 p = drc_key(d, p, "by_scope" as *u8) 172 d[p] = DRC_LBRACK as u8; p = p + 1 173 let done: *i64 = sys_mmap((nsc + 1) * DRC_WORD) as *i64 174 var shown: i64 = 0 175 var first: i64 = 1 176 var pass: i64 = 0 177 while pass < nsc { 178 var mi: i64 = 0 - 1 179 var mx: i64 = 0 - 1 180 var mt: i64 = 0 - 1 181 var k: i64 = 0 182 while k < nsc { 183 if done[k] == 0 { 184 let b: i64 = k * DRC_S_SLOTS 185 if st[b + DRC_S_ROPEN] > mx { mx = st[b + DRC_S_ROPEN]; mt = st[b + DRC_S_RTOTAL]; mi = k } else { 186 if st[b + DRC_S_ROPEN] == mx { if st[b + DRC_S_RTOTAL] > mt { mt = st[b + DRC_S_RTOTAL]; mi = k } } 187 } 188 } 189 k = k + 1 190 } 191 if mi >= 0 { 192 done[mi] = 1 193 let b5: i64 = mi * DRC_S_SLOTS 194 if st[b5 + DRC_S_RTOTAL] > 0 { if shown < topn { 195 if first == 0 { d[p] = DRC_COMMA as u8; p = p + 1 } 196 first = 0 197 d[p] = DRC_LBRACE as u8; p = p + 1 198 p = drc_key(d, p, "scope" as *u8); d[p] = DRC_QUOTE as u8; p = p + 1; p = sj_cat_esc(d, p, q, st[b5 + DRC_S_OFF], st[b5 + DRC_S_OFF] + st[b5 + DRC_S_LEN], DRC_NAME_MAX); d[p] = DRC_QUOTE as u8; p = p + 1; d[p] = DRC_COMMA as u8; p = p + 1 199 p = drc_key(d, p, "filed" as *u8); p = sj_catn(d, p, st[b5 + DRC_S_FILED]); d[p] = DRC_COMMA as u8; p = p + 1 200 p = drc_key(d, p, "eaten" as *u8); p = sj_catn(d, p, st[b5 + DRC_S_EATEN]); d[p] = DRC_COMMA as u8; p = p + 1 201 p = drc_key(d, p, "prior_id" as *u8); p = sj_catn(d, p, st[b5 + DRC_S_PRIOR]); d[p] = DRC_COMMA as u8; p = p + 1 202 p = drc_key(d, p, "recur_total" as *u8); p = sj_catn(d, p, st[b5 + DRC_S_RTOTAL]); d[p] = DRC_COMMA as u8; p = p + 1 203 p = drc_key(d, p, "recur_open" as *u8); p = sj_catn(d, p, st[b5 + DRC_S_ROPEN]); d[p] = DRC_COMMA as u8; p = p + 1 204 p = drc_key(d, p, "recur_ids" as *u8) 205 d[p] = DRC_LBRACK as u8; p = p + 1 206 var j: i64 = 0 207 while j < st[b5 + DRC_S_NIDS] { if j > 0 { d[p] = DRC_COMMA as u8; p = p + 1 } p = sj_catn(d, p, ids[mi * DRC_IDS_PER_SCOPE + j]); j = j + 1 } 208 d[p] = DRC_RBRACK as u8; p = p + 1 209 d[p] = DRC_COMMA as u8; p = p + 1 210 p = drc_key(d, p, "recur_ids_shown" as *u8); p = sj_catn(d, p, st[b5 + DRC_S_NIDS]); d[p] = DRC_COMMA as u8; p = p + 1 211 p = drc_key(d, p, "recur_ids_of" as *u8); p = sj_catn(d, p, st[b5 + DRC_S_RTOTAL]) 212 d[p] = DRC_RBRACE as u8; p = p + 1 213 shown = shown + 1 214 } } 215 } 216 pass = pass + 1 217 } 218 d[p] = DRC_RBRACK as u8; p = p + 1 219 d[p] = DRC_COMMA as u8; p = p + 1 220 p = drc_key(d, p, "scopes_shown" as *u8); p = sj_catn(d, p, shown) 221 d[p] = DRC_RBRACE as u8; p = p + 1 222 return p 223} 224 225// the spine path derived from the source: production iff the source IS the live plane, else a sidecar beside it 226func drc_spine_path(src: *u8, out: *u8) -> i64 { 227 if drc_streq(src, DRC_PROD_PREFIX) == 1 { return sj_cat(out, 0, DRC_SPINE_PROD) } 228 let o: i64 = sj_cat(out, 0, src) 229 return sj_cat(out, o, DRC_SPINE_SIDECAR) 230} 231// one spine row per run that measured rows, announced INSIDE the document (fd, wrote, of) so the receipt stays JSON 232func drc_spine_json(d: *u8, p0: i64, src: *u8, now: i64, r: *i64) -> i64 { 233 var p: i64 = p0 234 p = drc_key(d, p, "recurrence_spine" as *u8) 235 d[p] = DRC_LBRACE as u8; p = p + 1 236 if r[DRC_R_ROWS] <= 0 { 237 p = drc_key(d, p, "path" as *u8); p = drc_str(d, p, "NONE" as *u8); d[p] = DRC_COMMA as u8; p = p + 1 238 p = drc_key(d, p, "reason" as *u8); p = drc_str(d, p, "no rows measured: an unobservable run writes no trend point" as *u8) 239 } else { 240 let path: *u8 = sys_mmap(DRC_PATH) 241 drc_spine_path(src, path) 242 let row: *u8 = sys_mmap(DRC_LINE) 243 var o: i64 = sj_catn(row, 0, now) 244 o = sj_cat(row, o, "|scopes=" as *u8); o = sj_catn(row, o, r[DRC_R_SCOPES]) 245 o = sj_cat(row, o, "|with_recurrence=" as *u8); o = sj_catn(row, o, r[DRC_R_SCOPES_RECUR]) 246 o = sj_cat(row, o, "|recur_open=" as *u8); o = sj_catn(row, o, r[DRC_R_ROPEN]) 247 o = sj_cat(row, o, "|recur_total=" as *u8); o = sj_catn(row, o, r[DRC_R_RTOTAL]) 248 o = sj_cat(row, o, "|rows=" as *u8); o = sj_catn(row, o, r[DRC_R_ROWS]) 249 o = sj_cat(row, o, "|capped=" as *u8); o = sj_catn(row, o, r[DRC_R_CAPPED]) 250 row[o] = DRC_NL as u8; o = o + 1; row[o] = 0 as u8 251 let fd: i64 = sys_openat_append(path, DRC_MODE644) 252 var wrote: i64 = 0 - 1 253 if fd >= 0 { wrote = sys_write(fd, row, o); sys_close(fd) } 254 p = drc_key(d, p, "path" as *u8); p = drc_str(d, p, path); d[p] = DRC_COMMA as u8; p = p + 1 255 p = drc_key(d, p, "fd" as *u8); p = sj_catn(d, p, fd); d[p] = DRC_COMMA as u8; p = p + 1 256 p = drc_key(d, p, "wrote" as *u8); p = sj_catn(d, p, wrote); d[p] = DRC_COMMA as u8; p = p + 1 257 p = drc_key(d, p, "of" as *u8); p = sj_catn(d, p, o) 258 } 259 d[p] = DRC_RBRACE as u8; p = p + 1 260 return p 261}