code wiki / _hdl_build / nx_paper_gen.nx

nx_paper_gen.nx source

↩ module page · 266 lines · 12648 B

1// nx_paper_gen.nx -- the team PUBLISHES research papers on what improvements are improving 2// what (RESEARCH-PAPER doctrine + COMPREHENSIBILITY law, operator 2026-06-11). X-RP-001. 3// PLAIN LANGUAGE: a paper here is not prose someone typed -- it is COMPILED from the team's 4// own ledgers, so a human researcher reads WHY a capability exists (lineage: genetics + 5// epigenetics), WHAT improved (anchored gate/meter lines), and exactly where every number 6// came from. If a named source line is MISSING the organ REFUSES to emit -- a paper can 7// never contain an unledgered claim (ATTRIBUTION law: cite or it is an observation). 8// conf row: P <id> <slug> <lineage-child> <log1> <anchor1> <log2> <anchor2> <log3> <anchor3> 9// paper = claim (the lineage WHY) + genetics/mutations/epigenetics + the LAST line of 10// each log holding its anchor + limitations + generated-by stamp 11// output = <outdir>/<id>-<slug>.md (tmp + sys_renameat hot-swap: no torn reads) 12// durable = PAPERGEN rows -> knowledge/status/paper_gen.log; exit 0 iff every conf paper EMITTED 13// argv[1]=papers-conf argv[2]=lineage.tsv argv[3]=outdir (gates use scratch) 14// license_tier: ORIGINAL 15import "nx_syscalls.nx" 16const K_MAGIC_65536: i64 = 65536 17const K_MAGIC_65520: i64 = 65520 18const K_MAGIC_262144: i64 = 262144 19const K_MAGIC_262128: i64 = 262128 20const K_MAGIC_16384: i64 = 16384 21const K_MAGIC_1048592: i64 = 1048592 22const K_MAGIC_786432: i64 = 786432 23const K_MAGIC_1048576: i64 = 1048576 24func _p(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 25func _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 } 26func _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 } 27func pg_len(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 28func pg_cat(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 } 29func pg_read(path: *u8, buf: *u8, cap: i64) -> i64 { 30 let fd: i64 = sys_openat_rd(path) 31 if fd < 0 { return 0 } 32 var n: i64 = 0 33 var go: i64 = 1 34 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 } } 35 sys_close(fd) 36 return n 37} 38func pg_slice_contains(hay: *u8, a: i64, b: i64, pat: *u8) -> i64 { 39 let pl: i64 = pg_len(pat) 40 if pl == 0 { return 0 } 41 var i: i64 = a 42 while i + pl <= b { 43 var k: i64 = 0 44 var hit: i64 = 1 45 while k < pl { if hay[i+k] != pat[k] { hit = 0; k = pl } else { k = k + 1 } } 46 if hit == 1 { return 1 } 47 i = i + 1 48 } 49 return 0 50} 51func pg_last_line_with(hay: *u8, n: i64, pat: *u8, out: *i64) -> i64 { 52 var found: i64 = 0 53 var i: i64 = 0 54 while i < n { 55 var le: i64 = i 56 var scan: i64 = 1 57 while scan == 1 { 58 if le >= n { scan = 0 } 59 else { if hay[le] == (10 as u8) { scan = 0 } else { le = le + 1 } } 60 } 61 if pg_slice_contains(hay, i, le, pat) == 1 { out[0] = i; out[1] = le; found = 1 } 62 i = le + 1 63 } 64 return found 65} 66func pg_token(conf: *u8, pos: i64, le: i64, pool: *u8, poff: *i64) -> i64 { 67 var t: i64 = pos 68 var s: i64 = 1 69 while s == 1 { 70 if t >= le { s = 0 } 71 else { if conf[t] == (32 as u8) { s = 0 } else { pool[poff[0]] = conf[t]; poff[0] = poff[0] + 1; t = t + 1 } } 72 } 73 pool[poff[0]] = 0 as u8; poff[0] = poff[0] + 1 74 return t 75} 76// find the lineage line starting child+TAB; cols[c*2],cols[c*2+1] = [start,end) of 6 TAB columns 77func pg_lineage_cols(lin: *u8, ln: i64, child: *u8, cols: *i64) -> i64 { 78 let cl: i64 = pg_len(child) 79 var i: i64 = 0 80 while i < ln { 81 var le: i64 = i 82 var s: i64 = 1 83 while s == 1 { if le >= ln { s = 0 } else { if lin[le] == (10 as u8) { s = 0 } else { le = le + 1 } } } 84 if i + cl < le { 85 var k: i64 = 0 86 var hit: i64 = 1 87 while k < cl { if lin[i+k] != child[k] { hit = 0; k = cl } else { k = k + 1 } } 88 if hit == 1 { 89 if lin[i+cl] == (9 as u8) { 90 var c: i64 = 0 91 var p: i64 = i 92 while c < 6 { 93 var q: i64 = p 94 var s2: i64 = 1 95 while s2 == 1 { if q >= le { s2 = 0 } else { if lin[q] == (9 as u8) { s2 = 0 } else { q = q + 1 } } } 96 cols[c*2] = p 97 cols[c*2+1] = q 98 p = q + 1 99 c = c + 1 100 } 101 return 1 102 } 103 } 104 } 105 i = le + 1 106 } 107 return 0 108} 109func pg_sec(fd: i64, label: *u8, buf: *u8, a: i64, b: i64) -> i64 { 110 _fp(fd, "- " as *u8) 111 _fp(fd, label) 112 _fp(fd, ": " as *u8) 113 let base: i64 = buf as i64 114 sys_write(fd, (base + a) as *u8, b - a) 115 _fp(fd, "\n" as *u8) 116 return 0 117} 118// the paper head: title + claim + the lineage genetics block 119func pg_emit_head(fd: i64, id: *u8, slug: *u8, child: *u8, lin: *u8, cols: *i64) -> i64 { 120 _fp(fd, "# " as *u8) 121 _fp(fd, id) 122 _fp(fd, " -- " as *u8) 123 _fp(fd, slug) 124 _fp(fd, "\n\n## Claim (the lineage WHY -- what this improvement is for)\n" as *u8) 125 pg_sec(fd, "why" as *u8, lin, cols[10], cols[11]) 126 _fp(fd, "\n## Lineage (genetics + epigenetics -- why this is comprehensible)\n" as *u8) 127 _fp(fd, "subject organ: " as *u8) 128 _fp(fd, child) 129 _fp(fd, "\n" as *u8) 130 pg_sec(fd, "parents (genetics)" as *u8, lin, cols[2], cols[3]) 131 pg_sec(fd, "inherited genes" as *u8, lin, cols[4], cols[5]) 132 pg_sec(fd, "mutations (what is new + the defect that selected it)" as *u8, lin, cols[6], cols[7]) 133 pg_sec(fd, "epigenetic surfaces (data changes behavior, zero source change)" as *u8, lin, cols[8], cols[9]) 134 return 0 135} 136func pg_emit_tail(fd: i64) -> i64 { 137 _fp(fd, "\n## Limitations (named, honest)\n" as *u8) 138 _fp(fd, "- evidence lines are each log's LAST anchored verdict at emission time, not a time series\n" as *u8) 139 _fp(fd, "- attribution below organ level (per-word effect tables, COMP-007) is a pending rung: X-RP-003\n" as *u8) 140 _fp(fd, "- a live-network gate row can fail for environmental reasons; the Doctor row tracks root cause\n" as *u8) 141 _fp(fd, "\n---\ngenerated-by: nx_paper_gen (sovereign organ) epoch=" as *u8) 142 _fn(fd, sys_now_realtime_sec()) 143 _fp(fd, "\nevery line above is ledger-derived; this organ REFUSES to emit when a named source line is missing\n" as *u8) 144 return 0 145} 146func main(argc: i64, argv: *i64) -> i64 { 147 var confp: *u8 = "knowledge/registry/papers.conf" as *u8 148 var linp: *u8 = "knowledge/registry/lineage.tsv" as *u8 149 var outd: *u8 = "knowledge/papers" as *u8 150 if argc >= 2 { confp = argv[1] as *u8 } 151 if argc >= 3 { linp = argv[2] as *u8 } 152 if argc >= 4 { outd = argv[3] as *u8 } 153 _p("=== PAPER GEN: research papers COMPILED from the team ledgers (refuse > fabricate) ===\n" as *u8) 154 let conf: *u8 = sys_mmap(K_MAGIC_65536) 155 let cn: i64 = pg_read(confp, conf, K_MAGIC_65520) 156 if cn <= 0 { _p(" papers conf MISSING -- fail loud\n" as *u8); sys_exit(1); return 1 } 157 let lin: *u8 = sys_mmap(K_MAGIC_262144) 158 let ln: i64 = pg_read(linp, lin, K_MAGIC_262128) 159 if ln <= 0 { _p(" lineage MISSING -- fail loud\n" as *u8); sys_exit(1); return 1 } 160 let lfd: i64 = sys_openat_append("knowledge/status/paper_gen.log" as *u8, 0x1a4) 161 if lfd < 0 { _p(" paper log open failed\n" as *u8); sys_exit(1); return 1 } 162 let pool: *u8 = sys_mmap(K_MAGIC_16384) 163 let poffb: *i64 = sys_mmap(16) as *i64 164 poffb[0] = 0 165 let lb: *u8 = sys_mmap(K_MAGIC_1048592) 166 let span: *i64 = sys_mmap(16) as *i64 167 let cols: *i64 = sys_mmap(96) as *i64 168 let pathb: *u8 = sys_mmap(512) 169 let tmpb: *u8 = sys_mmap(512) 170 var npapers: i64 = 0 171 var nemit: i64 = 0 172 var ci: i64 = 0 173 while ci < cn { 174 var le: i64 = ci 175 var scan: i64 = 1 176 while scan == 1 { 177 if le >= cn { scan = 0 } 178 else { if conf[le] == (10 as u8) { scan = 0 } else { le = le + 1 } } 179 } 180 if conf[ci] == (80 as u8) { 181 let id: i64 = (pool as i64) + poffb[0] 182 var t: i64 = pg_token(conf, ci + 2, le, pool, poffb) 183 let slug: i64 = (pool as i64) + poffb[0] 184 t = pg_token(conf, t + 1, le, pool, poffb) 185 let child: i64 = (pool as i64) + poffb[0] 186 t = pg_token(conf, t + 1, le, pool, poffb) 187 npapers = npapers + 1 188 var ok: i64 = 1 189 if pg_lineage_cols(lin, ln, child as *u8, cols) == 0 { ok = 0 } 190 // three evidence pairs: remember [start,end) of each log's last anchored line 191 let ev: *i64 = sys_mmap(96) as *i64 192 let evbuf: *u8 = sys_mmap(K_MAGIC_786432) 193 var evn: i64 = 0 194 var pi: i64 = 0 195 while pi < 3 { 196 let lg: i64 = (pool as i64) + poffb[0] 197 t = pg_token(conf, t + 1, le, pool, poffb) 198 let anch: i64 = (pool as i64) + poffb[0] 199 t = pg_token(conf, t + 1, le, pool, poffb) 200 let gn: i64 = pg_read(lg as *u8, lb, K_MAGIC_1048576) 201 var got: i64 = 0 202 if gn > 0 { 203 if pg_last_line_with(lb, gn, anch as *u8, span) == 1 { 204 // copy the line into evbuf (lb is reused next pair) 205 var w: i64 = span[0] 206 var eo: i64 = evn 207 while w < span[1] { evbuf[eo] = lb[w]; eo = eo + 1; w = w + 1 } 208 ev[pi*4] = evn 209 ev[pi*4+1] = eo 210 ev[pi*4+2] = anch 211 evn = eo 212 got = 1 213 } 214 } 215 if got == 0 { ok = 0 } 216 pi = pi + 1 217 } 218 _fp(lfd, "PAPERGEN id=" as *u8); _fp(lfd, id as *u8) 219 if ok == 1 { 220 var o: i64 = pg_cat(pathb, 0, outd) 221 o = pg_cat(pathb, o, "/" as *u8) 222 o = pg_cat(pathb, o, id as *u8) 223 o = pg_cat(pathb, o, "-" as *u8) 224 o = pg_cat(pathb, o, slug as *u8) 225 o = pg_cat(pathb, o, ".md" as *u8) 226 pathb[o] = 0 as u8 227 var o2: i64 = pg_cat(tmpb, 0, outd) 228 o2 = pg_cat(tmpb, o2, "/.tmp_paper.md" as *u8) 229 tmpb[o2] = 0 as u8 230 let pfd: i64 = sys_openat_wr(tmpb, 0x1a4) 231 if pfd < 0 { ok = 0 } else { 232 pg_emit_head(pfd, id as *u8, slug as *u8, child as *u8, lin, cols) 233 _fp(pfd, "\n## Evidence (each line = its ledger's LAST anchored verdict)\n" as *u8) 234 var ei: i64 = 0 235 while ei < 3 { 236 pg_sec(pfd, ev[ei*4+2] as *u8, evbuf, ev[ei*4], ev[ei*4+1]) 237 ei = ei + 1 238 } 239 pg_emit_tail(pfd) 240 sys_close(pfd) 241 sys_renameat(tmpb, pathb) 242 _fp(lfd, " sources=4/4 verdict=EMITTED path=" as *u8) 243 _fp(lfd, pathb) 244 _fp(lfd, "\n" as *u8) 245 nemit = nemit + 1 246 _p(" " as *u8); _p(id as *u8); _p(": EMITTED\n" as *u8) 247 } 248 } 249 if ok == 0 { 250 _fp(lfd, " verdict=REFUSED (a named source line is missing -- refuse beats fabricate)\n" as *u8) 251 _p(" " as *u8); _p(id as *u8); _p(": REFUSED (missing source)\n" as *u8) 252 } 253 } 254 ci = le + 1 255 } 256 _fp(lfd, "PAPERGEN-SUM epoch=" as *u8); _fn(lfd, sys_now_realtime_sec()) 257 _fp(lfd, " papers=" as *u8); _fn(lfd, npapers) 258 _fp(lfd, " emitted=" as *u8); _fn(lfd, nemit) 259 if nemit == npapers { _fp(lfd, " verdict=GREEN\n" as *u8) } else { _fp(lfd, " verdict=RED\n" as *u8) } 260 sys_close(lfd) 261 _p(" durable: knowledge/status/paper_gen.log + papers in " as *u8); _p(outd); _p("\n" as *u8) 262 if nemit == npapers { _p(" PAPER GEN: GREEN (every conf paper emitted, all ledger-derived)\n" as *u8); sys_exit(0); return 0 } 263 _p(" PAPER GEN: RED (a paper refused -- its named source is the work)\n" as *u8) 264 sys_exit(1) 265 return 1 266}