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
omega-mv / tools / turing_field.c
| 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 | /* turing-field: TURING Field V0 view over stored receipts (no timed runs). |
| 18 | * |
| 19 | * turing-field [receipt.json ...] history view, winners, selector comparison |
| 20 | * turing-field --spec-ids contract digest + spec ids (rebuild check) |
| 21 | * |
| 22 | * Default receipts: evidence/MIXED_ALGEBRA/ma3_bench_run{1,2}.json. |
| 23 | * docs/turing/TURING_W0_PROPOSAL.md sections F, K. */ |
| 24 | #include "turing/select.h" |
| 25 | |
| 26 | #include <inttypes.h> |
| 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
| 30 | |
| 31 | static const char *const default_receipts[] = {"evidence/MIXED_ALGEBRA/ma3_bench_run1.json", |
| 32 | "evidence/MIXED_ALGEBRA/ma3_bench_run2.json"}; |
| 33 | |
| 34 | /* Pre-registered shapes (proposal K.4), sparsity 0.30. */ |
| 35 | static const struct { |
| 36 | const char *name, *where; |
| 37 | uint64_t n, m; |
| 38 | } shapes[4] = { |
| 39 | {"S1", "L2-fit (256 KiB int8 W)", 4096, 64}, |
| 40 | {"S2", "L2 edge (1 MiB int8 W)", 16384, 64}, |
| 41 | {"S3", "L3/DRAM (16 MiB int8 W)", 4096, 4096}, |
| 42 | {"S4", "DRAM-bound (64 MiB int8 W)", 16384, 4096}, |
| 43 | }; |
| 44 | |
| 45 | static void print_decision(const turing_store *st, const turing_decision *d, const turing_digest *id) { |
| 46 | char h[65], c[13]; |
| 47 | turing_hex(id, h); |
| 48 | printf("decision %s\n", h); |
| 49 | if (d->has_supersedes) { |
| 50 | turing_hex_short(&d->supersedes, c); |
| 51 | printf(" supersedes %s\n", c); |
| 52 | } else { |
| 53 | printf(" supersedes none (root)\n"); |
| 54 | } |
| 55 | turing_hex_short(&d->contract_digest, c); |
| 56 | printf(" selector %s contract %s (PROVISIONAL)\n", d->selector, c); |
| 57 | printf(" query n=%" PRIu64 " m=%" PRIu64 " sparsity=%.2f pack=%s cell %s\n", d->n, d->m, |
| 58 | d->sparsity_milli / 1000.0, d->pack ? "per_call" : "once", d->exact_cell ? "exact" : "nearest"); |
| 59 | if (d->chosen >= 0) { |
| 60 | turing_hex_short(&d->cand[d->chosen], c); |
| 61 | printf(" chosen %s (spec %s) %s margin %.1f%% band %.1f%% tie=%s\n", d->cand_rz[d->chosen], c, |
| 62 | d->verdict, d->margin_ppm / 1e4, d->band_ppm / 1e4, d->tie_resolution); |
| 63 | } |
| 64 | printf(" candidates "); |
| 65 | for (size_t i = 0; i < d->ncand; ++i) |
| 66 | printf(" %s:%s", d->cand_rz[i], turing_reason_name(d->reason[i])); |
| 67 | printf("\n"); |
| 68 | char why[200]; |
| 69 | int v = turing_decision_verify(st, d, why, sizeof why); |
| 70 | printf(" cites %zu evidence records over %zu receipts -> %s\n", d->ncite, st->nreceipt, |
| 71 | v == 0 ? "all receipts verify" : why); |
| 72 | for (size_t r = 0; r < st->nreceipt; ++r) { |
| 73 | turing_hex_short(&st->receipt[r].digest, c); |
| 74 | printf(" receipt %s %s\n", c, st->receipt[r].path); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | static int load(turing_store *st, const char *const *paths, size_t np) { |
| 79 | if (turing_ingest_registry(st) < 0) return -1; |
| 80 | for (size_t i = 0; i < np; ++i) |
| 81 | if (turing_ingest_receipt(st, paths[i]) < 0) { |
| 82 | fprintf(stderr, "turing-field: cannot ingest %s\n", paths[i]); |
| 83 | return -1; |
| 84 | } |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | static void print_regret(const char *label, const turing_regret *r, int checked) { |
| 89 | char fc[32] = "n/a", hc[32] = "n/a"; |
| 90 | if (checked) { |
| 91 | snprintf(fc, sizeof fc, "%zu/%zu", r->field_cites_ok, r->decisions); |
| 92 | snprintf(hc, sizeof hc, "%zu/%zu", r->hist_cites_ok, r->decisions); |
| 93 | } |
| 94 | printf(" %-40s n=%3zu | Field regret mean %6.2f%% max %6.2f%% receipts-verify %-7s | control mean %6.2f%% " |
| 95 | "max %8.2f%% receipts-verify %-7s | same pick %zu\n", |
| 96 | label, r->decisions, 100 * r->field_mean, 100 * r->field_max, fc, 100 * r->hist_mean, 100 * r->hist_max, |
| 97 | hc, r->agree); |
| 98 | } |
| 99 | |
| 100 | int main(int argc, char **argv) { |
| 101 | int spec_ids = argc > 1 && !strcmp(argv[1], "--spec-ids"); |
| 102 | const char *const *paths = default_receipts; |
| 103 | size_t np = 2; |
| 104 | if (!spec_ids && argc > 1) { |
| 105 | paths = (const char *const *)(argv + 1); |
| 106 | np = (size_t)(argc - 1); |
| 107 | } |
| 108 | turing_store *st = turing_store_new(), *st1 = turing_store_new(); |
| 109 | int rc = 1; |
| 110 | if (!st || !st1) goto out; |
| 111 | char h[65], c[13]; |
| 112 | if (spec_ids) { |
| 113 | if (turing_ingest_registry(st) < 0) goto out; |
| 114 | turing_hex(&st->contract_digest, h); |
| 115 | printf("contract %s PROVISIONAL\n", h); |
| 116 | for (size_t k = 0; k < st->nspec; ++k) { |
| 117 | turing_hex(&st->spec_id[k], h); |
| 118 | printf("spec %-12s %s\n", st->spec[k].rz_id, h); |
| 119 | } |
| 120 | rc = 0; |
| 121 | goto out; |
| 122 | } |
| 123 | if (load(st, paths, np) != 0 || load(st1, paths, 1) != 0) goto out; |
| 124 | |
| 125 | turing_hex(&st->contract_digest, h); |
| 126 | printf("TURING Field V0 (post hoc over stored receipts; no timed runs)\n"); |
| 127 | printf("contract_digest %s\n PROVISIONAL: SHA-256(\"%s\" 0x00 || OMG0 contract bytes); not an Omega semantic id\n", |
| 128 | h, TURING_DOMAIN_CONTRACT); |
| 129 | printf("specs %zu (one contract), evidence %zu, receipts %zu\n", st->nspec, st->nev, st->nreceipt); |
| 130 | for (size_t k = 0; k < st->nspec; ++k) { |
| 131 | turing_hex_short(&st->spec_id[k], c); |
| 132 | printf(" %-12s spec %s %s\n", st->spec[k].rz_id, c, st->spec[k].representation); |
| 133 | } |
| 134 | |
| 135 | printf("\n== History view (git log analogue: commit = decision, parent = supersedes, tree = cited evidence,\n" |
| 136 | " blob = receipt). Root decision from the first receipt, then a superseding one from all receipts.\n\n"); |
| 137 | for (size_t s = 0; s < 4; ++s) { |
| 138 | turing_query q = {shapes[s].n, shapes[s].m, 300, TURING_PACK_ONCE, 0}; |
| 139 | turing_decision d1, d2; |
| 140 | turing_digest id1, id2; |
| 141 | if (turing_field_select(st1, &q, NULL, &d1) != 0 || turing_decision_digest(&d1, &id1) != 0) goto out; |
| 142 | if (turing_field_select(st, &q, &id1, &d2) != 0 || turing_decision_digest(&d2, &id2) != 0) goto out; |
| 143 | printf("-- %s %s\n", shapes[s].name, shapes[s].where); |
| 144 | print_decision(st, &d2, &id2); |
| 145 | print_decision(st1, &d1, &id1); |
| 146 | printf("\n"); |
| 147 | } |
| 148 | |
| 149 | printf("== Winners from stored evidence alone (Field selector v1, all receipts)\n"); |
| 150 | for (int pack = 0; pack < 2; ++pack) |
| 151 | for (size_t s = 0; s < 4; ++s) { |
| 152 | turing_query q = {shapes[s].n, shapes[s].m, 300, pack, 0}; |
| 153 | turing_decision d; |
| 154 | if (turing_field_select(st, &q, NULL, &d) != 0) goto out; |
| 155 | printf(" %s %-28s pack=%-8s -> %-11s margin %5.2f%% band %5.2f%%\n", shapes[s].name, shapes[s].where, |
| 156 | pack ? "per_call" : "once", d.cand_rz[d.chosen], d.margin_ppm / 1e4, d.band_ppm / 1e4); |
| 157 | } |
| 158 | |
| 159 | turing_query named[8], all[160]; |
| 160 | size_t nn = 0, na = 0; |
| 161 | for (int pack = 0; pack < 2; ++pack) |
| 162 | for (size_t s = 0; s < 4; ++s) named[nn++] = (turing_query){shapes[s].n, shapes[s].m, 300, pack, 0}; |
| 163 | turing_query cells[80]; |
| 164 | size_t nc = turing_cells(st, cells, 80); |
| 165 | for (int pack = 0; pack < 2; ++pack) |
| 166 | for (size_t i = 0; i < nc; ++i) { |
| 167 | all[na] = cells[i]; |
| 168 | all[na++].pack = pack; |
| 169 | } |
| 170 | const uint64_t seed = 0x7475726967303031ull; /* "turig001" */ |
| 171 | const turing_field_rule V0 = TURING_FIELD_V0_RETIRED, V1 = TURING_FIELD_V1; |
| 172 | turing_regret r; |
| 173 | printf("\n== RECORDED FAIL (K.6): RETIRED Field v0 tie rule vs control arm, kept to reproduce the verdict\n"); |
| 174 | printf(" regret vs oracle = cheapest mean cost of the exact cell\n"); |
| 175 | printf(" seed 0x%016" PRIx64 "; control arm = StarPU-style history per (spec, footprint n x m x pack)\n", seed); |
| 176 | if (turing_compare(st, V0, named, nn, 0, seed, &r) != 0) goto out; |
| 177 | print_regret("S1-S4 x 2 pack, in-sample (warm)", &r, 1); |
| 178 | turing_regret prim = r; |
| 179 | if (turing_compare(st, V0, named, nn, 1, seed, &r) != 0) goto out; |
| 180 | print_regret("S1-S4 x 2 pack, leave-one-cell-out", &r, 1); |
| 181 | turing_regret loo = r; |
| 182 | if (turing_compare(st, V0, named, 4, 0, seed, &r) != 0) goto out; |
| 183 | print_regret(" of which pack once (S1-S4), in-sample", &r, 1); |
| 184 | if (turing_compare(st, V0, named + 4, 4, 0, seed, &r) != 0) goto out; |
| 185 | print_regret(" of which pack per call (S1-S4), in-sample", &r, 1); |
| 186 | if (turing_compare(st, V0, all, na, 0, seed, &r) != 0) goto out; |
| 187 | print_regret("all cells x 2 pack, in-sample", &r, 1); |
| 188 | if (turing_compare(st, V0, all, nc, 0, seed, &r) != 0) goto out; |
| 189 | print_regret(" of which pack once (36 cells), in-sample", &r, 1); |
| 190 | if (turing_compare(st, V0, all + nc, nc, 0, seed, &r) != 0) goto out; |
| 191 | print_regret(" of which per call (36 cells), in-sample", &r, 1); |
| 192 | if (turing_compare(st, V0, all, na, 1, seed, &r) != 0) goto out; |
| 193 | print_regret("all cells x 2 pack, leave-one-cell-out", &r, 1); |
| 194 | if (turing_compare_online(st, V0, all, na, 10, seed, &r) != 0) goto out; |
| 195 | print_regret("all cells x 2 pack, cold online x10", &r, 0); |
| 196 | |
| 197 | /* Pre-registered kill test (proposal K.4), dry run on stored data. */ |
| 198 | const double eq = 0.02, cap = 0.10; |
| 199 | int q1 = prim.field_mean <= prim.hist_mean + eq && prim.field_max <= cap; |
| 200 | int q1l = loo.field_mean <= loo.hist_mean + eq && loo.field_max <= cap; |
| 201 | int q2 = prim.field_cites_ok == prim.decisions && loo.field_cites_ok == loo.decisions; |
| 202 | int better = prim.field_mean < prim.hist_mean - eq || loo.field_mean < loo.hist_mean - eq; |
| 203 | printf("\n== Kill test (pre-registered, dry run on stored data; RETIRED v0 rule, verdict recorded 2026-09-29)\n"); |
| 204 | printf(" Q1 quality in-sample: Field mean <= control mean + 2%% and Field max <= 10%%: %s\n", q1 ? "PASS" : "FAIL"); |
| 205 | printf(" Q1 quality leave-one-out: %s\n", q1l ? "PASS" : "FAIL"); |
| 206 | printf(" Q2 every Field decision's cited receipts verify: %s\n", q2 ? "PASS" : "FAIL"); |
| 207 | printf(" verdict: %s\n", !(q1 && q1l && q2) ? "FAIL (Turing selection is integration-only)" |
| 208 | : better ? "BEAT on selection quality" |
| 209 | : "SURVIVES on explainability at equal quality (not a quality win)"); |
| 210 | |
| 211 | /* Reframe (K.7): Field v1 ranks with the control arm's rule and adds the record. Not a kill-test verdict. */ |
| 212 | printf("\n== Field v1 (record-keeping layer, K.7): same ranking rule as the control arm, plus decision records\n"); |
| 213 | printf(" Not a new kill-test verdict: selection is the control rule and is not claimed as novel.\n"); |
| 214 | struct { const turing_query *q; size_t n; int loo; const char *label; } v1rows[4] = { |
| 215 | {named, nn, 0, "S1-S4 x 2 pack, in-sample"}, {named, nn, 1, "S1-S4 x 2 pack, leave-one-cell-out"}, |
| 216 | {all, na, 0, "all cells x 2 pack, in-sample"}, {all, na, 1, "all cells x 2 pack, leave-one-cell-out"}}; |
| 217 | for (size_t i = 0; i < 4; ++i) { |
| 218 | if (turing_compare(st, V1, v1rows[i].q, v1rows[i].n, v1rows[i].loo, seed, &r) != 0) goto out; |
| 219 | print_regret(v1rows[i].label, &r, 1); |
| 220 | } |
| 221 | rc = 0; |
| 222 | out: |
| 223 | turing_store_free(st); |
| 224 | turing_store_free(st1); |
| 225 | return rc; |
| 226 | } |