code wiki / (root) / nx_mediajudge_gate.nx

nx_mediajudge_gate.nx source

↩ module page · 254 lines · 17662 B

1// nx_mediajudge_gate.nx -- the referee for nx_mediajudge_lib (aesthetictwin AT36, 2026-09-17). The fixture is the estate's own 2// seed-42 face rendered through nx_twinfit_lib (the render the outside oracle judged), judged through the media judge into a 3// scratch directory: the still decodes, the ruler places both eyes and the mouth, skin and symmetry are numbers, the 4// original copy lands byte-identical, the overlay PNG decodes back to the same frame with a RED marker at the measured 5// right lateral canthus and a GREEN pixel on the face box's top edge, the manifest carries a meta row, two tasset rows, 6// three tier rows and every judge row the numbers imply, the judge file carries the same rows in pipe grammar, and a 7// planted oracle row for the render makes the referee report its judgement. Controls: a flat field decodes but has no eye 8// pair (JUDGED-NO-EYE-PAIR, judge rows still emitted), prose is refused by every decoder, an absent file is unreadable. 9// license_tier: ORIGINAL No hw writes (Rule 26). 10import "nx_syscalls.nx" 11import "nx_gate_verdict.nx" 12import "nx_twinfit_lib.nx" 13import "nx_mediajudge_lib.nx" 14 15const MG_DIR: *u8 = "/tmp/mediajudge_gate" 16const MG_STILL: *u8 = "/tmp/mediajudge_gate/seed42.png" 17const MG_FLAT: *u8 = "/tmp/mediajudge_gate/flat.png" 18const MG_JUNK: *u8 = "/tmp/mediajudge_gate/prose.txt" 19const MG_JUNK_TEXT: *u8 = "no decoder should take this: it is prose, not an image\n" 20const MG_ABSENT: *u8 = "/tmp/mediajudge_gate/absent/none.png" 21const MG_ORACLE: *u8 = "/tmp/mediajudge_gate/seed42.oracle" 22// the outside oracle's row for the seed-42 render (knowledge/refintake/twinface_42.oracle), the file named by the path the 23// referee opens from this gate's CWD-free contract 24const MG_ORACLE_TEXT: *u8 = "oracle|/tmp/mediajudge_gate/seed42.png|-|512|384|1|2001,1619|2338,1670|2769,1679|3104,1619|2556,2470|2553,2559|2555,2593|2549,2744|2309,2518|2796,2528|2138,1614|2948,1619|2572,2291|2542,2954|2566,1024|85|101|390|1263|591|110|172,102,336,295\n" 25const MG_ORIG_COPY: *u8 = "/tmp/mediajudge_gate/seed42.original.png" 26const MG_OVERLAY: *u8 = "/tmp/mediajudge_gate/seed42.overlay.png" 27const MG_MANIFEST: *u8 = "/tmp/mediajudge_gate/seed42.manifest.tsv" 28const MG_JUDGE: *u8 = "/tmp/mediajudge_gate/seed42.judge" 29const MG_PLANE: *u8 = "/tmp/mediajudge_gate/seed42.plane.tsv" 30const MG_NOFACE_ORACLE: *u8 = "/tmp/mediajudge_gate/noface.oracle" 31// the same planted row with the faces column at 0: the oracle placed no face, so it has no axes to lend 32const MG_NOFACE_TEXT: *u8 = "oracle|/tmp/mediajudge_gate/seed42.png|-|512|384|0|2001,1619|2338,1670|2769,1679|3104,1619|2556,2470|2553,2559|2555,2593|2549,2744|2309,2518|2796,2528|2138,1614|2948,1619|2572,2291|2542,2954|2566,1024|85|101|390|1263|591|110|172,102,336,295\n" 33const MG_ICI_EXPECT: i64 = 390 // the planted canthi: inner span 43 px over outer span 110 px, the oracle row's own 390 34const MG_ICI_TOL: i64 = 5 35const MG_GAP_ROWS_EXPECT: i64 = 5 // four eye axes and the lip ratio: the seed-42 render measures both eyes and the mouth 36const MG_LIC_ROWS: i64 = 2 37const MG_COL_OURS: i64 = 2 // gap|axis|ours|best|holder in the judge file's pipe grammar 38const MG_COL_BEST: i64 = 3 39const MG_FLAT_MANIFEST: *u8 = "/tmp/mediajudge_gate/flat.manifest.tsv" 40const MG_HREFPFX: *u8 = "/compare/aesthetictwin/media/flat/" // the published form's site-absolute prefix (the page law) 41const MG_HREF_ROW: *u8 = "tasset\t1\timg\t/compare/aesthetictwin/media/flat/flat.original.png" 42const MG_SLUG: *u8 = "seed42" 43const MG_SLUG_KEY: *u8 = "seed42\t" // the plane row prefix: the slug then a tab, what nx_asset_page emit selects on 44const MG_FLAT_SLUG: *u8 = "flat" 45const MG_MODE755: i64 = 493 46const MG_MODE644: i64 = 420 47const MG_I64: i64 = 8 48const MG_SEED: i64 = 42 49const MG_YAW: i64 = 0 50const MG_FLAT_W: i64 = 96 51const MG_FLAT_H: i64 = 64 52const MG_FLAT_GREY: i64 = 8355711 // 127,127,127 packed 53const MG_RED: i64 = 255 54const MG_GREEN: i64 = 65280 55const MG_MIN_JUDGE_ROWS: i64 = 12 // 3 flags + 7 eye axes + lip + 3 skin + symmetry + 2 referee = 17 with eyes and a mouth; 12 is the floor with eyes 56// the referee's max NME on this render: 109 measured 2026-09-17 with the span normaliser; 81 since 2026-09-18 (AT44), when the photo 57// ruler's fissure stopped ending at its band's edge -- the four canthi now read 45 / 63 / 81 / 45 permil against the planted row 58const MG_ORACLE_NME_EXPECT: i64 = 81 59const MG_ORACLE_NME_TOL: i64 = 5 60 61func mg_write(path: *u8, text: *u8) -> i64 { 62 let fd: i64 = sys_openat_wr(path, MG_MODE644) 63 if fd < 0 { return 0 - 1 } 64 let n: i64 = sys_write(fd, text, ri_slen(text)) 65 sys_close(fd) 66 return n 67} 68func mg_same_file(a: *u8, b: *u8) -> i64 { 69 let la: *i64 = sys_mmap(16) as *i64 70 let lb: *i64 = sys_mmap(16) as *i64 71 let pa: *u8 = sys_read_file(a, la) 72 let pb: *u8 = sys_read_file(b, lb) 73 if (pa as i64) == 0 { return 0 } 74 if (pb as i64) == 0 { return 0 } 75 if la[0] != lb[0] { return 0 } 76 var i: i64 = 0 77 while i < la[0] { if pa[i] != pb[i] { return 0 } i = i + 1 } 78 return 1 79} 80// the packed pixel of a PNG on disk at (x, y), or -1 when it cannot be read 81func mg_png_pixel(path: *u8, x: i64, y: i64, wh: *i64) -> i64 { 82 let ln: *i64 = sys_mmap(16) as *i64 83 let raw: *u8 = sys_read_file(path, ln) 84 if (raw as i64) == 0 { return 0 - 1 } 85 let pr: *NxPngResult = nx_png_decode(raw, ln[0]) 86 if (pr as i64) == 0 { return 0 - 1 } 87 if pr.error_code != 0 { return 0 - 1 } 88 let hdr: *NxPngHeader = pr.header 89 wh[0] = hdr.width 90 wh[1] = hdr.height 91 if x < 0 { return 0 - 1 } 92 if y < 0 { return 0 - 1 } 93 if x >= hdr.width { return 0 - 1 } 94 if y >= hdr.height { return 0 - 1 } 95 let ch: i64 = pr.n_channels 96 let px: *u8 = pr.pixels 97 let i: i64 = (y * hdr.width + x) * ch 98 var r: i64 = px[i] as i64 99 var g: i64 = r 100 var b: i64 = r 101 if ch >= MJ_RGB_BPP { g = px[i + 1] as i64; b = px[i + 2] as i64 } 102 return r + (g << MJ_SHIFT_G) + (b << MJ_SHIFT_B) 103} 104func mg_count_rows(path: *u8, prefix: *u8) -> i64 { 105 let ln: *i64 = sys_mmap(16) as *i64 106 let buf: *u8 = sys_read_file(path, ln) 107 if (buf as i64) == 0 { return 0 - 1 } 108 var n: i64 = 0 109 var i: i64 = 0 110 var at_line: i64 = 1 111 while i < ln[0] { 112 if at_line == 1 { 113 var k: i64 = 0 114 var hit: i64 = 1 115 while prefix[k] != (0 as u8) { if i + k >= ln[0] { hit = 0 } else { if buf[i + k] != prefix[k] { hit = 0 } } k = k + 1 } 116 if hit == 1 { n = n + 1 } 117 at_line = 0 118 } 119 if (buf[i] as i64) == MJ_NL { at_line = 1 } 120 i = i + 1 121 } 122 return n 123} 124// THE FIXTURE IS FROZEN (AT39, 2026-09-17). This gate's subject is the JUDGE pipeline, and until today its fixture was rendered 125// fresh from the face generator on every run, so the day the generator grew a real eye opening every banked number here moved 126// and the judge was reported broken for a change it had no part in. The seed-42 still the outside oracle judged is now a FILE, 127// the estate's own render with no third-party pixel in it, copied into the scratch directory and held to its byte count; the 128// generator may keep improving and this gate keeps measuring the judge. 129const MG_FROZEN: *u8 = "knowledge/fixtures/mediajudge_seed42.png" 130const MG_FROZEN_BYTES: i64 = 94161 131// copy src to dst whole; returns the bytes written, -1 when the source cannot be read or the destination cannot be opened 132func mg_copy(src: *u8, dst: *u8) -> i64 { 133 let ln: *i64 = sys_mmap(16) as *i64 134 let buf: *u8 = sys_read_file(src, ln) 135 if (buf as i64) == 0 { return 0 - 1 } 136 let fd: i64 = sys_openat_wr(dst, MG_MODE644) 137 if fd < 0 { return 0 - 1 } 138 let n: i64 = sys_write(fd, buf, ln[0]) 139 sys_close(fd) 140 return n 141} 142// the integer in pipe column idx of the first judge-file row that starts with the prefix, or -1 when no row does 143func mg_row_int(path: *u8, prefix: *u8, idx: i64) -> i64 { 144 let ln: *i64 = sys_mmap(16) as *i64 145 let buf: *u8 = sys_read_file(path, ln) 146 if (buf as i64) == 0 { return 0 - 1 } 147 var ls: i64 = 0 148 while ls < ln[0] { 149 var le: i64 = ls 150 var scanning: i64 = 1 151 while scanning == 1 { 152 if le >= ln[0] { scanning = 0 } else { 153 if (buf[le] as i64) == MJ_NL { scanning = 0 } else { le = le + 1 } 154 } 155 } 156 var k: i64 = 0 157 var hit: i64 = 1 158 while prefix[k] != (0 as u8) { 159 if ls + k >= le { hit = 0 } else { if buf[ls + k] != prefix[k] { hit = 0 } } 160 k = k + 1 161 } 162 if hit == 1 { return pm_field_int(buf, ls, le, idx) } 163 ls = le + 1 164 } 165 return 0 - 1 166} 167func main(argc: i64, argv: *i64) -> i64 { 168 gv_head("nx_mediajudge_gate -- submitted media through the beauty rulers: decode, measure, overlay, manifest, judge file, referee" as *u8) 169 let ctr: *i64 = gv_ctr() 170 sys_mkdir(MG_DIR, MG_MODE755) 171 let cs: *i64 = sys_mmap(2 * MG_I64) as *i64 172 gv_need("fixture-reached-the-condition-cascades-loaded" as *u8, mj_load_cascades(cs), ctr) 173 let face_c: *HaarCascade = cs[0] as *HaarCascade 174 let eye_c: *HaarCascade = cs[1] as *HaarCascade 175 // the fixture: the FROZEN seed-42 still (see MG_FROZEN); the arena below is kept for the teeth that still use it 176 let base: i64 = sys_mmap(sdf_bytes()) as i64 177 let skin: *i64 = sys_mmap(TW_SLOTS * MG_I64) as *i64 178 let idv: *i64 = sys_mmap(TW_SLOTS * MG_I64) as *i64 179 gv_need("fixture-the-frozen-seed42-still-is-on-disk-whole" as *u8, (mg_copy(MG_FROZEN, MG_STILL) == MG_FROZEN_BYTES) as i64, ctr) 180 gv_need("fixture-oracle-row-written" as *u8, (mg_write(MG_ORACLE, MG_ORACLE_TEXT) > 0) as i64, ctr) 181 let res: *i64 = sys_mmap(PM_R_N * MG_I64) as *i64 182 let sres: *i64 = sys_mmap(SKS_R_N * MG_I64) as *i64 183 let out: *i64 = sys_mmap(MJ_R_N * MG_I64) as *i64 184 let rc: i64 = mj_judge_file(MG_STILL, MG_SLUG, MG_DIR, MG_ORACLE, face_c, eye_c, res, sres, out) 185 gv_check_eq("judge-returns-ok" as *u8, rc, 0, ctr) 186 gv_check_eq("judge-decodes-the-still-width" as *u8, out[MJ_R_W], ww(), ctr) 187 gv_check_eq("judge-decodes-the-still-height" as *u8, out[MJ_R_H], hh(), ctr) 188 gv_check_eq("judge-codec-is-still" as *u8, out[MJ_R_CODEC], MJ_CODEC_STILL, ctr) 189 gv_check_eq("judge-frames-is-one-for-a-still" as *u8, out[MJ_R_FRAMES], 1, ctr) 190 gv_check_eq("ruler-placed-both-eyes" as *u8, res[PM_R_EYES], 2, ctr) 191 gv_check_eq("ruler-found-the-mouth" as *u8, res[PM_R_MOUTH], 1, ctr) 192 gv_check("skin-pixels-counted" as *u8, (sres[SKS_R_COUNT] > 0) as i64, ctr) 193 gv_check("symmetry-is-a-number" as *u8, (out[MJ_R_SYMMETRY] >= 0) as i64, ctr) 194 gv_check("original-copy-is-byte-identical" as *u8, mg_same_file(MG_STILL, MG_ORIG_COPY), ctr) 195 gv_check("overlay-landed" as *u8, (out[MJ_R_OVERLAY_BYTES] > 0) as i64, ctr) 196 let wh: *i64 = sys_mmap(2 * MG_I64) as *i64 197 let at_canthus: i64 = mg_png_pixel(MG_OVERLAY, res[PM_R_RLAT_X], res[PM_R_RLAT_Y], wh) 198 gv_check_eq("overlay-decodes-to-the-frame-width" as *u8, wh[0], ww(), ctr) 199 gv_check_eq("overlay-marks-the-measured-right-lateral-canthus-red" as *u8, at_canthus, MG_RED, ctr) 200 let at_face: i64 = mg_png_pixel(MG_OVERLAY, (res[PM_R_FX0] + res[PM_R_FX1]) / 2, res[PM_R_FY0], wh) 201 gv_check_eq("overlay-draws-the-face-box-top-edge-green" as *u8, at_face, MG_GREEN, ctr) 202 let orig_px: i64 = mg_png_pixel(MG_ORIG_COPY, res[PM_R_RLAT_X], res[PM_R_RLAT_Y], wh) 203 gv_check("neg-control-original-copy-carries-no-marker" as *u8, (orig_px != MG_RED) as i64, ctr) 204 gv_check("manifest-landed" as *u8, (out[MJ_R_MANIFEST_BYTES] > 0) as i64, ctr) 205 gv_check_eq("manifest-one-meta-row" as *u8, mg_count_rows(MG_MANIFEST, "meta\t" as *u8), 1, ctr) 206 gv_check_eq("manifest-two-tasset-rows" as *u8, mg_count_rows(MG_MANIFEST, "tasset\t" as *u8), 2, ctr) 207 gv_check_eq("manifest-three-tier-rows" as *u8, mg_count_rows(MG_MANIFEST, "tier\t" as *u8), 3, ctr) 208 gv_check_eq("manifest-judge-rows-equal-the-count-announced" as *u8, mg_count_rows(MG_MANIFEST, "judge\t" as *u8), out[MJ_R_JUDGE_ROWS], ctr) 209 gv_check("manifest-carries-the-eye-axes-and-the-referee" as *u8, (out[MJ_R_JUDGE_ROWS] >= MG_MIN_JUDGE_ROWS) as i64, ctr) 210 gv_check_eq("judge-file-rows-equal-the-manifest-rows" as *u8, mg_count_rows(MG_JUDGE, "judge|" as *u8), out[MJ_R_JUDGE_ROWS], ctr) 211 gv_check("plane-staging-file-landed" as *u8, (out[MJ_R_PLANE_BYTES] > 0) as i64, ctr) 212 gv_check_eq("plane-rows-are-every-manifest-row-keyed-by-slug" as *u8, mg_count_rows(MG_PLANE, MG_SLUG_KEY), MJ_ROWS_FIXED + out[MJ_R_JUDGE_ROWS] + out[MJ_R_GAP_ROWS], ctr) 213 gv_check_eq("neg-control-plane-carries-no-unkeyed-row" as *u8, mg_count_rows(MG_PLANE, "meta\t" as *u8), 0, ctr) 214 // the licence beside the media and the triangulation rows: ours beside the outside oracle, axis by axis 215 gv_check_eq("manifest-two-licence-rows" as *u8, mg_count_rows(MG_MANIFEST, "lic\t" as *u8), MG_LIC_ROWS, ctr) 216 gv_check_eq("gap-rows-one-per-axis-both-sides-measured" as *u8, out[MJ_R_GAP_ROWS], MG_GAP_ROWS_EXPECT, ctr) 217 gv_check_eq("manifest-gap-rows-equal-the-count-announced" as *u8, mg_count_rows(MG_MANIFEST, "gap\t" as *u8), out[MJ_R_GAP_ROWS], ctr) 218 let oref: *i64 = sys_mmap(PM_R_N * MG_I64) as *i64 219 gv_check_eq("oracle-axes-read-the-planted-row" as *u8, mj_oracle_axes(MG_ORACLE, oref), 1, ctr) 220 gv_check_near("oracle-side-ici-is-the-planted-landmarks-arithmetic" as *u8, oref[PM_R_ICI], MG_ICI_EXPECT, MG_ICI_TOL, ctr) 221 gv_check_eq("gap-row-carries-the-oracle-side-ici" as *u8, mg_row_int(MG_JUDGE, "gap|intercanthal_index_permil|" as *u8, MG_COL_BEST), oref[PM_R_ICI], ctr) 222 gv_check_eq("gap-row-carries-our-ici" as *u8, mg_row_int(MG_JUDGE, "gap|intercanthal_index_permil|" as *u8, MG_COL_OURS), res[PM_R_ICI], ctr) 223 gv_check_eq("referee-judged-the-planted-oracle-row" as *u8, out[MJ_R_REFEREE_JUDGED], 1, ctr) 224 gv_check_near("referee-nme-max-is-the-measured-render-error" as *u8, out[MJ_R_REFEREE_NME_MAX], MG_ORACLE_NME_EXPECT, MG_ORACLE_NME_TOL, ctr) 225 // controls: a flat field has no eye pair, prose has no decoder, an absent file is unreadable 226 let n: i64 = MG_FLAT_W * MG_FLAT_H 227 let fb: *i64 = sys_mmap(n * MG_I64) as *i64 228 var i: i64 = 0 229 while i < n { fb[i] = MG_FLAT_GREY; i = i + 1 } 230 gv_need("fixture-flat-field-written" as *u8, (png_publish(fb, MG_FLAT_W, MG_FLAT_H, MG_FLAT, 0) > 0) as i64, ctr) 231 let res2: *i64 = sys_mmap(PM_R_N * MG_I64) as *i64 232 let out2: *i64 = sys_mmap(MJ_R_N * MG_I64) as *i64 233 let rc2: i64 = mj_judge_file(MG_FLAT, MG_FLAT_SLUG, MG_DIR, "" as *u8, face_c, eye_c, res2, sres, out2) 234 gv_check_eq("neg-control-flat-field-judges-without-refusing" as *u8, rc2, 0, ctr) 235 gv_check_eq("neg-control-flat-field-has-no-eye-pair" as *u8, res2[PM_R_EYES], 0, ctr) 236 gv_check("neg-control-flat-field-still-carries-judge-rows" as *u8, (out2[MJ_R_JUDGE_ROWS] > 0) as i64, ctr) 237 // the published form: the manifest's tasset hrefs carry the site-absolute prefix, the bare form carries none 238 gv_check_eq("href-prefix-form-judges" as *u8, mj_judge_file_at(MG_FLAT, MG_FLAT_SLUG, MG_DIR, "" as *u8, MG_HREFPFX, face_c, eye_c, res2, sres, out2), 0, ctr) 239 gv_check_eq("href-prefix-lands-on-the-tasset-row-site-absolute" as *u8, mg_count_rows(MG_FLAT_MANIFEST, MG_HREF_ROW), 1, ctr) 240 gv_check_eq("neg-control-bare-form-writes-no-site-absolute-href" as *u8, mg_count_rows(MG_MANIFEST, "tasset\t1\timg\t/" as *u8), 0, ctr) 241 // no oracle, no triangulation rows; an oracle that placed no face, or an absent one, lends no axes 242 gv_check_eq("neg-control-no-oracle-writes-no-gap-row" as *u8, mg_count_rows(MG_FLAT_MANIFEST, "gap\t" as *u8), 0, ctr) 243 gv_need("fixture-faceless-oracle-written" as *u8, (mg_write(MG_NOFACE_ORACLE, MG_NOFACE_TEXT) > 0) as i64, ctr) 244 gv_check_eq("neg-control-an-oracle-row-with-no-face-lends-no-axes" as *u8, mj_oracle_axes(MG_NOFACE_ORACLE, oref), 0, ctr) 245 gv_check_eq("neg-control-an-absent-oracle-lends-no-axes" as *u8, mj_oracle_axes(MG_ABSENT, oref), 0, ctr) 246 gv_need("fixture-prose-written" as *u8, (mg_write(MG_JUNK, MG_JUNK_TEXT) > 0) as i64, ctr) 247 gv_check_eq("neg-control-prose-takes-no-decoder" as *u8, mj_judge_file(MG_JUNK, MG_FLAT_SLUG, MG_DIR, "" as *u8, face_c, eye_c, res2, sres, out2), MJ_E_DECODE, ctr) 248 gv_check_eq("neg-control-absent-file-unreadable" as *u8, mj_judge_file(MG_ABSENT, MG_FLAT_SLUG, MG_DIR, "" as *u8, face_c, eye_c, res2, sres, out2), MJ_E_READ, ctr) 249 gv_values_head() 250 gv_kv("tilt_R_deg10" as *u8, res[PM_R_TILT_R]); gv_kv("tilt_L_deg10" as *u8, res[PM_R_TILT_L]); gv_kv("ici_permil" as *u8, res[PM_R_ICI]); gv_kv("icf_permil" as *u8, res[PM_R_ICF]); gv_kv("lip_permil" as *u8, res[PM_R_LIP]) 251 gv_kv("skin_L_e3" as *u8, sres[SKS_R_L]); gv_kv("skin_ita_deg10" as *u8, sres[SKS_R_ITA]); gv_kv("skin_pixels" as *u8, sres[SKS_R_COUNT]); gv_kv("symmetry_q1000" as *u8, out[MJ_R_SYMMETRY]) 252 gv_kv("overlay_bytes" as *u8, out[MJ_R_OVERLAY_BYTES]); gv_kv("manifest_bytes" as *u8, out[MJ_R_MANIFEST_BYTES]); gv_kv("plane_bytes" as *u8, out[MJ_R_PLANE_BYTES]); gv_kv("judge_rows" as *u8, out[MJ_R_JUDGE_ROWS]); gv_kv("gap_rows" as *u8, out[MJ_R_GAP_ROWS]); gv_kv("referee_nme_max" as *u8, out[MJ_R_REFEREE_NME_MAX]) 253 return gv_verdict("nx_mediajudge_gate" as *u8, ctr, "submitted media through the beauty rulers: the seed-42 render decoded, measured, skin and symmetry scored, copied byte-identical, overlaid with the markers at the measured landmarks, written as a manifest and a judge file whose rows agree, refereed against the planted oracle row; a flat field judges without an eye pair, prose takes no decoder, an absent file is unreadable" as *u8) 254}