code wiki / (root) / nx_video_h2h_gate.nx

nx_video_h2h_gate.nx source

↩ module page · 264 lines · 15338 B

1// nx_video_h2h_gate.nx -- THE REFEREE for the video head-to-head JUDGE (2026-09-02). 2// Subject: nx_video_h2h, the sovereign organ that turns raw strip samples into the video.bench receipt. The gate builds a 3// SYNTHETIC call whose truth is known in closed form -- a sender preview advancing one index every 33 ms, a receiver 4// showing the same indices 120 ms later with indices 50..59 never shown, two index-aligned luma snapshot pairs (one 5// identical, one shifted by exactly +1 everywhere) -- forks the judge on it and asks whether the receipt carries the numbers 6// the fixture planted: glass-to-glass 120000 us, 290 of 300 delivered (966 permil), one stall of 363 ms, the PSNR cap on the 7// identical pair and 48.13 dB on the +1 pair. Negative controls: a second file with a DIFFERENT card sha must read VOID, a 8// reference arm that does not exist must read UNMEASURABLE, and a fixture with a different planted latency must move the 9// median (the number is measured, not constant). A missing judge binary SKIPs (I could not look), never RED. 10// usage: nx_video_h2h_gate [judge-elf] (default _build/nx_video_h2h.sov.elf) exit: 0 GREEN . 1 RED . 3 SKIP 11// license_tier: ORIGINAL No hw writes (Rule 26). 12import "nx_syscalls.nx" 13import "nx_gate_verdict.nx" 14import "nx_gatekit_lib.nx" 15import "nx_bench_receipt_lib.nx" 16 17const G_DIR: *u8 = "/tmp/nx_video_h2h_gate" 18const G_MODE_DIR: i64 = 493 19const G_MODE_RW: i64 = 420 20const G_N: i64 = 300 21const G_T0: i64 = 1000000 22const G_IV_MS: i64 = 33 23const G_LAT_MS: i64 = 120 24const G_LAT2_MS: i64 = 200 25const G_DROP_FROM: i64 = 50 26const G_DROP_TO: i64 = 60 27const G_W: i64 = 352 28const G_H: i64 = 288 29const G_PLANE: i64 = 101376 30const G_TXT_CAP: i64 = 262144 31const G_OUT_CAP: i64 = 65536 32const G_PATTERN_MOD: i64 = 250 33const G_SNAP_A: i64 = 30 34const G_SNAP_B: i64 = 60 35const G_EXP_DELIVERED: i64 = 966 36const G_EXP_STALL_MAX: i64 = 363 37const G_EXP_EFF_FPS_MILLI: i64 = 29289 38const G_EXP_PSNR_PLUS1_CDB: i64 = 4813 39const G_PSNR_TOL: i64 = 20 40const G_FPS_TOL_PERMIL: i64 = 10 41const G_PSNR_CAP_CDB: i64 = 9900 42const G_US_PER_MS: i64 = 1000 43const G_EXIT_VOID: i64 = 1 44const G_EXIT_UNMEASURABLE: i64 = 3 45 46func g_cat(d: *u8, o: i64, s: *u8) -> i64 { return br_cat(d, o, s) } 47func g_catn(d: *u8, o: i64, v: i64) -> i64 { 48 var oo: i64 = o 49 var m: i64 = v 50 if m < 0 { d[oo] = 45 as u8; oo = oo + 1; m = 0 - m } 51 let t: *u8 = sys_mmap(32) 52 var k: i64 = 0 53 if m == 0 { t[0] = 48 as u8; k = 1 } 54 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 55 var j: i64 = 0 56 while j < k { d[oo + j] = t[k - 1 - j]; j = j + 1 } 57 d[oo + k] = 0 as u8 58 return oo + k 59} 60func g_writefile(path: *u8, buf: *u8, n: i64) -> i64 { 61 let fd: i64 = sys_openat_wr(path, G_MODE_RW) 62 if fd < 0 { return 0 - 1 } 63 let r: i64 = gk_write_all(fd, buf, n) 64 sys_close(fd) 65 return r 66} 67// value of "key=<int>" inside a NUL-terminated note; -1 when absent 68func g_note_int(note: *u8, key: *u8) -> i64 { 69 var kl: i64 = 0 70 while key[kl] != (0 as u8) { kl = kl + 1 } 71 var i: i64 = 0 72 while note[i] != (0 as u8) { 73 var j: i64 = 0 74 var ok: i64 = 1 75 while j < kl { if note[i + j] != key[j] { ok = 0; j = kl } else { j = j + 1 } } 76 if ok == 1 { 77 var p: i64 = i + kl 78 var neg: i64 = 0 79 if note[p] == (45 as u8) { neg = 1; p = p + 1 } 80 var v: i64 = 0 81 var any: i64 = 0 82 while note[p] >= (48 as u8) { if note[p] <= (57 as u8) { v = v * 10 + ((note[p] as i64) - 48); any = 1; p = p + 1 } else { p = p + 1000000 } } 83 if any == 0 { return 0 - 1 } 84 if neg == 1 { return 0 - v } 85 return v 86 } 87 i = i + 1 88 } 89 return 0 - 1 90} 91func g_has(s: *u8, needle: *u8) -> i64 { 92 var nl: i64 = 0 93 while needle[nl] != (0 as u8) { nl = nl + 1 } 94 var i: i64 = 0 95 while s[i] != (0 as u8) { 96 var j: i64 = 0 97 var ok: i64 = 1 98 while j < nl { if s[i + j] == (0 as u8) { ok = 0; j = nl } else { if s[i + j] != needle[j] { ok = 0; j = nl } else { j = j + 1 } } } 99 if ok == 1 { return 1 } 100 i = i + 1 101 } 102 return 0 103} 104// one synthetic samples file. lat_ms = planted glass-to-glass; sha0 = first hex char of the card sha (the VOID control flips it). 105// Returns the number of RECEIVER rows written (the fixture-reached-the-condition count). 106func g_fixture(path: *u8, lat_ms: i64, sha0: i64) -> i64 { 107 let b: *u8 = sys_mmap(G_TXT_CAP) 108 var o: i64 = 0 109 o = g_cat(b, o, "#meta arm=fix room=gate dur=10 card=testcard768.y4m card_sha256=" as *u8) 110 b[o] = sha0 as u8; o = o + 1 111 o = g_cat(b, o, "e4ff37c7d32f6eafd95acc9ed0af4c5ff9fd72cd3ab10bb315693c8f716cc5a card_fps=30 card_frames=768 strip_rows=32 start_ms=" as *u8) 112 o = g_catn(b, o, G_T0) 113 o = g_cat(b, o, " throttle=- codec=-\n#meta host=gatehost|cpu|os|nproc=1\n#meta browser=A url=https://fixture/a ua=gate\n#meta browser=B url=https://fixture/b ua=gate\n" as *u8) 114 var written: i64 = 0 115 var i: i64 = 0 116 while i < G_N { 117 let t: i64 = G_T0 + i * G_IV_MS 118 o = g_cat(b, o, "fix|A|" as *u8); o = g_catn(b, o, t); o = g_cat(b, o, "|VIDEO@tile.self|352|288|" as *u8); o = g_catn(b, o, i % 256); o = g_cat(b, o, "|235|16\n" as *u8) 119 var drop: i64 = 0 120 if i >= G_DROP_FROM { if i < G_DROP_TO { drop = 1 } } 121 if drop == 0 { 122 o = g_cat(b, o, "fix|B|" as *u8); o = g_catn(b, o, t + lat_ms); o = g_cat(b, o, "|CANVAS@tile|416|320|" as *u8); o = g_catn(b, o, i % 256); o = g_cat(b, o, "|235|16\n" as *u8) 123 written = written + 1 124 } 125 i = i + 1 126 } 127 o = g_cat(b, o, "#meta end_ms=" as *u8); o = g_catn(b, o, G_T0 + G_N * G_IV_MS + lat_ms); o = g_cat(b, o, "\n" as *u8) 128 if g_writefile(path, b, o) < 0 { return 0 - 1 } 129 // frames: idx rows + bin planes. Pair 1 (idx 30): identical planes. Pair 2 (idx 60): receiver = sender + 1 everywhere. 130 let ip: *u8 = sys_mmap(1024) 131 var io: i64 = g_cat(ip, 0, path); io = g_cat(ip, io, ".frames.idx" as *u8) 132 let bp: *u8 = sys_mmap(1024) 133 var bo: i64 = g_cat(bp, 0, path); bo = g_cat(bp, bo, ".frames.bin" as *u8) 134 let ib: *u8 = sys_mmap(4096) 135 var q: i64 = 0 136 q = g_cat(ib, q, "fix|A|" as *u8); q = g_catn(ib, q, G_T0 + G_SNAP_A * G_IV_MS); q = g_cat(ib, q, "|VIDEO@tile.self|" as *u8); q = g_catn(ib, q, G_SNAP_A); q = g_cat(ib, q, "|0|" as *u8); q = g_catn(ib, q, G_PLANE); q = g_cat(ib, q, "\n" as *u8) 137 q = g_cat(ib, q, "fix|B|" as *u8); q = g_catn(ib, q, G_T0 + G_SNAP_A * G_IV_MS + lat_ms); q = g_cat(ib, q, "|CANVAS@tile|" as *u8); q = g_catn(ib, q, G_SNAP_A); q = g_cat(ib, q, "|" as *u8); q = g_catn(ib, q, G_PLANE); q = g_cat(ib, q, "|" as *u8); q = g_catn(ib, q, G_PLANE); q = g_cat(ib, q, "\n" as *u8) 138 q = g_cat(ib, q, "fix|A|" as *u8); q = g_catn(ib, q, G_T0 + G_SNAP_B * G_IV_MS); q = g_cat(ib, q, "|VIDEO@tile.self|" as *u8); q = g_catn(ib, q, G_SNAP_B); q = g_cat(ib, q, "|" as *u8); q = g_catn(ib, q, 2 * G_PLANE); q = g_cat(ib, q, "|" as *u8); q = g_catn(ib, q, G_PLANE); q = g_cat(ib, q, "\n" as *u8) 139 q = g_cat(ib, q, "fix|B|" as *u8); q = g_catn(ib, q, G_T0 + G_SNAP_B * G_IV_MS + lat_ms); q = g_cat(ib, q, "|CANVAS@tile|" as *u8); q = g_catn(ib, q, G_SNAP_B); q = g_cat(ib, q, "|" as *u8); q = g_catn(ib, q, 3 * G_PLANE); q = g_cat(ib, q, "|" as *u8); q = g_catn(ib, q, G_PLANE); q = g_cat(ib, q, "\n" as *u8) 140 if g_writefile(ip, ib, q) < 0 { return 0 - 2 } 141 let bin: *u8 = sys_mmap(4 * G_PLANE) 142 var y: i64 = 0 143 while y < G_H { 144 var x: i64 = 0 145 while x < G_W { 146 let v: i64 = (x * 7 + y * 3) % G_PATTERN_MOD 147 let at: i64 = y * G_W + x 148 bin[at] = v as u8 149 bin[G_PLANE + at] = v as u8 150 bin[2 * G_PLANE + at] = v as u8 151 bin[3 * G_PLANE + at] = (v + 1) as u8 152 x = x + 1 153 } 154 y = y + 1 155 } 156 if g_writefile(bp, bin, 4 * G_PLANE) < 0 { return 0 - 3 } 157 return written 158} 159func g_arm_index(arms: *i64, n: i64, name: *u8) -> i64 { 160 var i: i64 = 0 161 while i < n { if br_streq(arms[i * BR_STRIDE + BR_A_NAME] as *u8, name) == 1 { return i } i = i + 1 } 162 return 0 - 1 163} 164 165func main(argc: i64, argv: *i64) -> i64 { 166 // The judge binary: an explicit argv[1], else the first of the estate's three artifact homes that exists -- the 167 // laptop build (_build/), the NAS build-tree fossil (buildroot/_build/), the promoted serving-root twin. Named, 168 // never guessed: the chosen path is printed so a stale twin can be told from the one under test. 169 var judge: *u8 = "_build/nx_video_h2h.sov.elf" as *u8 170 if argc > 1 { judge = argv[1] as *u8 } else { 171 let c2: *u8 = "buildroot/_build/nx_video_h2h.sov.elf" as *u8 172 let c3: *u8 = "nx_video_h2h.elf" as *u8 173 let t1: i64 = sys_openat_rd(judge) 174 if t1 >= 0 { sys_close(t1) } else { 175 let t2: i64 = sys_openat_rd(c2) 176 if t2 >= 0 { sys_close(t2); judge = c2 } else { let t3: i64 = sys_openat_rd(c3); if t3 >= 0 { sys_close(t3); judge = c3 } } 177 } 178 } 179 let ctr: *i64 = gv_ctr() 180 gv_head("nx_video_h2h_gate -- the judge's arithmetic on a synthetic call whose truth is known in closed form" as *u8) 181 gv_puts(" judge=" as *u8); gv_puts(judge); gv_puts("\n" as *u8) 182 let jfd: i64 = sys_openat_rd(judge) 183 if gv_need("judge binary present" as *u8, (jfd >= 0) as i64, ctr) == 0 { return gv_verdict("nx_video_h2h_gate" as *u8, ctr, "judge binary absent: build nx_video_h2h first" as *u8) } 184 sys_close(jfd) 185 sys_mkdir(G_DIR, G_MODE_DIR) 186 let f1: *u8 = "/tmp/nx_video_h2h_gate/fix.txt" as *u8 187 let f2: *u8 = "/tmp/nx_video_h2h_gate/fix_othercard.txt" as *u8 188 let f3: *u8 = "/tmp/nx_video_h2h_gate/fix_lat200.txt" as *u8 189 let r1: *u8 = "/tmp/nx_video_h2h_gate/r1.bench" as *u8 190 let r2: *u8 = "/tmp/nx_video_h2h_gate/r2.bench" as *u8 191 let r3: *u8 = "/tmp/nx_video_h2h_gate/r3.bench" as *u8 192 let r4: *u8 = "/tmp/nx_video_h2h_gate/r4.bench" as *u8 193 let w1: i64 = g_fixture(f1, G_LAT_MS, 50) // '2' = the real card's first hex char 194 let w2: i64 = g_fixture(f2, G_LAT_MS, 51) // '3' = a different card 195 let w3: i64 = g_fixture(f3, G_LAT2_MS, 50) 196 gv_check("fixture-reached-the-condition-290-receiver-rows-written" as *u8, (w1 == G_N - (G_DROP_TO - G_DROP_FROM)) as i64, ctr) 197 gv_check("fixture-controls-written-too" as *u8, (w2 == w1) as i64, ctr) 198 gv_check("fixture-shifted-latency-written" as *u8, (w3 == w1) as i64, ctr) 199 200 let out: *u8 = sys_mmap(G_OUT_CAP) 201 let outlen: *i64 = sys_mmap(16) as *i64 202 let rc1: i64 = gk_run_capture(judge, r1, "fix-B" as *u8, f1, 0 as *u8, out, G_OUT_CAP, outlen) 203 gv_check("judge-exits-0-VALID-on-the-fixture" as *u8, (rc1 == 0) as i64, ctr) 204 let hdr: *i64 = sys_mmap(8 * BR_H_N) as *i64 205 let arms: *i64 = sys_mmap(8 * BR_MAXARMS * BR_STRIDE) as *i64 206 let n: i64 = br_load(r1, hdr, arms) 207 gv_check("receipt-parses-two-arms-self-only-and-receiver" as *u8, (n == 2) as i64, ctr) 208 let ia: i64 = g_arm_index(arms, n, "fix-A" as *u8) 209 let ib2: i64 = g_arm_index(arms, n, "fix-B" as *u8) 210 var st_a: i64 = 0 - 1 211 var st_b: i64 = 0 - 1 212 var note_a: *u8 = "" as *u8 213 var note_b: *u8 = "" as *u8 214 var med_b: i64 = 0 - 1 215 var min_b: i64 = 0 - 1 216 if ia >= 0 { st_a = arms[ia * BR_STRIDE + BR_A_STATUS]; note_a = arms[ia * BR_STRIDE + BR_A_NOTE] as *u8 } 217 if ib2 >= 0 { st_b = arms[ib2 * BR_STRIDE + BR_A_STATUS]; note_b = arms[ib2 * BR_STRIDE + BR_A_NOTE] as *u8; med_b = arms[ib2 * BR_STRIDE + BR_A_MED]; min_b = arms[ib2 * BR_STRIDE + BR_A_MIN] } 218 gv_check("receiver-arm-fix-B-is-VALID" as *u8, (st_b == BR_ST_VALID) as i64, ctr) 219 var a_named: i64 = 0 220 if st_a == BR_ST_RUNFAIL { if g_has(note_a, "no remote tile" as *u8) == 1 { a_named = 1 } } 221 gv_check("self-only-arm-fix-A-is-RUN-FAIL-and-SAYS-no-remote-tile" as *u8, a_named, ctr) 222 gv_check("g2g-median-is-exactly-the-planted-120000us" as *u8, (med_b == G_LAT_MS * G_US_PER_MS) as i64, ctr) 223 gv_check("g2g-min-equals-median-on-a-jitter-free-fixture" as *u8, (min_b == G_LAT_MS * G_US_PER_MS) as i64, ctr) 224 gv_check("delivered-permil-966-from-10-dropped-of-300" as *u8, (g_note_int(note_b, "delivered_permil=" as *u8) == G_EXP_DELIVERED) as i64, ctr) 225 gv_check("exactly-one-stall-from-the-planted-gap" as *u8, (g_note_int(note_b, "stalls=" as *u8) == 1) as i64, ctr) 226 gv_check("stall-max-is-the-planted-363ms-gap" as *u8, (g_note_int(note_b, "stall_max_ms=" as *u8) == G_EXP_STALL_MAX) as i64, ctr) 227 let fps: i64 = g_note_int(note_b, "eff_fps_milli=" as *u8) 228 var fps_ok: i64 = 0 229 if fps > 0 { let d: i64 = fps - G_EXP_EFF_FPS_MILLI; var ad: i64 = d; if ad < 0 { ad = 0 - ad } if ad * 1000 <= G_EXP_EFF_FPS_MILLI * G_FPS_TOL_PERMIL { fps_ok = 1 } } 230 gv_check("eff-fps-milli-within-1-percent-of-29289" as *u8, fps_ok, ctr) 231 gv_check("fidelity-pairs-2-index-aligned" as *u8, (g_note_int(note_b, "fidelity_pairs=" as *u8) == 2) as i64, ctr) 232 let pmin: i64 = g_note_int(note_b, "psnr_cdb_min=" as *u8) 233 var pmin_ok: i64 = 0 234 if pmin >= G_EXP_PSNR_PLUS1_CDB - G_PSNR_TOL { if pmin <= G_EXP_PSNR_PLUS1_CDB + G_PSNR_TOL { pmin_ok = 1 } } 235 gv_check("psnr-min-is-the-plus-one-plane-48.13dB-within-0.2dB" as *u8, pmin_ok, ctr) 236 let pmean: i64 = g_note_int(note_b, "psnr_cdb_mean=" as *u8) 237 var pmean_ok: i64 = 0 238 if pmin > 0 { let want: i64 = (G_PSNR_CAP_CDB + pmin) / 2; var dd: i64 = pmean - want; if dd < 0 { dd = 0 - dd } if dd <= 1 { pmean_ok = 1 } } 239 gv_check("identical-pair-hits-the-99.00dB-cap-mean-is-cap-plus-min-over-2" as *u8, pmean_ok, ctr) 240 gv_check("identical-pair-and-plus-one-average-to-ssim-above-990" as *u8, (g_note_int(note_b, "ssim_milli_mean=" as *u8) >= 990) as i64, ctr) 241 242 // neg-control 1: a second file that played a DIFFERENT card -> VOID, exit 1 243 let rc2: i64 = gk_run_capture(judge, r2, "fix-B" as *u8, f1, f2, out, G_OUT_CAP, outlen) 244 let hdr2: *i64 = sys_mmap(8 * BR_H_N) as *i64 245 let arms2: *i64 = sys_mmap(8 * BR_MAXARMS * BR_STRIDE) as *i64 246 let n2: i64 = br_load(r2, hdr2, arms2) 247 gv_check("neg-control-different-card-reads-VOID-exit-1" as *u8, (rc2 == G_EXIT_VOID) as i64, ctr) 248 gv_check("neg-control-different-card-receipt-verdict-VOID" as *u8, (br_verdict(hdr2, arms2, n2) == BR_V_VOID) as i64, ctr) 249 // neg-control 2: a reference arm nobody played -> UNMEASURABLE, exit 3 250 let rc3: i64 = gk_run_capture(judge, r3, "nope-Z" as *u8, f1, 0 as *u8, out, G_OUT_CAP, outlen) 251 gv_check("neg-control-missing-reference-arm-reads-UNMEASURABLE-exit-3" as *u8, (rc3 == G_EXIT_UNMEASURABLE) as i64, ctr) 252 // neg-control 3: the planted latency MOVES the median (measured, not a constant) 253 let rc4: i64 = gk_run_capture(judge, r4, "fix-B" as *u8, f3, 0 as *u8, out, G_OUT_CAP, outlen) 254 let hdr4: *i64 = sys_mmap(8 * BR_H_N) as *i64 255 let arms4: *i64 = sys_mmap(8 * BR_MAXARMS * BR_STRIDE) as *i64 256 let n4: i64 = br_load(r4, hdr4, arms4) 257 let ib4: i64 = g_arm_index(arms4, n4, "fix-B" as *u8) 258 var med4: i64 = 0 - 1 259 if ib4 >= 0 { med4 = arms4[ib4 * BR_STRIDE + BR_A_MED] } 260 var moved: i64 = 0 261 if rc4 == 0 { if med4 == G_LAT2_MS * G_US_PER_MS { moved = 1 } } 262 gv_check("neg-control-shifted-fixture-moves-the-median-to-200000us" as *u8, moved, ctr) 263 return gv_verdict("nx_video_h2h_gate" as *u8, ctr, "" as *u8) 264}