code wiki / _hdl_build / nx_debt_audit.nx

nx_debt_audit.nx source

↩ module page · 133 lines · 6471 B

1// nx_debt_audit.nx -- the ANTI-SILENT-DEBT auditor (operator: audit from the hardware 2// layer up as part of security/racing/triage -- catch hacking, working-around, silent 3// technical debt; S-class-exceed discipline everywhere). SOVEREIGN organ replacing the 4// borrowed bench/nx_debt_audit.sh (the audit retires its OWN shell debt = dogfood). 5// Two mechanical checks, hardware-up: 6// 1. LEDGER DISCIPLINE: every QUAL/QUANT row in NISHI_DEBT_LEDGER.tsv must carry a 7// tracked plan (a non-empty later column) -- a debt row with no plan = SILENT debt. 8// 2. MARKER SCAN: scan a source for the silent-debt signal words (HACK, WORKAROUND, 9// FIXME, XXX, "for now", TODO without a row ref) -- a marker is fine IF the file is 10// named in the ledger (tracked) -- an UNTRACKED marker = a workaround nobody logged. 11// A workaround is ALLOWED (landmines exist) -- a workaround that is NOT TRACKED is the 12// violation. Durable: DEBTAUDIT rows + verdict -> knowledge/status/debt_audit.log. 13// Exit 0 = no silent debt. argv[1]=ledger override argv[2]=source-to-scan (optional). 14// license_tier: ORIGINAL 15import "nx_syscalls.nx" 16const K_MAGIC_1048576: i64 = 1048576 17const K_MAGIC_1048560: i64 = 1048560 18const K_MAGIC_2097152: i64 = 2097152 19const K_MAGIC_2097136: i64 = 2097136 20func _p(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 21func _fp(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 } 22func _fn(fd: i64, v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{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{bb[i]=t[k-1-i];i=i+1}; sys_write(fd,bb,k); return 0 } 23func da_len(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 24func da_read(path: *u8, buf: *u8, cap: i64) -> i64 { 25 let fd: i64 = sys_openat_rd(path) 26 if fd < 0 { return 0 - 1 } 27 var n: i64 = 0 28 var go: i64 = 1 29 while go == 1 { let base: i64 = buf as i64; let r: i64 = sys_read(fd, (base + n) as *u8, cap - n); if r <= 0 { go = 0 } else { n = n + r } if n >= cap { go = 0 } } 30 sys_close(fd) 31 return n 32} 33func da_slice_has(hay: *u8, a: i64, b: i64, pat: *u8) -> i64 { 34 let pl: i64 = da_len(pat) 35 if pl == 0 { return 0 } 36 var i: i64 = a 37 while i + pl <= b { 38 var k: i64 = 0 39 var hit: i64 = 1 40 while k < pl { if hay[i+k] != pat[k] { hit = 0; k = pl } else { k = k + 1 } } 41 if hit == 1 { return 1 } 42 i = i + 1 43 } 44 return 0 45} 46// count QUAL/QUANT data rows whose plan column (last tab field) is empty -> silent debt 47func da_ledger(buf: *u8, n: i64, lfd: i64) -> i64 { 48 var silent: i64 = 0 49 var rows: i64 = 0 50 var i: i64 = 0 51 while i < n { 52 var le: i64 = i 53 var s: i64 = 1 54 while s == 1 { if le >= n { s = 0 } else { if buf[le] == (10 as u8) { s = 0 } else { le = le + 1 } } } 55 if buf[i] != (35 as u8) { // not a comment 56 // a QUAL or QUANT row? 57 var isdebt: i64 = 0 58 if da_slice_has(buf, i, le, "QUAL" as *u8) == 1 { isdebt = 1 } 59 if da_slice_has(buf, i, le, "QUANT" as *u8) == 1 { isdebt = 1 } 60 if isdebt == 1 { 61 rows = rows + 1 62 // last field after the final TAB -- if empty/whitespace = no plan 63 var lastt: i64 = i 64 var p: i64 = i 65 while p < le { if buf[p] == (9 as u8) { lastt = p + 1 } p = p + 1 } 66 var nonblank: i64 = 0 67 var q: i64 = lastt 68 while q < le { if buf[q] != (32 as u8) { nonblank = 1; q = le } else { q = q + 1 } } 69 if nonblank == 0 { 70 silent = silent + 1 71 _fp(lfd, "DEBTAUDIT silent-ledger-row line-start-byte=" as *u8); _fn(lfd, i); _fp(lfd, "\n" as *u8) 72 } 73 } 74 } 75 i = le + 1 76 } 77 _fp(lfd, "DEBTAUDIT ledger_debt_rows=" as *u8); _fn(lfd, rows) 78 _fp(lfd, " silent_no_plan=" as *u8); _fn(lfd, silent); _fp(lfd, "\n" as *u8) 79 return silent 80} 81// scan a source for silent-debt MARKERS; return count (caller decides tracked vs not) 82func da_markers(buf: *u8, n: i64, name: *u8, lfd: i64) -> i64 { 83 var hits: i64 = 0 84 hits = hits + 0 85 let mk: **u8 = sys_mmap(8 * 8) as **u8 86 mk[0] = "HACK" as *u8 87 mk[1] = "WORKAROUND" as *u8 88 mk[2] = "FIXME" as *u8 89 mk[3] = "XXX" as *u8 90 mk[4] = "for now" as *u8 91 var mi: i64 = 0 92 while mi < 5 { 93 if da_slice_has(buf, 0, n, mk[mi]) == 1 { 94 hits = hits + 1 95 _fp(lfd, "DEBTAUDIT marker=" as *u8); _fp(lfd, mk[mi]) 96 _fp(lfd, " in=" as *u8); _fp(lfd, name); _fp(lfd, "\n" as *u8) 97 } 98 mi = mi + 1 99 } 100 return hits 101} 102func main(argc: i64, argv: *i64) -> i64 { 103 var ledp: *u8 = "NISHI_DEBT_LEDGER.tsv" as *u8 104 if argc >= 2 { ledp = argv[1] as *u8 } 105 _p("=== DEBT AUDIT: no silent debt, no untracked workaround (hardware-up discipline) ===\n" as *u8) 106 let lfd: i64 = sys_openat_append("knowledge/status/debt_audit.log" as *u8, 0x1a4) 107 if lfd < 0 { _p(" debt_audit log open failed\n" as *u8); sys_exit(1); return 1 } 108 let lb: *u8 = sys_mmap(K_MAGIC_1048576) 109 let ln: i64 = da_read(ledp, lb, K_MAGIC_1048560) 110 var silent: i64 = 0 111 if ln < 0 { 112 // a MISSING ledger is itself silent debt -- fail loud 113 _fp(lfd, "DEBTAUDIT ledger-MISSING (untracked debt by definition)\n" as *u8) 114 silent = silent + 1 115 } else { 116 silent = silent + da_ledger(lb, ln, lfd) 117 } 118 // optional source marker scan (the per-source triage leg) 119 if argc >= 3 { 120 let sb: *u8 = sys_mmap(K_MAGIC_2097152) 121 let sn: i64 = da_read(argv[2] as *u8, sb, K_MAGIC_2097136) 122 if sn > 0 { da_markers(sb, sn, argv[2] as *u8, lfd) } 123 } 124 _fp(lfd, "DEBTAUDIT epoch=" as *u8); _fn(lfd, sys_now_realtime_sec()) 125 _fp(lfd, " silent_debt=" as *u8); _fn(lfd, silent) 126 if silent == 0 { _fp(lfd, " verdict=GREEN\n" as *u8) } else { _fp(lfd, " verdict=RED\n" as *u8) } 127 sys_close(lfd) 128 _p(" silent_debt=" as *u8); _fn(1, silent) 129 if silent == 0 { _p(" -- DEBT AUDIT: GREEN (every debt tracked, no silent workaround)\n" as *u8); sys_exit(0); return 0 } 130 _p(" -- DEBT AUDIT: RED (silent debt found -- track it or fix it, never hide it)\n" as *u8) 131 sys_exit(1) 132 return 1 133}