code wiki / (root) / nx_librarian_audit_gate.nx

nx_librarian_audit_gate.nx source

↩ module page · 183 lines · 7123 B

1// nx_librarian_audit_gate.nx -- LIBRARIAN rung-1 GATE + the live census console. Proves classification 2// and tallies EXACTLY on a synthetic fixture directory (known files, known bytes, computed permille), 3// liar-kills the empty case, then runs the REAL audit across the data roots and prints the sprawl table 4// -- the measured baseline the TSV-migration ladder must move. Exits 0 iff ALL pass. license_tier: ORIGINAL 5import "nx_syscalls.nx" 6import "nx_librarian_audit.nx" 7 8func g_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 9func g_putn(v: i64) -> i64 { 10 if v == 0 { sys_write(1, "0" as *u8, 1); return 0 } 11 var m: i64 = v 12 if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m } 13 let d: *u8 = sys_mmap(24) 14 var k: i64 = 0 15 while m > 0 { d[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 16 let r: *u8 = sys_mmap(24) 17 var i: i64 = 0 18 while k > 0 { k = k - 1; r[i] = d[k]; i = i + 1 } 19 sys_write(1, r, i) 20 return 0 21} 22func chk(name: *u8, got: i64, want: i64, st: *i64) -> i64 { 23 if got == want { st[0] = st[0] + 1; g_puts(" PASS " as *u8); g_puts(name); g_puts("\n" as *u8) } 24 else { st[1] = st[1] + 1; g_puts(" FAIL " as *u8); g_puts(name); g_puts(" got=" as *u8); g_putn(got); g_puts(" want=" as *u8); g_putn(want); g_puts("\n" as *u8) } 25 return 0 26} 27func mkfile(dir: *u8, name: *u8, nbytes: i64) -> i64 { 28 let path: *u8 = sys_mmap(512) 29 var o: i64 = 0 30 var i: i64 = 0 31 while dir[i] != (0 as u8) { path[o] = dir[i]; o = o + 1; i = i + 1 } 32 path[o] = 47 as u8 33 o = o + 1 34 i = 0 35 while name[i] != (0 as u8) { path[o] = name[i]; o = o + 1; i = i + 1 } 36 path[o] = 0 as u8 37 let fd: i64 = sys_openat_wr(path, 0x1a4) 38 if fd < 0 { return 0 - 1 } 39 let b: *u8 = sys_mmap(256) 40 var j: i64 = 0 41 while j < nbytes { b[j] = 120 as u8; j = j + 1 } 42 sys_write(fd, b, nbytes) 43 sys_close(fd) 44 return 0 45} 46func prow(label: *u8, st: *i64, cls: i64) -> i64 { 47 g_puts(" " as *u8) 48 g_puts(label) 49 g_puts(": files=" as *u8) 50 g_putn(st[cls * 2]) 51 g_puts(" bytes=" as *u8) 52 g_putn(st[cls * 2 + 1]) 53 g_puts("\n" as *u8) 54 return 0 55} 56 57func main() -> i64 { 58 let st: *i64 = sys_mmap(16) as *i64 59 st[0] = 0 60 st[1] = 0 61 62 // ---- fixture dir with known files ---- 63 let dir: *u8 = sys_mmap(128) 64 var o: i64 = 0 65 let pre: *u8 = "/tmp/la_\x00" as *u8 66 var i: i64 = 0 67 while pre[i] != (0 as u8) { dir[o] = pre[i]; o = o + 1; i = i + 1 } 68 var m: i64 = sys_now_us() 69 let t: *u8 = sys_mmap(32) 70 var k: i64 = 0 71 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 72 while k > 0 { k = k - 1; dir[o] = t[k]; o = o + 1 } 73 dir[o] = 0 as u8 74 sys_mkdir(dir, 0x1ed) 75 mkfile(dir, "a.tsv\x00" as *u8, 10) 76 mkfile(dir, "b.csv\x00" as *u8, 5) 77 mkfile(dir, "c.json\x00" as *u8, 7) 78 mkfile(dir, "d.q\x00" as *u8, 3) 79 mkfile(dir, "e.txt\x00" as *u8, 4) 80 mkfile(dir, "x-seg-1.docs\x00" as *u8, 6) 81 mkfile(dir, "f.log\x00" as *u8, 2) 82 mkfile(dir, "g\x00" as *u8, 8) 83 84 let fx: *i64 = sys_mmap(8 * 24) as *i64 85 let nf: i64 = la_scan_dir(dir, fx) 86 chk("fixture scans 8 regular files" as *u8, nf, 8, st) 87 chk("tsv count 1" as *u8, fx[0], 1, st) 88 chk("tsv bytes 10" as *u8, fx[1], 10, st) 89 chk("csv bytes 5" as *u8, fx[3], 5, st) 90 chk("json bytes 7" as *u8, fx[5], 7, st) 91 chk("pipe-data (.q) bytes 3" as *u8, fx[7], 3, st) 92 chk("txt bytes 4" as *u8, fx[9], 4, st) 93 chk("NATIVE seg-store bytes 6" as *u8, fx[13], 6, st) 94 chk("log bytes 2" as *u8, fx[15], 2, st) 95 chk("other (no ext) bytes 8" as *u8, fx[19], 8, st) 96 var tot: i64 = 0 97 i = 0 98 while i < 10 { tot = tot + fx[i * 2 + 1]; i = i + 1 } 99 chk("fixture total 45 bytes" as *u8, tot, 45, st) 100 chk("NATIVE permille = 133 (6 of 45)" as *u8, la_permil(fx[13], tot), 133, st) 101 102 // ---- liar-kill: empty dir ---- 103 let ed: *u8 = sys_mmap(160) 104 o = 0 105 i = 0 106 while dir[i] != (0 as u8) { ed[o] = dir[i]; o = o + 1; i = i + 1 } 107 let sub: *u8 = "/empty\x00" as *u8 108 i = 0 109 while sub[i] != (0 as u8) { ed[o] = sub[i]; o = o + 1; i = i + 1 } 110 ed[o] = 0 as u8 111 sys_mkdir(ed, 0x1ed) 112 let fe: *i64 = sys_mmap(8 * 24) as *i64 113 chk("NEG empty dir scans 0 files" as *u8, la_scan_dir(ed, fe), 0, st) 114 chk("missing dir -> -1" as *u8, la_scan_dir("/tmp/does-not-exist-la\x00" as *u8, fe), 0 - 1, st) 115 116 // ---- REAL audit across the data roots (the census console) ---- 117 let rt: *i64 = sys_mmap(8 * 24) as *i64 118 let roots: *i64 = sys_mmap(8 * 16) as *i64 119 roots[0] = "runtime\x00" as *u8 as i64 120 roots[1] = "knowledge/compare\x00" as *u8 as i64 121 roots[2] = "knowledge/medbill\x00" as *u8 as i64 122 roots[3] = "knowledge/store\x00" as *u8 as i64 123 roots[4] = "knowledge/fetched\x00" as *u8 as i64 124 roots[5] = "knowledge/library\x00" as *u8 as i64 125 roots[6] = "knowledge/index\x00" as *u8 as i64 126 roots[7] = ".\x00" as *u8 as i64 127 g_puts("=== LIBRARIAN FORMAT-SPRAWL AUDIT (real data roots) ===\n" as *u8) 128 var totalfiles: i64 = 0 129 i = 0 130 while i < 8 { 131 let rp: *u8 = roots[i] as *u8 132 let got: i64 = la_scan_dir(rp, rt) 133 g_puts(" root " as *u8) 134 g_puts(rp) 135 g_puts(": files=" as *u8) 136 if got < 0 { g_puts("absent" as *u8) } else { g_putn(got); totalfiles = totalfiles + got } 137 g_puts("\n" as *u8) 138 i = i + 1 139 } 140 prow("tsv \x00" as *u8, rt, 0) 141 prow("csv \x00" as *u8, rt, 1) 142 prow("json/jsonl \x00" as *u8, rt, 2) 143 prow("pipe-data \x00" as *u8, rt, 3) 144 prow("txt \x00" as *u8, rt, 4) 145 prow("raw \x00" as *u8, rt, 5) 146 prow("SEG-STORE \x00" as *u8, rt, 6) 147 prow("log \x00" as *u8, rt, 7) 148 prow("conf \x00" as *u8, rt, 8) 149 prow("other \x00" as *u8, rt, 9) 150 var rtot: i64 = 0 151 i = 0 152 while i < 10 { rtot = rtot + rt[i * 2 + 1]; i = i + 1 } 153 g_puts(" TOTAL files=" as *u8) 154 g_putn(totalfiles) 155 g_puts(" bytes=" as *u8) 156 g_putn(rtot) 157 g_puts("\n NATIVE seg-store share = " as *u8) 158 g_putn(la_permil(rt[13], rtot)) 159 g_puts(" permille of data bytes; tsv+csv legacy files = " as *u8) 160 g_putn(rt[0] + rt[2]) 161 g_puts("\n" as *u8) 162 var seen: i64 = 0 163 if totalfiles > 0 { seen = 1 } 164 chk("real audit sees files" as *u8, seen, 1, st) 165 var hastsv: i64 = 0 166 if rt[0] >= 1 { hastsv = 1 } 167 chk("legacy tsv measured (the migration target exists)" as *u8, hastsv, 1, st) 168 var haspipe: i64 = 0 169 if rt[6] >= 1 { haspipe = 1 } 170 chk("pipe-data measured (compare plane q/axes/matrix)" as *u8, haspipe, 1, st) 171 var hasseg: i64 = 0 172 if rt[12] >= 1 { hasseg = 1 } 173 chk("NATIVE seg-store present (the destination exists)" as *u8, hasseg, 1, st) 174 175 g_puts("nx_librarian_audit_gate: PASS=" as *u8) 176 g_putn(st[0]) 177 g_puts(" FAIL=" as *u8) 178 g_putn(st[1]) 179 g_puts("\n" as *u8) 180 if st[1] == 0 { g_puts("LIBRARIAN-R1 nx_librarian_audit: GREEN\n" as *u8); return 0 } 181 g_puts("LIBRARIAN-R1 nx_librarian_audit: RED\n" as *u8) 182 return 1 183}