code wiki / (root) / nx_docpub_pulse.nx

nx_docpub_pulse.nx source

↩ module page · 117 lines · 6723 B

1// nx_docpub_pulse.nx -- R3 autonomy CORE: the CONTENT-CHANGE GATE for "publish docs as we work". The memory 2// flagged version-spam (every pulse re-stages even with no real change) as THE blocker to arming an autonomous 3// re-publish loop. This organ computes a deterministic content fingerprint over the generated doc SITE and 4// decides STAGE (content changed / first run) vs SKIP (unchanged) -- so an autonomous pulse only re-stages real 5// changes. The actual generate (nx_docpub_site) + stage (nx_docpub_publish) run only on a STAGE decision. 6// Self-contained (only nx_syscalls; poly rolling hash, no crypto import). Gated with version-spam teeth. 7// license_tier: ORIGINAL expect_exit: 0 8import "nx_syscalls.nx" 9import "nx_itoa_lib.nx" // shared MSB-first emitter (zero-alloc) 10const K_MAGIC_1048576: i64 = 1048576 11const K_MAGIC_1469598103: i64 = 1469598103 12 13func pls_w(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 14// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer 15// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the 16// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls). 17// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign. 18func pls_n(v: i64) -> i64 { nxi_out(v); return 0 } 19 20// deterministic poly rolling hash; 48-bit mask keeps it positive + overflow-free. folds one file (or a missing 21// sentinel) + its length into the running hash h. 22func pls_hash_file(path: *u8, h: i64) -> i64 { 23 let cap: i64 = K_MAGIC_1048576 24 let buf: *u8 = sys_mmap(cap) 25 let fd: i64 = sys_openat_rd(path) 26 if fd < 0 { return (h * 131 + 7) & 0x0000ffffffffffff } 27 let n: i64 = sys_read(fd, buf, cap); sys_close(fd) 28 var hh: i64 = h; var i: i64 = 0 29 while i < n { hh = (hh * 131 + (buf[i] & 0xff)) & 0x0000ffffffffffff; i = i + 1 } 30 hh = (hh * 131 + n) & 0x0000ffffffffffff 31 return hh 32} 33func pls_atoi(buf: *u8, n: i64) -> i64 { 34 var v: i64 = 0; var i: i64 = 0 35 while i < n { let c: i64 = buf[i] & 0xff; if c >= 48 { if c <= 57 { v = v * 10 + (c - 48); i = i + 1 } else { i = n } } else { i = n } } 36 return v 37} 38func pls_read_fp(path: *u8) -> i64 { 39 let buf: *u8 = sys_mmap(64) 40 let fd: i64 = sys_openat_rd(path); if fd < 0 { return 0 - 999 } 41 let n: i64 = sys_read(fd, buf, 64); sys_close(fd) 42 if n <= 0 { return 0 - 999 } 43 return pls_atoi(buf, n) 44} 45func pls_write_fp(path: *u8, v: i64) -> i64 { 46 let tmp: *u8 = sys_mmap(32); var m: i64 = v; var t: i64 = 0 47 while m > 0 { tmp[t] = (48 + (m % 10)) as u8; m = m / 10; t = t + 1 } 48 if t == 0 { tmp[0] = 48 as u8; t = 1 } 49 let buf: *u8 = sys_mmap(32); var i: i64 = 0; while i < t { buf[i] = tmp[t-1-i]; i = i + 1 } 50 let fd: i64 = sys_openat_wr(path, 0x1a4); if fd < 0 { return 0 - 1 } 51 sys_write(fd, buf, t); sys_close(fd); return t 52} 53func pls_trunc(path: *u8) -> i64 { let fd: i64 = sys_openat_wr(path, 0x1a4); if fd >= 0 { sys_close(fd) } return 0 } 54func pls_wr(path: *u8, content: *u8) -> i64 { 55 let fd: i64 = sys_openat_wr(path, 0x1a4); if fd < 0 { return 0 - 1 } 56 var n: i64 = 0; while content[n] != (0 as u8) { n = n + 1 } 57 sys_write(fd, content, n); sys_close(fd); return n 58} 59// 1=STAGE (changed/first), 0=SKIP (unchanged). Updates the stored fp on STAGE. 60func pls_decide(fp: *u8, h: i64) -> i64 { 61 let stored: i64 = pls_read_fp(fp) 62 if stored != h { pls_write_fp(fp, h); return 1 } 63 return 0 64} 65func pls_decide_test(fp: *u8, a: *u8, b: *u8) -> i64 { 66 var h: i64 = K_MAGIC_1469598103 67 h = pls_hash_file(a, h); h = pls_hash_file(b, h) 68 return pls_decide(fp, h) 69} 70func pls_decide_site(fp: *u8) -> i64 { 71 var h: i64 = K_MAGIC_1469598103 72 h = pls_hash_file("web_assets/docgen/index.html" as *u8, h) 73 h = pls_hash_file("web_assets/docgen/nx_docgen.html" as *u8, h) 74 h = pls_hash_file("web_assets/docgen/nx_docgen_lib.html" as *u8, h) 75 h = pls_hash_file("web_assets/docgen/nx_docpub_census.html" as *u8, h) 76 h = pls_hash_file("web_assets/docgen/nx_docpub_research_fetch.html" as *u8, h) 77 h = pls_hash_file("web_assets/docgen/nx_docpub_publish.html" as *u8, h) 78 h = pls_hash_file("web_assets/docgen/nx_publisher.html" as *u8, h) 79 return pls_decide(fp, h) 80} 81 82func main() -> i64 { 83 let pass: *i64 = sys_mmap(16) as *i64; pass[0] = 0 84 pls_w("=== NX-DOCPUB-PULSE -- content-change gate for autonomous re-stage (version-spam killer) ===\n" as *u8) 85 86 // hermetic teeth on /tmp fixtures: fresh->STAGE, unchanged->SKIP, changed->STAGE, unchanged->SKIP 87 let fp: *u8 = "/tmp/dp_pulse.fp" 88 let a: *u8 = "/tmp/dp_pulse_a" 89 let b: *u8 = "/tmp/dp_pulse_b" 90 pls_trunc(fp) 91 pls_wr(a, "alpha page bytes" as *u8) 92 pls_wr(b, "beta page bytes" as *u8) 93 let d1: i64 = pls_decide_test(fp, a, b) 94 let d2: i64 = pls_decide_test(fp, a, b) 95 pls_wr(b, "beta page bytes -- EDITED" as *u8) 96 let d3: i64 = pls_decide_test(fp, a, b) 97 let d4: i64 = pls_decide_test(fp, a, b) 98 pls_w(" decisions: fresh="); pls_n(d1); pls_w(" rerun="); pls_n(d2); pls_w(" afteredit="); pls_n(d3); pls_w(" rerun2="); pls_n(d4); pls_w(" (expect 1 0 1 0)\n") 99 100 let t1: i64 = (d1 == 1) as i64 101 if t1 == 1 { pls_w(" T1 first run -> STAGE: PASS\n" as *u8); pass[0] = pass[0] + 1 } else { pls_w(" T1 first run -> STAGE: FAIL\n" as *u8) } 102 let t2: i64 = (d2 == 0) as i64 103 if t2 == 1 { pls_w(" T2 unchanged -> SKIP (version-spam killed): PASS\n" as *u8); pass[0] = pass[0] + 1 } else { pls_w(" T2 unchanged -> SKIP: FAIL\n" as *u8) } 104 let t3: i64 = (d3 == 1) as i64 105 if t3 == 1 { pls_w(" T3 content changed -> STAGE: PASS\n" as *u8); pass[0] = pass[0] + 1 } else { pls_w(" T3 content changed -> STAGE: FAIL\n" as *u8) } 106 let t4: i64 = (d4 == 0) as i64 107 if t4 == 1 { pls_w(" T4 re-stabilizes -> SKIP: PASS\n" as *u8); pass[0] = pass[0] + 1 } else { pls_w(" T4 re-stabilizes -> SKIP: FAIL\n" as *u8) } 108 109 // REAL: decide over the actual generated site (initializes/updates knowledge/publish/docpub_site.fp) 110 __syscall(83, "knowledge/publish" as i64, 493, 0, 0, 0, 0) 111 let rd: i64 = pls_decide_site("knowledge/publish/docpub_site.fp" as *u8) 112 pls_w(" REAL site decision: "); if rd == 1 { pls_w("STAGE (changed / first run -> run nx_docpub_site + nx_docpub_publish)\n" as *u8) } else { pls_w("SKIP (unchanged -> no re-stage)\n" as *u8) } 113 114 pls_w("NX-DOCPUB-PULSE "); pls_n(pass[0]); pls_w("/4 ") 115 if pass[0] == 4 { pls_w("verdict=GREEN (content-change gate proven; autonomous loop re-stages ONLY real changes)\n" as *u8); sys_exit(0); return 0 } 116 pls_w("verdict=RED (change-gate teeth failed)\n" as *u8); sys_exit(1); return 1 117}