code wiki / _hdl_build / nx_library_sync.nx

nx_library_sync.nx source

↩ module page · 163 lines · 7901 B

1// nx_library_sync.nx -- MLIB-021 (publish-sync-on-beat, CALLOUT-012). The nishi-library 2// dashboard is REGENERATED on every beat / on DONE-rung change so the authed front-end always 3// reflects current state. Composes PROVEN organs (no rebuild, rule 15 DRY; MLIB-020 untouched, 4// rule 19): lp_count_done (nx_library_publish = the live build-status counter over the real queue) 5// + the nx_web_builder render. ls_sync_render is the BEAT-CALLABLE step (the autorun daemon calls 6// it each beat with the live DONE count). The SYNC contract is gate-proven (LIBSYNCGATE): 7// TRACKS -- queue with N DONE rungs -> dashboard shows "rungs DONE: N"; a +1 queue change 8// (N -> N+1) re-renders to "rungs DONE: N+1" (and the N render does NOT contain N+1). 9// LIVE -- the real queue's DONE count is what the live dashboard renders. 10// IDEMPOTENT -- same queue re-rendered twice = byte-identical HTML (no churn on a no-change beat). 11// Built sovereignly (nx_cc->nxasm_x86, no gcc). license_tier: ORIGINAL 12import "nx_library_publish.nx" 13import "nx_itoa_lib.nx" // shared MSB-first emitter (zero-alloc) 14const LS_MAGIC_131072: i64 = 131072 15const LS_MAGIC_8192: i64 = 8192 16const LS_MAGIC_400000: i64 = 400000 17 18const LS_QUEUE: *u8 = "knowledge/registry/assignment_queue.tsv" 19const LS_LIVE: *u8 = "knowledge/nishi-library-dashboard.html" 20const LS_LIVE2: *u8 = "knowledge/status/_libsync_live2.html" 21const LS_A: *u8 = "knowledge/status/_libsync_a.html" 22const LS_A2: *u8 = "knowledge/status/_libsync_a2.html" 23const LS_B: *u8 = "knowledge/status/_libsync_b.html" 24 25func ls_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 ls_putn(v: i64) -> i64 { nxi_out(v); return 0 } 31 32// build "rungs DONE: <v>\0" into out via the SHARED lp_wbuf; return length. 33func ls_needle(out: *u8, v: i64) -> i64 { 34 var o: i64 = lp_wbuf(out, 0, "rungs DONE: " as *u8) 35 var m: i64 = v; if m < 0 { m = 0 - m } 36 let t: *u8 = sys_mmap(28); var k: i64 = 0 37 if m == 0 { t[0] = 48 as u8; k = 1 } 38 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 39 var i: i64 = 0 40 while i < k { out[o] = t[k - 1 - i]; o = o + 1; i = i + 1 } 41 out[o] = 0 as u8 42 return o 43} 44 45// THE beat step: render the authed dashboard from a live DONE count (same shape MLIB-020 serves). 46func ls_sync_render(outpath: *u8, done: i64) -> i64 { 47 let dfd: i64 = sys_openat_wr(outpath, 420) 48 if dfd < 0 { return 0 - 1 } 49 wb_doc_open(dfd, "Nishi Library -- behind login" as *u8) 50 wb_header(dfd, "Nishi Library" as *u8, "Sign out" as *u8, "/wiki/logout" as *u8) 51 wb_w(dfd, "<section class=\"wrap\"><h2>Build status (live, synced from queue)</h2><div class=\"trust\">rungs DONE: " as *u8) 52 lp_wfn(dfd, done) 53 wb_w(dfd, " -- this view is BEHIND your wiki login.</div></section>\n" as *u8) 54 wb_cards_open(dfd, "Your library -- all media, one view" as *u8) 55 wb_card(dfd, "Video & TV" as *u8, "transcode + cast + scrub + resume" as *u8) 56 wb_card(dfd, "Photos & GIFs" as *u8, "perceptual dedup + EXIF + bulk triage" as *u8) 57 wb_card(dfd, "Audio" as *u8, "lossless gapless + smart playlists" as *u8) 58 wb_card(dfd, "Books & Manga" as *u8, "EPUB/PDF reader + TOC search" as *u8) 59 wb_card(dfd, "3D / STL" as *u8, "mesh viewer + dims + printability" as *u8) 60 wb_card(dfd, "Games" as *u8, "launcher + emulation + DRM-free ingest" as *u8) 61 wb_cards_close(dfd) 62 wb_footer(dfd, "Behind your wiki login. Synced. Sovereign." as *u8) 63 wb_doc_close(dfd) 64 sys_close(dfd) 65 return 0 66} 67 68// byte-identical file compare (idempotency proof). 1 equal / 0 differ. 69func ls_filecmp(pa: *u8, pb: *u8) -> i64 { 70 let ba: *u8 = sys_mmap(LS_MAGIC_131072) 71 let na: i64 = lp_readfile(pa, ba, LS_MAGIC_131072) 72 let bb: *u8 = sys_mmap(LS_MAGIC_131072) 73 let nb: i64 = lp_readfile(pb, bb, LS_MAGIC_131072) 74 if na <= 0 { return 0 } 75 if na != nb { return 0 } 76 var i: i64 = 0 77 while i < na { if ba[i] != bb[i] { return 0 } i = i + 1 } 78 return 1 79} 80 81// does the rendered dashboard at path contain "rungs DONE: <v>"? (TRACKS proof) 82func ls_render_has_count(path: *u8, v: i64) -> i64 { 83 let buf: *u8 = sys_mmap(LS_MAGIC_131072) 84 let n: i64 = lp_readfile(path, buf, LS_MAGIC_131072) 85 if n <= 0 { return 0 } 86 let need: *u8 = sys_mmap(64) 87 ls_needle(need, v) 88 return lp_contains(buf, n, need) 89} 90 91func ls_qline(dst: *u8, off: i64, id: *u8, status: *u8) -> i64 { 92 var o: i64 = lp_wbuf(dst, off, id) 93 o = lp_wbuf(dst, o, "\tMLIB\t9\tM\tBuilder\t" as *u8) 94 o = lp_wbuf(dst, o, status) 95 o = lp_wbuf(dst, o, "\t-\tg\tt\n" as *u8) 96 return o 97} 98 99func main() -> i64 { 100 // synthetic queue A: 3 DONE MLIB rungs + 1 TODO -> live counter sees 3 101 let qa: *u8 = sys_mmap(LS_MAGIC_8192) 102 var qan: i64 = 0 103 qan = ls_qline(qa, qan, "MLIB-101" as *u8, "DONE" as *u8) 104 qan = ls_qline(qa, qan, "MLIB-102" as *u8, "DONE" as *u8) 105 qan = ls_qline(qa, qan, "MLIB-103" as *u8, "DONE" as *u8) 106 qan = ls_qline(qa, qan, "MLIB-104" as *u8, "TODO" as *u8) 107 let count_a: i64 = lp_count_done(qa, qan) 108 109 // queue B = A with the TODO flipped DONE (the +1 queue change) -> counter sees 4 110 let qb: *u8 = sys_mmap(LS_MAGIC_8192) 111 var qbn: i64 = 0 112 qbn = ls_qline(qb, qbn, "MLIB-101" as *u8, "DONE" as *u8) 113 qbn = ls_qline(qb, qbn, "MLIB-102" as *u8, "DONE" as *u8) 114 qbn = ls_qline(qb, qbn, "MLIB-103" as *u8, "DONE" as *u8) 115 qbn = ls_qline(qb, qbn, "MLIB-104" as *u8, "DONE" as *u8) 116 let count_b: i64 = lp_count_done(qb, qbn) 117 118 // render A twice (idempotency) and B once (tracks the change) 119 ls_sync_render(LS_A, count_a) 120 ls_sync_render(LS_A2, count_a) 121 ls_sync_render(LS_B, count_b) 122 123 let tracks_a: i64 = ls_render_has_count(LS_A, count_a) 124 let tracks_b: i64 = ls_render_has_count(LS_B, count_b) 125 let a_not_b: i64 = 1 - ls_render_has_count(LS_A, count_b) // count-3 render must NOT show count-4 126 let idempotent: i64 = ls_filecmp(LS_A, LS_A2) 127 var changed: i64 = 0; if count_b != count_a { changed = 1 } 128 129 // LIVE: the real queue's DONE count IS what the live dashboard renders (the on-beat product). 130 let qbuf: *u8 = sys_mmap(LS_MAGIC_400000) 131 let qn: i64 = lp_readfile(LS_QUEUE, qbuf, LS_MAGIC_400000) 132 let count_live: i64 = lp_count_done(qbuf, qn) 133 ls_sync_render(LS_LIVE, count_live) 134 ls_sync_render(LS_LIVE2, count_live) 135 let live_match: i64 = ls_render_has_count(LS_LIVE, count_live) 136 let live_idempotent: i64 = ls_filecmp(LS_LIVE, LS_LIVE2) 137 138 ls_puts("LIBSYNCGATE count_a=" as *u8); ls_putn(count_a) 139 ls_puts(" count_b=" as *u8); ls_putn(count_b) 140 ls_puts(" changed=" as *u8); ls_putn(changed) 141 ls_puts(" tracks_a=" as *u8); ls_putn(tracks_a) 142 ls_puts(" tracks_b=" as *u8); ls_putn(tracks_b) 143 ls_puts(" a_not_b=" as *u8); ls_putn(a_not_b) 144 ls_puts(" idempotent=" as *u8); ls_putn(idempotent) 145 ls_puts(" live_done=" as *u8); ls_putn(count_live) 146 ls_puts(" live_match=" as *u8); ls_putn(live_match) 147 ls_puts(" live_idempotent=" as *u8); ls_putn(live_idempotent) 148 149 var ok: i64 = 1 150 if count_a != 3 { ok = 0 } 151 if count_b != 4 { ok = 0 } 152 if changed != 1 { ok = 0 } 153 if tracks_a != 1 { ok = 0 } 154 if tracks_b != 1 { ok = 0 } 155 if a_not_b != 1 { ok = 0 } 156 if idempotent != 1 { ok = 0 } 157 if live_match != 1 { ok = 0 } 158 if live_idempotent != 1 { ok = 0 } 159 if count_live < 8 { ok = 0 } 160 if ok == 1 { ls_puts(" verdict=GREEN\n" as *u8); return 0 } 161 ls_puts(" verdict=RED\n" as *u8) 162 return 1 163}