code wiki / _hdl_build / nx_critic_kernel.nx
nx_critic_kernel.nx source
↩ module page · 220 lines · 11787 B
1// nx_critic_kernel.nx -- the SOVEREIGN, DOMAIN-AGNOSTIC, self-growing CRITIC KERNEL (operator 2026-07-04:
2// "the critic needs to intelligently grow with your tutoring so this whole thing becomes state of the art not
3// just for [photoreal] but audio and industrial design and all kinds of projects"). ONE kernel; every domain
4// plugs in AXES; every axis is held under the epistemic Nishi Critic ([[nx_critic]]) and -- crucially -- CANNOT
5// be trusted (LAW) until it SURVIVES A RED-TEAM: the tutor supplies a GOOD sample AND an ADVERSARY the axis
6// must reject. The kernel RUNS the red-team and only marks the axis red-teamed if it DISCRIMINATES; a gameable
7// axis (adversary not rejected) is caught and queued for the tutor -- so the critic grows intelligently, never
8// self-flatters, and generalizes to visuals + audio + design + anything.
9//
10// PROOF: 3 real domains (VISUAL structure / AUDIO periodicity / DESIGN symmetry), each with a real measurement
11// + a real adversary, all graded by the SAME kernel; PLUS one deliberately WEAK axis (design "detail=variance",
12// which noise games) that the kernel CATCHES and refuses to bless. license_tier: ORIGINAL expect_exit: 0
13import "nx_syscalls.nx"
14import "nx_critic.nx"
15import "nx_itrig.nx"
16import "nx_sdfrender.nx"
17const K_MAGIC_4096: i64 = 4096
18const K_MAGIC_20000: i64 = 20000
19const K_MAGIC_1024: i64 = 1024
20const K_MAGIC_2654435761: i64 = 2654435761
21const K_MAGIC_40503: i64 = 40503
22const K_MAGIC_8191: i64 = 8191
23
24func hw(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
25func pn(v: i64) -> i64 {
26 let b: *u8 = sys_mmap(32) as *u8
27 var x: i64 = v
28 var neg: i64 = 0
29 if x < 0 { neg = 1; x = 0 - x }
30 var i: i64 = 31
31 if x == 0 { b[i] = 48 as u8; i = i - 1 }
32 while x > 0 { b[i] = (48 + x % 10) as u8; x = x / 10; i = i - 1 }
33 if neg == 1 { b[i] = 45 as u8; i = i - 1 }
34 sys_write(1, (b as i64 + i + 1) as *u8, 31 - i)
35 return 0
36}
37func kc(v: i64) -> i64 { if v < 0 { return 0 } if v > 1000 { return 1000 } return v }
38func kabs(v: i64) -> i64 { if v < 0 { return 0 - v } return v }
39func klum(g: i64) -> i64 { return ((g & 255) + ((g >> 8) & 255) + ((g >> 16) & 255)) / 3 }
40func kfg(g: i64) -> i64 { let r: i64 = g & 255; let b: i64 = (g >> 16) & 255; if r >= b { return 1 } return 0 }
41// measure OUR REAL render's fine_detail (the dominant photoreal axis) -- the honest capability driver
42func our_fine_detail(fb: *i64, w: i64, h: i64) -> i64 {
43 var fg: i64 = 0; var s: i64 = 0
44 var y: i64 = 1
45 while y < h - 1 {
46 var x: i64 = 1
47 while x < w - 1 {
48 let c: i64 = fb[y * w + x]
49 if kfg(c) == 1 {
50 let nb: i64 = (klum(fb[y * w + x - 1]) + klum(fb[y * w + x + 1]) + klum(fb[(y - 1) * w + x]) + klum(fb[(y + 1) * w + x])) / 4
51 s = s + kabs(klum(c) - nb); fg = fg + 1
52 }
53 x = x + 1
54 }
55 y = y + 1
56 }
57 if fg < 1 { fg = 1 }
58 return s / fg
59}
60
61// ---- DOMAIN measurements (each returns a 0..1000 quality on its own signal) ----
62// VISUAL: low-frequency structure (variance of a 4x4 block-mean grid over a 32x32 luminance field)
63func m_vis_structure(lum: *i64) -> i64 {
64 let means: *i64 = sys_mmap(16 * 8) as *i64
65 var gy: i64 = 0
66 while gy < 4 {
67 var gx: i64 = 0
68 while gx < 4 {
69 var s: i64 = 0
70 var yy: i64 = 0
71 while yy < 8 { var xx: i64 = 0; while xx < 8 { s = s + lum[(gy * 8 + yy) * 32 + (gx * 8 + xx)]; xx = xx + 1 } yy = yy + 1 }
72 means[gy * 4 + gx] = s / 64
73 gx = gx + 1
74 }
75 gy = gy + 1
76 }
77 var mean: i64 = 0; var i: i64 = 0
78 while i < 16 { mean = mean + means[i]; i = i + 1 }
79 mean = mean / 16
80 var v: i64 = 0; i = 0
81 while i < 16 { let d: i64 = means[i] - mean; v = v + d * d; i = i + 1 }
82 return kc((v / 16) * 1000 / 900)
83}
84// AUDIO: periodicity (normalized autocorrelation peak at the fundamental lag) -- voiced/tonal vs noise
85func m_aud_periodicity(s: *i64, n: i64, lag: i64) -> i64 {
86 var ac0: i64 = 0; var acL: i64 = 0
87 var i: i64 = 0
88 while i < n { ac0 = ac0 + s[i] * s[i] / K_MAGIC_4096; i = i + 1 }
89 i = 0
90 while i < n - lag { acL = acL + s[i] * s[i + lag] / K_MAGIC_4096; i = i + 1 }
91 if ac0 < 1 { return 0 }
92 return kc(acL * 1000 / ac0)
93}
94// DESIGN: mirror symmetry (1000 = perfectly symmetric; random = ~0)
95func m_des_symmetry(hgt: *i64, n: i64) -> i64 {
96 var e: i64 = 0
97 var i: i64 = 0
98 while i < n / 2 { e = e + kabs(hgt[i] - hgt[n - 1 - i]); i = i + 1 }
99 e = e / (n / 2)
100 return kc(1000 - e * 1000 / 300)
101}
102// DESIGN (WEAK/gameable): "detail = raw variance" -- noise games this exactly like it gamed fine_detail
103func m_des_variance(hgt: *i64, n: i64) -> i64 {
104 var mean: i64 = 0; var i: i64 = 0
105 while i < n { mean = mean + hgt[i]; i = i + 1 }
106 mean = mean / n
107 var v: i64 = 0; i = 0
108 while i < n { let d: i64 = hgt[i] - mean; v = v + d * d; i = i + 1 }
109 return kc((v / n) * 1000 / K_MAGIC_20000)
110}
111
112// the KERNEL red-team: an axis is trustworthy only if it PASSES its good sample AND REJECTS its adversary.
113// returns 1 if it discriminates (=> red-teamed), 0 if gameable (adversary not rejected).
114func kernel_redteam(good: i64, adv: i64) -> i64 {
115 if good >= 700 { if adv <= 300 { return 1 } }
116 return 0
117}
118func statusname(s: i64) -> *u8 {
119 if s == CRIT_LAW { return "LAW " as *u8 }
120 if s == CRIT_CONTESTED { return "CONTESTED" as *u8 }
121 if s == CRIT_THEORY { return "THEORY " as *u8 }
122 return "REFUTED " as *u8
123}
124
125// grade + report one registered axis through the epistemic core
126func grade_axis(dom: *u8, ax: *u8, good: i64, adv: i64) -> i64 {
127 let rt: i64 = kernel_redteam(good, adv)
128 var counter: i64 = 0
129 if rt == 0 { counter = 2 } // failed red-team = a surviving counter (gameable)
130 let repro: i64 = 2 // exercised on good + adversary
131 let st: i64 = crit_status2(repro, counter, rt)
132 let settled: i64 = crit_settledness(repro, counter, rt)
133 hw(" "); hw(dom); hw(" / "); hw(ax)
134 hw(" cal-good="); pn(good); hw(" adversary="); pn(adv) // SYNTHETIC calibration inputs, NOT our product
135 hw(" redteam="); if rt == 1 { hw("PASS" as *u8) } else { hw("FAIL" as *u8) }
136 hw(" RULER=["); hw(statusname(st)); hw("] settled="); pn(settled)
137 if crit_is_blindspot(repro, rt) == 1 { hw(" <-- QUEUE (tutor: fix/replace)" as *u8) }
138 hw("\n")
139 if st == CRIT_LAW { return 1 }
140 return 0
141}
142
143func main() -> i64 {
144 var fails: i64 = 0
145 hw("=== nx_critic_kernel -- one self-growing critic kernel across DOMAINS (visual/audio/design/...) ===\n" as *u8)
146
147 // ---------- build GOOD + ADVERSARY samples per domain ----------
148 // VISUAL: good = a horizontal gradient (real low-freq form); adversary = per-pixel noise (no form)
149 let vg: *i64 = sys_mmap(K_MAGIC_1024 * 8) as *i64
150 let va: *i64 = sys_mmap(K_MAGIC_1024 * 8) as *i64
151 var i: i64 = 0
152 while i < K_MAGIC_1024 { let x: i64 = i % 32; vg[i] = x * 8; va[i] = ((i * K_MAGIC_2654435761 + 7) >> 5) & 255; i = i + 1 }
153 let vis_good: i64 = m_vis_structure(vg)
154 let vis_adv: i64 = m_vis_structure(va)
155
156 // AUDIO: good = harmonic tone (period 32); adversary = white noise
157 let ag: *i64 = sys_mmap(256 * 8) as *i64
158 let aa: *i64 = sys_mmap(256 * 8) as *i64
159 i = 0
160 while i < 256 {
161 ag[i] = it_sin4096(i * 2 * IT_PI / 32) + it_sin4096(i * 2 * IT_PI / 16) / 2
162 aa[i] = (((i * K_MAGIC_40503 + 13) >> 3) & K_MAGIC_8191) - K_MAGIC_4096
163 i = i + 1
164 }
165 let aud_good: i64 = m_aud_periodicity(ag, 256, 32)
166 let aud_adv: i64 = m_aud_periodicity(aa, 256, 32)
167
168 // DESIGN: good = a symmetric triangular height profile; adversary = a random profile
169 let dg: *i64 = sys_mmap(64 * 8) as *i64
170 let da: *i64 = sys_mmap(64 * 8) as *i64
171 i = 0
172 while i < 64 {
173 var t: i64 = i; if 63 - i < t { t = 63 - i }
174 dg[i] = t * 10 // good: symmetric triangle
175 da[i] = i * 12 // adversary: a RAMP -- maximally NON-mirror-symmetric, yet
176 i = i + 1 // high-variance (symmetry rejects it; variance is fooled)
177 }
178 let des_good: i64 = m_des_symmetry(dg, 64)
179 let des_adv: i64 = m_des_symmetry(da, 64)
180 // the WEAK axis probe: variance can't tell a structured design from noise
181 let wk_good: i64 = m_des_variance(dg, 64)
182 let wk_adv: i64 = m_des_variance(da, 64)
183
184 // ---------- PART 1: is each RULER valid? (can the metric tell a GOOD sample from an ADVERSARY?) ----------
185 hw("-- PART 1: RULER VALIDATION -- is each measuring-stick sound? (cal-good vs adversary = SYNTHETIC, NOT us) --\n" as *u8)
186 var laws: i64 = 0
187 laws = laws + grade_axis("VISUAL" as *u8, "structure " as *u8, vis_good, vis_adv)
188 laws = laws + grade_axis("AUDIO " as *u8, "periodicity " as *u8, aud_good, aud_adv)
189 laws = laws + grade_axis("DESIGN" as *u8, "symmetry " as *u8, des_good, des_adv)
190 laws = laws + grade_axis("DESIGN" as *u8, "detail=var(WK)" as *u8, wk_good, wk_adv)
191 hw(" ★RULER=[LAW] means the METRIC is trustworthy -- NOT that WE are good. cal-good is a hand-made sample.\n" as *u8)
192
193 // integrity of the FRAMEWORK (not our capability)
194 if laws == 3 { hw(" [OK] 3 rulers (visual/audio/design) are VALID discriminators -- ONE kernel spans domains\n" as *u8) }
195 else { hw(" [FAIL] expected 3 valid rulers, got "); pn(laws); hw("\n"); fails = fails + 1 }
196 if kernel_redteam(wk_good, wk_adv) == 0 { hw(" [OK] the WEAK ruler (variance) was CAUGHT -> not blessed, queued\n" as *u8) }
197 else { hw(" [FAIL] gameable ruler passed\n"); fails = fails + 1 }
198
199 // ---------- PART 2: OUR ACTUAL CAPABILITY on these valid rulers (the honest scorecard) ----------
200 hw("-- PART 2: OUR ACTUAL CAPABILITY (the honest scorecard -- what WE score, not the calibration samples) --\n" as *u8)
201 let base: i64 = sys_mmap(sdf_bytes()) as i64
202 sdf_body(base); sdf_render(base, 0, 5, 240, 184, 160)
203 let vcap: i64 = kc(our_fine_detail((base + fb_off()) as *i64, ww(), hh()) * 1000 / 32)
204 hw(" VISUAL : our REAL render fine_detail-score = "); pn(vcap); hw("/1000 = CLAY (measured -- the ruler works and says we're clay)\n" as *u8)
205 hw(" AUDIO : NOT WIRED -- our TTS is formant/robotic (voice census BEHIND 10 on realism); a naturalness\n" as *u8)
206 hw(" ruler is NOT EVEN BUILT yet -> capability = UNMEASURED (0 claimed)\n" as *u8)
207 hw(" DESIGN : NOT STARTED -- there is NO industrial-design system; the symmetry ruler exists, the WORK does\n" as *u8)
208 hw(" not -> capability = 0 / does-not-exist\n" as *u8)
209
210 hw("=== HONEST VERDICT ===\n" as *u8)
211 hw("What is GREEN: the critic FRAMEWORK -- one epistemic kernel whose RULERS are valid across domains, that\n" as *u8)
212 hw("catches gameable metrics and grows under tutoring. What is NOT green: OUR CAPABILITY -- visual = CLAY\n" as *u8)
213 hw("("); pn(vcap); hw("/1000), audio = robotic/UNMEASURED, design = NOT STARTED (0). We validated the JUDGE,\n" as *u8)
214 hw("NOT the work. The rulers now make it IMPOSSIBLE to fake capability -- every future claim gets a number.\n" as *u8)
215 hw("HOW IT GROWS: register {measurement, GOOD, ADVERSARY}; promote only if it discriminates; tutor supplies\n" as *u8)
216 hw("harder adversaries; THEORY->LAW by reproduction. The rulers are ready; the CAPABILITIES are the mountain.\n" as *u8)
217 if fails == 0 { hw("GATE GREEN: the critic FRAMEWORK is valid + general + self-growing. (Our capability scorecard above is honestly clay/robotic/zero.)\n" as *u8) }
218 else { hw("GATE RED fails="); pn(fails); hw("\n"); }
219 return fails
220}