code wiki / _hdl_build / nx_build_autoroot_gate.nx
nx_build_autoroot_gate.nx source
↩ module page · 149 lines · 7785 B
1// nx_build_autoroot_gate.nx -- AUTO-ROOT THE WHOLE STACK via the import-DAG (operator: "rooted from god so
2// we can rebuild anything ... no floating mystery kids"). Insight: an organ declares its OWN ancestors in
3// its `import` lines, so we can root EVERY organ automatically -- no manual anchor. An organ is IMPORT-ROOTED
4// (rebuildable) iff every import it declares RESOLVES to a real file (which itself bottoms at nx_syscalls ->
5// toolchain -> god). A dangling import = a TRUE orphan (broken lineage, cannot rebuild). This collapses the
6// coarse "floating" count (organs lacking a registry anchor) into the PRECISE set of truly-unrootable organs.
7// Scans ALL nx_*.nx in runtime/_hdl_build. GATE w/ neg-control: GREEN iff the scan completes + a bogus import
8// is unresolvable + a real leaf resolves; it REPORTS import-rooted vs broken (the precise orphan worklist).
9// Sovereign: imports only nx_syscalls. license_tier: ORIGINAL expect_exit: 0
10import "nx_syscalls.nx"
11
12const ODIR: *u8 = "runtime/_hdl_build"
13const FB: i64 = 262144
14const SLOT: i64 = 160
15
16func ap(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
17func alen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
18func apn(v: i64) -> i64 {
19 let bb: *u8 = sys_mmap(28); var m: i64 = v
20 if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m }
21 let t: *u8 = sys_mmap(28); var k: i64 = 0
22 if m == 0 { t[0] = 48 as u8; k = 1 }
23 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
24 var i: i64 = 0; while i < k { bb[i] = t[k - 1 - i]; i = i + 1 }
25 sys_write(1, bb, k); return 0
26}
27func aread(path: *u8, buf: *u8, cap: i64) -> i64 {
28 let fd: i64 = sys_openat_rd(path); if fd < 0 { return 0 - 1 }
29 var total: i64 = 0; var nrd: i64 = sys_read(fd, buf, cap)
30 while nrd > 0 { total = total + nrd; if total >= cap { nrd = 0 } else { nrd = sys_read(fd, ((buf as i64) + total) as *u8, cap - total) } }
31 sys_close(fd); return total
32}
33func aexists(path: *u8) -> i64 { let fd: i64 = sys_openat_rd(path); if fd < 0 { return 0 } sys_close(fd); return 1 }
34func ajoin(pfx: *u8, name: *u8, out: *u8) -> i64 {
35 var o: i64 = 0; var i: i64 = 0
36 while pfx[i] != (0 as u8) { out[o] = pfx[i]; o = o + 1; i = i + 1 }
37 i = 0; while name[i] != (0 as u8) { out[o] = name[i]; o = o + 1; i = i + 1 }
38 out[o] = 0 as u8; return 0
39}
40// resolve an import name across the known organ roots. 1 if it exists somewhere.
41func aresolve(name: *u8) -> i64 {
42 let p: *u8 = sys_mmap(512)
43 ajoin("runtime/_hdl_build/" as *u8, name, p); if aexists(p) == 1 { return 1 }
44 ajoin("runtime/" as *u8, name, p); if aexists(p) == 1 { return 1 }
45 ajoin("runtime/wiki/" as *u8, name, p); if aexists(p) == 1 { return 1 }
46 ajoin("nxasm/" as *u8, name, p); if aexists(p) == 1 { return 1 }
47 return 0
48}
49func afind(buf: *u8, from: i64, to: i64, pat: *u8) -> i64 {
50 let pl: i64 = alen(pat); if pl == 0 { return 0 - 1 }
51 var i: i64 = from
52 while i + pl <= to { var j: i64 = 0; var ok: i64 = 1; while j < pl { if buf[i + j] != pat[j] { ok = 0; j = pl } else { j = j + 1 } } if ok == 1 { return i } i = i + 1 }
53 return 0 - 1
54}
55func agetdents(fd: i64, buf: *u8, count: i64) -> i64 { return __syscall(217, fd, buf, count, 0, 0, 0) }
56func a_isorgan(name: *u8) -> i64 {
57 if name[0] != (110 as u8) { return 0 }
58 if name[1] != (120 as u8) { return 0 }
59 if name[2] != (95 as u8) { return 0 }
60 let n: i64 = alen(name)
61 if n < 4 { return 0 }
62 if name[n - 3] != (46 as u8) { return 0 }
63 if name[n - 2] != (110 as u8) { return 0 }
64 if name[n - 1] != (120 as u8) { return 0 }
65 return 1
66}
67// returns count of DANGLING imports in the organ file at `path` (-1 if unreadable).
68func a_dangling(path: *u8) -> i64 {
69 let buf: *u8 = sys_mmap(FB); let n: i64 = aread(path, buf, FB)
70 if n <= 0 { return 0 - 1 }
71 var dang: i64 = 0; var i: i64 = 0
72 while i < n {
73 let pos: i64 = afind(buf, i, n, "import " as *u8)
74 if pos < 0 { i = n } else {
75 // REAL import only: "import " must be at line start (preceded by whitespace only),
76 // NOT inside a string literal or a // comment (that catches the parser's own code).
77 var atstart: i64 = 1
78 var bs: i64 = pos - 1
79 var gb: i64 = 1
80 while gb == 1 {
81 if bs < 0 { gb = 0 } else {
82 let c: i64 = buf[bs] as i64
83 if c == 10 { gb = 0 } else { if c == 32 { bs = bs - 1 } else { if c == 9 { bs = bs - 1 } else { atstart = 0; gb = 0 } } }
84 }
85 }
86 var q: i64 = pos + 7; var g: i64 = 1
87 while g == 1 { if q >= n { g = 0 } else { if buf[q] == (34 as u8) { g = 0 } else { q = q + 1 } } }
88 if q < n {
89 var s: i64 = q + 1; var o: i64 = 0; let nb: *u8 = sys_mmap(SLOT); var g2: i64 = 1
90 while g2 == 1 { if s >= n { g2 = 0 } else { if buf[s] == (34 as u8) { g2 = 0 } else { if o < SLOT - 1 { nb[o] = buf[s]; o = o + 1 } s = s + 1 } } }
91 nb[o] = 0 as u8
92 if atstart == 1 { if o > 0 { if aresolve(nb) == 0 { dang = dang + 1 } } }
93 i = s + 1
94 } else { i = n }
95 }
96 }
97 return dang
98}
99
100func main() -> i64 {
101 ap("=== nx_build_autoroot_gate: AUTO-ROOT the whole stack via the import-DAG ===\n" as *u8)
102 let fd: i64 = sys_openat_rd(ODIR)
103 if fd < 0 { ap(" ERROR: cannot open runtime/_hdl_build\n" as *u8); sys_exit(2); return 2 }
104 var total: i64 = 0; var rooted: i64 = 0; var broken: i64 = 0; var unreadable: i64 = 0; var shown: i64 = 0
105 let gbuf: *u8 = sys_mmap(65536)
106 var nread: i64 = agetdents(fd, gbuf, 65536)
107 while nread > 0 {
108 var off: i64 = 0
109 while off < nread {
110 let reclen: i64 = (gbuf[off + 16] as i64) | ((gbuf[off + 17] as i64) << 8)
111 if reclen <= 0 { off = nread } else {
112 let nm: *u8 = ((gbuf as i64) + off + 19) as *u8
113 if a_isorgan(nm) == 1 {
114 total = total + 1
115 let pth: *u8 = sys_mmap(512); ajoin("runtime/_hdl_build/" as *u8, nm, pth)
116 let d: i64 = a_dangling(pth)
117 if d < 0 { unreadable = unreadable + 1 } else {
118 if d == 0 { rooted = rooted + 1 } else {
119 broken = broken + 1
120 if shown < 12 { ap(" BROKEN (dangling import): " as *u8); ap(nm); ap("\n" as *u8); shown = shown + 1 }
121 }
122 }
123 }
124 off = off + reclen
125 }
126 }
127 nread = agetdents(fd, gbuf, 65536)
128 }
129 sys_close(fd)
130
131 ap(" scanned organs=" as *u8); apn(total)
132 ap(" IMPORT-ROOTED(rebuildable)=" as *u8); apn(rooted)
133 ap(" BROKEN(true orphans, dangling import)=" as *u8); apn(broken)
134 ap(" unreadable=" as *u8); apn(unreadable); ap("\n" as *u8)
135 ap(" => the coarse 'floating' worklist collapses to BROKEN=" as *u8); apn(broken); ap(" precise orphans (vs ~2600 unanchored)\n" as *u8)
136
137 let nc_bad: i64 = aresolve("nx_zzz_nonexistent_organ.nx" as *u8)
138 let nc_good: i64 = aresolve("nx_syscalls.nx" as *u8)
139 ap(" neg-control: resolve(bogus)=" as *u8); apn(nc_bad); ap(" (expect 0) resolve(nx_syscalls.nx)=" as *u8); apn(nc_good); ap(" (expect 1)\n" as *u8)
140
141 var ok: i64 = 1
142 if total <= 0 { ok = 0 }
143 if nc_bad != 0 { ok = 0 }
144 if nc_good != 1 { ok = 0 }
145 ap(" verdict=" as *u8)
146 if ok == 1 { ap("GREEN (whole stack scanned; import-rooted measured; orphans = the precise BROKEN set; liar-kill armed)\n" as *u8); sys_exit(0); return 0 }
147 ap("RED (scan failed or neg-control disarmed)\n" as *u8)
148 sys_exit(1); return 1
149}