Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/tests/turing/test_turing.c # omega-mv Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance. Brought by drakestapleton Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / tests / turing / test_turing.c` 489 lines ยท 23497 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /* test-turing: TURING Field v1 records (control-arm ranking), retired v0 FAIL 18 * reproduction, and control arm on stored receipts. 19 * docs/turing/TURING_W0_PROPOSAL.md sections J and K. No timed runs. */ 20 #define _POSIX_C_SOURCE 200809L 21 #include "turing/select.h" 22 23 #include "omega_canonical.h" 24 #include "sha256.h" 25 26 #include 27 #include 28 #include 29 #include 30 #include 31 #include 32 33 static int pass, fail; 34 #define CHECK(c, ...) \ 35 do { \ 36 if (c) { \ 37 ++pass; \ 38 } else { \ 39 ++fail; \ 40 printf("FAIL %s:%d: ", __FILE__, __LINE__); \ 41 printf(__VA_ARGS__); \ 42 printf("\n"); \ 43 } \ 44 } while (0) 45 46 static const char *R1 = "evidence/MIXED_ALGEBRA/ma3_bench_run1.json"; 47 static const char *R2 = "evidence/MIXED_ALGEBRA/ma3_bench_run2.json"; 48 49 /* Golden values: a rebuild (any flags, any day) must reproduce them. */ 50 static const char *GOLD_CONTRACT = "`75772afe8a98f73064f2933d76bc58f7bfe5b9353eb40b703e173b4cd3f22b5f`"; 51 static const char *GOLD_CRUMB = "`d785f5ba7b7cae43f84f71e01fa279d7bff9fbdd0f9604e5b55f6b692b5e00f7`"; 52 static const char *GOLD_PLAIN = "`272d7bdfd34514286b303857c98beb3c10b0ffada5e1d38dfb9fa94eaef17f99`"; 53 54 static turing_store *load2(const char *a, const char *b) { 55 turing_store *st = turing_store_new(); 56 if (!st || turing_ingest_registry(st) < 0 || turing_ingest_receipt(st, a) <= 0 || 57 (b && turing_ingest_receipt(st, b) <= 0)) { 58 turing_store_free(st); 59 return NULL; 60 } 61 return st; 62 } 63 64 static int copy_file(const char *from, const char *to) { 65 FILE *i = fopen(from, "rb"), *o = fopen(to, "wb"); 66 int ok = i && o; 67 char buf[8192]; 68 size_t k; 69 while (ok && (k = fread(buf, 1, sizeof buf, i)) > 0) ok = fwrite(buf, 1, k, o) == k; 70 if (i) fclose(i); 71 if (o) ok = (fclose(o) == 0) && ok; 72 return ok ? 0 : -1; 73 } 74 75 static int flip_byte(const char *path, long off) { 76 FILE *f = fopen(path, "r+b"); 77 if (!f) return -1; 78 int rc = -1; 79 if (fseek(f, off, SEEK_SET) == 0) { 80 int c = fgetc(f); 81 if (c != EOF && fseek(f, off, SEEK_SET) == 0 && fputc(c == '1' ? '2' : '1', f) != EOF) rc = 0; 82 } 83 return fclose(f) == 0 ? rc : -1; 84 } 85 86 static void test_digests(void) { 87 turing_contract c1, c2; 88 turing_contract_omega_x(&c1); 89 turing_contract_omega_x(&c2); 90 turing_digest d1, d2; 91 char h[65]; 92 CHECK(turing_contract_digest(&c1, &d1) == 0 && turing_contract_digest(&c2, &d2) == 0, "contract digest"); 93 CHECK(turing_digest_eq(&d1, &d2), "contract digest reproducible"); 94 turing_hex(&d1, h); 95 CHECK(!strcmp(h, GOLD_CONTRACT), "contract digest golden: got %s", h); 96 c2.max_n -= 1; 97 CHECK(turing_contract_digest(&c2, &d2) == 0 && !turing_digest_eq(&d1, &d2), "contract field change -> new digest"); 98 99 turing_store *st = turing_store_new(); 100 CHECK(st && turing_ingest_registry(st) == 10, "registry adapter: 10 realizations"); 101 if (!st) return; 102 int crumb = turing_find_spec(st, "R2c_crumb"), plain = turing_find_spec(st, "R1_plain"); 103 CHECK(crumb >= 0 && plain >= 0, "specs present"); 104 turing_hex(&st->spec_id[crumb], h); 105 CHECK(!strcmp(h, GOLD_CRUMB), "R2c_crumb spec_id golden: got %s", h); 106 turing_hex(&st->spec_id[plain], h); 107 CHECK(!strcmp(h, GOLD_PLAIN), "R1_plain spec_id golden: got %s", h); 108 /* One contract across all 10. */ 109 size_t same = 0; 110 for (size_t k = 0; k < st->nspec; ++k) same += turing_digest_eq(&st->spec[k].contract_digest, &st->contract_digest); 111 CHECK(same == 10 && st->nspec == 10, "one contract_digest across all %zu specs (%zu match)", st->nspec, same); 112 /* Distinct spec ids. */ 113 int distinct = 1; 114 for (size_t a = 0; a < st->nspec; ++a) 115 for (size_t b = a + 1; b < st->nspec; ++b) distinct &= !turing_digest_eq(&st->spec_id[a], &st->spec_id[b]); 116 CHECK(distinct, "spec ids distinct"); 117 /* Domain separation: a Field digest is not SHA-256 of the OMG0 bytes (the 118 * Omega semantic id construction). */ 119 uint8_t buf[8192]; 120 size_t len = 0; 121 turing_digest raw; 122 CHECK(turing_spec_bytes(&st->spec[crumb], buf, sizeof buf, &len) == 0 && len > 6, "spec canonical bytes"); 123 CHECK(!memcmp(buf, "OMG0", 4), "spec bytes use the OMG0 layout"); 124 sha256_hash(buf, len, raw.b); 125 CHECK(!turing_digest_eq(&raw, &st->spec_id[crumb]), "spec_id is domain-separated from an Omega semantic id"); 126 /* Byte-level reproducibility. */ 127 uint8_t buf2[8192]; 128 size_t len2 = 0; 129 CHECK(turing_spec_bytes(&st->spec[crumb], buf2, sizeof buf2, &len2) == 0 && len == len2 && !memcmp(buf, buf2, len), 130 "spec canonical bytes reproducible"); 131 turing_store_free(st); 132 } 133 134 static void test_rebuild_stable(void) { 135 /* spec ids from the registry == spec ids reconstructed from receipt rows. */ 136 turing_store *reg = turing_store_new(), *rec = turing_store_new(); 137 CHECK(reg && rec, "alloc"); 138 if (!reg || !rec) return; 139 CHECK(turing_ingest_registry(reg) == 10, "registry"); 140 CHECK(turing_ingest_receipt(rec, R1) == 360, "receipt-only store: 360 rows"); 141 CHECK(rec->nspec == 10, "receipt-only store: 10 specs (%zu)", rec->nspec); 142 size_t match = 0; 143 for (size_t k = 0; k < reg->nspec; ++k) { 144 int j = turing_find_spec(rec, reg->spec[k].rz_id); 145 match += j >= 0 && turing_digest_eq(®->spec_id[k], &rec->spec_id[j]); 146 } 147 CHECK(match == 10, "registry and receipt adapters give identical spec ids (%zu/10)", match); 148 /* Build fields live on evidence, not on the spec. */ 149 const turing_evidence *e = &rec->ev[0]; 150 CHECK(strlen(e->build_digest) == 64 && strlen(e->git_commit) == 40 && e->toolchain[0], "evidence carries build"); 151 CHECK(!strcmp(e->tier, "E2") && strstr(e->tier_source, "PROPOSED"), "tier text + ADR revision"); 152 CHECK(strstr(e->cpu_freq_state, "not recorded") && strstr(e->quiet_flag, "not recorded"), 153 "cpu freq + quiet flag stated as not recorded"); 154 /* Evidence digests reproducible. */ 155 size_t ok = 0; 156 for (size_t i = 0; i < rec->nev; ++i) { 157 turing_digest d; 158 ok += turing_evidence_digest(&rec->ev[i], &d) == 0 && turing_digest_eq(&d, &rec->ev_id[i]); 159 } 160 CHECK(ok == rec->nev, "evidence digests reproducible (%zu/%zu)", ok, rec->nev); 161 turing_store_free(reg); 162 turing_store_free(rec); 163 } 164 165 static void test_refusals(void) { 166 char dir[] = "/tmp/turing_refuse_XXXXXX"; 167 CHECK(mkdtemp(dir) != NULL, "mkdtemp"); 168 char p[300]; 169 snprintf(p, sizeof p, "%s/bad.json", dir); 170 FILE *f = fopen(p, "w"); 171 if (f) { 172 fputs("{\n \"schema\": \"SOMETHING_ELSE\",\n \"run_id\": \"x\"\n}\n", f); 173 fclose(f); 174 } 175 turing_store *st = turing_store_new(); 176 CHECK(st && turing_ingest_registry(st) == 10, "registry"); 177 CHECK(turing_ingest_receipt(st, p) < 0 && st->nev == 0, "unknown schema refused"); 178 CHECK(turing_ingest_receipt(st, "/nonexistent/receipt.json") < 0, "missing receipt refused"); 179 /* A receipt whose realization row disagrees with the registry is refused. */ 180 snprintf(p, sizeof p, "%s/maxn.json", dir); 181 CHECK(copy_file(R1, p) == 0, "copy"); 182 FILE *g = fopen(p, "r+b"); 183 long off = -1; 184 if (g) { 185 char line[1024]; 186 long pos = 0; 187 while (fgets(line, sizeof line, g)) { 188 char *hit = strstr(line, "\"id\": \"R3_sparse\""); 189 char *mx = hit ? strstr(line, "\"max_n\": 65536") : NULL; 190 if (mx) { 191 off = pos + (mx - line) + 10; /* first digit of 65536 */ 192 break; 193 } 194 pos = ftell(g); 195 } 196 fclose(g); 197 } 198 CHECK(off > 0 && flip_byte(p, off) == 0, "edit R3_sparse max_n in copy"); 199 CHECK(turing_ingest_receipt(st, p) < 0, "registry/receipt spec disagreement refused"); 200 CHECK(st->nev == 0, "no half-read evidence kept (%zu)", st->nev); 201 turing_store_free(st); 202 } 203 204 static void test_decisions_verify(void) { 205 turing_store *st = load2(R1, R2); 206 CHECK(st != NULL, "load both receipts"); 207 if (!st) return; 208 CHECK(st->nev == 720 && st->nreceipt == 2, "720 evidence rows, 2 receipts (%zu)", st->nev); 209 turing_query cells[80]; 210 size_t nc = turing_cells(st, cells, 80); 211 CHECK(nc == 36, "36 measured cells (%zu)", nc); 212 size_t ok = 0, total = 0, exact = 0, cites_cell = 0; 213 turing_decision d; 214 for (int pack = 0; pack < 2; ++pack) 215 for (size_t i = 0; i < nc; ++i) { 216 turing_query q = cells[i]; 217 q.pack = pack; 218 char why[200]; 219 if (turing_field_select(st, &q, NULL, &d) != 0) continue; 220 ++total; 221 ok += turing_decision_verify(st, &d, why, sizeof why) == 0 && d.ncite == 80; 222 exact += d.exact_cell; 223 int all = 1; 224 for (size_t c = 0; c < d.ncite; ++c) { 225 int k = turing_find_evidence(st, &d.cite[c]); 226 all &= k >= 0 && st->ev[k].n == q.n && st->ev[k].m == q.m; 227 } 228 cites_cell += all; 229 } 230 CHECK(total == 72 && ok == 72, "72 Field v1 decisions, each cites 80 rows (10 specs x 4 sparsities x 2 runs) whose receipts verify (%zu/%zu)", ok, total); 231 CHECK(exact == 72 && cites_cell == 72, "every decision on its exact footprint cites only that footprint"); 232 233 /* Winner flip from stored evidence alone: L2-fit int8, DRAM-bound crumb. */ 234 turing_query s1 = {4096, 64, 300, TURING_PACK_ONCE, 0}, s4 = {16384, 4096, 300, TURING_PACK_ONCE, 0}; 235 turing_decision a, b; 236 CHECK(turing_field_select(st, &s1, NULL, &a) == 0 && !strncmp(a.cand_rz[a.chosen], "R1_", 3), 237 "S1 (L2-fit) -> binary int8 (%s)", a.cand_rz[a.chosen]); 238 CHECK(turing_field_select(st, &s4, NULL, &b) == 0 && !strcmp(b.cand_rz[b.chosen], "R2c_crumb"), 239 "S4 (DRAM-bound) -> R2c_crumb (%s)", b.cand_rz[b.chosen]); 240 241 /* Decision digests: reproducible; supersedes changes the digest. */ 242 turing_digest i1, i2, i3; 243 CHECK(turing_decision_digest(&b, &i1) == 0 && turing_decision_digest(&b, &i2) == 0 && turing_digest_eq(&i1, &i2), 244 "decision digest reproducible"); 245 turing_decision b2; 246 CHECK(turing_field_select(st, &s4, &i1, &b2) == 0 && b2.has_supersedes && turing_decision_digest(&b2, &i3) == 0 && 247 !turing_digest_eq(&i1, &i3), 248 "superseding decision has its own digest"); 249 250 /* Off-grid query: domain filter with reason codes, nearest cell. */ 251 turing_query big = {100000, 64, 300, TURING_PACK_ONCE, 0}; 252 turing_decision o; 253 CHECK(turing_field_select(st, &big, NULL, &o) == 0 && !o.exact_cell, "off-grid decision"); 254 int r3 = turing_find_spec(st, "R3_sparse"), r4 = turing_find_spec(st, "R4_rns"); 255 CHECK(o.reason[r3] == TURING_R_MAX_N && o.reason[r4] == TURING_R_MAX_N, "R3/R4 filtered MAX_N"); 256 CHECK(o.cell_n == 16384 && o.cell_m == 64, "nearest cell 16384 x 64"); 257 258 /* RETIRED v0 tie rule (kept only to reproduce the K.6 FAIL): incumbent inside a tied set stands. */ 259 turing_query s1pc = {4096, 64, 300, TURING_PACK_PER_CALL, 0}; 260 turing_decision t0, t1; 261 CHECK(turing_field_select_v0_retired(st, &s1pc, NULL, NULL, &t0) == 0 && !strcmp(t0.verdict, "TIE"), "RETIRED v0: S1 per call TIE"); 262 int other = -1; 263 for (size_t k = 0; k < t0.ncand; ++k) 264 if (t0.reason[k] == TURING_R_TIED) other = (int)k; 265 if (other >= 0) { 266 CHECK(turing_field_select_v0_retired(st, &s1pc, t0.cand_rz[other], NULL, &t1) == 0 && t1.chosen == other && 267 !strcmp(t1.tie_resolution, "incumbent"), 268 "RETIRED v0: incumbent inside tied set stands"); 269 } 270 271 /* In-memory tamper of a cited evidence record -> verify fails. */ 272 int k = turing_find_evidence(st, &b.cite[0]); 273 char why[200]; 274 if (k >= 0) { 275 uint64_t keep = st->ev[k].median_ps; 276 st->ev[k].median_ps += 1; 277 CHECK(turing_decision_verify(st, &b, why, sizeof why) == -5, "tampered evidence record detected (%s)", why); 278 st->ev[k].median_ps = keep; 279 CHECK(turing_decision_verify(st, &b, why, sizeof why) == 0, "restored record verifies"); 280 } 281 turing_store_free(st); 282 } 283 284 static void test_receipt_tamper(void) { 285 char dir[] = "/tmp/turing_tamper_XXXXXX"; 286 CHECK(mkdtemp(dir) != NULL, "mkdtemp"); 287 char a[300], b[300]; 288 snprintf(a, sizeof a, "%s/run1.json", dir); 289 snprintf(b, sizeof b, "%s/run2.json", dir); 290 CHECK(copy_file(R1, a) == 0 && copy_file(R2, b) == 0, "copy receipts"); 291 turing_store *st = load2(a, b); 292 CHECK(st != NULL, "load copies"); 293 if (!st) return; 294 turing_query s4 = {16384, 4096, 300, TURING_PACK_ONCE, 0}; 295 turing_decision d, d2; 296 char why[300]; 297 CHECK(turing_field_select(st, &s4, NULL, &d) == 0 && turing_decision_verify(st, &d, why, sizeof why) == 0, 298 "decision on copies verifies"); 299 CHECK(flip_byte(a, 5000) == 0, "tamper one byte of run1 copy"); 300 int v = turing_decision_verify(st, &d, why, sizeof why); 301 CHECK(v == -8, "tampered receipt -> decision verify fails (%d: %s)", v, why); 302 /* A new decision stops using the tampered receipt. */ 303 CHECK(turing_field_select(st, &s4, NULL, &d2) == 0 && d2.ncite == 40 && 304 turing_decision_verify(st, &d2, why, sizeof why) == 0, 305 "fresh decision cites only the intact receipt (%zu cites)", d2.ncite); 306 CHECK(flip_byte(b, 5000) == 0, "tamper run2 copy too"); 307 turing_decision d3; 308 CHECK(turing_field_select(st, &s4, NULL, &d3) == 1 && d3.chosen < 0 && 309 d3.reason[turing_find_spec(st, "R2c_crumb")] == TURING_R_RECEIPT_UNVERIFIED, 310 "no verified receipt -> no choice, RECEIPT_UNVERIFIED"); 311 unlink(a); 312 unlink(b); 313 rmdir(dir); 314 turing_store_free(st); 315 } 316 317 static void test_history(void) { 318 turing_store *st = load2(R1, R2); 319 if (!st) return; 320 turing_history *h = malloc(sizeof *h), *h2 = malloc(sizeof *h2); 321 turing_decision d; 322 turing_query q = {4096, 64, 300, TURING_PACK_ONCE, 0}; 323 turing_history_init(h, 42); 324 turing_history_init(h2, 43); 325 int seen[TURING_MAX_SPECS] = {0}, order1[10], order2[10], explore = 1; 326 for (int t = 0; t < 10; ++t) { 327 int k = turing_history_select(h, st, &q, &d); 328 explore &= k >= 0 && !strcmp(d.verdict, "EXPLORE"); 329 if (k >= 0) { 330 seen[k]++; 331 order1[t] = k; 332 turing_history_observe(h, st, (size_t)k, q.n, q.m, q.pack, (uint64_t)(1000 + 10 * k)); 333 } 334 int k2 = turing_history_select(h2, st, &q, &d); 335 order2[t] = k2; 336 if (k2 >= 0) turing_history_observe(h2, st, (size_t)k2, q.n, q.m, q.pack, 5); 337 } 338 int once = 1; 339 for (int k = 0; k < 10; ++k) once &= seen[k] == 1; 340 CHECK(explore && once, "cold start: seeded round-robin tries each spec exactly once"); 341 CHECK(memcmp(order1, order2, sizeof order1) != 0, "different seed -> different exploration order"); 342 int k = turing_history_select(h, st, &q, &d); 343 CHECK(k == 0 && !strcmp(d.verdict, "CHOSEN"), "after calibration: minimum running mean (%d)", k); 344 CHECK(turing_decision_verify(st, &d, (char[64]){0}, 64) == -3, "control arm cites no receipts"); 345 free(h); 346 free(h2); 347 turing_store_free(st); 348 } 349 350 static void test_compare(void) { 351 turing_store *st = load2(R1, R2); 352 if (!st) return; 353 turing_query cells[80], all[160]; 354 size_t nc = turing_cells(st, cells, 80), na = 0; 355 for (int pack = 0; pack < 2; ++pack) 356 for (size_t i = 0; i < nc; ++i) all[na] = cells[i], all[na++].pack = pack; 357 turing_query named[8]; 358 size_t nn = 0; 359 const uint64_t shp[4][2] = {{4096, 64}, {16384, 64}, {4096, 4096}, {16384, 4096}}; 360 for (int pack = 0; pack < 2; ++pack) 361 for (int s = 0; s < 4; ++s) named[nn++] = (turing_query){shp[s][0], shp[s][1], 300, pack, 0}; 362 const uint64_t seed = 0x7475726967303031ull; 363 turing_regret r; 364 const char *label[4] = {"RETIRED v0 S1-S4 x2 in-sample", "S1-S4 x2 pack leave-one-out", "72 cells in-sample", 365 "72 cells leave-one-out"}; 366 for (int i = 0; i < 4; ++i) { 367 int rc = turing_compare(st, TURING_FIELD_V0_RETIRED, i < 2 ? named : all, i < 2 ? nn : na, i & 1, seed, &r); 368 CHECK(rc == 0 && r.decisions == (i < 2 ? 8u : 72u), "compare %s", label[i]); 369 CHECK(r.field_cites_ok == r.decisions && r.hist_cites_ok == 0, "cites: Field all verify, control none"); 370 CHECK(r.field_mean >= 0 && r.hist_mean >= 0, "regret non-negative"); 371 printf(" regret %-28s Field mean %6.2f%% max %6.2f%% | control mean %6.2f%% max %6.2f%% | same pick %zu/%zu\n", 372 label[i], 100 * r.field_mean, 100 * r.field_max, 100 * r.hist_mean, 100 * r.hist_max, r.agree, 373 r.decisions); 374 } 375 CHECK(turing_compare(st, TURING_FIELD_V0_RETIRED, named, 4, 0, seed, &r) == 0 && r.field_max == 0.0 && r.hist_max == 0.0, 376 "pack once S1-S4: both selectors pick the oracle"); 377 turing_regret on; 378 CHECK(turing_compare_online(st, TURING_FIELD_V0_RETIRED, all, na, 10, seed, &on) == 0 && on.decisions == 720, "online replay"); 379 printf(" regret %-28s Field mean %6.2f%% max %6.2f%% | control mean %6.2f%% max %6.2f%%\n", "72 cells cold online x10", 380 100 * on.field_mean, 100 * on.field_max, 100 * on.hist_mean, 100 * on.hist_max); 381 turing_regret on2; 382 CHECK(turing_compare_online(st, TURING_FIELD_V0_RETIRED, all, na, 10, seed, &on2) == 0 && on2.hist_mean == on.hist_mean, 383 "online replay deterministic for a seed"); 384 turing_store_free(st); 385 } 386 387 /* The recorded K.6 FAIL stays reproducible: RETIRED v0 tie rule vs control, 388 * pre-registered thresholds (0.02 mean gap, 0.10 max) unchanged. */ 389 static void test_recorded_fail(void) { 390 turing_store *st = load2(R1, R2); 391 if (!st) return; 392 const uint64_t shp[4][2] = {{4096, 64}, {16384, 64}, {4096, 4096}, {16384, 4096}}; 393 turing_query named[8]; 394 size_t nn = 0; 395 for (int pack = 0; pack < 2; ++pack) 396 for (int s = 0; s < 4; ++s) named[nn++] = (turing_query){shp[s][0], shp[s][1], 300, pack, 0}; 397 const uint64_t seed = 0x7475726967303031ull; 398 turing_regret in, loo; 399 CHECK(turing_compare(st, TURING_FIELD_V0_RETIRED, named, nn, 0, seed, &in) == 0 && 400 turing_compare(st, TURING_FIELD_V0_RETIRED, named, nn, 1, seed, &loo) == 0, 401 "RECORDED FAIL: retired v0 comparison runs"); 402 CHECK(llround(in.field_mean * 1e4) == 358 && llround(in.field_max * 1e4) == 2551 && 403 llround(in.hist_mean * 1e4) == 1 && llround(in.hist_max * 1e4) == 7, 404 "RECORDED FAIL reproduced: Field v0 %.2f%%/%.2f%% vs control %.2f%%/%.2f%%", 100 * in.field_mean, 405 100 * in.field_max, 100 * in.hist_mean, 100 * in.hist_max); 406 int q1 = in.field_mean <= in.hist_mean + 0.02 && in.field_max <= 0.10; 407 int q1l = loo.field_mean <= loo.hist_mean + 0.02 && loo.field_max <= 0.10; 408 int q2 = in.field_cites_ok == in.decisions && loo.field_cites_ok == loo.decisions; 409 CHECK(!q1 && !q1l && q2, "RECORDED FAIL: Q1 FAIL (in-sample and leave-one-out), Q2 PASS"); 410 turing_store_free(st); 411 } 412 413 /* Reframe (K.7): Field v1 ranks with the control arm's rule. On every stored 414 * cell, both pack modes, in-sample and leave-one-cell-out, it picks exactly 415 * what the warm control arm picks, and every Field decision verifies. */ 416 static void test_v1_matches_control(void) { 417 turing_store *st = load2(R1, R2); 418 if (!st) return; 419 turing_query cells[80]; 420 size_t nc = turing_cells(st, cells, 80); 421 const uint64_t seed = 0x7475726967303031ull; 422 turing_history *h = malloc(sizeof *h); 423 turing_decision *fd = malloc(sizeof *fd), *hd = malloc(sizeof *hd); 424 if (!h || !fd || !hd) { 425 CHECK(0, "alloc"); 426 free(h), free(fd), free(hd), turing_store_free(st); 427 return; 428 } 429 size_t total = 0, same = 0, verify = 0, sel = 0; 430 for (int loo = 0; loo < 2; ++loo) 431 for (int pack = 0; pack < 2; ++pack) 432 for (size_t i = 0; i < nc; ++i) { 433 turing_query q = cells[i]; 434 q.pack = pack; 435 q.exclude_exact_cell = loo; 436 turing_history_init(h, seed); 437 if (turing_history_load(h, st, loo ? &q : NULL) != 0) continue; 438 int hk = turing_history_select(h, st, &q, hd); 439 if (turing_field_select(st, &q, NULL, fd) != 0) continue; 440 ++total; 441 same += hk >= 0 && fd->chosen == hk && turing_digest_eq(&fd->cand[fd->chosen], &st->spec_id[hk]); 442 char why[200]; 443 verify += turing_decision_verify(st, fd, why, sizeof why) == 0; 444 sel += !strcmp(fd->selector, TURING_FIELD_SELECTOR); 445 } 446 CHECK(total == 144, "Field v1 decided every stored cell x 2 pack x {in-sample, leave-one-out} (%zu/144)", total); 447 CHECK(same == total, "Field v1 and control pick identically on all stored cells (%zu/%zu)", same, total); 448 CHECK(verify == total, "every Field v1 decision verifies against its receipts (%zu/%zu)", verify, total); 449 CHECK(sel == total, "Field v1 decisions are labeled %s", TURING_FIELD_SELECTOR); 450 451 turing_query all[160]; 452 size_t na = 0; 453 for (int pack = 0; pack < 2; ++pack) 454 for (size_t i = 0; i < nc; ++i) all[na] = cells[i], all[na++].pack = pack; 455 for (int loo = 0; loo < 2; ++loo) { 456 turing_regret r; 457 CHECK(turing_compare(st, TURING_FIELD_V1, all, na, loo, seed, &r) == 0 && r.agree == r.decisions && 458 r.field_mean == r.hist_mean && r.field_max == r.hist_max && r.field_cites_ok == r.decisions, 459 "Field v1 regret equals control %s (%.2f%%/%.2f%%), %zu/%zu receipts verify", 460 loo ? "leave-one-out" : "in-sample", 100 * r.field_mean, 100 * r.field_max, r.field_cites_ok, 461 r.decisions); 462 } 463 464 /* A Field v1 record changes digest if a single cite is dropped. */ 465 turing_query s4 = {16384, 4096, 300, TURING_PACK_ONCE, 0}; 466 turing_digest a, b; 467 if (turing_field_select(st, &s4, NULL, fd) == 0 && turing_decision_digest(fd, &a) == 0) { 468 fd->ncite -= 1; 469 CHECK(turing_decision_digest(fd, &b) == 0 && !turing_digest_eq(&a, &b), "decision digest commits to its cites"); 470 } 471 free(h); 472 free(fd); 473 free(hd); 474 turing_store_free(st); 475 } 476 477 int main(void) { 478 test_digests(); 479 test_rebuild_stable(); 480 test_refusals(); 481 test_decisions_verify(); 482 test_receipt_tamper(); 483 test_history(); 484 test_compare(); 485 test_recorded_fail(); 486 test_v1_matches_control(); 487 printf("test-turing: %d passed, %d failed\n", pass, fail); 488 return fail ? 1 : 0; 489 }