code wiki / _hdl_build / nx_reader_liveness.nx
nx_reader_liveness.nx source
↩ module page · 97 lines · 6716 B
1// nx_reader_liveness.nx -- ACTIVE BUILT-vs-LIVE auditor for the sovereign Library reader.
2// The static nx_reader_census records what is BUILT; this organ PROBES whether each format the library
3// actually CONTAINS is also WIRED into the serving binary -> it catches the BUILT-not-LIVE class the static
4// census structurally misses (mobi/azw/azw3 decoders existed + were gated GREEN, but ms_handle_open never
5// routed to them, so ~390 books dead-ended at /file with an alert). It reads REAL artifacts only -- zero
6// assertions: the library index (format presence), _offc/<extractor>.elf (BUILT), and the serving binary
7// _offc/nx_media_server.elf (WIRED = the binary actually references the extractor). A format the library
8// holds whose built extractor is NOT wired = a GAP (the actionable feedback). Anti-wave: a row that CLAIMS
9// live with no wiring evidence in the binary is REFUSED (liar-kill). main() is the self-validating gate.
10// expect_exit: 0 license_tier: ORIGINAL
11import "nx_syscalls.nx"
12
13func lw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
14func ln2(v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m;sys_write(1,"-" as *u8,1)} 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(1,bb,k); return 0 }
15func lslen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
16
17// count non-overlapping occurrences of needle in hay[0..hl)
18func count_sub(hay: *u8, hl: i64, needle: *u8) -> i64 {
19 let nl: i64 = lslen(needle)
20 if nl == 0 { return 0 }
21 var c: i64 = 0
22 var i: i64 = 0
23 while i + nl <= hl {
24 var k: i64 = 0
25 var hit: i64 = 1
26 while k < nl { if hay[i+k] != needle[k] { hit = 0; k = nl } else { k = k + 1 } }
27 if hit == 1 { c = c + 1; i = i + nl } else { i = i + 1 }
28 }
29 return c
30}
31func has_sub(hay: *u8, hl: i64, needle: *u8) -> i64 { if count_sub(hay, hl, needle) > 0 { return 1 } return 0 }
32func file_exists(path: *u8) -> i64 { let fd: i64 = sys_openat_rd(path); if fd >= 0 { sys_close(fd); return 1 } return 0 }
33
34// audit one format. prints a row, returns 1 if it is a BUILT-not-LIVE GAP (content>0 && built && !wired).
35func audit_row(label: *u8, idxtoken: *u8, elfpath: *u8, srvtoken: *u8, idxb: *u8, idxn: i64, srvb: *u8, srvn: i64, tally: *i64) -> i64 {
36 let cnt: i64 = count_sub(idxb, idxn, idxtoken)
37 let built: i64 = file_exists(elfpath)
38 let wired: i64 = has_sub(srvb, srvn, srvtoken)
39 var status: *u8 = "no-content" as *u8
40 var gap: i64 = 0
41 if cnt > 0 {
42 if built == 0 { status = "NO-EXTRACTOR(built=0)" as *u8 }
43 else {
44 if wired == 0 { status = "BUILT-NOT-LIVE *GAP*" as *u8; gap = 1 }
45 else { status = "LIVE" as *u8 }
46 }
47 }
48 lw(" ["); lw(status); lw("] "); lw(label)
49 lw(" index_hits="); ln2(cnt); lw(" built="); ln2(built); lw(" wired="); ln2(wired); lw("\n")
50 if gap == 1 { tally[0] = tally[0] + 1 }
51 return gap
52}
53
54func main() -> i64 {
55 lw("=== nx_reader_liveness: ACTIVE BUILT-vs-LIVE audit of the Library reader (probes real artifacts, zero egress) ===\n" as *u8)
56 let il: *i64 = sys_mmap(16) as *i64
57 let idxb: *u8 = sys_read_file("knowledge/staging/media/idx_book.wsl" as *u8, il)
58 var idxn: i64 = 0
59 if (idxb as i64) != 0 { idxn = il[0] }
60 let sl: *i64 = sys_mmap(16) as *i64
61 let srvb: *u8 = sys_read_file("_offc/nx_media_server.elf" as *u8, sl)
62 var srvn: i64 = 0
63 if (srvb as i64) != 0 { srvn = sl[0] }
64 lw(" library_index_bytes="); ln2(idxn); lw(" serving_binary_bytes="); ln2(srvn); lw("\n" as *u8)
65 if idxn == 0 { lw(" FATAL: library index unreadable -> cannot audit\n" as *u8); sys_exit(2); return 2 }
66 if srvn == 0 { lw(" FATAL: serving binary unreadable -> cannot audit\n" as *u8); sys_exit(2); return 2 }
67
68 lw("-- per-format liveness: a format the library CONTAINS must be BUILT (extractor .elf) AND WIRED (server references it) to be LIVE --\n" as *u8)
69 let gaps: *i64 = sys_mmap(8) as *i64; gaps[0] = 0
70 audit_row("EPUB (reflow text)" as *u8, ".epub" as *u8, "_offc/nx_epub_book.elf" as *u8, "nx_epub_book.elf" as *u8, idxb, idxn, srvb, srvn, gaps)
71 audit_row("PDF (pages/reflow)" as *u8, ".pdf" as *u8, "_offc/nx_cbx_pages.elf" as *u8, "nx_cbx_pages.elf" as *u8, idxb, idxn, srvb, srvn, gaps)
72 audit_row("CBZ (comic pages)" as *u8, ".cbz" as *u8, "_offc/nx_cbx_pages.elf" as *u8, "nx_cbx_pages.elf" as *u8, idxb, idxn, srvb, srvn, gaps)
73 audit_row("MOBI (Kindle MOBI6)" as *u8, ".mobi" as *u8, "_offc/nx_mobi_book.elf" as *u8, "nx_mobi_book.elf" as *u8, idxb, idxn, srvb, srvn, gaps)
74 audit_row("AZW3 (Kindle KF8)" as *u8, ".azw3" as *u8, "_offc/nx_kf8_book.elf" as *u8, "nx_kf8_book.elf" as *u8, idxb, idxn, srvb, srvn, gaps)
75 audit_row("AZW (Kindle MOBI/KF8)" as *u8, ".azw" as *u8, "_offc/nx_kf8_book.elf" as *u8, "nx_kf8_book.elf" as *u8, idxb, idxn, srvb, srvn, gaps)
76
77 lw("-- liar-kill (anti-wave guard): a format with content + a built extractor but a token the server does NOT contain MUST flag a GAP --\n" as *u8)
78 let lt: *i64 = sys_mmap(8) as *i64; lt[0] = 0
79 let liar_gap: i64 = audit_row("LIAR claims-live-via-absent-token" as *u8, ".epub" as *u8, "_offc/nx_epub_book.elf" as *u8, "nx_DEADBEEF_book.elf" as *u8, idxb, idxn, srvb, srvn, lt)
80 var ok: i64 = 1
81 if liar_gap != 1 { ok = 0 }
82
83 lw("---------------- SCORECARD (each axis stands on a real-artifact probe; no rollup) ----------------\n" as *u8)
84 lw(" BUILT-not-LIVE gaps="); ln2(gaps[0]); lw("\n" as *u8)
85 if gaps[0] != 0 { ok = 0 }
86
87 let lf: i64 = sys_openat_append("knowledge/status/reader_liveness.log" as *u8, 420)
88 if lf >= 0 {
89 sys_write(lf, "READER-LIVENESS gaps=" as *u8, lslen("READER-LIVENESS gaps=" as *u8))
90 let nb: *u8 = sys_mmap(28); var m: i64=gaps[0]; let tt: *u8=sys_mmap(28); var kk: i64=0; if m==0{tt[0]=48 as u8;kk=1} while m>0{tt[kk]=(48+(m%10)) as u8;m=m/10;kk=kk+1} var ii: i64=0; while ii<kk{nb[ii]=tt[kk-1-ii];ii=ii+1} sys_write(lf, nb, kk)
91 if ok==1 { sys_write(lf, " verdict=GREEN liarkill=fired\n" as *u8, lslen(" verdict=GREEN liarkill=fired\n" as *u8)) } else { sys_write(lf, " verdict=RED\n" as *u8, lslen(" verdict=RED\n" as *u8)) }
92 sys_close(lf)
93 }
94
95 if ok == 1 { lw("READER-LIVENESS verdict=GREEN (every format the library holds is BUILT and WIRED into the serving binary; anti-wave guard fired)\n" as *u8); sys_exit(0); return 0 }
96 lw("READER-LIVENESS verdict=RED (a BUILT-not-LIVE gap remains -> wire the extractor into ms_handle_open and rebuild)\n" as *u8); sys_exit(1); return 1
97}