nx_gensota.nx source
↩ module page · 414 lines · 16121 B
1// nx_gensota.nx -- THE per-generation SOTA rollup instrument (2026-08-01, gensota ws).
2// WHY: /code/sota scores each family-tree generation, but its band colors were
3// ASSESSED by hand. This organ DERIVES the per-generation numbers from the live
4// eco graph so the page's claims are measured, not asserted.
5// WHAT (per generation of the import DAG, BFS from the god roots, same walk as
6// nx_eco_graph_pedigree): total modules | validation artifacts (name-classified
7// _test/_gate/_bench/_demo/_probe/_smoke) | working organs | working organs with a
8// name-matching validation sibling (<base>_gate.nx or <base>_test.nx) -> attach_permil.
9// VERDICT: RED if ANY generation with organs misses the attach bar (headline = MIN
10// across generations, never the mean), or if the partition fails to SUM to the node
11// count (a partition is a claim: check the parts sum).
12// BARS: knowledge/gensota_bars.conf `attach_bar_permil=<n>` (rule 11: the threshold
13// lives in data). The argv override exists FOR THE GATE: a gate must be able to make
14// its instrument fail (bar=1001 is the crafted-bad input).
15// NON-VACUITY: missing store / zero nodes / zero edges = INSTRUMENT-BLIND exit 3;
16// absent bars conf with no override = REFUSED exit 4 -- a defaulted bar is a wish.
17// Comparisons CROSS-MULTIPLY (att*1000 vs bar*org), never divide-then-compare: the
18// integer-truncation silent-threshold-shift class (banked 2026-08-01).
19// Usage: nx_gensota [store] [bar_permil_override]
20// Exit: 0 GREEN | 1 RED | 3 INSTRUMENT-BLIND | 4 REFUSED-NO-BARS
21// license_tier: ORIGINAL expect_exit:0 No hw writes (Rule 26).
22import "nx_syscalls.nx"
23import "nx_eco_graph.nx"
24
25const GS_TAB_SLOTS: i64 = 65536
26const GS_TAB_SAFE_N: i64 = 60000
27const GS_NAME_CAP: i64 = 300
28const GS_MAXGEN_CAP: i64 = 64
29const GS_HASH_SEED: i64 = 5381
30const GS_HASH_MUL: i64 = 33
31const GS_HASH_MOD: i64 = 1000000007
32const GS_PERMIL: i64 = 1000
33const GS_BAR_IMPOSSIBLE: i64 = 1001
34
35func gs_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
36func gs_w(fd: i64, s: *u8) -> i64 { sys_write(fd, s, gs_slen(s)); return 0 }
37func gs_wn(fd: i64, v: i64) -> i64 {
38 let b: *u8 = sys_mmap(28)
39 let t: *u8 = sys_mmap(28)
40 var m: i64 = v
41 if m < 0 { m = 0 - m; sys_write(fd, "-" as *u8, 1) }
42 var k: i64 = 0
43 if m == 0 { t[0] = 48 as u8; k = 1 }
44 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
45 var i: i64 = 0
46 while i < k { b[i] = t[k-1-i]; i = i + 1 }
47 sys_write(fd, b, k)
48 return 0
49}
50
51func gs_nlen(g: *EcoGraph, idx: i64) -> i64 {
52 let off: i64 = g.node_off[idx]
53 var n: i64 = 0
54 while g.arena[off+n] != (0 as u8) { n = n + 1 }
55 return n
56}
57func gs_ends(g: *EcoGraph, idx: i64, suf: *u8) -> i64 {
58 let nl: i64 = gs_nlen(g, idx)
59 let sl: i64 = gs_slen(suf)
60 if nl < sl { return 0 }
61 let off: i64 = g.node_off[idx] + nl - sl
62 var i: i64 = 0
63 while i < sl {
64 if g.arena[off+i] != suf[i] { return 0 }
65 i = i + 1
66 }
67 return 1
68}
69func gs_isval(g: *EcoGraph, idx: i64) -> i64 {
70 if gs_ends(g, idx, "_test.nx" as *u8) == 1 { return 1 }
71 if gs_ends(g, idx, "_gate.nx" as *u8) == 1 { return 1 }
72 if gs_ends(g, idx, "_bench.nx" as *u8) == 1 { return 1 }
73 if gs_ends(g, idx, "_demo.nx" as *u8) == 1 { return 1 }
74 if gs_ends(g, idx, "_probe.nx" as *u8) == 1 { return 1 }
75 if gs_ends(g, idx, "_smoke.nx" as *u8) == 1 { return 1 }
76 return 0
77}
78
79func gs_hash_node(g: *EcoGraph, idx: i64) -> i64 {
80 let off: i64 = g.node_off[idx]
81 var h: i64 = GS_HASH_SEED
82 var i: i64 = 0
83 while g.arena[off+i] != (0 as u8) {
84 h = (h * GS_HASH_MUL + (g.arena[off+i] as i64)) % GS_HASH_MOD
85 i = i + 1
86 }
87 return h
88}
89func gs_hash_str(s: *u8) -> i64 {
90 var h: i64 = GS_HASH_SEED
91 var i: i64 = 0
92 while s[i] != (0 as u8) {
93 h = (h * GS_HASH_MUL + (s[i] as i64)) % GS_HASH_MOD
94 i = i + 1
95 }
96 return h
97}
98func gs_nameeq(g: *EcoGraph, idx: i64, s: *u8) -> i64 {
99 let off: i64 = g.node_off[idx]
100 var i: i64 = 0
101 while 1 == 1 {
102 if g.arena[off+i] != s[i] { return 0 }
103 if s[i] == (0 as u8) { return 1 }
104 i = i + 1
105 }
106 return 0
107}
108func gs_tab_insert(tab: *i64, g: *EcoGraph, idx: i64) -> i64 {
109 var slot: i64 = gs_hash_node(g, idx) % GS_TAB_SLOTS
110 while tab[slot] != 0 { slot = slot + 1; if slot == GS_TAB_SLOTS { slot = 0 } }
111 tab[slot] = idx + 1
112 return 0
113}
114func gs_tab_has(tab: *i64, g: *EcoGraph, s: *u8) -> i64 {
115 var slot: i64 = gs_hash_str(s) % GS_TAB_SLOTS
116 while tab[slot] != 0 {
117 if gs_nameeq(g, tab[slot]-1, s) == 1 { return 1 }
118 slot = slot + 1
119 if slot == GS_TAB_SLOTS { slot = 0 }
120 }
121 return 0
122}
123// build "<base><suf>" from node <base>.nx into out; 0 if not a .nx name or too long
124func gs_sib(g: *EcoGraph, idx: i64, suf: *u8, out: *u8) -> i64 {
125 let nl: i64 = gs_nlen(g, idx)
126 if nl < 4 { return 0 }
127 if nl + 12 >= GS_NAME_CAP { return 0 }
128 let off: i64 = g.node_off[idx]
129 if g.arena[off+nl-3] != (46 as u8) { return 0 }
130 var i: i64 = 0
131 while i < nl-3 { out[i] = g.arena[off+i]; i = i + 1 }
132 var j: i64 = 0
133 while suf[j] != (0 as u8) { out[i] = suf[j]; i = i + 1; j = j + 1 }
134 out[i] = 0 as u8
135 return 1
136}
137
138// bars conf: first `=` then a digit run; -1 = absent/unparseable (caller refuses)
139func gs_read_bar(path: *u8) -> i64 {
140 let lenp: *i64 = sys_mmap(16) as *i64
141 lenp[0] = 0
142 let src: *u8 = sys_read_file(path, lenp)
143 if (src as i64) == 0 { return 0 - 1 }
144 let n: i64 = lenp[0]
145 var i: i64 = 0
146 while i < n {
147 if (src[i] as i64) == 61 {
148 i = i + 1
149 var v: i64 = 0 - 1
150 while i < n {
151 let d: i64 = src[i] as i64
152 if d < 48 { return v }
153 if d > 57 { return v }
154 if v < 0 { v = 0 }
155 v = v*10 + (d-48)
156 i = i + 1
157 }
158 return v
159 }
160 i = i + 1
161 }
162 return 0 - 1
163}
164
165// WORKLIST: the harvest made consumable. A generation's attach_permil is a NUMBER; the work it
166// implies is a LIST. This emits the unattached organs of one generation ranked by fan-in
167// (importers = measured blast radius), so the highest-leverage gate to author is line 1.
168// A COUNT IS NOT A QUEUE -- an unranked backlog gets worked in file order, which is random
169// with respect to value.
170func gs_worklist(storep: *u8, wantgen: i64, topn: i64, fd: i64) -> i64 {
171 let g: *EcoGraph = eg_load(storep)
172 if (g as i64) == 0 { gs_w(fd, "VERDICT=RED INSTRUMENT-BLIND store not found\n" as *u8); return 3 }
173 let n: i64 = g.node_count
174 if n == 0 { gs_w(fd, "VERDICT=RED INSTRUMENT-BLIND zero nodes\n" as *u8); return 3 }
175 if n >= GS_TAB_SAFE_N { gs_w(fd, "VERDICT=RED node count exceeds table capacity\n" as *u8); return 1 }
176 let gen: *i64 = sys_mmap((n+2)*8) as *i64
177 let work: *i64 = sys_mmap((n+2)*8) as *i64
178 var i: i64 = 0
179 while i < n { gen[i] = 0 - 1; i = i + 1 }
180 var wt: i64 = 0
181 i = 0
182 while i < n {
183 if g.out_head[i] == g.out_head[i+1] { gen[i] = 0; work[wt] = i; wt = wt + 1 }
184 i = i + 1
185 }
186 var wh: i64 = 0
187 while wh < wt {
188 let v: i64 = work[wh]
189 var p: i64 = g.in_head[v]
190 let e: i64 = g.in_head[v+1]
191 while p < e {
192 let u: i64 = g.in_list[p]
193 if gen[u] < 0 { gen[u] = gen[v] + 1; work[wt] = u; wt = wt + 1 }
194 p = p + 1
195 }
196 wh = wh + 1
197 }
198 i = 0
199 while i < n { if gen[i] < 0 { gen[i] = 0 } i = i + 1 }
200 let tab: *i64 = sys_mmap(GS_TAB_SLOTS*8) as *i64
201 i = 0
202 while i < n { gs_tab_insert(tab, g, i); i = i + 1 }
203 let sib: *u8 = sys_mmap(GS_NAME_CAP)
204 gs_w(fd, "=== NX-GENSOTA WORKLIST: unattached organs, gen=" as *u8); gs_wn(fd, wantgen)
205 gs_w(fd, " ranked by importers (measured blast radius) ===\n" as *u8)
206 var cand: i64 = 0
207 i = 0
208 while i < n {
209 if gen[i] == wantgen {
210 if gs_isval(g, i) == 0 {
211 var a: i64 = 0
212 if gs_sib(g, i, "_gate.nx" as *u8, sib) == 1 { if gs_tab_has(tab, g, sib) == 1 { a = 1 } }
213 if a == 0 { if gs_sib(g, i, "_test.nx" as *u8, sib) == 1 { if gs_tab_has(tab, g, sib) == 1 { a = 1 } } }
214 if a == 0 { cand = cand + 1 }
215 }
216 }
217 i = i + 1
218 }
219 var shown: i64 = 0
220 var bar_fanin: i64 = 0 - 1
221 while shown < topn {
222 var best: i64 = 0 - 1
223 var bestfan: i64 = 0 - 1
224 i = 0
225 while i < n {
226 if gen[i] == wantgen {
227 if gs_isval(g, i) == 0 {
228 var a2: i64 = 0
229 if gs_sib(g, i, "_gate.nx" as *u8, sib) == 1 { if gs_tab_has(tab, g, sib) == 1 { a2 = 1 } }
230 if a2 == 0 { if gs_sib(g, i, "_test.nx" as *u8, sib) == 1 { if gs_tab_has(tab, g, sib) == 1 { a2 = 1 } } }
231 if a2 == 0 {
232 let fan: i64 = g.in_head[i+1] - g.in_head[i]
233 var elig: i64 = 0
234 if bar_fanin < 0 { elig = 1 }
235 if bar_fanin >= 0 { if fan < bar_fanin { elig = 1 } }
236 if elig == 1 { if fan > bestfan { bestfan = fan; best = i } }
237 }
238 }
239 }
240 i = i + 1
241 }
242 if best < 0 { shown = topn }
243 if best >= 0 {
244 gs_w(fd, " importers=" as *u8); gs_wn(fd, bestfan); gs_w(fd, " " as *u8)
245 let off: i64 = g.node_off[best]
246 sys_write(fd, ((g.arena as i64)+off) as *u8, gs_nlen(g, best))
247 gs_w(fd, "\n" as *u8)
248 bar_fanin = bestfan
249 shown = shown + 1
250 }
251 }
252 gs_w(fd, "unattached_total=" as *u8); gs_wn(fd, cand)
253 gs_w(fd, " shown=<=" as *u8); gs_wn(fd, topn)
254 gs_w(fd, " (ties beyond the cut are NOT shown -- the count is the denominator, the list is a slice)\n" as *u8)
255 if cand == 0 { gs_w(fd, "VERDICT=GREEN generation fully attached\n" as *u8); return 0 }
256 gs_w(fd, "VERDICT=GREEN worklist emitted\n" as *u8)
257 return 0
258}
259
260func gs_run(storep: *u8, bar: i64, fd: i64) -> i64 {
261 gs_w(fd, "=== NX-GENSOTA: per-generation rollup (measured; store=" as *u8)
262 gs_w(fd, storep)
263 gs_w(fd, " bar_attach_permil=" as *u8)
264 gs_wn(fd, bar)
265 gs_w(fd, ") ===\n" as *u8)
266 let g: *EcoGraph = eg_load(storep)
267 if (g as i64) == 0 { gs_w(fd, "VERDICT=RED INSTRUMENT-BLIND store not found\n" as *u8); return 3 }
268 let n: i64 = g.node_count
269 if n == 0 { gs_w(fd, "VERDICT=RED INSTRUMENT-BLIND zero nodes\n" as *u8); return 3 }
270 if n >= GS_TAB_SAFE_N { gs_w(fd, "VERDICT=RED node count exceeds table capacity -- raise GS_TAB_SLOTS\n" as *u8); return 1 }
271 let edges: i64 = g.out_head[n]
272 if edges == 0 { gs_w(fd, "VERDICT=RED INSTRUMENT-BLIND zero edges\n" as *u8); return 3 }
273 let gen: *i64 = sys_mmap((n+2)*8) as *i64
274 let work: *i64 = sys_mmap((n+2)*8) as *i64
275 var i: i64 = 0
276 while i < n { gen[i] = 0 - 1; i = i + 1 }
277 var wt: i64 = 0
278 i = 0
279 while i < n {
280 if g.out_head[i] == g.out_head[i+1] { gen[i] = 0; work[wt] = i; wt = wt + 1 }
281 i = i + 1
282 }
283 var wh: i64 = 0
284 while wh < wt {
285 let v: i64 = work[wh]
286 var p: i64 = g.in_head[v]
287 let e: i64 = g.in_head[v+1]
288 while p < e {
289 let u: i64 = g.in_list[p]
290 if gen[u] < 0 { gen[u] = gen[v] + 1; work[wt] = u; wt = wt + 1 }
291 p = p + 1
292 }
293 wh = wh + 1
294 }
295 var maxgen: i64 = 0
296 i = 0
297 while i < n {
298 if gen[i] < 0 { gen[i] = 0 }
299 if gen[i] > maxgen { maxgen = gen[i] }
300 i = i + 1
301 }
302 if maxgen >= GS_MAXGEN_CAP { gs_w(fd, "VERDICT=RED maxgen exceeds structural cap -- graph suspect\n" as *u8); return 1 }
303 let tab: *i64 = sys_mmap(GS_TAB_SLOTS*8) as *i64
304 i = 0
305 while i < n { gs_tab_insert(tab, g, i); i = i + 1 }
306 let tot: *i64 = sys_mmap((maxgen+2)*8) as *i64
307 let val: *i64 = sys_mmap((maxgen+2)*8) as *i64
308 let org: *i64 = sys_mmap((maxgen+2)*8) as *i64
309 let att: *i64 = sys_mmap((maxgen+2)*8) as *i64
310 var l: i64 = 0
311 while l <= maxgen { tot[l] = 0; val[l] = 0; org[l] = 0; att[l] = 0; l = l + 1 }
312 let sib: *u8 = sys_mmap(GS_NAME_CAP)
313 i = 0
314 while i < n {
315 let gi: i64 = gen[i]
316 tot[gi] = tot[gi] + 1
317 if gs_isval(g, i) == 1 { val[gi] = val[gi] + 1 }
318 if gs_isval(g, i) == 0 {
319 org[gi] = org[gi] + 1
320 var a: i64 = 0
321 if gs_sib(g, i, "_gate.nx" as *u8, sib) == 1 { if gs_tab_has(tab, g, sib) == 1 { a = 1 } }
322 if a == 0 { if gs_sib(g, i, "_test.nx" as *u8, sib) == 1 { if gs_tab_has(tab, g, sib) == 1 { a = 1 } } }
323 att[gi] = att[gi] + a
324 }
325 i = i + 1
326 }
327 gs_w(fd, "nodes=" as *u8); gs_wn(fd, n)
328 gs_w(fd, " edges=" as *u8); gs_wn(fd, edges)
329 gs_w(fd, " maxgen=" as *u8); gs_wn(fd, maxgen)
330 gs_w(fd, "\n" as *u8)
331 var sum: i64 = 0
332 var minp: i64 = GS_BAR_IMPOSSIBLE
333 var mingen: i64 = 0 - 1
334 var failgen: i64 = 0 - 1
335 l = 0
336 while l <= maxgen {
337 sum = sum + tot[l]
338 gs_w(fd, "gen=" as *u8); gs_wn(fd, l)
339 gs_w(fd, " total=" as *u8); gs_wn(fd, tot[l])
340 gs_w(fd, " validation=" as *u8); gs_wn(fd, val[l])
341 gs_w(fd, " organs=" as *u8); gs_wn(fd, org[l])
342 gs_w(fd, " attached=" as *u8); gs_wn(fd, att[l])
343 gs_w(fd, " attach_permil=" as *u8)
344 if org[l] == 0 { gs_w(fd, "na" as *u8) }
345 if org[l] > 0 {
346 let disp: i64 = (att[l]*GS_PERMIL)/org[l]
347 gs_wn(fd, disp)
348 if minp > disp { minp = disp; mingen = l }
349 if att[l]*GS_PERMIL < bar*org[l] { if failgen < 0 { failgen = l } }
350 }
351 gs_w(fd, "\n" as *u8)
352 l = l + 1
353 }
354 gs_w(fd, "partition_sum=" as *u8); gs_wn(fd, sum)
355 gs_w(fd, " of " as *u8); gs_wn(fd, n)
356 if sum == n { gs_w(fd, " SUM-OK\n" as *u8) }
357 if sum != n { gs_w(fd, " SUM-FAIL\n" as *u8) }
358 gs_w(fd, "headline_min_attach_permil=" as *u8); gs_wn(fd, minp)
359 gs_w(fd, " at_gen=" as *u8); gs_wn(fd, mingen)
360 gs_w(fd, " (headline=MIN never mean; display truncated, verdict cross-multiplied)\n" as *u8)
361 if sum != n { gs_w(fd, "VERDICT=RED partition does not sum to node count\n" as *u8); return 1 }
362 if failgen >= 0 {
363 gs_w(fd, "VERDICT=RED first generation under bar: gen=" as *u8)
364 gs_wn(fd, failgen)
365 gs_w(fd, "\n" as *u8)
366 return 1
367 }
368 gs_w(fd, "VERDICT=GREEN every generation meets the attach bar\n" as *u8)
369 return 0
370}
371
372func gs_atoi(s: *u8) -> i64 {
373 var v: i64 = 0
374 var i: i64 = 0
375 while s[i] != (0 as u8) { let c: i64 = s[i] as i64; if c >= 48 { if c <= 57 { v = v*10 + (c-48) } } i = i + 1 }
376 return v
377}
378
379func main(argc: i64, argv: *i64) -> i64 {
380 // verb form: nx_gensota worklist <gen> [topn] [store]
381 if argc >= 3 {
382 let v0: *u8 = argv[1] as *u8
383 if v0[0] == (119 as u8) {
384 if v0[1] == (111 as u8) {
385 let wg: i64 = gs_atoi(argv[2] as *u8)
386 var tn: i64 = 20
387 if argc >= 4 { tn = gs_atoi(argv[3] as *u8) }
388 var wsp: *u8 = "knowledge/store/ecograph_full\x00" as *u8
389 if argc >= 5 { wsp = argv[4] as *u8 }
390 let wrc: i64 = gs_worklist(wsp, wg, tn, 1)
391 sys_exit(wrc)
392 return 0
393 }
394 }
395 }
396 var storep: *u8 = "knowledge/store/ecograph_full\x00" as *u8
397 if argc >= 2 { storep = argv[1] as *u8 }
398 var bar: i64 = 0 - 1
399 if argc >= 3 {
400 let bs: *u8 = argv[2] as *u8
401 var bv: i64 = 0
402 var bi: i64 = 0
403 while bs[bi] != (0 as u8) { bv = bv*10 + ((bs[bi] as i64) - 48); bi = bi + 1 }
404 bar = bv
405 }
406 if bar < 0 { bar = gs_read_bar("knowledge/gensota_bars.conf\x00" as *u8) }
407 if bar < 0 {
408 gs_w(1, "NX-GENSOTA REFUSED: knowledge/gensota_bars.conf absent/unparseable and no override -- a defaulted bar is a wish\n" as *u8)
409 sys_exit(4)
410 }
411 let rc: i64 = gs_run(storep, bar, 1)
412 sys_exit(rc)
413 return 0
414}