code wiki / (root) / nx_fieldfit_verified_r3_gate_t333.nx

nx_fieldfit_verified_r3_gate_t333.nx source

↩ module page · 264 lines · 16173 B

1// nx_fieldfit_gate.nx -- THE FIELD-FIT MACHINERY (nx_fieldfit_lib) ON A SYNTHETIC ORACLE WITH A KNOWN ANSWER 2// (anatomy AN17, 2026-09-19). The oracle is a point sphere of radius FG_R centred (FG_CX, FG_CY, FG_CZ), sampled on a 3// 10-degree lattice; the field is a sphere whose centre and radius are stations 0..3 of a five-station vector, with 4// station 4 a DECOY the field never reads and the live mask retires. Started from a wrong vector, the descent must 5// recover the centre and the radius to FG_TOL mm, must leave the decoy untouched, and must not move from the true 6// vector (a move accepted at the optimum is a broken accept rule). The fit file's prefix contract and the spatial 7// hash are held to exact answers. NEG-CONTROL: the two-sided objective must charge a shrunken sphere MORE than the 8// true one (a one-sided objective is gamed by deleting geometry; this is the tooth that cannot pass on one side). 9// license_tier: ORIGINAL. No hw writes (Rule 26). 10import "nx_syscalls.nx" 11import "nx_gate_verdict.nx" 12import "nx_sdfprim_lib.nx" 13import "nx_fieldfit_verified_r3_t333.nx" 14 15const FG_NP: i64 = 5 16const FG_NREG: i64 = 8 // the octants 17const FG_CAP: i64 = 4096 18const FG_EXT: i64 = 60 // the sphere (centre 8, radius 40) fits in +/- 60 19const FG_DIM: i64 = 12 20const FG_LAT: i64 = 6 21const FG_R: i64 = 40 22const FG_CX: i64 = 5 23const FG_CY: i64 = 0-3 24const FG_CZ: i64 = 8 25const FG_DECOY: i64 = 777 26const FG_WRONG_DX: i64 = 12 // the wrong start: centre x shifted, radius shrunk 27const FG_WRONG_R: i64 = 25 28const FG_SHRUNK_R: i64 = 20 // the neg-control's deleted geometry 29const FG_PASSES: i64 = 6 30const FG_STEP0: i64 = 8 31const FG_TOL: i64 = 2 // mm: the reverse lattice at 6 mm bounds the placement 32const FG_STEP_DEG: i64 = 10 33const FG_FILE: *u8 = "/tmp/nx_fieldfit_gate.fit" 34const FG_FILE3: *u8 = "/tmp/nx_fieldfit_gate.fit3" 35const FG_FILE2: *u8 = "/tmp/nx_fieldfit_gate.fit2" 36const FG_ABSENT: *u8 = "/tmp/nx_fieldfit_gate.absent" 37const FG_MODE644: i64 = 420 38 39func fg_field(px: i64, py: i64, pz: i64, a: i64, b: i64, P: *i64) -> i64 { 40 let dx: i64 = px - P[0] 41 let dy: i64 = py - P[1] 42 let dz: i64 = pz - P[2] 43 return (sp_isqrt(dx*dx + dy*dy + dz*dz) - P[3])*SP_FQ 44} 45func fg_region(x: i64, y: i64, z: i64) -> i64 { 46 var r: i64 = 0 47 if x >= 0 { r = r + 1 } 48 if y >= 0 { r = r + 2 } 49 if z >= 0 { r = r + 4 } 50 return r 51} 52func fg_live(k: i64) -> i64 { if k == 4 { return 0 } return 1 } 53const FG_EXTRA: i64 = 900 54func fg_extra(P: *i64) -> i64 { return FG_EXTRA } 55func fg_abs(v: i64) -> i64 { if v < 0 { return 0 - v } return v } 56// the oracle: points on the sphere at every 10 degrees of polar and azimuthal angle 57func fg_oracle(c: *FfCtx) -> i64 { 58 var n: i64 = 0 59 var th: i64 = 0 60 while th <= 180 { 61 var ph: i64 = 0 62 while ph < 360 { 63 let s: i64 = sp_isin(th) 64 let cth: i64 = sp_icos(th) 65 var cph: i64 = sp_icos(ph) 66 var sph: i64 = sp_isin(ph) 67 if ph > 180 { cph = 0 - sp_icos(ph - 180); sph = 0 - sp_isin(ph - 180) } 68 c.RX[n] = FG_CX + FG_R*s/SP_PERMILLE*cph/SP_PERMILLE 69 c.RY[n] = FG_CY + FG_R*s/SP_PERMILLE*sph/SP_PERMILLE 70 c.RZ[n] = FG_CZ + FG_R*cth/SP_PERMILLE 71 n = n + 1 72 ph = ph + FG_STEP_DEG 73 } 74 th = th + FG_STEP_DEG 75 } 76 c.n = n 77 return n 78} 79func fg_write(path: *u8, s: *u8) -> i64 { 80 var n: i64 = 0 81 while s[n] != (0 as u8) { n = n + 1 } 82 let fd: i64 = sys_openat_wr(path, FG_MODE644) 83 if fd < 0 { return 0 - 1 } 84 sys_write(fd, s, n) 85 sys_close(fd) 86 return n 87} 88 89 90func fg_valid(P: *i64, np: i64) -> i64 { if np != FG_NP { return 0 } if P[3] <= 0 { return 0 } return 1 } 91func fg_contract() -> *FfFitContract { 92 let C: *FfFitContract = sys_mmap(32) as *FfFitContract 93 C.header = "NXFFIT2 schema=sphere5 frame=xyz-mm producer=fixture reference=sphere-known objective=two-sided-v1\n" as *u8 94 C.header_bytes = 99 95 C.np = FG_NP; C.valid = fg_valid 96 return C 97} 98 99 100static FG_BAD_TRIAL: i64 101func fg_decoy_live(k: i64) -> i64 { if k == 4 { return 1 } return 0 } 102func fg_bound_valid(P: *i64, np: i64) -> i64 { if P[4] < 0 { FG_BAD_TRIAL = FG_BAD_TRIAL + 1; return 0 } return fg_valid(P, np) } 103 104func main(argc: i64, argv: *i64) -> i64 { 105 gv_head("nx_fieldfit_gate: the field-fit machinery recovers a known sphere from a wrong start, honours the live mask, and keeps the fit-file prefix contract" as *u8) 106 let ctr: *i64 = gv_ctr() 107 let C: *FfCtx = ff_ctx_new(FG_NP, FG_NREG, FG_CAP, FG_EXT, FG_DIM, FG_LAT, SP_FQ) 108 C.field = fg_field 109 C.region = fg_region 110 C.live = fg_live 111 let n: i64 = fg_oracle(C) 112 gv_check("oracle-sampled-a-nonempty-sphere" as *u8, (n > 100) as i64, ctr) 113 ff_bin(C) 114 ff_regions(C) 115 // every octant is populated: the balance has eight regions to balance 116 var reg_ok: i64 = 1 117 var r: i64 = 0 118 while r < FG_NREG { 119 var seen: i64 = 0 120 var i: i64 = 0 121 while i < n { if C.RG[i] == r { seen = 1 } i = i + 1 } 122 if seen == 0 { reg_ok = 0 } 123 r = r + 1 124 } 125 gv_check("every-region-holds-oracle-samples" as *u8, reg_ok, ctr) 126 // ---- the spatial hash ---- 127 gv_check_eq("nearest-oracle-sample-to-a-sample-is-itself" as *u8, ff_near(C, C.RX[0], C.RY[0], C.RZ[0]), 0, ctr) 128 gv_check_eq("nearest-sample-7mm-radially-outside-the-pole-reads-7" as *u8, ff_near(C, FG_CX, FG_CY, FG_CZ + FG_R + 7), 7, ctr) 129 gv_check_eq("live-count-excludes-the-decoy" as *u8, ff_live_count(C), FG_NP - 1, ctr) 130 // ---- the objective ---- 131 let T: *i64 = sys_mmap(FG_NP*FF_I64) as *i64 132 T[0] = FG_CX; T[1] = FG_CY; T[2] = FG_CZ; T[3] = FG_R; T[4] = FG_DECOY 133 let s_true: i64 = ff_score2(C, T) 134 let W: *i64 = sys_mmap(FG_NP*FF_I64) as *i64 135 W[0] = FG_CX; W[1] = FG_CY; W[2] = FG_CZ; W[3] = FG_SHRUNK_R; W[4] = FG_DECOY 136 let s_shrunk: i64 = ff_score2(C, W) 137 gv_check("true-vector-scores-under-one-lattice-step" as *u8, (s_true < FG_LAT*FF_CENTI) as i64, ctr) 138 gv_bite("neg-control-a-shrunken-sphere-is-charged-more-than-the-true-one" as *u8, (s_shrunk > s_true) as i64, (s_true > s_true) as i64, ctr) 139 // the forward side alone reads the shrink as its whole depth: the two-sided mean must exceed the forward side of the truth 140 // the fixture's own radius, MEASURED: integer point generation lands the samples a few mm inside the nominal sphere, 141 // so the shrink is read against the mean radius the points actually have, never against the number typed above 142 var rsum: i64 = 0 143 var ri: i64 = 0 144 while ri < n { 145 let ddx: i64 = C.RX[ri] - FG_CX 146 let ddy: i64 = C.RY[ri] - FG_CY 147 let ddz: i64 = C.RZ[ri] - FG_CZ 148 rsum = rsum + sp_isqrt(ddx*ddx + ddy*ddy + ddz*ddz) 149 ri = ri + 1 150 } 151 let mean_r: i64 = rsum / n 152 let fwd_shrunk: i64 = ff_score_fwd(C, W) 153 gv_check_near("shrunken-forward-term-reads-the-measured-shrink-in-centi-mm" as *u8, fwd_shrunk, (mean_r - FG_SHRUNK_R)*FF_CENTI, FF_CENTI, ctr) 154 // ---- the descent from a wrong start ---- 155 let P: *i64 = sys_mmap(FG_NP*FF_I64) as *i64 156 P[0] = FG_CX + FG_WRONG_DX; P[1] = FG_CY; P[2] = FG_CZ; P[3] = FG_WRONG_R; P[4] = FG_DECOY 157 let s_start: i64 = ff_score2(C, P) 158 let best: i64 = ff_descend(C, P, FG_PASSES, FG_STEP0, 0) 159 gv_check("descent-lowered-the-measured-objective" as *u8, (best < s_start) as i64, ctr) 160 gv_check_near("descent-recovered-the-centre-x" as *u8, P[0], FG_CX, FG_TOL, ctr) 161 gv_check_near("descent-recovered-the-centre-y" as *u8, P[1], FG_CY, FG_TOL, ctr) 162 gv_check_near("descent-recovered-the-centre-z" as *u8, P[2], FG_CZ, FG_TOL, ctr) 163 gv_check_near("descent-recovered-the-radius" as *u8, P[3], FG_R, FG_TOL, ctr) 164 gv_check_eq("descent-left-the-retired-decoy-untouched" as *u8, P[4], FG_DECOY, ctr) 165 gv_check_eq("best-is-the-objective-of-the-vector-returned" as *u8, ff_score2(C, P) - best, 0, ctr) 166 // ---- neg-control: from the truth, nothing moves ---- 167 let Q: *i64 = sys_mmap(FG_NP*FF_I64) as *i64 168 Q[0] = FG_CX; Q[1] = FG_CY; Q[2] = FG_CZ; Q[3] = FG_R; Q[4] = FG_DECOY 169 let b2: i64 = ff_descend(C, Q, 2, FG_STEP0, 0) 170 gv_check("neg-control-descent-from-the-truth-stays-within-a-millimetre" as *u8, ((fg_abs(Q[0]-FG_CX) <= 1) * (fg_abs(Q[3]-FG_R) <= 1)) as i64, ctr) 171 gv_check("neg-control-descent-from-the-truth-does-not-raise-the-objective" as *u8, (b2 <= s_true) as i64, ctr) 172 // ---- the optional third term: a constant extra of 900 centi folds in as the three-way mean ---- 173 C.extra = fg_extra 174 C.has_extra = 1 175 let s3: i64 = ff_score2(C, T) 176 let f3: i64 = ff_score_fwd(C, T) 177 let r3: i64 = ff_score_rev(C, T) 178 gv_check_eq("extra-term-folds-in-as-the-three-way-mean" as *u8, s3, (f3 + r3 + FG_EXTRA)/3, ctr) 179 C.has_extra = 0 180 gv_check_eq("extra-term-off-restores-the-two-way-mean" as *u8, ff_score2(C, T), (f3 + r3)/2, ctr) 181 // ---- the fit file ---- 182 gv_check_eq("absent-fit-file-applies-nothing" as *u8, ff_load_prefix(FG_ABSENT, Q, FG_NP, 3), 0, ctr) 183 let wrote: i64 = ff_save(FG_FILE, P, FG_NP) 184 gv_check("save-wrote-bytes" as *u8, (wrote > 0) as i64, ctr) 185 let R: *i64 = sys_mmap(FG_NP*FF_I64) as *i64 186 R[0] = 1; R[1] = 2; R[2] = 3; R[3] = 4; R[4] = 5 187 gv_check_eq("save-then-load-applies-the-whole-vector" as *u8, ff_load_prefix(FG_FILE, R, FG_NP, 3), FG_NP, ctr) 188 gv_check_eq("save-then-load-round-trips-every-station" as *u8, (R[0]-P[0]) + (R[1]-P[1]) + (R[2]-P[2]) + (R[3]-P[3]) + (R[4]-P[4]), 0, ctr) 189 fg_write(FG_FILE3, "11\n-22\n33\n" as *u8) 190 R[0] = 0; R[1] = 0; R[2] = 0; R[3] = 4; R[4] = 5 191 gv_check_eq("a-three-int-file-applies-a-prefix-of-three" as *u8, ff_load_prefix(FG_FILE3, R, FG_NP, 3), 3, ctr) 192 gv_check_eq("the-prefix-lands-with-its-sign" as *u8, R[1], 0-22, ctr) 193 gv_check_eq("stations-past-the-prefix-keep-their-defaults" as *u8, R[3] + R[4], 9, ctr) 194 fg_write(FG_FILE2, "11\n22\n" as *u8) 195 R[0] = 0; R[1] = 0 196 gv_check_eq("a-file-shorter-than-minlen-applies-nothing" as *u8, ff_load_prefix(FG_FILE2, R, FG_NP, 3), 0, ctr) 197 gv_check_eq("a-refused-short-file-leaves-the-vector-unchanged" as *u8, R[0] + R[1], 0, ctr) 198 199 var VC: *FfFitContract = fg_contract() 200 gv_check_eq("verified-reader-classifies-legacy-without-applying" as *u8, ff_load_verified(FG_FILE, R, VC), FF_FIT_LEGACY_UNVERIFIED, ctr) 201 gv_check("verified-save-positive-domain" as *u8, (ff_save_verified(FG_FILE, P, VC) > 0) as i64, ctr) 202 gv_check_eq("verified-roundtrip-exact-vector" as *u8, ff_load_verified(FG_FILE, R, VC), FG_NP, ctr) 203 gv_check_eq("verified-roundtrip-radius" as *u8, R[3], P[3], ctr) 204 let bad: i64 = R[3] 205 R[3] = 0 - 9 206 gv_check_eq("negative-radius-save-refused" as *u8, ff_save_verified(FG_FILE2, R, VC), FF_FIT_INVALID, ctr) 207 gv_check_eq("negative-initial-descent-refused" as *u8, ff_descend_checked(C, R, 1, 8, 0, fg_valid), FF_FIT_INVALID, ctr) 208 gv_check_eq("invalid-descent-preserves-input" as *u8, R[3], 0 - 9, ctr) 209 R[3] = bad 210 let vbest: i64 = ff_descend_checked(C, R, 1, 8, 0, fg_valid) 211 gv_check("checked-descent-keeps-domain" as *u8, fg_valid(R, FG_NP), ctr) 212 gv_check_eq("checked-descent-returns-real-objective" as *u8, vbest, ff_score2(C, R), ctr) 213 VC.header = "NXFFIT2 schema=sphere5 frame=zyx-mm producer=fixture reference=sphere-known objective=two-sided-v1\n" as *u8 214 gv_check_eq("wrong-coordinate-frame-refused" as *u8, ff_load_verified(FG_FILE, R, VC), FF_FIT_CONTRACT_MISMATCH, ctr) 215 VC = fg_contract() 216 fg_write(FG_FILE2, "NXFFIT2 schema=sphere5 frame=xyz-mm producer=fixture reference=sphere-known objective=two-sided-v1\n1\n2\n3\n" as *u8) 217 gv_check_eq("verified-truncated-refused" as *u8, ff_load_verified(FG_FILE2, R, VC), FF_FIT_MALFORMED, ctr) 218 fg_write(FG_FILE2, "NXFFIT2 schema=sphere5 frame=xyz-mm producer=fixture reference=sphere-known objective=two-sided-v1\n1\n2\n3\n4\n5\n6\n" as *u8) 219 gv_check_eq("verified-extra-refused" as *u8, ff_load_verified(FG_FILE2, R, VC), FF_FIT_MALFORMED, ctr) 220 fg_write(FG_FILE2, "NXFFIT2 schema=sphere5 frame=xyz-mm producer=fixture reference=sphere-known objective=two-sided-v1\n1\n2\n3\n4x\n5\n" as *u8) 221 gv_check_eq("verified-garbage-refused" as *u8, ff_load_verified(FG_FILE2, R, VC), FF_FIT_MALFORMED, ctr) 222 fg_write(FG_FILE2, "NXFFIT2 schema=sphere5 frame=xyz-mm producer=fixture reference=sphere-known objective=two-sided-v1\n1\n2\n3\n9223372036854775808\n5\n" as *u8) 223 gv_check_eq("verified-overflow-refused" as *u8, ff_load_verified(FG_FILE2, R, VC), FF_FIT_MALFORMED, ctr) 224 fg_write(FG_FILE2, "NXFFIT2 schema=sphere5 frame=xyz-mm producer=fixture reference=sphere-known objective=two-sided-v1\n1\n2\n3\n-9\n5\n" as *u8) 225 gv_check_eq("verified-negative-radius-refused" as *u8, ff_load_verified(FG_FILE2, R, VC), FF_FIT_INVALID, ctr) 226 227 gv_check_eq("refused-files-preserve-vector-x" as *u8, R[0], P[0], ctr) 228 gv_check_eq("refused-files-preserve-vector-y" as *u8, R[1], P[1], ctr) 229 gv_check_eq("refused-files-preserve-vector-z" as *u8, R[2], P[2], ctr) 230 gv_check_eq("refused-files-preserve-vector-radius" as *u8, R[3], P[3], ctr) 231 gv_check_eq("refused-files-preserve-vector-decoy" as *u8, R[4], P[4], ctr) 232 R[4] = FF_FIT_I64_POS_LIMIT 233 C.live = fg_decoy_live; FG_BAD_TRIAL = 0 234 let overflow_best: i64 = ff_descend_checked(C, R, 1, 8, 0, fg_bound_valid) 235 gv_check_eq("overflow-trial-never-reaches-validator" as *u8, FG_BAD_TRIAL, 0, ctr) 236 gv_check_eq("overflow-trial-keeps-vector" as *u8, R[4], FF_FIT_I64_POS_LIMIT, ctr) 237 gv_check_eq("overflow-trial-keeps-actual-objective" as *u8, overflow_best, ff_score2(C, R), ctr) 238 C.live = fg_live; R[4] = P[4] 239 gv_check_eq("negative-step-refused" as *u8, ff_descend_checked(C, R, 1, 0 - 8, 0, fg_valid), FF_FIT_INVALID, ctr) 240 fg_write(FG_FILE2, "NXFFIT2 schema=sphere5 frame=xyz-mm producer=fixture reference=sphere-known objective=two-sided-v1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n" as *u8) 241 gv_check_eq("oversized-file-refused-before-file-sized-allocation" as *u8, ff_load_verified(FG_FILE2, R, VC), FF_FIT_MALFORMED, ctr) 242 243 let keep_np: i64 = VC.np 244 VC.np = FF_FIT_I64_POS_LIMIT 245 gv_check_eq("overflowing-contract-count-refused" as *u8, ff_load_verified(FG_FILE, R, VC), FF_FIT_CONTRACT_MISMATCH, ctr) 246 VC.np = keep_np 247 let keep_header: i64 = VC.header_bytes 248 VC.header_bytes = FF_FIT_I64_POS_LIMIT 249 gv_check_eq("overflowing-header-plus-sentinel-refused" as *u8, ff_load_verified(FG_FILE, R, VC), FF_FIT_CONTRACT_MISMATCH, ctr) 250 VC.header_bytes = keep_header 251 gv_check_eq("atomic-fit-crash-before-rename-preserves-published-file" as *u8, ar_crashwrite(FG_FILE, "torn" as *u8, 4), 0, ctr) 252 gv_check_eq("atomic-fit-old-file-still-valid-after-staging-crash" as *u8, ff_load_verified(FG_FILE, R, VC), FG_NP, ctr) 253 gv_check_eq("atomic-fit-old-radius-still-intact" as *u8, R[3], P[3], ctr) 254 gv_values_head() 255 gv_kv("oracle_samples" as *u8, n) 256 gv_kv("score_true_centi" as *u8, s_true) 257 gv_kv("score_shrunk_centi" as *u8, s_shrunk) 258 gv_kv("score_shrunk_fwd_centi" as *u8, fwd_shrunk) 259 gv_kv("fixture_mean_radius_mm" as *u8, mean_r) 260 gv_kv("score_start_centi" as *u8, s_start) 261 gv_kv("score_best_centi" as *u8, best) 262 gv_kv("recovered_cx" as *u8, P[0]); gv_kv("recovered_cy" as *u8, P[1]); gv_kv("recovered_cz" as *u8, P[2]); gv_kv("recovered_r" as *u8, P[3]) 263 return gv_verdict("nx_fieldfit_gate" as *u8, ctr, "one optimiser and one two-sided region-balanced objective for every anatomy field, proven on a sphere with a known answer" as *u8) 264}