code wiki / _hdl_build / nx_favela_census.nx

nx_favela_census.nx source

↩ module page · 191 lines · 9418 B

1// nx_favela_census.nx -- MEASURED ricketiness/favela index of the ecosystem (operator 2026-07-20: "no hacks or 2// workarounds or other bullshit -- measure the ricketiness and favela of our ecosystem"). Data-driven per rule 11: 3// the workaround-class roster lives in knowledge/registry/favela_classes.tsv (class/kind/sev/status/debt/evidence/ 4// root_fix), NOT in code. The organ MEASURES what is live-measurable (dup-basename count from dup_source.log, 5// .bak residue sprawl in the cwd root, wired deploy-guard rows) and computes: favela_permil = open-weight share 6// (open=2*sev, mitigated=sev, rooted=0 over a 2*sev-per-class total) and rooted_permil = retired share. HONEST BY 7// CONSTRUCTION: missing manifest = RED exit 1 (fail-closed); a missing evidence log measures -1 = UNMEASURED, 8// never a silent 0; the surveyed roster is DECLARED as a curated envelope, not claimed exhaustive. 9// Emits JSON to stdout AND colocates it to knowledge/status/favela_census.log with a VERDICT line (evidence layer). 10// NATIVE INFORMATION MANAGEMENT (2026-07-20): the class roster is read from the sovereign SEG-STORE PLANE 11// knowledge/store/favela- (NOT a .tsv) per the standing "no tsv -- nishi formats" law. 12// license_tier: ORIGINAL expect_exit: 0 13import "nx_store_seed_lib.nx" 14import "nx_seg_store.nx" 15import "nx_syscalls.nx" 16import "nx_estate_path.nx" // ep_anchor: the CWD must not decide this organ's verdict 17import "nx_sovjson_lib.nx" 18const FV_MAGIC_131072: i64 = 131072 19const FV_MAGIC_4096: i64 = 4096 20const FV_MAGIC_65536: i64 = 65536 21 22const FV_PLANE_CAP: i64 = 1048576 23 24func fv_puts(s: *u8) -> i64 { return sj_puts(s) } 25func fv_cat(o: *u8, at: i64, s: *u8) -> i64 { return sj_cat(o, at, s) } 26func fv_catf(o: *u8, at: i64, p: *u8, n: i64) -> i64 { var i: i64=0; var a: i64=at; while i<n { o[a]=p[i]; a=a+1; i=i+1 } return a } 27func fv_catn(o: *u8, at: i64, v: i64) -> i64 { return sj_catn(o, at, v) } 28// count occurrences of zero-terminated needle in buf[0..n) 29func fv_count(buf: *u8, n: i64, needle: *u8) -> i64 { 30 var nl: i64=0; while needle[nl]!=(0 as u8){nl=nl+1} 31 if nl==0 { return 0 } 32 var c: i64=0 33 var i: i64=0 34 while i+nl<=n { 35 var j: i64=0 36 var ok: i64=1 37 while j<nl { if buf[i+j]!=needle[j] { ok=0; j=nl } else { j=j+1 } } 38 if ok==1 { c=c+1; i=i+nl } else { i=i+1 } 39 } 40 return c 41} 42// does zero-terminated name contain zero-terminated sub 43func fv_has(name: *u8, sub: *u8) -> i64 { 44 var n: i64=0; while name[n]!=(0 as u8){n=n+1} 45 return fv_count(name, n, sub) 46} 47// count entries in dir "." whose name contains ".bak" 48func fv_bak_count() -> i64 { 49 let fd: i64=sys_openat_rd(".\x00" as *u8) 50 if fd<0 { return 0-1 } 51 let dbuf: *u8=sys_mmap(FV_MAGIC_131072) 52 var c: i64=0 53 var go: i64=1 54 while go==1 { 55 let nr: i64=sys_getdents64(fd, dbuf, FV_MAGIC_131072) 56 if nr<=0 { go=0 } else { 57 var off: i64=0 58 while off<nr { 59 let rec: *u8=(dbuf as i64+off) as *u8 60 let nm: *u8=dirent_name(rec) 61 if fv_has(nm, ".bak\x00" as *u8)>0 { c=c+1 } 62 off=off+dirent_reclen(rec) 63 } 64 } 65 } 66 sys_close(fd) 67 return c 68} 69// count non-comment non-empty lines in a file; -1 if unreadable 70func fv_data_lines(path: *u8) -> i64 { 71 let szp: *i64=sys_mmap(16) as *i64 72 let b: *u8=sys_read_file(path, szp) 73 if (b as i64)==0 { return 0-1 } 74 let n: i64=szp[0] 75 var c: i64=0 76 var j: i64=0 77 var atstart: i64=1 78 while j<n { 79 if atstart==1 { if b[j]!=(35 as u8) { if b[j]!=(10 as u8) { c=c+1 } } } 80 if b[j]==(10 as u8) { atstart=1 } else { atstart=0 } 81 j=j+1 82 } 83 return c 84} 85 86func main() -> i64 { 87 // ANCHOR FIRST (2026-08-04): run from buildroot this organ read an EMPTY/absent estate 88 // and fail-closed RED (or, worse, appended to a SECOND store). Its subject is the 89 // estate, so the caller's working directory must not decide its verdict. 90 ep_anchor() 91 let szp: *i64=sys_mmap(16) as *i64 92 let mb: *u8=sys_mmap(FV_PLANE_CAP) 93 let mn: i64=sts_load("knowledge/store/favela-\x00" as *u8, mb, FV_PLANE_CAP-FV_MAGIC_4096) 94 if mn<=0 { 95 fv_puts("FAVELA RED -- native plane knowledge/store/favela- ABSENT or EMPTY (fail-closed, no index without data)\n" as *u8) 96 sys_exit(1) 97 return 1 98 } 99 let jb: *u8=sys_mmap(FV_MAGIC_65536) 100 var j: i64=0 101 j=fv_cat(jb, j, "{\x22v\x22:1,\x22domain\x22:\x22favela\x22,\x22title\x22:\x22Ecosystem ricketiness/favela census -- workaround classes, measured\x22,\x22classes\x22:[" as *u8) 102 // parse manifest rows 103 let fs: *i64=sys_mmap(64) as *i64 104 let fl: *i64=sys_mmap(64) as *i64 105 var total: i64=0; var wsum: i64=0; var rsum: i64=0 106 var nopen: i64=0; var nmit: i64=0; var nroot: i64=0; var nclass: i64=0 107 var i: i64=0 108 var first: i64=1 109 while i<mn { 110 let ls: i64=i 111 var le: i64=ls 112 var scan: i64=1 113 while scan==1 { if le>=mn { scan=0 } else { if mb[le]==(10 as u8) { scan=0 } else { le=le+1 } } } 114 let lend: i64=le 115 i=le+1 116 let llen: i64=lend-ls 117 if llen>3 { if mb[ls]!=(35 as u8) { 118 // split into 7 tab fields 119 var nf: i64=0 120 var p: i64=ls 121 var fstart: i64=ls 122 while p<lend { 123 if mb[p]==(9 as u8) { 124 if nf<7 { fs[nf]=fstart; fl[nf]=p-fstart; nf=nf+1 } 125 fstart=p+1 126 } 127 p=p+1 128 } 129 if nf<7 { fs[nf]=fstart; fl[nf]=lend-fstart; nf=nf+1 } 130 if nf==7 { 131 let sev: i64=(mb[fs[2]] as i64)-48 132 let stc: i64=mb[fs[3]] as i64 133 var w: i64=0 134 if stc==111 { w=2*sev; nopen=nopen+1 } 135 if stc==109 { w=sev; nmit=nmit+1 } 136 if stc==114 { w=0; nroot=nroot+1; rsum=rsum+2*sev } 137 total=total+2*sev 138 wsum=wsum+w 139 nclass=nclass+1 140 if first==0 { j=fv_cat(jb, j, "," as *u8) } 141 first=0 142 j=fv_cat(jb, j, "{\x22class\x22:\x22" as *u8); j=fv_catf(jb, j, (mb as i64+fs[0]) as *u8, fl[0]) 143 j=fv_cat(jb, j, "\x22,\x22kind\x22:\x22" as *u8); j=fv_catf(jb, j, (mb as i64+fs[1]) as *u8, fl[1]) 144 j=fv_cat(jb, j, "\x22,\x22sev\x22:" as *u8); j=fv_catn(jb, j, sev) 145 j=fv_cat(jb, j, ",\x22status\x22:\x22" as *u8); j=fv_catf(jb, j, (mb as i64+fs[3]) as *u8, fl[3]) 146 j=fv_cat(jb, j, "\x22,\x22debt\x22:\x22" as *u8); j=fv_catf(jb, j, (mb as i64+fs[4]) as *u8, fl[4]) 147 j=fv_cat(jb, j, "\x22,\x22evidence\x22:\x22" as *u8); j=fv_catf(jb, j, (mb as i64+fs[5]) as *u8, fl[5]) 148 j=fv_cat(jb, j, "\x22,\x22root_fix\x22:\x22" as *u8); j=fv_catf(jb, j, (mb as i64+fs[6]) as *u8, fl[6]) 149 j=fv_cat(jb, j, "\x22}" as *u8) 150 } 151 } } 152 } 153 // live measurements (UNMEASURED = -1, never a silent 0) 154 var dupc: i64=0-1 155 let dl: *u8=sys_read_file("knowledge/status/dup_source.log\x00" as *u8, szp) 156 if (dl as i64)!=0 { dupc=fv_count(dl, szp[0], "DUP basename:\x00" as *u8) } 157 let bakc: i64=fv_bak_count() 158 let guards: i64=fv_data_lines("knowledge/registry/deploy_checks.tsv\x00" as *u8) 159 var favela: i64=0 160 var rooted: i64=0 161 if total>0 { favela=(wsum*1000)/total; rooted=(rsum*1000)/total } 162 j=fv_cat(jb, j, "],\x22measured\x22:{\x22dup_basenames\x22:" as *u8); j=fv_catn(jb, j, dupc) 163 j=fv_cat(jb, j, ",\x22bak_residue_files\x22:" as *u8); j=fv_catn(jb, j, bakc) 164 j=fv_cat(jb, j, ",\x22deploy_guards_wired\x22:" as *u8); j=fv_catn(jb, j, guards) 165 j=fv_cat(jb, j, ",\x22unmeasured_is_minus1\x22:1}" as *u8) 166 j=fv_cat(jb, j, ",\x22favela_permil\x22:" as *u8); j=fv_catn(jb, j, favela) 167 j=fv_cat(jb, j, ",\x22rooted_permil\x22:" as *u8); j=fv_catn(jb, j, rooted) 168 j=fv_cat(jb, j, ",\x22open\x22:" as *u8); j=fv_catn(jb, j, nopen) 169 j=fv_cat(jb, j, ",\x22mitigated\x22:" as *u8); j=fv_catn(jb, j, nmit) 170 j=fv_cat(jb, j, ",\x22rooted_classes\x22:" as *u8); j=fv_catn(jb, j, nroot) 171 j=fv_cat(jb, j, ",\x22formula\x22:\x22favela_permil = share of open shanty weight (open=2*sev, mitigated=sev, rooted=0) over 2*sev per surveyed class; 0 = fully engineered, 1000 = all shanty\x22" as *u8) 172 j=fv_cat(jb, j, ",\x22envelope\x22:\x22surveyed classes = the curated manifest (grows by named-class appends, NOT exhaustive); bak scan = cwd top level only; dup/guards read the colocated evidence files; -1 = evidence absent = UNMEASURED\x22" as *u8) 173 j=fv_cat(jb, j, ",\x22honest\x22:\x22a LOW favela_permil only means the NAMED classes are getting rooted -- unnamed shanty stays invisible until a row names it; naming a new workaround class RAISES the index (that is the system working)\x22}" as *u8) 174 sys_write(1, jb, j) 175 sys_write(1, "\n" as *u8, 1) 176 // colocate to the evidence layer 177 let lf: i64=sys_openat_wr("knowledge/status/favela_census.log\x00" as *u8, 0x1a4) 178 if lf>=0 { 179 sys_write(lf, jb, j) 180 let tail: *u8=sys_mmap(128) 181 var t: i64=0 182 t=fv_cat(tail, t, "\nVERDICT=GREEN classes=" as *u8) 183 t=fv_catn(tail, t, nclass) 184 t=fv_cat(tail, t, " favela=" as *u8) 185 t=fv_catn(tail, t, favela) 186 t=fv_cat(tail, t, "\n" as *u8) 187 sys_write(lf, tail, t) 188 sys_close(lf) 189 } 190 return 0 191}