nx_surfacedetail_gate.nx source
↩ module page · 231 lines · 10955 B
1// nx_surfacedetail_gate.nx -- THE RATCHET FOR THE SURFACE-DETAIL LEVER.
2//
3// WHY THIS EXISTS. On 2026-08-23 the human generator's high-frequency detail ceiling was found to
4// be ONE PICKED CONSTANT -- PF_NB=48 in nx_profile_fit, with no override -- while the oracle holds
5// up to 4096 points per section and the consumer already held 96. Removing it moved the measured
6// ceiling (detail_head 187 -> 231, and shape RECOVERED 900 -> 923 at 1.36M triangles). Without a
7// gate that is a MEASUREMENT, not a RATCHET, and the next lane can re-introduce exactly what was
8// removed. These teeth lock the three properties that make the lever real.
9//
10// SUBJECT: the nx_profile_fit ELF, forked for real. A gate that re-derived the binning in-process
11// would test a copy of the algorithm, not the shipped organ.
12//
13// THE ANTI-VACUITY TOOTH IS THE BITE PAIR AT THE END, AND IT IS THE POINT. T2 (neutrality) and T3
14// (dev falls) both pass trivially for an organ that IGNORES the bins argument and always emits 48:
15// neutrality holds by definition, and dev-falls could pass on noise. The bite asserts the EMITTED
16// bin count TRACKS THE REQUEST -- 96 when 96 is asked for, 48 when nothing is asked for. An organ
17// that hardcoded the count back would satisfy every other tooth here and fail this one.
18//
19// ALSO LOCKED: the format carries "N <bins>" WITH the data, so a consumer can never assume a
20// resolution the producer did not emit. That is the property that let the ceiling be lifted on
21// both sides independently.
22//
23// license_tier: ORIGINAL No hw writes (Rule 26). expect_exit: 0
24import "nx_syscalls.nx"
25import "nx_gate_verdict.nx"
26import "nx_tool_run.nx"
27
28const SD_SUBJECT_DEFAULT: *u8 = "./nx_profile_fit.elf"
29const SD_ORACLE: *u8 = "knowledge/skin.nxmesh"
30const SD_SHIPPED: *u8 = "knowledge/profile_human.dat"
31
32const SD_DIR: *u8 = "/tmp/nx_surfacedetail_gate"
33const SD_OUT48: *u8 = "/tmp/nx_surfacedetail_gate/p48.dat"
34const SD_OUT96: *u8 = "/tmp/nx_surfacedetail_gate/p96.dat"
35const SD_CANON: *u8 = "/tmp/nx_surfacedetail_gate/c96.dat"
36
37const SD_CAPTURE_CAP: i64 = 262144
38const SD_FILE_CAP: i64 = 4194304
39const SD_WORD: i64 = 8
40const SD_ARGV_SLOTS: i64 = 16
41const SD_MODE_DIR: i64 = 493 // 0755
42const SD_ASCII_ZERO: i64 = 48
43const SD_ASCII_NINE: i64 = 57
44const SD_DECIMAL: i64 = 10
45const SD_BYTE_MASK: i64 = 255
46const SD_MISS: i64 = 0 - 999999
47
48// the two bin counts under test. 48 is the HISTORIC default (neutrality anchor); 96 is the
49// consumer's own declared capacity and, measured, the oracle's angular support at this station
50// density (mean_abs_dev plateaus there: 83 -> 66 -> 66 at 48 -> 96 -> 128).
51const SD_BINS_DEFAULT: i64 = 48
52const SD_BINS_RAISED: i64 = 96
53
54func sd_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
55
56// run the subject with an explicit argv vector; returns the child exit code
57func sd_run(subject: *u8, out: *u8, outlen: *i64, bins: *u8) -> i64 {
58 let av: *i64 = sys_mmap(SD_WORD*SD_ARGV_SLOTS) as *i64
59 av[0] = subject as i64
60 av[1] = SD_ORACLE as i64
61 if bins == (0 as *u8) {
62 av[2] = SD_OUT48 as i64
63 av[3] = 0
64 } else {
65 av[2] = SD_OUT96 as i64
66 av[3] = "6" as *u8 as i64
67 av[4] = SD_CANON as i64
68 av[5] = "-1" as *u8 as i64
69 av[6] = "0" as *u8 as i64
70 av[7] = "1000" as *u8 as i64
71 av[8] = "-1" as *u8 as i64
72 av[9] = bins as i64
73 av[10] = 0
74 }
75 return tr_run_capture(subject, av, out, SD_CAPTURE_CAP, outlen)
76}
77
78// find `key` in buf and read the first run of digits after it. Returns SD_MISS when absent, so a
79// missing field can never be mistaken for a zero -- the estate's own abstain-never-acquit rule.
80func sd_num_after(buf: *u8, n: i64, key: *u8) -> i64 {
81 let kl: i64 = sd_slen(key)
82 var i: i64 = 0
83 while i + kl <= n {
84 var k: i64 = 0
85 var m: i64 = 1
86 while k < kl {
87 if buf[i+k] != key[k] { m = 0 }
88 k = k + 1
89 }
90 if m == 1 {
91 var o: i64 = i + kl
92 // skip any non-digit separator the field uses (':' or ' ')
93 var guard: i64 = 0
94 while guard < 8 {
95 if o < n {
96 let c0: i64 = (buf[o] & SD_BYTE_MASK) as i64
97 if c0 >= SD_ASCII_ZERO { if c0 <= SD_ASCII_NINE { guard = 8 } }
98 if guard < 8 { o = o + 1; guard = guard + 1 }
99 } else { guard = 8 }
100 }
101 var v: i64 = 0
102 var d: i64 = 0
103 var sc: i64 = 1
104 while sc == 1 {
105 if o >= n { sc = 0 }
106 if sc == 1 {
107 let c: i64 = (buf[o] & SD_BYTE_MASK) as i64
108 if c < SD_ASCII_ZERO { sc = 0 }
109 if c > SD_ASCII_NINE { sc = 0 }
110 if sc == 1 { v = v*SD_DECIMAL + (c - SD_ASCII_ZERO); d = d + 1; o = o + 1 }
111 }
112 }
113 if d > 0 { return v }
114 }
115 i = i + 1
116 }
117 return SD_MISS
118}
119
120// byte-for-byte file compare. SIZE IS NEVER AN IDENTITY (a same-size rewrite sails through a size
121// check), so this compares every byte and the length.
122func sd_same(a: *u8, b: *u8) -> i64 {
123 let la: *i64 = sys_mmap(16) as *i64
124 let lb: *i64 = sys_mmap(16) as *i64
125 let ba: *u8 = sys_read_file(a, la)
126 let bb: *u8 = sys_read_file(b, lb)
127 if (ba as i64) == 0 { return 0 }
128 if (bb as i64) == 0 { return 0 }
129 if la[0] != lb[0] { return 0 }
130 if la[0] <= 0 { return 0 }
131 var i: i64 = 0
132 while i < la[0] {
133 if ba[i] != bb[i] { return 0 }
134 i = i + 1
135 }
136 return 1
137}
138
139// read the declared bin count straight out of an emitted prior: the "N <bins>" row.
140func sd_declared_bins(path: *u8) -> i64 {
141 let ln: *i64 = sys_mmap(16) as *i64
142 let b: *u8 = sys_read_file(path, ln)
143 if (b as i64) == 0 { return SD_MISS }
144 return sd_num_after(b, ln[0], "N " as *u8)
145}
146
147func main(argc: i64, argv: *i64) -> i64 {
148 let ctr: *i64 = gv_ctr()
149 gv_head("nx_surfacedetail gate -- the detail ceiling is DATA-DERIVED, and the derivation is proven" as *u8)
150 var subject: *u8 = SD_SUBJECT_DEFAULT
151 if argc >= 2 { subject = argv[1] as *u8 }
152 gv_puts(" subject: " as *u8)
153 gv_puts(subject)
154 gv_puts("\n\n" as *u8)
155
156 // SETUP. mkdir at setup, never teardown -- a teardown does not run when a run crashes. Every
157 // output this gate might read is removed FIRST, so each run starts from a state it established
158 // rather than one it inherited (a gate that is not idempotent lies about every run after its
159 // first -- measured on nx_rigfloor_gate, 2026-08-22).
160 sys_mkdir(SD_DIR, SD_MODE_DIR)
161 sys_unlinkat(SD_OUT48)
162 sys_unlinkat(SD_OUT96)
163 sys_unlinkat(SD_CANON)
164 var clean: i64 = 0
165 let c1: i64 = sd_declared_bins(SD_OUT48)
166 let c2: i64 = sd_declared_bins(SD_OUT96)
167 if c1 == SD_MISS { if c2 == SD_MISS { clean = 1 } }
168 gv_check("setup-outputs-absent-before-measuring (gate is idempotent)" as *u8, clean, ctr)
169
170 let cap: *u8 = sys_mmap(SD_CAPTURE_CAP)
171 let olen: *i64 = sys_mmap(SD_WORD*2) as *i64
172
173 // ---- T1: the subject runs at its historic default ----
174 let rc48: i64 = sd_run(subject, cap, olen, 0 as *u8)
175 gv_check("default-run-exits-zero" as *u8, rc48 == 0, ctr)
176 let dev48: i64 = sd_num_after(cap, olen[0], "mean_abs_dev_permil" as *u8)
177 let rows48: i64 = sd_num_after(cap, olen[0], "\x22rows\x22" as *u8)
178 gv_puts(" bins=48 rows=" as *u8); gv_num(rows48)
179 gv_puts(" mean_abs_dev_permil=" as *u8); gv_num(dev48); gv_puts("\n" as *u8)
180 // FIXTURE REACHED THE CONDITION: without rows, every number below is about an empty run.
181 var reached48: i64 = 0
182 if rows48 != SD_MISS { if rows48 > 0 { reached48 = 1 } }
183 gv_check("fixture-reached-the-condition-at-default (rows measured, not an empty run)" as *u8, reached48, ctr)
184
185 // ---- T2: NEUTRALITY. The default run must reproduce the SHIPPED prior byte-for-byte. This is
186 // the property that made removing the cap safe: same args -> same output, proven not asserted.
187 let neutral: i64 = sd_same(SD_OUT48, SD_SHIPPED)
188 gv_check("default-bins-reproduce-the-shipped-prior-BYTE-IDENTICALLY" as *u8, neutral, ctr)
189
190 // ---- T3: the raised run ----
191 let rc96: i64 = sd_run(subject, cap, olen, "96" as *u8)
192 gv_check("raised-bins-run-exits-zero" as *u8, rc96 == 0, ctr)
193 let dev96: i64 = sd_num_after(cap, olen[0], "mean_abs_dev_permil" as *u8)
194 let rows96: i64 = sd_num_after(cap, olen[0], "\x22rows\x22" as *u8)
195 gv_puts(" bins=96 rows=" as *u8); gv_num(rows96)
196 gv_puts(" mean_abs_dev_permil=" as *u8); gv_num(dev96); gv_puts("\n" as *u8)
197 var reached96: i64 = 0
198 if rows96 != SD_MISS { if rows96 > 0 { reached96 = 1 } }
199 gv_check("fixture-reached-the-condition-at-raised-bins" as *u8, reached96, ctr)
200
201 // ---- T4: THE FIT MUST IMPROVE. More angular bins describe the oracle more faithfully, so the
202 // mean absolute deviation from the fitted ellipse MUST FALL. If it does not, the extra bins are
203 // carrying nothing and the whole lever is theatre. Measured 83 -> 66.
204 var improved: i64 = 0
205 if dev48 != SD_MISS { if dev96 != SD_MISS { if dev96 < dev48 { improved = 1 } } }
206 gv_check("raising-bins-IMPROVES-the-fit (mean_abs_dev must FALL)" as *u8, improved, ctr)
207
208 // ---- T5: THE BIN COUNT TRAVELS WITH THE DATA. The consumer reads "N <bins>" from the file, so
209 // a producer that emitted a count different from what it binned would silently corrupt every
210 // consumer's stride. Assert the emitted N equals what was requested, on BOTH runs.
211 let n48: i64 = sd_declared_bins(SD_OUT48)
212 let n96: i64 = sd_declared_bins(SD_OUT96)
213 gv_puts(" emitted N: default=" as *u8); gv_num(n48)
214 gv_puts(" raised=" as *u8); gv_num(n96); gv_puts("\n" as *u8)
215 var carries: i64 = 0
216 if n48 == SD_BINS_DEFAULT { if n96 == SD_BINS_RAISED { carries = 1 } }
217 gv_check("emitted-N-travels-with-the-data-and-matches-the-request" as *u8, carries, ctr)
218
219 // ---- ANTI-VACUITY BITE. Every tooth above passes for an organ that IGNORES the bins argument
220 // and always emits 48: neutrality holds by definition and the fit could improve on noise. The
221 // bite is the discriminator -- the RAISED run must differ from the default (fires on bad), and
222 // the DEFAULT run must NOT (silent on good). An organ with the cap re-introduced emits 48 for
223 // both, so `fires` goes 0 and this tooth is the one that catches it.
224 var fires: i64 = 0
225 if n96 != SD_BINS_DEFAULT { fires = 1 }
226 var fires_on_good: i64 = 0
227 if n48 != SD_BINS_DEFAULT { fires_on_good = 1 }
228 gv_bite("neg-control-bin-count-TRACKS-THE-REQUEST-not-a-constant" as *u8, fires, fires_on_good, ctr)
229
230 return gv_verdict("NX-SURFACEDETAIL" as *u8, ctr, "the angular resolution of the measured prior is a runtime decision, and raising it provably improves the fit" as *u8)
231}