code wiki / _hdl_build / nx_store_catalog.nx
nx_store_catalog.nx source
↩ module page · 116 lines · 5742 B
1// nx_store_catalog.nx -- the sovereign DATA CATALOG: discover every seg_store under knowledge/store/ and report
2// its segment count + entry count. The s-class-IM DISCOVERY rung (DataHub/OpenLineage-class catalog), READ-ONLY /
3// ADDITIVE (touches no store, no consumer -> zero break risk). Composes nx_seg_store (ss_manifest_cap / ss_load_aux
4// / ss_r32) -- a store is any "<name>manifest.txt" under knowledge/store/. Self-verifies: the largest store has
5// >=3000 entries (the algo-registry consolidated this session). NEXT rung = lineage (who produces/consumes each
6// store). expect_exit: 0 license_tier: ORIGINAL
7import "nx_seg_store.nx"
8import "nx_syscalls.nx"
9import "nx_estate_path.nx" // ep_anchor: the CWD must not decide this organ's verdict
10const K_MAGIC_262144: i64 = 262144
11const K_MAGIC_3000: i64 = 3000
12
13func sc_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
14func sc_putn(v: i64) -> i64 {
15 if v == 0 { sys_write(1, "0" as *u8, 1); return 0 }
16 var m: i64 = v
17 if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m }
18 let d: *u8 = sys_mmap(24); var k: i64 = 0
19 while m > 0 { d[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
20 // THE DIGITS WERE COMPUTED AND THEN THROWN AWAY (2026-07-30). There was no write here at all, so every
21 // non-zero number printed as EMPTY -- the catalog emitted "segs= entries=" for every store and
22 // "stores= total-entries= largest=" in its summary. The arithmetic was always correct (the >=3000
23 // self-check really did pass on a real maxent), so this was a pure DISPLAY defect -- which is exactly
24 // why a working discovery organ was never worth deploying: a catalog that cannot show you a number is
25 // not a catalog. Digits are generated least-significant-first, so reverse into a buffer and write once.
26 let o: *u8 = sys_mmap(24)
27 var w: i64 = 0
28 while w < k { o[w] = d[k - 1 - w]; w = w + 1 }
29 sys_write(1, o, k)
30 sys_munmap(o, 24)
31 sys_munmap(d, 24)
32 return 0
33}
34func sc_len(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
35// does name end with suf?
36func sc_ends(name: *u8, suf: *u8) -> i64 {
37 let nl: i64 = sc_len(name); let sl: i64 = sc_len(suf)
38 if nl < sl { return 0 }
39 var i: i64 = 0
40 while i < sl { if name[nl - sl + i] != suf[i] { return 0 } i = i + 1 }
41 return 1
42}
43// entries = sum of per-segment NXK1 key counts; segment count into out_seg[0]
44func sc_stat(prefix: *u8, out_seg: *i64) -> i64 {
45 let segs: *i64 = sys_mmap(8 * 512) as *i64
46 let nseg: i64 = ss_manifest_cap(prefix, segs, 512)
47 out_seg[0] = nseg
48 var ent: i64 = 0
49 let outs: *i64 = sys_mmap(8 * 8) as *i64
50 var i: i64 = 0
51 while i < nseg {
52 ss_load_aux(prefix, segs[i] as *u8, outs)
53 let kb: *u8 = outs[0] as *u8
54 let ksz: i64 = outs[1]
55 if ksz >= 8 { ent = ent + ss_r32(kb, 4) }
56 i = i + 1
57 }
58 return ent
59}
60func main() -> i64 {
61 // ANCHOR FIRST (2026-08-04): run from buildroot this organ read an EMPTY/absent estate
62 // and fail-closed RED (or, worse, appended to a SECOND store). Its subject is the
63 // estate, so the caller's working directory must not decide its verdict.
64 ep_anchor()
65 sc_puts("STORE CATALOG: sovereign seg_stores under knowledge/store/ (detailing entries>=3)\n" as *u8)
66 let fd: i64 = sys_openat_rd("knowledge/store" as *u8)
67 if fd < 0 { sc_puts(" open knowledge/store FAILED\n" as *u8); return 1 }
68 let dbuf: *u8 = sys_mmap(K_MAGIC_262144)
69 var nstores: i64 = 0
70 var nsmall: i64 = 0
71 var totent: i64 = 0
72 var maxent: i64 = 0
73 let segp: *i64 = sys_mmap(16) as *i64
74 var go: i64 = 1
75 while go == 1 {
76 let nb: i64 = sys_getdents64(fd, dbuf, K_MAGIC_262144)
77 if nb <= 0 { go = 0 } else {
78 var off: i64 = 0
79 while off < nb {
80 let rec: *u8 = (dbuf as i64 + off) as *u8
81 let rl: i64 = dirent_reclen(rec)
82 if rl <= 0 { off = nb } else {
83 let nm: *u8 = dirent_name(rec)
84 if sc_ends(nm, "manifest.txt" as *u8) == 1 {
85 let nl: i64 = sc_len(nm)
86 let cut: i64 = nl - 12
87 let pfx: *u8 = sys_mmap(512); var po: i64 = 0
88 po = ss_cat(pfx, po, "knowledge/store/" as *u8)
89 var j: i64 = 0
90 while j < cut { pfx[po] = nm[j]; po = po + 1; j = j + 1 }
91 pfx[po] = 0 as u8
92 let ent: i64 = sc_stat(pfx, segp)
93 nstores = nstores + 1
94 totent = totent + ent
95 if ent > maxent { maxent = ent }
96 if ent >= 3 {
97 sc_puts(" ")
98 var q: i64 = 0
99 while q < cut { let o1: *u8 = sys_mmap(1); o1[0] = nm[q]; sys_write(1, o1, 1); q = q + 1 }
100 sc_puts(" segs="); sc_putn(segp[0]); sc_puts(" entries="); sc_putn(ent); sc_puts("\n" as *u8)
101 } else { nsmall = nsmall + 1 }
102 }
103 off = off + rl
104 }
105 }
106 }
107 }
108 sys_close(fd)
109 sc_puts("CATALOG SUMMARY: stores="); sc_putn(nstores)
110 sc_puts(" (single/small<3="); sc_putn(nsmall)
111 sc_puts(") total-entries="); sc_putn(totent)
112 sc_puts(" largest="); sc_putn(maxent); sc_puts("\n" as *u8)
113 if maxent >= K_MAGIC_3000 { sc_puts("STORE-CATALOG: GREEN (algo-registry consolidated store cataloged)\n" as *u8); return 0 }
114 sc_puts("STORE-CATALOG: RED (expected a >=3000-entry store)\n" as *u8)
115 return 1
116}