code wiki / _hdl_build / nx_planecap_gate.nx

nx_planecap_gate.nx source

↩ module page · 118 lines · 6044 B

1// nx_planecap_gate.nx -- WHICH PLANES HAVE OUTGROWN THE 1 MiB READER CAP? A verifier, not a fixer. 2// 3// ★WHY THIS EXISTS. Three organs in one session presented a TRUNCATED SCAN AS THEIR VERDICT because a 4// compile-time cap was outgrown by the corpus: nx_debt_hygiene (1 MiB -> hid 21 of 23 sev-9 debts), 5// nx_dbthyg (same bug in the superset organ), nx_tooldiff (512 rows -> INVENTED a capability regression). 6// A grep finds **71** organs carrying `_CAP: i64 = 1048576`. Bulk-editing 71 files blind would be reckless 7// and most of those caps are legitimately bounded (a per-page or per-document scratch buffer is not a 8// hazard). ★THE ACTIONABLE QUESTION IS NOT "who has a cap" BUT "whose CORPUS HAS PASSED IT" -- so this 9// measures the planes and names the ones that are live hazards TODAY. 10// 11// ★IT DOES NOT EDIT ANYTHING. A tool that auto-raised 71 caps would be a mass rewrite justified by a 12// pattern rather than by measurement, which is the same reasoning error in the other direction. This 13// produces the list; a human raises the caps that matter, with the plane size as the evidence. 14// 15// METHOD: every `<prefix>-manifest.txt` under knowledge/store/ names a live plane. Load each with a 32 MiB 16// cap and report its true byte size. Anything at or above 1 MiB is flagged: any reader still sized at 17// 1048576 will silently truncate ITS verdict on that plane. 18// 19// usage: nx_planecap_gate (CWD = the store root) 20// exit 0 = no plane has outgrown the 1 MiB reader cap · 1 = at least one has (list printed) 21// license_tier: ORIGINAL expect_exit: 0 22import "nx_syscalls.nx" 23import "nx_store_seed_lib.nx" 24 25const PC_READ: i64 = 33554432 26const PC_DANGER: i64 = 1048576 27const PC_WARN: i64 = 786432 // 75% of the cap -- outgrowing it soon is worth knowing before it bites 28 29func w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 30func wn(v: i64) -> i64 { let t: *u8=sys_mmap(28); var m: i64=v; var k: i64=0; if m==0{t[0]=48 as u8;k=1} while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1} let b: *u8=sys_mmap(28); var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1} sys_write(1,b,k); return 0 } 31 32func main() -> i64 { 33 w("=== nx_planecap_gate -- planes measured against the 1 MiB reader cap (71 organs carry it) ===\n") 34 35 let dbuf: *u8 = sys_mmap(1 << 16) 36 let fd: i64 = sys_openat_rd("knowledge/store\x00" as *u8) 37 if fd < 0 { w(" cannot open knowledge/store\n"); sys_exit(2); return 2 } 38 39 let buf: *u8 = sys_mmap(PC_READ) 40 let f: *i64 = sys_mmap(64) as *i64 41 let prefix: *u8 = sys_mmap(512) 42 var planes: i64 = 0 43 var danger: i64 = 0 44 var warn: i64 = 0 45 var truncated: i64 = 0 46 47 var go: i64 = 1 48 while go == 1 { 49 let nr: i64 = sys_getdents64(fd, dbuf, 1 << 16) 50 if nr <= 0 { go = 0 } else { 51 var pos: i64 = 0 52 while pos < nr { 53 let rec: *u8 = ((dbuf as i64) + pos) as *u8 54 let reclen: i64 = dirent_reclen(rec) 55 let name: *u8 = ((rec as i64) + 19) as *u8 56 var ln: i64 = 0 57 while name[ln] != (0 as u8) { ln = ln + 1 } 58 // a live plane is exactly `<prefix>-manifest.txt` -- archives and .bak copies are not planes 59 var ism: i64 = 0 60 if ln > 13 { 61 let sfx: *u8 = "-manifest.txt\x00" as *u8 62 var m: i64 = 1 63 var q: i64 = 0 64 while q < 13 { if name[ln-13+q] != sfx[q] { m = 0; q = 13 } else { q = q + 1 } } 65 ism = m 66 } 67 if ism == 1 { 68 var o: i64 = 0 69 let pre: *u8 = "knowledge/store/" 70 var c: i64 = 0 71 while pre[c] != (0 as u8) { prefix[o] = pre[c]; o = o + 1; c = c + 1 } 72 var k: i64 = 0 73 while k < ln - 12 { prefix[o] = name[k]; o = o + 1; k = k + 1 } 74 prefix[o] = 0 as u8 75 let n: i64 = sts_load_honest(prefix, buf, PC_READ, f) 76 if n > 0 { 77 planes = planes + 1 78 var inbuf: i64 = 0 79 var b2: i64 = 0 80 while b2 < n { if buf[b2] == (10 as u8) { inbuf = inbuf + 1 } b2 = b2 + 1 } 81 // if even THIS 32 MiB read truncated, say so -- the auditor must not commit the 82 // very sin it is auditing for. 83 var mytrunc: i64 = 0 84 if inbuf != f[1] { mytrunc = 1; truncated = truncated + 1 } 85 if n >= PC_DANGER { 86 danger = danger + 1 87 w(" OVER-CAP "); w(prefix) 88 w(" bytes="); wn(n); w(" rows="); wn(f[1]) 89 if mytrunc == 1 { w(" [MY OWN READ ALSO TRUNCATED -- raise PC_READ]") } 90 w("\n") 91 } else { 92 if n >= PC_WARN { 93 warn = warn + 1 94 w(" NEAR-CAP "); w(prefix); w(" bytes="); wn(n); w(" rows="); wn(f[1]); w("\n") 95 } 96 } 97 } 98 } 99 if reclen <= 0 { pos = nr } else { pos = pos + reclen } 100 } 101 } 102 } 103 sys_close(fd) 104 105 w(" planes="); wn(planes) 106 w(" over_1MiB="); wn(danger) 107 w(" near_1MiB="); wn(warn) 108 w(" my_read_truncated="); wn(truncated); w("\n") 109 if danger == 0 { 110 w("VERDICT: verdict=GREEN (no plane has outgrown the 1 MiB reader cap)\n") 111 sys_exit(0); return 0 112 } 113 w(" ANY reader still sized at 1048576 will SILENTLY TRUNCATE ITS VERDICT on the plane(s) above.\n") 114 w(" This tool does not edit: raise those readers' caps deliberately, with the byte count as evidence.\n") 115 w("VERDICT: verdict=RED ("); wn(danger); w(" plane(s) past the 1 MiB reader cap)\n") 116 sys_exit(1) 117 return 1 118}