code wiki / _hdl_build / nx_magic_gate.nx
nx_magic_gate.nx source
↩ module page · 380 lines · 24037 B
1// nx_magic_gate.nx -- teeth on THE RULE-11 LITERAL SCANNER (nx_magic_lib.nx), and the first teeth it has
2// ever had. It is IN-PROCESS by construction: the scanner used to live inside nx_magic.nx beside main(),
3// so the only way to reach it was to fork the deployed elf, and every mutant of a fork/exec'd subject
4// comes back NOT-REACHED. Importing the lib puts mg_scan inside this gate's own build closure, which is
5// what makes nx_gate_bite able to kill anything here.
6//
7// EVERY FIXTURE IS BUILT IN MEMORY. A gate that shares a fixture with a production beat measures the
8// beat, and this gate's subject is a scanner whose real inputs are the estate's own sources -- pointing
9// it at one would make it flake whenever a sibling seat edits that file. The single filesystem tooth
10// uses /tmp/nx_magic_gate/ and exists only because nx_magicratchet composes mg_read + mg_scan and a
11// composition nobody exercises is a composition nobody has tested.
12//
13// WHAT WOULD MAKE THIS GATE VACUOUS, AND WHY IT CANNOT BE:
14// - an implementation that counts every occurrence of the bytes `0x` passes the hex teeth and DIES on
15// the comment / string teeth, which each carry a decimal literal on the same fixture so that a
16// scanner returning nothing at all also fails;
17// - an implementation that reads only the leading `0` of `0x1ed` returns a site whose VALUE is 0 and
18// whose LENGTH is 1 -- both asserted, and the length one is not cosmetic: `nx_magic apply` REPLACES
19// slen bytes at the site, so a length of 1 rewrites `0x1ed` into `PFX_MAGIC_0x1ed` and corrupts the
20// source of whatever organ it was aimed at;
21// - an implementation that emits sites unconditionally passes T1 and dies on the threshold tooth,
22// which asserts the SAME fixture yields nothing at the default bar because 0x1ed is 493 < 1024.
23// license_tier: ORIGINAL No hw writes (Rule 26).
24import "nx_syscalls.nx"
25import "nx_gate_verdict.nx"
26import "nx_magic_lib.nx"
27
28// expected values, named so a reader can check the arithmetic rather than trust the digits
29const TG_HEX_1ED: i64 = 493 // 0x1ed == 1*256 + 14*16 + 13 == 493 == octal 0755
30const TG_HEX_1A4: i64 = 420 // 0x1a4 == 1*256 + 10*16 + 4 == 420 == octal 0644
31const TG_LEN_0X1ED: i64 = 5 // the token `0x1ed` is five bytes; apply replaces exactly these
32const TG_THR_DEFAULT: i64 = 1024 // nx_magic's default bar, above both values above ON PURPOSE
33const TG_CH_G: i64 = 103 // 'g' -- the first letter past 'f', i.e. the hex run's terminator
34const TG_CH_E: i64 = 101 // 'e' -- a letter that IS a hex digit
35const TG_ONE: i64 = 1
36const TG_TWO: i64 = 2
37const TG_ZERO_SITES: i64 = 0
38// PURPOSE NAMES (2026-09-05): the planted names.conf and the six-site fixture the resolver is judged on
39const TG_NAMES_ROWS: i64 = 5
40const TG_NAMES_SITES: i64 = 6
41const TG_ROW_I64_BYTES: i64 = 0
42const TG_ROW_BITS: i64 = 1
43const TG_ROW_CH_LF: i64 = 2
44const TG_ROW_DECIMAL: i64 = 3
45const TG_ROW_COMMA: i64 = 4
46const TG_LONG_LINE: i64 = 200 // a line longer than MG_CTX, so the window must centre
47const TG_LONG_COL: i64 = 150
48const TG_TWO_DECLS: i64 = 2 // the placeholder fixture plants exactly two declarations whose NAME carries the placeholder
49
50func eq(a: i64, b: i64) -> i64 { if a == b { return 1 } return 0 }
51func gt(a: i64, b: i64) -> i64 { if a > b { return 1 } return 0 }
52
53// One scan of one in-memory fixture. The caller owns the value/base/len/stat arrays it cares about;
54// the positional ones it does not are allocated and discarded here so a tooth reads as one line.
55func tg_scan(fx: *u8, n: i64, thr: i64, sval: *i64, sbase: *i64, slen: *i64, sstat: *i64) -> i64 {
56 let ab: i64 = MG_MAXSITES * MG_I64_BYTES
57 let a_line: *i64 = sys_mmap(ab) as *i64
58 let a_col: *i64 = sys_mmap(ab) as *i64
59 let a_ls: *i64 = sys_mmap(ab) as *i64
60 let a_le: *i64 = sys_mmap(ab) as *i64
61 return mg_scan(fx, n, thr, a_line, a_col, slen, sval, a_ls, a_le, sbase, sstat)
62}
63func tg_vals() -> *i64 { return sys_mmap(MG_MAXSITES * MG_I64_BYTES) as *i64 }
64func tg_stat() -> *i64 { return sys_mmap(MG_STAT_N * MG_I64_BYTES) as *i64 }
65func tg_write(path: *u8, s: *u8) -> i64 {
66 let fd: i64 = sys_openat_wr(path, MODE_0644)
67 if fd < 0 { return 0 - 1 }
68 let n: i64 = mg_slen(s)
69 sys_write(fd, s, n)
70 sys_close(fd)
71 return n
72}
73func tg_dump(label: *u8, v: i64) -> i64 {
74 gv_puts(" " as *u8)
75 gv_puts(label)
76 gv_puts("=" as *u8)
77 gv_num(v)
78 gv_puts("\n" as *u8)
79 return 0
80}
81
82func main(argc: i64, argv: *i64) -> i64 {
83 let ctr: *i64 = gv_ctr()
84 gv_head("nx_magic_gate -- the rule-11 literal scanner sees BOTH bases, and only in code" as *u8)
85
86 // ---- F1: a hex literal in ordinary code -------------------------------------------------------
87 let f1: *u8 = "func f() -> i64 {\n sys_mkdir(p, 0x1ed)\n}\n" as *u8
88 let v1: *i64 = tg_vals()
89 let b1: *i64 = tg_vals()
90 let l1: *i64 = tg_vals()
91 let t1: *i64 = tg_stat()
92 let n1: i64 = tg_scan(f1, mg_slen(f1), MG_FLOOR, v1, b1, l1, t1)
93 tg_dump("F1_sites" as *u8, n1)
94 tg_dump("F1_value" as *u8, v1[0])
95 tg_dump("F1_base" as *u8, b1[0])
96 tg_dump("F1_len" as *u8, l1[0])
97 tg_dump("F1_hexstat" as *u8, t1[MG_STAT_HEX])
98 gv_check("hex-literal-in-code-is-a-site-at-all" as *u8, eq(n1, TG_ONE), ctr)
99 gv_check("hex-site-value-is-493-not-the-leading-zero" as *u8, eq(v1[0], TG_HEX_1ED), ctr)
100 gv_check("hex-site-declares-base-16" as *u8, eq(b1[0], MG_BASE_HEX), ctr)
101 gv_check("hex-site-length-spans-the-0x-prefix-so-apply-cannot-corrupt" as *u8, eq(l1[0], TG_LEN_0X1ED), ctr)
102 gv_check("hex-site-is-tallied-in-the-hex-stat" as *u8, eq(t1[MG_STAT_HEX], TG_ONE), ctr)
103
104 // THE FIXTURE REACHED THE THRESHOLD COMPARISON. Same bytes, default bar: 493 < 1024, so a scanner
105 // that emits sites unconditionally is separated here from one that governs them by threshold.
106 let v1b: *i64 = tg_vals()
107 let b1b: *i64 = tg_vals()
108 let l1b: *i64 = tg_vals()
109 let t1b: *i64 = tg_stat()
110 let n1b: i64 = tg_scan(f1, mg_slen(f1), TG_THR_DEFAULT, v1b, b1b, l1b, t1b)
111 tg_dump("F1_sites_at_1024" as *u8, n1b)
112 gv_check("same-hex-fixture-yields-nothing-above-the-default-bar" as *u8, eq(n1b, TG_ZERO_SITES), ctr)
113
114 // ---- F2: hex inside a TRAILING comment, decimal on the same line's code ------------------------
115 // The decimal is load-bearing: it proves the line WAS scanned as code, so a hexstat of 0 means the
116 // comment was skipped rather than that the scanner found nothing anywhere.
117 let f2: *u8 = " mk(a, 493) // the mode is 0x1ed here\n" as *u8
118 let v2: *i64 = tg_vals()
119 let b2: *i64 = tg_vals()
120 let l2: *i64 = tg_vals()
121 let t2: *i64 = tg_stat()
122 let n2: i64 = tg_scan(f2, mg_slen(f2), MG_FLOOR, v2, b2, l2, t2)
123 tg_dump("F2_sites" as *u8, n2)
124 tg_dump("F2_hexstat" as *u8, t2[MG_STAT_HEX])
125 gv_check("trailing-comment-fixture-did-scan-its-code-half" as *u8, eq(n2, TG_ONE), ctr)
126 gv_check("trailing-comment-code-site-is-the-decimal-one" as *u8, eq(b2[0], MG_BASE_DEC), ctr)
127 gv_check("hex-inside-a-trailing-comment-is-not-counted" as *u8, eq(t2[MG_STAT_HEX], TG_ZERO_SITES), ctr)
128
129 // ---- F3: hex inside a WHOLE-LINE comment ------------------------------------------------------
130 let f3: *u8 = "// the mode is 0x1ed always\n mk(a, 493)\n" as *u8
131 let v3: *i64 = tg_vals()
132 let b3: *i64 = tg_vals()
133 let l3: *i64 = tg_vals()
134 let t3: *i64 = tg_stat()
135 let n3: i64 = tg_scan(f3, mg_slen(f3), MG_FLOOR, v3, b3, l3, t3)
136 tg_dump("F3_sites" as *u8, n3)
137 tg_dump("F3_hexstat" as *u8, t3[MG_STAT_HEX])
138 gv_check("whole-line-comment-fixture-did-scan-its-code-line" as *u8, eq(n3, TG_ONE), ctr)
139 gv_check("hex-inside-a-whole-line-comment-is-not-counted" as *u8, eq(t3[MG_STAT_HEX], TG_ZERO_SITES), ctr)
140
141 // ---- F4: hex inside a STRING literal ----------------------------------------------------------
142 let f4: *u8 = " let s: *u8 = \"0x1ed\" as *u8\n mk(a, 493)\n" as *u8
143 let v4: *i64 = tg_vals()
144 let b4: *i64 = tg_vals()
145 let l4: *i64 = tg_vals()
146 let t4: *i64 = tg_stat()
147 let n4: i64 = tg_scan(f4, mg_slen(f4), MG_FLOOR, v4, b4, l4, t4)
148 tg_dump("F4_sites" as *u8, n4)
149 tg_dump("F4_hexstat" as *u8, t4[MG_STAT_HEX])
150 gv_check("in-string-fixture-did-scan-its-code-line" as *u8, eq(n4, TG_ONE), ctr)
151 gv_check("hex-inside-a-string-literal-is-not-counted" as *u8, eq(t4[MG_STAT_HEX], TG_ZERO_SITES), ctr)
152
153 // ---- F5: BOTH notations present at once -------------------------------------------------------
154 // Two tests that each isolate one signal do not prove discrimination between them.
155 let f5: *u8 = " mk(a, 0x1ed, 420)\n" as *u8
156 let v5: *i64 = tg_vals()
157 let b5: *i64 = tg_vals()
158 let l5: *i64 = tg_vals()
159 let t5: *i64 = tg_stat()
160 let n5: i64 = tg_scan(f5, mg_slen(f5), MG_FLOOR, v5, b5, l5, t5)
161 tg_dump("F5_sites" as *u8, n5)
162 tg_dump("F5_v0" as *u8, v5[0])
163 tg_dump("F5_b0" as *u8, b5[0])
164 tg_dump("F5_v1" as *u8, v5[1])
165 tg_dump("F5_b1" as *u8, b5[1])
166 gv_check("one-line-carrying-both-bases-yields-two-sites" as *u8, eq(n5, TG_TWO), ctr)
167 gv_check("mixed-line-hex-site-keeps-its-own-value-and-base" as *u8, eq(v5[0], TG_HEX_1ED) * eq(b5[0], MG_BASE_HEX), ctr)
168 gv_check("mixed-line-decimal-site-is-undisturbed-by-the-hex-path" as *u8, eq(v5[1], TG_HEX_1A4) * eq(b5[1], MG_BASE_DEC), ctr)
169
170 // ---- F6: uppercase 0X -------------------------------------------------------------------------
171 let f6: *u8 = " mk(a, 0X1ED)\n" as *u8
172 let v6: *i64 = tg_vals()
173 let b6: *i64 = tg_vals()
174 let l6: *i64 = tg_vals()
175 let t6: *i64 = tg_stat()
176 let n6: i64 = tg_scan(f6, mg_slen(f6), MG_FLOOR, v6, b6, l6, t6)
177 tg_dump("F6_sites" as *u8, n6)
178 tg_dump("F6_value" as *u8, v6[0])
179 gv_check("uppercase-0X-with-uppercase-digits-parses-to-the-same-value" as *u8, eq(n6, TG_ONE) * eq(v6[0], TG_HEX_1ED), ctr)
180
181 // ---- F7: a bare 0x that is not a number -------------------------------------------------------
182 let f7: *u8 = " mk(a, 0x)\n" as *u8
183 let v7: *i64 = tg_vals()
184 let b7: *i64 = tg_vals()
185 let l7: *i64 = tg_vals()
186 let t7: *i64 = tg_stat()
187 let n7: i64 = tg_scan(f7, mg_slen(f7), MG_FLOOR, v7, b7, l7, t7)
188 tg_dump("F7_sites" as *u8, n7)
189 gv_check("a-bare-0x-with-no-hex-digit-invents-no-value" as *u8, eq(n7, TG_ZERO_SITES), ctr)
190
191 // ---- F8: a hex run that continues into a letter is an identifier ------------------------------
192 let f8: *u8 = " mk(a, 0x1edge)\n" as *u8
193 let v8: *i64 = tg_vals()
194 let b8: *i64 = tg_vals()
195 let l8: *i64 = tg_vals()
196 let t8: *i64 = tg_stat()
197 let n8: i64 = tg_scan(f8, mg_slen(f8), MG_FLOOR, v8, b8, l8, t8)
198 tg_dump("F8_sites" as *u8, n8)
199 gv_check("hex-run-that-hits-a-non-hex-letter-stays-an-identifier" as *u8, eq(n8, TG_ZERO_SITES), ctr)
200
201 // ---- F9: the pre-existing identifier guard still holds for the hex path -----------------------
202 let f9: *u8 = " mk(a, p0x1ed)\n" as *u8
203 let v9: *i64 = tg_vals()
204 let b9: *i64 = tg_vals()
205 let l9: *i64 = tg_vals()
206 let t9: *i64 = tg_stat()
207 let n9: i64 = tg_scan(f9, mg_slen(f9), MG_FLOOR, v9, b9, l9, t9)
208 tg_dump("F9_sites" as *u8, n9)
209 gv_check("hex-preceded-by-an-identifier-character-is-still-skipped" as *u8, eq(n9, TG_ZERO_SITES), ctr)
210
211 // ---- F10: a hex literal too wide to represent exactly -----------------------------------------
212 let f10: *u8 = " mk(a, 0xffffffffffffffffffff)\n" as *u8
213 let v10: *i64 = tg_vals()
214 let b10: *i64 = tg_vals()
215 let l10: *i64 = tg_vals()
216 let t10: *i64 = tg_stat()
217 let n10: i64 = tg_scan(f10, mg_slen(f10), MG_FLOOR, v10, b10, l10, t10)
218 tg_dump("F10_sites" as *u8, n10)
219 tg_dump("F10_oversize" as *u8, t10[MG_STAT_HEX_OVERSIZE])
220 gv_check("oversize-hex-never-becomes-a-site-apply-could-rewrite" as *u8, eq(n10, TG_ZERO_SITES), ctr)
221 gv_check("oversize-hex-is-counted-and-reported-not-silently-dropped" as *u8, eq(t10[MG_STAT_HEX_OVERSIZE], TG_ONE), ctr)
222
223 // ---- F11/F12: the pre-existing exemptions must survive the new base path ----------------------
224 let f11: *u8 = "const K: i64 = 0x1ed\n" as *u8
225 let v11: *i64 = tg_vals()
226 let b11: *i64 = tg_vals()
227 let l11: *i64 = tg_vals()
228 let t11: *i64 = tg_stat()
229 let n11: i64 = tg_scan(f11, mg_slen(f11), MG_FLOOR, v11, b11, l11, t11)
230 tg_dump("F11_sites" as *u8, n11)
231 gv_check("const-declaration-line-is-still-exempt-when-its-literal-is-hex" as *u8, eq(n11, TG_ZERO_SITES), ctr)
232
233 let f12: *u8 = " P[P_MODE] = 0x1ed\n" as *u8
234 let v12: *i64 = tg_vals()
235 let b12: *i64 = tg_vals()
236 let l12: *i64 = tg_vals()
237 let t12: *i64 = tg_stat()
238 let n12: i64 = tg_scan(f12, mg_slen(f12), MG_FLOOR, v12, b12, l12, t12)
239 tg_dump("F12_sites" as *u8, n12)
240 gv_check("named-index-table-write-is-still-exempt-when-its-literal-is-hex" as *u8, eq(n12, TG_ZERO_SITES), ctr)
241
242 // ---- F13: THE POSITIVE CONTROL -- a decimal-only source must come back clean of hex -----------
243 let f13: *u8 = " mk(a, 420, 1500)\n" as *u8
244 let v13: *i64 = tg_vals()
245 let b13: *i64 = tg_vals()
246 let l13: *i64 = tg_vals()
247 let t13: *i64 = tg_stat()
248 let n13: i64 = tg_scan(f13, mg_slen(f13), MG_FLOOR, v13, b13, l13, t13)
249 tg_dump("F13_sites" as *u8, n13)
250 tg_dump("F13_hexstat" as *u8, t13[MG_STAT_HEX])
251 gv_check("decimal-only-source-still-yields-its-decimal-sites" as *u8, eq(n13, TG_TWO), ctr)
252 gv_check("decimal-only-source-reports-zero-hex-and-zero-oversize" as *u8, eq(t13[MG_STAT_HEX], TG_ZERO_SITES) * eq(t13[MG_STAT_HEX_OVERSIZE], TG_ZERO_SITES), ctr)
253
254 // ---- F14: THE LAW THIS WHOLE CHANGE EXISTS FOR, MADE EXECUTABLE -------------------------------
255 // "THE SAME CONSTANT IN TWO BASES IS TWO CONSTANTS TO EVERY SCANNER" was banked as prose after a
256 // MODE_0644 sweep matched decimal 420 and missed a raw 0x1a4. Here the two spellings meet one
257 // scanner and collapse to ONE distinct value, which is the only form in which that law is enforced
258 // rather than remembered.
259 let f14: *u8 = " mk(a, 0x1a4, 420)\n" as *u8
260 let v14: *i64 = tg_vals()
261 let b14: *i64 = tg_vals()
262 let l14: *i64 = tg_vals()
263 let t14: *i64 = tg_stat()
264 let n14: i64 = tg_scan(f14, mg_slen(f14), MG_FLOOR, v14, b14, l14, t14)
265 let dt: *i64 = sys_mmap(MG_MAXVALS * MG_I64_BYTES) as *i64
266 let nd14: i64 = mg_distinct(v14, n14, dt, MG_MAXVALS)
267 tg_dump("F14_sites" as *u8, n14)
268 tg_dump("F14_distinct" as *u8, nd14)
269 tg_dump("F14_distinct_value" as *u8, dt[0])
270 gv_check("0x1a4-and-420-are-two-sites" as *u8, eq(n14, TG_TWO), ctr)
271 gv_check("0x1a4-and-420-collapse-to-ONE-distinct-value-420" as *u8, eq(nd14, TG_ONE) * eq(dt[0], TG_HEX_1A4), ctr)
272
273 // ---- F15: mg_read + mg_scan composed, which is exactly what nx_magicratchet does ---------------
274 sys_mkdir("/tmp/nx_magic_gate" as *u8, MODE_0755)
275 let fp: *u8 = "/tmp/nx_magic_gate/fx.nx" as *u8
276 let wrote: i64 = tg_write(fp, f1)
277 let rb: *u8 = sys_mmap(MG_CAP)
278 let rn: i64 = mg_read(fp, rb, MG_CAP - 1)
279 let v15: *i64 = tg_vals()
280 let b15: *i64 = tg_vals()
281 let l15: *i64 = tg_vals()
282 let t15: *i64 = tg_stat()
283 let n15: i64 = tg_scan(rb, rn, MG_FLOOR, v15, b15, l15, t15)
284 tg_dump("F15_wrote" as *u8, wrote)
285 tg_dump("F15_read" as *u8, rn)
286 tg_dump("F15_sites" as *u8, n15)
287 gv_check("read-back-byte-count-equals-what-was-written" as *u8, eq(rn, wrote) * gt(rn, TG_ZERO_SITES), ctr)
288 gv_check("read-then-scan-composed-finds-the-same-hex-site" as *u8, eq(n15, TG_ONE) * eq(v15[0], TG_HEX_1ED), ctr)
289
290 // ---- NEGATIVE CONTROLS ------------------------------------------------------------------------
291 gv_bite("neg-control-hexdigit-predicate-rejects-the-letter-past-f" as *u8, eq(mg_is_hexdigit(TG_CH_G), TG_ZERO_SITES), eq(mg_is_hexdigit(TG_CH_E), TG_ZERO_SITES), ctr)
292 gv_bite("neg-control-hexval-returns-a-sentinel-not-a-plausible-zero" as *u8, eq(mg_hexval(TG_CH_G), 0 - 1), eq(mg_hexval(TG_CH_E), 0 - 1), ctr)
293 gv_bite("neg-control-hex-stat-fires-on-hex-and-is-silent-on-a-clean-source" as *u8, gt(t1[MG_STAT_HEX], TG_ZERO_SITES), gt(t13[MG_STAT_HEX], TG_ZERO_SITES), ctr)
294 gv_bite("neg-control-oversize-guard-fires-only-on-the-oversize-fixture" as *u8, gt(t10[MG_STAT_HEX_OVERSIZE], TG_ZERO_SITES), gt(t1[MG_STAT_HEX_OVERSIZE], TG_ZERO_SITES), ctr)
295
296 // ---- PURPOSE NAMES (2026-09-05): a literal is named by VALUE AND CONTEXT from knowledge/magic_names.conf ----
297 let ncf: *u8 = "/tmp/nx_magic_gate/names.conf" as *u8
298 tg_write(ncf, "# planted\n8|* 8)|I64_BYTES|bytes per i64\n8|<< |BITS_PER_BYTE|a shift count\n10|as u8|CH_LF|line feed\n10|*|DECIMAL|the decimal base\n44|*|CH_COMMA|comma\n" as *u8)
299 let nrows: i64 = mg_names_load(ncf)
300 gv_check("names-conf-loaded-every-row" as *u8, eq(nrows, TG_NAMES_ROWS), ctr)
301 gv_check("names-src-reads-conf" as *u8, eq(mg_names_src(), TG_ONE), ctr)
302 gv_check("names-row-name-readable" as *u8, mg_starts(mg_nm_name_at(TG_ROW_I64_BYTES), 0, mg_slen(mg_nm_name_at(TG_ROW_I64_BYTES)), "I64_BYTES" as *u8), ctr)
303 let nsrc: *u8 = "let t: *i64 = sys_mmap(n * 8) as *i64\nv = v << 8\nif b[i] == (10 as u8) { go = 1 }\nk = k * 10\nwc(fd, 44)\nx = 7\n" as *u8
304 let nn: i64 = mg_slen(nsrc)
305 let ab2: i64 = MG_MAXSITES * MG_I64_BYTES
306 let n_line: *i64 = sys_mmap(ab2) as *i64
307 let n_col: *i64 = sys_mmap(ab2) as *i64
308 let n_len: *i64 = sys_mmap(ab2) as *i64
309 let n_val: *i64 = sys_mmap(ab2) as *i64
310 let n_ls: *i64 = sys_mmap(ab2) as *i64
311 let n_le: *i64 = sys_mmap(ab2) as *i64
312 let n_base: *i64 = sys_mmap(ab2) as *i64
313 let n_stat: *i64 = tg_stat()
314 let nsites: i64 = mg_scan(nsrc, nn, MG_FLOOR, n_line, n_col, n_len, n_val, n_ls, n_le, n_base, n_stat)
315 gv_check("fixture-reached-the-condition: six literal sites at the floor" as *u8, eq(nsites, TG_NAMES_SITES), ctr)
316 let csp: *i64 = sys_mmap(MG_I64_BYTES) as *i64
317 let cep: *i64 = sys_mmap(MG_I64_BYTES) as *i64
318 mg_ctx_window(n_ls[0], n_le[0], n_col[0], csp, cep)
319 gv_check("ctx-window-is-the-whole-line-when-it-fits" as *u8, eq(csp[0], n_ls[0]) * eq(cep[0], n_le[0]), ctr)
320 gv_check("ctx-has-finds-the-operator-beside-the-literal" as *u8, mg_ctx_has(nsrc, csp[0], cep[0], "* 8)" as *u8), ctr)
321 gv_check("neg-control-ctx-has-empty-needle-claims-nothing" as *u8, eq(mg_ctx_has(nsrc, csp[0], cep[0], "" as *u8), TG_ZERO_SITES), ctr)
322 gv_check("neg-control-ctx-has-stays-inside-the-window" as *u8, eq(mg_ctx_has(nsrc, csp[0], cep[0], "<< " as *u8), TG_ZERO_SITES), ctr)
323 gv_check("lookup-8-beside-times-eight-is-I64_BYTES" as *u8, eq(mg_name_lookup(n_val[0], nsrc, csp[0], cep[0]), TG_ROW_I64_BYTES), ctr)
324 mg_ctx_window(n_ls[1], n_le[1], n_col[1], csp, cep)
325 gv_check("lookup-8-beside-shift-is-BITS_PER_BYTE-one-value-two-names" as *u8, eq(mg_name_lookup(n_val[1], nsrc, csp[0], cep[0]), TG_ROW_BITS), ctr)
326 mg_ctx_window(n_ls[2], n_le[2], n_col[2], csp, cep)
327 gv_check("lookup-10-as-u8-first-matching-row-wins" as *u8, eq(mg_name_lookup(n_val[2], nsrc, csp[0], cep[0]), TG_ROW_CH_LF), ctr)
328 mg_ctx_window(n_ls[3], n_le[3], n_col[3], csp, cep)
329 gv_check("lookup-10-arithmetic-falls-to-the-star-row" as *u8, eq(mg_name_lookup(n_val[3], nsrc, csp[0], cep[0]), TG_ROW_DECIMAL), ctr)
330 mg_ctx_window(n_ls[4], n_le[4], n_col[4], csp, cep)
331 gv_check("lookup-44-star-row" as *u8, eq(mg_name_lookup(n_val[4], nsrc, csp[0], cep[0]), TG_ROW_COMMA), ctr)
332 mg_ctx_window(n_ls[5], n_le[5], n_col[5], csp, cep)
333 gv_check("neg-control-lookup-unlisted-value-minus-one" as *u8, eq(mg_name_lookup(n_val[5], nsrc, csp[0], cep[0]), 0 - 1), ctr)
334 mg_ctx_window(0, TG_LONG_LINE, TG_LONG_COL, csp, cep)
335 gv_check("ctx-window-centred-on-a-long-line" as *u8, eq(csp[0], TG_LONG_COL - MG_CTX_LEAD) * eq(cep[0], TG_LONG_COL - MG_CTX_LEAD + MG_CTX), ctr)
336 let nabs: i64 = mg_names_load("/tmp/nx_magic_gate/absent.conf" as *u8)
337 gv_check("neg-control-absent-conf-minus-one-and-src-zero" as *u8, eq(nabs, 0 - 1) * eq(mg_names_src(), TG_ZERO_SITES), ctr)
338 gv_check("neg-control-lookup-after-absent-conf-minus-one" as *u8, eq(mg_name_lookup(n_val[0], nsrc, n_ls[0], n_le[0]), 0 - 1), ctr)
339
340 // ---- KIND AND DERIVATION (operator 2026-09-17): a number is classed by what it IS; the remedy is where it COMES FROM ----
341 // every value below is read out of a fixture STRING, so this gate adds no typed number of its own
342 let kconf: *u8 = "unit|86400|seconds per day\nkind|sys_mmap(|BUFFER\nkind|timeout|TIME\nderive|BUFFER|size it from its input\nderive|UNIT|a constant is right here\nderive|UNKNOWN|no row claims this site\n" as *u8
343 tg_write("/tmp/nx_magic_gate/kinds.conf" as *u8, kconf)
344 let kbytes: i64 = mg_kinds_load("/tmp/nx_magic_gate/kinds.conf" as *u8)
345 gv_check("kinds-fixture-reached-the-condition: the table was read whole" as *u8, eq(kbytes, mg_slen(kconf)), ctr)
346 let kw: *u8 = sys_mmap(MG_NAMES_FIELD)
347 let kd: *u8 = sys_mmap(kbytes + 1)
348 let kline: *u8 = " let b: *u8 = sys_mmap(9999)\n let t: i64 = timeout + 7777\n let z: i64 = 5555\n" as *u8
349 let kln: i64 = mg_slen(kline)
350 let kl1e: i64 = mg_line_end(kline, kln, 0)
351 let kl2e: i64 = mg_line_end(kline, kln, kl1e + 1)
352 let kl3e: i64 = mg_line_end(kline, kln, kl2e + 1)
353 let unitv: i64 = mg_nm_int("86400" as *u8)
354 let otherv: i64 = mg_nm_int("9999" as *u8)
355 mg_kind_of("x.nx" as *u8, otherv, kline, 0, kl1e, kw, MG_NAMES_FIELD)
356 gv_check("kind-a-literal-beside-sys_mmap-is-a-BUFFER" as *u8, mg_str_eq(kw, "BUFFER" as *u8), ctr)
357 mg_kind_derive(kw, kd, kbytes + 1)
358 gv_check("derive-the-BUFFER-sentence-comes-from-the-table" as *u8, mg_str_eq(kd, "size it from its input" as *u8), ctr)
359 mg_kind_of("x.nx" as *u8, otherv, kline, kl1e + 1, kl2e, kw, MG_NAMES_FIELD)
360 gv_check("kind-a-literal-beside-timeout-is-TIME" as *u8, mg_str_eq(kw, "TIME" as *u8), ctr)
361 mg_kind_of("x.nx" as *u8, otherv, kline, kl2e + 1, kl3e, kw, MG_NAMES_FIELD)
362 gv_check("neg-control-kind-a-site-no-row-claims-is-UNKNOWN-never-a-guess" as *u8, mg_str_eq(kw, "UNKNOWN" as *u8), ctr)
363 mg_kind_of("x.nx" as *u8, unitv, kline, 0, kl1e, kw, MG_NAMES_FIELD)
364 gv_check("kind-a-unit-value-is-a-UNIT-wherever-it-stands (even beside sys_mmap)" as *u8, mg_str_eq(kw, "UNIT" as *u8), ctr)
365 mg_kind_of("anything_gate.nx" as *u8, otherv, kline, 0, kl1e, kw, MG_NAMES_FIELD)
366 gv_check("kind-a-gate-source-is-FIXTURE-before-any-needle" as *u8, mg_str_eq(kw, "FIXTURE" as *u8), ctr)
367 gv_check("neg-control-derive-a-kind-with-no-derive-row-says-nothing (TIME has none in this fixture)" as *u8, eq(mg_kind_derive("TIME" as *u8, kd, kbytes + 1), TG_ZERO_SITES), ctr)
368 // placeholder-named consts: a literal wearing a name is counted; a USE of one and a COMMENT naming one are not declarations
369 let phsrc: *u8 = "const ST_MAGIC_4096: i64 = 4096\nconst ST_PAGE: i64 = 4096 // was ST_MAGIC_4096\n let x: i64 = ST_MAGIC_4096\nconst OTHER_MAGIC_77: i64 = 77\n" as *u8
370 gv_check("placeholder-declarations-are-counted (two const NAMES carry the placeholder)" as *u8, eq(mg_placeholder_count(phsrc, mg_slen(phsrc)), TG_TWO_DECLS), ctr)
371 let phclean: *u8 = "const ST_PAGE: i64 = 4096 // was ST_MAGIC_4096\n let x: i64 = ST_MAGIC_4096\n" as *u8
372 gv_check("neg-control-placeholder-a-USE-and-a-trailing-COMMENT-are-not-declarations" as *u8, eq(mg_placeholder_count(phclean, mg_slen(phclean)), TG_ZERO_SITES), ctr)
373 let ks: *u8 = sys_mmap(kbytes + MG_NAMES_FIELD)
374 let kso1: i64 = mg_seen_add(ks, 0, "BUFFER" as *u8)
375 let kso2: i64 = mg_seen_add(ks, kso1, "TIME" as *u8)
376 let kso3: i64 = mg_seen_add(ks, kso2, "BUFFER" as *u8)
377 gv_check("seen-a-kind-is-recorded-ONCE (adding BUFFER again changes nothing, adding TIME grew the list)" as *u8, eq(kso3, kso2) * gt(kso2, kso1), ctr)
378
379 return gv_verdict("nx_magic_gate" as *u8, ctr, "subject: mg_scan in nx_magic_lib.nx; all fixtures in-memory except one /tmp round-trip" as *u8)
380}