code wiki / _hdl_build / nx_biotwin_morphometrics_gate.nx

nx_biotwin_morphometrics_gate.nx source

↩ module page · 189 lines · 9731 B

1// nx_biotwin_morphometrics_gate.nx -- the LIAR-KILL gate for the morphometric-derivation organ (board axis 8). 2// 3// Grades a MEASUREMENT engine in a medical domain, so it must be impossible to rubber-stamp. The gate does NOT 4// trust the organ's self-report: it RE-DERIVES the axes itself through the same shared library and checks the 5// values against the BANKED canon (a known-answer test on real SDF geometry). Teeth: 6// T1 ARTIFACT EXISTS -- the organ ran and wrote knowledge/nx_biotwin_morphometrics.json. 7// T2 CANON KNOWN-ANSWER -- gate re-derives preset 0; every axis must land in its banked canon band. This is 8// the correctness tooth AND the negative-control target: a wrong transform in the lib drives a value out 9// of band -> RED (proven by the mutation test). 10// T3 PROVENANCE PUBLISHED -- the emitted artifact carries a transform + inputs for EVERY axis (grep count). 11// T4 DETERMINISM -- two independent derivations hash identically (the sovereign bit-exact exceed). 12// T5 THE FENCE IS ALIVE -- bm_fence_blocked()>=1 live in THIS run (the promotion clamp fires), the board's 13// axis-21 planted lie + clamp still stand in source, and the artifact publishes morpho_fence_blocked. 14// T6 CROSS-BODY GENERALISE-- the same engine on 3 body presets: statures differ (it reads real geometry) yet 15// heads-tall stays canon-valid on every body (it did not overfit to one human). 16// license_tier: ORIGINAL expect_exit: 0 17import "nx_syscalls.nx" 18import "nx_biotwin_morpho.nx" 19 20// broad documented adult standing-human envelope for heads-tall (x10): 6.5 heads (short/stocky) .. 8.5 heads 21// (heroic/tall). T6 asserts the engine measures every body inside this envelope -- it catches an impossible 22// value (the 46.8-heads chin-misfire class) without demanding every atlas preset be classical-canon-perfect. 23const PLAUS_LO: i64 = 65 24const PLAUS_HI: i64 = 85 25 26func gw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 27func gn(v: i64) -> i64 { 28 if v==0 { sys_write(1,"0" as *u8,1); return 0 } 29 var m: i64=v; if m<0 { sys_write(1,"-" as *u8,1); m=0-m } 30 let t: *u8=sys_mmap(32); var k: i64=0 31 while m>0 { t[k]=(48+(m%10)) as u8; m=m/10; k=k+1 } 32 let o: *u8=sys_mmap(32); var q: i64=k-1; var i: i64=0 33 while q>=0 { o[i]=t[q]; i=i+1; q=q-1 } 34 sys_write(1,o,i); return 0 35} 36func match_at(buf: *u8, n: i64, i: i64, pat: *u8) -> i64 { 37 var j: i64 = 0 38 while pat[j] != (0 as u8) { if i+j >= n { return 0 } if buf[i+j] != pat[j] { return 0 } j=j+1 } 39 return 1 40} 41func contains(buf: *u8, n: i64, pat: *u8) -> i64 { 42 var i: i64 = 0 43 while i < n { if match_at(buf,n,i,pat)==1 { return 1 } i=i+1 } 44 return 0 45} 46func count_occ(buf: *u8, n: i64, pat: *u8) -> i64 { 47 var c: i64 = 0 48 var i: i64 = 0 49 while i < n { if match_at(buf,n,i,pat)==1 { c=c+1 } i=i+1 } 50 return c 51} 52 53func main() -> i64 { 54 gw("=== nx_biotwin_morphometrics_gate: is the derivation real, or a rubber stamp on a body measurement? ===\n\n") 55 var pass: i64 = 0 56 var checks: i64 = 0 57 58 // ---------- read the emitted artifact once (T1/T3/T5) ---------- 59 let alen: *i64 = sys_mmap(8) as *i64 60 alen[0] = 0 61 let asrc: *u8 = sys_read_file("knowledge/nx_biotwin_morphometrics.json" as *u8, alen) 62 let an: i64 = alen[0] 63 64 // ---------- T1: the organ ran and wrote its evidence artifact ---------- 65 checks = checks + 1 66 if (asrc as i64) != 0 { if an > 600 { 67 gw("T1 GREEN artifact present: knowledge/nx_biotwin_morphometrics.json ("); gn(an); gw(" bytes)\n") 68 pass = pass + 1 69 } } 70 if (asrc as i64) == 0 { gw("T1 RED artifact missing -- the organ did not run\n") } 71 if (asrc as i64) != 0 { if an <= 600 { gw("T1 RED artifact suspiciously small\n") } } 72 73 // ---------- T2: re-derive preset 0 and check every axis against its banked canon band ---------- 74 checks = checks + 1 75 let OUT: *i64 = sys_mmap(N_AX*AX_STRIDE*8) as *i64 76 bm_derive(0, OUT) 77 var inband: i64 = 0 78 var k: i64 = 0 79 while k < N_AX { 80 let base: i64 = k*AX_STRIDE 81 let val: i64 = OUT[base+AX_VAL] 82 let lo: i64 = OUT[base+AX_LO] 83 let hi: i64 = OUT[base+AX_HI] 84 gw(" "); gw(OUT[base+AX_NM] as *u8); gw("="); gn(val); gw(" (canon "); gn(lo); gw(".."); gn(hi); gw(")") 85 if val >= lo { if val <= hi { inband = inband + 1; gw(" IN") } } 86 if val < lo { gw(" OUT-LOW") } 87 if val > hi { gw(" OUT-HIGH") } 88 gw("\n") 89 k = k + 1 90 } 91 if inband == N_AX { 92 gw("T2 GREEN known-answer: all "); gn(N_AX); gw(" re-derived axes land in the banked canon band\n") 93 pass = pass + 1 94 } else { 95 gw("T2 RED "); gn(N_AX-inband); gw("/"); gn(N_AX); gw(" derived axes OUT of canon band -- a wrong transform or a broken measurement\n") 96 } 97 98 // ---------- T3: the emitted artifact carries provenance (transform + inputs) for every axis ---------- 99 checks = checks + 1 100 var nt: i64 = 0 101 var ni: i64 = 0 102 if (asrc as i64) != 0 { nt = count_occ(asrc, an, "\x22transform\x22:\x22" as *u8) } 103 if (asrc as i64) != 0 { ni = count_occ(asrc, an, "\x22inputs\x22:\x22" as *u8) } 104 gw(" provenance records: transform="); gn(nt); gw(" inputs="); gn(ni); gw(" (need >= "); gn(N_AX); gw(" each)\n") 105 if nt >= N_AX { if ni >= N_AX { 106 gw("T3 GREEN every derived axis publishes its transform and its input landmarks\n") 107 pass = pass + 1 108 } } 109 if nt < N_AX { gw("T3 RED an axis is missing its transform provenance\n") } 110 if nt >= N_AX { if ni < N_AX { gw("T3 RED an axis is missing its input-landmark provenance\n") } } 111 112 // ---------- T4: determinism -- two independent derivations hash identically ---------- 113 checks = checks + 1 114 let h1: i64 = bm_checksum(0) 115 let h2: i64 = bm_checksum(0) 116 if h1 == h2 { 117 gw("T4 GREEN deterministic: independent re-derivations hash identically (checksum "); gn(h1); gw(")\n") 118 pass = pass + 1 119 } else { 120 gw("T4 RED non-deterministic derivation (integer engine must be bit-exact)\n") 121 } 122 123 // ---------- T5: the fence is alive (live clamp + board axis-21 lie + published proof) ---------- 124 checks = checks + 1 125 let fb: i64 = bm_fence_blocked() 126 let blen: *i64 = sys_mmap(8) as *i64 127 blen[0] = 0 128 let bsrc: *u8 = sys_read_file("runtime/_hdl_build/nx_biotwin_board.nx" as *u8, blen) 129 var b1: i64 = 0 130 var b2: i64 = 0 131 var b3: i64 = 0 132 if (bsrc as i64) != 0 { b1 = contains(bsrc, blen[0], "axcl[21]=2" as *u8) } 133 if (bsrc as i64) != 0 { b2 = contains(bsrc, blen[0], "axfn[21]=2" as *u8) } 134 if (bsrc as i64) != 0 { b3 = contains(bsrc, blen[0], "fence_blocked = fence_blocked + 1" as *u8) } 135 var a1: i64 = 0 136 if (asrc as i64) != 0 { a1 = contains(asrc, an, "morpho_fence_blocked" as *u8) } 137 gw(" live morpho_fence_blocked="); gn(fb); gw(" board axis-21 lie="); gn(b1); gw(" hard-fence="); gn(b2); gw(" clamp="); gn(b3); gw(" published="); gn(a1); gw("\n") 138 if fb >= 1 { if b1==1 { if b2==1 { if b3==1 { if a1==1 { 139 gw("T5 GREEN fence alive: promotion clamp fires (>=1), board axis-21 fence intact, proof published\n") 140 pass = pass + 1 141 } } } } } 142 if fb < 1 { gw("T5 RED the promotion clamp did not fire -- a measurement could be promoted to a clinical/genomic claim\n") } 143 if b1==0 { gw("T5 RED the board axis-21 PLANTED LIE was removed -- the board fence is no longer proven\n") } 144 if b3==0 { gw("T5 RED the board fence CLAMP was removed\n") } 145 if a1==0 { gw("T5 RED the artifact no longer publishes morpho_fence_blocked\n") } 146 147 // ---------- T6: cross-body generalisation ---------- 148 checks = checks + 1 149 let LM0: *i64 = sys_mmap(N_LM*8) as *i64 150 bm_measure(0, LM0) 151 let LM2: *i64 = sys_mmap(N_LM*8) as *i64 152 bm_measure(2, LM2) 153 let LM3: *i64 = sys_mmap(N_LM*8) as *i64 154 bm_measure(3, LM3) 155 let s0: i64 = LM0[LM_CROWN] - LM0[LM_SOLE] 156 let s2: i64 = LM2[LM_CROWN] - LM2[LM_SOLE] 157 let s3: i64 = LM3[LM_CROWN] - LM3[LM_SOLE] 158 var hd0: i64 = 0 159 if LM0[LM_CROWN]-LM0[LM_CHIN] != 0 { hd0 = s0*10/(LM0[LM_CROWN]-LM0[LM_CHIN]) } 160 var hd2: i64 = 0 161 if LM2[LM_CROWN]-LM2[LM_CHIN] != 0 { hd2 = s2*10/(LM2[LM_CROWN]-LM2[LM_CHIN]) } 162 var hd3: i64 = 0 163 if LM3[LM_CROWN]-LM3[LM_CHIN] != 0 { hd3 = s3*10/(LM3[LM_CROWN]-LM3[LM_CHIN]) } 164 var smax: i64 = s0 165 if s2 > smax { smax = s2 } 166 if s3 > smax { smax = s3 } 167 var smin: i64 = s0 168 if s2 < smin { smin = s2 } 169 if s3 < smin { smin = s3 } 170 gw(" presets 0/2/3 stature="); gn(s0); gw("/"); gn(s2); gw("/"); gn(s3); gw(" heads_tall_x10="); gn(hd0); gw("/"); gn(hd2); gw("/"); gn(hd3); gw(" stature-spread="); gn(smax-smin); gw("\n") 171 var sane: i64 = 0 172 if s0 > 1000 { if s2 > 1000 { if s3 > 1000 { sane = 1 } } } 173 var plaus: i64 = 0 174 if hd0 >= PLAUS_LO { if hd0 <= PLAUS_HI { if hd2 >= PLAUS_LO { if hd2 <= PLAUS_HI { if hd3 >= PLAUS_LO { if hd3 <= PLAUS_HI { plaus = 1 } } } } } } 175 var vary: i64 = 0 176 if smax - smin > 40 { vary = 1 } 177 if sane==1 { if plaus==1 { if vary==1 { 178 gw("T6 GREEN generalises: statures vary across body types yet heads-tall stays in the plausible-adult envelope on all 3\n") 179 pass = pass + 1 180 } } } 181 if sane==0 { gw("T6 RED a body preset failed to measure a sane stature\n") } 182 if plaus==0 { gw("T6 RED a body preset gave an implausible heads-tall (chin/crown misfire class)\n") } 183 if vary==0 { gw("T6 RED statures did not vary -- the engine may be ignoring real geometry\n") } 184 185 gw("\n=== nx_biotwin_morphometrics_gate "); gn(pass); gw("/"); gn(checks) 186 if pass == checks { gw(" GREEN ===\n"); return 0 } 187 gw(" RED ===\n") 188 return 1 189}