nx_gate_discover_test.nx source
↩ module page · 137 lines · 6769 B
1// nx_gate_discover_test.nx -- GATE + proof for nx_gate_discover (the dynamic feed
2// for the sovereign gate runner). NO LLM, no .sh: it self-asserts a known answer
3// + sys_exit(0).
4//
5// PROVES, on the real _hdl_build/ directory:
6// (1) DISCOVERY WORKS: scanning _hdl_build/ for *_test.nx finds a plausible,
7// non-empty set of gates (verdict == NX_GD_OK).
8// (2) IT FINDS THE KNOWN GATES: the three named gates from the runner's own
9// enrolled list -- nx_superopt_test.nx, nx_rule_soundness_test.nx,
10// nx_eqsat_membership_proof_test.nx -- are all in the discovered set
11// (nx_gate_discover_find returns >= 0 for each).
12// (3) IT CAN BE THE FEED FOR THE GATE RUNNER: each discovered entry carries a
13// NUL-terminated ABSOLUTE path ending in "_test.nx", i.e. exactly the
14// shape gr_gate(testnx: *u8) consumes. We assert the path of a known gate
15// resolves to the expected absolute path string -- proving the path-join
16// produced a runnable feed, without actually running 16 gates here (that
17// is gr_gate's job; this organ proves the SET + PATHS).
18// (4) FAIL-LOUD ON IMPLAUSIBLE COUNT (verify-before-enroll, negative control):
19// scanning a directory with NO gates returns EMPTY (not OK), and a real
20// scan with the floor enforced refuses any count below NX_GD_PLAUSIBLE_FLOOR.
21// We exercise the floor branch with a deliberately-non-gate directory so
22// the IMPLAUSIBLE/EMPTY path is genuinely taken -- a floor that has never
23// fired on a real low-count input is not a guard.
24//
25// KNOWN ANSWER (FAIL LOUD), one line:
26// "<verdict> <count_ge_floor> <found_superopt> <found_soundness> <found_membership>
27// <path_ok> <neg_empty> <neg_floor_caught>"
28// = "0 1 1 1 1 1 1 1 " run=0
29// verdict 0 = NX_GD_OK; count_ge_floor 1 = count >= NX_GD_PLAUSIBLE_FLOOR;
30// the three found_* = each known gate present; path_ok 1 = a known gate's
31// absolute path == the expected runner-shape string; neg_empty 1 = scanning a
32// no-gate dir returned EMPTY; neg_floor_caught 1 = enforcing the floor on a
33// no-gate dir returned EMPTY/IMPLAUSIBLE (NOT OK).
34
35import "nx_gate_discover.nx"
36
37const HDL_DIR: *u8 = "/mnt/c/Users/elder/nishi-core/nxc2/runtime/_hdl_build"
38// A real directory that contains NO *_test.nx gates -- the negative control for
39// the fail-loud floor (verify-before-enroll). runtime/_offc holds the pinned
40// compiler binary, no gate tests.
41const NOGATE_DIR: *u8 = "/mnt/c/Users/elder/nishi-core/nxc2/_offc"
42
43const WANT_SUPEROPT: *u8 = "nx_superopt_test.nx"
44const WANT_SOUNDNESS: *u8 = "nx_rule_soundness_test.nx"
45const WANT_MEMBERSHIP: *u8 = "nx_eqsat_membership_proof_test.nx"
46
47// Expected absolute path for the superopt gate -- exactly what the runner's
48// gates[16] hardcodes. Proves our path-join produces the runner feed shape.
49const EXPECT_SUPEROPT_PATH: *u8 = "/mnt/c/Users/elder/nishi-core/nxc2/runtime/_hdl_build/nx_superopt_test.nx"
50
51func t_strlen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != 0 as u8 { n = n + 1 } return n }
52
53func t_emit_num(v: i64) -> i64 {
54 let b: *u8 = sys_mmap(28); var n: i64 = v; if n < 0 { n = 0 - n }
55 let t2: *u8 = sys_mmap(28); var t: i64 = 0
56 if n == 0 { t2[0] = 48; t = 1 }
57 while n > 0 { t2[t] = 48 + (n % 10); n = n / 10; t = t + 1 }
58 var i: i64 = 0; while i < t { b[i] = t2[t - 1 - i]; i = i + 1 }
59 b[t] = 32; sys_write(1, b, t + 1); return 0
60}
61func t_nl() -> i64 { let z: *u8 = sys_mmap(2); z[0] = 10; sys_write(1, z, 1); return 0 }
62
63func t_streq(a: *u8, b: *u8) -> i64 {
64 var i: i64 = 0
65 while a[i] != 0 as u8 {
66 if a[i] != b[i] { return 0 }
67 i = i + 1
68 }
69 if b[i] != 0 as u8 { return 0 }
70 return 1
71}
72
73func main() -> i64 {
74 // ===== (1) + (2) + (3): real discovery on _hdl_build/ =====================
75 let gl: *NxGateList = sys_mmap(NX_GATE_LIST_BYTES) as *NxGateList
76 let v: i64 = nx_gate_discover(HDL_DIR, gl, 1) // enforce the floor
77
78 // Human-readable feed dump (this IS what a consumer would iterate).
79 nx_gate_discover_print(gl)
80
81 // count >= floor?
82 var count_ge_floor: i64 = 0
83 if gl.count >= NX_GD_PLAUSIBLE_FLOOR { count_ge_floor = 1 }
84
85 // the three known gates present?
86 let i_so: i64 = nx_gate_discover_find(gl, WANT_SUPEROPT)
87 let i_sd: i64 = nx_gate_discover_find(gl, WANT_SOUNDNESS)
88 let i_mb: i64 = nx_gate_discover_find(gl, WANT_MEMBERSHIP)
89 var found_so: i64 = 0
90 var found_sd: i64 = 0
91 var found_mb: i64 = 0
92 if i_so >= 0 { found_so = 1 }
93 if i_sd >= 0 { found_sd = 1 }
94 if i_mb >= 0 { found_mb = 1 }
95
96 // path-join produced the exact runner feed shape (absolute path).
97 var path_ok: i64 = 0
98 if i_so >= 0 {
99 let p: *u8 = gl.paths[i_so] as *u8
100 if t_streq(p, EXPECT_SUPEROPT_PATH) == 1 { path_ok = 1 }
101 }
102
103 // ===== (4) NEGATIVE CONTROL: fail-loud on implausible / empty =============
104 // 4a: a real dir with NO gates returns EMPTY (floor not enforced).
105 let gl2: *NxGateList = sys_mmap(NX_GATE_LIST_BYTES) as *NxGateList
106 let v2: i64 = nx_gate_discover(NOGATE_DIR, gl2, 0)
107 var neg_empty: i64 = 0
108 if v2 == NX_GD_EMPTY { neg_empty = 1 }
109
110 // 4b: enforcing the floor on the no-gate dir must NOT return OK (it returns
111 // EMPTY here -- 0 gates -- but a partial-but-low count would return
112 // IMPLAUSIBLE; either non-OK verdict means the guard fired).
113 let gl3: *NxGateList = sys_mmap(NX_GATE_LIST_BYTES) as *NxGateList
114 let v3: i64 = nx_gate_discover(NOGATE_DIR, gl3, 1)
115 var neg_floor_caught: i64 = 0
116 if v3 != NX_GD_OK { neg_floor_caught = 1 }
117
118 // ===== emit known-answer line ============================================
119 t_emit_num(v)
120 t_emit_num(count_ge_floor)
121 t_emit_num(found_so); t_emit_num(found_sd); t_emit_num(found_mb)
122 t_emit_num(path_ok)
123 t_emit_num(neg_empty)
124 t_emit_num(neg_floor_caught)
125 t_nl()
126
127 // ===== FAIL-LOUD known-answer assertions =================================
128 if v != NX_GD_OK { sys_exit(1); return 1 } // (1) plausible non-empty discovery
129 if count_ge_floor != 1 { sys_exit(2); return 2 } // (1) count met the floor
130 if found_so != 1 { sys_exit(3); return 3 } // (2) superopt gate discovered
131 if found_sd != 1 { sys_exit(4); return 4 } // (2) rule-soundness gate discovered
132 if found_mb != 1 { sys_exit(5); return 5 } // (2) membership-proof gate discovered
133 if path_ok != 1 { sys_exit(6); return 6 } // (3) abs path == runner feed shape
134 if neg_empty != 1 { sys_exit(7); return 7 } // (4a) no-gate dir -> EMPTY
135 if neg_floor_caught != 1 { sys_exit(8); return 8 } // (4b) floor guard fired (non-OK)
136 sys_exit(0); return 0
137}