code wiki / _hdl_build / nx_lineage_complete.nx

nx_lineage_complete.nx source

↩ module page · 251 lines · 12160 B

1// nx_lineage_complete.nx -- the GENEALOGIST's RECORD-HINTS + RE-PARENT organ (genealogist spec 2// item 6: "proactively suggest a node's missing parent link"; the root-trace SIL's PROPOSER leg). 3// 4// nx_root_trace measures which organs FAIL to trace to the spore. This organ CLOSES those gaps 5// the only sovereign-honest way: every .sov.elf is begotten by the build engine nx_sov_build_run 6// (BOOTSTRAP_MAP rung7), so that build-parent edge is the universal true ancestry. It: 7// 1. loads the children + parent-references already in lineage.tsv, 8// 2. finds MISSING-PARENT organs (referenced as a parent, organ-like, no '.', not yet a child) 9// and LOCATES each on disk (runtime/_hdl_build, runtime, runtime/bin) -- a parent that is NOT 10// a real file is NOT authored (never invent a node; report it NOTFOUND), 11// 3. finds FLOATING-ROOTS (parents "-" but not the spore) and RE-PARENTS them to nx_sov_build_run, 12// 4. writes lineage.tsv.bak, then atomically rewrites lineage.tsv: existing rows verbatim (floaters 13// re-parented) + one record-hint row per located missing-parent. 14// Each authored row asserts ONLY the true build-parent edge and marks rich genetics "pending" -- a 15// record-hint, never a fabricated lineage. The independent PROOF is nx_root_trace (a wrong edge 16// can't make it trace). Idempotent (rerun: no floaters, no missing-parents -> byte-identical). 17// Additive in spirit + reversible (.bak), per the nx_epic_rollup mutate precedent. license_tier: ORIGINAL 18import "nx_syscalls.nx" 19const LC_MAGIC_1048576: i64 = 1048576 20const LC_MAGIC_1048575: i64 = 1048575 21 22const LC_LIN: *u8 = "knowledge/registry/lineage.tsv" 23const LC_BAK: *u8 = "knowledge/registry/lineage.tsv.bak" 24const LC_TMP: *u8 = "knowledge/registry/.nx_lineage_complete.tmp" // SIBLING of lineage.tsv: renameat must stay on one filesystem (cross-device /tmp->/mnt/c = EXDEV) 25const LC_LOG: *u8 = "knowledge/status/lineage_complete.log" 26const LC_SPORE: *u8 = "rv64im_min_sim" 27const LC_WOMB: *u8 = "nx_sov_build_run" 28const LC_IDCAP: i64 = 48 29const LC_MAXC: i64 = 900 30 31func lc_w(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 } 32func lc_wn(fd: i64, v: i64) -> i64 { let bb: *u8 = sys_mmap(28); var m: i64=v; if m<0 {m=0-m; sys_write(fd,"-" 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(fd, bb, k); return 0 } 33func lc_wrange(fd: i64, buf: *u8, a: i64, b: i64) -> i64 { sys_write(fd, (buf as i64 + a) as *u8, b - a); return 0 } 34 35func lc_streq(a: *u8, b: *u8) -> i64 { 36 var i: i64 = 0 37 while i < 64 { if a[i] != b[i] { return 0 } if a[i] == (0 as u8) { return 1 } i = i + 1 } 38 return 1 39} 40func lc_in_set(arena: i64, cnt: i64, idcap: i64, name: *u8) -> i64 { 41 var i: i64 = 0 42 while i < cnt { if lc_streq((arena + i * idcap) as *u8, name) == 1 { return 1 } i = i + 1 } 43 return 0 44} 45func lc_has_dot(s: *u8) -> i64 { var i: i64 = 0; while s[i] != (0 as u8) { if s[i] == (46 as u8) { return 1 } i = i + 1 } return 0 } 46 47// organ-like token: starts 'n' (nx_*) or '_' (gate organs), is not "-", and has no '.' (data file). 48func lc_is_organ(t: *u8) -> i64 { 49 if t[0] == (45 as u8) { return 0 } 50 if lc_has_dot(t) == 1 { return 0 } 51 if t[0] == (110 as u8) { return 1 } // 'n' 52 if t[0] == (95 as u8) { return 1 } // '_' 53 return 0 54} 55 56func lc_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 } 57func lc_try(dir: *u8, name: *u8) -> i64 { 58 let p: *u8 = sys_mmap(256) 59 var o: i64 = 0 60 o = lc_cat(p, o, dir); o = lc_cat(p, o, name); o = lc_cat(p, o, ".nx" as *u8); p[o] = 0 as u8 61 let fd: i64 = sys_openat_rd(p) 62 if fd < 0 { return 0 } 63 sys_close(fd) 64 return 1 65} 66// does <name>.nx exist as a real organ file in any known dir? 67func lc_locate(name: *u8) -> i64 { 68 if lc_try("runtime/_hdl_build/" as *u8, name) == 1 { return 1 } 69 if lc_try("runtime/" as *u8, name) == 1 { return 1 } 70 if lc_try("runtime/bin/" as *u8, name) == 1 { return 1 } 71 return 0 72} 73 74func lc_field0(buf: *u8, ls: i64, le: i64, out: *u8, cap: i64) -> i64 { 75 var p: i64 = ls 76 var k: i64 = 0 77 while p < le { 78 if buf[p] == (9 as u8) { p = le } else { if k < cap - 1 { out[k] = buf[p]; k = k + 1 } p = p + 1 } 79 } 80 out[k] = 0 as u8 81 return k 82} 83// fill out[0]=index of 1st TAB, out[1]=index of 2nd TAB (-1 if absent). 84func lc_tabs(buf: *u8, ls: i64, le: i64, out: *i64) -> i64 { 85 out[0] = 0 - 1; out[1] = 0 - 1 86 var p: i64 = ls 87 while p < le { 88 if buf[p] == (9 as u8) { 89 if out[0] < 0 { out[0] = p } else { if out[1] < 0 { out[1] = p } } 90 } 91 p = p + 1 92 } 93 return 0 94} 95 96// append one record-hint lineage row for a located missing-parent organ. 97func lc_emit_hint(fd: i64, name: *u8) -> i64 { 98 lc_w(fd, name); lc_w(fd, "\t" as *u8) 99 lc_w(fd, LC_WOMB); lc_w(fd, "\t" as *u8) 100 lc_w(fd, "build-parent edge (universal sovereign ancestry)" as *u8); lc_w(fd, "\t" as *u8) 101 lc_w(fd, "auto-rooted genealogy record-hint (nx_lineage_complete): every sovereign organ is begotten by the build engine nx_sov_build_run -- BOOTSTRAP_MAP rung7; rich genetics pending backfill" as *u8); lc_w(fd, "\t" as *u8) 102 lc_w(fd, "-" as *u8); lc_w(fd, "\t" as *u8) 103 lc_w(fd, "closes a broken parent-link to genesis (organ located on disk before authoring -- never an invented node)" as *u8); lc_w(fd, "\t" as *u8) 104 lc_w(fd, "genealogy completeness -> root-trace closes -> the whole tree roots at the spore" as *u8) 105 lc_w(fd, "\n" as *u8) 106 return 0 107} 108 109func lc_report(fd: i64, children: i64, missing: i64, located: i64, notfound: i64, reparented: i64, epoch: i64, ok: i64) -> i64 { 110 lc_w(fd, "LINEAGECOMPLETE authored=organ existing_children=" as *u8); lc_wn(fd, children) 111 lc_w(fd, " missing_parents=" as *u8); lc_wn(fd, missing) 112 lc_w(fd, " located_added=" as *u8); lc_wn(fd, located) 113 lc_w(fd, " notfound_unauthored=" as *u8); lc_wn(fd, notfound) 114 lc_w(fd, " floating_roots_reparented=" as *u8); lc_wn(fd, reparented) 115 lc_w(fd, " epoch=" as *u8); lc_wn(fd, epoch) 116 if ok == 1 { lc_w(fd, " verdict=GREEN\n" as *u8) } else { lc_w(fd, " verdict=RED\n" as *u8) } 117 return 0 118} 119 120func main() -> i64 { 121 let buf: *u8 = sys_mmap(LC_MAGIC_1048576) 122 let fd: i64 = sys_openat_rd(LC_LIN) 123 if fd < 0 { lc_w(1, "LINEAGECOMPLETE verdict=RED reason=lineage-missing\n" as *u8); sys_exit(1); return 1 } 124 var n: i64 = 0 125 var r: i64 = sys_read(fd, buf, LC_MAGIC_1048575) 126 while r > 0 { n = n + r; r = sys_read(fd, buf + n, LC_MAGIC_1048575 - n) } 127 sys_close(fd) 128 129 // pass 1: collect children 130 let kids: i64 = sys_mmap(LC_MAXC * LC_IDCAP) as i64 131 var nk: i64 = 0 132 let cbuf: *u8 = sys_mmap(LC_IDCAP) 133 var i: i64 = 0 134 while i < n { 135 var le: i64 = i 136 var s: i64 = 1 137 while s == 1 { if le >= n { s = 0 } else { if buf[le] == (10 as u8) { s = 0 } else { le = le + 1 } } } 138 if le > i { if buf[i] != (35 as u8) { 139 lc_field0(buf, i, le, cbuf, LC_IDCAP) 140 if cbuf[0] != (0 as u8) { if nk < LC_MAXC { let d: *u8 = (kids + nk * LC_IDCAP) as *u8; var k: i64 = 0; while cbuf[k] != (0 as u8) { d[k] = cbuf[k]; k = k + 1 } d[k] = 0 as u8; nk = nk + 1 } } 141 } } 142 i = le + 1 143 } 144 145 // pass 2: collect unique missing-parent organs (referenced, organ-like, not a child) 146 let todo: i64 = sys_mmap(128 * LC_IDCAP) as i64 147 var nt: i64 = 0 148 let pbuf: *u8 = sys_mmap(512) 149 let tok: *u8 = sys_mmap(LC_IDCAP) 150 i = 0 151 while i < n { 152 var le: i64 = i 153 var s: i64 = 1 154 while s == 1 { if le >= n { s = 0 } else { if buf[le] == (10 as u8) { s = 0 } else { le = le + 1 } } } 155 if le > i { if buf[i] != (35 as u8) { 156 let tb: *i64 = sys_mmap(16) as *i64 157 lc_tabs(buf, i, le, tb) 158 if tb[0] >= 0 { if tb[1] >= 0 { 159 // field1 (parents) = [tb[0]+1, tb[1]) 160 var p: i64 = tb[0] + 1 161 var k: i64 = 0 162 var go: i64 = 1 163 while go == 1 { 164 var ch: i64 = 0 165 if p < tb[1] { ch = buf[p] as i64 } 166 var fin: i64 = 0 167 if p >= tb[1] { fin = 1 } 168 if ch == 44 { fin = 1 } 169 if fin == 1 { 170 if k > 0 { 171 tok[k] = 0 as u8 172 if lc_is_organ(tok) == 1 { if lc_in_set(kids, nk, LC_IDCAP, tok) == 0 { if lc_in_set(todo, nt, LC_IDCAP, tok) == 0 { 173 if nt < 128 { let d: *u8 = (todo + nt * LC_IDCAP) as *u8; var kk: i64 = 0; while tok[kk] != (0 as u8) { d[kk] = tok[kk]; kk = kk + 1 } d[kk] = 0 as u8; nt = nt + 1 } 174 } } } 175 k = 0 176 } 177 if p >= tb[1] { go = 0 } 178 } else { if k < LC_IDCAP - 1 { tok[k] = ch as u8; k = k + 1 } } 179 p = p + 1 180 } 181 } } 182 } } 183 i = le + 1 184 } 185 186 // pass 3: locate each missing-parent; mark addable 187 let addable: *i64 = sys_mmap(128 * 8) as *i64 188 var located: i64 = 0 189 var notfound: i64 = 0 190 var j: i64 = 0 191 while j < nt { 192 let nm: *u8 = (todo + j * LC_IDCAP) as *u8 193 if lc_locate(nm) == 1 { addable[j] = 1; located = located + 1 } else { addable[j] = 0; notfound = notfound + 1; lc_w(1, "LINEAGECOMPLETE NOTFOUND (not authored, no organ file) parent=" as *u8); lc_w(1, nm); lc_w(1, "\n" as *u8) } 194 j = j + 1 195 } 196 197 // backup current lineage.tsv 198 let bfd: i64 = sys_openat_wr(LC_BAK, 420) 199 if bfd >= 0 { sys_write(bfd, buf, n); sys_close(bfd) } 200 201 // rewrite to tmp: existing lines verbatim (floaters re-parented), then the located record-hints 202 let ofd: i64 = sys_openat_wr(LC_TMP, 420) 203 if ofd < 0 { lc_w(1, "LINEAGECOMPLETE verdict=RED reason=tmp-unwritable\n" as *u8); sys_exit(1); return 1 } 204 var reparented: i64 = 0 205 let cb2: *u8 = sys_mmap(LC_IDCAP) 206 i = 0 207 while i < n { 208 var le: i64 = i 209 var s: i64 = 1 210 while s == 1 { if le >= n { s = 0 } else { if buf[le] == (10 as u8) { s = 0 } else { le = le + 1 } } } 211 if le > i { 212 if buf[i] == (35 as u8) { lc_wrange(ofd, buf, i, le); lc_w(ofd, "\n" as *u8) } 213 else { 214 let tb: *i64 = sys_mmap(16) as *i64 215 lc_tabs(buf, i, le, tb) 216 var did: i64 = 0 217 if tb[0] >= 0 { if tb[1] >= 0 { 218 // floating-root? field1=="-" (one byte between the two tabs) and child != spore 219 if tb[1] == tb[0] + 2 { if buf[tb[0]+1] == (45 as u8) { 220 lc_field0(buf, i, le, cb2, LC_IDCAP) 221 if lc_streq(cb2, LC_SPORE) == 0 { 222 lc_wrange(ofd, buf, i, tb[0]) // field0 223 lc_w(ofd, "\t" as *u8); lc_w(ofd, LC_WOMB) 224 lc_wrange(ofd, buf, tb[1], le) // "\t" field2 .. end 225 lc_w(ofd, "\n" as *u8) 226 reparented = reparented + 1 227 did = 1 228 } 229 } } 230 } } 231 if did == 0 { lc_wrange(ofd, buf, i, le); lc_w(ofd, "\n" as *u8) } 232 } 233 } 234 i = le + 1 235 } 236 // append located record-hints 237 j = 0 238 while j < nt { if addable[j] == 1 { lc_emit_hint(ofd, (todo + j * LC_IDCAP) as *u8) } j = j + 1 } 239 sys_close(ofd) 240 241 let rok: i64 = sys_renameat(LC_TMP, LC_LIN) 242 let epoch: i64 = sys_now_realtime_sec() 243 var ok: i64 = 1 244 if rok < 0 { ok = 0 } 245 246 lc_report(1, nk, nt, located, notfound, reparented, epoch, ok) 247 let lg: i64 = sys_openat_append(LC_LOG, 420) 248 if lg >= 0 { lc_report(lg, nk, nt, located, notfound, reparented, epoch, ok); sys_close(lg) } 249 if ok == 1 { sys_exit(0); return 0 } 250 sys_exit(1); return 1 251}