code wiki / (root) / nx_recording_gate.nx

nx_recording_gate.nx source

↩ module page · 185 lines · 7172 B

1// nx_recording_gate.nx -- REFEREE for MEDIA-STUDIO R1 (nx_recording). 2// 3// Frames are 64x64, built as an 8x8 grid of 8px cells each set bright(255) 4// or dark(0) by a seeded LCG, then fingerprinted through the REAL nx_phash 5// aHash pipeline. Sequences of N=16 fingerprints exercise the classifier: 6// 7// LIVE : 16 distinct random frames -> no hold, no loop -> LIVE. 8// HOLD : same frame x16 (frozen slate) -> max_hold=16 -> RECORDING. 9// LOOP : 4 distinct frames cycled (period 4) -> recurrence -> RECORDING. 10// FREEZE : distinct frames with a 3-frame freeze in the middle. NEG-CONTROL: 11// a brief freeze (3 < hold_frames=8, gap < min_period=4) must NOT 12// be called a recording -> stays LIVE. Proves hold needs to be 13// SUSTAINED, not any near-match (the R1 analog of R0's 1px debounce). 14// 15// Plus a sanity check that distinct frames really differ (ham(live0,live1) > 16// near) so a green can't come from degenerate all-equal hashes. 17// 18// Every measured value PRINTED. stdout + knowledge/status/recording_gate.log. 19// Exit 0 GREEN / 1 RED. Sovereign: syscalls + nx_phash + nx_simhash + nx_recording. 20// license_tier: ORIGINAL 21import "syscalls.nx" 22import "nx_phash.nx" 23import "nx_simhash.nx" 24import "nx_recording.nx" 25 26const W: i64 = 64 27const H: i64 = 64 28const CS: i64 = 8 // cell size px (W/8) 29const N: i64 = 16 30const NEAR: i64 = 8 31const HOLD: i64 = 8 32const MINP: i64 = 4 33 34func gp(logfd: i64, s: *u8) -> i64 { 35 var n: i64 = 0 36 while s[n] != (0 as u8) { n = n + 1 } 37 sys_write(1, s, n) 38 if logfd > 0 { sys_write(logfd, s, n) } 39 return 0 40} 41func gn(logfd: i64, v: i64) -> i64 { 42 let bb: *u8 = sys_mmap(28) 43 var m: i64 = v 44 if m < 0 { sys_write(1, "-\x00" as *u8, 1); if logfd > 0 { sys_write(logfd, "-\x00" as *u8, 1) } m = 0 - m } 45 let t: *u8 = sys_mmap(28) 46 var k: i64 = 0 47 if m == 0 { t[0] = 48 as u8; k = 1 } 48 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 49 var i: i64 = 0 50 while i < k { bb[i] = t[k - 1 - i]; i = i + 1 } 51 sys_write(1, bb, k) 52 if logfd > 0 { sys_write(logfd, bb, k) } 53 return 0 54} 55 56// Draw a seeded blocky random frame into buf and return its aHash fingerprint. 57// Deterministic in `seed`: same seed -> same frame -> same fingerprint. 58func hash_seeded(buf: *u8, seed: i64) -> i64 { 59 var s: i64 = seed 60 var c: i64 = 0 61 while c < 64 { 62 s = (s * 1103515245 + 12345) & 0x7fffffff 63 var v: i64 = 0 64 if s > 0x3fffffff { v = 255 } // high bit, not the LCG LSB (period-2 trap) 65 let cx: i64 = c % 8 66 let cy: i64 = c / 8 67 var y: i64 = cy * CS 68 while y < cy * CS + CS { 69 var x: i64 = cx * CS 70 while x < cx * CS + CS { buf[y * W + x] = v as u8; x = x + 1 } 71 y = y + 1 72 } 73 c = c + 1 74 } 75 return nx_phash_ahash(buf, W, H) 76} 77 78func report(logfd: i64, label: *u8, hold: i64, lpf: i64, cls: i64) -> i64 { 79 gp(logfd, label) 80 gp(logfd, " max_hold=\x00" as *u8); gn(logfd, hold) 81 gp(logfd, " loop=\x00" as *u8); gn(logfd, lpf) 82 gp(logfd, " class=\x00" as *u8) 83 if cls == NX_REC_RECORDING { gp(logfd, "RECORDING\x00" as *u8) } 84 if cls == NX_REC_LIVE { gp(logfd, "LIVE\x00" as *u8) } 85 gp(logfd, "\n\x00" as *u8) 86 return 0 87} 88 89func main() -> i64 { 90 let logfd: i64 = sys_openat_append("knowledge/status/recording_gate.log\x00" as *u8, 0x1a4) 91 gp(logfd, "RECORDING-GATE MEDIA-STUDIO R1 N=\x00" as *u8); gn(logfd, N) 92 gp(logfd, " near=\x00" as *u8); gn(logfd, NEAR) 93 gp(logfd, " hold>=\x00" as *u8); gn(logfd, HOLD) 94 gp(logfd, " min_period=\x00" as *u8); gn(logfd, MINP); gp(logfd, "\n\x00" as *u8) 95 96 let buf: *u8 = sys_mmap(W * H) 97 let live: *i64 = sys_mmap(N * 8) as *i64 98 let hold: *i64 = sys_mmap(N * 8) as *i64 99 let lpq: *i64 = sys_mmap(N * 8) as *i64 100 let freeze: *i64 = sys_mmap(N * 8) as *i64 101 102 // LIVE: 16 distinct random frames (distinct seeds). 103 var i: i64 = 0 104 while i < N { live[i] = hash_seeded(buf, 1000 + i * 7919); i = i + 1 } 105 106 // HOLD: same frame x16. 107 let hfp: i64 = hash_seeded(buf, 424242) 108 i = 0 109 while i < N { hold[i] = hfp; i = i + 1 } 110 111 // LOOP: 4 distinct frames cycled with period 4. 112 let p0: i64 = hash_seeded(buf, 5000) 113 let p1: i64 = hash_seeded(buf, 5000 + 7919) 114 let p2: i64 = hash_seeded(buf, 5000 + 15838) 115 let p3: i64 = hash_seeded(buf, 5000 + 23757) 116 i = 0 117 while i < N { 118 let r: i64 = i % 4 119 if r == 0 { lpq[i] = p0 } 120 if r == 1 { lpq[i] = p1 } 121 if r == 2 { lpq[i] = p2 } 122 if r == 3 { lpq[i] = p3 } 123 i = i + 1 124 } 125 126 // FREEZE: distinct frames, but frames 6,7,8 are a 3-frame freeze. 127 let ffp: i64 = hash_seeded(buf, 77777) 128 i = 0 129 while i < N { 130 freeze[i] = hash_seeded(buf, 9000 + i * 7919) 131 if i == 6 { freeze[i] = ffp } 132 if i == 7 { freeze[i] = ffp } 133 if i == 8 { freeze[i] = ffp } 134 i = i + 1 135 } 136 137 // ---- measurements ---- 138 let live_h: i64 = nx_rec_max_hold(live, N, NEAR) 139 let live_l: i64 = nx_rec_has_loop(live, N, NEAR, MINP) 140 let live_c: i64 = nx_rec_classify(live, N, NEAR, HOLD, MINP) 141 report(logfd, " live \x00" as *u8, live_h, live_l, live_c) 142 143 let hold_h: i64 = nx_rec_max_hold(hold, N, NEAR) 144 let hold_l: i64 = nx_rec_has_loop(hold, N, NEAR, MINP) 145 let hold_c: i64 = nx_rec_classify(hold, N, NEAR, HOLD, MINP) 146 report(logfd, " hold \x00" as *u8, hold_h, hold_l, hold_c) 147 148 let loop_h: i64 = nx_rec_max_hold(lpq, N, NEAR) 149 let loop_l: i64 = nx_rec_has_loop(lpq, N, NEAR, MINP) 150 let loop_c: i64 = nx_rec_classify(lpq, N, NEAR, HOLD, MINP) 151 report(logfd, " loop \x00" as *u8, loop_h, loop_l, loop_c) 152 153 let frz_h: i64 = nx_rec_max_hold(freeze, N, NEAR) 154 let frz_l: i64 = nx_rec_has_loop(freeze, N, NEAR, MINP) 155 let frz_c: i64 = nx_rec_classify(freeze, N, NEAR, HOLD, MINP) 156 report(logfd, " freeze \x00" as *u8, frz_h, frz_l, frz_c) 157 158 let d01: i64 = nx_simhash_hamming(live[0], live[1]) 159 gp(logfd, " sanity ham(live0,live1)=\x00" as *u8); gn(logfd, d01); gp(logfd, "\n\x00" as *u8) 160 161 // ---- verdict ---- 162 var ok: i64 = 1 163 if live_c != NX_REC_LIVE { ok = 0 } // distinct frames = live 164 if live_h >= HOLD { ok = 0 } 165 if live_l != 0 { ok = 0 } 166 if hold_c != NX_REC_RECORDING { ok = 0 } // frozen slate = recording 167 if hold_h < HOLD { ok = 0 } 168 if loop_c != NX_REC_RECORDING { ok = 0 } // looping clip = recording 169 if loop_l != 1 { ok = 0 } 170 if frz_c != NX_REC_LIVE { ok = 0 } // NEG-CONTROL: brief freeze stays live 171 if frz_h < 3 { ok = 0 } // ...and the freeze IS detected as a 3-run 172 if frz_l != 0 { ok = 0 } 173 if d01 <= NEAR { ok = 0 } // distinct frames really differ 174 175 if ok == 1 { 176 gp(logfd, "RECORDING-GATE result=ALL-PASS verdict=GREEN\n\x00" as *u8) 177 if logfd > 0 { sys_close(logfd) } 178 sys_exit(0) 179 return 0 180 } 181 gp(logfd, "RECORDING-GATE result=FAIL verdict=RED\n\x00" as *u8) 182 if logfd > 0 { sys_close(logfd) } 183 sys_exit(1) 184 return 1 185}