code wiki / _hdl_build / nx_capability_shape_audit.nx

nx_capability_shape_audit.nx source

↩ module page · 125 lines · 8485 B

1// nx_capability_shape_audit.nx -- the CAPABILITIES AUDIT on the SIZING/frontier axis (operator: "we need a 2// capabilities audit"; distinct from the existing nx_capability_audit which classifies design-system 3// exceed-vs-sovereignty-floor). ONE sovereign walk grades EVERY organ on the perfect-object axis 4// (nx_capability_shape) + counts un-consolidated GENERATIONS (shadow collisions) = the full curation map: 5// TOO_SMALL (merge) / PERFECT (right-sized, the climbed-capability count) / TOO_BIG (split), + collisions to 6// resolve. Writes the metrics to knowledge/status/capability_audit.log so the no-regression safeguard ratchets 7// them. READ-ONLY walk (one reused buffer + sys_munmap; never-brick). license_tier: ORIGINAL expect_exit: 0 8import "nx_capability_shape.nx" // cs_funccount, cs_grade, CS_TOO_SMALL/PERFECT/TOO_BIG 9import "nx_janitor_dupname.nx" // jdn_scan, jdn_collisions 10import "nx_organ_callers.nx" // oc_getdents, oc_isdir, oc_join, oc_ends, oc_eq, oc_slen, oc_read 11import "nx_syscalls.nx" 12// Named for what they hold, not their value (were CSA_MAGIC_<value>). CSA_PATH_BYTES was not even a 13// const: it sat as a bare 768 in FOUR places -- the queue allocation, the per-slot stride, the child 14// scratch and the munmap -- so the stride and the allocation it must match were four independent 15// literals. ★★A SLOT SIZE THAT APPEARS BOTH IN AN ALLOCATION AND IN THE INDEX ARITHMETIC IS ONE FACT 16// WRITTEN TWICE; MOVE ONE AND THE WALK READS PAST ITS OWN QUEUE. 17const CSA_PATH_BYTES: i64 = 768 // one path slot in the BFS queue 18const CSA_QUEUE_MAX: i64 = 16384 // max directories queued 19const CSA_DIRENT_BYTES: i64 = 65536 // one getdents64 batch 20// ⚠THIS IS A GUESSED CEILING ON A FILE READ, WHICH RULE 11 NAMES AS A DEFECT GENERATOR IN BOTH 21// DIRECTIONS: too small truncates a source in silence, too large wastes address space, and raising it 22// only moves the guess. The estate's answer is sys_read_file, which sizes its buffer from the file via 23// lseek END and cannot short-read. Naming it is the bounded fix; composing sys_read_file here is the 24// real one, and it changes behaviour so it is NOT bundled into a rename. 25const CSA_FILE_BYTES: i64 = 4194304 // per-source read window -- see the caveat above 26const CSA_FILE_MAX: i64 = 32768 // max files recorded 27 28const CSA_LOG: *u8 = "knowledge/status/capability_audit.log" 29 30func aw(fd: i64, s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(fd,s,n); return 0 } 31func an(fd: i64, v: i64) -> i64 { let b: *u8=sys_mmap(28); var m: i64=v; if m<0{sys_write(fd,"-" as *u8,1);m=0-m} let t: *u8=sys_mmap(28); 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} var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1} sys_write(fd,b,k); return 0 } 32 33// walk `root`: grade every .nx by function-count; tally[0]=TOO_SMALL [1]=PERFECT [2]=TOO_BIG. returns graded count. 34func csa_walk(root: *u8, tally: *i64) -> i64 { 35 let qbuf: *u8=sys_mmap(CSA_PATH_BYTES*CSA_QUEUE_MAX); let gbuf: *u8=sys_mmap(CSA_DIRENT_BYTES); let stbuf: *u8=sys_mmap(256) 36 let fbuf: *u8=sys_mmap(CSA_FILE_BYTES); let child: *u8=sys_mmap(CSA_PATH_BYTES) 37 var qh: i64=0; var qt: i64=0 38 var ri: i64=0; while root[ri]!=(0 as u8){ qbuf[ri]=root[ri]; ri=ri+1 } qbuf[ri]=0 as u8; qt=1 39 var graded: i64=0 40 while qh<qt { 41 let dir: *u8=((qbuf as i64)+qh*768) as *u8; qh=qh+1 42 let fd: i64=sys_openat_rd(dir) 43 if fd<0 { } else { 44 var nread: i64=oc_getdents(fd, gbuf, CSA_DIRENT_BYTES) 45 while nread>0 { 46 var off: i64=0 47 while off<nread { 48 let reclen: i64=(gbuf[off+16] as i64)|((gbuf[off+17] as i64)<<8) 49 if reclen<=0 { off=nread } else { 50 let name: *u8=((gbuf as i64)+off+19) as *u8 51 var skip: i64=0 52 if oc_eq(name, "." as *u8)==1 { skip=1 } 53 if oc_eq(name, ".." as *u8)==1 { skip=1 } 54 if skip==0 { 55 oc_join(child, dir, name) 56 let isd: i64=oc_isdir(child, stbuf) 57 if isd==1 { 58 var vend: i64=0 59 if oc_eq(name, "node_modules" as *u8)==1 { vend=1 } 60 if oc_eq(name, ".git" as *u8)==1 { vend=1 } 61 if oc_eq(name, ".alelane" as *u8)==1 { vend=1 } 62 if vend==0 { if qt<CSA_QUEUE_MAX { let dst: *u8=((qbuf as i64)+qt*CSA_PATH_BYTES) as *u8; var c: i64=0; while child[c]!=(0 as u8){ dst[c]=child[c]; c=c+1 } dst[c]=0 as u8; qt=qt+1 } } 63 } else { if isd==0 { 64 let pl: i64=oc_slen(child) 65 if oc_ends(child, pl, ".nx" as *u8)==1 { 66 let fn: i64=oc_read(child, fbuf, CSA_FILE_BYTES) 67 // ★TRUNCATION ANNOUNCES INSTEAD OF MIS-GRADING. A source that fills the 68 // buffer may have been cut, so its function count is unreliable and it 69 // is counted in its OWN bucket rather than folded into the tally -- 70 // a truncated grade is not a small error, it is an UNKNOWN wearing the 71 // shape of an answer. Rule 11 permits a bound only where the size is 72 // unknowable AND the truncation announces; here the shared buffer is 73 // deliberate (this is a hot loop over ~18.7k files and allocating per 74 // file would leak address space), so ANNOUNCING is the half that was 75 // missing. 76 if fn>=CSA_FILE_BYTES { tally[3]=tally[3]+1 } 77 else { if fn>0 { let g: i64=cs_grade(cs_funccount(fbuf, fn)); tally[g]=tally[g]+1; graded=graded+1 } } 78 } 79 } } 80 } 81 off=off+reclen 82 } 83 } 84 nread=oc_getdents(fd, gbuf, CSA_DIRENT_BYTES) 85 } 86 sys_close(fd) 87 } 88 } 89 sys_munmap(qbuf, CSA_PATH_BYTES*CSA_QUEUE_MAX); sys_munmap(gbuf, CSA_DIRENT_BYTES); sys_munmap(fbuf, CSA_FILE_BYTES) 90 return graded 91} 92 93func main() -> i64 { 94 aw(1, "=== nx_capability_shape_audit: grade EVERY organ on the perfect-object axis (the curation map) ===\n" as *u8) 95 let tally: *i64=sys_mmap(64) as *i64; tally[0]=0; tally[1]=0; tally[2]=0; tally[3]=0 96 let graded: i64=csa_walk("runtime\x00" as *u8, tally) 97 // collisions = capabilities still split across generations 98 let fnames: *u8=sys_mmap(CSA_FILE_BYTES); let fnoff: *i64=sys_mmap(8*CSA_FILE_MAX) as *i64; let hc: *i64=sys_mmap(16) as *i64 99 let NF: i64=jdn_scan(fnames, fnoff, hc) 100 let coll: i64=jdn_collisions(fnames, fnoff, hc[0], NF) 101 102 aw(1, " graded=" as *u8); an(1, graded) 103 aw(1, " TOO_SMALL(merge)=" as *u8); an(1, tally[0]) 104 aw(1, " PERFECT(right-sized)=" as *u8); an(1, tally[1]) 105 aw(1, " TOO_BIG(split)=" as *u8); an(1, tally[2]) 106 aw(1, " shadow-collisions(generations to merge)=" as *u8); an(1, coll) 107 aw(1, " TRUNCATED(read hit the buffer, grade withheld)=" as *u8); an(1, tally[3]); aw(1, "\n" as *u8) 108 var permil: i64=0 109 if graded>0 { permil = (tally[1]*1000)/graded } 110 aw(1, " perfect-fraction permil=" as *u8); an(1, permil); aw(1, " (the climbed-capability metric the no-regression safeguard ratchets)\n" as *u8) 111 112 // durable log for the no-regression safeguard 113 let lfd: i64=sys_openat_append(CSA_LOG, MODE_0644) 114 if lfd>=0 { 115 aw(lfd, "CAP-AUDIT graded=" as *u8); an(lfd, graded) 116 aw(lfd, " too_small=" as *u8); an(lfd, tally[0]) 117 aw(lfd, " perfect=" as *u8); an(lfd, tally[1]) 118 aw(lfd, " too_big=" as *u8); an(lfd, tally[2]) 119 aw(lfd, " collisions=" as *u8); an(lfd, coll) 120 aw(lfd, " perfect_permil=" as *u8); an(lfd, permil); aw(lfd, "\n" as *u8) 121 sys_close(lfd) 122 } 123 if graded>0 { aw(1, "CAP-SHAPE-AUDIT=GREEN (full curation map written to capability_audit.log)\n" as *u8); sys_exit(0); return 0 } 124 aw(1, "CAP-SHAPE-AUDIT=RED (nothing graded)\n" as *u8); sys_exit(1); return 1 125}