code wiki / _hdl_build / nx_library_publish.nx

nx_library_publish.nx source

↩ module page · 163 lines · 9235 B

1// nx_library_publish.nx -- MLIB-020 (CALLOUT-012, operator 2026-06-13: publish to 2// nishifamily.com/wiki BEHIND the user login; see+work anywhere, synced). The AUTH-GATED 3// PUBLISH organ: serve the nishi-library dashboard ONLY to a valid OPAQUE-login session, 4// else 302 -> /wiki/login (data NEVER served unauthenticated -- the security property). 5// Composes PROVEN organs: nx_no_cookie_session (Ed25519 session mint/validate, the per-request 6// gate nx_modern_auth_validate_session is built on) + nx_web_builder (responsive render) + 7// a LIVE read of assignment_queue.tsv (build-status sync -- the page reflects current DONE rungs). 8// Self-validating gate (PUBLISHGATE): valid-session->served, forged/expired->DENIED, build-status 9// reflects live queue. Live socket serving rides nx_http_server; remote deploy = MLIB-022 10// (operator-triggered). license_tier: ORIGINAL 11import "nx_syscalls.nx" 12import "nx_itoa_lib.nx" // shared MSB-first emitter (zero-alloc) 13import "nx_ed25519_signature.nx" 14import "hub/nx_no_cookie_session.nx" 15import "nx_web_builder.nx" 16const LP_MAGIC_4000: i64 = 4000 17const LP_MAGIC_400000: i64 = 400000 18const LP_MAGIC_60000: i64 = 60000 19 20const LP_NOW: i64 = 1000000 21const LP_TTL: i64 = 3600 22const LP_OUT: *u8 = "knowledge/nishi-library-dashboard.html" 23const LP_QUEUE: *u8 = "knowledge/registry/assignment_queue.tsv" 24 25func lp_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 26// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer 27// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the 28// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls). 29// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign. 30func lp_putn(v: i64) -> i64 { nxi_out(v); return 0 } 31// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer 32// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the 33// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls). 34// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign. 35func lp_wfn(fd: i64, v: i64) -> i64 { nxi_fd(fd, v); return 0 } 36func lp_wbuf(dst: *u8, off: i64, s: *u8) -> i64 { var i: i64 = 0; while s[i] != (0 as u8) { dst[off+i] = s[i]; i = i + 1 } return off + i } 37 38func lp_readfile(path: *u8, buf: *u8, cap: i64) -> i64 { 39 let fd: i64 = sys_openat_rd(path) 40 if fd < 0 { return 0 - 1 } 41 var tot: i64 = 0 42 var r: i64 = 1 43 while r > 0 { let dst: *u8 = ((buf as i64) + tot) as *u8; r = sys_read(fd, dst, cap - tot); if r > 0 { tot = tot + r } } 44 sys_close(fd) 45 return tot 46} 47func lp_contains(buf: *u8, n: i64, needle: *u8) -> i64 { 48 var m: i64 = 0; while needle[m] != (0 as u8) { m = m + 1 } 49 if m == 0 { return 0 } 50 var i: i64 = 0 51 while i + m <= n { var j: i64 = 0; var ok: i64 = 1; while j < m { if buf[i + j] != needle[j] { ok = 0; j = m } else { j = j + 1 } } if ok == 1 { return 1 } i = i + 1 } 52 return 0 53} 54func lp_scan(buf: *u8, n: i64, start: i64, delim: i64) -> i64 { var i: i64 = start; var s: i64 = 1; while s == 1 { if i >= n { s = 0 } else { if buf[i] == (delim as u8) { s = 0 } else { i = i + 1 } } } return i } 55func lp_startswith(buf: *u8, a: i64, e: i64, pre: *u8) -> i64 { var i: i64 = 0; while pre[i] != (0 as u8) { if a + i >= e { return 0 } if buf[a + i] != pre[i] { return 0 } i = i + 1 } return 1 } 56func lp_rangehas(buf: *u8, a: i64, e: i64, needle: *u8) -> i64 { var m: i64 = 0; while needle[m] != (0 as u8) { m = m + 1 } if m == 0 { return 0 } var i: i64 = a; while i + m <= e { var j: i64 = 0; var ok: i64 = 1; while j < m { if buf[i + j] != needle[j] { ok = 0; j = m } else { j = j + 1 } } if ok == 1 { return 1 } i = i + 1 } return 0 } 57 58// count DONE rungs in the nishi-library families (live build-status from the real queue). 59func lp_count_done(buf: *u8, n: i64) -> i64 { 60 var c: i64 = 0 61 var p: i64 = 0 62 while p < n { 63 let a: i64 = p 64 let e: i64 = lp_scan(buf, n, a, 10) 65 var fam: i64 = 0 66 if lp_startswith(buf, a, e, "MLIB-" as *u8) == 1 { fam = 1 } 67 if lp_startswith(buf, a, e, "X-CENSUS-" as *u8) == 1 { fam = 1 } 68 if lp_startswith(buf, a, e, "X-CAP-001" as *u8) == 1 { fam = 1 } 69 if fam == 1 { if lp_rangehas(buf, a, e, "\tDONE\t" as *u8) == 1 { c = c + 1 } } 70 p = e + 1 71 } 72 return c 73} 74 75func lp_card(fd: i64, t: *u8, b: *u8) -> i64 { wb_card(fd, t, b); return 0 } 76 77func main() -> i64 { 78 // server keypair (test): derive pub from a fixed priv 79 let priv: *u8 = sys_mmap(32) 80 var i: i64 = 0; while i < 32 { priv[i] = (i + 1) as u8; i = i + 1 } 81 let pub: *u8 = sys_mmap(32) 82 ed25519_pub_from_priv(priv, pub) 83 // identity hashes (post-OPAQUE-login these come from the login; test fills) 84 let uid: *u8 = sys_mmap(32); i = 0; while i < 32 { uid[i] = 17 as u8; i = i + 1 } 85 let realm: *u8 = sys_mmap(32); i = 0; while i < 32 { realm[i] = 34 as u8; i = i + 1 } 86 87 let tok: *u8 = sys_mmap(152) 88 let mrc: i64 = nx_ncs_mint_token(priv, uid, realm, LP_NOW, LP_TTL, tok) 89 var mint_ok: i64 = 0; if mrc == 0 { mint_ok = 1 } 90 91 let v1: i64 = nx_ncs_validate_token(pub, tok, LP_NOW, 0 as *u8, 0 as *u8) // valid -> OK(0) 92 let forged: *u8 = sys_mmap(152); i = 0; while i < 152 { forged[i] = tok[i]; i = i + 1 } 93 forged[88] = ((forged[88] as i64) ^ 1) as u8 // flip a sig byte 94 let v2: i64 = nx_ncs_validate_token(pub, forged, LP_NOW, 0 as *u8, 0 as *u8) // forged -> !=0 95 let v3: i64 = nx_ncs_validate_token(pub, tok, LP_NOW + LP_MAGIC_4000, 0 as *u8, 0 as *u8) // expired -> !=0 96 97 var authed: i64 = 0; if v1 == 0 { authed = 1 } 98 99 // live build-status from the real queue (sync) 100 let qbuf: *u8 = sys_mmap(LP_MAGIC_400000) 101 let qn: i64 = lp_readfile(LP_QUEUE, qbuf, LP_MAGIC_400000) 102 let done: i64 = lp_count_done(qbuf, qn) 103 104 // render the authed dashboard ONLY when authed (the gate) 105 if authed == 1 { 106 let dfd: i64 = sys_openat_wr(LP_OUT, 420) 107 if dfd >= 0 { 108 wb_doc_open(dfd, "Nishi Library -- behind login" as *u8) 109 wb_header(dfd, "Nishi Library" as *u8, "Sign out" as *u8, "/wiki/logout" as *u8) 110 wb_w(dfd, "<section class=\"wrap\"><h2>Build status (live, synced from queue)</h2><div class=\"trust\">rungs DONE: " as *u8) 111 lp_wfn(dfd, done) 112 wb_w(dfd, " -- this view is BEHIND your wiki login.</div></section>\n" as *u8) 113 wb_cards_open(dfd, "Your library -- all media, one view" as *u8) 114 lp_card(dfd, "Video & TV" as *u8, "transcode + cast + scrub + resume" as *u8) 115 lp_card(dfd, "Photos & GIFs" as *u8, "perceptual dedup + EXIF + bulk triage" as *u8) 116 lp_card(dfd, "Audio" as *u8, "lossless gapless + smart playlists" as *u8) 117 lp_card(dfd, "Books & Manga" as *u8, "EPUB/PDF reader + TOC search" as *u8) 118 lp_card(dfd, "3D / STL" as *u8, "mesh viewer + dims + printability" as *u8) 119 lp_card(dfd, "Games" as *u8, "launcher + emulation + DRM-free ingest" as *u8) 120 wb_cards_close(dfd) 121 wb_footer(dfd, "Behind your wiki login. Synced. Sovereign." as *u8) 122 wb_doc_close(dfd) 123 sys_close(dfd) 124 } 125 } 126 127 // the unauth response (what a request with no/forged session gets) 128 let ubuf: *u8 = sys_mmap(256) 129 var uo: i64 = 0 130 uo = lp_wbuf(ubuf, uo, "HTTP/1.1 302 Found\r\nLocation: /wiki/login\r\n\r\n" as *u8) 131 132 // read the dashboard back to verify it was served with the live status 133 let dbuf: *u8 = sys_mmap(LP_MAGIC_60000) 134 let dn: i64 = lp_readfile(LP_OUT, dbuf, LP_MAGIC_60000) 135 let dash_brand: i64 = lp_contains(dbuf, dn, "Nishi Library" as *u8) 136 let dash_status: i64 = lp_contains(dbuf, dn, "rungs DONE" as *u8) 137 let dash_grid: i64 = lp_contains(dbuf, dn, "3D / STL" as *u8) 138 let unauth_302: i64 = lp_contains(ubuf, uo, "302 Found" as *u8) 139 let unauth_loc: i64 = lp_contains(ubuf, uo, "/wiki/login" as *u8) 140 141 lp_puts("PUBLISHGATE mint_ok=" as *u8); lp_putn(mint_ok) 142 lp_puts(" authed_served=" as *u8); lp_putn(authed) 143 lp_puts(" forged_denied=" as *u8); if v2 != 0 { lp_putn(1) } else { lp_putn(0) } 144 lp_puts(" expired_denied=" as *u8); if v3 != 0 { lp_putn(1) } else { lp_putn(0) } 145 lp_puts(" build_status_done=" as *u8); lp_putn(done) 146 lp_puts(" dash[brand=" as *u8); lp_putn(dash_brand); lp_puts(" status=" as *u8); lp_putn(dash_status); lp_puts(" grid=" as *u8); lp_putn(dash_grid) 147 lp_puts("] unauth[302=" as *u8); lp_putn(unauth_302); lp_puts(" login=" as *u8); lp_putn(unauth_loc); lp_puts("]" as *u8) 148 149 var ok: i64 = 1 150 if mint_ok != 1 { ok = 0 } 151 if v1 != 0 { ok = 0 } 152 if v2 == 0 { ok = 0 } 153 if v3 == 0 { ok = 0 } 154 if dash_brand != 1 { ok = 0 } 155 if dash_status != 1 { ok = 0 } 156 if dash_grid != 1 { ok = 0 } 157 if unauth_302 != 1 { ok = 0 } 158 if unauth_loc != 1 { ok = 0 } 159 if done < 8 { ok = 0 } 160 if ok == 1 { lp_puts(" verdict=GREEN\n" as *u8); return 0 } 161 lp_puts(" verdict=RED\n" as *u8) 162 return 1 163}