code wiki / (root) / nx_comparewatch_census.nx

nx_comparewatch_census.nx source

↩ module page · 263 lines · 12953 B

1// nx_comparewatch_census.nx -- THE /compare DISTANCE-TO-100%, made a one-call sovereign measurement 2// (2026-08-29). Prints total/OPEN/LANDED per the watch plane, per-domain, the zero-gap frontier, and 3// appends one trend row per run. Before this organ the only correct count ever taken was a HAND TALLY. 4// 5// COMPOSES, NEVER RE-IMPLEMENTS: 6// - parsing/versioning rules: nx_comparewatch_lib (pure -- the gate exercises the same code); 7// - plane iteration: nx_seg_store's SEQUENTIAL CURSOR (ss_cur_open/ss_cur_next). The naive 8// whole-plane pass via ss_get point lookups is the documented quadratic read defect -- the seg 9// store's own header measures 10,000 lookups DYING where the cursor survives; one mapping per 10// SEGMENT, not per record. 11// 12// HONEST LIMITS, stated not hidden: 13// - ss_cur_next treats an UNREADABLE segment exactly like an empty one (it moves to the next), so a 14// torn segment silently contributes zero rows AT THE CURSOR LAYER. The compensating witness is the 15// plane's own q:n declared-count row: measured-live != declared -> verdict RED. Books that do not 16// balance are the alarm; this organ cannot name WHICH segment tore. 17// - rows are counted by KEY (last version wins, tombstones excluded); q:n counts what the writer 18// last declared. The regen re-puts the whole plane, so the two agree in health. 19// 20// VERDICT VOCABULARY (closed): GREEN = books balance (live==declared, partition sums, no foreign keys 21// over the bar); RED = the instrument's books do NOT balance -- an accounting alarm, NOT "there are 22// open contracts" (826 open is a normal state, not a failure); UNMEASURABLE(exit 3) = no plane. 23// exit: 0 GREEN | 1 RED | 3 UNMEASURABLE 24// license_tier: ORIGINAL No hw writes (Rule 26). 25import "nx_syscalls.nx" 26import "nx_seg_store.nx" 27import "nx_comparewatch_lib.nx" 28 29const CWC_PREFIX_DEFAULT: *u8 = "knowledge/store/comparewatch-" 30const CWC_JRNL: *u8 = "knowledge/status/comparewatch_census.log" 31// capacity bounds -- each announces when exceeded (no silent caps): plane is ~1k rows / ~93 domains. 32const CWC_MAX_ROWS: i64 = 8192 33const CWC_DOM_W: i64 = 48 34const CWC_MAX_DOMS: i64 = 256 35const CWC_EXIT_RED: i64 = 1 36const CWC_EXIT_UNMEASURABLE: i64 = 3 37const CWC_EXIT_USAGE: i64 = 2 38 39func cwc_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 40func cwc_num(v: i64) -> i64 { 41 var m: i64 = v 42 if m < 0 { cwc_puts("-" as *u8); m = 0 - m } 43 let t: *u8 = sys_mmap(32) 44 var k: i64 = 0 45 if m == 0 { t[0] = 48 as u8; k = 1 } 46 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 47 let o: *u8 = sys_mmap(32) 48 var i: i64 = 0 49 while i < k { o[i] = t[k - 1 - i]; i = i + 1 } 50 sys_write(1, o, k) 51 return 0 52} 53func cwc_cat(b: *u8, o: i64, s: *u8) -> i64 { var i: i64 = 0; var p: i64 = o; while s[i] != (0 as u8) { b[p] = s[i]; p = p + 1; i = i + 1 } return p } 54func cwc_catn(b: *u8, o: i64, v: i64) -> i64 { 55 var m: i64 = v 56 var p: i64 = o 57 if m < 0 { b[p] = 45 as u8; p = p + 1; m = 0 - m } 58 let t: *u8 = sys_mmap(32) 59 var k: i64 = 0 60 if m == 0 { t[0] = 48 as u8; k = 1 } 61 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 62 var i: i64 = 0 63 while i < k { b[p + i] = t[k - 1 - i]; i = i + 1 } 64 return p + k 65} 66func cwc_streqz(a: *u8, b: *u8) -> i64 { 67 var i: i64 = 0 68 while a[i] != (0 as u8) { if a[i] != b[i] { return 0 } i = i + 1 } 69 if b[i] != (0 as u8) { return 0 } 70 return 1 71} 72 73// ONE WRITER, ONE LOG, ONE ROW (the rm_trend_log law): the run's durable artifact so two runs are a 74// trend. An unwritable journal ANNOUNCES rather than vanishing. 75func cwc_jrnl(rows: i64, open: i64, landed: i64, other: i64, malformed: i64, tomb: i64, foreign: i64, declared: i64, doms: i64, zerogap: i64, verdict: *u8) -> i64 { 76 let fd: i64 = sys_openat_append(CWC_JRNL, MODE_0644) 77 if fd < 0 { cwc_puts("CWC jrnl-unwritable -- row lost, said out loud\n" as *u8); return 0 - 1 } 78 let b: *u8 = sys_mmap(512) 79 var o: i64 = cwc_cat(b, 0, "ts=" as *u8) 80 o = cwc_catn(b, o, sys_now_realtime_sec()) 81 o = cwc_cat(b, o, " rows=" as *u8); o = cwc_catn(b, o, rows) 82 o = cwc_cat(b, o, " open=" as *u8); o = cwc_catn(b, o, open) 83 o = cwc_cat(b, o, " landed=" as *u8); o = cwc_catn(b, o, landed) 84 o = cwc_cat(b, o, " other=" as *u8); o = cwc_catn(b, o, other) 85 o = cwc_cat(b, o, " malformed=" as *u8); o = cwc_catn(b, o, malformed) 86 o = cwc_cat(b, o, " tombstoned=" as *u8); o = cwc_catn(b, o, tomb) 87 o = cwc_cat(b, o, " foreign_keys=" as *u8); o = cwc_catn(b, o, foreign) 88 o = cwc_cat(b, o, " declared=" as *u8); o = cwc_catn(b, o, declared) 89 o = cwc_cat(b, o, " domains=" as *u8); o = cwc_catn(b, o, doms) 90 o = cwc_cat(b, o, " zero_gap_domains=" as *u8); o = cwc_catn(b, o, zerogap) 91 o = cwc_cat(b, o, " verdict=" as *u8) 92 o = cwc_cat(b, o, verdict) 93 b[o] = 10 as u8 94 sys_write(fd, b, o + 1) 95 sys_close(fd) 96 return 0 97} 98 99func main(argc: i64, argv: *i64) -> i64 { 100 var prefix: *u8 = CWC_PREFIX_DEFAULT 101 if argc >= 2 { prefix = argv[1] as *u8 } 102 // A BARE PREFIX RESOLVES AGAINST CWD AND READS A POPULATED PLANE AS EMPTY -- the banked 103 // planepeek trap. Refuse it by name instead of answering wrongly. 104 var hasslash: i64 = 0 105 var pi: i64 = 0 106 while prefix[pi] != (0 as u8) { if (prefix[pi] as i64) == 47 { hasslash = 1 } pi = pi + 1 } 107 if hasslash == 0 { 108 cwc_puts("CWC REFUSED bare-prefix: spell the FULL path (knowledge/store/comparewatch-). A bare prefix resolves against CWD and reports a populated plane as EMPTY.\n" as *u8) 109 sys_exit(CWC_EXIT_USAGE) 110 return CWC_EXIT_USAGE 111 } 112 113 let stArr: *i64 = sys_mmap(8 * CWC_MAX_ROWS) as *i64 114 let domBuf: *u8 = sys_mmap(CWC_MAX_ROWS * CWC_DOM_W) 115 var maxidx: i64 = 0 - 1 116 var declared: i64 = 0 - 1 117 var foreign: i64 = 0 118 var overflow: i64 = 0 119 var records: i64 = 0 120 121 let cur: *i64 = ss_cur_open(prefix) 122 if cur[1] == 0 { 123 cwc_puts("CWC UNMEASURABLE: no segments resolve under " as *u8) 124 cwc_puts(prefix) 125 cwc_puts(" -- this is NOT an empty-plane claim; could-not-look and nothing-there must never share a representation.\n" as *u8) 126 cwc_jrnl(0, 0, 0, 0, 0, 0, 0, 0 - 1, 0, 0, "UNMEASURABLE" as *u8) 127 sys_exit(CWC_EXIT_UNMEASURABLE) 128 return CWC_EXIT_UNMEASURABLE 129 } 130 131 let ko: *i64 = sys_mmap(16) as *i64 132 let kl: *i64 = sys_mmap(16) as *i64 133 let vo: *i64 = sys_mmap(16) as *i64 134 let vl: *i64 = sys_mmap(16) as *i64 135 let f2: *i64 = sys_mmap(16) as *i64 136 var walking: i64 = 1 137 while walking == 1 { 138 let r: i64 = ss_cur_next(prefix, cur, ko, kl, vo, vl) 139 if r == 0 { walking = 0 } else { 140 records = records + 1 141 let kidx: i64 = cw_key_idx(ko[0] as *u8, kl[0]) 142 if kidx == CW_KEY_COUNT { 143 declared = cw_parse_count(vo[0] as *u8, vl[0]) 144 } else { if kidx == CW_KEY_FOREIGN { 145 foreign = foreign + 1 146 } else { 147 if kidx > maxidx { maxidx = kidx } 148 if cur[6] == 2 { 149 if cw_apply(stArr, domBuf, CWC_MAX_ROWS, CWC_DOM_W, kidx, CW_TOMB, "-" as *u8, 1) == 0 { overflow = overflow + 1 } 150 } else { 151 let v: *u8 = vo[0] as *u8 152 let st: i64 = cw_status_code(v, vl[0]) 153 var dp: *u8 = "?" as *u8 154 var dl: i64 = 1 155 if cw_field(v, vl[0], 1, f2) == 1 { dp = ((v as i64) + f2[0]) as *u8; dl = f2[1] } 156 if cw_apply(stArr, domBuf, CWC_MAX_ROWS, CWC_DOM_W, kidx, st, dp, dl) == 0 { overflow = overflow + 1 } 157 } 158 } } 159 } 160 } 161 162 // ---- totals + per-domain aggregation over the final (last-version) state -------------------- 163 let domNames: *u8 = sys_mmap(CWC_MAX_DOMS * CWC_DOM_W) 164 let domOpen: *i64 = sys_mmap(8 * CWC_MAX_DOMS) as *i64 165 let domLanded: *i64 = sys_mmap(8 * CWC_MAX_DOMS) as *i64 166 let domOther: *i64 = sys_mmap(8 * CWC_MAX_DOMS) as *i64 167 var ndoms: i64 = 0 168 var domoverflow: i64 = 0 169 var open: i64 = 0 170 var landed: i64 = 0 171 var other: i64 = 0 172 var malformed: i64 = 0 173 var tomb: i64 = 0 174 var i: i64 = 0 175 while i <= maxidx { 176 let st: i64 = stArr[i] 177 if st != CW_UNSET { 178 if st == CW_TOMB { tomb = tomb + 1 } else { 179 if st == CW_OPEN { open = open + 1 } 180 if st == CW_LANDED { landed = landed + 1 } 181 if st == CW_OTHER { other = other + 1 } 182 if st == CW_MALFORMED { malformed = malformed + 1 } 183 let dn: *u8 = ((domBuf as i64) + i * CWC_DOM_W) as *u8 184 var slot: i64 = 0 - 1 185 var d: i64 = 0 186 while d < ndoms { 187 if cwc_streqz(((domNames as i64) + d * CWC_DOM_W) as *u8, dn) == 1 { slot = d; d = ndoms } else { d = d + 1 } 188 } 189 if slot < 0 { 190 if ndoms < CWC_MAX_DOMS { 191 slot = ndoms 192 var c: i64 = 0 193 var go: i64 = 1 194 while go == 1 { 195 let ch: u8 = dn[c] 196 domNames[slot * CWC_DOM_W + c] = ch 197 if ch == (0 as u8) { go = 0 } else { c = c + 1 } 198 } 199 ndoms = ndoms + 1 200 } else { domoverflow = domoverflow + 1 } 201 } 202 if slot >= 0 { 203 if st == CW_OPEN { domOpen[slot] = domOpen[slot] + 1 } 204 if st == CW_LANDED { domLanded[slot] = domLanded[slot] + 1 } 205 if st == CW_OTHER { domOther[slot] = domOther[slot] + 1 } 206 if st == CW_MALFORMED { domOther[slot] = domOther[slot] + 1 } 207 } 208 } 209 } 210 i = i + 1 211 } 212 let live: i64 = open + landed + other + malformed 213 214 cwc_puts("=== nx_comparewatch_census -- the /compare distance to 100 percent, measured not tallied ===\n" as *u8) 215 cwc_puts("plane=" as *u8); cwc_puts(prefix) 216 cwc_puts(" records_streamed=" as *u8); cwc_num(records) 217 cwc_puts(" live_rows=" as *u8); cwc_num(live) 218 cwc_puts(" declared_qn=" as *u8); cwc_num(declared) 219 cwc_puts("\n" as *u8) 220 cwc_puts("open=" as *u8); cwc_num(open) 221 cwc_puts(" landed=" as *u8); cwc_num(landed) 222 cwc_puts(" other_status=" as *u8); cwc_num(other) 223 cwc_puts(" malformed=" as *u8); cwc_num(malformed) 224 cwc_puts(" tombstoned=" as *u8); cwc_num(tomb) 225 cwc_puts(" foreign_keys=" as *u8); cwc_num(foreign) 226 cwc_puts(" overflow_refused=" as *u8); cwc_num(overflow) 227 cwc_puts(" PARTITION open+landed+other+malformed=" as *u8); cwc_num(live) 228 cwc_puts(" (sums by construction; tombstoned/foreign are SEPARATE axes, not partition members)\n" as *u8) 229 230 var zerogap: i64 = 0 231 var d2: i64 = 0 232 while d2 < ndoms { 233 cwc_puts(" dom=" as *u8); cwc_puts(((domNames as i64) + d2 * CWC_DOM_W) as *u8) 234 cwc_puts(" open=" as *u8); cwc_num(domOpen[d2]) 235 cwc_puts(" landed=" as *u8); cwc_num(domLanded[d2]) 236 if domOther[d2] > 0 { cwc_puts(" other=" as *u8); cwc_num(domOther[d2]) } 237 if domOpen[d2] == 0 { if domOther[d2] == 0 { if domLanded[d2] > 0 { 238 zerogap = zerogap + 1 239 cwc_puts(" <== ZERO-GAP: every declared contract MEASURED LANDED" as *u8) 240 } } } 241 cwc_puts("\n" as *u8) 242 d2 = d2 + 1 243 } 244 if domoverflow > 0 { cwc_puts(" DOMAIN-TABLE FULL: " as *u8); cwc_num(domoverflow); cwc_puts(" rows uncounted per-domain (totals unaffected) -- raise CWC_MAX_DOMS\n" as *u8) } 245 cwc_puts("domains=" as *u8); cwc_num(ndoms) 246 cwc_puts(" zero_gap_domains=" as *u8); cwc_num(zerogap) 247 cwc_puts("\n" as *u8) 248 249 // ---- verdict: an ACCOUNTING alarm, never an opinion about how much work remains ------------- 250 var red: i64 = 0 251 if declared >= 0 { if declared != live { red = 1 } } 252 if overflow > 0 { red = 1 } 253 if declared < 0 { cwc_puts("NOTE declared_qn UNAVAILABLE (no parseable q:n row) -- the unreadable-segment cross-check is OFF for this run and cannot acquit.\n" as *u8) } 254 if red == 1 { 255 cwc_puts("verdict=RED (the books do not balance: live_rows vs declared_qn disagree, or rows were refused at capacity. A torn/unreadable segment silently drops rows at the cursor layer -- this cross-check is the witness that catches it.)\n" as *u8) 256 cwc_jrnl(live, open, landed, other, malformed, tomb, foreign, declared, ndoms, zerogap, "RED" as *u8) 257 sys_exit(CWC_EXIT_RED) 258 return CWC_EXIT_RED 259 } 260 cwc_puts("verdict=GREEN (books balance; open contracts are WORK, not a failure of this instrument)\n" as *u8) 261 cwc_jrnl(live, open, landed, other, malformed, tomb, foreign, declared, ndoms, zerogap, "GREEN" as *u8) 262 return 0 263}