code wiki / (root) / nx_meta_verdict.nx

nx_meta_verdict.nx source

↩ module page · 422 lines · 18387 B

1// nx_meta_verdict.nx -- META "God saw it was good" composer over layer verdicts. 2// 3// Per user 2026-05-16: "always layer by layer and overall meta have 4// a god is this good and it should do that based on the math like we 5// were building in the elder ai for images and video and sound" 6// 7// Genesis 1:31 -- "And God saw every thing that he had made, and, 8// behold, it was very good." This primitive ships the SABBATH-LEVEL 9// VERDICT: given N per-layer LAYER_VERDICT records (from 10// nx_layer_verdict.nx), compute a single overall META verdict that 11// says whether the whole world is "good" or not. 12// 13// Mathematical posture (matches Elder AI's overall-quality grader): 14// 15// 1. Hard floor rule: ANY layer at F -> META F (one broken layer 16// breaks the world; you cannot polish a turd). 17// 2. Sabbath rule: ALL layers >= A AND no failing axis below 18// MARGINAL across the whole set -> META = min(layer grades). 19// 3. Geometric-mean rule: otherwise, the META grade is computed 20// from the geometric-mean axis score across ALL layers, 21// penalised by the count of below-threshold axes. 22// 4. Min-axis floor: if any single axis across any layer is below 23// 0.2Q, the META grade is capped at C (one terrible axis 24// drags the whole world down). 25// 26// These mathematical rules implement "God saw" -- a deterministic 27// pass/fail that the iterative procgen loop can use to decide 28// whether to refine, accept, or regenerate. 29// 30// META_VERDICT layout (16 i64): 31// m[0] overall_grade F=0..S=5 32// m[1] n_layers total input layers 33// m[2] worst_layer_kind sealed enum (the dragging layer) 34// m[3] worst_layer_grade its grade 35// m[4] worst_axis_q14 lowest axis score across all layers 36// m[5] n_layers_S 37// m[6] n_layers_A 38// m[7] n_layers_BCD B + C + D counts 39// m[8] n_layers_F hard-failures 40// m[9] mean_grade_q14 average grade across layers 41// m[10] geometric_axis_mean_q14 ~ N-th root of product of all axis scores 42// m[11] n_loss_axes total axes scored LOSS across all layers 43// m[12] n_win_axes total axes scored WIN 44// m[13] god_said_good 1 if META >= B AND no F layer, else 0 45// m[14] refine_priority sealed enum: which layer to refine first 46// m[15] reserved 0 (future axis-priority weighting) 47// 48// genealogy_id: elder_ai_overall_grader_canon + 49// genesis_1_31_god_saw_good + 50// nx_quality_grade_sclass_canon 51// lineage_id: nx_meta_verdict_god_saw_good_v1 52 53// nx_safety_envelope: 54// intended_use: AUTO_APPLIED -- primitive-specific tuning queued 55// sil_target: SIL1 56// evidence: [bulk_applied_2026-05-16, see-file-comment-for-detail] 57// verdict: NOT_YET_EVALUATED 58 59import "nx_syscalls.nx" 60import "nx_tier.nx" 61import "nx_layer_verdict.nx" 62const NX_MAGIC_16384: i64 = 16384 63const NX_MAGIC_25976: i64 = 25976 64const NX_MAGIC_32768: i64 = 32768 65const NX_MAGIC_38048: i64 = 38048 66const NX_MAGIC_42361: i64 = 42361 67const NX_MAGIC_46006: i64 = 46006 68const NX_MAGIC_49152: i64 = 49152 69const NX_MAGIC_51916: i64 = 51916 70const NX_MAGIC_54432: i64 = 54432 71const NX_MAGIC_56619: i64 = 56619 72const NX_MAGIC_58744: i64 = 58744 73const NX_MAGIC_60686: i64 = 60686 74const NX_MAGIC_62390: i64 = 62390 75const NX_MAGIC_63984: i64 = 63984 76const NX_MAGIC_65536: i64 = 65536 77 78// ===== Q14 ========================================================== 79const NX_MV_Q: nx_int = 16384 80 81// ===== META_VERDICT layout ========================================= 82const NX_MV_STRIDE: nx_int = 16 83 84const NX_MV_OFF_GRADE: nx_int = 0 85const NX_MV_OFF_N_LAYERS: nx_int = 1 86const NX_MV_OFF_WORST_LAYER_KIND: nx_int = 2 87const NX_MV_OFF_WORST_LAYER_GRADE: nx_int = 3 88const NX_MV_OFF_WORST_AXIS_Q14: nx_int = 4 89const NX_MV_OFF_N_LAYERS_S: nx_int = 5 90const NX_MV_OFF_N_LAYERS_A: nx_int = 6 91const NX_MV_OFF_N_LAYERS_BCD: nx_int = 7 92const NX_MV_OFF_N_LAYERS_F: nx_int = 8 93const NX_MV_OFF_MEAN_GRADE_Q14: nx_int = 9 94const NX_MV_OFF_GEO_AXIS_MEAN_Q14: nx_int = 10 95const NX_MV_OFF_N_LOSS_AXES: nx_int = 11 96const NX_MV_OFF_N_WIN_AXES: nx_int = 12 97const NX_MV_OFF_GOD_SAID_GOOD: nx_int = 13 98const NX_MV_OFF_REFINE_PRIORITY: nx_int = 14 99 100// ===== Min-axis floor (capped-grade trigger) ======================= 101const NX_MV_MIN_AXIS_FLOOR_Q14: nx_int = 3277 // 0.2Q 102 103// ===== Internal: log2(bin) for bin in [1, 16] (Q14) =============== 104func _mv_log2_q14(bin: nx_int) -> nx_int { 105 if bin <= 1 { return 0 } 106 if bin == 2 { return NX_MAGIC_16384 } 107 if bin == 3 { return NX_MAGIC_25976 } 108 if bin == 4 { return NX_MAGIC_32768 } 109 if bin == 5 { return NX_MAGIC_38048 } 110 if bin == 6 { return NX_MAGIC_42361 } 111 if bin == 7 { return NX_MAGIC_46006 } 112 if bin == 8 { return NX_MAGIC_49152 } 113 if bin == 9 { return NX_MAGIC_51916 } 114 if bin == 10 { return NX_MAGIC_54432 } 115 if bin == 11 { return NX_MAGIC_56619 } 116 if bin == 12 { return NX_MAGIC_58744 } 117 if bin == 13 { return NX_MAGIC_60686 } 118 if bin == 14 { return NX_MAGIC_62390 } 119 if bin == 15 { return NX_MAGIC_63984 } 120 return NX_MAGIC_65536 121} 122 123// ===== Internal: 2^x for x in [-4Q, 0] in Q14 ===================== 124func _mv_pow2_q14(x_q14: nx_int) -> nx_int { 125 let q: nx_int = NX_MV_Q 126 if x_q14 >= 0 { return q } 127 if x_q14 <= 0 - 4 * q { return q / 16 } 128 let neg: nx_int = 0 - x_q14 129 if neg <= q { 130 let f: nx_int = q - neg 131 return (q / 2) + (q / 2) * f / q 132 } 133 if neg <= 2 * q { 134 let f: nx_int = 2 * q - neg 135 return (q / 4) + (q / 4) * f / q 136 } 137 if neg <= 3 * q { 138 let f: nx_int = 3 * q - neg 139 return (q / 8) + (q / 8) * f / q 140 } 141 let f: nx_int = 4 * q - neg 142 return (q / 16) + (q / 16) * f / q 143} 144 145// ===== Internal: find worst layer index ============================ 146func _mv_find_worst_layer_idx( 147 layer_verdicts: *i64, n_layers: nx_int 148) -> nx_int { 149 var idx: nx_int = 0 150 var worst: nx_int = NX_LV_GRADE_S 151 var i: nx_int = 0 152 while i < n_layers { 153 let lv: *i64 = (layer_verdicts as i64 + i * NX_LV_STRIDE * NX_SIZEOF_NX_INT) as *i64 154 let g: nx_int = lv[NX_LV_OFF_GRADE] 155 if g < worst { worst = g; idx = i } 156 i = i + 1 157 } 158 return idx 159} 160 161// ===== Composer ==================================================== 162// Aggregates N LAYER_VERDICT records into one META_VERDICT. 163// 164// layer_verdicts: pointer to N consecutive 16-i64 LAYER_VERDICT records. 165// n_layers: number of layers. 166// out: 16-i64 META_VERDICT buffer. 167func nx_meta_verdict_compose( 168 layer_verdicts: *i64, 169 n_layers: nx_int, 170 out: *i64 171) { 172 let q: nx_int = NX_MV_Q 173 174 // Init. 175 var k: nx_int = 0 176 while k < NX_MV_STRIDE { out[k] = 0; k = k + 1 } 177 out[NX_MV_OFF_GRADE] = NX_LV_GRADE_F 178 out[NX_MV_OFF_N_LAYERS] = n_layers 179 out[NX_MV_OFF_WORST_LAYER_KIND] = 0 - 1 180 out[NX_MV_OFF_WORST_AXIS_Q14] = q 181 182 if n_layers <= 0 { return } 183 184 var sum_grade: nx_int = 0 185 var n_S: nx_int = 0 186 var n_A: nx_int = 0 187 var n_BCD: nx_int = 0 188 var n_F: nx_int = 0 189 var n_loss_axes: nx_int = 0 190 var n_win_axes: nx_int = 0 191 var min_axis: nx_int = q 192 var worst_grade: nx_int = NX_LV_GRADE_S 193 var worst_kind: nx_int = 0 - 1 194 195 // Geometric-mean tracking. We compute log2-sum of axis scores 196 // to avoid i64 overflow on direct product. At end: 197 // geom_mean = 2^(log2_sum / total_axes) 198 // Stored as Q14. Use a small log2 table (1..16 bin lookup). 199 var log2_sum_q14: nx_int = 0 200 var total_axes: nx_int = 0 201 202 var li: nx_int = 0 203 while li < n_layers { 204 let lv: *i64 = (layer_verdicts as i64 + li * NX_LV_STRIDE * NX_SIZEOF_NX_INT) as *i64 205 let g: nx_int = lv[NX_LV_OFF_GRADE] 206 let kind: nx_int = lv[NX_LV_OFF_KIND] 207 sum_grade = sum_grade + g 208 if g == NX_LV_GRADE_S { n_S = n_S + 1 } 209 if g == NX_LV_GRADE_A { n_A = n_A + 1 } 210 if g == NX_LV_GRADE_F { n_F = n_F + 1 } 211 if g >= NX_LV_GRADE_D { if g <= NX_LV_GRADE_B { n_BCD = n_BCD + 1 } } 212 if g < worst_grade { worst_grade = g; worst_kind = kind } 213 if g == worst_grade { 214 if worst_kind == 0 - 1 { worst_kind = kind } 215 } 216 217 let n_axes: nx_int = lv[NX_LV_OFF_N_AXES] 218 var ai: nx_int = 0 219 while ai < n_axes { 220 if ai < NX_LV_MAX_AXES { 221 let s: nx_int = lv[NX_LV_OFF_AXIS_0 + ai] 222 let v: nx_int = nx_lv_axis_verdict(s) 223 if v == 1 { n_win_axes = n_win_axes + 1 } 224 if v == 0 - 1 { n_loss_axes = n_loss_axes + 1 } 225 if s < min_axis { min_axis = s } 226 // log2 approximation: bin s/Q into 16 buckets and look up. 227 var bin: nx_int = (s * 16) / q 228 if bin < 1 { bin = 1 } 229 if bin > 16 { bin = 16 } 230 // log2(bin/16) in Q14: log2(bin) - log2(16) = log2(bin) - 4. 231 // Use _mv_log2_q14(bin) helper. 232 log2_sum_q14 = log2_sum_q14 + (_mv_log2_q14(bin) - 4 * q) 233 total_axes = total_axes + 1 234 } 235 ai = ai + 1 236 } 237 li = li + 1 238 } 239 240 // Compute aggregates. 241 let mean_grade_q14: nx_int = (sum_grade * q) / n_layers 242 out[NX_MV_OFF_N_LAYERS_S] = n_S 243 out[NX_MV_OFF_N_LAYERS_A] = n_A 244 out[NX_MV_OFF_N_LAYERS_BCD] = n_BCD 245 out[NX_MV_OFF_N_LAYERS_F] = n_F 246 out[NX_MV_OFF_MEAN_GRADE_Q14] = mean_grade_q14 247 out[NX_MV_OFF_N_LOSS_AXES] = n_loss_axes 248 out[NX_MV_OFF_N_WIN_AXES] = n_win_axes 249 out[NX_MV_OFF_WORST_LAYER_KIND] = worst_kind 250 out[NX_MV_OFF_WORST_LAYER_GRADE] = worst_grade 251 out[NX_MV_OFF_WORST_AXIS_Q14] = min_axis 252 253 // Geometric-mean axis score: 2^(log2_sum / total_axes). Q14. 254 var geo_axis_mean: nx_int = 0 255 if total_axes > 0 { 256 let mean_log2_q14: nx_int = log2_sum_q14 / total_axes 257 // 2^(mean_log2): if x = mean_log2 in Q14 metres (= mean_log2 / q), 258 // and we want 2^x as a Q14 fraction. For x in [-4, 0]: 259 // x = 0 -> 1.0 Q 260 // x = -1 -> 0.5 Q 261 // x = -2 -> 0.25 Q 262 // x = -3 -> 0.125 Q 263 // x = -4 -> 0.0625 Q 264 // Piecewise-linear over these 5 anchors. 265 geo_axis_mean = _mv_pow2_q14(mean_log2_q14) 266 } 267 out[NX_MV_OFF_GEO_AXIS_MEAN_Q14] = geo_axis_mean 268 269 // Rule 1: ANY layer F -> META F. 270 if n_F > 0 { 271 out[NX_MV_OFF_GRADE] = NX_LV_GRADE_F 272 out[NX_MV_OFF_GOD_SAID_GOOD] = 0 273 out[NX_MV_OFF_REFINE_PRIORITY] = NX_LAYER_REFINE_REPLACE_LAYER 274 return 275 } 276 277 // Rule 2: ALL layers >= A AND no loss axes -> min(layer grade). 278 let n_AS: nx_int = n_S + n_A 279 if n_AS == n_layers { 280 if n_loss_axes == 0 { 281 out[NX_MV_OFF_GRADE] = worst_grade 282 out[NX_MV_OFF_GOD_SAID_GOOD] = 1 283 out[NX_MV_OFF_REFINE_PRIORITY] = NX_LAYER_REFINE_NONE 284 return 285 } 286 } 287 288 // Rule 3: derive grade from geometric-axis-mean + grade-mean. 289 // grade_letter = floor(mean_grade) typically; bump down if many 290 // loss axes; bump up if grade-mean is high AND no loss axes. 291 var derived_grade: nx_int = mean_grade_q14 / q // floor 292 // If we have any losses, cap at one below mean. 293 if n_loss_axes > 0 { 294 if total_axes > 0 { 295 let loss_ratio_q: nx_int = (n_loss_axes * q) / total_axes 296 if loss_ratio_q >= q / 4 { derived_grade = derived_grade - 1 } // >25% loss 297 if loss_ratio_q >= q / 2 { derived_grade = derived_grade - 1 } // >50% loss 298 } 299 } 300 if derived_grade < NX_LV_GRADE_F { derived_grade = NX_LV_GRADE_F } 301 if derived_grade > NX_LV_GRADE_S { derived_grade = NX_LV_GRADE_S } 302 303 // Rule 4: min-axis floor. If any axis is below 0.2Q, cap at C. 304 if min_axis < NX_MV_MIN_AXIS_FLOOR_Q14 { 305 if derived_grade > NX_LV_GRADE_C { derived_grade = NX_LV_GRADE_C } 306 } 307 308 out[NX_MV_OFF_GRADE] = derived_grade 309 310 // GOD_SAID_GOOD: meta-grade >= B AND no F layer (already filtered). 311 var gsg: nx_int = 0 312 if derived_grade >= NX_LV_GRADE_B { gsg = 1 } 313 out[NX_MV_OFF_GOD_SAID_GOOD] = gsg 314 315 // Refine priority: the layer pulling the verdict down. 316 if worst_kind >= 0 { 317 if worst_grade < NX_LV_GRADE_B { 318 // Match the worst-layer's own refine hint if available. 319 let worst_lv: *i64 = (layer_verdicts as i64 + 320 _mv_find_worst_layer_idx(layer_verdicts, n_layers) * 321 NX_LV_STRIDE * NX_SIZEOF_NX_INT) as *i64 322 out[NX_MV_OFF_REFINE_PRIORITY] = worst_lv[NX_LV_OFF_REFINE_HINT] 323 } 324 } 325} 326 327// ===== God-said-good helper (the user's "is this good" check) ==== 328// Quick predicate for the iterative procgen loop: 329// return 1 if META verdict passes Sabbath rule (overall grade >= B 330// and no F layer; matches the GOD_SAID_GOOD field). 331// Caller should call this AFTER nx_meta_verdict_compose. 332func nx_god_said_good(meta: *i64) -> nx_int { 333 return meta[NX_MV_OFF_GOD_SAID_GOOD] 334} 335 336// ===== Self-test ==================================================== 337func main() -> i64 { 338 let q: nx_int = NX_MV_Q 339 340 // Build a tiny test world with 3 layers. 341 let layers: *i64 = (sys_mmap(3 * NX_LV_STRIDE * NX_SIZEOF_NX_INT)) as *i64 342 let l0: *i64 = layers 343 let l1: *i64 = (layers as i64 + NX_LV_STRIDE * NX_SIZEOF_NX_INT) as *i64 344 let l2: *i64 = (layers as i64 + 2 * NX_LV_STRIDE * NX_SIZEOF_NX_INT) as *i64 345 let meta: *i64 = (sys_mmap(NX_MV_STRIDE * NX_SIZEOF_NX_INT)) as *i64 346 347 let axes: *i64 = (sys_mmap(NX_LV_MAX_AXES * NX_SIZEOF_NX_INT)) as *i64 348 349 // T1: 3 layers all S -> META S; GOD_SAID_GOOD=1. 350 var i: nx_int = 0 351 while i < 8 { axes[i] = q; i = i + 1 } 352 nx_layer_verdict_write(l0, NX_LAYER_KIND_HEIGHTMAP, 8, NX_LAYER_REFINE_NONE, axes) 353 nx_layer_verdict_write(l1, NX_LAYER_KIND_FOREST, 8, NX_LAYER_REFINE_NONE, axes) 354 nx_layer_verdict_write(l2, NX_LAYER_KIND_RIVER, 8, NX_LAYER_REFINE_NONE, axes) 355 nx_meta_verdict_compose(layers, 3, meta) 356 if meta[NX_MV_OFF_GRADE] != NX_LV_GRADE_S { return __syscall(93, 1, 0, 0, 0, 0, 0) } 357 if meta[NX_MV_OFF_GOD_SAID_GOOD] != 1 { return __syscall(93, 2, 0, 0, 0, 0, 0) } 358 if meta[NX_MV_OFF_N_LAYERS_S] != 3 { return __syscall(93, 3, 0, 0, 0, 0, 0) } 359 if meta[NX_MV_OFF_N_LAYERS_F] != 0 { return __syscall(93, 4, 0, 0, 0, 0, 0) } 360 if nx_god_said_good(meta) != 1 { return __syscall(93, 5, 0, 0, 0, 0, 0) } 361 362 // T2: One layer F -> META F (Rule 1 hard floor). 363 var j: nx_int = 0 364 while j < 8 { axes[j] = q / 10; j = j + 1 } // LOSS everywhere 365 nx_layer_verdict_write(l2, NX_LAYER_KIND_RIVER, 8, NX_LAYER_REFINE_NONE, axes) 366 nx_meta_verdict_compose(layers, 3, meta) 367 if meta[NX_MV_OFF_GRADE] != NX_LV_GRADE_F { return __syscall(93, 10, 0, 0, 0, 0, 0) } 368 if meta[NX_MV_OFF_GOD_SAID_GOOD] != 0 { return __syscall(93, 11, 0, 0, 0, 0, 0) } 369 if meta[NX_MV_OFF_N_LAYERS_F] != 1 { return __syscall(93, 12, 0, 0, 0, 0, 0) } 370 371 // T3: Sabbath rule: all A, no losses -> META = min = A. 372 var k: nx_int = 0 373 while k < 7 { axes[k] = q; k = k + 1 } 374 axes[7] = q * 5 / 10 // 1 MARGINAL -> grade A 375 nx_layer_verdict_write(l0, NX_LAYER_KIND_HEIGHTMAP, 8, NX_LAYER_REFINE_NONE, axes) 376 nx_layer_verdict_write(l1, NX_LAYER_KIND_FOREST, 8, NX_LAYER_REFINE_NONE, axes) 377 nx_layer_verdict_write(l2, NX_LAYER_KIND_RIVER, 8, NX_LAYER_REFINE_NONE, axes) 378 nx_meta_verdict_compose(layers, 3, meta) 379 if meta[NX_MV_OFF_GRADE] != NX_LV_GRADE_A { return __syscall(93, 20, 0, 0, 0, 0, 0) } 380 if nx_god_said_good(meta) != 1 { return __syscall(93, 21, 0, 0, 0, 0, 0) } 381 382 // T4: Mixed grades with no F -- derived from grade-mean. 383 // Layer 0: S (8 wins). Layer 1: A (7 wins + 1 marginal). 384 // Layer 2: C (4 wins + 4 marginal -> wins>=half_n=4, losses<=1 -> C). 385 var m1: nx_int = 0 386 while m1 < 8 { axes[m1] = q; m1 = m1 + 1 } 387 nx_layer_verdict_write(l0, NX_LAYER_KIND_HEIGHTMAP, 8, NX_LAYER_REFINE_NONE, axes) 388 axes[7] = q / 2 389 nx_layer_verdict_write(l1, NX_LAYER_KIND_FOREST, 8, NX_LAYER_REFINE_NONE, axes) 390 var m2: nx_int = 0 391 while m2 < 4 { axes[m2] = q; m2 = m2 + 1 } 392 while m2 < 8 { axes[m2] = q / 2; m2 = m2 + 1 } 393 nx_layer_verdict_write(l2, NX_LAYER_KIND_RIVER, 8, NX_LAYER_REFINE_NONE, axes) 394 nx_meta_verdict_compose(layers, 3, meta) 395 // Mean grade = (S=5 + A=4 + C=2) / 3 = 11/3 ~ 3.67 -> floor 3 = B. 396 // No loss axes so no penalty. 397 if meta[NX_MV_OFF_GRADE] != NX_LV_GRADE_B { return __syscall(93, 30, 0, 0, 0, 0, 0) } 398 // god_said_good = 1 since grade >= B. 399 if nx_god_said_good(meta) != 1 { return __syscall(93, 31, 0, 0, 0, 0, 0) } 400 // Worst layer kind = RIVER (the C one). 401 if meta[NX_MV_OFF_WORST_LAYER_KIND] != NX_LAYER_KIND_RIVER { return __syscall(93, 32, 0, 0, 0, 0, 0) } 402 if meta[NX_MV_OFF_WORST_LAYER_GRADE] != NX_LV_GRADE_C { return __syscall(93, 33, 0, 0, 0, 0, 0) } 403 404 // T5: Min-axis floor. All layers B (so grade-mean = 3), but ONE 405 // axis is at 0.1Q (below 0.2Q floor) -> capped at C. 406 var n1: nx_int = 0 407 while n1 < 7 { axes[n1] = q; n1 = n1 + 1 } 408 axes[7] = q / 10 // 0.1Q -- below floor 409 // 7 WIN + 1 LOSS = wins=7, losses=1. three_quarter_n=6. wins>=6, losses<=1 -> B. 410 nx_layer_verdict_write(l0, NX_LAYER_KIND_HEIGHTMAP, 8, NX_LAYER_REFINE_NONE, axes) 411 nx_layer_verdict_write(l1, NX_LAYER_KIND_FOREST, 8, NX_LAYER_REFINE_NONE, axes) 412 nx_layer_verdict_write(l2, NX_LAYER_KIND_RIVER, 8, NX_LAYER_REFINE_NONE, axes) 413 nx_meta_verdict_compose(layers, 3, meta) 414 // All B; mean=3=B. But min_axis=0.1Q below floor -> cap at C. 415 if meta[NX_MV_OFF_GRADE] != NX_LV_GRADE_C { return __syscall(93, 40, 0, 0, 0, 0, 0) } 416 417 // T6: Empty input -> safe F default. 418 nx_meta_verdict_compose(layers, 0, meta) 419 if meta[NX_MV_OFF_GRADE] != NX_LV_GRADE_F { return __syscall(93, 50, 0, 0, 0, 0, 0) } 420 421 return 0 422}