code wiki / _hdl_build / nx_percept.nx
nx_percept.nx source
↩ module page · 653 lines · 29246 B
1// nx_percept.nx -- AL2: THE PERCEPTUAL JUDGE (operator 2026-08-09: EXCEED needs a human-confirmed
2// perceptual win; the composite critic is CONVICTED as quality -- random squares score 628 on it).
3// This organ does NOT claim absolute quality. It claims RELATIVE PERCEPTUAL POSITION: a candidate's
4// multi-scale structure statistics compared against TWO banked anchor sets --
5// GOOD = the 10 commercial reference frames (the estate's benchmark artifacts, gate-floored)
6// JUNK = the caricature controls (flat colour, random-squares mosaic)
7// score = sim(candidate, GOOD centroid) - sim(candidate, JUNK centroid), permil cosine, integer.
8// ADMISSION IS EARNED, NEVER ASSUMED: the `ladder` verb replays the HUMAN-CONFIRMED ordering from
9// 2026-08-09 (flat < squares < m2d frame < craft frame; every reference above squares, LEAVE-ONE-OUT
10// so an anchor never scores against a centroid containing itself). If the ladder does not reproduce
11// the human ordering, the judge REFUSES ITSELF -- numbers published either way, all values printed.
12// Features (integer, per NXFH1 400x240 frame): 7-bin hue-order histogram + 3-scale block-delta
13// histograms (2/8/32 px, 8 bins each) + edge-direction distribution (H/V/diag + density) + horizontal
14// same-run length histogram (8 log bins). Each family permil-normalized => 43 dims, cosine compared.
15// license_tier: ORIGINAL expect_exit: 0
16import "nx_syscalls.nx"
17const PJ_MAGIC_500000: i64 = 500000
18const PJ_MAGIC_1000000000: i64 = 1000000000
19
20const PJ_W: i64 = 400
21const PJ_H: i64 = 240
22const PJ_ND: i64 = 43
23const PJ_NANCH: i64 = 12
24const PJ_FCAP: i64 = 1048576
25
26func pw2(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
27func pn2(v: i64) -> i64 {
28 if v==0 { sys_write(1,"0" as *u8,1); return 0 }
29 var m: i64=v
30 if m<0 { sys_write(1,"-" as *u8,1); m=0-m }
31 let t: *u8=sys_mmap(32); var k: i64=0
32 while m>0 { t[k]=(48+(m%10)) as u8; m=m/10; k=k+1 }
33 let o: *u8=sys_mmap(32); var q: i64=k-1; var i: i64=0
34 while q>=0 { o[i]=t[q]; i=i+1; q=q-1 }
35 sys_write(1,o,i); return 0
36}
37func pj_abs(v: i64) -> i64 { if v<0 { return 0-v } return v }
38func pj_isq(v: i64) -> i64 {
39 if v <= 0 { return 0 }
40 var x: i64 = v
41 var y: i64 = (x+1)/2
42 while y < x { x = y; y = (x + v/x)/2 }
43 return x
44}
45func pj_hex1(c: i64) -> i64 {
46 if c >= 48 { if c <= 57 { return c-48 } }
47 if c >= 97 { if c <= 102 { return c-87 } }
48 return 0-1
49}
50
51const PJ_MAGIC_LEN: i64 = 6 // the six bytes of "NXFH1 " including its separating space
52const PJ_ASCII_0: i64 = 48
53const PJ_ASCII_9: i64 = 57
54const PJ_DEC_BASE: i64 = 10
55const PJ_WH_BYTES: i64 = 16 // two i64 out-slots, page-rounded by mmap
56const PJ_HDR_READ: i64 = 64 // the header line settles the question; never read the body for it
57const PJ_HDR_MIN: i64 = 10 // shortest possible "NXFH1 w h" + newline
58const PJ_EXIT_UNJUDGED: i64 = 4 // NOT a low score and NOT a failure: NO score
59
60// ★THE HEADER DECLARES THE SIZE AND THIS ORGAN USED TO IGNORE IT ENTIRELY.
61// pj_load skips line 1 and then reads EXACTLY PJ_W*PJ_H hex triplets out of the body. For a frame
62// LARGER than 400x240 that is a silent PREFIX -- for a 1920x1200 capture, the top 50 rows -- scored
63// against 400x240 anchors and published as a confident number about the whole image. Its only guard
64// was a BYTE-COUNT floor, which a larger frame passes trivially.
65// MEASURED 2026-08-28: nothing in the estate checked the declared dimensions on this path.
66// ★A JUDGE THAT REPORTS A CONFIDENT NUMBER ABOUT A WINDOW OF ITS INPUT IS WORSE THAN ONE THAT
67// REFUSES, BECAUSE THE NUMBER IS AUTHORITATIVE AND WRONG -- and it makes every quality improvement
68// above the ceiling unmeasurable and every regression there invisible.
69// ⚠THIS IS A CEILING, NOT A BUFFER TO WIDEN. The banked GOOD/JUNK anchor sets are themselves 400x240
70// (verified: all twelve anchors plus both ladder frames declare "NXFH1 400 240"), so a candidate at
71// another extent would be compared against anchors of a different subject. Raising PJ_W/PJ_H means
72// re-banking every anchor, which is a deliberate project, not a constant edit.
73// SAFE BY CONSTRUCTION for every existing caller: each shipped anchor already declares 400x240, so
74// this check refuses only frames that were previously being read as prefixes.
75// out[0]=w out[1]=h are written EITHER WAY, so a caller can NAME the mismatch instead of emitting a
76// bare refusal that reads like an unreadable file.
77func pj_hdr_wh(b: *u8, n: i64, out: *i64) -> i64 {
78 out[0] = 0
79 out[1] = 0
80 if n < PJ_HDR_MIN { return 0 }
81 let mg: *u8 = "NXFH1 " as *u8
82 var k: i64 = 0
83 var m: i64 = 1
84 while k < PJ_MAGIC_LEN {
85 if b[k] != mg[k] { m = 0 }
86 k = k + 1
87 }
88 if m == 0 { return 0 }
89 var p: i64 = PJ_MAGIC_LEN
90 var w: i64 = 0
91 var go: i64 = 1
92 while go == 1 {
93 if p >= n { go = 0 }
94 if go == 1 {
95 let c: i64 = b[p] as i64
96 var dg: i64 = 0
97 if c >= PJ_ASCII_0 { if c <= PJ_ASCII_9 { dg = 1 } }
98 if dg == 1 { w = w*PJ_DEC_BASE + (c - PJ_ASCII_0); p = p + 1 }
99 if dg == 0 { go = 0 }
100 }
101 }
102 p = p + 1
103 var h: i64 = 0
104 var go2: i64 = 1
105 while go2 == 1 {
106 if p >= n { go2 = 0 }
107 if go2 == 1 {
108 let c2: i64 = b[p] as i64
109 var dg2: i64 = 0
110 if c2 >= PJ_ASCII_0 { if c2 <= PJ_ASCII_9 { dg2 = 1 } }
111 if dg2 == 1 { h = h*PJ_DEC_BASE + (c2 - PJ_ASCII_0); p = p + 1 }
112 if dg2 == 0 { go2 = 0 }
113 }
114 }
115 out[0] = w
116 out[1] = h
117 if w != PJ_W { return 0 }
118 if h != PJ_H { return 0 }
119 return 1
120}
121
122// Read ONLY the header line of a capture. Sizing a buffer from the file would pull a multi-megabyte
123// frame into memory to answer a question its first line settles.
124func pj_hdr_of_file(path: *u8, out: *i64) -> i64 {
125 out[0] = 0
126 out[1] = 0
127 let b: *u8 = sys_mmap(PJ_HDR_READ)
128 let fd: i64 = sys_openat_rd(path)
129 if fd < 0 { sys_munmap(b, PJ_HDR_READ); return 0 }
130 let got: i64 = sys_read(fd, b, PJ_HDR_READ)
131 sys_close(fd)
132 var r: i64 = 0
133 if got > 0 { r = pj_hdr_wh(b, got, out) }
134 sys_munmap(b, PJ_HDR_READ)
135 return r
136}
137
138// load an NXFH1 400x240 frame into rgb arrays. returns 1 ok / 0 refuse.
139func pj_load(path: *u8, rr: *i64, gg: *i64, bb: *i64) -> i64 {
140 let lp: *i64 = sys_mmap(8) as *i64
141 lp[0]=0
142 let b: *u8 = sys_read_file(path, lp)
143 if (b as i64)==0 { return 0 }
144 let n: i64 = lp[0]
145 if n < PJ_MAGIC_500000 { return 0 }
146 // ★DEFENCE IN DEPTH: refuse a frame whose DECLARED extent is not this judge's. The byte-count floor
147 // above only catches frames that are too SMALL; a larger one sails past it and is then read as a
148 // prefix. Callers that need to NAME the mismatch call pj_hdr_of_file first (the judge verb does);
149 // this guard exists so no future caller can reintroduce the silent-prefix path by forgetting to.
150 let hw: *i64 = sys_mmap(PJ_WH_BYTES) as *i64
151 let dimok: i64 = pj_hdr_wh(b, n, hw)
152 sys_munmap(hw as *u8, PJ_WH_BYTES)
153 if dimok == 0 { return 0 }
154 var i: i64 = 0
155 while i < n { if b[i]==(10 as u8) { i = i + 1 } else { i = i + PJ_MAGIC_1000000000 } }
156 var p: i64 = 0
157 while b[p]!=(10 as u8) { p=p+1 }
158 p = p + 1
159 var px: i64 = 0
160 while px < PJ_W*PJ_H {
161 if b[p]==(10 as u8) { p=p+1 }
162 let h0: i64 = pj_hex1(b[p] as i64)
163 let h1: i64 = pj_hex1(b[p+1] as i64)
164 let h2: i64 = pj_hex1(b[p+2] as i64)
165 let h3: i64 = pj_hex1(b[p+3] as i64)
166 let h4: i64 = pj_hex1(b[p+4] as i64)
167 let h5: i64 = pj_hex1(b[p+5] as i64)
168 if h0 < 0 { return 0 }
169 if h1 < 0 { return 0 }
170 if h2 < 0 { return 0 }
171 if h3 < 0 { return 0 }
172 if h4 < 0 { return 0 }
173 if h5 < 0 { return 0 }
174 rr[px] = h0*16+h1
175 gg[px] = h2*16+h3
176 bb[px] = h4*16+h5
177 p = p + 6
178 px = px + 1
179 }
180 return 1
181}
182
183func pj_norm(f: *i64, from: i64, cnt: i64) -> i64 {
184 var s: i64 = 0
185 var i: i64 = from
186 while i < from+cnt { s = s + f[i]; i = i + 1 }
187 if s <= 0 { return 0 }
188 i = from
189 while i < from+cnt { f[i] = f[i]*1000/s; i = i + 1 }
190 return 0
191}
192
193// extract the 43-dim feature vector
194func pj_feat(rr: *i64, gg: *i64, bb: *i64, f: *i64) -> i64 {
195 var i: i64 = 0
196 while i < PJ_ND { f[i]=0; i=i+1 }
197 let lum: *i64 = sys_mmap(PJ_W*PJ_H*8) as *i64
198 i = 0
199 while i < PJ_W*PJ_H { lum[i] = (rr[i]*3 + gg[i]*6 + bb[i]) / 10; i = i + 1 }
200 // F1 hue-order histogram: dims 0..6 (6 channel orderings + gray)
201 i = 0
202 while i < PJ_W*PJ_H {
203 let r: i64 = rr[i]
204 let g: i64 = gg[i]
205 let b: i64 = bb[i]
206 var mx: i64 = r
207 if g > mx { mx = g }
208 if b > mx { mx = b }
209 var mn: i64 = r
210 if g < mn { mn = g }
211 if b < mn { mn = b }
212 var bin: i64 = 6
213 if mx - mn >= 24 {
214 bin = 0
215 if r >= g { if g >= b { bin = 0 } }
216 if r >= b { if b > g { bin = 1 } }
217 if g > r { if r >= b { bin = 2 } }
218 if g >= b { if b > r { bin = 3 } }
219 if b > g { if g >= r { bin = 4 } }
220 if b > r { if r > g { bin = 5 } }
221 }
222 f[bin] = f[bin] + 1
223 i = i + 1
224 }
225 pj_norm(f, 0, 7)
226 // F2 block-delta histograms at 2/8/32 px: dims 7..14, 15..22, 23..30
227 var sc: i64 = 0
228 while sc < 3 {
229 var bs: i64 = 2
230 if sc == 1 { bs = 8 }
231 if sc == 2 { bs = 32 }
232 let bw: i64 = PJ_W/bs
233 let bh: i64 = PJ_H/bs
234 let bm: *i64 = sys_mmap(bw*bh*8) as *i64
235 var by: i64 = 0
236 while by < bh {
237 var bx: i64 = 0
238 while bx < bw {
239 var s2: i64 = 0
240 var yy: i64 = 0
241 while yy < bs {
242 var xx: i64 = 0
243 while xx < bs { s2 = s2 + lum[(by*bs+yy)*PJ_W + bx*bs+xx]; xx = xx + 1 }
244 yy = yy + 1
245 }
246 bm[by*bw+bx] = s2/(bs*bs)
247 bx = bx + 1
248 }
249 by = by + 1
250 }
251 let base: i64 = 7 + sc*8
252 by = 0
253 while by < bh {
254 var bx2: i64 = 0
255 while bx2 < bw {
256 if bx2+1 < bw {
257 var d: i64 = pj_abs(bm[by*bw+bx2+1]-bm[by*bw+bx2]) / 8
258 if d > 7 { d = 7 }
259 f[base+d] = f[base+d] + 1
260 }
261 if by+1 < bh {
262 var d2: i64 = pj_abs(bm[(by+1)*bw+bx2]-bm[by*bw+bx2]) / 8
263 if d2 > 7 { d2 = 7 }
264 f[base+d2] = f[base+d2] + 1
265 }
266 bx2 = bx2 + 1
267 }
268 by = by + 1
269 }
270 pj_norm(f, base, 8)
271 sc = sc + 1
272 }
273 // F3 edge directions: dims 31..33 (H/V/diag) + 34 edge density permil
274 var ne: i64 = 0
275 var y3: i64 = 0
276 while y3 < PJ_H-1 {
277 var x3: i64 = 0
278 while x3 < PJ_W-1 {
279 let dx3: i64 = pj_abs(lum[y3*PJ_W+x3+1]-lum[y3*PJ_W+x3])
280 let dy3: i64 = pj_abs(lum[(y3+1)*PJ_W+x3]-lum[y3*PJ_W+x3])
281 if dx3+dy3 > 60 {
282 ne = ne + 1
283 var db: i64 = 33
284 if dx3 > 2*dy3 { db = 31 }
285 if dy3 > 2*dx3 { db = 32 }
286 f[db] = f[db] + 1
287 }
288 x3 = x3 + 1
289 }
290 y3 = y3 + 1
291 }
292 pj_norm(f, 31, 3)
293 f[34] = ne*1000/(PJ_W*PJ_H)
294 // F4 horizontal same-run lengths: dims 35..42 (log2 bins)
295 var y4: i64 = 0
296 while y4 < PJ_H {
297 var run: i64 = 1
298 var x4: i64 = 1
299 while x4 < PJ_W {
300 let i0: i64 = y4*PJ_W+x4-1
301 let i1: i64 = y4*PJ_W+x4
302 var same: i64 = 1
303 if pj_abs(rr[i1]-rr[i0]) > 12 { same = 0 }
304 if pj_abs(gg[i1]-gg[i0]) > 12 { same = 0 }
305 if pj_abs(bb[i1]-bb[i0]) > 12 { same = 0 }
306 if same == 1 { run = run + 1 }
307 if same == 0 {
308 var lb: i64 = 0
309 var rv: i64 = run
310 while rv > 1 { rv = rv/2; lb = lb + 1 }
311 if lb > 7 { lb = 7 }
312 f[35+lb] = f[35+lb] + 1
313 run = 1
314 }
315 x4 = x4 + 1
316 }
317 var lb2: i64 = 0
318 var rv2: i64 = run
319 while rv2 > 1 { rv2 = rv2/2; lb2 = lb2 + 1 }
320 if lb2 > 7 { lb2 = 7 }
321 f[35+lb2] = f[35+lb2] + 1
322 y4 = y4 + 1
323 }
324 pj_norm(f, 35, 8)
325 return 0
326}
327
328// permil cosine similarity between two 43-dim vectors
329func pj_cos(a: *i64, b: *i64) -> i64 {
330 var dot: i64 = 0
331 var na: i64 = 0
332 var nb: i64 = 0
333 var i: i64 = 0
334 while i < PJ_ND {
335 dot = dot + a[i]*b[i]
336 na = na + a[i]*a[i]
337 nb = nb + b[i]*b[i]
338 i = i + 1
339 }
340 let d: i64 = pj_isq(na) * pj_isq(nb)
341 if d <= 0 { return 0 }
342 return dot*1000/d
343}
344
345func pj_featfile(path: *u8, f: *i64) -> i64 {
346 let rr: *i64 = sys_mmap(PJ_W*PJ_H*8) as *i64
347 let gg: *i64 = sys_mmap(PJ_W*PJ_H*8) as *i64
348 let bb: *i64 = sys_mmap(PJ_W*PJ_H*8) as *i64
349 if pj_load(path, rr, gg, bb) == 0 { return 0 }
350 pj_feat(rr, gg, bb, f)
351 return 1
352}
353
354// CENTROIDS, EXTRACTED so the scorer and the family decomposition below cannot disagree about what the
355// GOOD and JUNK centroids ARE. Byte-for-byte the arithmetic that used to sit inside pj_score, and the
356// neutrality is CHECKABLE ON REAL DATA rather than asserted: `ladder` must still print exactly
357// flat=-342 squares=-167 m2d=283 craft=196 after this extraction.
358func pj_centroids(av: *i64, skip: i64, gc: *i64, jc: *i64) -> i64 {
359 var d2: i64 = 0
360 while d2 < PJ_ND {
361 var sg: i64 = 0
362 var ng: i64 = 0
363 var ai: i64 = 0
364 while ai < 10 {
365 if ai != skip { sg = sg + av[ai*PJ_ND+d2]; ng = ng + 1 }
366 ai = ai + 1
367 }
368 gc[d2] = sg/ng
369 jc[d2] = (av[10*PJ_ND+d2] + av[11*PJ_ND+d2])/2
370 d2 = d2 + 1
371 }
372 return 0
373}
374
375// FAMILY-SCOPED COSINE: pj_cos restricted to one contiguous dim range, so a disagreement can be
376// ATTRIBUTED to a feature family instead of asserted about all 43 dims at once. The ladder could say
377// the judge contradicts the human and nothing could say WHICH PART OF THE JUDGE did it.
378func pj_cos_range(a: *i64, b: *i64, lo: i64, hi: i64) -> i64 {
379 var dot: i64 = 0
380 var na: i64 = 0
381 var nb: i64 = 0
382 var i: i64 = lo
383 while i < hi {
384 dot = dot + a[i]*b[i]
385 na = na + a[i]*a[i]
386 nb = nb + b[i]*b[i]
387 i = i + 1
388 }
389 let d: i64 = pj_isq(na) * pj_isq(nb)
390 if d <= 0 { return 0 }
391 return dot*1000/d
392}
393
394// FAMILY-MASKED COSINE: pj_cos over every dim OUTSIDE [lo, hi). The complement of pj_cos_range, and the
395// arithmetic ladderx needs to ask what the judge would say if one family were not in the vector at all.
396func pj_cos_excl(a: *i64, b: *i64, lo: i64, hi: i64) -> i64 {
397 var dot: i64 = 0
398 var na: i64 = 0
399 var nb: i64 = 0
400 var i: i64 = 0
401 while i < PJ_ND {
402 var use: i64 = 0
403 if i < lo { use = 1 }
404 if i >= hi { use = 1 }
405 if use == 1 {
406 dot = dot + a[i]*b[i]
407 na = na + a[i]*a[i]
408 nb = nb + b[i]*b[i]
409 }
410 i = i + 1
411 }
412 let d: i64 = pj_isq(na) * pj_isq(nb)
413 if d <= 0 { return 0 }
414 return dot*1000/d
415}
416
417// the judge's own score with one family masked out. lo==hi==0 masks NOTHING and must therefore reproduce
418// pj_score exactly -- that identity is ladderx's control row and is the reason the control is printed.
419func pj_score_excl(f: *i64, av: *i64, lo: i64, hi: i64) -> i64 {
420 let gc: *i64 = sys_mmap(PJ_ND*8) as *i64
421 let jc: *i64 = sys_mmap(PJ_ND*8) as *i64
422 pj_centroids(av, 0-1, gc, jc)
423 return pj_cos_excl(f, gc, lo, hi) - pj_cos_excl(f, jc, lo, hi)
424}
425
426// judge score of vector f against GOOD centroid (skip index `skip` for leave-one-out; -1 = none)
427func pj_score(f: *i64, av: *i64, skip: i64) -> i64 {
428 let gc: *i64 = sys_mmap(PJ_ND*8) as *i64
429 let jc: *i64 = sys_mmap(PJ_ND*8) as *i64
430 pj_centroids(av, skip, gc, jc)
431 return pj_cos(f, gc) - pj_cos(f, jc)
432}
433
434func pj_anchor_path(i: i64) -> *u8 {
435 if i == 0 { return "knowledge/breeders1.nxfh" as *u8 }
436 if i == 1 { return "knowledge/veloren1.nxfh" as *u8 }
437 if i == 2 { return "knowledge/ref_madisland.nxfh" as *u8 }
438 if i == 3 { return "knowledge/ref_carnal.nxfh" as *u8 }
439 if i == 4 { return "knowledge/ref_dik.nxfh" as *u8 }
440 if i == 5 { return "knowledge/ref_vrhot.nxfh" as *u8 }
441 if i == 6 { return "knowledge/mgi1.nxfh" as *u8 }
442 if i == 7 { return "knowledge/oplc1.nxfh" as *u8 }
443 if i == 8 { return "knowledge/cm3d21.nxfh" as *u8 }
444 if i == 9 { return "knowledge/vam1.nxfh" as *u8 }
445 if i == 10 { return "knowledge/flat_control.nxfh" as *u8 }
446 return "knowledge/squares_control.nxfh" as *u8
447}
448
449func main(argc: i64, argv: *i64) -> i64 {
450 let av: *i64 = sys_mmap(PJ_NANCH*PJ_ND*8) as *i64
451 var ai: i64 = 0
452 while ai < PJ_NANCH {
453 if pj_featfile(pj_anchor_path(ai), ((av as i64) + ai*PJ_ND*8) as *i64) == 0 {
454 pw2("PERCEPT RED: anchor unreadable: " as *u8)
455 pw2(pj_anchor_path(ai))
456 pw2("\x0averdict=RED\x0a" as *u8)
457 return 1
458 }
459 ai = ai + 1
460 }
461 // LADDERX: THE LADDER RE-RUN WITH ONE FEATURE FAMILY MASKED OUT, FOUR TIMES, PLUS THE UNMASKED CONTROL.
462 // This changes NO score the judge publishes -- it is an instrument that asks WHICH FAMILY the L3
463 // disagreement lives in, by measurement instead of by argument. ALL FIVE ROWS PRINT, ALWAYS: handing a
464 // reader only the mask that produces the desired ordering would be choosing the experiment after seeing
465 // the result, and if SEVERAL masks flip L3 then the evidence for any one of them is correspondingly
466 // weaker and the output must show that rather than hide it.
467 // * A MASK THAT FLIPS THE VERDICT IS A HYPOTHESIS ABOUT A CONFOUND, NEVER A LICENCE TO DROP THE FAMILY:
468 // dropping a family because it disagrees is loosening the gate to flatter the number.
469 if argc >= 2 {
470 let a0: *u8 = argv[1] as *u8
471 if a0[0]==(120 as u8) {
472 let fmx: *i64 = sys_mmap(PJ_ND*8) as *i64
473 let fcx: *i64 = sys_mmap(PJ_ND*8) as *i64
474 if pj_featfile("knowledge/nx_m2d_frame.nxfh" as *u8, fmx) == 0 { pw2("PERCEPT RED: m2d frame unreadable\x0averdict=RED\x0a" as *u8); return 1 }
475 if pj_featfile("knowledge/craft1.nxfh" as *u8, fcx) == 0 { pw2("PERCEPT RED: craft frame unreadable\x0averdict=RED\x0a" as *u8); return 1 }
476 pw2("=== nx_percept ladderx: L3 (m2d < craft) with ONE FAMILY MASKED OUT, all five rows printed ===\x0a" as *u8)
477 var mi: i64 = 0
478 var flips: i64 = 0
479 while mi < 5 {
480 var lo: i64 = 0
481 var hi: i64 = 0
482 if mi == 1 { lo = 0; hi = 7 }
483 if mi == 2 { lo = 7; hi = 31 }
484 if mi == 3 { lo = 31; hi = 35 }
485 if mi == 4 { lo = 35; hi = 43 }
486 let sm: i64 = pj_score_excl(fmx, av, lo, hi)
487 let sc: i64 = pj_score_excl(fcx, av, lo, hi)
488 pw2(" masked=" as *u8)
489 if mi == 0 { pw2("none(control)" as *u8) }
490 if mi == 1 { pw2("hue" as *u8) }
491 if mi == 2 { pw2("blockdelta" as *u8) }
492 if mi == 3 { pw2("edge" as *u8) }
493 if mi == 4 { pw2("runlength" as *u8) }
494 pw2(" m2d=" as *u8); pn2(sm)
495 pw2(" craft=" as *u8); pn2(sc)
496 pw2(" L3=" as *u8)
497 if sm < sc { pw2("GREEN" as *u8) if mi > 0 { flips = flips + 1 } } else { pw2("RED" as *u8) }
498 pw2("\x0a" as *u8)
499 mi = mi + 1
500 }
501 pw2(" masks_that_flip_L3=" as *u8); pn2(flips)
502 pw2(" of 4 -- ONE is a located confound, SEVERAL is a weak signal, ZERO means no single family explains it\x0a" as *u8)
503 return 0
504 }
505 }
506 // judge <path>
507 if argc >= 3 {
508 let a1: *u8 = argv[1] as *u8
509 // ANCHORS: THE PER-ANCHOR DECOMPOSITION. A SCORE THAT CANNOT BE DECOMPOSED CANNOT BE ARGUED WITH.
510 // `ladder` can say the judge contradicts the human ordering and NOTHING could say why, so L3 has
511 // stood RED as a bare verdict. This prints the cosine to EVERY banked anchor with its class, which
512 // is what separates the two candidate explanations: a SUBJECT MISMATCH (the GOOD centroid is 9/10
513 // commercial CHARACTER renders, so a world/terrain frame is far from it for a reason that is not
514 // quality) from a genuine quality disagreement. Those two have opposite remedies, and guessing
515 // between them is how a gate gets loosened to flatter a number.
516 // Read-only and strictly additive: no existing verb, path or number changes.
517 // FAMILIES: the same decomposition ONE LEVEL DEEPER -- per FEATURE FAMILY, cosine to the GOOD and
518 // JUNK centroids and their difference. `anchors` says WHICH ANCHOR pulls a frame; this says WHICH
519 // PART OF THE 43-DIM VECTOR does the pulling, and that is exactly the difference between two
520 // remedies that look identical from a score: RE-BANK AN ANCHOR, versus THE FEATURE SET CANNOT SEE
521 // WHAT THE HUMAN SEES. Layout is fixed by pj_feat and read from its own comments rather than
522 // guessed: hue 0..6, block-delta 7..30 (three scales x 8 bins), edge 31..34, runlength 35..42.
523 if a1[0]==(102 as u8) {
524 let cff: *i64 = sys_mmap(PJ_ND*8) as *i64
525 if pj_featfile(argv[2] as *u8, cff) == 0 {
526 pw2("PERCEPT RED: candidate unreadable\x0averdict=RED\x0a" as *u8)
527 return 1
528 }
529 let gcf: *i64 = sys_mmap(PJ_ND*8) as *i64
530 let jcf: *i64 = sys_mmap(PJ_ND*8) as *i64
531 pj_centroids(av, 0-1, gcf, jcf)
532 pw2("{\x22organ\x22:\x22nx_percept\x22,\x22verb\x22:\x22families\x22,\x22families\x22:[" as *u8)
533 var fi: i64 = 0
534 while fi < 4 {
535 var lo: i64 = 0
536 var hi: i64 = 7
537 if fi == 1 { lo = 7; hi = 31 }
538 if fi == 2 { lo = 31; hi = 35 }
539 if fi == 3 { lo = 35; hi = 43 }
540 if fi > 0 { pw2("," as *u8) }
541 pw2("{\x22family\x22:\x22" as *u8)
542 if fi == 0 { pw2("hue" as *u8) }
543 if fi == 1 { pw2("blockdelta" as *u8) }
544 if fi == 2 { pw2("edge" as *u8) }
545 if fi == 3 { pw2("runlength" as *u8) }
546 pw2("\x22,\x22dims\x22:" as *u8); pn2(hi - lo)
547 let cg: i64 = pj_cos_range(cff, gcf, lo, hi)
548 let cj: i64 = pj_cos_range(cff, jcf, lo, hi)
549 pw2(",\x22cos_good\x22:" as *u8); pn2(cg)
550 pw2(",\x22cos_junk\x22:" as *u8); pn2(cj)
551 pw2(",\x22diff\x22:" as *u8); pn2(cg - cj)
552 pw2("}" as *u8)
553 fi = fi + 1
554 }
555 pw2("]}\x0a" as *u8)
556 return 0
557 }
558 if a1[0]==(97 as u8) {
559 let caf: *i64 = sys_mmap(PJ_ND*8) as *i64
560 if pj_featfile(argv[2] as *u8, caf) == 0 {
561 pw2("PERCEPT RED: candidate unreadable\x0averdict=RED\x0a" as *u8)
562 return 1
563 }
564 pw2("{\x22organ\x22:\x22nx_percept\x22,\x22verb\x22:\x22anchors\x22,\x22sims\x22:[" as *u8)
565 var qi: i64 = 0
566 while qi < PJ_NANCH {
567 if qi > 0 { pw2("," as *u8) }
568 pw2("{\x22anchor\x22:\x22" as *u8); pw2(pj_anchor_path(qi))
569 pw2("\x22,\x22class\x22:\x22" as *u8)
570 if qi < 10 { pw2("GOOD" as *u8) } else { pw2("JUNK" as *u8) }
571 pw2("\x22,\x22cos\x22:" as *u8)
572 pn2(pj_cos(caf, ((av as i64) + qi*PJ_ND*8) as *i64))
573 pw2("}" as *u8)
574 qi = qi + 1
575 }
576 pw2("]}\x0a" as *u8)
577 return 0
578 }
579 if a1[0]==(106 as u8) {
580 // ★DECLARE THE CEILING BEFORE SCORING, AND NAME A MISMATCH INSTEAD OF ABSORBING IT.
581 // A frame that parses but is not this judge's extent is UNJUDGED, not RED and not a low
582 // score: the ruler cannot see it, and "the ruler is blind here" and "the image is bad"
583 // are different findings with different remedies. Before 2026-08-28 this path silently
584 // scored the first 400x240 pixels of whatever it was handed.
585 let hw2: *i64 = sys_mmap(PJ_WH_BYTES) as *i64
586 let hdrok: i64 = pj_hdr_of_file(argv[2] as *u8, hw2)
587 if hdrok == 0 { if hw2[0] > 0 {
588 pw2("{\x22organ\x22:\x22nx_percept\x22,\x22verb\x22:\x22judge\x22,\x22verdict\x22:\x22UNJUDGED-AT-RESOLUTION\x22,\x22frame_w\x22:" as *u8); pn2(hw2[0])
589 pw2(",\x22frame_h\x22:" as *u8); pn2(hw2[1])
590 pw2(",\x22ceiling_w\x22:" as *u8); pn2(PJ_W)
591 pw2(",\x22ceiling_h\x22:" as *u8); pn2(PJ_H)
592 pw2(",\x22why\x22:\x22this judge's banked GOOD and JUNK anchors are ceiling_w x ceiling_h, so a frame of any other extent would be compared against anchors of a different subject; it was previously read as a PREFIX of that size and scored anyway\x22" as *u8)
593 pw2(",\x22fix\x22:\x22fit the capture to the ceiling deliberately and say so in the caller, or raise the ceiling by re-banking every anchor -- this is a CEILING, not a buffer to widen\x22}\x0a" as *u8)
594 return PJ_EXIT_UNJUDGED
595 } }
596 let cf: *i64 = sys_mmap(PJ_ND*8) as *i64
597 if pj_featfile(argv[2] as *u8, cf) == 0 {
598 pw2("PERCEPT RED: candidate unreadable\x0averdict=RED\x0a" as *u8)
599 return 1
600 }
601 let sc: i64 = pj_score(cf, av, 0-1)
602 pw2("{\x22organ\x22:\x22nx_percept\x22,\x22verb\x22:\x22judge\x22,\x22score\x22:" as *u8)
603 pn2(sc)
604 // ★EVERY SCORE CARRIES THE RESOLUTION IT WAS TAKEN AT AND THE RULER'S OWN REACH. A reader
605 // cannot otherwise tell a quality verdict from a resolution ceiling, and those two have
606 // opposite remedies. Printed on the SUCCESS path deliberately: a ceiling that only shows
607 // up in refusals is invisible exactly when someone is acting on a number.
608 pw2(",\x22judged_w\x22:" as *u8); pn2(PJ_W)
609 pw2(",\x22judged_h\x22:" as *u8); pn2(PJ_H)
610 pw2(",\x22ceiling_w\x22:" as *u8); pn2(PJ_W)
611 pw2(",\x22ceiling_h\x22:" as *u8); pn2(PJ_H)
612 pw2(",\x22meaning\x22:\x22permil(sim-to-commercial-band minus sim-to-junk-band); RELATIVE position, never absolute quality; admitted only while `ladder` reproduces the human ordering\x22}\x0a" as *u8)
613 return 0
614 }
615 }
616 // ladder -- the admission oracle (default verb)
617 let fm: *i64 = sys_mmap(PJ_ND*8) as *i64
618 let fc: *i64 = sys_mmap(PJ_ND*8) as *i64
619 if pj_featfile("knowledge/nx_m2d_frame.nxfh" as *u8, fm) == 0 { pw2("PERCEPT RED: m2d frame unreadable\x0averdict=RED\x0a" as *u8); return 1 }
620 if pj_featfile("knowledge/craft1.nxfh" as *u8, fc) == 0 { pw2("PERCEPT RED: craft frame unreadable\x0averdict=RED\x0a" as *u8); return 1 }
621 let sflat: i64 = pj_score(((av as i64) + 10*PJ_ND*8) as *i64, av, 0-1)
622 let ssq: i64 = pj_score(((av as i64) + 11*PJ_ND*8) as *i64, av, 0-1)
623 let sm2d: i64 = pj_score(fm, av, 0-1)
624 let scraft: i64 = pj_score(fc, av, 0-1)
625 pw2("=== nx_percept ladder: does the judge reproduce the HUMAN ordering? ===\x0a" as *u8)
626 pw2(" flat=" as *u8); pn2(sflat)
627 pw2(" squares=" as *u8); pn2(ssq)
628 pw2(" m2d=" as *u8); pn2(sm2d)
629 pw2(" craft=" as *u8); pn2(scraft)
630 pw2("\x0a" as *u8)
631 var passn: i64 = 0
632 var totn: i64 = 0
633 totn = totn + 1
634 if sflat < ssq { passn = passn + 1; pw2(" L1 flat < squares GREEN\x0a" as *u8) } else { pw2(" L1 flat < squares RED\x0a" as *u8) }
635 totn = totn + 1
636 if ssq < sm2d { passn = passn + 1; pw2(" L2 squares < m2d GREEN\x0a" as *u8) } else { pw2(" L2 squares < m2d RED\x0a" as *u8) }
637 totn = totn + 1
638 if sm2d < scraft { passn = passn + 1; pw2(" L3 m2d < craft (the operator ordering) GREEN\x0a" as *u8) } else { pw2(" L3 m2d < craft (the operator ordering) RED\x0a" as *u8) }
639 var ri: i64 = 0
640 while ri < 10 {
641 totn = totn + 1
642 let sr: i64 = pj_score(((av as i64) + ri*PJ_ND*8) as *i64, av, ri)
643 pw2(" ref[" as *u8); pn2(ri)
644 pw2("] loo=" as *u8); pn2(sr)
645 if sr > ssq { passn = passn + 1; pw2(" > squares GREEN\x0a" as *u8) } else { pw2(" NOT > squares RED\x0a" as *u8) }
646 ri = ri + 1
647 }
648 pw2("=== nx_percept ladder " as *u8); pn2(passn)
649 pw2("/" as *u8); pn2(totn)
650 if passn == totn { pw2(" ADMITTED (judge reproduces the human ordering; advisory pick instrument, human override stands)\x0averdict=GREEN\x0a" as *u8); return 0 }
651 pw2(" REFUSED (the judge does NOT reproduce the human ordering -- numbers above are published, the verdict is about the JUDGE)\x0averdict=RED\x0a" as *u8)
652 return 1
653}