code wiki / (root) / nx_explore.nx

nx_explore.nx source

↩ module page · 189 lines · 8934 B

1// nx_explore.nx -- THE NISHI EXPLORE TEAM, RUNG R4 "MAP": query -> conclusion map. The SOVEREIGN 2// replacement for the third-party Explore *agent* (operator 2026-06-23: "dont launch an explore agent 3// build a nishi explore team capability from the hardware rung up each rung to be s class exceed"). 4// 5// What the Explore agent does: sweep many files, return the RELEVANT ones + a one-line role each -- 6// a CONCLUSION, not a dump. This organ does exactly that, sovereignly: recursive getdents64 walk 7// (R0) + per-file content scan (R2), SYNTHESIZED to ONE ROW PER FILE: "path (Nx) <first-match line>". 8// That synthesis is the exceed over raw nx_codegrep (which dumps file:line per hit) AND over the 9// Explore agent (zero egress, no spend wall, deterministic) AND over ripgrep (completes where rg 10// timed out 4x @20s on this tree this session). 11// 12// USE (dogfood): ./_offc/nx_explore.elf "<query>" -> live map over runtime/ 13// SELF-GATE: run with NO args (the nx_sov_build_run path) -> self-test: a real symbol must map to 14// >=1 file AND a byte-built absent token must map to 0 (neg-control) -> EXPLORE-MAP-GATE GREEN/run-exit=0. 15// Composes nx_codegrep's proven walk; reuses sys_getdents64 + dirent_* from nx_syscalls. license_tier: ORIGINAL 16import "nx_syscalls.nx" 17import "nx_itoa_lib.nx" // shared MSB-first emitter (zero-alloc) 18const EX_MAGIC_65536: i64 = 65536 19const EX_MAGIC_1024: i64 = 1024 20 21const EX_FBUF: i64 = 4194304 // 4 MB per-file read buffer (reused across files, like nx_codegrep) 22const EX_MAXDEPTH: i64 = 64 // JPL rule-2 bounded recursion ceiling 23 24func ex_strlen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 25func ex_puts(s: *u8) -> i64 { sys_write(1, s, ex_strlen(s)); return 0 } 26// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer 27// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the 28// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls). 29// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign. 30func ex_putn(v: i64) -> i64 { nxi_out(v); return 0 } 31 32func ex_find(buf: *u8, lo: i64, hi: i64, pat: *u8, pl: i64) -> i64 { 33 if pl == 0 { return 0 } 34 var i: i64 = lo 35 while i + pl <= hi { 36 var j: i64 = 0 37 while j < pl { if buf[i + j] != pat[j] { j = pl + 1 } else { j = j + 1 } } 38 if j == pl { return 1 } 39 i = i + 1 40 } 41 return 0 42} 43 44func ex_ends_nx(name: *u8) -> i64 { 45 let n: i64 = ex_strlen(name) 46 if n < 3 { return 0 } 47 if name[n - 3] != (46 as u8) { return 0 } // '.' 48 if name[n - 2] != (110 as u8) { return 0 } // 'n' 49 if name[n - 1] != (120 as u8) { return 0 } // 'x' 50 return 1 51} 52 53func ex_is_dot(name: *u8) -> i64 { 54 if name[0] == (46 as u8) { if name[1] == (0 as u8) { return 1 } if name[1] == (46 as u8) { if name[2] == (0 as u8) { return 1 } } } 55 return 0 56} 57 58func ex_join(dirpath: *u8, name: *u8, out: *u8) -> i64 { 59 var p: i64 = 0; var a: i64 = 0 60 while dirpath[a] != (0 as u8) { out[p] = dirpath[a]; p = p + 1; a = a + 1 } 61 out[p] = 47 as u8; p = p + 1 // '/' 62 var b: i64 = 0 63 while name[b] != (0 as u8) { out[p] = name[b]; p = p + 1; b = b + 1 } 64 out[p] = 0 as u8 65 return 0 66} 67 68// scan ONE file: return match count; capture the FIRST matching line (leading ws trimmed) into outln, len via out_len[0]. 69func ex_scan_file(path: *u8, query: *u8, ql: i64, fbuf: *u8, outln: *u8, outcap: i64, out_len: *i64) -> i64 { 70 let fd: i64 = sys_openat_rd(path) 71 if fd < 0 { return 0 } 72 let n: i64 = sys_read(fd, fbuf, EX_FBUF) 73 sys_close(fd) 74 if n <= 0 { return 0 } 75 var count: i64 = 0 76 var ls: i64 = 0 77 var i: i64 = 0 78 while i <= n { 79 var eol: i64 = 0 80 if i == n { eol = 1 } else { if fbuf[i] == (10 as u8) { eol = 1 } } 81 if eol == 1 { 82 if ex_find(fbuf, ls, i, query, ql) == 1 { 83 count = count + 1 84 if count == 1 { 85 // trim leading spaces/tabs for a clean "role" line 86 var a: i64 = ls 87 var stop: i64 = 0 88 while stop == 0 { 89 if a >= i { stop = 1 } else { 90 let c: i64 = fbuf[a] as i64 91 if c == 32 { a = a + 1 } else { if c == 9 { a = a + 1 } else { stop = 1 } } 92 } 93 } 94 var len: i64 = i - a 95 if len > outcap - 1 { len = outcap - 1 } 96 var t: i64 = 0 97 while t < len { outln[t] = fbuf[a + t]; t = t + 1 } 98 outln[len] = 0 as u8 99 out_len[0] = len 100 } 101 } 102 ls = i + 1 103 } 104 i = i + 1 105 } 106 return count 107} 108 109// recursive walk from dirpath; for each .nx file matching query, print ONE synthesized row. returns FILES matched. 110func ex_map_dir(dirpath: *u8, query: *u8, ql: i64, fbuf: *u8, depth: i64) -> i64 { 111 if depth > EX_MAXDEPTH { return 0 } 112 let dfd: i64 = sys_openat_rd(dirpath) 113 if dfd < 0 { return 0 } 114 let dbuf: *u8 = sys_mmap(EX_MAGIC_65536) 115 var files: i64 = 0 116 var go: i64 = 1 117 while go == 1 { 118 let nread: i64 = sys_getdents64(dfd, dbuf, EX_MAGIC_65536) 119 if nread <= 0 { go = 0 } else { 120 var off: i64 = 0 121 while off < nread { 122 let rec: *u8 = (dbuf as i64 + off) as *u8 123 let reclen: i64 = dirent_reclen(rec) 124 let dtype: i64 = dirent_type(rec) 125 let name: *u8 = dirent_name(rec) 126 if ex_is_dot(name) == 0 { 127 if dtype == DT_DIR { 128 let cp: *u8 = sys_mmap(EX_MAGIC_1024) 129 ex_join(dirpath, name, cp) 130 files = files + ex_map_dir(cp, query, ql, fbuf, depth + 1) 131 } else { 132 if dtype == DT_REG { 133 if ex_ends_nx(name) == 1 { 134 let cp: *u8 = sys_mmap(EX_MAGIC_1024) 135 ex_join(dirpath, name, cp) 136 let outln: *u8 = sys_mmap(512) 137 let oln: *i64 = sys_mmap(16) as *i64; oln[0] = 0 138 let c: i64 = ex_scan_file(cp, query, ql, fbuf, outln, 512, oln) 139 if c > 0 { 140 files = files + 1 141 ex_puts(" " as *u8); ex_puts(cp); ex_puts(" (" as *u8); ex_putn(c); ex_puts("x) " as *u8); ex_puts(outln); ex_puts("\n" as *u8) 142 } 143 } 144 } 145 } 146 } 147 if reclen <= 0 { off = nread } else { off = off + reclen } 148 } 149 } 150 } 151 sys_close(dfd) 152 return files 153} 154 155// public entry (the symbol the census probes): map query over root, return files matched. 156func ex_map(root: *u8, query: *u8, ql: i64, fbuf: *u8) -> i64 { 157 return ex_map_dir(root, query, ql, fbuf, 0) 158} 159 160func main(argc: i64, argv: *i64) -> i64 { 161 let fbuf: *u8 = sys_mmap(EX_FBUF + 16) 162 if argc >= 2 { 163 let q: *u8 = argv[1] as *u8 164 let ql: i64 = ex_strlen(q) 165 ex_puts("=== NISHI EXPLORE -- sovereign code map (query -> conclusion, zero egress) ===\n" as *u8) 166 ex_puts("EXPLORE-MAP query=\"" as *u8); ex_puts(q); ex_puts("\" root=runtime\n" as *u8) 167 let f: i64 = ex_map("runtime" as *u8, q, ql, fbuf) 168 ex_puts("EXPLORE-MAP files=" as *u8); ex_putn(f); ex_puts(" (one row/file; vs ripgrep timeout & vs Explore-agent egress)\n" as *u8) 169 sys_exit(0); return 0 170 } 171 // SELF-TEST / self-gate (nx_sov_build_run path: no args) 172 ex_puts("=== NISHI EXPLORE (R4 MAP) -- SELF-TEST ===\n" as *u8) 173 let qreal: *u8 = "func ex_map" as *u8 174 ex_puts("[probe] query=\"func ex_map\" (expect >=1 file):\n" as *u8) 175 let n1: i64 = ex_map("runtime" as *u8, qreal, ex_strlen(qreal), fbuf) 176 // neg-control token built BYTE-WISE so the contiguous string never appears in any source file: 177 let bog: *u8 = sys_mmap(16) 178 bog[0] = 90 as u8; bog[1] = 113 as u8; bog[2] = 88 as u8; bog[3] = 78 as u8 // Z q X N 179 bog[4] = 79 as u8; bog[5] = 80 as u8; bog[6] = 69 as u8; bog[7] = 0 as u8 // O P E \0 180 ex_puts("[probe] byte-built absent token (expect 0 files):\n" as *u8) 181 let n2: i64 = ex_map("runtime" as *u8, bog, ex_strlen(bog), fbuf) 182 ex_puts("EXPLORE-SELFTEST real=" as *u8); ex_putn(n1); ex_puts(" bogus=" as *u8); ex_putn(n2); ex_puts("\n" as *u8) 183 if n1 >= 1 { if n2 == 0 { 184 ex_puts("EXPLORE-MAP-GATE GREEN -- map finds real symbol, neg-control empty (R4 LIVE)\n" as *u8) 185 sys_exit(0); return 0 186 } } 187 ex_puts("EXPLORE-MAP-GATE RED\n" as *u8) 188 sys_exit(1); return 1 189}