code wiki / (root) / nx_incumbent_gate.nx

nx_incumbent_gate.nx source

↩ module page · 269 lines · 18426 B

1// nx_incumbent_gate.nx -- GATE for the incumbents ruler (nx_incumbent_lib, engineshift ES28), driven IN-PROCESS on a 2// planted fixture tree under /tmp: thirteen fake organs (a dense-judge twin naming a knowledge/ asset and carrying a 3// rejection line, a cross-encoder twin, a voxel organ, nine fillers, one whose header overruns the read window) and two 4// fake assets. Proves: the index envelope (files, assets, truncation counted, corpus_complete), a rerank contract names 5// both rerank twins and neither the voxel organ nor the excluded organ, the asset named in a matched header is listed, 6// the rejection line is listed, a voxel contract names the voxel organ alone, a nonsense contract prints NONE with its 7// terms, a contract made only of words every header carries is dropped as non-discriminating, the index states 8// (FRESH, STALE by age, ABSENT, MALFORMED), and THE DECLARED ACCEPT RULE on the live tree: the search board's R0 9// contract, answered from a live index built into /tmp, names nx_dr_densejudge, embed_v1.bin and the DR-13 rejection 10// with no seat input. Every fixture asserts its own condition first. 11// license_tier: ORIGINAL Writes only under /tmp/nx_incumbent_gate. No hw writes (Rule 26). 12import "nx_syscalls.nx" 13import "nx_gatekit_lib.nx" 14import "nx_incumbent_lib.nx" 15import "nx_gate_verdict.nx" 16 17const G_ROOT: *u8 = "/tmp/nx_incumbent_gate" 18const G_RA: *u8 = "/tmp/nx_incumbent_gate/ra" 19const G_RB: *u8 = "/tmp/nx_incumbent_gate/rb" 20const G_IDXDIR: *u8 = "/tmp/nx_incumbent_gate/idx" 21const G_INDEX: *u8 = "/tmp/nx_incumbent_gate/incumbent_index.txt" 22const G_LIVE_INDEX: *u8 = "/tmp/nx_incumbent_gate/live_index.txt" 23const G_JUNK: *u8 = "/tmp/nx_incumbent_gate/junk.txt" 24const G_ABSENT: *u8 = "/tmp/nx_incumbent_gate/absent.txt" 25const G_DENSE: *u8 = "/tmp/nx_incumbent_gate/ra/nx_fake_densejudge.nx" 26const G_CROSS: *u8 = "/tmp/nx_incumbent_gate/rb/nx_fake_crossenc.nx" 27const G_VOXEL: *u8 = "/tmp/nx_incumbent_gate/ra/nx_fake_voxel.nx" 28const G_LONG: *u8 = "/tmp/nx_incumbent_gate/rb/nx_fake_longhead.nx" 29const G_ASSET1: *u8 = "/tmp/nx_incumbent_gate/idx/fake_embed_v1.bin" 30const G_ASSET2: *u8 = "/tmp/nx_incumbent_gate/idx/unrelated.tsv" 31const G_DENSE_SRC: *u8 = "// nx_fake_densejudge.nx -- TRAINED DENSE representation for the reranker judge (a sovereign fixture twin of DR-13).\n// Three measured rejections localised the ceiling to the features themselves: learned fusion over PPMI does not transfer.\n// This uses the trained dense embeddings (knowledge/index/fake_embed_v1.bin) produced by the trainer.\nimport \"nx_syscalls.nx\"\nfunc dj_fake_maxsim() -> i64 { return 0 }\n" 32const G_CROSS_SRC: *u8 = "// nx_fake_crossenc.nx -- cross-encoder rerank over the top candidates, a sovereign fixture.\n// A rerank arm that must beat the BM25 arm before it is wired.\nimport \"nx_syscalls.nx\"\nfunc ce_fake_score() -> i64 { return 0 }\n" 33const G_VOXEL_SRC: *u8 = "// nx_fake_voxel.nx -- voxel exterior flood fill with a pad, a sovereign fixture.\n// The flood seeds from the pad and the exterior cells are counted.\nimport \"nx_syscalls.nx\"\nfunc vx_fake_flood() -> i64 { return 0 }\n" 34const G_FILLER_HEAD: *u8 = "// nx_fake_f" 35const G_FILLER_MID: *u8 = ".nx -- sovereign fixture organ about " 36const G_FILLER_TAIL: *u8 = ".\nimport \"nx_syscalls.nx\"\nfunc fk_noop() -> i64 { return 0 }\n" 37const G_LONG_LINE: *u8 = "// sovereign fixture filler line with granite words to overrun the header window of the reader\n" 38const G_LONG_LINES: i64 = 220 39const G_FILLERS: i64 = 9 40const G_FIXTURE_FILES: i64 = 14 // dense twin, cross twin, voxel, prose-only, nine fillers, one long header 41const G_PROSE: *u8 = "/tmp/nx_incumbent_gate/rb/nx_fake_prose.nx" 42const G_PROSE_SRC: *u8 = "// nx_fake_prose.nx -- notes on the reranker, dense and trained, over the years, a sovereign fixture.\n// It matches exactly the bag words the dense twin matches and carries none of them in its name.\nimport \"nx_syscalls.nx\"\nfunc pr_fake_notes() -> i64 { return 0 }\n" 43const G_FIXTURE_ASSETS: i64 = 2 44const G_OUT_CAP: i64 = 16384 45const G_I64: i64 = 8 46const G_LIVE_PLAN_A: *u8 = "buildroot/knowledge/compare/search.plan" 47const G_LIVE_PLAN_B: *u8 = "knowledge/compare/search.plan" 48const G_LIVE_RUNG: *u8 = "rung|R0|" 49const G_LIVE_EXCL: *u8 = "nx_neural_reranker" 50const G_LIVE_WANT_ORGAN: *u8 = "nx_dr_densejudge" 51const G_LIVE_WANT_ASSET: *u8 = "embed_v1.bin" 52const G_LIVE_WANT_REJ: *u8 = "rejections" 53const G_LIVE_MIN_FILES: i64 = 1000 54const G_R0_TITLE: *u8 = "Neural rerank that beats BM25" 55const G_R0_SYM: *u8 = "nr_rerank" 56const G_R0_NOTE: *u8 = "A trained cross-encoder or dense reranker over the top-k" 57const G_VX_TITLE: *u8 = "Voxel exterior flood" 58const G_NONSENSE: *u8 = "Zzzq qqxx wwvv" 59const G_COMMON: *u8 = "sovereign fixture" 60 61func g_topic(k: i64) -> *u8 { 62 if k == 0 { return "lattice" as *u8 } 63 if k == 1 { return "orchard" as *u8 } 64 if k == 2 { return "granite" as *u8 } 65 if k == 3 { return "saffron" as *u8 } 66 if k == 4 { return "tundra" as *u8 } 67 if k == 5 { return "harbour" as *u8 } 68 if k == 6 { return "quartz" as *u8 } 69 if k == 7 { return "meadow" as *u8 } 70 return "cobalt" as *u8 71} 72func g_has(hay: *u8, needle: *u8) -> i64 { return inc_contains(hay, inc_slen(hay), needle) } 73// is organ name among the results 74func g_result_has(g: *i64, r: *i64, name: *u8) -> i64 { 75 let rname: *i64 = g[INC_G_R_NAME] as *i64 76 let rrow: *i64 = r[INC_R_ROW] as *i64 77 var i: i64 = 0 78 while i < r[INC_R_N] { if inc_streq(rname[rrow[i]] as *u8, name) == 1 { return 1 } i = i + 1 } 79 return 0 80} 81// a plan field copied out NUL-terminated (fields split at |) 82func g_field(line: *u8, k: i64, dst: *u8, cap: i64) -> i64 { 83 var f: i64 = 0 84 var i: i64 = 0 85 var s: i64 = 0 86 var found: i64 = 0 87 var go: i64 = 1 88 while go == 1 { 89 let b: i64 = line[i] as i64 90 if b == 0 { if f == k { found = 1 } go = 0 } 91 else { if b == INC_PIPE { if f == k { found = 1; go = 0 } else { f = f + 1; s = i + 1; i = i + 1 } } else { i = i + 1 } } 92 } 93 var o: i64 = 0 94 if found == 1 { var j: i64 = s; while j < i { if o < cap - 1 { dst[o] = line[j]; o = o + 1 } j = j + 1 } } 95 dst[o] = 0 as u8 96 return o 97} 98 99func main() -> i64 { 100 gv_head("=== nx_incumbent_gate -- a rung contract answers with its incumbents and the record's rejections, from the index, never from a seat ===" as *u8) 101 let ctr: *i64 = gv_ctr() 102 let c: *i64 = inc_conf_load() 103 gk_mkdir(G_ROOT) 104 gk_mkdir(G_RA) 105 gk_mkdir(G_RB) 106 gk_mkdir(G_IDXDIR) 107 gk_write(G_DENSE, G_DENSE_SRC) 108 gk_write(G_CROSS, G_CROSS_SRC) 109 gk_write(G_VOXEL, G_VOXEL_SRC) 110 gk_write(G_PROSE, G_PROSE_SRC) 111 gk_write(G_ASSET1, "x" as *u8) 112 gk_write(G_ASSET2, "y" as *u8) 113 let path: *u8 = sys_mmap(INC_PATH_CAP) 114 let body: *u8 = sys_mmap(INC_PATH_CAP) 115 var k: i64 = 0 116 while k < G_FILLERS { 117 var o: i64 = inc_cat(path, 0, G_RA) 118 if k >= 4 { o = inc_cat(path, 0, G_RB) } 119 o = inc_cat(path, o, "/nx_fake_f" as *u8); o = inc_catn(path, o, INC_PATH_CAP, k); o = inc_cat(path, o, ".nx" as *u8) 120 var bo: i64 = inc_cat(body, 0, G_FILLER_HEAD); bo = inc_catn(body, bo, INC_PATH_CAP, k); bo = inc_cat(body, bo, G_FILLER_MID); bo = inc_cat(body, bo, g_topic(k)); bo = inc_cat(body, bo, G_FILLER_TAIL) 121 gk_write(path, body) 122 k = k + 1 123 } 124 let longbuf: *u8 = sys_mmap((G_LONG_LINES + 2) * (inc_slen(G_LONG_LINE) + 1) + inc_slen(G_FILLER_TAIL) + 64) 125 var lo: i64 = 0 126 k = 0 127 while k < G_LONG_LINES { lo = inc_cat(longbuf, lo, G_LONG_LINE); k = k + 1 } 128 lo = inc_cat(longbuf, lo, "import \"nx_syscalls.nx\"\nfunc lh_noop() -> i64 { return 0 }\n" as *u8) 129 gk_write(G_LONG, longbuf) 130 gv_check("fixture-reached-the-condition: the long header exceeds the read window" as *u8, (lo > c[INC_C_HEAD_BYTES]) as i64, ctr) 131 gv_check_eq("fixture-reached-the-condition: the dense twin was written" as *u8, gk_exists(G_DENSE), 1, ctr) 132 // build 133 let env: *i64 = inc_index_build(c, G_RA, G_RB, G_IDXDIR, G_INDEX) 134 gv_check_eq("index-build-counts-every-fixture-source (files)" as *u8, env[INC_E_FILES], G_FIXTURE_FILES, ctr) 135 gv_check_eq("index-build-counts-every-fixture-asset" as *u8, env[INC_E_ASSETS], G_FIXTURE_ASSETS, ctr) 136 gv_check_eq("index-build-COUNTS-the-overrun-header-rather-than-cutting-it-silently (head_truncated 1)" as *u8, env[INC_E_TRUNC], 1, ctr) 137 gv_check_eq("index-build-corpus_complete-when-every-dir-listed" as *u8, env[INC_E_COMPLETE], 1, ctr) 138 gv_check("index-build-published-bytes" as *u8, (env[INC_E_BYTES] > 0) as i64, ctr) 139 gv_check_eq("index-file-exists-after-the-atomic-publish" as *u8, gk_exists(G_INDEX), 1, ctr) 140 // load 141 let now: i64 = inc_now() 142 let g: *i64 = inc_load_path(c, G_INDEX, now) 143 gv_check_eq("index-load-state-FRESH" as *u8, g[INC_G_STATE], INC_S_FRESH, ctr) 144 gv_check_eq("index-load-organ-rows" as *u8, g[INC_G_ROWS], G_FIXTURE_FILES, ctr) 145 gv_check_eq("index-load-asset-rows" as *u8, g[INC_G_ASSETS], G_FIXTURE_ASSETS, ctr) 146 gv_check_eq("index-load-head-files-agree-with-the-build" as *u8, g[INC_G_FILES], env[INC_E_FILES], ctr) 147 gv_check("index-load-postings-present" as *u8, (g[INC_G_NPOST] > 0) as i64, ctr) 148 // the rerank contract 149 let out: *u8 = sys_mmap(G_OUT_CAP) 150 var r: *i64 = inc_match(g, c, G_R0_TITLE, G_R0_SYM, G_R0_NOTE, "" as *u8) 151 inc_render(g, c, r, "R0" as *u8, out, G_OUT_CAP) 152 gv_check("rerank-contract-names-the-dense-judge-twin" as *u8, g_result_has(g, r, "nx_fake_densejudge" as *u8), ctr) 153 gv_check("rerank-contract-names-the-cross-encoder-twin" as *u8, g_result_has(g, r, "nx_fake_crossenc" as *u8), ctr) 154 gv_check_eq("neg-control-rerank-contract-does-not-name-the-voxel-organ" as *u8, g_result_has(g, r, "nx_fake_voxel" as *u8), 0, ctr) 155 gv_check_eq("neg-control-rerank-contract-does-not-name-a-filler" as *u8, g_result_has(g, r, "nx_fake_f2" as *u8), 0, ctr) 156 gv_check("rerank-contract-lists-the-asset-the-matched-header-names (fake_embed_v1.bin)" as *u8, g_has(out, "fake_embed_v1.bin" as *u8), ctr) 157 gv_check("rerank-contract-lists-the-rejection-line-the-matched-header-carries" as *u8, g_has(out, "rejections" as *u8), ctr) 158 gv_check("rerank-contract-prints-the-matched-terms (rerank)" as *u8, g_has(out, "rerank" as *u8), ctr) 159 gv_check("rerank-contract-prints-the-population-it-matched-from (matching,)" as *u8, g_has(out, " matching, " as *u8), ctr) 160 let matched_rerank: i64 = r[INC_R_MATCHED] 161 // the name tier: a contract word in an organ's NAME outranks more contract words in prose alone 162 let rrow0: *i64 = r[INC_R_ROW] as *i64 163 let rname0: *i64 = g[INC_G_R_NAME] as *i64 164 var twins_first: i64 = 0 165 if r[INC_R_N] >= 3 { 166 var a: i64 = 0 167 if inc_streq(rname0[rrow0[0]] as *u8, "nx_fake_densejudge" as *u8) == 1 { a = a + 1 } 168 if inc_streq(rname0[rrow0[0]] as *u8, "nx_fake_crossenc" as *u8) == 1 { a = a + 1 } 169 if inc_streq(rname0[rrow0[1]] as *u8, "nx_fake_densejudge" as *u8) == 1 { a = a + 1 } 170 if inc_streq(rname0[rrow0[1]] as *u8, "nx_fake_crossenc" as *u8) == 1 { a = a + 1 } 171 if a == 2 { if inc_streq(rname0[rrow0[2]] as *u8, "nx_fake_prose" as *u8) == 1 { twins_first = 1 } } 172 } 173 gv_check("rerank-contract-names-the-prose-only-organ-too (it does match)" as *u8, g_result_has(g, r, "nx_fake_prose" as *u8), ctr) 174 gv_check("name-tier: both twins (a contract word in their NAME) rank above the prose-only organ that matches more words" as *u8, twins_first, ctr) 175 // exclusion 176 r = inc_match(g, c, G_R0_TITLE, G_R0_SYM, G_R0_NOTE, "nx_fake_crossenc" as *u8) 177 gv_check_eq("the-rung's-own-organ-never-answers-its-own-contract (excluded twin absent)" as *u8, g_result_has(g, r, "nx_fake_crossenc" as *u8), 0, ctr) 178 gv_check("exclusion-keeps-the-other-incumbent" as *u8, g_result_has(g, r, "nx_fake_densejudge" as *u8), ctr) 179 // the voxel contract 180 r = inc_match(g, c, G_VX_TITLE, "vx_flood" as *u8, "seal the exterior with a pad" as *u8, "" as *u8) 181 inc_render(g, c, r, "V1" as *u8, out, G_OUT_CAP) 182 gv_check("voxel-contract-names-the-voxel-organ" as *u8, g_result_has(g, r, "nx_fake_voxel" as *u8), ctr) 183 gv_check_eq("neg-control-voxel-contract-does-not-name-the-dense-judge" as *u8, g_result_has(g, r, "nx_fake_densejudge" as *u8), 0, ctr) 184 gv_check("voxel-contract-lists-no-asset (none)" as *u8, g_has(out, "ASSETS: none" as *u8), ctr) 185 // nonsense: NONE, never a guess 186 r = inc_match(g, c, G_NONSENSE, "" as *u8, "" as *u8, "" as *u8) 187 inc_render(g, c, r, "Z9" as *u8, out, G_OUT_CAP) 188 gv_check_eq("neg-control-a-contract-matching-nothing-returns-no-results" as *u8, r[INC_R_N], 0, ctr) 189 gv_check("neg-control-and-renders-NONE-with-the-terms-tried" as *u8, g_has(out, "NONE (0 of" as *u8), ctr) 190 gv_check("neg-control-the-terms-tried-are-printed (zzzq)" as *u8, g_has(out, "zzzq" as *u8), ctr) 191 // words every header carries discriminate nothing: dropped and counted 192 r = inc_match(g, c, G_COMMON, "" as *u8, "" as *u8, "" as *u8) 193 gv_check_eq("a-term-carried-by-every-organ-is-dropped-as-non-discriminating (dropped 2)" as *u8, r[INC_R_DROPPED], 2, ctr) 194 gv_check_eq("and-yields-no-results" as *u8, r[INC_R_N], 0, ctr) 195 // index states 196 let gs: *i64 = inc_load_path(c, G_INDEX, now + c[INC_C_INDEX_MAX_AGE_S] + 1) 197 gv_check_eq("neg-control-an-index-older-than-index_max_age_s-is-STALE" as *u8, gs[INC_G_STATE], INC_S_STALE, ctr) 198 let ga: *i64 = inc_load_path(c, G_ABSENT, now) 199 gv_check_eq("neg-control-a-missing-index-is-ABSENT" as *u8, ga[INC_G_STATE], INC_S_ABSENT, ctr) 200 gk_write(G_JUNK, "org|nothing|here|no head line\n" as *u8) 201 let gm: *i64 = inc_load_path(c, G_JUNK, now) 202 gv_check_eq("neg-control-an-index-without-the-derived-head-is-MALFORMED" as *u8, gm[INC_G_STATE], INC_S_MALFORMED, ctr) 203 inc_envelope(g, out, G_OUT_CAP) 204 gv_check("the-envelope-names-the-state-and-the-counts" as *u8, g_has(out, "state=FRESH" as *u8), ctr) 205 // THE DECLARED ACCEPT RULE (engineshift ES28, declared before the build): on search.plan R0 the ruler names 206 // nx_dr_densejudge, embed_v1.bin and the DR-13 rejection with no seat input -- from a live index built into /tmp 207 let host: i64 = inc_cwd_is_nishihost() 208 var rt: *u8 = INC_ROOT_RT_B 209 var hdl: *u8 = INC_ROOT_HDL_B 210 var assets: *u8 = INC_ASSET_B 211 var planp: *u8 = G_LIVE_PLAN_B 212 if host == 1 { rt = INC_ROOT_RT_A; hdl = INC_ROOT_HDL_A; assets = INC_ASSET_A; planp = G_LIVE_PLAN_A } 213 let lenp: *i64 = sys_mmap(G_I64) as *i64 214 lenp[0] = 0 215 let plan: *u8 = sys_read_file(planp, lenp) 216 var live_ok: i64 = 0 217 if lenp[0] > 0 { let fdp: i64 = sys_openat_rd(rt); if fdp >= 0 { sys_close(fdp); live_ok = 1 } } 218 gv_check_eq("fixture-reached-the-condition: the live search plan and runtime root are readable from this CWD" as *u8, live_ok, 1, ctr) 219 var live_files: i64 = 0 220 var live_rows: i64 = 0 221 if live_ok == 1 { 222 let lenv: *i64 = inc_index_build(c, rt, hdl, assets, G_LIVE_INDEX) 223 live_files = lenv[INC_E_FILES] 224 gv_check("live-index-scans-the-whole-runtime-population (files above the floor)" as *u8, (lenv[INC_E_FILES] >= G_LIVE_MIN_FILES) as i64, ctr) 225 gv_check_eq("live-index-corpus_complete" as *u8, lenv[INC_E_COMPLETE], 1, ctr) 226 let gl: *i64 = inc_load_path(c, G_LIVE_INDEX, inc_now()) 227 live_rows = gl[INC_G_ROWS] 228 gv_check_eq("live-index-loads-FRESH" as *u8, gl[INC_G_STATE], INC_S_FRESH, ctr) 229 // the R0 row of the live plan 230 let n: i64 = lenp[0] 231 var p: i64 = 0 232 var found: i64 = 0 233 let line: *u8 = sys_mmap(n + 1) 234 while p < n { 235 let e: i64 = inc_lend(plan, n, p) 236 if found == 0 { if inc_starts((plan as i64 + p) as *u8, G_LIVE_RUNG) == 1 { 237 var j: i64 = 0 238 while p + j < e { line[j] = plan[p + j]; j = j + 1 } 239 line[j] = 0 as u8 240 found = 1 241 } } 242 p = e + 1 243 } 244 gv_check_eq("fixture-reached-the-condition: the live plan carries rung R0" as *u8, found, 1, ctr) 245 if found == 1 { 246 let title: *u8 = sys_mmap(n + 1) 247 let sym: *u8 = sys_mmap(INC_NAME_CAP) 248 let note: *u8 = sys_mmap(n + 1) 249 g_field(line, 2, title, n + 1) 250 g_field(line, 3, sym, INC_NAME_CAP) 251 g_field(line, 4, note, n + 1) 252 let rl: *i64 = inc_match(gl, c, title, sym, note, G_LIVE_EXCL) 253 inc_render(gl, c, rl, "R0" as *u8, out, G_OUT_CAP) 254 gv_check("ACCEPT-RULE live R0 names nx_dr_densejudge (as an incumbent or under PRIOR-REJECTIONS)" as *u8, g_has(out, G_LIVE_WANT_ORGAN), ctr) 255 gv_check("ACCEPT-RULE live R0 lists embed_v1.bin among its assets" as *u8, g_has(out, G_LIVE_WANT_ASSET), ctr) 256 gv_check("ACCEPT-RULE live R0 carries the DR-13 rejection line" as *u8, g_has(out, G_LIVE_WANT_REJ), ctr) 257 gv_check_eq("ACCEPT-RULE live R0 never names its own organ (nx_neural_reranker excluded)" as *u8, g_result_has(gl, rl, G_LIVE_EXCL), 0, ctr) 258 gv_check("ACCEPT-RULE live R0 answers from a population, not a handful (matched above 1)" as *u8, (rl[INC_R_MATCHED] > 1) as i64, ctr) 259 } 260 } 261 gv_values_head() 262 gv_kv("fixture_files" as *u8, env[INC_E_FILES]) 263 gv_kv("fixture_head_truncated" as *u8, env[INC_E_TRUNC]) 264 gv_kv("fixture_postings" as *u8, g[INC_G_NPOST]) 265 gv_kv("rerank_matched_organs" as *u8, matched_rerank) 266 gv_kv("live_files" as *u8, live_files) 267 gv_kv("live_rows" as *u8, live_rows) 268 return gv_verdict("nx_incumbent_gate" as *u8, ctr, "the incumbents ruler proven on a planted fixture tree and on the live tree: the index counts every source and asset and COUNTS an overrun header rather than cutting it silently, a rerank contract names both rerank twins with the asset and the rejection line their headers carry and never the voxel organ, a filler or the excluded organ, a voxel contract names the voxel organ alone, a contract matching nothing renders NONE with the terms tried, words every header carries are dropped as non-discriminating, an index is FRESH or STALE by age or ABSENT or MALFORMED and never silently trusted, and the declared accept rule holds on the live search board: R0 names nx_dr_densejudge, embed_v1.bin and the DR-13 rejection with no seat input; every fixture asserts its own condition first" as *u8) 269}