code wiki / (root) / nx_regdup.nx

nx_regdup.nx source

↩ module page · 292 lines · 10810 B

1// nx_regdup.nx -- DUPLICATE-KEY DETECTOR FOR ANY TSV REGISTRY. Fail-closed, additive, general. 2// 3// WHY. On 2026-08-07 the papers registry carried the identifier RT-012 TWICE, on two different 4// manuscripts, and `nx_papers_index` rendered both sections without complaint -- so the live page 5// published a collision as authority. That is this estate's own banked law: 6// ★★★★★★ A GENERATED INDEX INHERITS ITS REGISTRY'S ERRORS AND LAUNDERS THEM AS AUTHORITY. 7// The generator was not wrong; nothing was CHECKING. And the same session added a THIRD collision, 8// because its author declared an id free after reading WINDOWS of the registry instead of enumerating 9// it ⇒ ★★★★★★ A SAMPLE OF A FILE CANNOT ESTABLISH THAT A KEY IS FREE. ONLY AN ENUMERATION CAN. 10// 11// WHY A SEPARATE ORGAN AND NOT AN EDIT TO nx_papers_index. Measured the same day: the LOCAL and NAS 12// copies of that organ have FORKED -- the local source emits a different page design than the one 13// actually served. Editing the local copy and promoting it would have silently regressed the live 14// page's design while "fixing" a duplicate. So this is additive: it adds an invariant nothing checked 15// before, touches no serving organ, and can regress nothing by construction. 16// ⚠It is NOT a second ruler for an existing invariant (that would be the duplicate-ruler defect) -- 17// no organ in this estate checked registry key uniqueness at all. 18// 19// GENERAL ON PURPOSE. Every plane here is key-first TSV, and at least one other is known defective: 20// the clockjobs plane is recorded at 67 rows over 50 distinct names, i.e. 17 surplus, where the LIVE 21// row is the OLDEST because the registrar is idempotent-by-name. One ruler, every registry. 22// 23// nx_regdup check <registry.tsv> [keycol] exit 0 UNIQUE · 1 DUPLICATES · 3 unreadable 24// nx_regdup selftest 25// license_tier: ORIGINAL No hardware writes (Rule 26). 26import "nx_syscalls.nx" 27import "nx_gate_verdict.nx" 28 29const RD_CAP: i64 = 1048576 30const RD_MAXROWS: i64 = 8192 31const RD_EXIT_USAGE: i64 = 2 32const RD_EXIT_UNREADABLE: i64 = 3 33const RD_TAB: i64 = 9 34const RD_NL: i64 = 10 35 36func rd_err(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(2, s, n); return 0 } 37func rd_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 38func rd_streq(a: *u8, b: *u8) -> i64 { 39 var i: i64 = 0 40 while a[i] != (0 as u8) { if a[i] != b[i] { return 0 } i = i + 1 } 41 if b[i] != (0 as u8) { return 0 } 42 return 1 43} 44func rd_atoi(s: *u8) -> i64 { 45 var i: i64 = 0 46 var v: i64 = 0 47 var go: i64 = 1 48 while go == 1 { 49 let c: i64 = s[i] as i64 50 if c < 48 { go = 0 } else { if c > 57 { go = 0 } else { v = (v * 10) + (c - 48); i = i + 1 } } 51 } 52 return v 53} 54func rd_len(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 55func rd_read(path: *u8, buf: *u8, cap: i64) -> i64 { 56 let fd: i64 = sys_openat_rd(path) 57 if fd < 0 { return 0 - 1 } 58 var n: i64 = 0 59 var go: i64 = 1 60 while go == 1 { 61 let base: i64 = buf as i64 62 let r: i64 = sys_read(fd, (base + n) as *u8, cap - n) 63 if r <= 0 { go = 0 } else { n = n + r } 64 if n >= cap { go = 0 } 65 } 66 sys_close(fd) 67 return n 68} 69func rd_eol(buf: *u8, n: i64, i: i64) -> i64 { 70 var e: i64 = i 71 var go: i64 = 1 72 while go == 1 { 73 if e >= n { go = 0 } else { if (buf[e] as i64) == RD_NL { go = 0 } else { e = e + 1 } } 74 } 75 return e 76} 77// [start,end) of field `k` within line [ls,le). Returns 0 if the line has fewer than k+1 fields. 78func rd_field(buf: *u8, ls: i64, le: i64, k: i64, out: *i64) -> i64 { 79 var f: i64 = 0 80 var s: i64 = ls 81 var i: i64 = ls 82 var go: i64 = 1 83 while go == 1 { 84 if i >= le { 85 if f == k { out[0] = s; out[1] = le; return 1 } 86 go = 0 87 } else { 88 if (buf[i] as i64) == RD_TAB { 89 if f == k { out[0] = s; out[1] = i; return 1 } 90 f = f + 1 91 s = i + 1 92 } 93 i = i + 1 94 } 95 } 96 return 0 97} 98func rd_range_eq(buf: *u8, a: *i64, b: *i64) -> i64 { 99 let la: i64 = a[1] - a[0] 100 let lb: i64 = b[1] - b[0] 101 if la != lb { return 0 } 102 var k: i64 = 0 103 while k < la { 104 if buf[a[0] + k] != buf[b[0] + k] { return 0 } 105 k = k + 1 106 } 107 return 1 108} 109// res[0]=rows res[1]=distinct res[2]=surplus res[3]=keyless_rows 110func rd_scan(buf: *u8, n: i64, keycol: i64, res: *i64, verbose: i64) -> i64 { 111 let ks: *i64 = sys_mmap(8 * RD_MAXROWS) as *i64 112 let ke: *i64 = sys_mmap(8 * RD_MAXROWS) as *i64 113 let fr: *i64 = sys_mmap(2 * 8) as *i64 114 var rows: i64 = 0 115 var keyless: i64 = 0 116 var i: i64 = 0 117 var go: i64 = 1 118 while go == 1 { 119 if i >= n { go = 0 } else { 120 let le: i64 = rd_eol(buf, n, i) 121 if le > i { 122 if rows < RD_MAXROWS { 123 if rd_field(buf, i, le, keycol, fr) == 1 { 124 ks[rows] = fr[0] 125 ke[rows] = fr[1] 126 rows = rows + 1 127 } else { keyless = keyless + 1 } 128 } else { 129 rd_puts("REGDUP REFUSED: more rows than the buffer admits; a capped scan reporting UNIQUE is a false all-clear\n" as *u8) 130 res[0] = 0 - 1 131 return 0 - 1 132 } 133 } 134 i = le + 1 135 } 136 } 137 let a: *i64 = sys_mmap(2 * 8) as *i64 138 let b: *i64 = sys_mmap(2 * 8) as *i64 139 var distinct: i64 = 0 140 var surplus: i64 = 0 141 var r: i64 = 0 142 while r < rows { 143 a[0] = ks[r] 144 a[1] = ke[r] 145 var first: i64 = 0 - 1 146 var p: i64 = 0 147 while p < r { 148 if first < 0 { 149 b[0] = ks[p] 150 b[1] = ke[p] 151 if rd_range_eq(buf, a, b) == 1 { first = p } 152 } 153 p = p + 1 154 } 155 if first < 0 { distinct = distinct + 1 } else { 156 surplus = surplus + 1 157 if verbose == 1 { 158 rd_puts(" DUPLICATE KEY: " as *u8) 159 sys_write(1, (ks[r] as i64) as *u8, ke[r] - ks[r]) 160 rd_puts(" -- row " as *u8) 161 gv_num(r) 162 rd_puts(" collides with row " as *u8) 163 gv_num(first) 164 rd_puts("\n" as *u8) 165 } 166 } 167 r = r + 1 168 } 169 res[0] = rows 170 res[1] = distinct 171 res[2] = surplus 172 res[3] = keyless 173 return 0 174} 175 176func rd_check(path: *u8, keycol: i64) -> i64 { 177 let buf: *u8 = sys_mmap(RD_CAP) 178 let n: i64 = rd_read(path, buf, RD_CAP - 16) 179 if n <= 0 { 180 rd_puts("REGDUP REFUSED: registry unreadable or empty -> " as *u8) 181 rd_puts(path) 182 rd_puts("\n (an unreadable registry is not a unique one; refusing to report UNIQUE)\n" as *u8) 183 return RD_EXIT_UNREADABLE 184 } 185 let res: *i64 = sys_mmap(8 * 8) as *i64 186 rd_puts("nx_regdup " as *u8) 187 rd_puts(path) 188 rd_puts(" keycol=" as *u8) 189 gv_num(keycol) 190 rd_puts("\n" as *u8) 191 if rd_scan(buf, n, keycol, res, 1) < 0 { return RD_EXIT_UNREADABLE } 192 rd_puts(" rows=" as *u8) 193 gv_num(res[0]) 194 rd_puts(" distinct=" as *u8) 195 gv_num(res[1]) 196 rd_puts(" surplus=" as *u8) 197 gv_num(res[2]) 198 rd_puts(" keyless=" as *u8) 199 gv_num(res[3]) 200 rd_puts(" partition_sums=" as *u8) 201 if (res[1] + res[2]) == res[0] { rd_puts("true" as *u8) } else { rd_puts("FALSE" as *u8) } 202 rd_puts("\n" as *u8) 203 if res[2] > 0 { return 1 } 204 return 0 205} 206 207func rd_fx_dup() -> *u8 { 208 return "RT-011\tinternal\ta.md\tAlpha\nRT-012\tinternal\tb.md\tBeta\nRT-013\tinternal\tc.md\tGamma\nRT-012\tinternal\td.md\tDelta\n" as *u8 209} 210func rd_fx_clean() -> *u8 { 211 return "RT-011\tinternal\ta.md\tAlpha\nRT-012\tinternal\tb.md\tBeta\nRT-013\tinternal\tc.md\tGamma\nRT-014\tinternal\td.md\tDelta\n" as *u8 212} 213 214func rd_selftest() -> i64 { 215 let ctr: *i64 = gv_ctr() 216 gv_head("nx_regdup -- a registry key collision must be found before a generator publishes it" as *u8) 217 let res: *i64 = sys_mmap(8 * 8) as *i64 218 let d: *u8 = rd_fx_dup() 219 let dn: i64 = rd_len(d) 220 let c: *u8 = rd_fx_clean() 221 let cn: i64 = rd_len(c) 222 223 rd_scan(d, dn, 0, res, 0) 224 var ok: i64 = 0 225 if res[0] == 4 { ok = 1 } 226 gv_check("T0 duplicate fixture parsed to exactly 4 rows" as *u8, ok, ctr) 227 ok = 0 228 if res[2] == 1 { ok = 1 } 229 gv_check("T1 exactly ONE surplus key found, not zero and not two" as *u8, ok, ctr) 230 ok = 0 231 if res[1] == 3 { ok = 1 } 232 gv_check("T2 distinct=3 (the collision is counted once, not dropped)" as *u8, ok, ctr) 233 ok = 0 234 if (res[1] + res[2]) == res[0] { ok = 1 } 235 gv_check("T3 partition sums: distinct + surplus == rows" as *u8, ok, ctr) 236 let dupsurplus: i64 = res[2] 237 238 rd_scan(c, cn, 0, res, 0) 239 ok = 0 240 if res[0] == 4 { ok = 1 } 241 gv_check("T4 clean fixture parsed to exactly 4 rows (same shape, one byte apart)" as *u8, ok, ctr) 242 ok = 0 243 if res[2] == 0 { ok = 1 } 244 gv_check("neg-control-clean: a registry with no collision reports zero surplus" as *u8, ok, ctr) 245 ok = 0 246 if res[1] == 4 { ok = 1 } 247 gv_check("neg-control-distinct: all four keys counted distinct" as *u8, ok, ctr) 248 let cleansurplus: i64 = res[2] 249 250 var bad: i64 = 0 251 if dupsurplus > 0 { bad = 1 } 252 var good: i64 = 0 253 if cleansurplus > 0 { good = 1 } 254 gv_bite("T5 collision-detector" as *u8, bad, good, ctr) 255 256 rd_scan(d, dn, 1, res, 0) 257 ok = 0 258 if res[2] == 3 { ok = 1 } 259 gv_check("T6 keycol is honoured: column 1 is constant, so 3 of 4 rows are surplus" as *u8, ok, ctr) 260 261 gv_puts("\n measured: dup-fixture surplus=" as *u8) 262 gv_num(dupsurplus) 263 gv_puts(" clean-fixture surplus=" as *u8) 264 gv_num(cleansurplus) 265 gv_puts("\n" as *u8) 266 return gv_verdict("REGDUP" as *u8, ctr, "collision detector bite-proven against a one-character-different twin" as *u8) 267} 268 269func main(argc: i64, argv: *i64) -> i64 { 270 if argc < 2 { 271 rd_err("usage: nx_regdup {check <registry.tsv> [keycol] | selftest}\n" as *u8) 272 sys_exit(RD_EXIT_USAGE) 273 return RD_EXIT_USAGE 274 } 275 let verb: *u8 = argv[1] as *u8 276 if rd_streq(verb, "selftest" as *u8) == 1 { 277 let rc: i64 = rd_selftest() 278 sys_exit(rc) 279 return rc 280 } 281 if rd_streq(verb, "check" as *u8) == 1 { 282 if argc < 3 { rd_err("usage: nx_regdup check <registry.tsv> [keycol]\n" as *u8); sys_exit(RD_EXIT_USAGE); return RD_EXIT_USAGE } 283 var kc: i64 = 0 284 if argc >= 4 { kc = rd_atoi(argv[3] as *u8) } 285 let rc: i64 = rd_check(argv[2] as *u8, kc) 286 sys_exit(rc) 287 return rc 288 } 289 rd_err("unknown verb\n" as *u8) 290 sys_exit(RD_EXIT_USAGE) 291 return RD_EXIT_USAGE 292}