code wiki / _hdl_build / nx_genesis_lineage.nx
nx_genesis_lineage.nx source
↩ module page · 244 lines · 10324 B
1// nx_genesis_lineage.nx -- the GENESIS-SPINE lineage emitter (closes X-GEN-LINEAGE w=9, the
2// cardinal hidden corner: the genesis/adam toolchain has ZERO rows in lineage.tsv so "the engine
3// cannot see its own birth", BOOTSTRAP_MAP s4 #5 = WIKI_STRUCTURE s4 #10).
4//
5// AUTHOR=ORGAN: the ORGAN authors the registry rows; Claude does NOT hand-write lineage. The
6// parent->child edges + genetics are SOURCED DATA in knowledge/specs/genesis_descent.spec.tsv
7// (transcribed from the anchored BOOTSTRAP_MAP s3 descent table). This organ:
8// 1. reads the spec (field delimiter '|', rewritten to TAB on emit),
9// 2. loads the children already in lineage.tsv (idempotency: a child already present is SKIPPED),
10// 3. REFUSES any row whose parent is neither the spore, an existing lineage child, nor a batch
11// sibling -- it will never author a BROKEN LINK (the wiki's author-time "born with a home or
12// not at all" guard, applied to genealogy),
13// 4. appends the surviving rows to lineage.tsv and logs a GENLINEAGE verdict.
14// The independent PROOF that the chain actually roots at the spore is nx_root_trace (separate
15// organ); a wrong edge here cannot go green there. Idempotent (rerun = emitted=0 skipped=N),
16// additive-only (append, never overwrite -- rule 13). Sovereign syscalls only. license_tier: ORIGINAL
17import "nx_syscalls.nx"
18import "nx_itoa_lib.nx" // shared MSB-first emitter (zero-alloc)
19const GL_MAGIC_4096: i64 = 4096
20const GL_MAGIC_1048576: i64 = 1048576
21const GL_MAGIC_1048575: i64 = 1048575
22const GL_MAGIC_131072: i64 = 131072
23const GL_MAGIC_131071: i64 = 131071
24
25const GL_SPEC: *u8 = "knowledge/specs/genesis_descent.spec.tsv"
26const GL_LIN: *u8 = "knowledge/registry/lineage.tsv"
27const GL_LOG: *u8 = "knowledge/status/genesis_lineage.log"
28const GL_IDCAP: i64 = 48
29
30func gl_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 }
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 gl_wn(fd: i64, v: i64) -> i64 { nxi_fd(fd, v); return 0 }
36
37func gl_streq(a: *u8, b: *u8) -> i64 {
38 var i: i64 = 0
39 while i < 64 { if a[i] != b[i] { return 0 } if a[i] == (0 as u8) { return 1 } i = i + 1 }
40 return 1
41}
42
43func gl_in_set(arena: i64, cnt: i64, idcap: i64, name: *u8) -> i64 {
44 var i: i64 = 0
45 while i < cnt { if gl_streq((arena + i * idcap) as *u8, name) == 1 { return 1 } i = i + 1 }
46 return 0
47}
48
49// copy field `idx` (0-based, '|'-delimited) of line [ls,le) into out (NUL-terminated, bounded).
50func gl_field(buf: *u8, ls: i64, le: i64, idx: i64, out: *u8, outcap: i64) -> i64 {
51 var fi: i64 = 0
52 var p: i64 = ls
53 while fi < idx {
54 if p >= le { out[0] = 0 as u8; return 0 }
55 if buf[p] == (124 as u8) { fi = fi + 1 }
56 p = p + 1
57 }
58 var k: i64 = 0
59 while p < le {
60 if buf[p] == (124 as u8) { p = le } else {
61 if k < outcap - 1 { out[k] = buf[p]; k = k + 1 }
62 p = p + 1
63 }
64 }
65 out[k] = 0 as u8
66 return k
67}
68
69// every parent (comma list) must be "-" (root), an existing lineage child, or a batch sibling.
70func gl_validate(parents: *u8, ea: i64, ec: i64, ba: i64, bc: i64, idcap: i64) -> i64 {
71 if parents[0] == (45 as u8) { if parents[1] == (0 as u8) { return 1 } }
72 let tok: *u8 = sys_mmap(64)
73 var i: i64 = 0
74 var k: i64 = 0
75 var okall: i64 = 1
76 var go: i64 = 1
77 while go == 1 {
78 let ch: i64 = parents[i] as i64
79 var fin: i64 = 0
80 if ch == 0 { fin = 1 }
81 if ch == 44 { fin = 1 }
82 if fin == 1 {
83 if k > 0 {
84 tok[k] = 0 as u8
85 var found: i64 = 0
86 if gl_in_set(ea, ec, idcap, tok) == 1 { found = 1 }
87 if gl_in_set(ba, bc, idcap, tok) == 1 { found = 1 }
88 if found == 0 { okall = 0 }
89 k = 0
90 }
91 if ch == 0 { go = 0 }
92 } else { if k < 63 { tok[k] = ch as u8; k = k + 1 } }
93 i = i + 1
94 }
95 return okall
96}
97
98// append line [ls,le) to fd, rewriting '|' -> TAB, then a newline.
99func gl_append(fd: i64, buf: *u8, ls: i64, le: i64) -> i64 {
100 let ob: *u8 = sys_mmap(GL_MAGIC_4096)
101 var i: i64 = ls
102 var o: i64 = 0
103 while i < le {
104 if buf[i] == (124 as u8) { ob[o] = 9 as u8 } else { ob[o] = buf[i] }
105 o = o + 1
106 i = i + 1
107 }
108 ob[o] = 10 as u8; o = o + 1
109 sys_write(fd, ob, o)
110 return 0
111}
112
113// load field0 (child id) of every non-comment row of a TAB lineage file into arena; returns count.
114func gl_load_children(path: *u8, arena: i64, idcap: i64, maxc: i64) -> i64 {
115 let buf: *u8 = sys_mmap(GL_MAGIC_1048576)
116 let fd: i64 = sys_openat_rd(path)
117 if fd < 0 { return 0 }
118 var n: i64 = 0
119 var r: i64 = sys_read(fd, buf, GL_MAGIC_1048575)
120 while r > 0 { n = n + r; r = sys_read(fd, buf + n, GL_MAGIC_1048575 - n) }
121 sys_close(fd)
122 var cnt: i64 = 0
123 var i: i64 = 0
124 while i < n {
125 if buf[i] == (35 as u8) {
126 var sk: i64 = 1
127 while sk == 1 { if i >= n { sk = 0 } else { if buf[i] == (10 as u8) { sk = 0 } i = i + 1 } }
128 } else {
129 var fe: i64 = i
130 var scan: i64 = 1
131 while scan == 1 {
132 if fe >= n { scan = 0 } else {
133 if buf[fe] == (9 as u8) { scan = 0 } else { if buf[fe] == (10 as u8) { scan = 0 } else { fe = fe + 1 } }
134 }
135 }
136 let flen: i64 = fe - i
137 if flen > 0 { if flen < idcap { if cnt < maxc {
138 let dst: *u8 = (arena + cnt * idcap) as *u8
139 var k: i64 = 0
140 while k < flen { dst[k] = buf[i + k]; k = k + 1 }
141 dst[flen] = 0 as u8
142 cnt = cnt + 1
143 } } }
144 var le: i64 = fe
145 var s2: i64 = 1
146 while s2 == 1 { if le >= n { s2 = 0 } else { if buf[le] == (10 as u8) { s2 = 0 } else { le = le + 1 } } }
147 i = le + 1
148 }
149 }
150 return cnt
151}
152
153func gl_report(fd: i64, nl: i64, emitted: i64, skipped: i64, refused: i64, epoch: i64, ok: i64) -> i64 {
154 gl_w(fd, "GENLINEAGE authored=organ source=genesis_descent.spec.tsv spec_rows=" as *u8); gl_wn(fd, nl)
155 gl_w(fd, " emitted=" as *u8); gl_wn(fd, emitted)
156 gl_w(fd, " skipped_idempotent=" as *u8); gl_wn(fd, skipped)
157 gl_w(fd, " refused_broken=" as *u8); gl_wn(fd, refused)
158 gl_w(fd, " epoch=" as *u8); gl_wn(fd, epoch)
159 if ok == 1 { gl_w(fd, " verdict=GREEN\n" as *u8) } else { gl_w(fd, " verdict=RED\n" as *u8) }
160 return 0
161}
162
163func main(argc: i64, argv: *i64) -> i64 {
164 // argv[1] overrides the spec path: generalizes this emitter from the genesis spine to ANY sourced
165 // lineage spec (e.g. genealogy_lineage.spec.tsv). The validation + broken-link REFUSAL are unchanged,
166 // so a row whose parent is not the spore / an existing lineage child / a batch sibling is still
167 // refused -- no broken link can be authored regardless of which spec. Default = the genesis spine.
168 var spec_path: *u8 = GL_SPEC
169 if argc > 1 { spec_path = (argv[1]) as *u8 }
170 // read the spec
171 let buf: *u8 = sys_mmap(GL_MAGIC_131072)
172 let fd: i64 = sys_openat_rd(spec_path)
173 if fd < 0 { gl_w(1, "GENLINEAGE verdict=RED reason=spec-missing\n" as *u8); sys_exit(1); return 1 }
174 var n: i64 = 0
175 var r: i64 = sys_read(fd, buf, GL_MAGIC_131071)
176 while r > 0 { n = n + r; r = sys_read(fd, buf + n, GL_MAGIC_131071 - n) }
177 sys_close(fd)
178
179 // pass A: record line bounds + batch children
180 let lss: *i64 = sys_mmap(64 * 8) as *i64
181 let les: *i64 = sys_mmap(64 * 8) as *i64
182 let batch: i64 = sys_mmap(64 * GL_IDCAP) as i64
183 var nl: i64 = 0
184 var bc: i64 = 0
185 var i: i64 = 0
186 while i < n {
187 var le: i64 = i
188 var s: i64 = 1
189 while s == 1 { if le >= n { s = 0 } else { if buf[le] == (10 as u8) { s = 0 } else { le = le + 1 } } }
190 if le > i { if buf[i] != (35 as u8) {
191 if nl < 64 {
192 lss[nl] = i; les[nl] = le
193 let cb: *u8 = (batch + bc * GL_IDCAP) as *u8
194 gl_field(buf, i, le, 0, cb, GL_IDCAP)
195 if cb[0] != (0 as u8) { bc = bc + 1 }
196 nl = nl + 1
197 }
198 } }
199 i = le + 1
200 }
201
202 // existing lineage children (idempotency + parent-validation universe)
203 let exist: i64 = sys_mmap(700 * GL_IDCAP) as i64
204 var ec: i64 = gl_load_children(GL_LIN, exist, GL_IDCAP, 700)
205
206 // pass B: validate + emit (append-only)
207 let lfd: i64 = sys_openat_append(GL_LIN, 420)
208 if lfd < 0 { gl_w(1, "GENLINEAGE verdict=RED reason=lineage-unwritable\n" as *u8); sys_exit(1); return 1 }
209 var emitted: i64 = 0
210 var skipped: i64 = 0
211 var refused: i64 = 0
212 let cbuf: *u8 = sys_mmap(GL_IDCAP)
213 let pbuf: *u8 = sys_mmap(512)
214 var j: i64 = 0
215 while j < nl {
216 gl_field(buf, lss[j], les[j], 0, cbuf, GL_IDCAP)
217 gl_field(buf, lss[j], les[j], 1, pbuf, 512)
218 if gl_in_set(exist, ec, GL_IDCAP, cbuf) == 1 { skipped = skipped + 1 }
219 else {
220 if gl_validate(pbuf, exist, ec, batch, bc, GL_IDCAP) == 0 {
221 refused = refused + 1
222 gl_w(1, "GENLINEAGE REFUSE broken-parent child=" as *u8); gl_w(1, cbuf); gl_w(1, "\n" as *u8)
223 } else {
224 gl_append(lfd, buf, lss[j], les[j])
225 if ec < 700 { let d: *u8 = (exist + ec * GL_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; ec = ec + 1 }
226 emitted = emitted + 1
227 }
228 }
229 j = j + 1
230 }
231 sys_close(lfd)
232
233 let epoch: i64 = sys_now_realtime_sec()
234 var ok: i64 = 1
235 if refused != 0 { ok = 0 }
236 if emitted + skipped != nl { ok = 0 }
237
238 gl_report(1, nl, emitted, skipped, refused, epoch, ok)
239 let glog: i64 = sys_openat_append(GL_LOG, 420)
240 if glog >= 0 { gl_report(glog, nl, emitted, skipped, refused, epoch, ok); sys_close(glog) }
241
242 if ok == 1 { sys_exit(0); return 0 }
243 sys_exit(1); return 1
244}